drug_stock_api_contorller.go 122KB

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