drug_stock_api_contorller.go 125KB

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