drug_stock_api_contorller.go 103KB

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