drug_stock_api_contorller.go 138KB

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