drug_stock_api_contorller.go 144KB

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