drug_stock_api_contorller.go 143KB

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