drug_stock_api_contorller.go 143KB

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