drug_stock_api_contorller.go 134KB

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