drug_stock_api_contorller.go 143KB

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