drug_stock_api_contorller.go 143KB

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