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