drug_stock_api_contorller.go 132KB

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