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. //创建一个出库单
  998. warehouseOut := models.DrugWarehouseOut{
  999. WarehouseOutOrderNumber: warehousing_out_order,
  1000. OperationTime: operation_time,
  1001. OrgId: adminUserInfo.CurrentOrgId,
  1002. Creater: creater,
  1003. Ctime: ctime,
  1004. Status: 1,
  1005. WarehouseOutTime: warehousingOutDate.Unix(),
  1006. Type: types,
  1007. StorehouseId: storehouse_id,
  1008. IsCheck: 2,
  1009. }
  1010. fmt.Println("warehouse_out_id232323322323232323233223", warehouse_out_id)
  1011. if warehouse_out_id == 0 {
  1012. service.AddSigleDrugWarehouseOut(&warehouseOut)
  1013. }
  1014. if warehouse_out_id > 0 {
  1015. service.UpdateSingleDrugWarehouseOut(warehouse_out_id, storehouse_id)
  1016. }
  1017. lastWarehouseOut, _ := service.GetDrugLastWarehouseOut(adminUserInfo.CurrentOrgId)
  1018. dataBody := make(map[string]interface{}, 0)
  1019. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1020. if err != nil {
  1021. utils.ErrorLog(err.Error())
  1022. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1023. return
  1024. }
  1025. //出库单详情
  1026. var warehousingOutInfo []*models.DrugWarehouseOutInfo
  1027. //把前端的参数加工为出库单详情
  1028. if dataBody["stockOut"] != nil && reflect.TypeOf(dataBody["stockOut"]).String() == "[]interface {}" {
  1029. thisStockIn, _ := dataBody["stockOut"].([]interface{})
  1030. if len(thisStockIn) > 0 {
  1031. for _, item := range thisStockIn {
  1032. items := item.(map[string]interface{})
  1033. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1034. utils.ErrorLog("drug_id")
  1035. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1036. return
  1037. }
  1038. drug_id := int64(items["drug_id"].(float64))
  1039. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  1040. utils.ErrorLog("count")
  1041. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1042. return
  1043. }
  1044. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1045. utils.ErrorLog("price")
  1046. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1047. return
  1048. }
  1049. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1050. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  1051. total := float64(count) * price
  1052. retail_price_total := float64(count) * price
  1053. remark := items["remark"].(string)
  1054. max_unit := items["max_unit"].(string)
  1055. dealer := int64(items["dealer"].(float64))
  1056. manufacturer := int64(items["manufacturer"].(float64))
  1057. number := items["number"].(string)
  1058. batch_number := items["batch_number"].(string)
  1059. warehouse_info_id := int64(items["warehouse_info_id"].(float64))
  1060. var productDates int64
  1061. var expiryDates int64
  1062. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  1063. expiryDates = 0
  1064. } else {
  1065. if len(items["expiry_date"].(string)) == 0 {
  1066. expiryDates = 0
  1067. } else {
  1068. expiryDate, _ := items["expiry_date"].(string)
  1069. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  1070. expiryDates = expiry_date.Unix()
  1071. }
  1072. }
  1073. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  1074. productDates = 0
  1075. } else {
  1076. if len(items["product_date"].(string)) == 0 {
  1077. productDates = 0
  1078. } else {
  1079. productDate, _ := items["product_date"].(string)
  1080. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  1081. productDates = product_date.Unix()
  1082. }
  1083. }
  1084. admin_user_id := int64(items["admin_user_id"].(float64))
  1085. //if items["last_price"] == nil || reflect.TypeOf(items["last_price"]).String() != "string" {
  1086. // utils.ErrorLog("last_price")
  1087. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1088. // return
  1089. //}
  1090. //last_price, _ := strconv.ParseFloat(items["last_price"].(string), 64)
  1091. stock_count, _ := items["stock_count"].(string)
  1092. id := int64(items["id"].(float64))
  1093. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1094. ID: id,
  1095. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1096. WarehouseOutId: lastWarehouseOut.ID,
  1097. DrugId: drug_id,
  1098. Count: count,
  1099. Price: price,
  1100. TotalPrice: total,
  1101. Status: 1,
  1102. Ctime: ctime,
  1103. Remark: remark,
  1104. OrgId: adminUserInfo.CurrentOrgId,
  1105. Type: types,
  1106. Manufacturer: manufacturer,
  1107. Dealer: dealer,
  1108. RetailPrice: price,
  1109. RetailTotalPrice: retail_price_total,
  1110. CountUnit: max_unit,
  1111. ExpiryDate: expiryDates,
  1112. ProductDate: productDates,
  1113. Number: number,
  1114. BatchNumber: batch_number,
  1115. IsSys: 0,
  1116. WarehouseInfoId: warehouse_info_id,
  1117. StorehouseId: storehouse_id,
  1118. AdminUserId: admin_user_id,
  1119. StockCount: stock_count,
  1120. IsCheck: 2,
  1121. }
  1122. warehousingOutInfo = append(warehousingOutInfo, warehouseOutInfo)
  1123. }
  1124. }
  1125. }
  1126. //var total_count int64 //总库存
  1127. //var prescribing_number_total int64 //出库数据
  1128. //调用出库逻辑
  1129. for _, item := range warehousingOutInfo {
  1130. if item.ID == 0 {
  1131. errOne := service.AddSigleDrugWarehouseOutInfo(item)
  1132. fmt.Println(errOne)
  1133. }
  1134. if item.ID > 0 {
  1135. service.UpdatedDrugWarehouseInfo(item, item.ID)
  1136. }
  1137. }
  1138. list, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
  1139. manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
  1140. warehousingOutInfoList, _ := service.GetDrugWarehouseOutListById(lastWarehouseOut.ID, adminUserInfo.CurrentOrgId)
  1141. var drugType = "药品类型"
  1142. drugTypeParent, _ := service.GetDrugDataConfig(0, drugType)
  1143. drugTypeList, _ := service.GetParentDataConfig(drugTypeParent.ID, adminUserInfo.CurrentOrgId)
  1144. c.ServeSuccessJSON(map[string]interface{}{
  1145. "msg": "2",
  1146. "drug_name": "",
  1147. "dose": "",
  1148. "dose_unit": "",
  1149. "min_number": "",
  1150. "min_unit": "",
  1151. "max_unit": "",
  1152. "dealerList": list,
  1153. "manufacturerList": manufacturerList,
  1154. "warehousingOutInfo": warehousingOutInfoList,
  1155. "warehouseOut": lastWarehouseOut,
  1156. "drugTypeList": drugTypeList,
  1157. })
  1158. return
  1159. }
  1160. func (c *StockDrugApiController) GetDrugWarehouseOutList() {
  1161. page, _ := c.GetInt64("page", -1)
  1162. limit, _ := c.GetInt64("limit", -1)
  1163. start_time := c.GetString("start_time")
  1164. end_time := c.GetString("end_time")
  1165. types, _ := c.GetInt64("type", 0)
  1166. keywords := c.GetString("keywords")
  1167. storehouse_id, _ := c.GetInt64("storehouse_id")
  1168. way_type, _ := c.GetInt64("way_type")
  1169. check_type, _ := c.GetInt64("check_type")
  1170. timeLayout := "2006-01-02"
  1171. loc, _ := time.LoadLocation("Local")
  1172. var startTime int64
  1173. if len(start_time) > 0 {
  1174. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  1175. if err != nil {
  1176. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1177. return
  1178. }
  1179. startTime = theTime.Unix()
  1180. }
  1181. var endTime int64
  1182. if len(end_time) > 0 {
  1183. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  1184. if err != nil {
  1185. utils.ErrorLog(err.Error())
  1186. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1187. return
  1188. }
  1189. endTime = theTime.Unix()
  1190. }
  1191. adminUserInfo := c.GetAdminUserInfo()
  1192. var ids []int64
  1193. var goodids []int64
  1194. if len(keywords) > 0 {
  1195. //查询商品名称
  1196. list, _ := service.GetDrugNameByKeyword(keywords, adminUserInfo.CurrentOrgId)
  1197. for _, item := range list {
  1198. goodids = append(goodids, item.ID)
  1199. }
  1200. if len(goodids) > 0 {
  1201. //出库详情但里面查询
  1202. info, _ := service.GetDrugWarehouseOrderDetail(goodids, adminUserInfo.CurrentOrgId, startTime, endTime, storehouse_id)
  1203. for _, it := range info {
  1204. ids = append(ids, it.WarehouseOutId)
  1205. }
  1206. }
  1207. }
  1208. warehouseOutList, total, err := service.FindAllDrugWarehouseOutListOne(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords, ids, storehouse_id, way_type, check_type)
  1209. fmt.Println("warehouseOutList232332323232323", warehouseOutList)
  1210. houseList, _ := service.GetAllStoreHouseList(adminUserInfo.CurrentOrgId)
  1211. fmt.Println(err)
  1212. if err == nil {
  1213. c.ServeSuccessJSON(map[string]interface{}{
  1214. "list": warehouseOutList,
  1215. "total": total,
  1216. "houseList": houseList,
  1217. })
  1218. } else {
  1219. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  1220. }
  1221. }
  1222. func (c *StockDrugApiController) DeleteDrugWarehouseOut() {
  1223. ids := c.GetString("ids")
  1224. if len(ids) == 0 {
  1225. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1226. return
  1227. }
  1228. idArray := strings.Split(ids, ",")
  1229. err := service.DeleteDrugWarehouseOut(idArray)
  1230. //list, _ := service.GetDrugWarhouseOutByIds(idArray)
  1231. //for _, item := range list {
  1232. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  1233. // if item.CountUnit == medical.MaxUnit {
  1234. //
  1235. // service.UpdateDrugInfoByIds(item.WarehouseInfoId, item.Count)
  1236. //
  1237. // }
  1238. // if medical.MaxUnit != medical.MinUnit && item.CountUnit == medical.MinUnit {
  1239. // service.UpdateDrugInfoByIdsOne(item.WarehouseInfoId, item.Count)
  1240. // }
  1241. //
  1242. // //查询默认仓库
  1243. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  1244. // //查询默认仓库剩余多少库存
  1245. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  1246. // var sum_count int64
  1247. // var sum_in_count int64
  1248. // for _, it := range list {
  1249. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  1250. // if it.MaxUnit == baseDrug.MaxUnit {
  1251. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1252. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1253. // }
  1254. // sum_count += it.StockMaxNumber + it.StockMinNumber
  1255. // sum_in_count += it.WarehousingCount
  1256. // }
  1257. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  1258. //}
  1259. if err != nil {
  1260. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  1261. } else {
  1262. c.ServeSuccessJSON(map[string]interface{}{
  1263. "msg": "删除成功",
  1264. })
  1265. }
  1266. }
  1267. func (this *StockDrugApiController) DeleteDrugWarehouseOutInfo() {
  1268. id, _ := this.GetInt64("id", 0)
  1269. if id == 0 {
  1270. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1271. return
  1272. }
  1273. ////查询详情
  1274. //info, _ := service.GetDrugWarehosueOutInfo(id)
  1275. //
  1276. ////查询默认仓库
  1277. //houseConfig, _ := service.GetAllStoreHouseConfig(info.OrgId)
  1278. ////查询默认仓库剩余多少库存
  1279. //list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, info.OrgId, info.DrugId)
  1280. //
  1281. //for _, it := range list {
  1282. //
  1283. // //查询默认仓库
  1284. // houseConfig, _ := service.GetAllStoreHouseConfig(it.OrgId)
  1285. // //查询默认仓库剩余多少库存
  1286. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, it.OrgId, it.DrugId)
  1287. // var sum_count int64
  1288. // var sum_in_count int64
  1289. // for _, it := range list {
  1290. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  1291. // if it.MaxUnit == baseDrug.MaxUnit {
  1292. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1293. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1294. // }
  1295. // sum_count += it.StockMaxNumber + it.StockMinNumber
  1296. // sum_in_count += it.WarehousingCount
  1297. // }
  1298. // service.UpdateMedicalSumCount(it.DrugId, sum_count, sum_in_count, it.OrgId)
  1299. //
  1300. //}
  1301. //删除流水
  1302. //service.DeleteDrugWarehouseOutInfoById(info.ID)
  1303. err := service.UpDateDrugWarehouseOutStatus(id)
  1304. if err != nil {
  1305. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  1306. } else {
  1307. this.ServeSuccessJSON(map[string]interface{}{
  1308. "msg": "删除成功",
  1309. })
  1310. }
  1311. }
  1312. func (c *StockDrugApiController) GetDrugWarehouseOutInfoList() {
  1313. id, _ := c.GetInt64("id", 0)
  1314. adminInfo := c.GetAdminUserInfo()
  1315. warehouseOutInfo, _ := service.FindDrugWarehouseOutInfoById(id, adminInfo.CurrentOrgId)
  1316. warehouseOut, _ := service.FindDrugWareHouseOutById(id, adminInfo.CurrentOrgId)
  1317. manulist, _ := service.GetAllManufacturerList(adminInfo.CurrentOrgId)
  1318. dealerList, _ := service.GetAllDealerList(adminInfo.CurrentOrgId)
  1319. houstList, _ := service.GetAllStoreHouseList(adminInfo.CurrentOrgId)
  1320. appId := c.GetAdminUserInfo().CurrentAppId
  1321. doctorList, _ := service.GetAllDoctorListSix(adminInfo.CurrentOrgId, appId)
  1322. c.ServeSuccessJSON(map[string]interface{}{
  1323. "list": warehouseOutInfo,
  1324. "info": warehouseOut,
  1325. "manulist": manulist,
  1326. "dealerList": dealerList,
  1327. "houstList": houstList,
  1328. "doctorList": doctorList,
  1329. })
  1330. }
  1331. func (c *StockDrugApiController) EditDrugWarehouseOut() {
  1332. warehouse_out_time := c.GetString("warehouse_out_time")
  1333. id, _ := c.GetInt64("id", 0)
  1334. types, _ := c.GetInt64("type", 0)
  1335. storehouse_id, _ := c.GetInt64("storehouse_id")
  1336. if id == 0 {
  1337. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1338. return
  1339. }
  1340. ctime := time.Now().Unix()
  1341. mtime := time.Now().Unix()
  1342. adminUserInfo := c.GetAdminUserInfo()
  1343. warehouseOutDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", warehouse_out_time)
  1344. if parseDateErr != nil {
  1345. c.ErrorLog("日期(%v)解析错误:%v", warehouseOutDate, parseDateErr)
  1346. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1347. return
  1348. }
  1349. warehouseOut, _ := service.FindDrugWareHouseOutById(id, adminUserInfo.CurrentOrgId)
  1350. tempWarehouseOut := models.DrugWarehouseOut{
  1351. ID: warehouseOut.ID,
  1352. Mtime: mtime,
  1353. WarehouseOutTime: warehouseOutDate.Unix(),
  1354. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1355. StorehouseId: storehouse_id,
  1356. }
  1357. service.EditDrugWarehouseOut(tempWarehouseOut)
  1358. dataBody := make(map[string]interface{}, 0)
  1359. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1360. if err != nil {
  1361. utils.ErrorLog(err.Error())
  1362. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1363. return
  1364. }
  1365. var warehousingOutInfo []*models.DrugWarehouseOutInfo
  1366. var upDateWarehouseOutInfos []*models.DrugWarehouseOutInfo
  1367. var drugFlow []*models.DrugFlow
  1368. if dataBody["stockOut"] != nil && reflect.TypeOf(dataBody["stockOut"]).String() == "[]interface {}" {
  1369. thisStockOut, _ := dataBody["stockOut"].([]interface{})
  1370. if len(thisStockOut) > 0 {
  1371. for _, item := range thisStockOut {
  1372. items := item.(map[string]interface{})
  1373. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1374. utils.ErrorLog("drug_id")
  1375. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1376. return
  1377. }
  1378. drug_id := int64(items["drug_id"].(float64))
  1379. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  1380. utils.ErrorLog("count")
  1381. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1382. return
  1383. }
  1384. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  1385. count_unit, _ := items["count_unit"].(string)
  1386. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  1387. utils.ErrorLog("price")
  1388. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1389. return
  1390. }
  1391. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  1392. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  1393. utils.ErrorLog("retail_price")
  1394. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1395. return
  1396. }
  1397. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  1398. total := float64(count) * price
  1399. retail_price_total := float64(count) * retail_price
  1400. remark := items["remark"].(string)
  1401. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  1402. utils.ErrorLog("id")
  1403. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1404. return
  1405. }
  1406. id := int64(items["id"].(float64))
  1407. dealer := int64(items["dealer"].(float64))
  1408. manufacturer := int64(items["manufacturer"].(float64))
  1409. batch_number := items["batch_number"].(string)
  1410. number := items["number"].(string)
  1411. max_unit := items["count_unit"].(string)
  1412. warehouse_info_id := int64(items["warehouse_info_id"].(float64))
  1413. var productDates int64
  1414. var expiryDates int64
  1415. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  1416. expiryDates = 0
  1417. } else {
  1418. if len(items["expiry_date"].(string)) == 0 {
  1419. expiryDates = 0
  1420. } else {
  1421. expiryDate, _ := items["expiry_date"].(string)
  1422. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  1423. expiryDates = expiry_date.Unix()
  1424. }
  1425. }
  1426. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  1427. productDates = 0
  1428. } else {
  1429. if len(items["product_date"].(string)) == 0 {
  1430. productDates = 0
  1431. } else {
  1432. productDate, _ := items["product_date"].(string)
  1433. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  1434. productDates = product_date.Unix()
  1435. }
  1436. }
  1437. admin_user_id := int64(items["admin_user_id"].(float64))
  1438. //if items["last_price"] == nil || reflect.TypeOf(items["last_price"]).String() != "string" {
  1439. // utils.ErrorLog("last_price")
  1440. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1441. // return
  1442. //}
  1443. //last_price, _ := strconv.ParseFloat(items["last_price"].(string), 64)
  1444. stock_count, _ := items["stock_count"].(string)
  1445. if id == 0 {
  1446. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1447. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1448. WarehouseOutId: warehouseOut.ID,
  1449. DrugId: drug_id,
  1450. Count: count,
  1451. Price: price,
  1452. TotalPrice: total,
  1453. Status: 1,
  1454. Ctime: ctime,
  1455. Remark: remark,
  1456. OrgId: adminUserInfo.CurrentOrgId,
  1457. Type: types,
  1458. Manufacturer: manufacturer,
  1459. Dealer: dealer,
  1460. IsSys: 0,
  1461. SysRecordTime: 0,
  1462. RetailPrice: retail_price,
  1463. RetailTotalPrice: retail_price_total,
  1464. BatchNumber: batch_number,
  1465. Number: number,
  1466. ProductDate: productDates,
  1467. ExpiryDate: expiryDates,
  1468. CountUnit: count_unit,
  1469. WarehouseInfoId: warehouse_info_id,
  1470. StorehouseId: storehouse_id,
  1471. AdminUserId: admin_user_id,
  1472. StockCount: stock_count,
  1473. }
  1474. warehousingOutInfo = append(warehousingOutInfo, warehouseOutInfo)
  1475. drugflow := &models.DrugFlow{
  1476. WarehousingOrder: "",
  1477. WarehousingId: 0,
  1478. DrugId: drug_id,
  1479. Number: number,
  1480. ProductDate: productDates,
  1481. ExpireDate: expiryDates,
  1482. Count: count,
  1483. Price: price,
  1484. Status: 1,
  1485. Ctime: ctime,
  1486. UserOrgId: adminUserInfo.CurrentOrgId,
  1487. Manufacturer: manufacturer,
  1488. Dealer: dealer,
  1489. BatchNumber: batch_number,
  1490. MaxUnit: max_unit,
  1491. ConsumableType: 1,
  1492. IsEdit: 1,
  1493. Creator: adminUserInfo.AdminUser.Id,
  1494. IsSys: 0,
  1495. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1496. WarehouseOutId: id,
  1497. StorehouseId: storehouse_id,
  1498. AdminUserId: admin_user_id,
  1499. StockCount: stock_count,
  1500. }
  1501. drugFlow = append(drugFlow, drugflow)
  1502. } else {
  1503. if items["is_sys"] == nil || reflect.TypeOf(items["is_sys"]).String() != "float64" {
  1504. utils.ErrorLog("is_sys")
  1505. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1506. return
  1507. }
  1508. is_sys := int64(items["is_sys"].(float64))
  1509. if items["sys_record_time"] == nil || reflect.TypeOf(items["sys_record_time"]).String() != "float64" {
  1510. utils.ErrorLog("sys_record_time")
  1511. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1512. return
  1513. }
  1514. sys_record_time := int64(items["sys_record_time"].(float64))
  1515. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1516. ID: id,
  1517. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1518. WarehouseOutId: warehouseOut.ID,
  1519. DrugId: drug_id,
  1520. Count: count,
  1521. Price: price,
  1522. TotalPrice: total,
  1523. Status: 1,
  1524. Ctime: ctime,
  1525. Remark: remark,
  1526. OrgId: adminUserInfo.CurrentOrgId,
  1527. Mtime: time.Now().Unix(),
  1528. Type: types,
  1529. Manufacturer: manufacturer,
  1530. Dealer: dealer,
  1531. IsSys: is_sys,
  1532. SysRecordTime: sys_record_time,
  1533. RetailPrice: retail_price,
  1534. RetailTotalPrice: retail_price_total,
  1535. BatchNumber: batch_number,
  1536. Number: number,
  1537. ProductDate: productDates,
  1538. ExpiryDate: expiryDates,
  1539. CountUnit: count_unit,
  1540. WarehouseInfoId: warehouse_info_id,
  1541. StorehouseId: storehouse_id,
  1542. AdminUserId: admin_user_id,
  1543. StockCount: stock_count,
  1544. }
  1545. upDateWarehouseOutInfos = append(upDateWarehouseOutInfos, warehouseOutInfo)
  1546. drugflow := &models.DrugFlow{
  1547. WarehousingOrder: "",
  1548. WarehousingId: 0,
  1549. DrugId: drug_id,
  1550. Number: number,
  1551. ProductDate: productDates,
  1552. ExpireDate: expiryDates,
  1553. Count: count,
  1554. Price: price,
  1555. Status: 1,
  1556. Ctime: ctime,
  1557. UserOrgId: adminUserInfo.CurrentOrgId,
  1558. Manufacturer: manufacturer,
  1559. Dealer: dealer,
  1560. BatchNumber: batch_number,
  1561. MaxUnit: max_unit,
  1562. ConsumableType: 1,
  1563. IsEdit: 1,
  1564. Creator: adminUserInfo.AdminUser.Id,
  1565. IsSys: 0,
  1566. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1567. WarehouseOutId: warehouseOut.ID,
  1568. StorehouseId: storehouse_id,
  1569. AdminUserId: admin_user_id,
  1570. StockCount: stock_count,
  1571. }
  1572. drugFlow = append(drugFlow, drugflow)
  1573. }
  1574. }
  1575. }
  1576. }
  1577. var errs error
  1578. if len(warehousingOutInfo) > 0 {
  1579. //var total_count int64
  1580. //var prescribing_number_total int64
  1581. //调用出库逻辑
  1582. for _, item := range warehousingOutInfo {
  1583. errOnes := service.AddSigleDrugWarehouseOutInfo(item)
  1584. fmt.Println("errone", errOnes)
  1585. ////获取药品库存
  1586. //info, _ := service.GetDrugTotalCount(item.DrugId, item.OrgId, storehouse_id)
  1587. ////查询改药品信息
  1588. //medical, _ := service.GetBaseDrugMedical(item.DrugId)
  1589. ////判断单位是否相等
  1590. //if medical.MaxUnit == item.CountUnit {
  1591. // //转化为最小单位
  1592. // total_count = info.Count * medical.MinNumber
  1593. // prescribing_number_total = item.Count * medical.MinNumber
  1594. //}
  1595. //if medical.MinUnit == item.CountUnit {
  1596. // total_count = info.Count
  1597. // prescribing_number_total = item.Count
  1598. //}
  1599. //
  1600. ////判断单位
  1601. //if total_count == 0 {
  1602. // goodObj, _ := service.GetDrugByGoodId(item.DrugId)
  1603. // c.ServeSuccessJSON(map[string]interface{}{
  1604. // "msg": "1",
  1605. // "drug_name": goodObj.DrugName,
  1606. // "dose": goodObj.Dose,
  1607. // "dose_unit": goodObj.DoseUnit,
  1608. // "min_number": goodObj.MinNumber,
  1609. // "min_unit": goodObj.MinUnit,
  1610. // "max_unit": goodObj.MaxUnit,
  1611. // })
  1612. // return
  1613. //}
  1614. //if prescribing_number_total > total_count {
  1615. //
  1616. // goodObj, _ := service.GetDrugByGoodId(item.DrugId)
  1617. // c.ServeSuccessJSON(map[string]interface{}{
  1618. // "msg": "1",
  1619. // "drug_name": goodObj.DrugName,
  1620. // "dose": goodObj.Dose,
  1621. // "dose_unit": goodObj.DoseUnit,
  1622. // "min_number": goodObj.MinNumber,
  1623. // "min_unit": goodObj.MinUnit,
  1624. // "max_unit": goodObj.MaxUnit,
  1625. // })
  1626. // return
  1627. //} else {
  1628. //
  1629. // // 出库流程
  1630. // // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  1631. // drup, _ := service.FindBaseDrugLibRecord(item.OrgId, item.DrugId)
  1632. // if drup.ID > 0 {
  1633. // prescribingNumber := item.Count
  1634. // service.AutoDrugDeliverInfoTwentyOne(item.OrgId, prescribingNumber, &tempWarehouseOut, &drup, item)
  1635. // }
  1636. //}
  1637. }
  1638. }
  1639. if len(upDateWarehouseOutInfos) > 0 {
  1640. for _, item := range upDateWarehouseOutInfos {
  1641. service.UpdatedDrugWarehouseInfo(item, item.ID)
  1642. ////获取药品库存
  1643. //info, _ := service.GetDrugTotalCount(item.DrugId, item.OrgId, storehouse_id)
  1644. ////查询改药品信息
  1645. //medical, _ := service.GetBaseDrugMedical(item.DrugId)
  1646. ////查询最后一次出库记录
  1647. //outInfo, _ := service.GetLastDrugWarehouseOutInfo(item.DrugId, item.WarehouseOutId, item.OrgId)
  1648. //var min_number int64 //最后一次出库数量
  1649. //var max_number int64 //当前出库数量
  1650. //var all_number int64 //总库存
  1651. //var cha_number int64 //库存差
  1652. //var maxNumber int64
  1653. //var minNumber int64
  1654. //
  1655. //if medical.MaxUnit == outInfo.CountUnit {
  1656. // //转为最小单位
  1657. // min_number = outInfo.Count * medical.MinNumber
  1658. //}
  1659. //
  1660. //if medical.MinUnit == outInfo.CountUnit {
  1661. // min_number = outInfo.Count
  1662. //}
  1663. //
  1664. //if medical.MaxUnit == item.CountUnit {
  1665. // max_number = item.Count * medical.MinNumber
  1666. //}
  1667. //
  1668. //if medical.MinUnit == item.CountUnit {
  1669. // max_number = item.Count
  1670. //}
  1671. //
  1672. //all_number = info.Count*medical.MinNumber + info.StockMinNumber
  1673. //
  1674. ////比较当前出库数量 小于等于 最后一次出库数量,则需要退库
  1675. //if max_number <= min_number {
  1676. // errs = service.UpDateDrugWarehouseOutInfo(item)
  1677. // flow := models.DrugFlow{
  1678. // Count: max_number,
  1679. // ExpireDate: item.ExpiryDate,
  1680. // ProductDate: item.ProductDate,
  1681. // Price: item.Price,
  1682. // Manufacturer: item.Manufacturer,
  1683. // Dealer: item.Dealer,
  1684. // Number: item.Number,
  1685. // WarehouseOutId: item.WarehouseOutId,
  1686. // StorehouseId: item.StorehouseId,
  1687. // AdminUserId: item.AdminUserId,
  1688. // StockCount: item.StockCount,
  1689. // }
  1690. // service.UpdateDrugFlowSix(item.WarehouseOutId, item.DrugId, item.WarehouseOutOrderNumber, flow)
  1691. // cha_number = min_number - max_number
  1692. //
  1693. // if item.CountUnit == medical.MaxUnit {
  1694. // maxNumber = cha_number / medical.MinNumber
  1695. //
  1696. // parseDateErr := service.UpdateWarehouseInfo(maxNumber, item.DrugId, item.OrgId, outInfo.WarehouseInfoId)
  1697. //
  1698. // if parseDateErr != nil {
  1699. // utils.ErrorLog(errs.Error())
  1700. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1701. // return
  1702. // }
  1703. //
  1704. // c.ServeSuccessJSON(map[string]interface{}{
  1705. // "msg": "2",
  1706. // "drug_name": "",
  1707. // "dose": "",
  1708. // "dose_unit": "",
  1709. // "min_number": "",
  1710. // "min_unit": "",
  1711. // "max_unit": "",
  1712. // })
  1713. // minNumber = cha_number % medical.MinNumber
  1714. // if minNumber < 0 {
  1715. // minNumber = 0
  1716. // }
  1717. // parseDateErr = service.UpdateWarehouseInfoOne(minNumber, item.DrugId, item.OrgId, outInfo.WarehouseInfoId)
  1718. //
  1719. // if parseDateErr != nil {
  1720. // utils.ErrorLog(errs.Error())
  1721. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1722. // return
  1723. // }
  1724. //
  1725. // c.ServeSuccessJSON(map[string]interface{}{
  1726. // "msg": "2",
  1727. // "drug_name": "",
  1728. // "dose": "",
  1729. // "dose_unit": "",
  1730. // "min_number": "",
  1731. // "min_unit": "",
  1732. // "max_unit": "",
  1733. // })
  1734. // }
  1735. // if item.CountUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  1736. // parseDateErr := service.UpdateWarehouseInfoOne(cha_number, item.DrugId, item.OrgId, outInfo.WarehouseInfoId)
  1737. //
  1738. // if parseDateErr != nil {
  1739. // utils.ErrorLog(errs.Error())
  1740. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1741. // return
  1742. // }
  1743. //
  1744. // c.ServeSuccessJSON(map[string]interface{}{
  1745. // "msg": "2",
  1746. // "drug_name": "",
  1747. // "dose": "",
  1748. // "dose_unit": "",
  1749. // "min_number": "",
  1750. // "min_unit": "",
  1751. // "max_unit": "",
  1752. // })
  1753. // }
  1754. //
  1755. //}
  1756. ////比较当前出库数量 大于 最后一次出库数量,则需要出库
  1757. //if max_number > min_number {
  1758. //
  1759. // cha_number = max_number - min_number
  1760. // //如果总库存大于差,正常出库
  1761. // if all_number > cha_number {
  1762. //
  1763. // errs = service.UpDateDrugWarehouseOutInfo(item)
  1764. // flow := models.DrugFlow{
  1765. // Count: max_number,
  1766. // ExpireDate: item.ExpiryDate,
  1767. // ProductDate: item.ProductDate,
  1768. // Price: item.Price,
  1769. // Manufacturer: item.Manufacturer,
  1770. // Dealer: item.Dealer,
  1771. // Number: item.Number,
  1772. // StorehouseId: item.StorehouseId,
  1773. // AdminUserId: item.AdminUserId,
  1774. // }
  1775. // service.UpdateDrugFlowSix(item.WarehouseOutId, item.DrugId, item.WarehouseOutOrderNumber, flow)
  1776. //
  1777. // service.UpdateWarehouseInfoTwentyFive(cha_number, item.DrugId, item.OrgId, storehouse_id, &medical)
  1778. //
  1779. //
  1780. // }
  1781. //
  1782. // if all_number == cha_number {
  1783. // warehouseInfo := models.XtDrugWarehouseInfo{
  1784. // Number: item.Number,
  1785. // ProductDate: item.ProductDate,
  1786. // ExpiryDate: item.ExpiryDate,
  1787. // Price: item.Price,
  1788. // TotalPrice: item.TotalPrice,
  1789. // Dealer: item.Dealer,
  1790. // Manufacturer: item.Manufacturer,
  1791. // Remark: item.Remark,
  1792. // BatchNumber: item.BatchNumber,
  1793. // MaxUnit: item.CountUnit,
  1794. // StorehouseId: item.StorehouseId,
  1795. // }
  1796. // parseDateErr := service.UpdateDrugWarehouseingInfoSix(item.ID, warehouseInfo)
  1797. //
  1798. // if parseDateErr != nil {
  1799. // utils.ErrorLog(errs.Error())
  1800. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1801. // return
  1802. // }
  1803. // fmt.Println("单位123223232323", item.CountUnit)
  1804. // fmt.Println("单位232232323323232", medical.MaxUnit)
  1805. //
  1806. // errs = service.UpDateDrugWarehouseOutInfo(item)
  1807. // flow := models.DrugFlow{
  1808. // Count: item.Count,
  1809. // ExpireDate: item.ExpiryDate,
  1810. // ProductDate: item.ProductDate,
  1811. // Price: item.Price,
  1812. // Manufacturer: item.Manufacturer,
  1813. // Dealer: item.Dealer,
  1814. // Number: item.Number,
  1815. // StorehouseId: item.StorehouseId,
  1816. // AdminUserId: item.AdminUserId,
  1817. // }
  1818. // service.UpdateDrugFlowSix(item.WarehouseOutId, item.DrugId, item.WarehouseOutOrderNumber, flow)
  1819. // if item.CountUnit == medical.MaxUnit {
  1820. // maxNumber = cha_number / medical.MinNumber
  1821. //
  1822. // parseDateErr := service.UpdateWarehouseInfoTwo(maxNumber, item.DrugId, item.OrgId)
  1823. //
  1824. // fmt.Println("parseDateErr", parseDateErr)
  1825. // if parseDateErr != nil {
  1826. // utils.ErrorLog(errs.Error())
  1827. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1828. // return
  1829. // }
  1830. //
  1831. // c.ServeSuccessJSON(map[string]interface{}{
  1832. // "msg": "2",
  1833. // "drug_name": "",
  1834. // "dose": "",
  1835. // "dose_unit": "",
  1836. // "min_number": "",
  1837. // "min_unit": "",
  1838. // "max_unit": "",
  1839. // })
  1840. // minNumber = cha_number % medical.MinNumber
  1841. // if minNumber < 0 {
  1842. // minNumber = 0
  1843. // }
  1844. // parseDateErr = service.UpdateWarehouseInfoFour(minNumber, item.DrugId, item.OrgId)
  1845. //
  1846. // if parseDateErr != nil {
  1847. // utils.ErrorLog(errs.Error())
  1848. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1849. // return
  1850. // }
  1851. //
  1852. // c.ServeSuccessJSON(map[string]interface{}{
  1853. // "msg": "2",
  1854. // "drug_name": "",
  1855. // "dose": "",
  1856. // "dose_unit": "",
  1857. // "min_number": "",
  1858. // "min_unit": "",
  1859. // "max_unit": "",
  1860. // })
  1861. // }
  1862. // if item.CountUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  1863. // parseDateErr := service.UpdateWarehouseInfoFour(cha_number, item.DrugId, item.OrgId)
  1864. //
  1865. // if parseDateErr != nil {
  1866. // utils.ErrorLog(errs.Error())
  1867. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1868. // return
  1869. // }
  1870. //
  1871. // c.ServeSuccessJSON(map[string]interface{}{
  1872. // "msg": "2",
  1873. // "drug_name": "",
  1874. // "dose": "",
  1875. // "dose_unit": "",
  1876. // "min_number": "",
  1877. // "min_unit": "",
  1878. // "max_unit": "",
  1879. // })
  1880. // }
  1881. //
  1882. // }
  1883. // if all_number < cha_number {
  1884. // goodObj, _ := service.GetDrugByGoodId(item.DrugId)
  1885. // c.ServeSuccessJSON(map[string]interface{}{
  1886. // "msg": "1",
  1887. // "drug_name": goodObj.DrugName,
  1888. // "dose": goodObj.Dose,
  1889. // "dose_unit": goodObj.DoseUnit,
  1890. // "min_number": goodObj.MinNumber,
  1891. // "min_unit": goodObj.MinUnit,
  1892. // "max_unit": goodObj.MaxUnit,
  1893. // })
  1894. // return
  1895. // }
  1896. //}
  1897. //
  1898. ////查询默认仓库
  1899. //houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  1900. ////查询默认仓库剩余多少库存
  1901. //list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  1902. //var sum_count int64
  1903. //var sum_in_count int64
  1904. //for _, it := range list {
  1905. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  1906. // if it.MaxUnit == baseDrug.MaxUnit {
  1907. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  1908. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  1909. //
  1910. // }
  1911. // sum_count += it.StockMaxNumber + it.StockMinNumber
  1912. // sum_in_count += it.WarehousingCount
  1913. //}
  1914. //service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  1915. }
  1916. }
  1917. if errs != nil {
  1918. utils.ErrorLog(errs.Error())
  1919. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockOutFail)
  1920. return
  1921. }
  1922. c.ServeSuccessJSON(map[string]interface{}{
  1923. "msg": "2",
  1924. "drug_name": "",
  1925. "dose": "",
  1926. "dose_unit": "",
  1927. "min_number": "",
  1928. "min_unit": "",
  1929. "max_unit": "",
  1930. })
  1931. }
  1932. func (c *StockDrugApiController) CreateDrugCancelStock() {
  1933. dealer_id, _ := c.GetInt64("dealer_id", 0)
  1934. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  1935. cancel_stock_time := c.GetString("time")
  1936. types, _ := c.GetInt64("type", 0)
  1937. storehouse_id, _ := c.GetInt64("storehouse_id")
  1938. cancelstock_id, _ := c.GetInt64("cancelstock_id")
  1939. cancelStockDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", cancel_stock_time)
  1940. if parseDateErr != nil {
  1941. c.ErrorLog("日期(%v)解析错误:%v", cancelStockDate, parseDateErr)
  1942. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1943. return
  1944. }
  1945. adminUserInfo := c.GetAdminUserInfo()
  1946. operation_time := time.Now().Unix()
  1947. creater := adminUserInfo.AdminUser.Id
  1948. ctime := time.Now().Unix()
  1949. timeStr := time.Now().Format("2006-01-02")
  1950. timeArr := strings.Split(timeStr, "-")
  1951. total, _ := service.FindAllDrugCancelStockTotal(adminUserInfo.CurrentOrgId)
  1952. total = total + 1
  1953. orderNumber := "CKTKD" + strconv.FormatInt(adminUserInfo.CurrentOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1954. cancelStock := models.DrugCancelStock{
  1955. OrderNumber: orderNumber,
  1956. OperaTime: operation_time,
  1957. OrgId: adminUserInfo.CurrentOrgId,
  1958. Creater: creater,
  1959. Ctime: ctime,
  1960. Status: 1,
  1961. ReturnTime: cancelStockDate.Unix(),
  1962. Dealer: dealer_id,
  1963. Manufacturer: manufacturer_id,
  1964. Type: types,
  1965. StorehouseId: storehouse_id,
  1966. IsCheck: 2,
  1967. }
  1968. if cancelstock_id == 0 {
  1969. service.AddSigleDrugCancelStock(&cancelStock)
  1970. }
  1971. if cancelstock_id > 0 {
  1972. service.UpdateSingleDrugCancelStock(cancelstock_id, storehouse_id)
  1973. }
  1974. cancelLastStock, _ := service.GetLastDrugCancelStockById(adminUserInfo.CurrentOrgId)
  1975. dataBody := make(map[string]interface{}, 0)
  1976. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  1977. if err != nil {
  1978. utils.ErrorLog(err.Error())
  1979. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1980. return
  1981. }
  1982. var cancelStockInfos []*models.DrugCancelStockInfo
  1983. var drugFlow []*models.DrugFlow
  1984. if dataBody["cancelStock"] != nil && reflect.TypeOf(dataBody["cancelStock"]).String() == "[]interface {}" {
  1985. thisStockIn, _ := dataBody["cancelStock"].([]interface{})
  1986. if len(thisStockIn) > 0 {
  1987. for _, item := range thisStockIn {
  1988. items := item.(map[string]interface{})
  1989. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  1990. utils.ErrorLog("id")
  1991. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1992. return
  1993. }
  1994. id := int64(items["id"].(float64))
  1995. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  1996. utils.ErrorLog("drug_id")
  1997. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1998. return
  1999. }
  2000. drug_id := int64(items["drug_id"].(float64))
  2001. return_count, _ := items["return_count"].(string)
  2002. if len(return_count) == 0 {
  2003. utils.ErrorLog("len(return_count) == 0")
  2004. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2005. return
  2006. }
  2007. count, _ := strconv.ParseInt(return_count, 10, 64)
  2008. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  2009. utils.ErrorLog("price")
  2010. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2011. return
  2012. }
  2013. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  2014. total := float64(count) * price
  2015. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  2016. utils.ErrorLog("retail_price")
  2017. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2018. return
  2019. }
  2020. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  2021. retail_price_total := float64(count) * retail_price
  2022. register_account, _ := items["register_account"].(string)
  2023. remark, _ := items["remark"].(string)
  2024. var productDates int64
  2025. var expiryDates int64
  2026. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  2027. expiryDates = 0
  2028. } else {
  2029. if len(items["expiry_date"].(string)) == 0 {
  2030. expiryDates = 0
  2031. } else {
  2032. expiryDate, _ := items["expiry_date"].(string)
  2033. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  2034. expiryDates = expiry_date.Unix()
  2035. }
  2036. }
  2037. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  2038. productDates = 0
  2039. } else {
  2040. if len(items["product_date"].(string)) == 0 {
  2041. productDates = 0
  2042. } else {
  2043. productDate, _ := items["product_date"].(string)
  2044. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  2045. productDates = product_date.Unix()
  2046. }
  2047. }
  2048. manufacturer, _ := items["manufacturer"].(string)
  2049. dealer, _ := items["dealer"].(string)
  2050. batch_number := items["batch_number"].(string)
  2051. max_unit := items["max_unit"].(string)
  2052. batch_number_id := int64(items["batch_number_id"].(float64))
  2053. var manufacturer_id int64
  2054. var dealer_id int64
  2055. manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
  2056. dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
  2057. for _, item := range manufacturerList {
  2058. if manufacturer == item.ManufacturerName {
  2059. manufacturer_id = item.ID
  2060. }
  2061. }
  2062. for _, item := range dealerList {
  2063. if dealer == item.DealerName {
  2064. dealer_id = item.ID
  2065. }
  2066. }
  2067. cancelInfo, _ := service.GetLastDrugCancelStockById(adminUserInfo.CurrentOrgId)
  2068. cancelStockInfo := &models.DrugCancelStockInfo{
  2069. ID: id,
  2070. OrderNumber: cancelInfo.OrderNumber,
  2071. CancelStockId: cancelInfo.ID,
  2072. DrugId: drug_id,
  2073. Count: count,
  2074. Status: 1,
  2075. Ctime: ctime,
  2076. OrgId: adminUserInfo.CurrentOrgId,
  2077. Type: types,
  2078. Manufacturer: manufacturer,
  2079. Dealer: dealer,
  2080. Total: total,
  2081. RetailPrice: retail_price,
  2082. RetailTotalPrice: retail_price_total,
  2083. Price: price,
  2084. RegisterAccount: register_account,
  2085. Remark: remark,
  2086. BatchNumber: batch_number,
  2087. MaxUnit: max_unit,
  2088. ProductDate: productDates,
  2089. ExpiryDate: expiryDates,
  2090. BatchNumberId: batch_number_id,
  2091. StorehouseId: storehouse_id,
  2092. IsCheck: 2,
  2093. }
  2094. cancelStockInfos = append(cancelStockInfos, cancelStockInfo)
  2095. flow := &models.DrugFlow{
  2096. WarehousingId: 0,
  2097. DrugId: drug_id,
  2098. Number: "",
  2099. BatchNumber: batch_number,
  2100. Count: count,
  2101. UserOrgId: adminUserInfo.CurrentOrgId,
  2102. PatientId: 0,
  2103. SystemTime: ctime,
  2104. ConsumableType: 4,
  2105. IsSys: 0,
  2106. WarehousingOrder: "",
  2107. WarehouseOutId: 0,
  2108. WarehouseOutOrderNumber: "",
  2109. IsEdit: 0,
  2110. CancelStockId: cancelStock.ID,
  2111. CancelOrderNumber: cancelStock.OrderNumber,
  2112. Manufacturer: manufacturer_id,
  2113. Dealer: dealer_id,
  2114. Creator: adminUserInfo.AdminUser.Id,
  2115. UpdateCreator: 0,
  2116. Status: 1,
  2117. Ctime: time.Now().Unix(),
  2118. Mtime: 0,
  2119. Price: price,
  2120. WarehousingDetailId: 0,
  2121. WarehouseOutDetailId: 0,
  2122. CancelOutDetailId: 0,
  2123. ExpireDate: expiryDates,
  2124. ProductDate: productDates,
  2125. MaxUnit: max_unit,
  2126. MinUnit: "",
  2127. StorehouseId: storehouse_id,
  2128. }
  2129. drugFlow = append(drugFlow, flow)
  2130. }
  2131. }
  2132. }
  2133. for _, item := range cancelStockInfos {
  2134. if item.ID == 0 {
  2135. service.CreateCancelStockInfoTwo(item)
  2136. }
  2137. if item.ID > 0 {
  2138. service.ModifyDrugCancelStockInfo(item.ID, item)
  2139. }
  2140. }
  2141. list, _ := service.GetLastCancelStockByList(cancelLastStock.ID, adminUserInfo.CurrentOrgId)
  2142. manufacturerList, _ := service.GetAllManufacturerList(adminUserInfo.CurrentOrgId)
  2143. dealerList, _ := service.GetAllDealerList(adminUserInfo.CurrentOrgId)
  2144. var drugType = "药品类型"
  2145. drugTypeParent, _ := service.GetDrugDataConfig(0, drugType)
  2146. drugTypeList, _ := service.GetParentDataConfig(drugTypeParent.ID, adminUserInfo.CurrentOrgId)
  2147. c.ServeSuccessJSON(map[string]interface{}{
  2148. "cancelStock": cancelLastStock,
  2149. "cancelLastStock": cancelLastStock,
  2150. "list": list,
  2151. "manufacturerList": manufacturerList,
  2152. "dealerList": dealerList,
  2153. "drugTypeList": drugTypeList,
  2154. "msg": "1",
  2155. })
  2156. //扣减库存逻辑
  2157. //for _, item := range cancelStockInfos {
  2158. //
  2159. // var total_number int64
  2160. // var out_number int64
  2161. // var can_number int64
  2162. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  2163. // //将当前退库数转为最小单位
  2164. // if item.MaxUnit == medical.MaxUnit {
  2165. // total_number = item.Count * medical.MinNumber
  2166. // }
  2167. // if item.MaxUnit == medical.MinUnit {
  2168. // total_number = item.Count
  2169. // }
  2170. // //查询当前药品退库的批次号的总入库数
  2171. // infoWareInfo, _ := service.GetDrugWarehouseInfo(item.BatchNumberId)
  2172. // var total_count int64
  2173. // total_count = infoWareInfo.WarehousingCount * medical.MinNumber
  2174. //
  2175. // //查询该批次的出库数量
  2176. // outInfo, _ := service.GetDrugWarehouseOutInfo(item.BatchNumberId, item.DrugId)
  2177. //
  2178. // //查询该批次总的退库数量
  2179. // canInfo, _ := service.GetCancelDrugStockOutInfo(item.BatchNumberId, item.DrugId)
  2180. //
  2181. // for _, it := range canInfo {
  2182. // if it.MaxUnit == medical.MaxUnit {
  2183. // it.Count = it.Count * medical.MinNumber
  2184. // }
  2185. // if it.MaxUnit == medical.MinUnit {
  2186. // it.Count = it.Count
  2187. // }
  2188. // }
  2189. //
  2190. // for _, it := range canInfo {
  2191. // can_number += it.Count
  2192. // }
  2193. //
  2194. // for _, it := range outInfo {
  2195. // if it.CountUnit == medical.MaxUnit {
  2196. // it.Count = it.Count * medical.MinNumber
  2197. // }
  2198. // if it.CountUnit == medical.MinUnit {
  2199. // it.Count = it.Count
  2200. // }
  2201. // }
  2202. // for _, it := range outInfo {
  2203. // out_number += it.Count
  2204. // }
  2205. //
  2206. // if out_number == 0 {
  2207. // service.UpdateDrugCancel(item.CancelStockId)
  2208. // c.ServeSuccessJSON(map[string]interface{}{
  2209. // "msg": "2",
  2210. // })
  2211. // return
  2212. // }
  2213. // fmt.Println("当前退库数", total_count)
  2214. // fmt.Println("当前批次总库存", total_count)
  2215. // fmt.Println("总出库数", out_number)
  2216. // //判断退库数量是否大于总入库数量
  2217. // if total_number > total_count {
  2218. // service.UpdateDrugCancel(item.CancelStockId)
  2219. // c.ServeSuccessJSON(map[string]interface{}{
  2220. // "msg": "2",
  2221. // })
  2222. // return
  2223. // }
  2224. //
  2225. // //判断总退库数量是否大于出库数量
  2226. //
  2227. // if can_number+total_number > out_number {
  2228. // //清空退库单
  2229. // service.UpdateDrugCancel(item.CancelStockId)
  2230. // c.ServeSuccessJSON(map[string]interface{}{
  2231. // "msg": "2",
  2232. // })
  2233. // return
  2234. // }
  2235. //
  2236. // //正常退库
  2237. // if total_number <= out_number {
  2238. // //创建退库详情
  2239. // errs := service.CreateCancelStockInfoTwo(item)
  2240. // lastInfo, _ := service.GetLastStockInfoThree(item.DrugId)
  2241. // //创建库存明细
  2242. // flows := models.DrugFlow{
  2243. // WarehousingId: 0,
  2244. // DrugId: item.DrugId,
  2245. // Number: "",
  2246. // BatchNumber: item.BatchNumber,
  2247. // Count: item.Count,
  2248. // UserOrgId: adminUserInfo.CurrentOrgId,
  2249. // PatientId: 0,
  2250. // SystemTime: ctime,
  2251. // ConsumableType: 4,
  2252. // IsSys: 0,
  2253. // WarehousingOrder: infoWareInfo.WarehousingOrder,
  2254. // WarehouseOutId: 0,
  2255. // WarehouseOutOrderNumber: "",
  2256. // IsEdit: 0,
  2257. // CancelStockId: cancelStock.ID,
  2258. // CancelOrderNumber: cancelStock.OrderNumber,
  2259. // Manufacturer: manufacturer_id,
  2260. // Dealer: dealer_id,
  2261. // Creator: adminUserInfo.AdminUser.Id,
  2262. // UpdateCreator: 0,
  2263. // Status: 1,
  2264. // Ctime: time.Now().Unix(),
  2265. // Mtime: 0,
  2266. // Price: item.Price,
  2267. // WarehousingDetailId: 0,
  2268. // WarehouseOutDetailId: 0,
  2269. // CancelOutDetailId: lastInfo.ID,
  2270. // ExpireDate: item.ExpiryDate,
  2271. // ProductDate: item.ProductDate,
  2272. // MaxUnit: item.MaxUnit,
  2273. // MinUnit: "",
  2274. // StorehouseId: storehouse_id,
  2275. // }
  2276. // errs = service.CreateDrugFlowOne(flows)
  2277. //
  2278. // var total int64
  2279. //
  2280. // if medical.MaxUnit == item.MaxUnit {
  2281. // total = item.Count
  2282. //
  2283. // warehouseInfo := models.XtDrugWarehouseInfo{
  2284. // StockMaxNumber: total,
  2285. // }
  2286. //
  2287. // errs = service.UpdateDrugWarehouseInfo(&warehouseInfo, item.BatchNumberId)
  2288. // //查询默认仓库
  2289. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2290. // //查询默认仓库剩余多少库存
  2291. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2292. // var sum_count int64
  2293. // var sum_in_count int64
  2294. // for _, it := range list {
  2295. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2296. // if it.MaxUnit == baseDrug.MaxUnit {
  2297. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2298. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2299. // }
  2300. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2301. // sum_in_count += it.WarehousingCount
  2302. // }
  2303. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2304. // if errs != nil {
  2305. // utils.ErrorLog(errs.Error())
  2306. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2307. // return
  2308. // }
  2309. //
  2310. // c.ServeSuccessJSON(map[string]interface{}{
  2311. // "msg": "1",
  2312. // })
  2313. // }
  2314. //
  2315. // if medical.MinUnit == item.MaxUnit && medical.MaxUnit != medical.MinUnit {
  2316. // total = item.Count
  2317. // warehouseInfo := models.XtDrugWarehouseInfo{
  2318. // StockMinNumber: total,
  2319. // }
  2320. //
  2321. // errs = service.UpdateDrugWarehouseInfoOne(&warehouseInfo, item.BatchNumberId)
  2322. // //查询默认仓库
  2323. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2324. // //查询默认仓库剩余多少库存
  2325. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2326. // var sum_count int64
  2327. // var sum_in_count int64
  2328. // for _, it := range list {
  2329. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2330. // if it.MaxUnit == baseDrug.MaxUnit {
  2331. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2332. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2333. // }
  2334. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2335. // sum_in_count += it.WarehousingCount
  2336. // }
  2337. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2338. // if errs != nil {
  2339. // utils.ErrorLog(errs.Error())
  2340. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2341. // return
  2342. // }
  2343. //
  2344. // c.ServeSuccessJSON(map[string]interface{}{
  2345. // "msg": "1",
  2346. // })
  2347. // }
  2348. // }
  2349. //
  2350. //}
  2351. }
  2352. func (c *StockDrugApiController) GetDrugCancelStockInfoList() {
  2353. id, _ := c.GetInt64("id", 0)
  2354. adminUserInfo := c.GetAdminUserInfo()
  2355. cancelStockInfoList, _ := service.FindDrugCancelStockInfoById(id, adminUserInfo.CurrentOrgId)
  2356. info, _ := service.FindCancelDrugById(id, adminUserInfo.CurrentOrgId)
  2357. list, _ := service.GetAllStoreHouseList(adminUserInfo.CurrentOrgId)
  2358. c.ServeSuccessJSON(map[string]interface{}{
  2359. "list": cancelStockInfoList,
  2360. "info": info,
  2361. "houselist": list,
  2362. })
  2363. }
  2364. func (c *StockDrugApiController) GetDrugCancelStockList() {
  2365. page, _ := c.GetInt64("page", -1)
  2366. limit, _ := c.GetInt64("limit", -1)
  2367. start_time := c.GetString("start_time")
  2368. end_time := c.GetString("end_time")
  2369. types, _ := c.GetInt64("type", 0)
  2370. keywords := c.GetString("keywords")
  2371. storehouse_id, _ := c.GetInt64("storehouse_id")
  2372. check_type, _ := c.GetInt64("check_type")
  2373. timeLayout := "2006-01-02"
  2374. loc, _ := time.LoadLocation("Local")
  2375. var startTime int64
  2376. if len(start_time) > 0 {
  2377. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  2378. if err != nil {
  2379. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2380. return
  2381. }
  2382. startTime = theTime.Unix()
  2383. }
  2384. var endTime int64
  2385. if len(end_time) > 0 {
  2386. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  2387. if err != nil {
  2388. utils.ErrorLog(err.Error())
  2389. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2390. return
  2391. }
  2392. endTime = theTime.Unix()
  2393. }
  2394. adminUserInfo := c.GetAdminUserInfo()
  2395. returnList, total, err := service.FindAllDrugCancelList(adminUserInfo.CurrentOrgId, page, limit, startTime, endTime, types, keywords, storehouse_id, check_type)
  2396. houseList, _ := service.GetAllStoreHouseList(adminUserInfo.CurrentOrgId)
  2397. if err == nil {
  2398. c.ServeSuccessJSON(map[string]interface{}{
  2399. "list": returnList,
  2400. "total": total,
  2401. "houseList": houseList,
  2402. })
  2403. } else {
  2404. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  2405. }
  2406. }
  2407. func (this *StockDrugApiController) DeleteDrugCancelStock() {
  2408. ids := this.GetString("ids")
  2409. if len(ids) == 0 {
  2410. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2411. return
  2412. }
  2413. idArray := strings.Split(ids, ",")
  2414. ////获取入库数据
  2415. //list, _ := service.GetDrugWareInfoByCancelId(idArray)
  2416. //for _, item := range list {
  2417. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  2418. // if item.MaxUnit == medical.MaxUnit {
  2419. // //回退库存
  2420. // service.ModifyDrugWarehouseByInfoId(item.BatchNumberId, item.Count)
  2421. //
  2422. // }
  2423. // if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  2424. // service.ModifyDrugWarehouseByStockMin(item.BatchNumberId, item.Count)
  2425. // }
  2426. //
  2427. // //查询默认仓库
  2428. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2429. // //查询默认仓库剩余多少库存
  2430. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2431. // var sum_count int64
  2432. // var sum_in_count int64
  2433. // for _, it := range list {
  2434. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2435. // if it.MaxUnit == baseDrug.MaxUnit {
  2436. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2437. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2438. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2439. // sum_in_count += it.WarehousingCount
  2440. // }
  2441. // }
  2442. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2443. //}
  2444. err := service.DeleteDrugCancelStock(idArray)
  2445. if err != nil {
  2446. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  2447. } else {
  2448. this.ServeSuccessJSON(map[string]interface{}{
  2449. "msg": "删除成功",
  2450. })
  2451. }
  2452. }
  2453. func (this *StockDrugApiController) DeleteDrugCancelStockInfo() {
  2454. id, _ := this.GetInt64("id", 0)
  2455. if id == 0 {
  2456. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2457. return
  2458. }
  2459. ////查询
  2460. //cancelinfo, _ := service.GetDrugCancelById(id)
  2461. //
  2462. //drug, _ := service.GetBaseDrugMedical(cancelinfo.DrugId)
  2463. //if drug.MaxUnit == cancelinfo.MaxUnit {
  2464. // service.ModeFyDrugWareseInfo(cancelinfo.BatchNumberId, cancelinfo.Count)
  2465. //
  2466. //}
  2467. //if drug.MinUnit == cancelinfo.MaxUnit && drug.MaxUnit != drug.MinUnit {
  2468. // service.ModeFyDrugWarehouseMin(cancelinfo.BatchNumberId, cancelinfo.Count)
  2469. //}
  2470. //
  2471. err := service.UpDateDrugCancleStockStatus(id)
  2472. //删除流水
  2473. // service.UpDateDrugCancelStockById(cancelinfo.ID)
  2474. //查询默认仓库
  2475. //houseConfig, _ := service.GetAllStoreHouseConfig(cancelinfo.OrgId)
  2476. ////查询默认仓库剩余多少库存
  2477. //list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, cancelinfo.OrgId, cancelinfo.DrugId)
  2478. //var sum_count int64
  2479. //var sum_in_count int64
  2480. //for _, it := range list {
  2481. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2482. // if it.MaxUnit == baseDrug.MaxUnit {
  2483. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2484. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2485. // }
  2486. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2487. // sum_in_count += it.WarehousingCount
  2488. //}
  2489. //service.UpdateMedicalSumCount(cancelinfo.DrugId, sum_count, sum_in_count, cancelinfo.OrgId)
  2490. if err != nil {
  2491. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteFail)
  2492. } else {
  2493. this.ServeSuccessJSON(map[string]interface{}{
  2494. "msg": "删除成功",
  2495. })
  2496. }
  2497. }
  2498. func (c *StockDrugApiController) EditDrugCancelStock() {
  2499. cancel_time := c.GetString("cancel_time")
  2500. id, _ := c.GetInt64("id", 0)
  2501. types, _ := c.GetInt64("type", 0)
  2502. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  2503. dealer_id, _ := c.GetInt64("dealer_id", 0)
  2504. storehouse_id, _ := c.GetInt64("storehouse_id")
  2505. if id == 0 {
  2506. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2507. return
  2508. }
  2509. ctime := time.Now().Unix()
  2510. mtime := time.Now().Unix()
  2511. adminUserInfo := c.GetAdminUserInfo()
  2512. cancelDate, parseDateErr := utils.ParseTimeStringToTime("2006-01-02", cancel_time)
  2513. if parseDateErr != nil {
  2514. c.ErrorLog("日期(%v)解析错误:%v", cancelDate, parseDateErr)
  2515. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2516. return
  2517. }
  2518. cancelStock, _ := service.FindDrugCancelStockById(id)
  2519. tempCancelStock := models.DrugCancelStock{
  2520. ID: cancelStock.ID,
  2521. Mtime: mtime,
  2522. ReturnTime: cancelDate.Unix(),
  2523. Manufacturer: manufacturer_id,
  2524. Dealer: dealer_id,
  2525. StorehouseId: storehouse_id,
  2526. IsCheck: 1,
  2527. }
  2528. service.EditDrugCancelStock(tempCancelStock)
  2529. dataBody := make(map[string]interface{}, 0)
  2530. err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody)
  2531. if err != nil {
  2532. utils.ErrorLog(err.Error())
  2533. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2534. return
  2535. }
  2536. var cancelStockInfos []*models.DrugCancelStockInfo
  2537. var upDateCancelStockInfos []*models.DrugCancelStockInfo
  2538. var drugFlow []*models.DrugFlow
  2539. if dataBody["cancelStock"] != nil && reflect.TypeOf(dataBody["cancelStock"]).String() == "[]interface {}" {
  2540. thisCancelStock, _ := dataBody["cancelStock"].([]interface{})
  2541. if len(thisCancelStock) > 0 {
  2542. for _, item := range thisCancelStock {
  2543. items := item.(map[string]interface{})
  2544. if items["drug_id"] == nil || reflect.TypeOf(items["drug_id"]).String() != "float64" {
  2545. utils.ErrorLog("drug_id")
  2546. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2547. return
  2548. }
  2549. drug_id := int64(items["drug_id"].(float64))
  2550. if items["count"] == nil || reflect.TypeOf(items["count"]).String() != "string" {
  2551. utils.ErrorLog("count")
  2552. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2553. return
  2554. }
  2555. count, _ := strconv.ParseInt(items["count"].(string), 10, 64)
  2556. if items["price"] == nil || reflect.TypeOf(items["price"]).String() != "string" {
  2557. utils.ErrorLog("price")
  2558. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2559. return
  2560. }
  2561. price, _ := strconv.ParseFloat(items["price"].(string), 64)
  2562. total := float64(count) * price
  2563. if items["retail_price"] == nil || reflect.TypeOf(items["retail_price"]).String() != "string" {
  2564. utils.ErrorLog("retail_price")
  2565. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2566. return
  2567. }
  2568. retail_price, _ := strconv.ParseFloat(items["retail_price"].(string), 64)
  2569. retail_price_total := float64(count) * retail_price
  2570. if items["id"] == nil || reflect.TypeOf(items["id"]).String() != "float64" {
  2571. utils.ErrorLog("id")
  2572. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  2573. return
  2574. }
  2575. id := int64(items["id"].(float64))
  2576. max_unit := items["max_unit"].(string)
  2577. batch_number := items["batch_number"].(string)
  2578. register_account := items["register_account"].(string)
  2579. remark := items["remark"].(string)
  2580. manufacturer := items["manufacturer"].(string)
  2581. dealer := items["dealer"].(string)
  2582. var productDates int64
  2583. var expiryDates int64
  2584. if items["expiry_date"] == nil || reflect.TypeOf(items["expiry_date"]).String() != "string" {
  2585. expiryDates = 0
  2586. } else {
  2587. if len(items["expiry_date"].(string)) == 0 {
  2588. expiryDates = 0
  2589. } else {
  2590. expiryDate, _ := items["expiry_date"].(string)
  2591. expiry_date, _ := utils.ParseTimeStringToTime("2006-01-02", expiryDate)
  2592. expiryDates = expiry_date.Unix()
  2593. }
  2594. }
  2595. if items["product_date"] == nil || reflect.TypeOf(items["product_date"]).String() != "string" {
  2596. productDates = 0
  2597. } else {
  2598. if len(items["product_date"].(string)) == 0 {
  2599. productDates = 0
  2600. } else {
  2601. productDate, _ := items["product_date"].(string)
  2602. product_date, _ := utils.ParseTimeStringToTime("2006-01-02", productDate)
  2603. productDates = product_date.Unix()
  2604. }
  2605. }
  2606. batch_number_id := int64(items["batch_number_id"].(float64))
  2607. if id == 0 {
  2608. cancelStockInfo := &models.DrugCancelStockInfo{
  2609. DrugId: drug_id,
  2610. Count: count,
  2611. Status: 1,
  2612. Ctime: ctime,
  2613. OrgId: adminUserInfo.CurrentOrgId,
  2614. OrderNumber: cancelStock.OrderNumber,
  2615. CancelStockId: cancelStock.ID,
  2616. Mtime: time.Now().Unix(),
  2617. Type: types,
  2618. RetailPrice: retail_price,
  2619. RetailTotalPrice: retail_price_total,
  2620. Price: price,
  2621. Total: total,
  2622. Manufacturer: manufacturer,
  2623. Dealer: dealer,
  2624. RegisterAccount: register_account,
  2625. MaxUnit: max_unit,
  2626. BatchNumber: batch_number,
  2627. Remark: remark,
  2628. ProductDate: productDates,
  2629. ExpiryDate: expiryDates,
  2630. BatchNumberId: batch_number_id,
  2631. StorehouseId: storehouse_id,
  2632. }
  2633. cancelStockInfos = append(cancelStockInfos, cancelStockInfo)
  2634. } else {
  2635. cancelStockInfo := &models.DrugCancelStockInfo{
  2636. ID: id,
  2637. DrugId: drug_id,
  2638. Count: count,
  2639. Status: 1,
  2640. Ctime: ctime,
  2641. OrgId: adminUserInfo.CurrentOrgId,
  2642. OrderNumber: cancelStock.OrderNumber,
  2643. CancelStockId: cancelStock.ID,
  2644. Mtime: time.Now().Unix(),
  2645. Type: types,
  2646. RetailPrice: retail_price,
  2647. RetailTotalPrice: retail_price_total,
  2648. Price: price,
  2649. Total: total,
  2650. Manufacturer: manufacturer,
  2651. Dealer: dealer,
  2652. RegisterAccount: register_account,
  2653. MaxUnit: max_unit,
  2654. BatchNumber: batch_number,
  2655. Remark: remark,
  2656. ProductDate: productDates,
  2657. ExpiryDate: expiryDates,
  2658. BatchNumberId: batch_number_id,
  2659. StorehouseId: storehouse_id,
  2660. }
  2661. upDateCancelStockInfos = append(upDateCancelStockInfos, cancelStockInfo)
  2662. }
  2663. flow := &models.DrugFlow{
  2664. WarehousingId: 0,
  2665. DrugId: drug_id,
  2666. Number: "",
  2667. BatchNumber: batch_number,
  2668. Count: count,
  2669. UserOrgId: adminUserInfo.CurrentOrgId,
  2670. PatientId: 0,
  2671. SystemTime: time.Now().Unix(),
  2672. ConsumableType: 4,
  2673. IsSys: 0,
  2674. WarehousingOrder: "",
  2675. WarehouseOutId: 0,
  2676. WarehouseOutOrderNumber: "",
  2677. IsEdit: 0,
  2678. CancelStockId: cancelStock.ID,
  2679. CancelOrderNumber: cancelStock.OrderNumber,
  2680. Manufacturer: 0,
  2681. Dealer: 0,
  2682. Creator: adminUserInfo.AdminUser.Id,
  2683. UpdateCreator: adminUserInfo.AdminUser.Id,
  2684. Status: 1,
  2685. Ctime: time.Now().Unix(),
  2686. Mtime: 0,
  2687. Price: price,
  2688. WarehousingDetailId: 0,
  2689. WarehouseOutDetailId: 0,
  2690. CancelOutDetailId: 0,
  2691. ExpireDate: expiryDates,
  2692. ProductDate: productDates,
  2693. MaxUnit: max_unit,
  2694. MinUnit: "",
  2695. StorehouseId: storehouse_id,
  2696. }
  2697. drugFlow = append(drugFlow, flow)
  2698. }
  2699. }
  2700. }
  2701. if len(cancelStockInfos) > 0 {
  2702. service.CreateDrugCancelStockInfo(cancelStockInfos)
  2703. }
  2704. if len(upDateCancelStockInfos) > 0 {
  2705. for _, item := range upDateCancelStockInfos {
  2706. service.UpDateDrugCancelStockInfo(item)
  2707. }
  2708. }
  2709. //var errs error
  2710. //if len(cancelStockInfos) > 0 {
  2711. //
  2712. // //扣减库存逻辑
  2713. // for _, item := range cancelStockInfos {
  2714. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  2715. // //查询当前药品退库的批次号的总入库数
  2716. // infoWareInfo, _ := service.GetDrugWarehouseInfo(item.BatchNumberId)
  2717. //
  2718. // var total_count int64
  2719. // var cancel_count int64
  2720. // var out_count int64
  2721. // total_count = infoWareInfo.StockMaxNumber*medical.MinNumber + infoWareInfo.StockMinNumber
  2722. // //转化为最小单位
  2723. // var total_number int64
  2724. // if item.MaxUnit == medical.MaxUnit {
  2725. // total_number = item.Count * medical.MinNumber
  2726. // }
  2727. // if item.MaxUnit == medical.MinUnit {
  2728. // total_number = item.Count
  2729. // }
  2730. //
  2731. // //统计该批次的总出库数
  2732. // outInfo, _ := service.GetDrugWarehouseOutInfo(item.BatchNumberId, item.DrugId)
  2733. // for _, item := range outInfo {
  2734. // if item.CountUnit == medical.MaxUnit {
  2735. // item.Count = item.Count * medical.MinNumber
  2736. // }
  2737. // if item.CountUnit == medical.MinUnit {
  2738. // item.Count = item.Count
  2739. // }
  2740. // }
  2741. //
  2742. // for _, item := range outInfo {
  2743. // out_count += item.Count
  2744. // }
  2745. //
  2746. // //判断退库数量是否大于出库数量
  2747. // cancelInfo, _ := service.GetCancelDrugStockOutInfo(item.BatchNumberId, item.DrugId)
  2748. // for _, item := range cancelInfo {
  2749. // if item.MaxUnit == medical.MaxUnit {
  2750. // item.Count = item.Count * medical.MinNumber
  2751. // }
  2752. // if item.MaxUnit == medical.MinUnit {
  2753. // item.Count = item.Count
  2754. // }
  2755. // }
  2756. //
  2757. // for _, item := range cancelInfo {
  2758. // cancel_count += item.Count
  2759. // }
  2760. //
  2761. // if total_number+cancel_count > out_count {
  2762. // c.ServeSuccessJSON(map[string]interface{}{
  2763. // "msg": "4",
  2764. // })
  2765. // return
  2766. // }
  2767. //
  2768. // //判断退库数量是否大于总入库数量
  2769. // if total_number > total_count {
  2770. // c.ServeSuccessJSON(map[string]interface{}{
  2771. // "msg": "2",
  2772. // })
  2773. // return
  2774. // }
  2775. //
  2776. // //正常退库
  2777. // if total_number <= total_count {
  2778. // //创建退库详情
  2779. // errs := service.CreateDrugCancelStockInfo(cancelStockInfos)
  2780. // //创建库存明细
  2781. // //查询这个药品这个订单是否已存在流水
  2782. // for _, items := range drugFlow {
  2783. // drugwarehouseinfo, _ := service.GetLastDrugCancelStockInfoById(items.DrugId)
  2784. // items.CancelOutDetailId = drugwarehouseinfo.ID
  2785. // _, errsId := service.GetCancelStockId(items.CancelStockId, items.DrugId)
  2786. // if errsId == gorm.ErrRecordNotFound {
  2787. //
  2788. // errs = service.CreateDrugFlowTwo(items)
  2789. // }
  2790. // }
  2791. //
  2792. // //获取该批次最后一条退库详情
  2793. // lastCancel, _ := service.GetLastCancelStockInfo(id)
  2794. // var cancel_number int64
  2795. // if lastCancel.MaxUnit == medical.MaxUnit {
  2796. // cancel_number = lastCancel.Count * medical.MinNumber
  2797. // }
  2798. //
  2799. // if lastCancel.MaxUnit == medical.MinUnit {
  2800. // cancel_number = lastCancel.Count
  2801. // }
  2802. //
  2803. // var total int64
  2804. //
  2805. // if medical.MaxUnit == item.MaxUnit {
  2806. // total = item.Count - cancel_number
  2807. //
  2808. // warehouseInfo := models.XtDrugWarehouseInfo{
  2809. // StockMaxNumber: total,
  2810. // }
  2811. // errs = service.UpdateDrugWarehouseInfo(&warehouseInfo, item.BatchNumberId)
  2812. //
  2813. // if errs != nil {
  2814. // utils.ErrorLog(errs.Error())
  2815. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2816. // return
  2817. // }
  2818. //
  2819. // c.ServeSuccessJSON(map[string]interface{}{
  2820. // "msg": "1",
  2821. // })
  2822. // }
  2823. // if medical.MinUnit == item.MaxUnit && medical.MaxUnit != medical.MinUnit {
  2824. // total = item.Count - cancel_number
  2825. // warehouseInfo := models.XtDrugWarehouseInfo{
  2826. // StockMinNumber: total,
  2827. // }
  2828. //
  2829. // errs = service.UpdateDrugWarehouseInfoOne(&warehouseInfo, item.BatchNumberId)
  2830. //
  2831. // //查询默认仓库
  2832. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2833. // //查询默认仓库剩余多少库存
  2834. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2835. // var sum_count int64
  2836. // var sum_in_count int64
  2837. // for _, it := range list {
  2838. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2839. // if it.MaxUnit == baseDrug.MaxUnit {
  2840. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2841. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2842. // }
  2843. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2844. // sum_in_count += it.WarehousingCount
  2845. // }
  2846. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2847. //
  2848. // if errs != nil {
  2849. // utils.ErrorLog(errs.Error())
  2850. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCancelStockFail)
  2851. // return
  2852. // }
  2853. //
  2854. // c.ServeSuccessJSON(map[string]interface{}{
  2855. // "msg": "1",
  2856. // })
  2857. // }
  2858. //
  2859. // }
  2860. //
  2861. // }
  2862. //}
  2863. //if len(drugFlow) > 0 {
  2864. // for _, items := range drugFlow {
  2865. // _, errsId := service.GetCancelStockId(items.CancelStockId, items.DrugId)
  2866. // if errsId == gorm.ErrRecordNotFound {
  2867. // errs = service.CreateDrugFlowTwo(items)
  2868. // }
  2869. // }
  2870. //}
  2871. //
  2872. //if len(upDateCancelStockInfos) > 0 {
  2873. // var total int64
  2874. // var out_count int64
  2875. // var cancel_count int64
  2876. // var now_count int64
  2877. // var all_count int64
  2878. // for _, item := range upDateCancelStockInfos {
  2879. //
  2880. // //查询最后一条退库记录
  2881. // info, _ := service.GetLastCancelStockInfoOne(id, item.DrugId)
  2882. //
  2883. // errs = service.UpDateDrugCancelStockInfo(item)
  2884. //
  2885. // //查询该药品的信息
  2886. // medical, _ := service.GetBaseDrugMedical(item.DrugId)
  2887. // //统计该批次的总出库数
  2888. // outInfo, _ := service.GetDrugWarehouseOutInfo(item.BatchNumberId, item.DrugId)
  2889. // //查询该批次的总退库数量
  2890. // cancelInfo, _ := service.GetTotalDrugCancelInfo(item.BatchNumberId, item.DrugId, item.ID)
  2891. // for _, item := range outInfo {
  2892. // if item.CountUnit == medical.MaxUnit {
  2893. // item.Count = item.Count * medical.MinNumber
  2894. // }
  2895. // if item.CountUnit == medical.MinUnit {
  2896. // item.Count = item.Count
  2897. // }
  2898. // }
  2899. //
  2900. // for _, item := range outInfo {
  2901. // out_count += item.Count
  2902. // }
  2903. //
  2904. // for _, item := range cancelInfo {
  2905. // if item.MaxUnit == medical.MaxUnit {
  2906. // item.Count = item.Count * medical.MinNumber
  2907. // }
  2908. // if item.MaxUnit == medical.MinUnit {
  2909. // item.Count = item.Count
  2910. // }
  2911. // }
  2912. //
  2913. // for _, item := range cancelInfo {
  2914. // cancel_count += item.Count
  2915. // }
  2916. //
  2917. // fmt.Println("out_count232233332232323223", out_count)
  2918. //
  2919. // if item.MaxUnit == medical.MaxUnit {
  2920. // now_count = item.Count * medical.MinNumber
  2921. // }
  2922. // if item.MaxUnit == medical.MinUnit {
  2923. // now_count = item.Count
  2924. // }
  2925. // fmt.Println("hhhhhh2323322323", now_count+cancel_count)
  2926. // //总退库批次
  2927. // all_count = now_count + cancel_count
  2928. //
  2929. // //该批次的总出库数小与退库数报错返回
  2930. // if all_count > out_count {
  2931. // c.ServeSuccessJSON(map[string]interface{}{
  2932. // "msg": "4",
  2933. // })
  2934. // return
  2935. // }
  2936. //
  2937. // if item.MaxUnit == medical.MaxUnit {
  2938. // //更新库存
  2939. // total = item.Count - info.Count
  2940. // parseDateErr := service.UpdatedDrugInfo(item.BatchNumberId, total)
  2941. // fmt.Println("parseDateErr", parseDateErr)
  2942. //
  2943. // flow := models.DrugFlow{
  2944. // Count: item.Count,
  2945. // }
  2946. // service.UpdatedDrugFlow(info.CancelStockId, item.DrugId, flow)
  2947. // }
  2948. //
  2949. // if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  2950. // item.Count = item.Count
  2951. // total = item.Count - info.Count
  2952. // service.UpdatedDrugInfoOne(item.BatchNumberId, total)
  2953. //
  2954. // flow := models.DrugFlow{
  2955. // Count: item.Count,
  2956. // }
  2957. // service.UpdatedDrugFlow(info.CancelStockId, item.DrugId, flow)
  2958. // }
  2959. //
  2960. // //查询默认仓库
  2961. // houseConfig, _ := service.GetAllStoreHouseConfig(item.OrgId)
  2962. // //查询默认仓库剩余多少库存
  2963. // list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  2964. // var sum_count int64
  2965. // var sum_in_count int64
  2966. // for _, it := range list {
  2967. // baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  2968. // if it.MaxUnit == baseDrug.MaxUnit {
  2969. // it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  2970. // it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  2971. // sum_count += it.StockMaxNumber + it.StockMinNumber
  2972. // sum_in_count += it.WarehousingCount
  2973. // }
  2974. // }
  2975. // service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  2976. //
  2977. // }
  2978. //}
  2979. //if errs != nil {
  2980. // utils.ErrorLog(errs.Error())
  2981. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateCancelStockFail)
  2982. // return
  2983. //}
  2984. c.ServeSuccessJSON(map[string]interface{}{
  2985. "msg": "1",
  2986. })
  2987. }
  2988. func (c *StockDrugApiController) GetAllConfig() {
  2989. adminUserInfo := c.GetAdminUserInfo()
  2990. manufacturer, _ := service.FindAllManufacturer(adminUserInfo.CurrentOrgId)
  2991. dealer, _ := service.FindAllDealer(adminUserInfo.CurrentOrgId)
  2992. stockIns, _ := service.FindStockInNumber(adminUserInfo.CurrentOrgId)
  2993. drugs, _ := service.GetDrugs(adminUserInfo.CurrentOrgId)
  2994. list, _ := service.GetAllStoreHouseListThree(adminUserInfo.CurrentOrgId)
  2995. configlist, _ := service.GetAllStoreHouseConfig(adminUserInfo.CurrentOrgId)
  2996. appId := c.GetAdminUserInfo().CurrentAppId
  2997. doctorList, _ := service.GetAllDoctorListSix(adminUserInfo.CurrentOrgId, appId)
  2998. c.ServeSuccessJSON(map[string]interface{}{
  2999. "manufacturer": manufacturer,
  3000. "dealer": dealer,
  3001. "drugs": drugs,
  3002. "numbers": stockIns,
  3003. "list": list,
  3004. "configlist": configlist,
  3005. "doctorList": doctorList,
  3006. })
  3007. }
  3008. func (c *StockDrugApiController) GetAllDrugStock() {
  3009. adminUserInfo := c.GetAdminUserInfo()
  3010. manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
  3011. dealer_id, _ := c.GetInt64("dealer_id", 0)
  3012. drugs, err := service.FindAllDrugByManufactureId(manufacturer_id, dealer_id, adminUserInfo.CurrentOrgId)
  3013. if err == nil {
  3014. c.ServeSuccessJSON(map[string]interface{}{
  3015. "drugs": drugs,
  3016. })
  3017. } else {
  3018. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  3019. }
  3020. }
  3021. func (c *StockDrugApiController) GetDrugQueryInfo() {
  3022. page, _ := c.GetInt64("page", -1)
  3023. limit, _ := c.GetInt64("limit", -1)
  3024. keyword := c.GetString("keyword")
  3025. drug_category, _ := c.GetInt64("drug_category")
  3026. start_time := c.GetString("start_time")
  3027. end_time := c.GetString("end_time")
  3028. timeLayout := "2006-01-02"
  3029. loc, _ := time.LoadLocation("Local")
  3030. var startTime int64
  3031. if len(start_time) > 0 {
  3032. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  3033. if err != nil {
  3034. fmt.Println(err)
  3035. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  3036. return
  3037. }
  3038. startTime = theTime.Unix()
  3039. }
  3040. var endTime int64
  3041. if len(end_time) > 0 {
  3042. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  3043. if err != nil {
  3044. utils.ErrorLog(err.Error())
  3045. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  3046. return
  3047. }
  3048. endTime = theTime.Unix()
  3049. }
  3050. adminUserInfo := c.GetAdminUserInfo()
  3051. list, total, err := service.FindAllDrugStockInfo(adminUserInfo.CurrentOrgId, page, limit, keyword, drug_category, startTime, endTime)
  3052. if err == nil {
  3053. c.ServeSuccessJSON(map[string]interface{}{
  3054. "list": list,
  3055. "total": total,
  3056. })
  3057. } else {
  3058. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  3059. }
  3060. }
  3061. func (this *StockDrugApiController) GetUserDetailInfo() {
  3062. order_id, _ := this.GetInt64("id")
  3063. if order_id <= 0 {
  3064. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  3065. return
  3066. }
  3067. adminUserInfo := this.GetAdminUserInfo()
  3068. userDetails, err, total := service.FindDrugStockUserDetailById(adminUserInfo.CurrentOrgId, order_id)
  3069. if err == nil {
  3070. this.ServeSuccessJSON(map[string]interface{}{
  3071. "list": userDetails,
  3072. "total": total,
  3073. })
  3074. } else {
  3075. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  3076. return
  3077. }
  3078. }
  3079. func (this *StockDrugApiController) GetCancelDrugOrderPrint() {
  3080. orderId := this.GetString("order_id")
  3081. ids := strings.Split(orderId, ",")
  3082. orgId := this.GetAdminUserInfo().CurrentOrgId
  3083. list, _ := service.GetCancelDrugOrderPrint(ids, orgId)
  3084. this.ServeSuccessJSON(map[string]interface{}{
  3085. "list": list,
  3086. })
  3087. }
  3088. func (this *StockDrugApiController) GetDrugCancelExportList() {
  3089. orderId := this.GetString("order_id")
  3090. ids := strings.Split(orderId, ",")
  3091. orgId := this.GetAdminUserInfo().CurrentOrgId
  3092. list, _ := service.GetDrugCancelExportList(ids, orgId)
  3093. this.ServeSuccessJSON(map[string]interface{}{
  3094. "list": list,
  3095. })
  3096. }
  3097. func (c *StockDrugApiController) GetDrugWarehouseInfoPrint() {
  3098. start_time := c.GetString("start_time")
  3099. end_time := c.GetString("end_time")
  3100. order_type, _ := c.GetInt64("order_type")
  3101. timeLayout := "2006-01-02"
  3102. loc, _ := time.LoadLocation("Local")
  3103. var startTime int64
  3104. if len(start_time) > 0 {
  3105. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  3106. if err != nil {
  3107. fmt.Println(err)
  3108. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  3109. return
  3110. }
  3111. startTime = theTime.Unix()
  3112. }
  3113. var endTime int64
  3114. if len(end_time) > 0 {
  3115. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  3116. if err != nil {
  3117. utils.ErrorLog(err.Error())
  3118. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  3119. return
  3120. }
  3121. endTime = theTime.Unix()
  3122. }
  3123. adminUserInfo := c.GetAdminUserInfo()
  3124. orgId := adminUserInfo.CurrentOrgId
  3125. if order_type == 1 {
  3126. list, err := service.GetDrugWarehouseInfoPrint(startTime, endTime, orgId)
  3127. if err == nil {
  3128. c.ServeSuccessJSON(map[string]interface{}{
  3129. "list": list,
  3130. })
  3131. return
  3132. }
  3133. }
  3134. }
  3135. func (c *StockDrugApiController) GetDrugOutOrderPrint() {
  3136. start_time := c.GetString("start_time")
  3137. end_time := c.GetString("end_time")
  3138. order_type, _ := c.GetInt64("order_type")
  3139. timeLayout := "2006-01-02"
  3140. loc, _ := time.LoadLocation("Local")
  3141. var startTime int64
  3142. if len(start_time) > 0 {
  3143. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
  3144. if err != nil {
  3145. fmt.Println(err)
  3146. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  3147. return
  3148. }
  3149. startTime = theTime.Unix()
  3150. }
  3151. var endTime int64
  3152. if len(end_time) > 0 {
  3153. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
  3154. if err != nil {
  3155. utils.ErrorLog(err.Error())
  3156. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  3157. return
  3158. }
  3159. endTime = theTime.Unix()
  3160. }
  3161. adminUserInfo := c.GetAdminUserInfo()
  3162. orgId := adminUserInfo.CurrentOrgId
  3163. if order_type == 2 {
  3164. list, err := service.GetDrugOutOrderInfoPrintList(startTime, endTime, orgId)
  3165. if err == nil {
  3166. c.ServeSuccessJSON(map[string]interface{}{
  3167. "list": list,
  3168. })
  3169. return
  3170. }
  3171. }
  3172. }
  3173. func (c *StockDrugApiController) GetConfigList() {
  3174. adminUserInfo := c.GetAdminUserInfo()
  3175. manufacturer, _ := service.FindAllManufacturer(adminUserInfo.CurrentOrgId)
  3176. dealer, _ := service.FindAllDealer(adminUserInfo.CurrentOrgId)
  3177. stockIns, _ := service.FindStockInNumber(adminUserInfo.CurrentOrgId)
  3178. list, _ := service.GetAllStoreHouseListThree(adminUserInfo.CurrentOrgId)
  3179. configlist, _ := service.GetAllStoreHouseConfig(adminUserInfo.CurrentOrgId)
  3180. appId := c.GetAdminUserInfo().CurrentAppId
  3181. doctorList, _ := service.GetAllDoctorListSix(adminUserInfo.CurrentOrgId, appId)
  3182. c.ServeSuccessJSON(map[string]interface{}{
  3183. "manufacturer": manufacturer,
  3184. "dealer": dealer,
  3185. "numbers": stockIns,
  3186. "list": list,
  3187. "configlist": configlist,
  3188. "doctorList": doctorList,
  3189. })
  3190. }
  3191. func (c *StockDrugApiController) CheckWarehousingInfo() {
  3192. warehousing_id, _ := c.GetInt64("warehousing_id")
  3193. orgId := c.GetAdminUserInfo().CurrentOrgId
  3194. houseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  3195. //更改核对状态
  3196. err := service.CheckWarehousingInfo(warehousing_id, orgId)
  3197. if err == nil {
  3198. list, _ := service.GetWarehousingInfoByList(warehousing_id, orgId)
  3199. for _, item := range list {
  3200. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  3201. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3202. //新增库存
  3203. service.AddDrugWarehouseStockMaxNumber(item.WarehousingCount, item.ID)
  3204. }
  3205. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3206. //新增库存
  3207. service.AddDrugWarehouseStockMaxNumber(item.WarehousingCount, item.ID)
  3208. }
  3209. if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3210. //新增库存
  3211. service.AddDrugWarehouseStockMinNumber(item.WarehousingCount, item.ID)
  3212. }
  3213. Creator := c.GetAdminUserInfo().AdminUser.Id
  3214. flow := &models.DrugFlow{
  3215. WarehousingId: 0,
  3216. DrugId: item.DrugId,
  3217. Number: item.Number,
  3218. BatchNumber: item.BatchNumber,
  3219. Count: item.WarehousingCount,
  3220. UserOrgId: item.OrgId,
  3221. PatientId: 0,
  3222. SystemTime: time.Now().Unix(),
  3223. ConsumableType: 1,
  3224. IsSys: 0,
  3225. WarehousingOrder: item.WarehousingOrder,
  3226. WarehouseOutId: 0,
  3227. WarehouseOutOrderNumber: "",
  3228. IsEdit: 0,
  3229. CancelStockId: 0,
  3230. CancelOrderNumber: "",
  3231. Manufacturer: 0,
  3232. Dealer: 0,
  3233. Creator: Creator,
  3234. UpdateCreator: Creator,
  3235. Status: 1,
  3236. Ctime: time.Now().Unix(),
  3237. Mtime: 0,
  3238. Price: item.Price,
  3239. WarehousingDetailId: item.ID,
  3240. WarehouseOutDetailId: 0,
  3241. CancelOutDetailId: 0,
  3242. ExpireDate: item.ExpiryDate,
  3243. ProductDate: item.ProductDate,
  3244. MaxUnit: item.MaxUnit,
  3245. MinUnit: item.MinUnit,
  3246. StorehouseId: item.StorehouseId,
  3247. }
  3248. service.CreateDrugFlowTwo(flow)
  3249. //查询默认仓库
  3250. //查询默认仓库剩余多少库存
  3251. list, _ := service.GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  3252. var sum_count int64
  3253. var sum_in_count int64
  3254. for _, it := range list {
  3255. baseDrug, _ := service.GetBaseDrugMedical(it.DrugId)
  3256. if it.MaxUnit == baseDrug.MaxUnit {
  3257. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  3258. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  3259. }
  3260. sum_count += it.StockMaxNumber + it.StockMinNumber
  3261. sum_in_count += it.WarehousingCount
  3262. }
  3263. service.UpdateMedicalSumCount(item.DrugId, sum_count, sum_in_count, item.OrgId)
  3264. }
  3265. c.ServeSuccessJSON(map[string]interface{}{
  3266. "list": list,
  3267. })
  3268. }
  3269. }
  3270. func (c *StockDrugApiController) ReturnCheckWarehouseingInfo() {
  3271. warehousing_id, _ := c.GetInt64("warehousing_id")
  3272. orgId := c.GetAdminUserInfo().CurrentOrgId
  3273. list, _ := service.GetWarehousingInfoByList(warehousing_id, orgId)
  3274. for _, item := range list {
  3275. //查询该药品是否有出库数据
  3276. outInfo, _ := service.GetDrugWarehouseOutByWarehouseInfoId(item.ID, item.DrugId, item.OrgId)
  3277. if len(outInfo) > 0 {
  3278. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCheckGoodParamWrong)
  3279. return
  3280. }
  3281. }
  3282. for _, item := range list {
  3283. //删除流水
  3284. service.UpdateWarehousingInfoFlow(item.ID, orgId)
  3285. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  3286. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3287. //新增库存
  3288. service.AddDrugReturnWarehouseStockMaxNumber(item.WarehousingCount, item.ID)
  3289. }
  3290. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3291. //新增库存
  3292. service.AddDrugReturnWarehouseStockMaxNumber(item.WarehousingCount, item.ID)
  3293. }
  3294. if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3295. //新增库存
  3296. service.AddDrugReturnWarehouseStockMinNumber(item.WarehousingCount, item.ID)
  3297. }
  3298. storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  3299. stockInfoList, _ := service.GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgId, item.DrugId)
  3300. var sum_count int64
  3301. var sum_in_count int64
  3302. for _, its := range stockInfoList {
  3303. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3304. sum_count += its.StockMaxNumber * medical.MinNumber
  3305. sum_in_count += its.WarehousingCount * medical.MinNumber
  3306. }
  3307. if its.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3308. sum_count += its.StockMaxNumber
  3309. sum_in_count += its.WarehousingCount
  3310. }
  3311. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3312. sum_count += its.StockMaxNumber
  3313. sum_in_count += its.WarehousingCount
  3314. }
  3315. }
  3316. service.UpdateBaseDrugSumInfo(sum_count, item.DrugId, item.OrgId, sum_in_count)
  3317. }
  3318. //更新审核
  3319. err := service.ReturnCheckWarehouseingInfo(warehousing_id, orgId)
  3320. if err == nil {
  3321. c.ServeSuccessJSON(map[string]interface{}{
  3322. "msg": "反审核成功!",
  3323. })
  3324. return
  3325. }
  3326. }
  3327. func (c *StockDrugApiController) CheckDrugOut() {
  3328. warehouse_out_id, _ := c.GetInt64("warehouse_out_id")
  3329. orgId := c.GetAdminUserInfo().CurrentOrgId
  3330. warehousingOutInfoList, _ := service.GetDrugWarehouseOutListById(warehouse_out_id, orgId)
  3331. drugWarehouseOut, _ := service.GetDrugWarehouseOutByLastId(warehouse_out_id)
  3332. storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  3333. for _, it := range warehousingOutInfoList {
  3334. var total_count int64
  3335. var out_count int64
  3336. //查询剩余库存
  3337. stockInfo, _ := service.GetDrugAllStockInfo(it.StorehouseId, it.OrgId, it.DrugId)
  3338. drup, _ := service.FindBaseDrugLibRecord(it.OrgId, it.DrugId)
  3339. for _, item := range stockInfo {
  3340. if item.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  3341. item.StockMaxNumber = item.StockMaxNumber * drup.MinNumber
  3342. }
  3343. total_count += item.StockMaxNumber + item.StockMinNumber
  3344. }
  3345. if it.CountUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  3346. it.Count = it.Count * drup.MinNumber
  3347. }
  3348. out_count += it.Count
  3349. fmt.Println("出库数量23232323232我", out_count)
  3350. fmt.Println("总库存", total_count)
  3351. if out_count > total_count {
  3352. c.ServeSuccessJSON(map[string]interface{}{
  3353. "warehousingOutInfoList": warehousingOutInfoList,
  3354. "drug_name": drup.DrugName,
  3355. "dose": drup.Dose,
  3356. "dose_unit": drup.DoseUnit,
  3357. "min_number": drup.MinNumber,
  3358. "min_unit": drup.MinUnit,
  3359. "max_unit": drup.MaxUnit,
  3360. "msg": 2,
  3361. })
  3362. return
  3363. }
  3364. }
  3365. for _, it := range warehousingOutInfoList {
  3366. drup, _ := service.FindBaseDrugLibRecord(it.OrgId, it.DrugId)
  3367. //出库逻辑
  3368. service.AutoDrugDeliverInfoFourtyOne(orgId, it.Count, &drugWarehouseOut, &drup, it)
  3369. medical, _ := service.GetBaseDrugMedical(it.DrugId)
  3370. //查询剩余库存
  3371. stockInfo, _ := service.GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, it.OrgId, it.DrugId)
  3372. var sum_count int64
  3373. for _, its := range stockInfo {
  3374. sum_count += its.StockMaxNumber*medical.MinNumber + its.StockMinNumber
  3375. }
  3376. service.UpdateBaseDrugSumTwo(it.DrugId, sum_count, it.OrgId)
  3377. }
  3378. out := models.DrugWarehouseOut{
  3379. IsCheck: 1,
  3380. }
  3381. service.UpdateCheckDrugOut(out, warehouse_out_id)
  3382. info := models.DrugWarehouseOutInfo{
  3383. IsCheck: 1,
  3384. }
  3385. service.UpdateCheckDrugOutInfo(info, warehouse_out_id)
  3386. c.ServeSuccessJSON(map[string]interface{}{
  3387. "warehousingOutInfoList": warehousingOutInfoList,
  3388. "drug_name": "",
  3389. "dose": "",
  3390. "dose_unit": "",
  3391. "min_number": "",
  3392. "min_unit": "",
  3393. "max_unit": "",
  3394. "msg": 1,
  3395. })
  3396. }
  3397. func (c *StockDrugApiController) ToReturnCheck() {
  3398. warehouse_out_id, _ := c.GetInt64("warehouse_out_id")
  3399. out := models.DrugWarehouseOut{
  3400. IsCheck: 2,
  3401. }
  3402. service.UpdateCheckDrugOut(out, warehouse_out_id)
  3403. outInfo := models.DrugWarehouseOutInfo{
  3404. IsCheck: 2,
  3405. }
  3406. service.UpdateCheckDrugOutInfoList(outInfo, warehouse_out_id)
  3407. orgId := c.GetAdminUserInfo().CurrentOrgId
  3408. list, _ := service.GetDrugWarehouseOutListById(warehouse_out_id, orgId)
  3409. storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  3410. //调用出库逻辑
  3411. for _, item := range list {
  3412. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  3413. if item.CountUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3414. service.UpdateDrugMaxNumber(item.Count, item.WarehouseInfoId)
  3415. service.DeleteDrugOutFlow(item.ID, item.OrgId)
  3416. }
  3417. if item.CountUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3418. service.UpdateDrugMaxNumber(item.Count, item.WarehouseInfoId)
  3419. service.DeleteDrugOutFlow(item.ID, item.OrgId)
  3420. }
  3421. if item.CountUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3422. service.UpdateDrugMinNumber(item.Count, item.WarehouseInfoId)
  3423. service.DeleteDrugOutFlow(item.ID, item.OrgId)
  3424. }
  3425. stockInfo, _ := service.GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  3426. var sum_count int64
  3427. for _, its := range stockInfo {
  3428. sum_count += its.StockMaxNumber*medical.MinNumber + its.StockMinNumber
  3429. }
  3430. service.UpdateBaseDrugSumTwo(item.DrugId, sum_count, item.OrgId)
  3431. }
  3432. c.ServeSuccessJSON(map[string]interface{}{
  3433. "out": out,
  3434. })
  3435. }
  3436. func (c *StockDrugApiController) CheckCancelDrugStock() {
  3437. cancelstock_id, _ := c.GetInt64("cancelstock_id")
  3438. orgId := c.GetAdminUserInfo().CurrentOrgId
  3439. cancelDrugStock, _ := service.GetCancelDrugStock(cancelstock_id, orgId)
  3440. list, _ := service.GetCancelDrugStockList(cancelstock_id, orgId)
  3441. cancelStock := models.DrugCancelStock{
  3442. IsCheck: 1,
  3443. }
  3444. var manufacturer_id int64
  3445. var dealer_id int64
  3446. manufacturerList, _ := service.GetAllManufacturerList(orgId)
  3447. dealerList, _ := service.GetAllDealerList(orgId)
  3448. creater := c.GetAdminUserInfo().AdminUser.Id
  3449. //查询退库单据
  3450. cancelList, _ := service.GetDrugCancelCheckByIdTwenty(cancelstock_id, orgId)
  3451. storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  3452. var cancel_total_count int64
  3453. var out_count int64
  3454. for _, its := range cancelList {
  3455. medical, _ := service.GetBaseDrugMedical(its.DrugId)
  3456. //查询该批次的出库数据
  3457. outList, _ := service.GetDrugWarehouseOutList(its.BatchNumberId, its.DrugId, its.OrgId)
  3458. for _, item := range outList {
  3459. if item.CountUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3460. out_count += item.Count * medical.MinNumber
  3461. }
  3462. if item.CountUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3463. out_count += item.Count
  3464. }
  3465. if item.CountUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3466. out_count += item.Count
  3467. }
  3468. }
  3469. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3470. cancel_total_count += its.Count * medical.MinNumber
  3471. }
  3472. if its.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3473. cancel_total_count += its.Count
  3474. }
  3475. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3476. cancel_total_count += its.Count
  3477. }
  3478. if cancel_total_count > out_count {
  3479. c.ServeSuccessJSON(map[string]interface{}{
  3480. "drug_name": medical.DrugName,
  3481. "dose": medical.Dose,
  3482. "dose_unit": medical.DoseUnit,
  3483. "min_number": medical.MinNumber,
  3484. "min_unit": medical.MinUnit,
  3485. "max_unit": medical.MaxUnit,
  3486. "msg": "2",
  3487. })
  3488. return
  3489. }
  3490. }
  3491. for _, its := range list {
  3492. for _, item := range manufacturerList {
  3493. if its.Manufacturer == item.ManufacturerName {
  3494. manufacturer_id = item.ID
  3495. }
  3496. }
  3497. for _, item := range dealerList {
  3498. if its.Dealer == item.DealerName {
  3499. dealer_id = item.ID
  3500. }
  3501. }
  3502. //查询该批次的退库数量
  3503. drugFlow := models.DrugFlow{
  3504. ID: 0,
  3505. WarehousingId: 0,
  3506. DrugId: its.DrugId,
  3507. Number: its.Number,
  3508. BatchNumber: "",
  3509. Count: its.Count,
  3510. UserOrgId: its.OrgId,
  3511. PatientId: 0,
  3512. SystemTime: cancelDrugStock.ReturnTime,
  3513. ConsumableType: 4,
  3514. IsSys: 0,
  3515. WarehousingOrder: "",
  3516. WarehouseOutId: 0,
  3517. WarehouseOutOrderNumber: "",
  3518. IsEdit: 0,
  3519. CancelStockId: 0,
  3520. CancelOrderNumber: its.OrderNumber,
  3521. Manufacturer: manufacturer_id,
  3522. Dealer: dealer_id,
  3523. Creator: creater,
  3524. UpdateCreator: 0,
  3525. Status: 1,
  3526. Ctime: time.Now().Unix(),
  3527. Mtime: 0,
  3528. Price: its.Price,
  3529. WarehousingDetailId: 0,
  3530. WarehouseOutDetailId: 0,
  3531. CancelOutDetailId: its.ID,
  3532. ExpireDate: its.ExpiryDate,
  3533. ProductDate: its.ProductDate,
  3534. MaxUnit: its.MaxUnit,
  3535. MinUnit: "",
  3536. StockMaxNumber: 0,
  3537. StockMinNumber: 0,
  3538. LastStockMaxNumber: 0,
  3539. LastStockMinNumber: 0,
  3540. AdviceId: 0,
  3541. SupplyWarehouseId: 0,
  3542. SupplyCancelOutId: 0,
  3543. SupplyWarehouseDetailInfo: 0,
  3544. StorehouseId: its.StorehouseId,
  3545. SecondWarehouseInfoId: 0,
  3546. AdminUserId: 0,
  3547. LastPrice: 0,
  3548. StockCount: "",
  3549. PharmacyId: 0,
  3550. }
  3551. service.CreateDrugFlowOne(drugFlow)
  3552. }
  3553. for _, its := range cancelList {
  3554. medical, _ := service.GetBaseDrugMedical(its.DrugId)
  3555. //扣减库存
  3556. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3557. service.ModifyDrugWarehouseInfoStockMaxNumber(its.Count, its.DrugId, its.OrgId, its.BatchNumberId)
  3558. }
  3559. if its.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3560. service.ModifyDrugWarehouseInfoStockMinNumber(its.Count, its.DrugId, its.OrgId, its.BatchNumberId)
  3561. }
  3562. if its.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3563. service.ModifyDrugWarehouseInfoStockMaxNumber(its.Count, its.DrugId, its.OrgId, its.BatchNumberId)
  3564. }
  3565. stockInfo, _ := service.GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, its.OrgId, its.DrugId)
  3566. var sum_count int64
  3567. for _, its := range stockInfo {
  3568. sum_count += its.StockMaxNumber*medical.MinNumber + its.StockMinNumber
  3569. }
  3570. service.UpdateBaseDrugSumTwo(its.DrugId, sum_count, its.OrgId)
  3571. }
  3572. service.ModifyCancelStock(cancelstock_id, cancelStock, orgId)
  3573. //新增库存
  3574. c.ServeSuccessJSON(map[string]interface{}{
  3575. "drug_name": "",
  3576. "dose": "",
  3577. "dose_unit": "",
  3578. "min_number": "",
  3579. "min_unit": "",
  3580. "max_unit": "",
  3581. "msg": "1",
  3582. })
  3583. return
  3584. }
  3585. func (c *StockDrugApiController) ReturnDrugCancelStock() {
  3586. cancelstock_id, _ := c.GetInt64("cancelstock_id")
  3587. orgId := c.GetAdminUserInfo().CurrentOrgId
  3588. drugStock := models.DrugCancelStock{
  3589. IsCheck: 2,
  3590. }
  3591. err := service.ReturnDrugCancelStock(cancelstock_id, drugStock)
  3592. list, err := service.GetDrugCancelStockInfo(cancelstock_id, orgId)
  3593. storeHouseConfig, _ := service.GetAllStoreHouseConfig(orgId)
  3594. for _, item := range list {
  3595. //删除流水
  3596. service.ModifyDrugFlowByCancelId(item.ID, item.DrugId, item.OrgId)
  3597. //扣减库存
  3598. medical, _ := service.GetBaseDrugMedical(item.DrugId)
  3599. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit != medical.MinUnit {
  3600. service.ModifyDrugMaxNumberWarehouseInfo(item.BatchNumberId, item.Count, item.OrgId)
  3601. }
  3602. if item.MaxUnit == medical.MinUnit && medical.MaxUnit != medical.MinUnit {
  3603. service.ModifyDrugMinNumberWarehouseInfo(item.BatchNumberId, item.Count, item.OrgId)
  3604. }
  3605. if item.MaxUnit == medical.MaxUnit && medical.MaxUnit == medical.MinUnit {
  3606. service.ModifyDrugMaxNumberWarehouseInfo(item.BatchNumberId, item.Count, item.OrgId)
  3607. }
  3608. stockInfo, _ := service.GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, item.OrgId, item.DrugId)
  3609. var sum_count int64
  3610. for _, its := range stockInfo {
  3611. sum_count += its.StockMaxNumber*medical.MinNumber + its.StockMinNumber
  3612. }
  3613. service.UpdateBaseDrugSumTwo(item.DrugId, sum_count, item.OrgId)
  3614. }
  3615. if err == nil {
  3616. c.ServeSuccessJSON(map[string]interface{}{
  3617. "msg": "反审核成功!",
  3618. })
  3619. return
  3620. }
  3621. }