drug_stock_api_contorller.go 143KB

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