drug_stock_api_contorller.go 98KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "XT_New/utils"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/astaxie/beego"
  10. "github.com/jinzhu/gorm"
  11. "reflect"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. type StockDrugApiController struct {
  17. BaseAuthAPIController
  18. }
  19. func DrugStockManagerApiRegistRouters() {
  20. //入库管理和其他入库管理相关接口
  21. beego.Router("/api/drugwarehouse/create", &StockDrugApiController{}, "post:CreateDrugWarehouse")
  22. beego.Router("/api/drugwarehouse/list", &StockDrugApiController{}, "get:GetDrugWarehouseList")
  23. beego.Router("/api/drugwarehouse/delete", &StockDrugApiController{}, "post:DeleteDrugWarehouse")
  24. beego.Router("/api/drugwarehouseinfo/delete", &StockDrugApiController{}, "post:DeleteDrugWarehouseInfo")
  25. beego.Router("/api/drugwarehouseinfo/list", &StockDrugApiController{}, "get:GetDrugWarehouseInfoList")
  26. beego.Router("/api/drugwarehouse/edit", &StockDrugApiController{}, "post:EditDrugWarehouse")
  27. //退货管理和其他退货管理相关接口
  28. beego.Router("/api/drugsalesreturn/create", &StockDrugApiController{}, "post:CreateDrugSalesReturn")
  29. beego.Router("/api/drugsalesreturn/delete", &StockDrugApiController{}, "post:DeleteDrugSalesReturn")
  30. beego.Router("/api/drugsalesreturninfo/delete", &StockDrugApiController{}, "post:DeleteDrugSalesReturnInfo")
  31. beego.Router("/api/drugsalesreturninfo/list", &StockDrugApiController{}, "get:GetDrugSalesReturnInfoList")
  32. beego.Router("/api/drugsalesreturn/list", &StockDrugApiController{}, "get:GetDrugSalesReturnList")
  33. beego.Router("/api/drugsalesreturn/edit", &StockDrugApiController{}, "post:EditDrugReturnInfo")
  34. //出库管理和其他出库管理相关接口
  35. beego.Router("/api/drugwarehouseout/create", &StockDrugApiController{}, "post:CreateDrugWarehouseOut")
  36. beego.Router("/api/drugwarehouseout/delete", &StockDrugApiController{}, "post:DeleteDrugWarehouseOut")
  37. beego.Router("/api/drugwarehouseoutinfo/delete", &StockDrugApiController{}, "post:DeleteDrugWarehouseOutInfo")
  38. beego.Router("/api/drugwarehouseout/list", &StockDrugApiController{}, "get:GetDrugWarehouseOutList")
  39. beego.Router("/api/drugwarehouseout/edit", &StockDrugApiController{}, "post:EditDrugWarehouseOut")
  40. beego.Router("/api/drugwarehouseout/info", &StockDrugApiController{}, "get:GetDrugWarehouseOutInfoList")
  41. //出库退库管理和其他出库退库管理相关接口
  42. beego.Router("/api/drugcancelstock/create", &StockDrugApiController{}, "post:CreateDrugCancelStock")
  43. beego.Router("/api/drugcancelstock/list", &StockDrugApiController{}, "get:GetDrugCancelStockList")
  44. beego.Router("/api/drugcancelstock/info", &StockDrugApiController{}, "get:GetDrugCancelStockInfoList")
  45. beego.Router("/api/drugcancelstock/delete", &StockDrugApiController{}, "post:DeleteDrugCancelStock")
  46. beego.Router("/api/drugcancelstockinfo/delete", &StockDrugApiController{}, "post:DeleteDrugCancelStockInfo")
  47. beego.Router("/api/drugcancelstockinfo/edit", &StockDrugApiController{}, "post:EditDrugCancelStock")
  48. //beego.Router("/api/drugcancelstockinfo/config", &StockDrugApiController{}, "get:GetDrugCancelStockConfig")
  49. ////库存相关全局配置
  50. beego.Router("/api/drugstock/config", &StockDrugApiController{}, "get:GetAllConfig")
  51. //beego.Router("/api/drugreturn/config", &StockDrugApiController{}, "get:GetAllSalesReturnConfig")
  52. //
  53. ////库存查询
  54. beego.Router("/api/drugstock/query", &StockDrugApiController{}, "get:GetDrugQueryInfo")
  55. //
  56. //beego.Router("/api/drugstock/detail", &StockDrugApiController{}, "get:GetDetailInfo")
  57. //
  58. beego.Router("/api/drugwarehouseout/user", &StockDrugApiController{}, "get:GetUserDetailInfo")
  59. beego.Router("/api/drugstock/get", &StockDrugApiController{}, "get:GetAllDrugStock")
  60. beego.Router("/api/drug/getcanceldrugorderprint", &StockDrugApiController{}, "get:GetCancelDrugOrderPrint")
  61. beego.Router("/api/drug/drugcancelexportlist", &StockDrugApiController{}, "get:GetDrugCancelExportList")
  62. beego.Router("/api/drug/getdrugwarehouseinfoprint", &StockDrugApiController{}, "get:GetDrugWarehouseInfoPrint")
  63. beego.Router("/api/drug/getdrugoutorderprint", &StockDrugApiController{}, "get:GetDrugOutOrderPrint")
  64. }
  65. func (c *StockDrugApiController) CreateDrugWarehouse() {
  66. warehousing_time := c.GetString("warehousing_time")
  67. types, _ := c.GetInt64("type", 0)
  68. ctime := time.Now().Unix()
  69. adminUserInfo := c.GetAdminUserInfo()
  70. warehousingDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehousing_time)
  71. if parseDateErr != nil {
  72. c.ErrorLog("日期(%v)解析错误:%v", warehousingDate, parseDateErr)
  73. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  74. return
  75. }
  76. timeStr := time.Now().Format("2006-01-02")
  77. timeArr := strings.Split(timeStr, "-")
  78. total, _ := service.FindAllWarehouseTotalOne(adminUserInfo.CurrentOrgId)
  79. total = total + 1
  80. fmt.Println("total2323232323232", total)
  81. warehousing_order := "YPRKD" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  82. fmt.Println("r入库单据好 2323232322223", warehousing_order)
  83. operation_time := time.Now().Unix()
  84. creater := adminUserInfo.AdminUser.Id
  85. warehousing := models.DrugWarehouse{
  86. WarehousingOrder: warehousing_order,
  87. OperationTime: operation_time,
  88. OrgId: adminUserInfo.CurrentOrgId,
  89. Creater: creater,
  90. Ctime: ctime,
  91. Status: 1,
  92. WarehousingTime: warehousingDate.Unix(),
  93. Type: types,
  94. }
  95. service.AddSigleDrugWarehouse(&warehousing)
  96. dataBody := make(map[string]interface{}, 0)
  97. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  98. if err != nil {
  99. utils.ErrorLog(err.Error())
  100. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  101. return
  102. }
  103. var warehousingInfo []*models.DrugWarehouseInfo
  104. var drugFlow []*models.DrugFlow
  105. if dataBody["stockIn"] != nil && reflect.TypeOf(dataBody["stockIn"]).String() == "[]interface {}" {
  106. thisStockIn, _ := dataBody["stockIn"].([]interface{})
  107. if len(thisStockIn) > 0 {
  108. for _, item := range thisStockIn {
  109. items := item.(map[string]interface{})
  110. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  111. utils.ErrorLog("drug_id")
  112. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  113. return
  114. }
  115. drug_id := int64(items["drug_id"].(float64))
  116. if items["warehousing_count"] == nil || reflect.TypeOf(items["warehousing_count"]).String() != "string" {
  117. utils.ErrorLog("warehousing_count")
  118. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  119. return
  120. }
  121. warehousing_count, _ := strconv.ParseInt(items["warehousing_count"].(string), 10, 64)
  122. if items["last_price"] == nil || reflect.TypeOf(items["last_price"]).String() != "string" {
  123. utils.ErrorLog("last_price")
  124. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  125. return
  126. }
  127. last_price, _ := strconv.ParseFloat(items["last_price"].(string), 64)
  128. total := float64(warehousing_count) * last_price
  129. retail_price_total := float64(warehousing_count) * last_price
  130. var productDates int64
  131. var expiryDates int64
  132. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  133. expiryDates = 0
  134. } else {
  135. if len(items["expiry_date"].(string)) == 0 {
  136. expiryDates = 0
  137. } else {
  138. expiryDate, _ := items["expiry_date"].(string)
  139. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  140. expiryDates = expiry_date.Unix()
  141. }
  142. }
  143. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  144. productDates = 0
  145. } else {
  146. if len(items["product_date"].(string)) == 0 {
  147. productDates = 0
  148. } else {
  149. productDate, _ := items["product_date"].(string)
  150. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  151. productDates = product_date.Unix()
  152. }
  153. }
  154. number, _ := items["number"].(string)
  155. batch_number, _ := items["batch_number"].(string)
  156. if items["max_unit"] == nil || reflect.TypeOf(items["max_unit"]).String() != "string" {
  157. utils.ErrorLog("max_unit")
  158. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  159. return
  160. }
  161. max_unit, _ := items["max_unit"].(string)
  162. if items["min_unit"] == nil || reflect.TypeOf(items["min_unit"]).String() != "string" {
  163. utils.ErrorLog("min_unit")
  164. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  165. return
  166. }
  167. min_unit, _ := items["min_unit"].(string)
  168. var remark string
  169. if items["remark"] == nil || reflect.TypeOf(items["remark"]).String() != "string" {
  170. remark = ""
  171. } else {
  172. remark = items["remark"].(string)
  173. }
  174. manufacturer := int64(items["manufacturer"].(float64))
  175. dealer := int64(items["dealer"].(float64))
  176. warehouseInfo := &models.DrugWarehouseInfo{
  177. WarehousingOrder: warehousing.WarehousingOrder,
  178. WarehousingId: warehousing.ID,
  179. DrugId: drug_id,
  180. Number: number,
  181. ProductDate: productDates,
  182. ExpiryDate: expiryDates,
  183. WarehousingCount: warehousing_count,
  184. Price: last_price,
  185. TotalPrice: total,
  186. Status: 1,
  187. Ctime: ctime,
  188. Remark: remark,
  189. OrgId: adminUserInfo.CurrentOrgId,
  190. Type: types,
  191. Manufacturer: manufacturer,
  192. Dealer: dealer,
  193. StockMaxNumber: warehousing_count,
  194. RetailTotalPrice: retail_price_total,
  195. BatchNumber: batch_number,
  196. MaxUnit: max_unit,
  197. MinUnit: min_unit,
  198. }
  199. warehousingInfo = append(warehousingInfo, warehouseInfo)
  200. drugflow := &models.DrugFlow{
  201. WarehousingOrder: warehousing.WarehousingOrder,
  202. WarehousingId: warehousing.ID,
  203. DrugId: drug_id,
  204. Number: number,
  205. ProductDate: productDates,
  206. ExpireDate: expiryDates,
  207. Count: warehousing_count,
  208. Price: last_price,
  209. Status: 1,
  210. Ctime: ctime,
  211. UserOrgId: adminUserInfo.CurrentOrgId,
  212. Manufacturer: manufacturer,
  213. Dealer: dealer,
  214. BatchNumber: batch_number,
  215. MaxUnit: max_unit,
  216. MinUnit: min_unit,
  217. ConsumableType: 1,
  218. IsEdit: 1,
  219. Creator: adminUserInfo.AdminUser.Id,
  220. IsSys: 0,
  221. }
  222. drugFlow = append(drugFlow, drugflow)
  223. }
  224. }
  225. }
  226. //创建入库单
  227. errs := service.CreateDrugWarehousingInfo(warehousingInfo)
  228. if errs == nil {
  229. //更改库存
  230. var total float64
  231. var min_number int64
  232. for _, item := range warehousingInfo {
  233. //查询刚入库耗材的库存
  234. wareseInfo, _ := service.FindDrugWaresingCount(item.DrugId, item.OrgId)
  235. //更新药品库的总量
  236. info, _ := service.FindeDrugInfo(item.DrugId)
  237. min_number = wareseInfo.StockMaxNumber * info.MinNumber
  238. str := strconv.FormatInt(min_number, 10)
  239. minFloat, _ := strconv.ParseFloat(str, 64)
  240. total = info.Total + minFloat
  241. lib := models.BaseDrugLib{
  242. Total: total,
  243. }
  244. service.UpdateBaseDrug(&lib, item.DrugId)
  245. }
  246. }
  247. //创建流水单
  248. service.CreateDrugFlow(drugFlow)
  249. info, _ := service.FindLastDrugWarehousingInfo(warehousing.WarehousingOrder)
  250. if errs != nil {
  251. utils.ErrorLog(errs.Error())
  252. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockInFail)
  253. return
  254. }
  255. c.ServeSuccessJSON(map[string]interface{}{
  256. "msg": "入库成功",
  257. "warehousing_order": warehousing_order,
  258. "info": info,
  259. })
  260. }
  261. func (c *StockDrugApiController) GetDrugWarehouseList() {
  262. page, _ := c.GetInt64("page", -1)
  263. limit, _ := c.GetInt64("limit", -1)
  264. start_time := c.GetString("start_time")
  265. end_time := c.GetString("end_time")
  266. types, _ := c.GetInt64("type", 0)
  267. keywords := c.GetString("keywords")
  268. timeLayout := "2006-01-02"
  269. loc, _ := time.LoadLocation("Local")
  270. var startTime int64
  271. if len(start_time) > 0 {
  272. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  273. if err != nil {
  274. fmt.Println(err)
  275. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  276. return
  277. }
  278. startTime = theTime.Unix()
  279. }
  280. var endTime int64
  281. if len(end_time) > 0 {
  282. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  283. if err != nil {
  284. utils.ErrorLog(err.Error())
  285. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  286. return
  287. }
  288. endTime = theTime.Unix()
  289. }
  290. adminUserInfo := c.GetAdminUserInfo()
  291. fmt.Println("hhh2h3h23h2h32h3h2h32h32h3h2h3", keywords)
  292. //按药品名称搜索
  293. var ids []int64
  294. var idArray []int64
  295. list, _ := service.GetDrugNameByKeyword(keywords, adminUserInfo.CurrentOrgId)
  296. for _, item := range list {
  297. ids = append(ids, item.ID)
  298. }
  299. //查找入库单详情
  300. if len(ids) > 0 {
  301. info, _ := service.GetDrugWarehouseOrderDetailTwo(ids, adminUserInfo.CurrentOrgId, startTime, endTime)
  302. for _, it := range info {
  303. idArray = append(idArray, it.WarehousingId)
  304. }
  305. }
  306. warehouseList, total, err := service.FindAllDrugWarehousingListOne(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords, idArray)
  307. if err == nil {
  308. c.ServeSuccessJSON(map[string]interface{}{
  309. "list": warehouseList,
  310. "total": total,
  311. })
  312. } else {
  313. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  314. }
  315. }
  316. func (c *StockDrugApiController) GetDrugWarehouseInfoList() {
  317. id, _ := c.GetInt64("id", 0)
  318. if id <= 0 {
  319. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  320. return
  321. }
  322. admin := c.GetAdminUserInfo()
  323. warehousing, err := service.FindDrugWarehousingById(id, admin.CurrentOrgId)
  324. manufacturerList, _ := service.GetAllManufacturerList(admin.CurrentOrgId)
  325. dealerList, _ := service.GetAllDealerList(admin.CurrentOrgId)
  326. if err != nil {
  327. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  328. return
  329. }
  330. if warehousing.ID <= 0 {
  331. return
  332. }
  333. warehousingInfo, err := service.FindDrugWarehousingInfoByIdThree(id, admin.CurrentOrgId)
  334. if err == nil {
  335. c.ServeSuccessJSON(map[string]interface{}{
  336. "info": warehousingInfo,
  337. "warehousing": warehousing,
  338. "manufacturerList": manufacturerList,
  339. "dealerList": dealerList,
  340. })
  341. } else {
  342. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  343. }
  344. }
  345. func (c *StockDrugApiController) EditDrugWarehouse() {
  346. warehousing_time := c.GetString("warehousing_time")
  347. id, _ := c.GetInt64("id", 0)
  348. types, _ := c.GetInt64("type", 0)
  349. if id == 0 {
  350. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  351. return
  352. }
  353. ctime := time.Now().Unix()
  354. mtime := time.Now().Unix()
  355. adminUserInfo := c.GetAdminUserInfo()
  356. warehousingDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehousing_time)
  357. if parseDateErr != nil {
  358. c.ErrorLog("日期(%v)解析错误:%v", warehousingDate, parseDateErr)
  359. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  360. return
  361. }
  362. warehouse, _ := service.FindDrugWarehousingById(id, adminUserInfo.CurrentOrgId)
  363. warehousing := models.DrugWarehouse{
  364. WarehousingOrder: warehouse.WarehousingOrder,
  365. OrgId: adminUserInfo.CurrentOrgId,
  366. Modifier: adminUserInfo.AdminUser.Id,
  367. Mtime: mtime,
  368. Status: 1,
  369. WarehousingTime: warehousingDate.Unix(),
  370. Type: warehouse.Type,
  371. }
  372. service.EditDrugWarehousingOne(warehousing, id)
  373. dataBody := make(map[string]interface{}, 0)
  374. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  375. if err != nil {
  376. utils.ErrorLog(err.Error())
  377. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  378. return
  379. }
  380. var warehousingInfo []*models.DrugWarehouseInfo
  381. var upDateWarehousingInfo []*models.DrugWarehouseInfo
  382. var drugFlow []*models.DrugFlow
  383. if dataBody["stockIn"] != nil && reflect.TypeOf(dataBody["stockIn"]).String() == "[]interface {}" {
  384. thisStockIn, _ := dataBody["stockIn"].([]interface{})
  385. if len(thisStockIn) > 0 {
  386. for _, item := range thisStockIn {
  387. items := item.(map[string]interface{})
  388. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  389. utils.ErrorLog("drug_id")
  390. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  391. return
  392. }
  393. drug_id := int64(items["drug_id"].(float64))
  394. if items["warehousing_count"] == nil || reflect.TypeOf(items["warehousing_count"]).String() != "string" {
  395. utils.ErrorLog("warehousing_count")
  396. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  397. return
  398. }
  399. warehousing_count, _ := strconv.ParseInt(items["warehousing_count"].(string), 10, 64)
  400. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  401. utils.ErrorLog("price")
  402. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  403. return
  404. }
  405. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  406. total := float64(warehousing_count) * price
  407. if items["last_price"] == nil || reflect.TypeOf(items["last_price"]).String() != "string" {
  408. utils.ErrorLog("last_price")
  409. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  410. return
  411. }
  412. last_price, _ := strconv.ParseFloat(items["last_price"].(string), 64)
  413. retail_price_total := float64(warehousing_count) * last_price
  414. var productDates int64
  415. var expiryDates int64
  416. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  417. expiryDates = 0
  418. } else {
  419. if len(items["expiry_date"].(string)) == 0 {
  420. expiryDates = 0
  421. } else {
  422. expiryDate, _ := items["expiry_date"].(string)
  423. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  424. expiryDates = expiry_date.Unix()
  425. }
  426. }
  427. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  428. productDates = 0
  429. } else {
  430. if len(items["product_date"].(string)) == 0 {
  431. productDates = 0
  432. } else {
  433. productDate, _ := items["product_date"].(string)
  434. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  435. productDates = product_date.Unix()
  436. }
  437. }
  438. number, _ := items["number"].(string)
  439. batch_number, _ := items["batch_number"].(string)
  440. max_unit, _ := items["max_unit"].(string)
  441. min_unit, _ := items["min_unit"].(string)
  442. var remark string
  443. if items["remark"] == nil || reflect.TypeOf(items["remark"]).String() != "string" {
  444. remark = ""
  445. } else {
  446. remark = items["remark"].(string)
  447. }
  448. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  449. utils.ErrorLog("id")
  450. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  451. return
  452. }
  453. id := int64(items["id"].(float64))
  454. manufacturer := int64(items["manufacturer"].(float64))
  455. dealer := int64(items["dealer"].(float64))
  456. if id == 0 {
  457. warehouseInfo := &models.DrugWarehouseInfo{
  458. WarehousingOrder: warehouse.WarehousingOrder,
  459. WarehousingId: warehouse.ID,
  460. DrugId: drug_id,
  461. Number: number,
  462. ProductDate: productDates,
  463. ExpiryDate: expiryDates,
  464. WarehousingCount: warehousing_count,
  465. Price: price,
  466. TotalPrice: total,
  467. Status: 1,
  468. Ctime: ctime,
  469. Remark: remark,
  470. OrgId: adminUserInfo.CurrentOrgId,
  471. Type: types,
  472. Manufacturer: manufacturer,
  473. Dealer: dealer,
  474. RetailPrice: last_price,
  475. RetailTotalPrice: retail_price_total,
  476. BatchNumber: batch_number,
  477. MaxUnit: max_unit,
  478. MinUnit: min_unit,
  479. StockMaxNumber: warehousing_count,
  480. }
  481. warehousingInfo = append(warehousingInfo, warehouseInfo)
  482. } else {
  483. warehouseInfo := &models.DrugWarehouseInfo{
  484. ID: id,
  485. WarehousingOrder: warehouse.WarehousingOrder,
  486. WarehousingId: warehouse.ID,
  487. DrugId: drug_id,
  488. Number: number,
  489. ProductDate: productDates,
  490. ExpiryDate: expiryDates,
  491. WarehousingCount: warehousing_count,
  492. Price: price,
  493. TotalPrice: total,
  494. Status: 1,
  495. Ctime: ctime,
  496. Remark: remark,
  497. OrgId: adminUserInfo.CurrentOrgId,
  498. Type: types,
  499. Manufacturer: manufacturer,
  500. Dealer: dealer,
  501. RetailPrice: last_price,
  502. RetailTotalPrice: retail_price_total,
  503. MaxUnit: max_unit,
  504. MinUnit: min_unit,
  505. BatchNumber: batch_number,
  506. }
  507. upDateWarehousingInfo = append(upDateWarehousingInfo, warehouseInfo)
  508. drugflow := &models.DrugFlow{
  509. WarehousingOrder: warehousing.WarehousingOrder,
  510. WarehousingId: id,
  511. DrugId: drug_id,
  512. Number: number,
  513. ProductDate: productDates,
  514. ExpireDate: expiryDates,
  515. Count: warehousing_count,
  516. Price: last_price,
  517. Status: 1,
  518. Ctime: ctime,
  519. UserOrgId: adminUserInfo.CurrentOrgId,
  520. Manufacturer: manufacturer,
  521. Dealer: dealer,
  522. BatchNumber: batch_number,
  523. MaxUnit: max_unit,
  524. MinUnit: min_unit,
  525. ConsumableType: 1,
  526. IsEdit: 1,
  527. Creator: adminUserInfo.AdminUser.Id,
  528. IsSys: 0,
  529. }
  530. drugFlow = append(drugFlow, drugflow)
  531. }
  532. }
  533. }
  534. }
  535. var errs error
  536. if len(warehousingInfo) > 0 {
  537. errs = service.CreateDrugWarehousingInfo(warehousingInfo)
  538. if errs == nil {
  539. //更改库存
  540. service.UpdateDrugInfo(warehousingInfo)
  541. for _, it := range warehousingInfo {
  542. info, _ := service.GetLastDrugWarehouseInfSix(it.DrugId)
  543. flows := models.DrugFlow{
  544. WarehousingOrder: info.WarehousingOrder,
  545. WarehousingId: info.ID,
  546. DrugId: info.DrugId,
  547. Number: info.Number,
  548. ProductDate: info.ProductDate,
  549. ExpireDate: info.ExpiryDate,
  550. Count: info.WarehousingCount,
  551. Price: info.Price,
  552. Status: 1,
  553. Ctime: ctime,
  554. UserOrgId: adminUserInfo.CurrentOrgId,
  555. Manufacturer: info.Manufacturer,
  556. Dealer: info.Dealer,
  557. BatchNumber: info.BatchNumber,
  558. MaxUnit: info.MaxUnit,
  559. MinUnit: "",
  560. ConsumableType: 1,
  561. IsEdit: 1,
  562. Creator: adminUserInfo.AdminUser.Id,
  563. IsSys: 0,
  564. }
  565. _, existerrcodes := service.GetDrugFlowIsExist(info.ID, info.DrugId)
  566. if existerrcodes == gorm.ErrRecordNotFound {
  567. service.CreateDrugFlowOne(flows)
  568. }
  569. }
  570. }
  571. }
  572. if len(upDateWarehousingInfo) > 0 {
  573. var total int64
  574. var minNumber float64
  575. var allTotal int64
  576. var chaTotal int64
  577. for _, item := range upDateWarehousingInfo {
  578. //查询修改的记录
  579. orderInfo, _ := service.GetDrugWarehouseOrderOne(id, item.DrugId)
  580. fmt.Println("hh23323232323232332233232", orderInfo)
  581. info, _ := service.FindeDrugInfo(item.DrugId)
  582. //历史入库总数
  583. total = orderInfo.StockMaxNumber * info.MinNumber
  584. //当前入库总数
  585. allTotal = item.WarehousingCount * info.MinNumber
  586. //如果历史的入库总数 大于当前入库总数 则需要扣减
  587. if total > allTotal {
  588. chaTotal = total - allTotal
  589. str := strconv.FormatInt(chaTotal, 10)
  590. minFloat, _ := strconv.ParseFloat(str, 64)
  591. minNumber = info.Total - minFloat
  592. lib := models.BaseDrugLib{
  593. Total: minNumber,
  594. }
  595. service.UpdateBaseDrug(&lib, item.DrugId)
  596. }
  597. //如果历史的入库总数 小于当前入库总数 则需增加库存
  598. if total < allTotal {
  599. chaTotal = allTotal - total
  600. str := strconv.FormatInt(chaTotal, 10)
  601. minFloat, _ := strconv.ParseFloat(str, 64)
  602. minNumber = info.Total + minFloat
  603. lib := models.BaseDrugLib{
  604. Total: minNumber,
  605. }
  606. service.UpdateBaseDrug(&lib, item.DrugId)
  607. }
  608. warehouseInfo, _ := service.FindeLastWarehouseInfo(item.ID)
  609. fmt.Println("hhhhh23hh32h232332323223", warehouseInfo)
  610. var total int64
  611. //比较大小(加)
  612. // 如果当前的入库数量 大于 历史入库数量
  613. if item.WarehousingCount > warehouseInfo.WarehousingCount {
  614. total = item.WarehousingCount - warehouseInfo.WarehousingCount
  615. item.StockMaxNumber = warehouseInfo.StockMaxNumber + total
  616. errs = service.UpDateDrugWarehousingInfo(item)
  617. if len(drugFlow) > 0 {
  618. for _, it := range drugFlow {
  619. fmt.Println(it)
  620. flow := models.DrugFlow{
  621. Count: item.StockMaxNumber,
  622. }
  623. parseDateErr := service.UpdateDrugFlow(flow, item.DrugId, warehouseInfo.WarehousingId)
  624. fmt.Println(parseDateErr)
  625. }
  626. }
  627. break
  628. }
  629. // 如果当前的入库数量 小于 历史入库数量
  630. if item.WarehousingCount < warehouseInfo.WarehousingCount {
  631. total = warehouseInfo.WarehousingCount - item.WarehousingCount
  632. item.StockMaxNumber = warehouseInfo.StockMaxNumber - total
  633. errs = service.UpDateDrugWarehousingInfo(item)
  634. if len(drugFlow) > 0 {
  635. for _, it := range drugFlow {
  636. fmt.Println(it)
  637. flow := models.DrugFlow{
  638. Count: item.StockMaxNumber,
  639. }
  640. parseDateErr := service.UpdateDrugFlow(flow, item.DrugId, warehouseInfo.WarehousingId)
  641. fmt.Println(parseDateErr)
  642. }
  643. }
  644. break
  645. }
  646. if item.WarehousingCount == warehouseInfo.WarehousingCount {
  647. errs = service.UpDateDrugWarehousingInfo(item)
  648. }
  649. }
  650. }
  651. if errs != nil {
  652. utils.ErrorLog(errs.Error())
  653. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockInFail)
  654. return
  655. }
  656. c.ServeSuccessJSON(map[string]interface{}{
  657. "msg": "编辑成功",
  658. })
  659. }
  660. func (c *StockDrugApiController) DeleteDrugWarehouse() {
  661. ids := c.GetString("ids")
  662. if len(ids) == 0 {
  663. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  664. return
  665. }
  666. idArray := strings.Split(ids, ",")
  667. err := service.DeleteDrugWarehouse(idArray)
  668. if err != nil {
  669. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  670. } else {
  671. c.ServeSuccessJSON(map[string]interface{}{
  672. "msg": "删除成功",
  673. })
  674. }
  675. }
  676. func (c *StockDrugApiController) DeleteDrugWarehouseInfo() {
  677. id, _ := c.GetInt64("id", 0)
  678. if id == 0 {
  679. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  680. return
  681. }
  682. err := service.UpDateDrugWarehouseStatus(id)
  683. if err != nil {
  684. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  685. } else {
  686. c.ServeSuccessJSON(map[string]interface{}{
  687. "msg": "删除成功",
  688. })
  689. }
  690. }
  691. func (c *StockDrugApiController) CreateDrugSalesReturn() {
  692. dealer_id, _ := c.GetInt64("dealer_id", 0)
  693. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  694. sales_return_time := c.GetString("time")
  695. types, _ := c.GetInt64("type", 0)
  696. salesReturnDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", sales_return_time)
  697. if parseDateErr != nil {
  698. c.ErrorLog("日期(%v)解析错误:%v", salesReturnDate, parseDateErr)
  699. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  700. return
  701. }
  702. adminUserInfo := c.GetAdminUserInfo()
  703. operation_time := time.Now().Unix()
  704. creater := adminUserInfo.AdminUser.Id
  705. ctime := time.Now().Unix()
  706. timeStr := time.Now().Format("2006-01-02")
  707. timeArr := strings.Split(timeStr, "-")
  708. total, _ := service.FindAllSalesReturnTotal(adminUserInfo.CurrentOrgId)
  709. total = total + 1
  710. orderNumber := "YPTHD" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  711. salesReturn := models.DrugSalesReturn{
  712. OrderNumber: orderNumber,
  713. OperaTime: operation_time,
  714. OrgId: adminUserInfo.CurrentOrgId,
  715. Creater: creater,
  716. Ctime: ctime,
  717. Status: 1,
  718. ReturnTime: salesReturnDate.Unix(),
  719. Dealer: dealer_id,
  720. Manufacturer: manufacturer_id,
  721. Type: types,
  722. }
  723. service.AddSigleDrugSalesReturn(&salesReturn)
  724. dataBody := make(map[string]interface{}, 0)
  725. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  726. if err != nil {
  727. utils.ErrorLog(err.Error())
  728. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  729. return
  730. }
  731. var salesReturnInfos []*models.DrugSalesReturnInfo
  732. if dataBody["salesReturn"] != nil && reflect.TypeOf(dataBody["salesReturn"]).String() == "[]interface {}" {
  733. thisStockIn, _ := dataBody["salesReturn"].([]interface{})
  734. if len(thisStockIn) > 0 {
  735. for _, item := range thisStockIn {
  736. items := item.(map[string]interface{})
  737. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  738. utils.ErrorLog("drug_id")
  739. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  740. return
  741. }
  742. drug_id := int64(items["drug_id"].(float64))
  743. return_count, _ := items["return_count"].(string)
  744. if len(return_count) == 0 {
  745. utils.ErrorLog("len(return_count) == 0")
  746. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  747. return
  748. }
  749. count, _ := strconv.ParseInt(return_count, 10, 64)
  750. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  751. utils.ErrorLog("price")
  752. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  753. return
  754. }
  755. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  756. total := float64(count) * price
  757. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  758. utils.ErrorLog("retail_price")
  759. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  760. return
  761. }
  762. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  763. retail_price_total := float64(count) * retail_price
  764. salesReturnInfo := &models.DrugSalesReturnInfo{
  765. OrderNumber: salesReturn.OrderNumber,
  766. SalesReturnId: salesReturn.ID,
  767. DrugId: drug_id,
  768. Count: count,
  769. Price: price,
  770. Total: total,
  771. Status: 1,
  772. Ctime: ctime,
  773. OrgId: adminUserInfo.CurrentOrgId,
  774. Type: types,
  775. Manufacturer: manufacturer_id,
  776. Dealer: dealer_id,
  777. RetailPrice: retail_price,
  778. RetailTotalPrice: retail_price_total,
  779. }
  780. salesReturnInfos = append(salesReturnInfos, salesReturnInfo)
  781. }
  782. }
  783. }
  784. errs := service.CreateDrugSalesReturnInfo(salesReturnInfos)
  785. if errs != nil {
  786. utils.ErrorLog(errs.Error())
  787. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateReturnFail)
  788. return
  789. }
  790. c.ServeSuccessJSON(map[string]interface{}{
  791. "msg": "退货成功",
  792. })
  793. }
  794. func (c *StockDrugApiController) GetDrugSalesReturnList() {
  795. page, _ := c.GetInt64("page", -1)
  796. limit, _ := c.GetInt64("limit", -1)
  797. start_time := c.GetString("start_time")
  798. end_time := c.GetString("end_time")
  799. types, _ := c.GetInt64("type", 0)
  800. keywords := c.GetString("keywords")
  801. timeLayout := "2006-01-02"
  802. loc, _ := time.LoadLocation("Local")
  803. var startTime int64
  804. if len(start_time) > 0 {
  805. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  806. if err != nil {
  807. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  808. return
  809. }
  810. startTime = theTime.Unix()
  811. }
  812. var endTime int64
  813. if len(end_time) > 0 {
  814. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  815. if err != nil {
  816. utils.ErrorLog(err.Error())
  817. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  818. return
  819. }
  820. endTime = theTime.Unix()
  821. }
  822. adminUserInfo := c.GetAdminUserInfo()
  823. returnList, total, err := service.FindAllDrugReturnList(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords)
  824. if err == nil {
  825. c.ServeSuccessJSON(map[string]interface{}{
  826. "list": returnList,
  827. "total": total,
  828. })
  829. } else {
  830. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  831. }
  832. }
  833. func (this *StockDrugApiController) DeleteDrugSalesReturn() {
  834. ids := this.GetString("ids")
  835. if len(ids) == 0 {
  836. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  837. return
  838. }
  839. idArray := strings.Split(ids, ",")
  840. err := service.DeleteDrugSalesReturn(idArray)
  841. if err != nil {
  842. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  843. } else {
  844. this.ServeSuccessJSON(map[string]interface{}{
  845. "msg": "删除成功",
  846. })
  847. }
  848. }
  849. func (this *StockDrugApiController) DeleteDrugSalesReturnInfo() {
  850. id, _ := this.GetInt64("id", 0)
  851. fmt.Println(id)
  852. if id == 0 {
  853. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  854. return
  855. }
  856. err := service.UpDateDrugSaleReturnStatus(id)
  857. if err != nil {
  858. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  859. } else {
  860. this.ServeSuccessJSON(map[string]interface{}{
  861. "msg": "删除成功",
  862. })
  863. }
  864. }
  865. func (this *StockDrugApiController) GetDrugSalesReturnInfoList() {
  866. id, _ := this.GetInt64("id", 0)
  867. adminUserInfo := this.GetAdminUserInfo()
  868. list, _ := service.FindAllDrugSalesReturnInfoById(id, adminUserInfo.CurrentOrgId)
  869. salesReturn, _ := service.FindAllDrugSalesReturnById(id, adminUserInfo.CurrentOrgId)
  870. this.ServeSuccessJSON(map[string]interface{}{
  871. "list": list,
  872. "salesReturn": salesReturn,
  873. })
  874. }
  875. func (c *StockDrugApiController) EditDrugReturnInfo() {
  876. return_time := c.GetString("return_time")
  877. id, _ := c.GetInt64("id", 0)
  878. types, _ := c.GetInt64("type", 0)
  879. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  880. dealer_id, _ := c.GetInt64("dealer_id", 0)
  881. if id == 0 {
  882. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  883. return
  884. }
  885. ctime := time.Now().Unix()
  886. mtime := time.Now().Unix()
  887. adminUserInfo := c.GetAdminUserInfo()
  888. returnDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", return_time)
  889. if parseDateErr != nil {
  890. c.ErrorLog("日期(%v)解析错误:%v", returnDate, parseDateErr)
  891. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  892. return
  893. }
  894. salesReturns, _ := service.FindDrugSalesReturnById(id)
  895. sales := models.DrugSalesReturn{
  896. ID: salesReturns.ID,
  897. Mtime: mtime,
  898. ReturnTime: returnDate.Unix(),
  899. Manufacturer: manufacturer_id,
  900. Dealer: dealer_id,
  901. }
  902. service.EditDrugSaleReturn(sales)
  903. dataBody := make(map[string]interface{}, 0)
  904. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  905. if err != nil {
  906. utils.ErrorLog(err.Error())
  907. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  908. return
  909. }
  910. var returnInfos []*models.DrugSalesReturnInfo
  911. var upDateReturnInfos []*models.DrugSalesReturnInfo
  912. if dataBody["salesReturn"] != nil && reflect.TypeOf(dataBody["salesReturn"]).String() == "[]interface {}" {
  913. thisStockIn, _ := dataBody["salesReturn"].([]interface{})
  914. if len(thisStockIn) > 0 {
  915. for _, item := range thisStockIn {
  916. items := item.(map[string]interface{})
  917. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  918. utils.ErrorLog("drug_id")
  919. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  920. return
  921. }
  922. drug_id := int64(items["drug_id"].(float64))
  923. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  924. utils.ErrorLog("count")
  925. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  926. return
  927. }
  928. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  929. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  930. utils.ErrorLog("id")
  931. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  932. return
  933. }
  934. id := int64(items["id"].(float64))
  935. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  936. utils.ErrorLog("price")
  937. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  938. return
  939. }
  940. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  941. total := float64(count) * price
  942. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  943. utils.ErrorLog("retail_price")
  944. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  945. return
  946. }
  947. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  948. retail_price_total := float64(count) * retail_price
  949. if id == 0 {
  950. returnInfo := &models.DrugSalesReturnInfo{
  951. DrugId: drug_id,
  952. Count: count,
  953. Status: 1,
  954. Ctime: ctime,
  955. OrgId: adminUserInfo.CurrentOrgId,
  956. OrderNumber: sales.OrderNumber,
  957. SalesReturnId: sales.ID,
  958. Mtime: time.Now().Unix(),
  959. Type: types,
  960. Manufacturer: manufacturer_id,
  961. Dealer: dealer_id,
  962. RetailPrice: retail_price,
  963. RetailTotalPrice: retail_price_total,
  964. Total: total,
  965. Price: price,
  966. }
  967. returnInfos = append(returnInfos, returnInfo)
  968. } else {
  969. returnInfo := &models.DrugSalesReturnInfo{
  970. ID: id,
  971. DrugId: drug_id,
  972. Count: count,
  973. Status: 1,
  974. Ctime: ctime,
  975. OrgId: adminUserInfo.CurrentOrgId,
  976. OrderNumber: sales.OrderNumber,
  977. SalesReturnId: sales.ID,
  978. Mtime: time.Now().Unix(),
  979. Type: types,
  980. Manufacturer: manufacturer_id,
  981. Dealer: dealer_id,
  982. RetailPrice: retail_price,
  983. RetailTotalPrice: retail_price_total,
  984. Total: total,
  985. Price: price,
  986. }
  987. upDateReturnInfos = append(upDateReturnInfos, returnInfo)
  988. }
  989. }
  990. }
  991. }
  992. var errs error
  993. if len(returnInfos) > 0 {
  994. errs = service.CreateDrugSalesReturnInfo(returnInfos)
  995. }
  996. if len(upDateReturnInfos) > 0 {
  997. for _, item := range upDateReturnInfos {
  998. errs = service.UpDateDrugSalesReturnInfo(item)
  999. }
  1000. }
  1001. if errs != nil {
  1002. utils.ErrorLog(errs.Error())
  1003. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateReturnFail)
  1004. return
  1005. }
  1006. c.ServeSuccessJSON(map[string]interface{}{
  1007. "msg": "编辑成功",
  1008. })
  1009. }
  1010. func (c *StockDrugApiController) CreateDrugWarehouseOut() {
  1011. types, _ := c.GetInt64("type", 0)
  1012. ctime := time.Now().Unix()
  1013. adminUserInfo := c.GetAdminUserInfo()
  1014. warehousing_out_time := c.GetString("warehousing_out_time")
  1015. warehousingOutDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehousing_out_time)
  1016. if parseDateErr != nil {
  1017. c.ErrorLog("日期(%v)解析错误:%v", warehousingOutDate, parseDateErr)
  1018. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1019. return
  1020. }
  1021. timeStr := time.Now().Format("2006-01-02")
  1022. timeArr := strings.Split(timeStr, "-")
  1023. total, _ := service.FindAllDrugWarehouseOut(adminUserInfo.CurrentOrgId)
  1024. total = total + 1
  1025. warehousing_out_order := strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  1026. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  1027. number = number + total
  1028. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  1029. operation_time := time.Now().Unix()
  1030. creater := adminUserInfo.AdminUser.Id
  1031. warehouseOut := models.DrugWarehouseOut{
  1032. WarehouseOutOrderNumber: warehousing_out_order,
  1033. OperationTime: operation_time,
  1034. OrgId: adminUserInfo.CurrentOrgId,
  1035. Creater: creater,
  1036. Ctime: ctime,
  1037. Status: 1,
  1038. WarehouseOutTime: warehousingOutDate.Unix(),
  1039. Type: types,
  1040. }
  1041. dataBody := make(map[string]interface{}, 0)
  1042. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1043. if err != nil {
  1044. utils.ErrorLog(err.Error())
  1045. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1046. return
  1047. }
  1048. var warehousingOutInfo []*models.DrugWarehouseOutInfo
  1049. //var drugFlow []*models.DrugFlow
  1050. if dataBody["stockOut"] != nil && reflect.TypeOf(dataBody["stockOut"]).String() == "[]interface {}" {
  1051. thisStockIn, _ := dataBody["stockOut"].([]interface{})
  1052. if len(thisStockIn) > 0 {
  1053. for _, item := range thisStockIn {
  1054. items := item.(map[string]interface{})
  1055. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1056. utils.ErrorLog("drug_id")
  1057. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1058. return
  1059. }
  1060. drug_id := int64(items["drug_id"].(float64))
  1061. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  1062. utils.ErrorLog("count")
  1063. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1064. return
  1065. }
  1066. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1067. utils.ErrorLog("price")
  1068. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1069. return
  1070. }
  1071. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1072. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  1073. total := float64(count) * price
  1074. retail_price_total := float64(count) * price
  1075. remark := items["remark"].(string)
  1076. max_unit := items["max_unit"].(string)
  1077. dealer := int64(items["dealer"].(float64))
  1078. manufacturer := int64(items["manufacturer"].(float64))
  1079. number := items["number"].(string)
  1080. batch_number := items["batch_number"].(string)
  1081. warehouse_info_id := int64(items["warehouse_info_id"].(float64))
  1082. var productDates int64
  1083. var expiryDates int64
  1084. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  1085. expiryDates = 0
  1086. } else {
  1087. if len(items["expiry_date"].(string)) == 0 {
  1088. expiryDates = 0
  1089. } else {
  1090. expiryDate, _ := items["expiry_date"].(string)
  1091. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  1092. expiryDates = expiry_date.Unix()
  1093. }
  1094. }
  1095. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  1096. productDates = 0
  1097. } else {
  1098. if len(items["product_date"].(string)) == 0 {
  1099. productDates = 0
  1100. } else {
  1101. productDate, _ := items["product_date"].(string)
  1102. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  1103. productDates = product_date.Unix()
  1104. }
  1105. }
  1106. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1107. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1108. WarehouseOutId: warehouseOut.ID,
  1109. DrugId: drug_id,
  1110. Count: count,
  1111. Price: price,
  1112. TotalPrice: total,
  1113. Status: 1,
  1114. Ctime: ctime,
  1115. Remark: remark,
  1116. OrgId: adminUserInfo.CurrentOrgId,
  1117. Type: types,
  1118. Manufacturer: manufacturer,
  1119. Dealer: dealer,
  1120. RetailPrice: price,
  1121. RetailTotalPrice: retail_price_total,
  1122. CountUnit: max_unit,
  1123. ExpiryDate: expiryDates,
  1124. ProductDate: productDates,
  1125. Number: number,
  1126. BatchNumber: batch_number,
  1127. IsSys: 0,
  1128. WarehouseInfoId: warehouse_info_id,
  1129. }
  1130. warehousingOutInfo = append(warehousingOutInfo, warehouseOutInfo)
  1131. //drugflow := &models.DrugFlow{
  1132. // WarehouseOutId: warehouseOut.ID,
  1133. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1134. // DrugId: drug_id,
  1135. // Number: number,
  1136. // ProductDate: productDates,
  1137. // ExpireDate: expiryDates,
  1138. // Count: count,
  1139. // Price: price,
  1140. // Status: 1,
  1141. // Ctime: ctime,
  1142. // UserOrgId: adminUserInfo.CurrentOrgId,
  1143. // Manufacturer: manufacturer,
  1144. // Dealer: dealer,
  1145. // BatchNumber: batch_number,
  1146. // MaxUnit: max_unit,
  1147. // ConsumableType: 2,
  1148. // IsEdit: 1,
  1149. // Creator: adminUserInfo.AdminUser.Id,
  1150. // IsSys: 0,
  1151. //}
  1152. //drugFlow = append(drugFlow, drugflow)
  1153. }
  1154. }
  1155. }
  1156. var total_count int64 //总库存
  1157. var prescribing_number_total int64 //出库数据
  1158. //调用出库逻辑
  1159. for _, item := range warehousingOutInfo {
  1160. //获取药品库存
  1161. info, _ := service.GetDrugTotalCountTwo(item.DrugId)
  1162. for _, it := range info {
  1163. total_count += it.StockMaxNumber*it.MinNumber + it.StockMinNumber
  1164. }
  1165. //查询改药品信息
  1166. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  1167. //判断单位是否相等
  1168. if medical.MaxUnit == item.CountUnit {
  1169. //转化为最小单位
  1170. prescribing_number_total = item.Count * medical.MinNumber
  1171. }
  1172. if medical.MinUnit == item.CountUnit {
  1173. prescribing_number_total = item.Count
  1174. }
  1175. fmt.Println("药品ID", item.DrugId)
  1176. fmt.Println("数量一", prescribing_number_total)
  1177. fmt.Println("数量二", total_count)
  1178. //判断单位
  1179. if total_count == 0 {
  1180. goodObj, _ := service.GetDrugByGoodId(item.DrugId)
  1181. c.ServeSuccessJSON(map[string]interface{}{
  1182. "msg": "1",
  1183. "drug_name": goodObj.DrugName,
  1184. "dose": goodObj.Dose,
  1185. "dose_unit": goodObj.DoseUnit,
  1186. "min_number": goodObj.MinNumber,
  1187. "min_unit": goodObj.MinUnit,
  1188. "max_unit": goodObj.MaxUnit,
  1189. })
  1190. return
  1191. }
  1192. if prescribing_number_total > total_count {
  1193. goodObj, _ := service.GetDrugByGoodId(item.DrugId)
  1194. c.ServeSuccessJSON(map[string]interface{}{
  1195. "msg": "1",
  1196. "drug_name": goodObj.DrugName,
  1197. "dose": goodObj.Dose,
  1198. "dose_unit": goodObj.DoseUnit,
  1199. "min_number": goodObj.MinNumber,
  1200. "min_unit": goodObj.MinUnit,
  1201. "max_unit": goodObj.MaxUnit,
  1202. })
  1203. return
  1204. } else {
  1205. service.AddSigleDrugWarehouseOut(&warehouseOut)
  1206. //list, _ := service.GetLastWarehouseOut(adminUserInfo.CurrentOrgId)
  1207. //for _, item := range drugFlow {
  1208. // item.WarehouseOutId = list.ID
  1209. //}
  1210. //service.CreateDrugFlow(drugFlow)
  1211. // 出库流程
  1212. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  1213. drup, _ := service.FindBaseDrugLibRecord(item.OrgId, item.DrugId)
  1214. if drup.ID > 0 {
  1215. prescribingNumber := item.Count
  1216. service.AutoDrugDeliverInfo(item.OrgId, prescribingNumber, &warehouseOut, &drup, item)
  1217. }
  1218. prescribing_number_total = 0
  1219. total_count = 0
  1220. }
  1221. }
  1222. c.ServeSuccessJSON(map[string]interface{}{
  1223. "msg": "2",
  1224. "drug_name": "",
  1225. "dose": "",
  1226. "dose_unit": "",
  1227. "min_number": "",
  1228. "min_unit": "",
  1229. "max_unit": "",
  1230. })
  1231. return
  1232. }
  1233. func (c *StockDrugApiController) GetDrugWarehouseOutList() {
  1234. page, _ := c.GetInt64("page", -1)
  1235. limit, _ := c.GetInt64("limit", -1)
  1236. start_time := c.GetString("start_time")
  1237. end_time := c.GetString("end_time")
  1238. types, _ := c.GetInt64("type", 0)
  1239. keywords := c.GetString("keywords")
  1240. timeLayout := "2006-01-02"
  1241. loc, _ := time.LoadLocation("Local")
  1242. var startTime int64
  1243. if len(start_time) > 0 {
  1244. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  1245. if err != nil {
  1246. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1247. return
  1248. }
  1249. startTime = theTime.Unix()
  1250. }
  1251. var endTime int64
  1252. if len(end_time) > 0 {
  1253. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  1254. if err != nil {
  1255. utils.ErrorLog(err.Error())
  1256. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1257. return
  1258. }
  1259. endTime = theTime.Unix()
  1260. }
  1261. adminUserInfo := c.GetAdminUserInfo()
  1262. var ids []int64
  1263. var goodids []int64
  1264. if len(keywords) > 0 {
  1265. //查询商品名称
  1266. list, _ := service.GetDrugNameByKeyword(keywords, adminUserInfo.CurrentOrgId)
  1267. for _, item := range list {
  1268. goodids = append(goodids, item.ID)
  1269. }
  1270. if len(goodids) > 0 {
  1271. //出库详情但里面查询
  1272. info, _ := service.GetDrugWarehouseOrderDetail(goodids, adminUserInfo.CurrentOrgId, startTime, endTime)
  1273. for _, it := range info {
  1274. ids = append(ids, it.WarehouseOutId)
  1275. }
  1276. }
  1277. }
  1278. warehouseOutList, total, err := service.FindAllDrugWarehouseOutListOne(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords, ids)
  1279. fmt.Println(err)
  1280. if err == nil {
  1281. c.ServeSuccessJSON(map[string]interface{}{
  1282. "list": warehouseOutList,
  1283. "total": total,
  1284. })
  1285. } else {
  1286. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  1287. }
  1288. }
  1289. func (c *StockDrugApiController) DeleteDrugWarehouseOut() {
  1290. ids := c.GetString("ids")
  1291. if len(ids) == 0 {
  1292. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1293. return
  1294. }
  1295. idArray := strings.Split(ids, ",")
  1296. err := service.DeleteDrugWarehouseOut(idArray)
  1297. if err != nil {
  1298. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  1299. } else {
  1300. c.ServeSuccessJSON(map[string]interface{}{
  1301. "msg": "删除成功",
  1302. })
  1303. }
  1304. }
  1305. func (this *StockDrugApiController) DeleteDrugWarehouseOutInfo() {
  1306. id, _ := this.GetInt64("id", 0)
  1307. fmt.Println(id)
  1308. if id == 0 {
  1309. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1310. return
  1311. }
  1312. err := service.UpDateDrugWarehouseOutStatus(id)
  1313. if err != nil {
  1314. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  1315. } else {
  1316. this.ServeSuccessJSON(map[string]interface{}{
  1317. "msg": "删除成功",
  1318. })
  1319. }
  1320. }
  1321. func (c *StockDrugApiController) GetDrugWarehouseOutInfoList() {
  1322. id, _ := c.GetInt64("id", 0)
  1323. adminInfo := c.GetAdminUserInfo()
  1324. warehouseOutInfo, _ := service.FindDrugWarehouseOutInfoById(id, adminInfo.CurrentOrgId)
  1325. warehouseOut, _ := service.FindDrugWareHouseOutById(id, adminInfo.CurrentOrgId)
  1326. manulist, _ := service.GetAllManufacturerList(adminInfo.CurrentOrgId)
  1327. dealerList, _ := service.GetAllDealerList(adminInfo.CurrentOrgId)
  1328. c.ServeSuccessJSON(map[string]interface{}{
  1329. "list": warehouseOutInfo,
  1330. "info": warehouseOut,
  1331. "manulist": manulist,
  1332. "dealerList": dealerList,
  1333. })
  1334. }
  1335. func (c *StockDrugApiController) EditDrugWarehouseOut() {
  1336. warehouse_out_time := c.GetString("warehouse_out_time")
  1337. id, _ := c.GetInt64("id", 0)
  1338. types, _ := c.GetInt64("type", 0)
  1339. if id == 0 {
  1340. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1341. return
  1342. }
  1343. ctime := time.Now().Unix()
  1344. mtime := time.Now().Unix()
  1345. adminUserInfo := c.GetAdminUserInfo()
  1346. warehouseOutDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehouse_out_time)
  1347. if parseDateErr != nil {
  1348. c.ErrorLog("日期(%v)解析错误:%v", warehouseOutDate, parseDateErr)
  1349. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1350. return
  1351. }
  1352. warehouseOut, _ := service.FindDrugWareHouseOutById(id, adminUserInfo.CurrentOrgId)
  1353. tempWarehouseOut := models.DrugWarehouseOut{
  1354. ID: warehouseOut.ID,
  1355. Mtime: mtime,
  1356. WarehouseOutTime: warehouseOutDate.Unix(),
  1357. }
  1358. service.EditDrugWarehouseOut(tempWarehouseOut)
  1359. dataBody := make(map[string]interface{}, 0)
  1360. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1361. if err != nil {
  1362. utils.ErrorLog(err.Error())
  1363. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1364. return
  1365. }
  1366. var warehousingOutInfo []*models.DrugWarehouseOutInfo
  1367. var upDateWarehouseOutInfos []*models.DrugWarehouseOutInfo
  1368. var drugFlow []*models.DrugFlow
  1369. if dataBody["stockOut"] != nil && reflect.TypeOf(dataBody["stockOut"]).String() == "[]interface {}" {
  1370. thisStockOut, _ := dataBody["stockOut"].([]interface{})
  1371. if len(thisStockOut) > 0 {
  1372. for _, item := range thisStockOut {
  1373. items := item.(map[string]interface{})
  1374. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1375. utils.ErrorLog("drug_id")
  1376. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1377. return
  1378. }
  1379. drug_id := int64(items["drug_id"].(float64))
  1380. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  1381. utils.ErrorLog("count")
  1382. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1383. return
  1384. }
  1385. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  1386. count_unit, _ := items["count_unit"].(string)
  1387. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1388. utils.ErrorLog("price")
  1389. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1390. return
  1391. }
  1392. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1393. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  1394. utils.ErrorLog("retail_price")
  1395. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1396. return
  1397. }
  1398. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  1399. total := float64(count) * price
  1400. retail_price_total := float64(count) * retail_price
  1401. remark := items["remark"].(string)
  1402. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  1403. utils.ErrorLog("id")
  1404. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1405. return
  1406. }
  1407. id := int64(items["id"].(float64))
  1408. dealer := int64(items["dealer"].(float64))
  1409. manufacturer := int64(items["manufacturer"].(float64))
  1410. batch_number := items["batch_number"].(string)
  1411. number := items["number"].(string)
  1412. max_unit := items["count_unit"].(string)
  1413. warehouse_info_id := int64(items["warehouse_info_id"].(float64))
  1414. var productDates int64
  1415. var expiryDates int64
  1416. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  1417. expiryDates = 0
  1418. } else {
  1419. if len(items["expiry_date"].(string)) == 0 {
  1420. expiryDates = 0
  1421. } else {
  1422. expiryDate, _ := items["expiry_date"].(string)
  1423. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  1424. expiryDates = expiry_date.Unix()
  1425. }
  1426. }
  1427. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  1428. productDates = 0
  1429. } else {
  1430. if len(items["product_date"].(string)) == 0 {
  1431. productDates = 0
  1432. } else {
  1433. productDate, _ := items["product_date"].(string)
  1434. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  1435. productDates = product_date.Unix()
  1436. }
  1437. }
  1438. if id == 0 {
  1439. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1440. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1441. WarehouseOutId: warehouseOut.ID,
  1442. DrugId: drug_id,
  1443. Count: count,
  1444. Price: price,
  1445. TotalPrice: total,
  1446. Status: 1,
  1447. Ctime: ctime,
  1448. Remark: remark,
  1449. OrgId: adminUserInfo.CurrentOrgId,
  1450. Type: types,
  1451. Manufacturer: manufacturer,
  1452. Dealer: dealer,
  1453. IsSys: 0,
  1454. SysRecordTime: 0,
  1455. RetailPrice: retail_price,
  1456. RetailTotalPrice: retail_price_total,
  1457. BatchNumber: batch_number,
  1458. Number: number,
  1459. ProductDate: productDates,
  1460. ExpiryDate: expiryDates,
  1461. CountUnit: count_unit,
  1462. WarehouseInfoId: warehouse_info_id,
  1463. }
  1464. warehousingOutInfo = append(warehousingOutInfo, warehouseOutInfo)
  1465. drugflow := &models.DrugFlow{
  1466. WarehousingOrder: "",
  1467. WarehousingId: 0,
  1468. DrugId: drug_id,
  1469. Number: number,
  1470. ProductDate: productDates,
  1471. ExpireDate: expiryDates,
  1472. Count: count,
  1473. Price: price,
  1474. Status: 1,
  1475. Ctime: ctime,
  1476. UserOrgId: adminUserInfo.CurrentOrgId,
  1477. Manufacturer: manufacturer,
  1478. Dealer: dealer,
  1479. BatchNumber: batch_number,
  1480. MaxUnit: max_unit,
  1481. ConsumableType: 1,
  1482. IsEdit: 1,
  1483. Creator: adminUserInfo.AdminUser.Id,
  1484. IsSys: 0,
  1485. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1486. WarehouseOutId: id,
  1487. }
  1488. drugFlow = append(drugFlow, drugflow)
  1489. } else {
  1490. if items["is_sys"] == nil || reflect.TypeOf(items["is_sys"]).String() != "float64" {
  1491. utils.ErrorLog("is_sys")
  1492. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1493. return
  1494. }
  1495. is_sys := int64(items["is_sys"].(float64))
  1496. if items["sys_record_time"] == nil || reflect.TypeOf(items["sys_record_time"]).String() != "float64" {
  1497. utils.ErrorLog("sys_record_time")
  1498. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1499. return
  1500. }
  1501. sys_record_time := int64(items["sys_record_time"].(float64))
  1502. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1503. ID: id,
  1504. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1505. WarehouseOutId: warehouseOut.ID,
  1506. DrugId: drug_id,
  1507. Count: count,
  1508. Price: price,
  1509. TotalPrice: total,
  1510. Status: 1,
  1511. Ctime: ctime,
  1512. Remark: remark,
  1513. OrgId: adminUserInfo.CurrentOrgId,
  1514. Mtime: time.Now().Unix(),
  1515. Type: types,
  1516. Manufacturer: manufacturer,
  1517. Dealer: dealer,
  1518. IsSys: is_sys,
  1519. SysRecordTime: sys_record_time,
  1520. RetailPrice: retail_price,
  1521. RetailTotalPrice: retail_price_total,
  1522. BatchNumber: batch_number,
  1523. Number: number,
  1524. ProductDate: productDates,
  1525. ExpiryDate: expiryDates,
  1526. CountUnit: count_unit,
  1527. WarehouseInfoId: warehouse_info_id,
  1528. }
  1529. upDateWarehouseOutInfos = append(upDateWarehouseOutInfos, warehouseOutInfo)
  1530. drugflow := &models.DrugFlow{
  1531. WarehousingOrder: "",
  1532. WarehousingId: 0,
  1533. DrugId: drug_id,
  1534. Number: number,
  1535. ProductDate: productDates,
  1536. ExpireDate: expiryDates,
  1537. Count: count,
  1538. Price: price,
  1539. Status: 1,
  1540. Ctime: ctime,
  1541. UserOrgId: adminUserInfo.CurrentOrgId,
  1542. Manufacturer: manufacturer,
  1543. Dealer: dealer,
  1544. BatchNumber: batch_number,
  1545. MaxUnit: max_unit,
  1546. ConsumableType: 1,
  1547. IsEdit: 1,
  1548. Creator: adminUserInfo.AdminUser.Id,
  1549. IsSys: 0,
  1550. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1551. WarehouseOutId: warehouseOut.ID,
  1552. }
  1553. drugFlow = append(drugFlow, drugflow)
  1554. }
  1555. }
  1556. }
  1557. }
  1558. var errs error
  1559. if len(warehousingOutInfo) > 0 {
  1560. var total_count int64
  1561. var prescribing_number_total int64
  1562. //调用出库逻辑
  1563. for _, item := range warehousingOutInfo {
  1564. //获取药品库存
  1565. info, _ := service.GetDrugTotalCount(item.DrugId, item.OrgId)
  1566. //查询改药品信息
  1567. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  1568. //判断单位是否相等
  1569. if medical.MaxUnit == item.CountUnit {
  1570. //转化为最小单位
  1571. total_count = info.Count * medical.MinNumber
  1572. prescribing_number_total = item.Count * medical.MinNumber
  1573. }
  1574. if medical.MinUnit == item.CountUnit {
  1575. total_count = info.Count
  1576. prescribing_number_total = item.Count
  1577. }
  1578. fmt.Println("数量一", prescribing_number_total)
  1579. fmt.Println("数量二", total_count)
  1580. //判断单位
  1581. if total_count == 0 {
  1582. goodObj, _ := service.GetDrugByGoodId(item.DrugId)
  1583. c.ServeSuccessJSON(map[string]interface{}{
  1584. "msg": "1",
  1585. "drug_name": goodObj.DrugName,
  1586. "dose": goodObj.Dose,
  1587. "dose_unit": goodObj.DoseUnit,
  1588. "min_number": goodObj.MinNumber,
  1589. "min_unit": goodObj.MinUnit,
  1590. "max_unit": goodObj.MaxUnit,
  1591. })
  1592. return
  1593. }
  1594. if prescribing_number_total > total_count {
  1595. goodObj, _ := service.GetDrugByGoodId(item.DrugId)
  1596. c.ServeSuccessJSON(map[string]interface{}{
  1597. "msg": "1",
  1598. "drug_name": goodObj.DrugName,
  1599. "dose": goodObj.Dose,
  1600. "dose_unit": goodObj.DoseUnit,
  1601. "min_number": goodObj.MinNumber,
  1602. "min_unit": goodObj.MinUnit,
  1603. "max_unit": goodObj.MaxUnit,
  1604. })
  1605. return
  1606. } else {
  1607. errs = service.CreateDrugWarehousingOutInfo(warehousingOutInfo)
  1608. service.CreateDrugFlow(drugFlow)
  1609. // 出库流程
  1610. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  1611. drup, _ := service.FindBaseDrugLibRecord(item.OrgId, item.DrugId)
  1612. if drup.ID > 0 {
  1613. prescribingNumber := item.Count
  1614. service.AutoDrugDeliverInfo(item.OrgId, prescribingNumber, &tempWarehouseOut, &drup, item)
  1615. }
  1616. }
  1617. }
  1618. }
  1619. if len(upDateWarehouseOutInfos) > 0 {
  1620. for _, item := range upDateWarehouseOutInfos {
  1621. //获取药品库存
  1622. info, _ := service.GetDrugTotalCount(item.DrugId, item.OrgId)
  1623. //查询改药品信息
  1624. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  1625. //查询最后一次出库记录
  1626. outInfo, _ := service.GetLastDrugWarehouseOutInfo(item.DrugId, item.WarehouseOutId, item.OrgId)
  1627. var min_number int64 //最后一次库存
  1628. var max_number int64 //当前库存
  1629. var all_number int64 //总库存
  1630. var cha_number int64 //库存差
  1631. var maxNumber int64
  1632. var minNumber int64
  1633. if medical.MaxUnit == outInfo.CountUnit {
  1634. //转为最小单位
  1635. min_number = outInfo.Count * medical.MinNumber
  1636. }
  1637. if medical.MinUnit == outInfo.CountUnit {
  1638. min_number = outInfo.Count
  1639. }
  1640. fmt.Println("单位1", medical.MaxUnit)
  1641. fmt.Println("单位2", item.CountUnit)
  1642. if medical.MaxUnit == item.CountUnit {
  1643. max_number = item.Count * medical.MinNumber
  1644. }
  1645. if medical.MinUnit == item.CountUnit {
  1646. max_number = item.Count
  1647. }
  1648. all_number = info.Count*medical.MinNumber + info.StockMinNumber
  1649. ////比较当前出库数量 和 最后一次出库数量,正常出库
  1650. if max_number <= min_number {
  1651. errs = service.UpDateDrugWarehouseOutInfo(item)
  1652. cha_number = min_number - max_number
  1653. if item.CountUnit == medical.MaxUnit {
  1654. maxNumber = cha_number / medical.MinNumber
  1655. parseDateErr := service.UpdateWarehouseInfo(maxNumber, item.DrugId, item.OrgId)
  1656. if parseDateErr != nil {
  1657. utils.ErrorLog(errs.Error())
  1658. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1659. return
  1660. }
  1661. c.ServeSuccessJSON(map[string]interface{}{
  1662. "msg": "2",
  1663. "drug_name": "",
  1664. "dose": "",
  1665. "dose_unit": "",
  1666. "min_number": "",
  1667. "min_unit": "",
  1668. "max_unit": "",
  1669. })
  1670. minNumber = cha_number % medical.MinNumber
  1671. parseDateErr = service.UpdateWarehouseInfoOne(minNumber, item.DrugId, item.OrgId)
  1672. if parseDateErr != nil {
  1673. utils.ErrorLog(errs.Error())
  1674. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1675. return
  1676. }
  1677. c.ServeSuccessJSON(map[string]interface{}{
  1678. "msg": "2",
  1679. "drug_name": "",
  1680. "dose": "",
  1681. "dose_unit": "",
  1682. "min_number": "",
  1683. "min_unit": "",
  1684. "max_unit": "",
  1685. })
  1686. }
  1687. if item.CountUnit == medical.MinUnit {
  1688. parseDateErr := service.UpdateWarehouseInfoOne(cha_number, item.DrugId, item.OrgId)
  1689. if parseDateErr != nil {
  1690. utils.ErrorLog(errs.Error())
  1691. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1692. return
  1693. }
  1694. c.ServeSuccessJSON(map[string]interface{}{
  1695. "msg": "2",
  1696. "drug_name": "",
  1697. "dose": "",
  1698. "dose_unit": "",
  1699. "min_number": "",
  1700. "min_unit": "",
  1701. "max_unit": "",
  1702. })
  1703. }
  1704. }
  1705. //退库操作
  1706. if max_number > min_number {
  1707. cha_number = min_number - max_number
  1708. //如果总库存大于差,正常出库
  1709. if all_number >= cha_number {
  1710. errs = service.UpDateDrugWarehouseOutInfo(item)
  1711. if item.CountUnit == medical.MaxUnit {
  1712. maxNumber = cha_number / medical.MinNumber
  1713. parseDateErr := service.UpdateWarehouseInfo(maxNumber, item.DrugId, item.OrgId)
  1714. fmt.Println("parseDateErr", parseDateErr)
  1715. if parseDateErr != nil {
  1716. utils.ErrorLog(errs.Error())
  1717. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1718. return
  1719. }
  1720. c.ServeSuccessJSON(map[string]interface{}{
  1721. "msg": "2",
  1722. "drug_name": "",
  1723. "dose": "",
  1724. "dose_unit": "",
  1725. "min_number": "",
  1726. "min_unit": "",
  1727. "max_unit": "",
  1728. })
  1729. minNumber = cha_number % medical.MinNumber
  1730. parseDateErr = service.UpdateWarehouseInfoOne(minNumber, item.DrugId, item.OrgId)
  1731. if parseDateErr != nil {
  1732. utils.ErrorLog(errs.Error())
  1733. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1734. return
  1735. }
  1736. c.ServeSuccessJSON(map[string]interface{}{
  1737. "msg": "2",
  1738. "drug_name": "",
  1739. "dose": "",
  1740. "dose_unit": "",
  1741. "min_number": "",
  1742. "min_unit": "",
  1743. "max_unit": "",
  1744. })
  1745. }
  1746. if item.CountUnit == medical.MinUnit {
  1747. parseDateErr := service.UpdateWarehouseInfoOne(cha_number, item.DrugId, item.OrgId)
  1748. if parseDateErr != nil {
  1749. utils.ErrorLog(errs.Error())
  1750. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1751. return
  1752. }
  1753. c.ServeSuccessJSON(map[string]interface{}{
  1754. "msg": "2",
  1755. "drug_name": "",
  1756. "dose": "",
  1757. "dose_unit": "",
  1758. "min_number": "",
  1759. "min_unit": "",
  1760. "max_unit": "",
  1761. })
  1762. }
  1763. }
  1764. if all_number < cha_number {
  1765. goodObj, _ := service.GetDrugByGoodId(item.DrugId)
  1766. c.ServeSuccessJSON(map[string]interface{}{
  1767. "msg": "1",
  1768. "drug_name": goodObj.DrugName,
  1769. "dose": goodObj.Dose,
  1770. "dose_unit": goodObj.DoseUnit,
  1771. "min_number": goodObj.MinNumber,
  1772. "min_unit": goodObj.MinUnit,
  1773. "max_unit": goodObj.MaxUnit,
  1774. })
  1775. return
  1776. }
  1777. }
  1778. }
  1779. }
  1780. if errs != nil {
  1781. utils.ErrorLog(errs.Error())
  1782. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1783. return
  1784. }
  1785. c.ServeSuccessJSON(map[string]interface{}{
  1786. "msg": "2",
  1787. "drug_name": "",
  1788. "dose": "",
  1789. "dose_unit": "",
  1790. "min_number": "",
  1791. "min_unit": "",
  1792. "max_unit": "",
  1793. })
  1794. }
  1795. func (c *StockDrugApiController) CreateDrugCancelStock() {
  1796. dealer_id, _ := c.GetInt64("dealer_id", 0)
  1797. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  1798. cancel_stock_time := c.GetString("time")
  1799. types, _ := c.GetInt64("type", 0)
  1800. cancelStockDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", cancel_stock_time)
  1801. if parseDateErr != nil {
  1802. c.ErrorLog("日期(%v)解析错误:%v", cancelStockDate, parseDateErr)
  1803. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1804. return
  1805. }
  1806. adminUserInfo := c.GetAdminUserInfo()
  1807. operation_time := time.Now().Unix()
  1808. creater := adminUserInfo.AdminUser.Id
  1809. ctime := time.Now().Unix()
  1810. timeStr := time.Now().Format("2006-01-02")
  1811. timeArr := strings.Split(timeStr, "-")
  1812. total, _ := service.FindAllDrugCancelStockTotal(adminUserInfo.CurrentOrgId)
  1813. total = total + 1
  1814. orderNumber := "CKTKD" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1815. cancelStock := models.DrugCancelStock{
  1816. OrderNumber: orderNumber,
  1817. OperaTime: operation_time,
  1818. OrgId: adminUserInfo.CurrentOrgId,
  1819. Creater: creater,
  1820. Ctime: ctime,
  1821. Status: 1,
  1822. ReturnTime: cancelStockDate.Unix(),
  1823. Dealer: dealer_id,
  1824. Manufacturer: manufacturer_id,
  1825. Type: types,
  1826. }
  1827. service.AddSigleDrugCancelStock(&cancelStock)
  1828. dataBody := make(map[string]interface{}, 0)
  1829. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1830. if err != nil {
  1831. utils.ErrorLog(err.Error())
  1832. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1833. return
  1834. }
  1835. var cancelStockInfos []*models.DrugCancelStockInfo
  1836. var drugFlow []*models.DrugFlow
  1837. if dataBody["cancelStock"] != nil && reflect.TypeOf(dataBody["cancelStock"]).String() == "[]interface {}" {
  1838. thisStockIn, _ := dataBody["cancelStock"].([]interface{})
  1839. if len(thisStockIn) > 0 {
  1840. for _, item := range thisStockIn {
  1841. items := item.(map[string]interface{})
  1842. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1843. utils.ErrorLog("drug_id")
  1844. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1845. return
  1846. }
  1847. drug_id := int64(items["drug_id"].(float64))
  1848. return_count, _ := items["return_count"].(string)
  1849. if len(return_count) == 0 {
  1850. utils.ErrorLog("len(return_count) == 0")
  1851. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1852. return
  1853. }
  1854. count, _ := strconv.ParseInt(return_count, 10, 64)
  1855. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1856. utils.ErrorLog("price")
  1857. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1858. return
  1859. }
  1860. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1861. total := float64(count) * price
  1862. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  1863. utils.ErrorLog("retail_price")
  1864. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1865. return
  1866. }
  1867. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  1868. retail_price_total := float64(count) * retail_price
  1869. register_account, _ := items["register_account"].(string)
  1870. remark, _ := items["remark"].(string)
  1871. var productDates int64
  1872. var expiryDates int64
  1873. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  1874. expiryDates = 0
  1875. } else {
  1876. if len(items["expiry_date"].(string)) == 0 {
  1877. expiryDates = 0
  1878. } else {
  1879. expiryDate, _ := items["expiry_date"].(string)
  1880. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  1881. expiryDates = expiry_date.Unix()
  1882. }
  1883. }
  1884. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  1885. productDates = 0
  1886. } else {
  1887. if len(items["product_date"].(string)) == 0 {
  1888. productDates = 0
  1889. } else {
  1890. productDate, _ := items["product_date"].(string)
  1891. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  1892. productDates = product_date.Unix()
  1893. }
  1894. }
  1895. manufacturer, _ := items["manufacturer"].(string)
  1896. dealer, _ := items["dealer"].(string)
  1897. batch_number := items["batch_number"].(string)
  1898. max_unit := items["max_unit"].(string)
  1899. batch_number_id := int64(items["batch_number_id"].(float64))
  1900. var manufacturer_id int64
  1901. var dealer_id int64
  1902. manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
  1903. dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
  1904. for _, item := range manufacturerList {
  1905. if manufacturer == item.ManufacturerName {
  1906. manufacturer_id = item.ID
  1907. }
  1908. }
  1909. for _, item := range dealerList {
  1910. if dealer == item.DealerName {
  1911. dealer_id = item.ID
  1912. }
  1913. }
  1914. cancelInfo, _ := service.GetLastDrugCancelStockById(adminUserInfo.CurrentOrgId)
  1915. cancelStockInfo := &models.DrugCancelStockInfo{
  1916. OrderNumber: cancelInfo.OrderNumber,
  1917. CancelStockId: cancelInfo.ID,
  1918. DrugId: drug_id,
  1919. Count: count,
  1920. Status: 1,
  1921. Ctime: ctime,
  1922. OrgId: adminUserInfo.CurrentOrgId,
  1923. Type: types,
  1924. Manufacturer: manufacturer,
  1925. Dealer: dealer,
  1926. Total: total,
  1927. RetailPrice: retail_price,
  1928. RetailTotalPrice: retail_price_total,
  1929. Price: price,
  1930. RegisterAccount: register_account,
  1931. Remark: remark,
  1932. BatchNumber: batch_number,
  1933. MaxUnit: max_unit,
  1934. ProductDate: productDates,
  1935. ExpiryDate: expiryDates,
  1936. BatchNumberId: batch_number_id,
  1937. }
  1938. cancelStockInfos = append(cancelStockInfos, cancelStockInfo)
  1939. flow := &models.DrugFlow{
  1940. WarehousingId: 0,
  1941. DrugId: drug_id,
  1942. Number: "",
  1943. BatchNumber: batch_number,
  1944. Count: count,
  1945. UserOrgId: adminUserInfo.CurrentOrgId,
  1946. PatientId: 0,
  1947. SystemTime: ctime,
  1948. ConsumableType: 4,
  1949. IsSys: 0,
  1950. WarehousingOrder: "",
  1951. WarehouseOutId: 0,
  1952. WarehouseOutOrderNumber: "",
  1953. IsEdit: 0,
  1954. CancelStockId: cancelStock.ID,
  1955. CancelOrderNumber: cancelStock.OrderNumber,
  1956. Manufacturer: manufacturer_id,
  1957. Dealer: dealer_id,
  1958. Creator: adminUserInfo.AdminUser.Id,
  1959. UpdateCreator: 0,
  1960. Status: 1,
  1961. Ctime: time.Now().Unix(),
  1962. Mtime: 0,
  1963. Price: price,
  1964. WarehousingDetailId: 0,
  1965. WarehouseOutDetailId: 0,
  1966. CancelOutDetailId: 0,
  1967. ExpireDate: expiryDates,
  1968. ProductDate: productDates,
  1969. MaxUnit: max_unit,
  1970. MinUnit: "",
  1971. }
  1972. drugFlow = append(drugFlow, flow)
  1973. }
  1974. }
  1975. }
  1976. //扣减库存逻辑
  1977. for _, item := range cancelStockInfos {
  1978. var total_number int64
  1979. var out_number int64
  1980. var can_number int64
  1981. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  1982. //将当前退库数转为最小单位
  1983. if item.MaxUnit == medical.MaxUnit {
  1984. total_number = item.Count * medical.MinNumber
  1985. }
  1986. if item.MaxUnit == medical.MinUnit {
  1987. total_number = item.Count
  1988. }
  1989. //查询当前药品退库的批次号的总入库数
  1990. infoWareInfo, _ := service.GetDrugWarehouseInfo(item.BatchNumberId)
  1991. var total_count int64
  1992. total_count = infoWareInfo.StockMaxNumber*medical.MinNumber + infoWareInfo.StockMinNumber
  1993. fmt.Println("当前批次总库存", total_count)
  1994. //查询该批次的出库数量
  1995. outInfo, _ := service.GetDrugWarehouseOutInfo(item.BatchNumberId, item.DrugId)
  1996. //查询该批次总的退库数量
  1997. canInfo, _ := service.GetCancelDrugStockOutInfo(item.BatchNumberId, item.DrugId)
  1998. for _, item := range canInfo {
  1999. if item.MaxUnit == medical.MaxUnit {
  2000. item.Count = item.Count * medical.MinNumber
  2001. }
  2002. if item.MaxUnit == medical.MinUnit {
  2003. item.Count = item.Count
  2004. }
  2005. }
  2006. for _, item := range canInfo {
  2007. can_number += item.Count
  2008. }
  2009. for _, item := range outInfo {
  2010. if item.CountUnit == medical.MaxUnit {
  2011. item.Count = item.Count * medical.MinNumber
  2012. }
  2013. if item.CountUnit == medical.MinUnit {
  2014. item.Count = item.Count
  2015. }
  2016. }
  2017. for _, item := range outInfo {
  2018. out_number += item.Count
  2019. }
  2020. if out_number == 0 {
  2021. service.UpdateDrugCancel(item.CancelStockId)
  2022. c.ServeSuccessJSON(map[string]interface{}{
  2023. "msg": "2",
  2024. })
  2025. return
  2026. }
  2027. //判断退库数量是否大于总入库数量
  2028. if total_number > total_count {
  2029. service.UpdateDrugCancel(item.CancelStockId)
  2030. c.ServeSuccessJSON(map[string]interface{}{
  2031. "msg": "2",
  2032. })
  2033. return
  2034. }
  2035. //判断总退库数量是否大于出库数量
  2036. fmt.Println("该批次总退库数量", can_number+total_number)
  2037. fmt.Println("总出库数量", out_number)
  2038. if can_number+total_number > out_number {
  2039. //清空退库单
  2040. service.UpdateDrugCancel(item.CancelStockId)
  2041. c.ServeSuccessJSON(map[string]interface{}{
  2042. "msg": "2",
  2043. })
  2044. return
  2045. }
  2046. fmt.Println("当前退库数量", total_number)
  2047. fmt.Println("总出口数量", out_number)
  2048. //正常退库
  2049. if total_number <= out_number {
  2050. //创建退库详情
  2051. errs := service.CreateDrugCancelStockInfo(cancelStockInfos)
  2052. //创建库存明细
  2053. errs = service.CreateDrugFlow(drugFlow)
  2054. var total int64
  2055. if medical.MaxUnit == item.MaxUnit {
  2056. total = item.Count
  2057. warehouseInfo := models.XtDrugWarehouseInfo{
  2058. StockMaxNumber: total,
  2059. }
  2060. errs = service.UpdateDrugWarehouseInfo(&warehouseInfo, item.BatchNumberId)
  2061. if errs != nil {
  2062. utils.ErrorLog(errs.Error())
  2063. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2064. return
  2065. }
  2066. c.ServeSuccessJSON(map[string]interface{}{
  2067. "msg": "1",
  2068. })
  2069. }
  2070. if medical.MinUnit == item.MaxUnit {
  2071. total = item.Count
  2072. warehouseInfo := models.XtDrugWarehouseInfo{
  2073. StockMinNumber: total,
  2074. }
  2075. errs = service.UpdateDrugWarehouseInfoOne(&warehouseInfo, item.BatchNumberId)
  2076. if errs != nil {
  2077. utils.ErrorLog(errs.Error())
  2078. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2079. return
  2080. }
  2081. c.ServeSuccessJSON(map[string]interface{}{
  2082. "msg": "1",
  2083. })
  2084. }
  2085. }
  2086. }
  2087. }
  2088. func (c *StockDrugApiController) GetDrugCancelStockInfoList() {
  2089. id, _ := c.GetInt64("id", 0)
  2090. adminUserInfo := c.GetAdminUserInfo()
  2091. cancelStockInfoList, _ := service.FindDrugCancelStockInfoById(id, adminUserInfo.CurrentOrgId)
  2092. info, _ := service.FindCancelDrugById(id, adminUserInfo.CurrentOrgId)
  2093. c.ServeSuccessJSON(map[string]interface{}{
  2094. "list": cancelStockInfoList,
  2095. "info": info,
  2096. })
  2097. }
  2098. func (c *StockDrugApiController) GetDrugCancelStockList() {
  2099. page, _ := c.GetInt64("page", -1)
  2100. limit, _ := c.GetInt64("limit", -1)
  2101. start_time := c.GetString("start_time")
  2102. end_time := c.GetString("end_time")
  2103. types, _ := c.GetInt64("type", 0)
  2104. keywords := c.GetString("keywords")
  2105. timeLayout := "2006-01-02"
  2106. loc, _ := time.LoadLocation("Local")
  2107. var startTime int64
  2108. if len(start_time) > 0 {
  2109. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  2110. if err != nil {
  2111. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2112. return
  2113. }
  2114. startTime = theTime.Unix()
  2115. }
  2116. var endTime int64
  2117. if len(end_time) > 0 {
  2118. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  2119. if err != nil {
  2120. utils.ErrorLog(err.Error())
  2121. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2122. return
  2123. }
  2124. endTime = theTime.Unix()
  2125. }
  2126. adminUserInfo := c.GetAdminUserInfo()
  2127. returnList, total, err := service.FindAllDrugCancelList(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords)
  2128. if err == nil {
  2129. c.ServeSuccessJSON(map[string]interface{}{
  2130. "list": returnList,
  2131. "total": total,
  2132. })
  2133. } else {
  2134. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  2135. }
  2136. }
  2137. func (this *StockDrugApiController) DeleteDrugCancelStock() {
  2138. ids := this.GetString("ids")
  2139. if len(ids) == 0 {
  2140. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2141. return
  2142. }
  2143. idArray := strings.Split(ids, ",")
  2144. err := service.DeleteDrugCancelStock(idArray)
  2145. if err != nil {
  2146. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  2147. } else {
  2148. this.ServeSuccessJSON(map[string]interface{}{
  2149. "msg": "删除成功",
  2150. })
  2151. }
  2152. }
  2153. func (this *StockDrugApiController) DeleteDrugCancelStockInfo() {
  2154. id, _ := this.GetInt64("id", 0)
  2155. fmt.Println(id)
  2156. if id == 0 {
  2157. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2158. return
  2159. }
  2160. err := service.UpDateDrugCancleStockStatus(id)
  2161. if err != nil {
  2162. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  2163. } else {
  2164. this.ServeSuccessJSON(map[string]interface{}{
  2165. "msg": "删除成功",
  2166. })
  2167. }
  2168. }
  2169. func (c *StockDrugApiController) EditDrugCancelStock() {
  2170. cancel_time := c.GetString("cancel_time")
  2171. id, _ := c.GetInt64("id", 0)
  2172. types, _ := c.GetInt64("type", 0)
  2173. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  2174. dealer_id, _ := c.GetInt64("dealer_id", 0)
  2175. if id == 0 {
  2176. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2177. return
  2178. }
  2179. ctime := time.Now().Unix()
  2180. mtime := time.Now().Unix()
  2181. adminUserInfo := c.GetAdminUserInfo()
  2182. cancelDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", cancel_time)
  2183. if parseDateErr != nil {
  2184. c.ErrorLog("日期(%v)解析错误:%v", cancelDate, parseDateErr)
  2185. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2186. return
  2187. }
  2188. cancelStock, _ := service.FindDrugCancelStockById(id)
  2189. tempCancelStock := models.DrugCancelStock{
  2190. ID: cancelStock.ID,
  2191. Mtime: mtime,
  2192. ReturnTime: cancelDate.Unix(),
  2193. Manufacturer: manufacturer_id,
  2194. Dealer: dealer_id,
  2195. }
  2196. service.EditDrugCancelStock(tempCancelStock)
  2197. dataBody := make(map[string]interface{}, 0)
  2198. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  2199. if err != nil {
  2200. utils.ErrorLog(err.Error())
  2201. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2202. return
  2203. }
  2204. var cancelStockInfos []*models.DrugCancelStockInfo
  2205. var upDateCancelStockInfos []*models.DrugCancelStockInfo
  2206. var drugFlow []*models.DrugFlow
  2207. if dataBody["cancelStock"] != nil && reflect.TypeOf(dataBody["cancelStock"]).String() == "[]interface {}" {
  2208. thisCancelStock, _ := dataBody["cancelStock"].([]interface{})
  2209. if len(thisCancelStock) > 0 {
  2210. for _, item := range thisCancelStock {
  2211. items := item.(map[string]interface{})
  2212. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  2213. utils.ErrorLog("drug_id")
  2214. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2215. return
  2216. }
  2217. drug_id := int64(items["drug_id"].(float64))
  2218. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  2219. utils.ErrorLog("count")
  2220. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2221. return
  2222. }
  2223. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  2224. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  2225. utils.ErrorLog("price")
  2226. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2227. return
  2228. }
  2229. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  2230. total := float64(count) * price
  2231. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  2232. utils.ErrorLog("retail_price")
  2233. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2234. return
  2235. }
  2236. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  2237. retail_price_total := float64(count) * retail_price
  2238. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  2239. utils.ErrorLog("id")
  2240. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2241. return
  2242. }
  2243. id := int64(items["id"].(float64))
  2244. max_unit := items["max_unit"].(string)
  2245. batch_number := items["batch_number"].(string)
  2246. register_account := items["register_account"].(string)
  2247. remark := items["remark"].(string)
  2248. manufacturer := items["manufacturer"].(string)
  2249. dealer := items["dealer"].(string)
  2250. var productDates int64
  2251. var expiryDates int64
  2252. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  2253. expiryDates = 0
  2254. } else {
  2255. if len(items["expiry_date"].(string)) == 0 {
  2256. expiryDates = 0
  2257. } else {
  2258. expiryDate, _ := items["expiry_date"].(string)
  2259. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  2260. expiryDates = expiry_date.Unix()
  2261. }
  2262. }
  2263. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  2264. productDates = 0
  2265. } else {
  2266. if len(items["product_date"].(string)) == 0 {
  2267. productDates = 0
  2268. } else {
  2269. productDate, _ := items["product_date"].(string)
  2270. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  2271. productDates = product_date.Unix()
  2272. }
  2273. }
  2274. batch_number_id := int64(items["batch_number_id"].(float64))
  2275. if id == 0 {
  2276. cancelStockInfo := &models.DrugCancelStockInfo{
  2277. DrugId: drug_id,
  2278. Count: count,
  2279. Status: 1,
  2280. Ctime: ctime,
  2281. OrgId: adminUserInfo.CurrentOrgId,
  2282. OrderNumber: cancelStock.OrderNumber,
  2283. CancelStockId: cancelStock.ID,
  2284. Mtime: time.Now().Unix(),
  2285. Type: types,
  2286. RetailPrice: retail_price,
  2287. RetailTotalPrice: retail_price_total,
  2288. Price: price,
  2289. Total: total,
  2290. Manufacturer: manufacturer,
  2291. Dealer: dealer,
  2292. RegisterAccount: register_account,
  2293. MaxUnit: max_unit,
  2294. BatchNumber: batch_number,
  2295. Remark: remark,
  2296. ProductDate: productDates,
  2297. ExpiryDate: expiryDates,
  2298. BatchNumberId: batch_number_id,
  2299. }
  2300. cancelStockInfos = append(cancelStockInfos, cancelStockInfo)
  2301. } else {
  2302. cancelStockInfo := &models.DrugCancelStockInfo{
  2303. ID: id,
  2304. DrugId: drug_id,
  2305. Count: count,
  2306. Status: 1,
  2307. Ctime: ctime,
  2308. OrgId: adminUserInfo.CurrentOrgId,
  2309. OrderNumber: cancelStock.OrderNumber,
  2310. CancelStockId: cancelStock.ID,
  2311. Mtime: time.Now().Unix(),
  2312. Type: types,
  2313. RetailPrice: retail_price,
  2314. RetailTotalPrice: retail_price_total,
  2315. Price: price,
  2316. Total: total,
  2317. Manufacturer: manufacturer,
  2318. Dealer: dealer,
  2319. RegisterAccount: register_account,
  2320. MaxUnit: max_unit,
  2321. BatchNumber: batch_number,
  2322. Remark: remark,
  2323. ProductDate: productDates,
  2324. ExpiryDate: expiryDates,
  2325. BatchNumberId: batch_number_id,
  2326. }
  2327. upDateCancelStockInfos = append(upDateCancelStockInfos, cancelStockInfo)
  2328. }
  2329. flow := &models.DrugFlow{
  2330. WarehousingId: 0,
  2331. DrugId: drug_id,
  2332. Number: "",
  2333. BatchNumber: batch_number,
  2334. Count: count,
  2335. UserOrgId: adminUserInfo.CurrentOrgId,
  2336. PatientId: 0,
  2337. SystemTime: time.Now().Unix(),
  2338. ConsumableType: 4,
  2339. IsSys: 0,
  2340. WarehousingOrder: "",
  2341. WarehouseOutId: 0,
  2342. WarehouseOutOrderNumber: "",
  2343. IsEdit: 0,
  2344. CancelStockId: cancelStock.ID,
  2345. CancelOrderNumber: cancelStock.OrderNumber,
  2346. Manufacturer: 0,
  2347. Dealer: 0,
  2348. Creator: adminUserInfo.AdminUser.Id,
  2349. UpdateCreator: adminUserInfo.AdminUser.Id,
  2350. Status: 1,
  2351. Ctime: time.Now().Unix(),
  2352. Mtime: 0,
  2353. Price: price,
  2354. WarehousingDetailId: 0,
  2355. WarehouseOutDetailId: 0,
  2356. CancelOutDetailId: 0,
  2357. ExpireDate: expiryDates,
  2358. ProductDate: productDates,
  2359. MaxUnit: max_unit,
  2360. MinUnit: "",
  2361. }
  2362. drugFlow = append(drugFlow, flow)
  2363. }
  2364. }
  2365. }
  2366. var errs error
  2367. if len(cancelStockInfos) > 0 {
  2368. //扣减库存逻辑
  2369. for _, item := range cancelStockInfos {
  2370. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  2371. //查询当前药品退库的批次号的总入库数
  2372. infoWareInfo, _ := service.GetDrugWarehouseInfo(item.BatchNumberId)
  2373. fmt.Println("hhh2h3h2hh3233", item.MaxUnit, medical.MaxUnit)
  2374. var total_count int64
  2375. var cancel_count int64
  2376. var out_count int64
  2377. total_count = infoWareInfo.StockMaxNumber*medical.MinNumber + infoWareInfo.StockMinNumber
  2378. //转化为最小单位
  2379. var total_number int64
  2380. if item.MaxUnit == medical.MaxUnit {
  2381. total_number = item.Count * medical.MinNumber
  2382. }
  2383. if item.MaxUnit == medical.MinUnit {
  2384. total_number = item.Count
  2385. }
  2386. //统计该批次的总出库数
  2387. outInfo, _ := service.GetDrugWarehouseOutInfo(item.BatchNumberId, item.DrugId)
  2388. for _, item := range outInfo {
  2389. if item.CountUnit == medical.MaxUnit {
  2390. item.Count = item.Count * medical.MinNumber
  2391. }
  2392. if item.CountUnit == medical.MinUnit {
  2393. item.Count = item.Count
  2394. }
  2395. }
  2396. for _, item := range outInfo {
  2397. out_count += item.Count
  2398. }
  2399. fmt.Println("out_count232233332232323223", out_count)
  2400. //判断退库数量是否大于出库数量
  2401. cancelInfo, _ := service.GetCancelDrugStockOutInfo(item.BatchNumberId, item.DrugId)
  2402. for _, item := range cancelInfo {
  2403. if item.MaxUnit == medical.MaxUnit {
  2404. item.Count = item.Count * medical.MinNumber
  2405. }
  2406. if item.MaxUnit == medical.MinUnit {
  2407. item.Count = item.Count
  2408. }
  2409. }
  2410. for _, item := range cancelInfo {
  2411. cancel_count += item.Count
  2412. }
  2413. fmt.Println("cancel_count", cancel_count)
  2414. fmt.Println("hhhhhh23h2h332322323232", total_number+cancel_count)
  2415. if total_number+cancel_count > out_count {
  2416. c.ServeSuccessJSON(map[string]interface{}{
  2417. "msg": "4",
  2418. })
  2419. return
  2420. }
  2421. //判断退库数量是否大于总入库数量
  2422. if total_number > total_count {
  2423. c.ServeSuccessJSON(map[string]interface{}{
  2424. "msg": "2",
  2425. })
  2426. return
  2427. }
  2428. //正常退库
  2429. if total_number <= total_count {
  2430. //创建退库详情
  2431. errs := service.CreateDrugCancelStockInfo(cancelStockInfos)
  2432. //创建库存明细
  2433. //查询这个药品这个订单是否已存在流水
  2434. for _, items := range drugFlow {
  2435. _, errsId := service.GetCancelStockId(items.CancelStockId, items.DrugId)
  2436. if errsId == gorm.ErrRecordNotFound {
  2437. errs = service.CreateDrugFlowTwo(items)
  2438. }
  2439. }
  2440. //获取该批次最后一条退库详情
  2441. lastCancel, _ := service.GetLastCancelStockInfo(id)
  2442. var cancel_number int64
  2443. if lastCancel.MaxUnit == medical.MaxUnit {
  2444. cancel_number = lastCancel.Count * medical.MinNumber
  2445. }
  2446. if lastCancel.MaxUnit == medical.MinUnit {
  2447. cancel_number = lastCancel.Count
  2448. }
  2449. fmt.Println("999999232323232232232323232323", item.Count, cancel_number)
  2450. var total int64
  2451. if medical.MaxUnit == item.MaxUnit {
  2452. total = item.Count - cancel_number
  2453. warehouseInfo := models.XtDrugWarehouseInfo{
  2454. StockMaxNumber: total,
  2455. }
  2456. errs = service.UpdateDrugWarehouseInfo(&warehouseInfo, item.BatchNumberId)
  2457. if errs != nil {
  2458. utils.ErrorLog(errs.Error())
  2459. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2460. return
  2461. }
  2462. c.ServeSuccessJSON(map[string]interface{}{
  2463. "msg": "1",
  2464. })
  2465. }
  2466. if medical.MinUnit == item.MaxUnit {
  2467. total = item.Count - cancel_number
  2468. warehouseInfo := models.XtDrugWarehouseInfo{
  2469. StockMinNumber: total,
  2470. }
  2471. errs = service.UpdateDrugWarehouseInfoOne(&warehouseInfo, item.BatchNumberId)
  2472. if errs != nil {
  2473. utils.ErrorLog(errs.Error())
  2474. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2475. return
  2476. }
  2477. c.ServeSuccessJSON(map[string]interface{}{
  2478. "msg": "1",
  2479. })
  2480. }
  2481. if errs != nil {
  2482. utils.ErrorLog(errs.Error())
  2483. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2484. return
  2485. }
  2486. c.ServeSuccessJSON(map[string]interface{}{
  2487. "msg": "1",
  2488. })
  2489. }
  2490. }
  2491. }
  2492. if len(drugFlow) > 0 {
  2493. for _, items := range drugFlow {
  2494. _, errsId := service.GetCancelStockId(items.CancelStockId, items.DrugId)
  2495. if errsId == gorm.ErrRecordNotFound {
  2496. errs = service.CreateDrugFlowTwo(items)
  2497. }
  2498. }
  2499. }
  2500. if len(upDateCancelStockInfos) > 0 {
  2501. var total int64
  2502. var out_count int64
  2503. var cancel_count int64
  2504. var now_count int64
  2505. var all_count int64
  2506. for _, item := range upDateCancelStockInfos {
  2507. //查询最后一条退库记录
  2508. info, _ := service.GetLastCancelStockInfoOne(id, item.DrugId)
  2509. errs = service.UpDateDrugCancelStockInfo(item)
  2510. fmt.Println("hh23232oooooooo2o3o2o32o23oo23o23o23", info)
  2511. //查询该药品的信息
  2512. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  2513. //统计该批次的总出库数
  2514. outInfo, _ := service.GetDrugWarehouseOutInfo(item.BatchNumberId, item.DrugId)
  2515. //查询该批次的总退库数量
  2516. cancelInfo, _ := service.GetTotalDrugCancelInfo(item.BatchNumberId, item.DrugId, item.ID)
  2517. for _, item := range outInfo {
  2518. if item.CountUnit == medical.MaxUnit {
  2519. item.Count = item.Count * medical.MinNumber
  2520. }
  2521. if item.CountUnit == medical.MinUnit {
  2522. item.Count = item.Count
  2523. }
  2524. }
  2525. for _, item := range outInfo {
  2526. out_count += item.Count
  2527. }
  2528. for _, item := range cancelInfo {
  2529. if item.MaxUnit == medical.MaxUnit {
  2530. item.Count = item.Count * medical.MinNumber
  2531. }
  2532. if item.MaxUnit == medical.MinUnit {
  2533. item.Count = item.Count
  2534. }
  2535. }
  2536. for _, item := range cancelInfo {
  2537. cancel_count += item.Count
  2538. }
  2539. fmt.Println("out_count232233332232323223", out_count)
  2540. if item.MaxUnit == medical.MaxUnit {
  2541. now_count = item.Count * medical.MinNumber
  2542. }
  2543. if item.MaxUnit == medical.MinUnit {
  2544. now_count = item.Count
  2545. }
  2546. fmt.Println("hhhhhh2323322323", now_count+cancel_count)
  2547. //总退库批次
  2548. all_count = now_count + cancel_count
  2549. //该批次的总出库数小与退库数报错返回
  2550. if all_count > out_count {
  2551. c.ServeSuccessJSON(map[string]interface{}{
  2552. "msg": "4",
  2553. })
  2554. return
  2555. }
  2556. if item.MaxUnit == medical.MaxUnit {
  2557. //更新库存
  2558. total = item.Count - info.Count
  2559. parseDateErr := service.UpdatedDrugInfo(item.BatchNumberId, total)
  2560. fmt.Println("parseDateErr", parseDateErr)
  2561. flow := models.DrugFlow{
  2562. Count: item.Count,
  2563. }
  2564. service.UpdatedDrugFlow(info.CancelStockId, item.DrugId, flow)
  2565. break
  2566. }
  2567. if item.MaxUnit == medical.MinUnit {
  2568. item.Count = item.Count
  2569. total = item.Count - info.Count
  2570. service.UpdatedDrugInfoOne(item.BatchNumberId, total)
  2571. flow := models.DrugFlow{
  2572. Count: item.Count,
  2573. }
  2574. service.UpdatedDrugFlow(info.CancelStockId, item.DrugId, flow)
  2575. break
  2576. }
  2577. }
  2578. }
  2579. if errs != nil {
  2580. utils.ErrorLog(errs.Error())
  2581. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateCancelStockFail)
  2582. return
  2583. }
  2584. c.ServeSuccessJSON(map[string]interface{}{
  2585. "msg": "1",
  2586. })
  2587. }
  2588. func (c *StockDrugApiController) GetAllConfig() {
  2589. adminUserInfo := c.GetAdminUserInfo()
  2590. manufacturer, _ := service.FindAllManufacturer(adminUserInfo.CurrentOrgId)
  2591. dealer, _ := service.FindAllDealer(adminUserInfo.CurrentOrgId)
  2592. stockIns, _ := service.FindStockInNumber(adminUserInfo.CurrentOrgId)
  2593. drugs, _ := service.GetDrugs(adminUserInfo.CurrentOrgId)
  2594. c.ServeSuccessJSON(map[string]interface{}{
  2595. "manufacturer": manufacturer,
  2596. "dealer": dealer,
  2597. "drugs": drugs,
  2598. "numbers": stockIns,
  2599. })
  2600. }
  2601. func (c *StockDrugApiController) GetAllDrugStock() {
  2602. adminUserInfo := c.GetAdminUserInfo()
  2603. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  2604. dealer_id, _ := c.GetInt64("dealer_id", 0)
  2605. drugs, err := service.FindAllDrugByManufactureId(manufacturer_id, dealer_id, adminUserInfo.CurrentOrgId)
  2606. if err == nil {
  2607. c.ServeSuccessJSON(map[string]interface{}{
  2608. "drugs": drugs,
  2609. })
  2610. } else {
  2611. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  2612. }
  2613. }
  2614. func (c *StockDrugApiController) GetDrugQueryInfo() {
  2615. page, _ := c.GetInt64("page", -1)
  2616. limit, _ := c.GetInt64("limit", -1)
  2617. keyword := c.GetString("keyword")
  2618. drug_category, _ := c.GetInt64("drug_category")
  2619. start_time := c.GetString("start_time")
  2620. end_time := c.GetString("end_time")
  2621. timeLayout := "2006-01-02"
  2622. loc, _ := time.LoadLocation("Local")
  2623. var startTime int64
  2624. if len(start_time) > 0 {
  2625. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  2626. if err != nil {
  2627. fmt.Println(err)
  2628. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2629. return
  2630. }
  2631. startTime = theTime.Unix()
  2632. }
  2633. var endTime int64
  2634. if len(end_time) > 0 {
  2635. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  2636. if err != nil {
  2637. utils.ErrorLog(err.Error())
  2638. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2639. return
  2640. }
  2641. endTime = theTime.Unix()
  2642. }
  2643. adminUserInfo := c.GetAdminUserInfo()
  2644. list, total, err := service.FindAllDrugStockInfo(adminUserInfo.CurrentOrgId, page, limit, keyword, drug_category, startTime, endTime)
  2645. if err == nil {
  2646. c.ServeSuccessJSON(map[string]interface{}{
  2647. "list": list,
  2648. "total": total,
  2649. })
  2650. } else {
  2651. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  2652. }
  2653. }
  2654. func (this *StockDrugApiController) GetUserDetailInfo() {
  2655. order_id, _ := this.GetInt64("id")
  2656. if order_id <= 0 {
  2657. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2658. return
  2659. }
  2660. adminUserInfo := this.GetAdminUserInfo()
  2661. userDetails, err, total := service.FindDrugStockUserDetailById(adminUserInfo.CurrentOrgId, order_id)
  2662. if err == nil {
  2663. this.ServeSuccessJSON(map[string]interface{}{
  2664. "list": userDetails,
  2665. "total": total,
  2666. })
  2667. } else {
  2668. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  2669. return
  2670. }
  2671. }
  2672. func (this *StockDrugApiController) GetCancelDrugOrderPrint() {
  2673. orderId := this.GetString("order_id")
  2674. ids := strings.Split(orderId, ",")
  2675. orgId := this.GetAdminUserInfo().CurrentOrgId
  2676. list, _ := service.GetCancelDrugOrderPrint(ids, orgId)
  2677. this.ServeSuccessJSON(map[string]interface{}{
  2678. "list": list,
  2679. })
  2680. }
  2681. func (this *StockDrugApiController) GetDrugCancelExportList() {
  2682. orderId := this.GetString("order_id")
  2683. fmt.Println("order_id2323232323223", orderId)
  2684. ids := strings.Split(orderId, ",")
  2685. orgId := this.GetAdminUserInfo().CurrentOrgId
  2686. list, _ := service.GetDrugCancelExportList(ids, orgId)
  2687. this.ServeSuccessJSON(map[string]interface{}{
  2688. "list": list,
  2689. })
  2690. }
  2691. func (c *StockDrugApiController) GetDrugWarehouseInfoPrint() {
  2692. start_time := c.GetString("start_time")
  2693. end_time := c.GetString("end_time")
  2694. fmt.Println("开始实际23323233322323", start_time)
  2695. order_type, _ := c.GetInt64("order_type")
  2696. timeLayout := "2006-01-02"
  2697. loc, _ := time.LoadLocation("Local")
  2698. var startTime int64
  2699. if len(start_time) > 0 {
  2700. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  2701. if err != nil {
  2702. fmt.Println(err)
  2703. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2704. return
  2705. }
  2706. startTime = theTime.Unix()
  2707. }
  2708. var endTime int64
  2709. if len(end_time) > 0 {
  2710. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  2711. if err != nil {
  2712. utils.ErrorLog(err.Error())
  2713. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2714. return
  2715. }
  2716. endTime = theTime.Unix()
  2717. }
  2718. adminUserInfo := c.GetAdminUserInfo()
  2719. orgId := adminUserInfo.CurrentOrgId
  2720. if order_type == 1 {
  2721. list, err := service.GetDrugWarehouseInfoPrint(startTime, endTime, orgId)
  2722. if err == nil {
  2723. c.ServeSuccessJSON(map[string]interface{}{
  2724. "list": list,
  2725. })
  2726. return
  2727. }
  2728. }
  2729. }
  2730. func (c *StockDrugApiController) GetDrugOutOrderPrint() {
  2731. start_time := c.GetString("start_time")
  2732. end_time := c.GetString("end_time")
  2733. order_type, _ := c.GetInt64("order_type")
  2734. timeLayout := "2006-01-02"
  2735. loc, _ := time.LoadLocation("Local")
  2736. var startTime int64
  2737. if len(start_time) > 0 {
  2738. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  2739. if err != nil {
  2740. fmt.Println(err)
  2741. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2742. return
  2743. }
  2744. startTime = theTime.Unix()
  2745. }
  2746. var endTime int64
  2747. if len(end_time) > 0 {
  2748. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  2749. if err != nil {
  2750. utils.ErrorLog(err.Error())
  2751. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2752. return
  2753. }
  2754. endTime = theTime.Unix()
  2755. }
  2756. adminUserInfo := c.GetAdminUserInfo()
  2757. orgId := adminUserInfo.CurrentOrgId
  2758. if order_type == 2 {
  2759. //list, err := service.GetDrugOutOrderInfoPrintList(startTime, endTime, orgId)
  2760. list, err := service.GetDrugOutOrderPrintList(startTime, endTime, orgId)
  2761. if err == nil {
  2762. c.ServeSuccessJSON(map[string]interface{}{
  2763. "list": list,
  2764. })
  2765. return
  2766. }
  2767. }
  2768. }