drug_stock_api_contorller.go 142KB

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