new_warehouse_service.go 175KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639
  1. package service
  2. import (
  3. "XT_New/models"
  4. "XT_New/utils"
  5. "errors"
  6. "fmt"
  7. "github.com/jinzhu/gorm"
  8. "math"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // 耗材出库
  14. func ConsumablesGoodDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  15. //开事务
  16. var deliver_number int64 = 0
  17. var stock_number int64 = 0
  18. var maxNumber int64 = 0
  19. deliver_number = goods.Count
  20. // 根据先进先出原则,查询最先入库的批次,进行出库
  21. // 如果没有对应的库存,则报错
  22. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  23. if goods.Count > 0 {
  24. if err != nil {
  25. return errors.New("库存数量不足")
  26. }
  27. }
  28. stock_number = warehouse.StockCount
  29. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  30. if stock_number >= deliver_number {
  31. maxNumber = goods.Count
  32. //出库
  33. warehouse.StockCount = warehouse.StockCount - maxNumber
  34. warehouse.Mtime = time.Now().Unix()
  35. if warehouse.StockCount < 0 {
  36. return errors.New("库存数量不足")
  37. }
  38. errThree := UpDateWarehouseInfoByStock(&warehouse)
  39. if errThree != nil {
  40. return errThree
  41. }
  42. //if orgID == 10265 {
  43. // UpdateGoodInfoByPrice(warehouse.GoodId, orgID, warehouse.Price, warehouse.PackingPrice)
  44. //}
  45. //更新his_prescripton_project出库状态值
  46. UpdateHisPrescriptionProjectStatus(goods.GoodId, record_time, orgID, patient_id)
  47. //查询剩余库存
  48. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  49. var sum_count int64
  50. for _, item := range goodList {
  51. sum_count += item.StockCount
  52. }
  53. warehouseOutInfo := &models.WarehouseOutInfo{
  54. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  55. WarehouseOutId: warehouseOut.ID,
  56. WarehouseInfotId: warehouse.ID,
  57. Status: 1,
  58. Ctime: time.Now().Unix(),
  59. Remark: warehouse.Remark,
  60. OrgId: orgID,
  61. Type: 1,
  62. Manufacturer: warehouse.Manufacturer,
  63. Dealer: warehouse.Dealer,
  64. IsSys: 1,
  65. SysRecordTime: record_time,
  66. GoodTypeId: goods.GoodTypeId,
  67. GoodId: goods.GoodId,
  68. PatientId: patient_id,
  69. Number: warehouse.Number,
  70. LicenseNumber: warehouse.LicenseNumber,
  71. Price: warehouse.PackingPrice,
  72. ExpiryDate: warehouse.ExpiryDate,
  73. ProductDate: warehouse.ProductDate,
  74. ProjectId: goods.ProjectId,
  75. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  76. StorehouseId: goods.StorehouseId,
  77. IsCheck: 1,
  78. OverCount: sum_count,
  79. RegisterNumber: warehouse.RegisterNumber,
  80. BuyPrice: warehouse.PackingPrice,
  81. }
  82. warehouseOutInfo.Count = count
  83. if orgID == 9671 || orgID == 10265 {
  84. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  85. fmt.Println("加个我我我哦我我我", goodsInfo.PackingPrice)
  86. warehouseOutInfo.Price = goodsInfo.PackingPrice
  87. }
  88. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  89. if errcodes == gorm.ErrRecordNotFound {
  90. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  91. if errOne != nil {
  92. return errOne
  93. }
  94. } else if errcodes == nil {
  95. outInfoOne, _ := GetWarehouseOutInfoIsExistTwo(goods.GoodId, patient_id, record_time, goods.ProjectId)
  96. if count != outInfoOne.Count {
  97. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  98. }
  99. }
  100. lastOut, _ := FindWarehouseOutInfoByPatientId(patient_id, record_time, goods.GoodId, orgID)
  101. //查询已经出库的数据
  102. flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  103. var out_count int64
  104. var out_count_one int64
  105. for _, item := range flowGood {
  106. out_count += item.Count
  107. }
  108. //查询退库数据
  109. flowGoodTwo, _ := GetStockFlowIsBatchNumberThree(patient_id, record_time, goods.GoodId)
  110. for _, item := range flowGoodTwo {
  111. out_count_one += item.Count
  112. }
  113. //如果本次出库数据大于历史出库数据 新增1条流水
  114. if count > (out_count - out_count_one) {
  115. flow := models.VmStockFlow{
  116. WarehouseOutId: warehouseOut.ID,
  117. WarehousingId: warehouse.ID,
  118. GoodId: goods.GoodId,
  119. Number: warehouse.Number,
  120. ProductDate: warehouse.ProductDate,
  121. ExpireDate: warehouse.ExpiryDate,
  122. Count: count - out_count + out_count_one,
  123. Price: warehouse.PackingPrice,
  124. Status: 1,
  125. Ctime: time.Now().Unix(),
  126. UserOrgId: orgID,
  127. Manufacturer: warehouse.Manufacturer,
  128. Dealer: warehouse.Dealer,
  129. LicenseNumber: warehouse.LicenseNumber,
  130. IsEdit: 2,
  131. Creator: warehouseOut.Creater,
  132. SystemTime: record_time,
  133. ConsumableType: 3,
  134. WarehouseOutDetailId: lastOut.ID,
  135. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  136. IsSys: 1,
  137. PatientId: patient_id,
  138. ProjectId: goods.ProjectId,
  139. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  140. StorehouseId: goods.StorehouseId,
  141. OverCount: sum_count,
  142. BuyPrice: warehouse.Price,
  143. RegisterNumber: warehouse.RegisterNumber,
  144. }
  145. if orgID == 9671 || orgID == 10265 {
  146. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  147. flow.Price = goodsInfo.PackingPrice
  148. flow.BuyPrice = goodsInfo.BuyPrice
  149. }
  150. errThre := CreateStockFlowOne(flow)
  151. if errThre != nil {
  152. return errThre
  153. }
  154. }
  155. //如果本次出库数据小于历史出库数据 新增1条退库流水
  156. if count < (out_count - out_count_one) {
  157. operation_time := time.Now().Unix()
  158. //创建退库单
  159. timeStr := time.Now().Format("2006-01-02")
  160. timeArr := strings.Split(timeStr, "-")
  161. total, _ := FindAllCancelStockTotal(orgID)
  162. total = total + 1
  163. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  164. cancelStock := models.CancelStock{
  165. OrderNumber: orderNumber,
  166. OperaTime: operation_time,
  167. OrgId: orgID,
  168. Creater: warehouseOut.Creater,
  169. Ctime: time.Now().Unix(),
  170. Status: 1,
  171. ReturnTime: record_time,
  172. Type: 1,
  173. StorehouseId: goods.StorehouseId,
  174. IsCheck: 1,
  175. }
  176. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  177. if msgerrkonde == gorm.ErrRecordNotFound {
  178. AddSigleCancelStock(&cancelStock)
  179. }
  180. cancel, _ := GetLastCancelStockById(orgID)
  181. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  182. deaerler, _ := GetDealerById(warehouse.Dealer)
  183. cancelStockInfo := models.CancelStockInfo{
  184. GoodId: goods.GoodId,
  185. CancelStockId: cancel.ID,
  186. GoodTypeId: goods.GoodTypeId,
  187. Count: out_count - out_count_one - count,
  188. Price: warehouse.PackingPrice,
  189. Total: 0,
  190. ProductDate: warehouse.ProductDate,
  191. ExpiryDate: warehouse.ExpiryDate,
  192. Ctime: time.Now().Unix(),
  193. Status: 1,
  194. OrgId: orgID,
  195. OrderNumber: cancel.OrderNumber,
  196. Type: 0,
  197. Dealer: deaerler.DealerName,
  198. Manufacturer: manufacturer.ManufacturerName,
  199. Number: warehouse.Number,
  200. RegisterAccount: "",
  201. Remark: "",
  202. WarehouseInfoId: warehouse.ID,
  203. PatientId: patient_id,
  204. RecordDate: record_time,
  205. StorehouseId: goods.StorehouseId,
  206. IsCheck: 1,
  207. }
  208. if orgID == 9671 || orgID == 10265 {
  209. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  210. cancelStockInfo.Price = goodsInfo.PackingPrice
  211. }
  212. CreateCancelStockInfoOne(&cancelStockInfo)
  213. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  214. flow := models.VmStockFlow{
  215. WarehousingId: warehouse.ID,
  216. GoodId: goods.GoodId,
  217. Number: warehouse.Number,
  218. LicenseNumber: warehouse.LicenseNumber,
  219. Count: out_count - out_count_one - count,
  220. UserOrgId: orgID,
  221. PatientId: patient_id,
  222. SystemTime: record_time,
  223. ConsumableType: 7,
  224. IsSys: 1,
  225. WarehousingOrder: "",
  226. WarehouseOutId: lastOut.WarehouseOutId,
  227. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  228. IsEdit: 0,
  229. CancelStockId: cancel.ID,
  230. CancelOrderNumber: cancel.OrderNumber,
  231. Manufacturer: manufacturer.ID,
  232. Dealer: 0,
  233. Creator: warehouseOut.Creater,
  234. UpdateCreator: 0,
  235. Status: 1,
  236. Ctime: time.Now().Unix(),
  237. Mtime: 0,
  238. Price: warehouse.PackingPrice,
  239. WarehousingDetailId: warehouse.ID,
  240. WarehouseOutDetailId: lastOut.ID,
  241. CancelOutDetailId: cancelInfo.ID,
  242. ProductDate: warehouse.ProductDate,
  243. ExpireDate: warehouse.ExpiryDate,
  244. StorehouseId: goods.StorehouseId,
  245. BuyPrice: warehouse.Price,
  246. ProjectId: goods.ProjectId,
  247. OverCount: sum_count,
  248. RegisterNumber: warehouse.RegisterNumber,
  249. }
  250. if orgID == 9671 || orgID == 10265 {
  251. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  252. flow.Price = goodsInfo.PackingPrice
  253. flow.BuyPrice = goodsInfo.BuyPrice
  254. }
  255. CreateStockFlowOne(flow)
  256. }
  257. details := models.BloodAutomaticReduceDetail{
  258. WarehouseOutId: lastOut.ID,
  259. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  260. PatientId: patient_id,
  261. Ctime: time.Now().Unix(),
  262. Mtime: time.Now().Unix(),
  263. Status: 1,
  264. RecordTime: record_time,
  265. OrgId: orgID,
  266. GoodId: goods.GoodId,
  267. GoodTypeId: goods.GoodTypeId,
  268. Count: count,
  269. ProjectId: goods.ProjectId,
  270. StorehouseId: goods.StorehouseId,
  271. }
  272. ////查询该耗材已经出库的数量
  273. _, errcode := GetAutoMaticReduceDetailTwenty(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  274. if errcode == gorm.ErrRecordNotFound {
  275. errTwo := CreateAutoReduceRecord(&details)
  276. if errTwo != nil {
  277. return errTwo
  278. }
  279. } else if errcode == nil {
  280. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  281. CreateAutoReduceRecord(&details)
  282. }
  283. return nil
  284. } else {
  285. // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  286. warehouse.StockCount = 0
  287. warehouse.Mtime = time.Now().Unix()
  288. errThree := UpDateWarehouseInfoByStock(&warehouse)
  289. if errThree != nil {
  290. return errThree
  291. }
  292. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  293. var sum_count int64
  294. for _, item := range goodList {
  295. sum_count += item.StockCount
  296. }
  297. warehouseOutInfo := &models.WarehouseOutInfo{
  298. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  299. WarehouseOutId: warehouseOut.ID,
  300. WarehouseInfotId: warehouse.ID,
  301. Status: 1,
  302. Ctime: time.Now().Unix(),
  303. Remark: warehouse.Remark,
  304. OrgId: orgID,
  305. Type: 1,
  306. Manufacturer: warehouse.Manufacturer,
  307. Dealer: warehouse.Dealer,
  308. IsSys: 1,
  309. SysRecordTime: record_time,
  310. GoodTypeId: goods.GoodTypeId,
  311. GoodId: goods.GoodId,
  312. PatientId: patient_id,
  313. Number: warehouse.Number,
  314. LicenseNumber: warehouse.LicenseNumber,
  315. Price: warehouse.PackingPrice,
  316. ExpiryDate: warehouse.ExpiryDate,
  317. ProductDate: warehouse.ProductDate,
  318. ProjectId: goods.ProjectId,
  319. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  320. StorehouseId: goods.StorehouseId,
  321. IsCheck: 1,
  322. OverCount: sum_count,
  323. RegisterNumber: warehouse.RegisterNumber,
  324. }
  325. warehouseOutInfo.Count = stock_number
  326. if orgID == 9671 || orgID == 10265 {
  327. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  328. warehouseOutInfo.Price = goodsInfo.PackingPrice
  329. }
  330. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  331. if errcodes == gorm.ErrRecordNotFound {
  332. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  333. if errOne != nil {
  334. return errOne
  335. }
  336. } else if errcodes == nil {
  337. goods.Count = deliver_number - stock_number
  338. //更新数量为 该批次剩余数量 + 还有未出的数量
  339. warehouseOutInfo.Count = stock_number
  340. outInfoOne, _ := GetWarehouseOutInfoIsExistTwo(goods.GoodId, patient_id, record_time, goods.ProjectId)
  341. if count != outInfoOne.Count {
  342. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  343. }
  344. }
  345. lastOut, _ := FindWarehouseOutInfoByPatientId(patient_id, record_time, goods.GoodId, orgID)
  346. //查询该该批次已经出库的数据
  347. flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  348. var out_count int64
  349. var out_count_one int64
  350. for _, item := range flowGood {
  351. out_count += item.Count
  352. }
  353. flowGoodTwo, _ := GetStockFlowIsBatchNumberThree(patient_id, record_time, goods.GoodId)
  354. for _, item := range flowGoodTwo {
  355. out_count_one += item.Count
  356. }
  357. //如果出库数量 大于 历史出库数据 新增1条流水
  358. if count > out_count-out_count_one {
  359. flow := models.VmStockFlow{
  360. WarehouseOutId: warehouseOut.ID,
  361. WarehousingId: warehouse.ID,
  362. GoodId: goods.GoodId,
  363. Number: warehouse.Number,
  364. ProductDate: warehouse.ProductDate,
  365. ExpireDate: warehouse.ExpiryDate,
  366. Count: stock_number,
  367. Price: warehouse.PackingPrice,
  368. Status: 1,
  369. Ctime: time.Now().Unix(),
  370. UserOrgId: orgID,
  371. Manufacturer: warehouse.Manufacturer,
  372. Dealer: warehouse.Dealer,
  373. LicenseNumber: warehouse.LicenseNumber,
  374. IsEdit: 2,
  375. Creator: warehouseOut.Creater,
  376. SystemTime: record_time,
  377. ConsumableType: 3,
  378. WarehouseOutDetailId: lastOut.ID,
  379. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  380. IsSys: 1,
  381. PatientId: patient_id,
  382. ProjectId: goods.ProjectId,
  383. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  384. StorehouseId: goods.StorehouseId,
  385. OverCount: sum_count,
  386. BuyPrice: warehouse.Price,
  387. RegisterNumber: warehouse.RegisterNumber,
  388. }
  389. if orgID == 9671 || orgID == 10265 {
  390. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  391. flow.Price = goodsInfo.PackingPrice
  392. flow.BuyPrice = goodsInfo.BuyPrice
  393. }
  394. errThre := CreateStockFlowOne(flow)
  395. if errThre != nil {
  396. return errThre
  397. }
  398. }
  399. //退库
  400. if count < out_count-out_count_one {
  401. operation_time := time.Now().Unix()
  402. //创建退库单
  403. timeStr := time.Now().Format("2006-01-02")
  404. timeArr := strings.Split(timeStr, "-")
  405. total, _ := FindAllCancelStockTotal(orgID)
  406. total = total + 1
  407. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  408. cancelStock := models.CancelStock{
  409. OrderNumber: orderNumber,
  410. OperaTime: operation_time,
  411. OrgId: orgID,
  412. Creater: warehouseOut.Creater,
  413. Ctime: time.Now().Unix(),
  414. Status: 1,
  415. ReturnTime: record_time,
  416. Type: 1,
  417. StorehouseId: goods.StorehouseId,
  418. IsCheck: 1,
  419. }
  420. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  421. if msgerrkonde == gorm.ErrRecordNotFound {
  422. AddSigleCancelStock(&cancelStock)
  423. }
  424. cancel, _ := GetLastCancelStockById(orgID)
  425. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  426. deaerler, _ := GetDealerById(warehouse.Dealer)
  427. cancelStockInfo := models.CancelStockInfo{
  428. GoodId: goods.GoodId,
  429. CancelStockId: cancel.ID,
  430. GoodTypeId: goods.GoodTypeId,
  431. Count: out_count - out_count_one - count,
  432. Price: warehouse.PackingPrice,
  433. Total: 0,
  434. ProductDate: warehouse.ProductDate,
  435. ExpiryDate: warehouse.ExpiryDate,
  436. Ctime: time.Now().Unix(),
  437. Status: 1,
  438. OrgId: orgID,
  439. OrderNumber: cancel.OrderNumber,
  440. Type: 0,
  441. Dealer: deaerler.DealerName,
  442. Manufacturer: manufacturer.ManufacturerName,
  443. Number: warehouse.Number,
  444. RegisterAccount: "",
  445. Remark: "",
  446. WarehouseInfoId: warehouse.ID,
  447. PatientId: patient_id,
  448. RecordDate: record_time,
  449. StorehouseId: goods.StorehouseId,
  450. IsCheck: 1,
  451. }
  452. if orgID == 9671 || orgID == 10265 {
  453. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  454. cancelStockInfo.Price = goodsInfo.PackingPrice
  455. }
  456. CreateCancelStockInfoOne(&cancelStockInfo)
  457. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  458. flow := models.VmStockFlow{
  459. WarehousingId: warehouse.ID,
  460. GoodId: goods.GoodId,
  461. Number: warehouse.Number,
  462. LicenseNumber: warehouse.LicenseNumber,
  463. Count: out_count - count,
  464. UserOrgId: orgID,
  465. PatientId: patient_id,
  466. SystemTime: record_time,
  467. ConsumableType: 7,
  468. IsSys: 1,
  469. WarehousingOrder: "",
  470. WarehouseOutId: lastOut.WarehouseOutId,
  471. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  472. IsEdit: 0,
  473. CancelStockId: cancel.ID,
  474. CancelOrderNumber: cancel.OrderNumber,
  475. Manufacturer: manufacturer.ID,
  476. Dealer: 0,
  477. Creator: warehouseOut.Creater,
  478. UpdateCreator: 0,
  479. Status: 1,
  480. Ctime: time.Now().Unix(),
  481. Mtime: 0,
  482. Price: warehouse.PackingPrice,
  483. WarehousingDetailId: warehouse.ID,
  484. WarehouseOutDetailId: lastOut.ID,
  485. CancelOutDetailId: cancelInfo.ID,
  486. ProductDate: warehouse.ProductDate,
  487. ExpireDate: warehouse.ExpiryDate,
  488. StorehouseId: goods.StorehouseId,
  489. BuyPrice: warehouse.Price,
  490. ProjectId: goods.ProjectId,
  491. OverCount: sum_count,
  492. RegisterNumber: warehouse.RegisterNumber,
  493. }
  494. if orgID == 9671 || orgID == 10265 {
  495. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  496. flow.Price = goodsInfo.PackingPrice
  497. flow.BuyPrice = goodsInfo.BuyPrice
  498. }
  499. CreateStockFlowOne(flow)
  500. }
  501. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  502. goods.Count = deliver_number - stock_number
  503. ConsumablesGoodDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  504. }
  505. return nil
  506. }
  507. func FindWarehouseOutInfoByPatientId(patient_id int64, record_time int64, good_id int64, org_id int64) (models.WarehouseOutInfo, error) {
  508. outInfo := models.WarehouseOutInfo{}
  509. err := XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and org_id = ? and status = 1", patient_id, record_time, good_id, org_id).Last(&outInfo).Error
  510. return outInfo, err
  511. }
  512. func FindWarehouseOutInfoByPatientIdTwo(patient_id int64, record_time int64, good_id int64, org_id int64, project_id int64) (models.WarehouseOutInfo, error) {
  513. outInfo := models.WarehouseOutInfo{}
  514. err := XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and org_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, org_id, project_id).Last(&outInfo).Error
  515. return outInfo, err
  516. }
  517. func UpdateDrugStockCount(drug_id int64, org_id int64, storehouse_id int64, flush_count int64) error {
  518. drug := models.XtDrugStockCount{}
  519. ut := XTWriteDB().Begin()
  520. err := ut.Model(&drug).Where("drug_id = ? and user_org_id = ? and storehouse_id = ? and status = 1", drug_id, org_id, storehouse_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  521. if err != nil {
  522. ut.Rollback()
  523. return err
  524. }
  525. ut.Commit()
  526. return err
  527. }
  528. func AddDrugCount(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
  529. ut := XTWriteDB().Begin()
  530. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", out_count)).Error
  531. if err != nil {
  532. ut.Rollback()
  533. return err
  534. }
  535. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", out_count)).Error
  536. if err != nil {
  537. ut.Rollback()
  538. return err
  539. }
  540. ut.Commit()
  541. return err
  542. }
  543. func FindOverCount(drug_id int64, org_id int64, storehouse_id int64) (models.XtDrugStockCount, error) {
  544. stockCount := models.XtDrugStockCount{}
  545. err := XTReadDB().Where("drug_id = ? and user_org_id = ? and storehouse_id=?", drug_id, org_id, storehouse_id).Find(&stockCount).Error
  546. return stockCount, err
  547. }
  548. func UpdateActOut(id int64, sum_in_count int64, flush_count int64, sum_cancel_count int64) error {
  549. var sum_out_count int64
  550. sum_out_count = sum_in_count - flush_count
  551. var sum_act_out_count int64
  552. sum_act_out_count = sum_out_count + sum_cancel_count
  553. err := XTWriteDB().Model(&models.XtDrugStockCount{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"sum_out_count": sum_out_count, "sum_act_out_count": sum_act_out_count}).Error
  554. return err
  555. }
  556. func ReduceDrugCount(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
  557. ut := XTWriteDB().Begin()
  558. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", out_count)).Error
  559. if err != nil {
  560. ut.Rollback()
  561. return err
  562. }
  563. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count - ?", out_count)).Error
  564. if err != nil {
  565. ut.Rollback()
  566. return err
  567. }
  568. ut.Commit()
  569. return err
  570. }
  571. func ReduceDrugCountTwo(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
  572. ut := XTWriteDB().Begin()
  573. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id = ?", storehouse_id, org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", out_count)).Error
  574. if err != nil {
  575. ut.Rollback()
  576. return err
  577. }
  578. ut.Commit()
  579. return err
  580. }
  581. // 药品自动出库 递归方式
  582. func BloodDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  583. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  584. var deliver_number int64 = 0
  585. var stock_number int64 = 0
  586. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  587. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  588. var drug_price float64
  589. if advice.PrescribingNumberUnit == drup.MaxUnit {
  590. deliver_number = count * drup.MinNumber
  591. } else {
  592. deliver_number = count
  593. }
  594. if advice.PrescribingNumberUnit == drup.MaxUnit {
  595. drug_price = drup.RetailPrice
  596. }
  597. fmt.Println(drug_price)
  598. if advice.PrescribingNumberUnit == drup.MinUnit {
  599. drug_price = drup.MinPrice
  600. }
  601. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  602. drug_price = drup.RetailPrice
  603. }
  604. // 根据先进先出原则,查询最先入库的批次,进行出库
  605. // 如果没有对应的库存,则报错
  606. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  607. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  608. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  609. var stockMax int64
  610. var stockMin int64
  611. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  612. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  613. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  614. UpdateMinNumber(lastWarehouse.ID, stockMin)
  615. }
  616. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  617. if err != nil {
  618. return err
  619. }
  620. // 将该批次的剩余库存数量转换为拆零数量
  621. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  622. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  623. if stock_number >= deliver_number {
  624. var maxNumber int64 = 0
  625. var minNumber int64 = 0
  626. maxNumber = deliver_number / drup.MinNumber
  627. minNumber = deliver_number % drup.MinNumber
  628. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  629. minNumber = maxNumber
  630. }
  631. if drup.MaxUnit != drup.MinUnit {
  632. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  633. return errors.New("库存数量不足")
  634. }
  635. }
  636. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  637. if warehouse.StockMaxNumber < 0 {
  638. warehouse.StockMaxNumber = 0
  639. }
  640. if warehouse.StockMinNumber < 0 {
  641. warehouse.StockMinNumber = 0
  642. }
  643. warehouse.Mtime = time.Now().Unix()
  644. if warehouse.StockMinNumber < minNumber {
  645. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  646. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  647. } else {
  648. if minNumber > 0 {
  649. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  650. }
  651. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  652. if warehouse.StockMinNumber > 0 {
  653. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  654. }
  655. }
  656. }
  657. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  658. if (warehouse.StockMinNumber - deliver_number) > 0 {
  659. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  660. }
  661. }
  662. if drup.MaxUnit != drup.MinUnit {
  663. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  664. return errors.New("库存数量不足")
  665. }
  666. }
  667. if warehouse.StockMinNumber <= 0 {
  668. warehouse.StockMinNumber = 0
  669. }
  670. //扣减库存数据
  671. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  672. if errThree != nil {
  673. return errThree
  674. }
  675. //查询剩余库存
  676. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  677. var sum_count int64
  678. for _, its := range stockInfo {
  679. if its.MaxUnit == drup.MaxUnit {
  680. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  681. }
  682. sum_count += its.StockMaxNumber + its.StockMinNumber
  683. }
  684. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  685. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  686. WarehouseOutId: warehouseout.ID,
  687. Status: 1,
  688. Ctime: time.Now().Unix(),
  689. Remark: warehouse.Remark,
  690. OrgId: orgID,
  691. Type: 1,
  692. Manufacturer: warehouse.Manufacturer,
  693. Dealer: warehouse.Dealer,
  694. IsSys: 1,
  695. SysRecordTime: advice.RecordDate,
  696. DrugId: advice.DrugId,
  697. Number: warehouse.Number,
  698. BatchNumber: warehouse.BatchNumber,
  699. Price: warehouse.RetailPrice,
  700. CountUnit: drup.MinUnit,
  701. RetailPrice: warehouse.RetailPrice,
  702. ProductDate: warehouse.ProductDate,
  703. ExpiryDate: warehouse.ExpiryDate,
  704. PatientId: advice.PatientId,
  705. Count: deliver_number, //出最小单位
  706. WarehouseInfoId: warehouse.ID,
  707. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  708. StorehouseId: storeConfig.DrugStorehouseOut,
  709. IsCheck: 1,
  710. OverCount: sum_count,
  711. AdviceId: advice.ID,
  712. }
  713. //查询今日该药品该患者是否有出库数据
  714. lastDrugOutInfo, errThreeCode := GetNewDrugWarehouseOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId)
  715. if errThreeCode == gorm.ErrRecordNotFound {
  716. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  717. if errOne != nil {
  718. return errOne
  719. }
  720. }
  721. if errThreeCode == nil {
  722. errThree := UpdateSigleDrugWarehouseOutInfo(lastDrugOutInfo.ID, warehouseOutInfo)
  723. if errThree != nil {
  724. return errThree
  725. }
  726. }
  727. var out_count int64
  728. var cancel_count int64
  729. //查询出库数据
  730. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  731. //查询退库数据
  732. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  733. for _, item := range infoCountList {
  734. out_count += item.Count
  735. }
  736. for _, item := range cancelInfoList {
  737. cancel_count += item.Count
  738. }
  739. //如果本次出库数据大于历史出库数据 新增1条流水
  740. if deliver_number > (out_count - cancel_count) {
  741. drugflow := models.DrugFlow{
  742. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  743. WarehouseOutId: warehouseout.ID,
  744. DrugId: advice.DrugId,
  745. Number: warehouse.Number,
  746. ProductDate: warehouse.ProductDate,
  747. ExpireDate: warehouse.ExpiryDate,
  748. Count: deliver_number - (out_count - cancel_count), //按最小单位计算,
  749. Price: warehouse.RetailPrice,
  750. Status: 1,
  751. Ctime: time.Now().Unix(),
  752. UserOrgId: orgID,
  753. Manufacturer: warehouse.Manufacturer,
  754. Dealer: warehouse.Dealer,
  755. BatchNumber: warehouse.BatchNumber,
  756. MaxUnit: drup.MinUnit,
  757. ConsumableType: 3,
  758. IsEdit: 1,
  759. Creator: advice.ExecutionStaff,
  760. IsSys: 1,
  761. PatientId: advice.PatientId,
  762. SystemTime: advice.AdviceDate,
  763. WarehousingDetailId: warehouse.ID,
  764. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  765. StorehouseId: storeConfig.DrugStorehouseOut,
  766. WarehouseOutDetailId: lastDrugOutInfo.ID,
  767. OverCount: sum_count,
  768. AdviceId: advice.ID,
  769. LastPrice: warehouse.Price,
  770. }
  771. CreateDrugFlowOne(drugflow)
  772. //出库数量相加
  773. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  774. }
  775. //如果本次出库数据小于历史出库数据 新增1条退库流水
  776. if deliver_number < (out_count - cancel_count) {
  777. operation_time := time.Now().Unix()
  778. ctime := time.Now().Unix()
  779. timeStr := time.Now().Format("2006-01-02")
  780. timeArr := strings.Split(timeStr, "-")
  781. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  782. total = total + 1
  783. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  784. cancelStock := models.DrugCancelStock{
  785. OrderNumber: orderNumber,
  786. OperaTime: operation_time,
  787. OrgId: advice.UserOrgId,
  788. Creater: advice.ExecutionStaff,
  789. Ctime: ctime,
  790. Status: 1,
  791. ReturnTime: advice.AdviceDate,
  792. Dealer: 0,
  793. Manufacturer: 0,
  794. Type: 1,
  795. StorehouseId: storeConfig.DrugStorehouseOut,
  796. IsCheck: 1,
  797. }
  798. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  799. if errCode == gorm.ErrRecordNotFound {
  800. AddSigleDrugCancelStock(&cancelStock)
  801. }
  802. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  803. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  804. dealer, _ := GetDealerById(warehouse.Dealer)
  805. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  806. cancelStockInfo := models.DrugCancelStockInfo{
  807. OrderNumber: cancelInfo.OrderNumber,
  808. CancelStockId: lastDrugCancelStock.ID,
  809. DrugId: advice.DrugId,
  810. Count: (out_count - cancel_count) - deliver_number,
  811. Status: 1,
  812. Ctime: ctime,
  813. OrgId: advice.UserOrgId,
  814. Type: 1,
  815. Manufacturer: manufactureName.ManufacturerName,
  816. Dealer: dealer.DealerName,
  817. Total: 0,
  818. RetailPrice: warehouse.RetailPrice,
  819. RetailTotalPrice: warehouse.RetailTotalPrice,
  820. Price: warehouse.Price,
  821. RegisterAccount: "",
  822. Remark: warehouse.Remark,
  823. BatchNumber: warehouse.BatchNumber,
  824. MaxUnit: drup.MinUnit,
  825. ProductDate: warehouse.ProductDate,
  826. ExpiryDate: warehouse.ExpiryDate,
  827. BatchNumberId: warehouse.ID,
  828. StorehouseId: storeConfig.DrugStorehouseOut,
  829. IsCheck: 1,
  830. }
  831. CreatedCancelStock(cancelStockInfo)
  832. flow := models.DrugFlow{
  833. WarehousingId: warehouse.ID,
  834. DrugId: warehouse.DrugId,
  835. Number: "",
  836. BatchNumber: warehouse.BatchNumber,
  837. Count: (out_count - cancel_count) - deliver_number,
  838. UserOrgId: advice.UserOrgId,
  839. PatientId: advice.PatientId,
  840. SystemTime: advice.AdviceDate,
  841. ConsumableType: 7,
  842. IsSys: 1,
  843. WarehousingOrder: "",
  844. WarehouseOutId: 0,
  845. WarehouseOutOrderNumber: "",
  846. IsEdit: 0,
  847. CancelStockId: cancelStock.ID,
  848. CancelOrderNumber: cancelStock.OrderNumber,
  849. Manufacturer: warehouse.Manufacturer,
  850. Dealer: warehouse.Dealer,
  851. Creator: advice.ExecutionStaff,
  852. UpdateCreator: 0,
  853. Status: 1,
  854. Ctime: time.Now().Unix(),
  855. Mtime: 0,
  856. Price: warehouse.RetailPrice,
  857. WarehousingDetailId: warehouse.ID,
  858. WarehouseOutDetailId: 0,
  859. CancelOutDetailId: lastDrugCancelStock.ID,
  860. ExpireDate: warehouse.ExpiryDate,
  861. ProductDate: warehouse.ProductDate,
  862. MaxUnit: drup.MinUnit,
  863. MinUnit: "",
  864. AdviceId: advice.ID,
  865. StorehouseId: storeConfig.DrugStorehouseOut,
  866. LastPrice: warehouse.Price,
  867. }
  868. CreateDrugFlowOne(flow)
  869. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  870. //出库数量减
  871. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  872. }
  873. //查询是否存在数据
  874. details := &models.DrugAutomaticReduceDetail{
  875. WarehouseOutId: warehouseout.ID,
  876. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  877. PatientId: advice.PatientId,
  878. Ctime: time.Now().Unix(),
  879. Mtime: time.Now().Unix(),
  880. Status: 1,
  881. RecordTime: advice.RecordDate,
  882. OrgId: orgID,
  883. DrugId: advice.DrugId,
  884. Count: deliver_number,
  885. CountUnit: drup.MinUnit,
  886. WarehouseInfoId: warehouse.ID,
  887. StorehouseId: storeConfig.DrugStorehouseOut,
  888. AdviceId: advice.ID,
  889. }
  890. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  891. if errTwo != nil {
  892. return errTwo
  893. }
  894. return nil
  895. } else {
  896. info := models.XtDrugWarehouseInfo{
  897. ID: warehouse.ID,
  898. WarehousingId: warehouse.WarehousingId,
  899. DrugId: warehouse.DrugId,
  900. Number: warehouse.Number,
  901. ProductDate: warehouse.ProductDate,
  902. ExpiryDate: warehouse.ExpiryDate,
  903. WarehousingCount: warehouse.WarehousingCount,
  904. Price: warehouse.RetailPrice,
  905. TotalPrice: warehouse.TotalPrice,
  906. Dealer: warehouse.Dealer,
  907. Manufacturer: warehouse.Manufacturer,
  908. Remark: warehouse.Remark,
  909. Ctime: warehouse.Ctime,
  910. Mtime: warehouse.Mtime,
  911. Status: 1,
  912. OrgId: warehouse.OrgId,
  913. IsReturn: warehouse.IsReturn,
  914. WarehousingOrder: warehouse.WarehousingOrder,
  915. Type: warehouse.Type,
  916. RetailPrice: warehouse.RetailPrice,
  917. RetailTotalPrice: warehouse.RetailPrice,
  918. StockMaxNumber: 0,
  919. StockMinNumber: 0,
  920. BatchNumber: warehouse.BatchNumber,
  921. MaxUnit: warehouse.MaxUnit,
  922. WarehousingInfoId: warehouse.WarehousingInfoId,
  923. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  924. StorehouseId: storeConfig.DrugStorehouseOut,
  925. IsCheck: 1,
  926. }
  927. //扣减库存
  928. errThree := UpDateDrugWarehouseInfoByStock(&info)
  929. if errThree != nil {
  930. return errThree
  931. }
  932. //查询剩余库存
  933. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  934. var sum_count int64
  935. for _, its := range stockInfo {
  936. if its.MaxUnit == drup.MaxUnit {
  937. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  938. }
  939. sum_count += its.StockMaxNumber + its.StockMinNumber
  940. }
  941. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  942. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  943. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  944. WarehouseOutId: warehouseout.ID,
  945. Status: 1,
  946. Ctime: time.Now().Unix(),
  947. Remark: warehouse.Remark,
  948. OrgId: orgID,
  949. Type: 1,
  950. Manufacturer: warehouse.Manufacturer,
  951. Dealer: warehouse.Dealer,
  952. IsSys: 1,
  953. SysRecordTime: advice.RecordDate,
  954. DrugId: advice.DrugId,
  955. Number: warehouse.Number,
  956. BatchNumber: warehouse.BatchNumber,
  957. Price: warehouse.RetailPrice,
  958. CountUnit: drup.MinUnit,
  959. ProductDate: warehouse.ProductDate,
  960. ExpiryDate: warehouse.ExpiryDate,
  961. PatientId: advice.PatientId,
  962. Count: stock_number,
  963. WarehouseInfoId: warehouse.ID,
  964. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  965. StorehouseId: storeConfig.DrugStorehouseOut,
  966. IsCheck: 1,
  967. OverCount: sum_count,
  968. AdviceId: advice.ID,
  969. }
  970. //查询是否存在出库数据
  971. _, errCode := GetSigleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, advice.ID)
  972. if errCode == gorm.ErrRecordNotFound {
  973. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  974. if errOne != nil {
  975. return errOne
  976. }
  977. }
  978. if errCode == nil {
  979. UpdateSingleDrugWarehouseOutInfo(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo)
  980. }
  981. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  982. details := &models.DrugAutomaticReduceDetail{
  983. WarehouseOutId: warehouseout.ID,
  984. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  985. PatientId: advice.PatientId,
  986. Ctime: time.Now().Unix(),
  987. Mtime: time.Now().Unix(),
  988. Status: 1,
  989. RecordTime: advice.RecordDate,
  990. OrgId: orgID,
  991. DrugId: advice.DrugId,
  992. Count: stock_number,
  993. CountUnit: drup.MinUnit,
  994. WarehouseInfoId: warehouse.ID,
  995. StorehouseId: storeConfig.DrugStorehouseOut,
  996. AdviceId: advice.ID,
  997. }
  998. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  999. if errcodeThree == gorm.ErrRecordNotFound {
  1000. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1001. if errTwo != nil {
  1002. return errTwo
  1003. }
  1004. }
  1005. if errcodeThree == nil {
  1006. UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  1007. }
  1008. var out_count int64
  1009. var cancel_count int64
  1010. //查询出库数据
  1011. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1012. //查询退库数据
  1013. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1014. for _, item := range infoCountList {
  1015. out_count += item.Count
  1016. }
  1017. for _, item := range cancelInfoList {
  1018. cancel_count += item.Count
  1019. }
  1020. //如果本次出库数据大于历史出库数据 新增1条流水
  1021. if deliver_number > (out_count - cancel_count) {
  1022. drugflow := models.DrugFlow{
  1023. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1024. WarehouseOutId: warehouseout.ID,
  1025. DrugId: advice.DrugId,
  1026. Number: warehouse.Number,
  1027. ProductDate: warehouse.ProductDate,
  1028. ExpireDate: warehouse.ExpiryDate,
  1029. Count: stock_number,
  1030. Price: warehouse.RetailPrice,
  1031. Status: 1,
  1032. Ctime: time.Now().Unix(),
  1033. UserOrgId: orgID,
  1034. Manufacturer: warehouse.Manufacturer,
  1035. Dealer: warehouse.Dealer,
  1036. BatchNumber: warehouse.BatchNumber,
  1037. MaxUnit: drup.MinUnit,
  1038. ConsumableType: 3,
  1039. IsEdit: 1,
  1040. Creator: advice.ExecutionStaff,
  1041. IsSys: 1,
  1042. PatientId: advice.PatientId,
  1043. WarehousingDetailId: warehouse.ID,
  1044. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1045. StorehouseId: storeConfig.DrugStorehouseOut,
  1046. WarehouseOutDetailId: lastDrugOutInfo.ID,
  1047. OverCount: sum_count,
  1048. AdviceId: advice.ID,
  1049. }
  1050. CreateDrugFlowOne(drugflow)
  1051. //出库数量相加
  1052. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  1053. }
  1054. if deliver_number < (out_count - cancel_count) {
  1055. operation_time := time.Now().Unix()
  1056. ctime := time.Now().Unix()
  1057. timeStr := time.Now().Format("2006-01-02")
  1058. timeArr := strings.Split(timeStr, "-")
  1059. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  1060. total = total + 1
  1061. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1062. cancelStock := models.DrugCancelStock{
  1063. OrderNumber: orderNumber,
  1064. OperaTime: operation_time,
  1065. OrgId: advice.UserOrgId,
  1066. Creater: advice.ExecutionStaff,
  1067. Ctime: ctime,
  1068. Status: 1,
  1069. ReturnTime: advice.AdviceDate,
  1070. Dealer: 0,
  1071. Manufacturer: 0,
  1072. Type: 1,
  1073. StorehouseId: storeConfig.DrugStorehouseOut,
  1074. IsCheck: 1,
  1075. }
  1076. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1077. if errCode == gorm.ErrRecordNotFound {
  1078. AddSigleDrugCancelStock(&cancelStock)
  1079. }
  1080. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1081. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  1082. dealer, _ := GetDealerById(warehouse.Dealer)
  1083. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  1084. cancelStockInfo := models.DrugCancelStockInfo{
  1085. OrderNumber: cancelInfo.OrderNumber,
  1086. CancelStockId: lastDrugCancelStock.ID,
  1087. DrugId: advice.DrugId,
  1088. Count: (out_count - cancel_count) - deliver_number,
  1089. Status: 1,
  1090. Ctime: ctime,
  1091. OrgId: advice.UserOrgId,
  1092. Type: 1,
  1093. Manufacturer: manufactureName.ManufacturerName,
  1094. Dealer: dealer.DealerName,
  1095. Total: 0,
  1096. RetailPrice: warehouse.RetailPrice,
  1097. RetailTotalPrice: warehouse.RetailTotalPrice,
  1098. Price: warehouse.Price,
  1099. RegisterAccount: "",
  1100. Remark: warehouse.Remark,
  1101. BatchNumber: warehouse.BatchNumber,
  1102. MaxUnit: drup.MinUnit,
  1103. ProductDate: warehouse.ProductDate,
  1104. ExpiryDate: warehouse.ExpiryDate,
  1105. BatchNumberId: warehouse.ID,
  1106. StorehouseId: storeConfig.DrugStorehouseOut,
  1107. IsCheck: 1,
  1108. }
  1109. CreatedCancelStock(cancelStockInfo)
  1110. flow := models.DrugFlow{
  1111. WarehousingId: warehouse.ID,
  1112. DrugId: warehouse.DrugId,
  1113. Number: "",
  1114. BatchNumber: warehouse.BatchNumber,
  1115. Count: (out_count - cancel_count) - deliver_number,
  1116. UserOrgId: advice.UserOrgId,
  1117. PatientId: advice.PatientId,
  1118. SystemTime: advice.AdviceDate,
  1119. ConsumableType: 7,
  1120. IsSys: 1,
  1121. WarehousingOrder: "",
  1122. WarehouseOutId: 0,
  1123. WarehouseOutOrderNumber: "",
  1124. IsEdit: 0,
  1125. CancelStockId: cancelStock.ID,
  1126. CancelOrderNumber: cancelStock.OrderNumber,
  1127. Manufacturer: warehouse.Manufacturer,
  1128. Dealer: warehouse.Dealer,
  1129. Creator: advice.ExecutionStaff,
  1130. UpdateCreator: 0,
  1131. Status: 1,
  1132. Ctime: time.Now().Unix(),
  1133. Mtime: 0,
  1134. Price: warehouse.Price,
  1135. WarehousingDetailId: warehouse.ID,
  1136. WarehouseOutDetailId: 0,
  1137. CancelOutDetailId: lastDrugCancelStock.ID,
  1138. ExpireDate: warehouse.ExpiryDate,
  1139. ProductDate: warehouse.ProductDate,
  1140. MaxUnit: drup.MinUnit,
  1141. MinUnit: "",
  1142. AdviceId: advice.ID,
  1143. StorehouseId: storeConfig.DrugStorehouseOut,
  1144. }
  1145. CreateDrugFlowOne(flow)
  1146. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  1147. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  1148. }
  1149. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1150. prescribingNumber_two_temp := deliver_number - stock_number
  1151. overPlusNumber := float64(prescribingNumber_two_temp)
  1152. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  1153. advice.PrescribingNumberUnit = drup.MinUnit
  1154. BloodDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  1155. }
  1156. return
  1157. }
  1158. // 药品自动出库 递归方式
  1159. func BloodHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo, hisprescribingNumber float64, unit string) (err error) {
  1160. fmt.Println("出库拉22222222222222222222222222222222222222222222222222222222222222222222")
  1161. fmt.Println("剩余库存2哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦噢噢噢噢", prescribingNumber)
  1162. //开事务
  1163. tx := XTWriteDB().Begin()
  1164. defer func() {
  1165. if err != nil {
  1166. utils.ErrorLog("事务失败,原因为: %v", err)
  1167. tx.Rollback()
  1168. } else {
  1169. tx.Commit()
  1170. }
  1171. }()
  1172. drugError := models.XtDrugError{
  1173. UserOrgId: orgID,
  1174. DrugId: advice.DrugId,
  1175. RecordDate: advice.AdviceDate,
  1176. PatientId: advice.PatientId,
  1177. Remark: "进入出库页面",
  1178. Status: 1,
  1179. Ctime: time.Now().Unix(),
  1180. Mtime: 0,
  1181. SumCount: 0,
  1182. Prescribingnumber: advice.PrescribingNumber,
  1183. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1184. }
  1185. CreateDrugError(drugError)
  1186. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1187. var deliver_number int64 = 0
  1188. var deliver_number_one int64 = 0
  1189. var stock_number int64 = 0
  1190. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  1191. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1192. prescribingNumber_temp_one := strconv.FormatFloat(math.Abs(hisprescribingNumber), 'f', 0, 64)
  1193. countone, _ := strconv.ParseInt(prescribingNumber_temp_one, 10, 64)
  1194. var drug_price float64
  1195. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1196. deliver_number = count * drup.MinNumber
  1197. } else {
  1198. deliver_number = count
  1199. }
  1200. if unit == drup.MaxUnit {
  1201. deliver_number_one = countone * drup.MinNumber
  1202. } else {
  1203. deliver_number_one = countone
  1204. }
  1205. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1206. drug_price = drup.RetailPrice
  1207. }
  1208. fmt.Println(drug_price)
  1209. if advice.PrescribingNumberUnit == drup.MinUnit {
  1210. drug_price = drup.MinPrice
  1211. }
  1212. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  1213. drug_price = drup.RetailPrice
  1214. }
  1215. // 根据先进先出原则,查询最先入库的批次,进行出库
  1216. // 如果没有对应的库存,则报错
  1217. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  1218. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1219. if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  1220. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  1221. var stockMax int64
  1222. var stockMin int64
  1223. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  1224. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  1225. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  1226. UpdateMinNumber(lastWarehouse.ID, stockMin)
  1227. }
  1228. }
  1229. if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
  1230. lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
  1231. ChangeMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber)
  1232. }
  1233. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1234. if err != nil {
  1235. drugError := models.XtDrugError{
  1236. UserOrgId: orgID,
  1237. DrugId: advice.DrugId,
  1238. RecordDate: advice.AdviceDate,
  1239. PatientId: advice.PatientId,
  1240. Remark: "查询库存报错",
  1241. Status: 1,
  1242. Ctime: time.Now().Unix(),
  1243. Mtime: 0,
  1244. SumCount: 0,
  1245. Prescribingnumber: advice.PrescribingNumber,
  1246. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1247. }
  1248. CreateDrugError(drugError)
  1249. return err
  1250. }
  1251. // 将该批次的剩余库存数量转换为拆零数量
  1252. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1253. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1254. }
  1255. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  1256. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  1257. }
  1258. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1259. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1260. }
  1261. fmt.Println("药品名称", drup.DrugName)
  1262. fmt.Println("库存数量我哦我我", stock_number)
  1263. fmt.Println("出库数量", deliver_number)
  1264. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1265. if stock_number >= deliver_number {
  1266. var maxNumber int64 = 0
  1267. var minNumber int64 = 0
  1268. maxNumber = deliver_number / drup.MinNumber
  1269. minNumber = deliver_number % drup.MinNumber
  1270. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1271. minNumber = maxNumber
  1272. }
  1273. if drup.MaxUnit != drup.MinUnit {
  1274. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  1275. return errors.New("库存数量不足")
  1276. }
  1277. }
  1278. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1279. //fmt.Println("金1")
  1280. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  1281. }
  1282. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1283. //fmt.Println("金2")
  1284. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1285. }
  1286. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1287. //fmt.Println("金3")
  1288. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1289. }
  1290. if warehouse.StockMaxNumber < 0 {
  1291. warehouse.StockMaxNumber = 0
  1292. }
  1293. if warehouse.StockMinNumber < 0 {
  1294. warehouse.StockMinNumber = 0
  1295. }
  1296. warehouse.Mtime = time.Now().Unix()
  1297. if warehouse.StockMinNumber < minNumber {
  1298. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1299. if warehouse.MaxUnit != warehouse.MinUnit {
  1300. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  1301. }
  1302. if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
  1303. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1304. }
  1305. } else {
  1306. if minNumber > 0 {
  1307. if (warehouse.StockMinNumber - minNumber) >= 0 {
  1308. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  1309. }
  1310. }
  1311. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  1312. if warehouse.StockMinNumber > 0 {
  1313. if warehouse.StockMinNumber-deliver_number >= 0 {
  1314. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  1315. }
  1316. }
  1317. }
  1318. }
  1319. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
  1320. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  1321. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  1322. }
  1323. }
  1324. if drup.MaxUnit != drup.MinUnit {
  1325. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  1326. return errors.New("库存数量不足")
  1327. }
  1328. }
  1329. if warehouse.StockMinNumber <= 0 {
  1330. warehouse.StockMinNumber = 0
  1331. }
  1332. //扣减库存数据
  1333. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1334. if errThree != nil {
  1335. drugError := models.XtDrugError{
  1336. UserOrgId: orgID,
  1337. DrugId: advice.DrugId,
  1338. RecordDate: advice.AdviceDate,
  1339. PatientId: advice.PatientId,
  1340. Remark: "扣减库存失败",
  1341. Status: 1,
  1342. Ctime: time.Now().Unix(),
  1343. Mtime: 0,
  1344. SumCount: 0,
  1345. Prescribingnumber: advice.PrescribingNumber,
  1346. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1347. }
  1348. CreateDrugError(drugError)
  1349. return errThree
  1350. }
  1351. //更改字典里面的价格
  1352. if advice.UserOrgId == 10265 || advice.UserOrgId == 10215 {
  1353. UpdateBasePrice(lastWarehouse.DrugId, lastWarehouse.RetailPrice)
  1354. }
  1355. //查询剩余库存
  1356. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  1357. var sum_count int64
  1358. for _, its := range stockInfo {
  1359. if its.MaxUnit == drup.MaxUnit {
  1360. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  1361. }
  1362. sum_count += its.StockMaxNumber + its.StockMinNumber
  1363. }
  1364. if orgID != 10188 && orgID != 10217 && orgID != 3877 {
  1365. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1366. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1367. WarehouseOutId: warehouseout.ID,
  1368. Status: 1,
  1369. Ctime: advice.RecordDate,
  1370. Mtime: time.Now().Unix(),
  1371. Remark: warehouse.Remark,
  1372. OrgId: orgID,
  1373. Type: 1,
  1374. Manufacturer: warehouse.Manufacturer,
  1375. Dealer: warehouse.Dealer,
  1376. IsSys: 1,
  1377. SysRecordTime: advice.RecordDate,
  1378. DrugId: advice.DrugId,
  1379. Number: warehouse.Number,
  1380. BatchNumber: warehouse.BatchNumber,
  1381. Price: warehouse.RetailPrice,
  1382. CountUnit: drup.MinUnit,
  1383. RetailPrice: warehouse.RetailPrice,
  1384. ProductDate: warehouse.ProductDate,
  1385. ExpiryDate: warehouse.ExpiryDate,
  1386. PatientId: advice.PatientId,
  1387. Count: deliver_number, //出最小单位
  1388. WarehouseInfoId: warehouse.ID,
  1389. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1390. StorehouseId: storeConfig.DrugStorehouseOut,
  1391. IsCheck: 1,
  1392. OverCount: sum_count,
  1393. AdviceId: advice.ID,
  1394. }
  1395. //查询今日该药品该患者是否有出库数据
  1396. lastDrugOutInfo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  1397. if lastDrugOutInfo.ID == 0 {
  1398. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1399. if errOne != nil {
  1400. drugError := models.XtDrugError{
  1401. UserOrgId: orgID,
  1402. DrugId: advice.DrugId,
  1403. RecordDate: advice.AdviceDate,
  1404. PatientId: advice.PatientId,
  1405. Remark: "创建出库单失败",
  1406. Status: 1,
  1407. Ctime: time.Now().Unix(),
  1408. Mtime: 0,
  1409. SumCount: 0,
  1410. Prescribingnumber: advice.PrescribingNumber,
  1411. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1412. }
  1413. CreateDrugError(drugError)
  1414. }
  1415. }
  1416. if lastDrugOutInfo.ID > 0 {
  1417. //删除在新增
  1418. UpdateDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID)
  1419. AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1420. }
  1421. }
  1422. var out_count int64
  1423. var cancel_count int64
  1424. //查询出库数据
  1425. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1426. //查询退库数据
  1427. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1428. for _, item := range infoCountList {
  1429. out_count += item.Count
  1430. }
  1431. for _, item := range cancelInfoList {
  1432. cancel_count += item.Count
  1433. }
  1434. if deliver_number_one-(out_count-cancel_count) >= 0 {
  1435. if (deliver_number_one - out_count - cancel_count) != 0 {
  1436. if orgID == 10217 || orgID == 10188 || orgID == 3877 {
  1437. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1438. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1439. WarehouseOutId: warehouseout.ID,
  1440. Status: 1,
  1441. Ctime: advice.RecordDate,
  1442. Mtime: time.Now().Unix(),
  1443. Remark: warehouse.Remark,
  1444. OrgId: orgID,
  1445. Type: 1,
  1446. Manufacturer: warehouse.Manufacturer,
  1447. Dealer: warehouse.Dealer,
  1448. IsSys: 1,
  1449. SysRecordTime: advice.RecordDate,
  1450. DrugId: advice.DrugId,
  1451. Number: warehouse.Number,
  1452. BatchNumber: warehouse.BatchNumber,
  1453. Price: warehouse.RetailPrice,
  1454. CountUnit: drup.MinUnit,
  1455. RetailPrice: warehouse.RetailPrice,
  1456. ProductDate: warehouse.ProductDate,
  1457. ExpiryDate: warehouse.ExpiryDate,
  1458. PatientId: advice.PatientId,
  1459. Count: deliver_number_one - (out_count - cancel_count), //出最小单位
  1460. WarehouseInfoId: warehouse.ID,
  1461. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1462. StorehouseId: storeConfig.DrugStorehouseOut,
  1463. IsCheck: 1,
  1464. OverCount: sum_count,
  1465. AdviceId: advice.ID,
  1466. }
  1467. errThreeThrtiy := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1468. if errThreeThrtiy != nil {
  1469. drugError := models.XtDrugError{
  1470. UserOrgId: orgID,
  1471. DrugId: advice.DrugId,
  1472. RecordDate: advice.AdviceDate,
  1473. PatientId: advice.PatientId,
  1474. Remark: "插入数据报错",
  1475. Status: 1,
  1476. Ctime: time.Now().Unix(),
  1477. Mtime: 0,
  1478. SumCount: 0,
  1479. Prescribingnumber: advice.PrescribingNumber,
  1480. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1481. }
  1482. CreateDrugError(drugError)
  1483. }
  1484. }
  1485. lastDrugOutInfo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  1486. drugflow := models.DrugFlow{
  1487. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1488. WarehouseOutId: warehouseout.ID,
  1489. DrugId: advice.DrugId,
  1490. Number: warehouse.Number,
  1491. ProductDate: warehouse.ProductDate,
  1492. ExpireDate: warehouse.ExpiryDate,
  1493. Count: deliver_number_one - (out_count - cancel_count), //按最小单位计算,
  1494. Price: warehouse.RetailPrice,
  1495. Status: 1,
  1496. Ctime: advice.AdviceDate,
  1497. Mtime: time.Now().Unix(),
  1498. UserOrgId: orgID,
  1499. Manufacturer: warehouse.Manufacturer,
  1500. Dealer: warehouse.Dealer,
  1501. BatchNumber: warehouse.BatchNumber,
  1502. MaxUnit: drup.MinUnit,
  1503. ConsumableType: 3,
  1504. IsEdit: 1,
  1505. Creator: advice.ExecutionStaff,
  1506. IsSys: 1,
  1507. PatientId: advice.PatientId,
  1508. SystemTime: advice.AdviceDate,
  1509. WarehousingDetailId: warehouse.ID,
  1510. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1511. StorehouseId: storeConfig.DrugStorehouseOut,
  1512. WarehouseOutDetailId: lastDrugOutInfo.ID,
  1513. OverCount: sum_count,
  1514. AdviceId: advice.ID,
  1515. LastPrice: warehouse.Price,
  1516. }
  1517. errThreeTy := CreateDrugFlowOne(drugflow)
  1518. if errThreeTy != nil {
  1519. drugError := models.XtDrugError{
  1520. UserOrgId: orgID,
  1521. DrugId: advice.DrugId,
  1522. RecordDate: advice.AdviceDate,
  1523. PatientId: advice.PatientId,
  1524. Remark: "创建流水失败",
  1525. Status: 1,
  1526. Ctime: time.Now().Unix(),
  1527. Mtime: 0,
  1528. SumCount: 0,
  1529. Prescribingnumber: advice.PrescribingNumber,
  1530. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1531. }
  1532. CreateDrugError(drugError)
  1533. }
  1534. //出库数量相加
  1535. errThreeTys := AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  1536. if errThreeTys != nil {
  1537. drugError := models.XtDrugError{
  1538. UserOrgId: orgID,
  1539. DrugId: advice.DrugId,
  1540. RecordDate: advice.AdviceDate,
  1541. PatientId: advice.PatientId,
  1542. Remark: "出库数量相加失败",
  1543. Status: 1,
  1544. Ctime: time.Now().Unix(),
  1545. Mtime: 0,
  1546. SumCount: 0,
  1547. Prescribingnumber: advice.PrescribingNumber,
  1548. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1549. }
  1550. CreateDrugError(drugError)
  1551. }
  1552. }
  1553. }
  1554. //如果本次出库数据小于历史出库数据 新增1条退库流水
  1555. if deliver_number_one-(out_count-cancel_count) < 0 {
  1556. operation_time := time.Now().Unix()
  1557. ctime := time.Now().Unix()
  1558. timeStr := time.Now().Format("2006-01-02")
  1559. timeArr := strings.Split(timeStr, "-")
  1560. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  1561. total = total + 1
  1562. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1563. cancelStock := models.DrugCancelStock{
  1564. OrderNumber: orderNumber,
  1565. OperaTime: operation_time,
  1566. OrgId: advice.UserOrgId,
  1567. Creater: advice.ExecutionStaff,
  1568. Ctime: ctime,
  1569. Status: 1,
  1570. ReturnTime: advice.AdviceDate,
  1571. Dealer: 0,
  1572. Manufacturer: 0,
  1573. Type: 1,
  1574. StorehouseId: storeConfig.DrugStorehouseOut,
  1575. IsCheck: 1,
  1576. }
  1577. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1578. if errCode == gorm.ErrRecordNotFound {
  1579. errCode := AddSigleDrugCancelStock(&cancelStock)
  1580. if errCode != nil {
  1581. drugError := models.XtDrugError{
  1582. UserOrgId: orgID,
  1583. DrugId: advice.DrugId,
  1584. RecordDate: advice.AdviceDate,
  1585. PatientId: advice.PatientId,
  1586. Remark: "创建退库单失败",
  1587. Status: 1,
  1588. Ctime: time.Now().Unix(),
  1589. Mtime: 0,
  1590. SumCount: 0,
  1591. Prescribingnumber: advice.PrescribingNumber,
  1592. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1593. }
  1594. CreateDrugError(drugError)
  1595. }
  1596. }
  1597. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1598. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  1599. dealer, _ := GetDealerById(warehouse.Dealer)
  1600. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  1601. cancelStockInfo := models.DrugCancelStockInfo{
  1602. OrderNumber: cancelInfo.OrderNumber,
  1603. CancelStockId: lastDrugCancelStock.ID,
  1604. DrugId: advice.DrugId,
  1605. Count: (out_count - cancel_count) - deliver_number,
  1606. Status: 1,
  1607. Ctime: advice.AdviceDate,
  1608. Mtime: time.Now().Unix(),
  1609. OrgId: advice.UserOrgId,
  1610. Type: 1,
  1611. Manufacturer: manufactureName.ManufacturerName,
  1612. Dealer: dealer.DealerName,
  1613. Total: 0,
  1614. RetailPrice: warehouse.RetailPrice,
  1615. RetailTotalPrice: warehouse.RetailTotalPrice,
  1616. Price: warehouse.Price,
  1617. RegisterAccount: "",
  1618. Remark: warehouse.Remark,
  1619. BatchNumber: warehouse.BatchNumber,
  1620. MaxUnit: drup.MinUnit,
  1621. ProductDate: warehouse.ProductDate,
  1622. ExpiryDate: warehouse.ExpiryDate,
  1623. BatchNumberId: warehouse.ID,
  1624. StorehouseId: storeConfig.DrugStorehouseOut,
  1625. IsCheck: 1,
  1626. }
  1627. errCodes := CreatedCancelStock(cancelStockInfo)
  1628. if errCodes != nil {
  1629. drugError := models.XtDrugError{
  1630. UserOrgId: orgID,
  1631. DrugId: advice.DrugId,
  1632. RecordDate: advice.AdviceDate,
  1633. PatientId: advice.PatientId,
  1634. Remark: "创建退库单失败",
  1635. Status: 1,
  1636. Ctime: time.Now().Unix(),
  1637. Mtime: 0,
  1638. SumCount: 0,
  1639. Prescribingnumber: advice.PrescribingNumber,
  1640. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1641. }
  1642. CreateDrugError(drugError)
  1643. }
  1644. flow := models.DrugFlow{
  1645. WarehousingId: warehouse.ID,
  1646. DrugId: warehouse.DrugId,
  1647. Number: "",
  1648. BatchNumber: warehouse.BatchNumber,
  1649. Count: out_count - cancel_count - deliver_number,
  1650. UserOrgId: advice.UserOrgId,
  1651. PatientId: advice.PatientId,
  1652. SystemTime: advice.AdviceDate,
  1653. ConsumableType: 7,
  1654. IsSys: 0,
  1655. WarehousingOrder: "",
  1656. WarehouseOutId: 0,
  1657. WarehouseOutOrderNumber: "",
  1658. IsEdit: 0,
  1659. CancelStockId: cancelStock.ID,
  1660. CancelOrderNumber: cancelStock.OrderNumber,
  1661. Manufacturer: warehouse.Manufacturer,
  1662. Dealer: warehouse.Dealer,
  1663. Creator: advice.ExecutionStaff,
  1664. UpdateCreator: 0,
  1665. Status: 1,
  1666. Ctime: advice.AdviceDate,
  1667. Mtime: time.Now().Unix(),
  1668. Price: warehouse.RetailPrice,
  1669. WarehousingDetailId: warehouse.ID,
  1670. WarehouseOutDetailId: 0,
  1671. CancelOutDetailId: lastDrugCancelStock.ID,
  1672. ExpireDate: warehouse.ExpiryDate,
  1673. ProductDate: warehouse.ProductDate,
  1674. MaxUnit: drup.MinUnit,
  1675. MinUnit: "",
  1676. AdviceId: advice.ID,
  1677. StorehouseId: storeConfig.DrugStorehouseOut,
  1678. LastPrice: warehouse.Price,
  1679. OverCount: sum_count,
  1680. }
  1681. errCodeflow := CreateDrugFlowOne(flow)
  1682. if errCodeflow != nil {
  1683. drugError := models.XtDrugError{
  1684. UserOrgId: orgID,
  1685. DrugId: advice.DrugId,
  1686. RecordDate: advice.AdviceDate,
  1687. PatientId: advice.PatientId,
  1688. Remark: "退库流水创建失败",
  1689. Status: 1,
  1690. Ctime: time.Now().Unix(),
  1691. Mtime: 0,
  1692. SumCount: 0,
  1693. Prescribingnumber: advice.PrescribingNumber,
  1694. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1695. }
  1696. CreateDrugError(drugError)
  1697. }
  1698. errCodesum := AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  1699. if errCodesum != nil {
  1700. drugError := models.XtDrugError{
  1701. UserOrgId: orgID,
  1702. DrugId: advice.DrugId,
  1703. RecordDate: advice.AdviceDate,
  1704. PatientId: advice.PatientId,
  1705. Remark: "增加退库数量失败",
  1706. Status: 1,
  1707. Ctime: time.Now().Unix(),
  1708. Mtime: 0,
  1709. SumCount: 0,
  1710. Prescribingnumber: advice.PrescribingNumber,
  1711. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1712. }
  1713. CreateDrugError(drugError)
  1714. }
  1715. errCodereduce := ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  1716. if errCodereduce != nil {
  1717. drugError := models.XtDrugError{
  1718. UserOrgId: orgID,
  1719. DrugId: advice.DrugId,
  1720. RecordDate: advice.AdviceDate,
  1721. PatientId: advice.PatientId,
  1722. Remark: "减少出库数量失败",
  1723. Status: 1,
  1724. Ctime: time.Now().Unix(),
  1725. Mtime: 0,
  1726. SumCount: 0,
  1727. Prescribingnumber: advice.PrescribingNumber,
  1728. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1729. }
  1730. CreateDrugError(drugError)
  1731. }
  1732. }
  1733. //查询是否存在数据
  1734. details := &models.DrugAutomaticReduceDetail{
  1735. WarehouseOutId: warehouseout.ID,
  1736. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1737. PatientId: advice.PatientId,
  1738. Ctime: advice.RecordDate,
  1739. Mtime: time.Now().Unix(),
  1740. Status: 1,
  1741. RecordTime: advice.RecordDate,
  1742. OrgId: orgID,
  1743. DrugId: advice.DrugId,
  1744. Count: deliver_number,
  1745. CountUnit: drup.MinUnit,
  1746. WarehouseInfoId: warehouse.ID,
  1747. StorehouseId: storeConfig.DrugStorehouseOut,
  1748. AdviceId: advice.ID,
  1749. }
  1750. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1751. if errTwo != nil {
  1752. drugError := models.XtDrugError{
  1753. UserOrgId: orgID,
  1754. DrugId: advice.DrugId,
  1755. RecordDate: advice.AdviceDate,
  1756. PatientId: advice.PatientId,
  1757. Remark: "新增自动出库表失败",
  1758. Status: 1,
  1759. Ctime: time.Now().Unix(),
  1760. Mtime: 0,
  1761. SumCount: 0,
  1762. Prescribingnumber: advice.PrescribingNumber,
  1763. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1764. }
  1765. CreateDrugError(drugError)
  1766. return errTwo
  1767. }
  1768. return nil
  1769. } else {
  1770. info := models.XtDrugWarehouseInfo{
  1771. ID: warehouse.ID,
  1772. WarehousingId: warehouse.WarehousingId,
  1773. DrugId: warehouse.DrugId,
  1774. Number: warehouse.Number,
  1775. ProductDate: warehouse.ProductDate,
  1776. ExpiryDate: warehouse.ExpiryDate,
  1777. WarehousingCount: warehouse.WarehousingCount,
  1778. Price: warehouse.RetailPrice,
  1779. TotalPrice: warehouse.TotalPrice,
  1780. Dealer: warehouse.Dealer,
  1781. Manufacturer: warehouse.Manufacturer,
  1782. Remark: warehouse.Remark,
  1783. Ctime: warehouse.Ctime,
  1784. Mtime: warehouse.Mtime,
  1785. Status: 1,
  1786. OrgId: warehouse.OrgId,
  1787. IsReturn: warehouse.IsReturn,
  1788. WarehousingOrder: warehouse.WarehousingOrder,
  1789. Type: warehouse.Type,
  1790. RetailPrice: warehouse.RetailPrice,
  1791. RetailTotalPrice: warehouse.RetailPrice,
  1792. StockMaxNumber: 0,
  1793. StockMinNumber: 0,
  1794. BatchNumber: warehouse.BatchNumber,
  1795. MaxUnit: warehouse.MaxUnit,
  1796. MinUnit: drup.MinUnit,
  1797. WarehousingInfoId: warehouse.WarehousingInfoId,
  1798. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1799. StorehouseId: storeConfig.DrugStorehouseOut,
  1800. IsCheck: 1,
  1801. }
  1802. //扣减库存
  1803. errThree := UpDateDrugWarehouseInfoByStock(&info)
  1804. if errThree != nil {
  1805. drugError := models.XtDrugError{
  1806. UserOrgId: orgID,
  1807. DrugId: advice.DrugId,
  1808. RecordDate: advice.AdviceDate,
  1809. PatientId: advice.PatientId,
  1810. Remark: "第二批次扣减库存失败",
  1811. Status: 1,
  1812. Ctime: time.Now().Unix(),
  1813. Mtime: 0,
  1814. SumCount: 0,
  1815. Prescribingnumber: advice.PrescribingNumber,
  1816. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1817. }
  1818. CreateDrugError(drugError)
  1819. return errThree
  1820. }
  1821. //查询剩余库存
  1822. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  1823. var sum_count int64
  1824. for _, its := range stockInfo {
  1825. if its.MaxUnit == drup.MaxUnit {
  1826. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  1827. }
  1828. sum_count += its.StockMaxNumber + its.StockMinNumber
  1829. }
  1830. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1831. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1832. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1833. WarehouseOutId: warehouseout.ID,
  1834. Status: 1,
  1835. Ctime: advice.RecordDate,
  1836. Mtime: time.Now().Unix(),
  1837. Remark: warehouse.Remark,
  1838. OrgId: orgID,
  1839. Type: 1,
  1840. Manufacturer: warehouse.Manufacturer,
  1841. Dealer: warehouse.Dealer,
  1842. IsSys: 1,
  1843. SysRecordTime: advice.RecordDate,
  1844. DrugId: advice.DrugId,
  1845. Number: warehouse.Number,
  1846. BatchNumber: warehouse.BatchNumber,
  1847. Price: warehouse.RetailPrice,
  1848. CountUnit: drup.MinUnit,
  1849. ProductDate: warehouse.ProductDate,
  1850. ExpiryDate: warehouse.ExpiryDate,
  1851. PatientId: advice.PatientId,
  1852. Count: stock_number,
  1853. WarehouseInfoId: warehouse.ID,
  1854. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1855. StorehouseId: storeConfig.DrugStorehouseOut,
  1856. IsCheck: 1,
  1857. OverCount: sum_count,
  1858. AdviceId: advice.ID,
  1859. }
  1860. //查询该药品是否存在出库数据
  1861. _, errCode := GetSigleDrugWarehouseOutInfoTwo(advice.PatientId, advice.AdviceDate, orgID, advice.DrugId)
  1862. if errCode == gorm.ErrRecordNotFound {
  1863. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1864. if errOne != nil {
  1865. drugError := models.XtDrugError{
  1866. UserOrgId: orgID,
  1867. DrugId: advice.DrugId,
  1868. RecordDate: advice.AdviceDate,
  1869. PatientId: advice.PatientId,
  1870. Remark: "创建出库表失败",
  1871. Status: 1,
  1872. Ctime: time.Now().Unix(),
  1873. Mtime: 0,
  1874. SumCount: 0,
  1875. Prescribingnumber: advice.PrescribingNumber,
  1876. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1877. }
  1878. CreateDrugError(drugError)
  1879. return errOne
  1880. }
  1881. }
  1882. if errCode == nil {
  1883. lastDrugOutInfo, _ := GetSigleDrugWarehouseOutInfoTwo(advice.PatientId, advice.AdviceDate, orgID, advice.DrugId)
  1884. var his_count int64
  1885. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  1886. his_count = lastDrugOutInfo.Count * drup.MinNumber
  1887. }
  1888. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  1889. his_count = lastDrugOutInfo.Count
  1890. }
  1891. if deliver_number != his_count {
  1892. errThree := UpdateSingleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo, advice.ID)
  1893. if errThree != nil {
  1894. drugError := models.XtDrugError{
  1895. UserOrgId: orgID,
  1896. DrugId: advice.DrugId,
  1897. RecordDate: advice.AdviceDate,
  1898. PatientId: advice.PatientId,
  1899. Remark: "更新出库表失败",
  1900. Status: 1,
  1901. Ctime: time.Now().Unix(),
  1902. Mtime: 0,
  1903. SumCount: 0,
  1904. Prescribingnumber: advice.PrescribingNumber,
  1905. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1906. }
  1907. CreateDrugError(drugError)
  1908. return errThree
  1909. }
  1910. }
  1911. }
  1912. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  1913. details := &models.DrugAutomaticReduceDetail{
  1914. WarehouseOutId: warehouseout.ID,
  1915. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1916. PatientId: advice.PatientId,
  1917. Ctime: advice.AdviceDate,
  1918. Mtime: time.Now().Unix(),
  1919. Status: 1,
  1920. RecordTime: advice.RecordDate,
  1921. OrgId: orgID,
  1922. DrugId: advice.DrugId,
  1923. Count: stock_number,
  1924. CountUnit: drup.MinUnit,
  1925. WarehouseInfoId: warehouse.ID,
  1926. StorehouseId: storeConfig.DrugStorehouseOut,
  1927. AdviceId: advice.ID,
  1928. }
  1929. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  1930. if errcodeThree == gorm.ErrRecordNotFound {
  1931. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1932. if errTwo != nil {
  1933. drugError := models.XtDrugError{
  1934. UserOrgId: orgID,
  1935. DrugId: advice.DrugId,
  1936. RecordDate: advice.AdviceDate,
  1937. PatientId: advice.PatientId,
  1938. Remark: "第二批次新增自动出库表失败",
  1939. Status: 1,
  1940. Ctime: time.Now().Unix(),
  1941. Mtime: 0,
  1942. SumCount: 0,
  1943. Prescribingnumber: advice.PrescribingNumber,
  1944. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1945. }
  1946. CreateDrugError(drugError)
  1947. return errTwo
  1948. }
  1949. }
  1950. if errcodeThree == nil {
  1951. errThreeauto := UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  1952. if errThreeauto != nil {
  1953. drugError := models.XtDrugError{
  1954. UserOrgId: orgID,
  1955. DrugId: advice.DrugId,
  1956. RecordDate: advice.AdviceDate,
  1957. PatientId: advice.PatientId,
  1958. Remark: "第二批次更新自动出库表失败",
  1959. Status: 1,
  1960. Ctime: time.Now().Unix(),
  1961. Mtime: 0,
  1962. SumCount: 0,
  1963. Prescribingnumber: advice.PrescribingNumber,
  1964. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  1965. }
  1966. CreateDrugError(drugError)
  1967. }
  1968. }
  1969. var out_count int64
  1970. var cancel_count int64
  1971. //查询出库数据
  1972. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1973. //查询退库数据
  1974. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1975. for _, item := range infoCountList {
  1976. out_count += item.Count
  1977. }
  1978. for _, item := range cancelInfoList {
  1979. cancel_count += item.Count
  1980. }
  1981. //如果本次出库数据大于历史出库数据 新增1条流水
  1982. if deliver_number > (out_count - cancel_count) {
  1983. drugflow := models.DrugFlow{
  1984. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1985. WarehouseOutId: warehouseout.ID,
  1986. DrugId: advice.DrugId,
  1987. Number: warehouse.Number,
  1988. ProductDate: warehouse.ProductDate,
  1989. ExpireDate: warehouse.ExpiryDate,
  1990. Count: stock_number,
  1991. Price: warehouse.RetailPrice,
  1992. Status: 1,
  1993. Ctime: advice.AdviceDate,
  1994. Mtime: time.Now().Unix(),
  1995. UserOrgId: orgID,
  1996. Manufacturer: warehouse.Manufacturer,
  1997. Dealer: warehouse.Dealer,
  1998. BatchNumber: warehouse.BatchNumber,
  1999. MaxUnit: drup.MinUnit,
  2000. ConsumableType: 3,
  2001. IsEdit: 1,
  2002. Creator: advice.ExecutionStaff,
  2003. IsSys: 1,
  2004. PatientId: advice.PatientId,
  2005. WarehousingDetailId: warehouse.ID,
  2006. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2007. StorehouseId: storeConfig.DrugStorehouseOut,
  2008. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2009. OverCount: sum_count,
  2010. AdviceId: advice.ID,
  2011. LastPrice: warehouse.Price,
  2012. SystemTime: advice.AdviceDate,
  2013. }
  2014. CreateDrugFlowOne(drugflow)
  2015. //出库数量相加
  2016. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2017. }
  2018. if deliver_number < (out_count - cancel_count) {
  2019. operation_time := time.Now().Unix()
  2020. ctime := time.Now().Unix()
  2021. timeStr := time.Now().Format("2006-01-02")
  2022. timeArr := strings.Split(timeStr, "-")
  2023. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  2024. total = total + 1
  2025. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  2026. cancelStock := models.DrugCancelStock{
  2027. OrderNumber: orderNumber,
  2028. OperaTime: operation_time,
  2029. OrgId: advice.UserOrgId,
  2030. Creater: advice.ExecutionStaff,
  2031. Ctime: ctime,
  2032. Status: 1,
  2033. ReturnTime: advice.AdviceDate,
  2034. Dealer: 0,
  2035. Manufacturer: 0,
  2036. Type: 1,
  2037. StorehouseId: storeConfig.DrugStorehouseOut,
  2038. IsCheck: 1,
  2039. }
  2040. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2041. if errCode == gorm.ErrRecordNotFound {
  2042. errCodeCancel := AddSigleDrugCancelStock(&cancelStock)
  2043. if errCodeCancel != nil {
  2044. drugError := models.XtDrugError{
  2045. UserOrgId: orgID,
  2046. DrugId: advice.DrugId,
  2047. RecordDate: advice.AdviceDate,
  2048. PatientId: advice.PatientId,
  2049. Remark: "第二批次新增流水表失败",
  2050. Status: 1,
  2051. Ctime: time.Now().Unix(),
  2052. Mtime: 0,
  2053. SumCount: 0,
  2054. Prescribingnumber: advice.PrescribingNumber,
  2055. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  2056. }
  2057. CreateDrugError(drugError)
  2058. }
  2059. }
  2060. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2061. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  2062. dealer, _ := GetDealerById(warehouse.Dealer)
  2063. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  2064. cancelStockInfo := models.DrugCancelStockInfo{
  2065. OrderNumber: cancelInfo.OrderNumber,
  2066. CancelStockId: lastDrugCancelStock.ID,
  2067. DrugId: advice.DrugId,
  2068. Count: (out_count - cancel_count) - deliver_number,
  2069. Status: 1,
  2070. Ctime: advice.AdviceDate,
  2071. Mtime: time.Now().Unix(),
  2072. OrgId: advice.UserOrgId,
  2073. Type: 1,
  2074. Manufacturer: manufactureName.ManufacturerName,
  2075. Dealer: dealer.DealerName,
  2076. Total: 0,
  2077. RetailPrice: warehouse.RetailPrice,
  2078. RetailTotalPrice: warehouse.RetailTotalPrice,
  2079. Price: warehouse.Price,
  2080. RegisterAccount: "",
  2081. Remark: warehouse.Remark,
  2082. BatchNumber: warehouse.BatchNumber,
  2083. MaxUnit: drup.MinUnit,
  2084. ProductDate: warehouse.ProductDate,
  2085. ExpiryDate: warehouse.ExpiryDate,
  2086. BatchNumberId: warehouse.ID,
  2087. StorehouseId: storeConfig.DrugStorehouseOut,
  2088. IsCheck: 1,
  2089. }
  2090. errCodeCancel := CreatedCancelStock(cancelStockInfo)
  2091. if errCodeCancel != nil {
  2092. drugError := models.XtDrugError{
  2093. UserOrgId: orgID,
  2094. DrugId: advice.DrugId,
  2095. RecordDate: advice.AdviceDate,
  2096. PatientId: advice.PatientId,
  2097. Remark: "第二批次创建自动退库表失败",
  2098. Status: 1,
  2099. Ctime: time.Now().Unix(),
  2100. Mtime: 0,
  2101. SumCount: 0,
  2102. Prescribingnumber: advice.PrescribingNumber,
  2103. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  2104. }
  2105. CreateDrugError(drugError)
  2106. }
  2107. flow := models.DrugFlow{
  2108. WarehousingId: warehouse.ID,
  2109. DrugId: warehouse.DrugId,
  2110. Number: "",
  2111. BatchNumber: warehouse.BatchNumber,
  2112. Count: out_count - cancel_count - deliver_number,
  2113. UserOrgId: advice.UserOrgId,
  2114. PatientId: advice.PatientId,
  2115. SystemTime: advice.AdviceDate,
  2116. ConsumableType: 7,
  2117. IsSys: 0,
  2118. WarehousingOrder: "",
  2119. WarehouseOutId: 0,
  2120. WarehouseOutOrderNumber: "",
  2121. IsEdit: 0,
  2122. CancelStockId: cancelStock.ID,
  2123. CancelOrderNumber: cancelStock.OrderNumber,
  2124. Manufacturer: warehouse.Manufacturer,
  2125. Dealer: warehouse.Dealer,
  2126. Creator: advice.ExecutionStaff,
  2127. UpdateCreator: 0,
  2128. Status: 1,
  2129. Ctime: advice.AdviceDate,
  2130. Mtime: time.Now().Unix(),
  2131. Price: warehouse.RetailPrice,
  2132. WarehousingDetailId: warehouse.ID,
  2133. WarehouseOutDetailId: 0,
  2134. CancelOutDetailId: lastDrugCancelStock.ID,
  2135. ExpireDate: warehouse.ExpiryDate,
  2136. ProductDate: warehouse.ProductDate,
  2137. MaxUnit: drup.MinUnit,
  2138. MinUnit: "",
  2139. AdviceId: advice.ID,
  2140. StorehouseId: storeConfig.DrugStorehouseOut,
  2141. LastPrice: warehouse.Price,
  2142. OverCount: sum_count,
  2143. }
  2144. errCodeFlows := CreateDrugFlowOne(flow)
  2145. if errCodeFlows != nil {
  2146. drugError := models.XtDrugError{
  2147. UserOrgId: orgID,
  2148. DrugId: advice.DrugId,
  2149. RecordDate: advice.AdviceDate,
  2150. PatientId: advice.PatientId,
  2151. Remark: "第二批次创建自动退库流水表失败",
  2152. Status: 1,
  2153. Ctime: time.Now().Unix(),
  2154. Mtime: 0,
  2155. SumCount: 0,
  2156. Prescribingnumber: advice.PrescribingNumber,
  2157. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  2158. }
  2159. CreateDrugError(drugError)
  2160. }
  2161. //退库数量增加
  2162. errCodeFlows = AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  2163. if errCodeFlows != nil {
  2164. drugError := models.XtDrugError{
  2165. UserOrgId: orgID,
  2166. DrugId: advice.DrugId,
  2167. RecordDate: advice.AdviceDate,
  2168. PatientId: advice.PatientId,
  2169. Remark: "第二批次创建自动退库流水表失败",
  2170. Status: 1,
  2171. Ctime: time.Now().Unix(),
  2172. Mtime: 0,
  2173. SumCount: 0,
  2174. Prescribingnumber: advice.PrescribingNumber,
  2175. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  2176. }
  2177. CreateDrugError(drugError)
  2178. }
  2179. errCodereduce := ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  2180. if errCodereduce != nil {
  2181. drugError := models.XtDrugError{
  2182. UserOrgId: orgID,
  2183. DrugId: advice.DrugId,
  2184. RecordDate: advice.AdviceDate,
  2185. PatientId: advice.PatientId,
  2186. Remark: "第二批次减少库存表失败",
  2187. Status: 1,
  2188. Ctime: time.Now().Unix(),
  2189. Mtime: 0,
  2190. SumCount: 0,
  2191. Prescribingnumber: advice.PrescribingNumber,
  2192. PrescribingNumberUnit: advice.PrescribingNumberUnit,
  2193. }
  2194. CreateDrugError(drugError)
  2195. }
  2196. }
  2197. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2198. prescribingNumber_two_temp := deliver_number - stock_number
  2199. overPlusNumber := float64(prescribingNumber_two_temp)
  2200. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2201. advice.PrescribingNumberUnit = drup.MinUnit
  2202. BloodHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, hisprescribingNumber, unit)
  2203. }
  2204. return
  2205. }
  2206. func GetSigleDrugWarehouseOutInfoSix(patient_id int64, advice_date int64, orgid int64, advice_id int64) (*models.XtDrugWarehouseOutInfo, error) {
  2207. info := models.XtDrugWarehouseOutInfo{}
  2208. var err error
  2209. err = XTReadDB().Where("patient_id = ? and advice_date = ? and org_id = ? and status = 1 and advice_id =?", patient_id, advice_date, orgid, advice_id).Find(&info).Error
  2210. if err == gorm.ErrRecordNotFound {
  2211. return nil, err
  2212. }
  2213. if err != nil {
  2214. return nil, err
  2215. }
  2216. return &info, nil
  2217. }
  2218. func GetSigleDrugWarehouseOutInfo(patient_id int64, advice_date int64, orgid int64) (*models.XtDrugWarehouseOutInfo, error) {
  2219. info := models.XtDrugWarehouseOutInfo{}
  2220. var err error
  2221. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", patient_id, advice_date, orgid).Find(&info).Error
  2222. if err == gorm.ErrRecordNotFound {
  2223. return nil, err
  2224. }
  2225. if err != nil {
  2226. return nil, err
  2227. }
  2228. return &info, nil
  2229. }
  2230. func GetSigleDrugWarehouseOutInfoTwo(patient_id int64, advice_date int64, orgid int64, drug_id int64) (*models.XtDrugWarehouseOutInfo, error) {
  2231. info := models.XtDrugWarehouseOutInfo{}
  2232. var err error
  2233. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1 and drug_id = ?", patient_id, advice_date, orgid, drug_id).Find(&info).Error
  2234. if err == gorm.ErrRecordNotFound {
  2235. return nil, err
  2236. }
  2237. if err != nil {
  2238. return nil, err
  2239. }
  2240. return &info, nil
  2241. }
  2242. func GetSigleDrugWarehouseOutInfoOne(patient_id int64, advice_date int64, orgid int64) (models.XtDrugWarehouseOutInfo, error) {
  2243. info := models.XtDrugWarehouseOutInfo{}
  2244. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", patient_id, advice_date, orgid).Find(&info).Error
  2245. return info, err
  2246. }
  2247. func UpdateSingleDrugWarehouseOutInfoSix(patient_id int64, advice_date int64, orgid int64, info *models.DrugWarehouseOutInfo, advice_id int64) error {
  2248. err := XTWriteDB().Model(&models.XtDrugWarehouseOutInfo{}).Where("patient_id = ? and sys_record_time = ? and org_id = ? and advice_id = ? and status= 1", patient_id, advice_date, orgid, advice_id).Update(map[string]interface{}{"count": info.Count, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "number": info.Number, "batch_number": info.BatchNumber, "count_unit": info.CountUnit, "warehouse_info_id": info.WarehouseInfoId, "storehouse_id": info.StorehouseId}).Error
  2249. return err
  2250. }
  2251. func UpdateSingleDrugWarehouseOutInfo(patient_id int64, advice_date int64, orgid int64, info *models.DrugWarehouseOutInfo) error {
  2252. err := XTWriteDB().Model(&models.XtDrugWarehouseOutInfo{}).Where("patient_id = ? and advice_date = ? and org_id = ?", patient_id, advice_date, orgid).Update(map[string]interface{}{"count": info.Count, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "number": info.Number, "batch_number": info.BatchNumber, "count_unit": info.CountUnit, "warehouse_info_id": info.WarehouseInfoId, "storehouse_id": info.StorehouseId}).Error
  2253. return err
  2254. }
  2255. func FindDrugAutoReduceRecordInfo(patient_id int64, advice_date int64, orgid int64) (*models.DrugAutomaticReduceDetail, error) {
  2256. detail := models.DrugAutomaticReduceDetail{}
  2257. var err error
  2258. err = XTReadDB().Where("patient_id = ? and record_time = ? and org_id = ?", patient_id, advice_date, orgid).Find(&detail).Error
  2259. if err == gorm.ErrRecordNotFound {
  2260. return nil, err
  2261. }
  2262. if err != nil {
  2263. return nil, err
  2264. }
  2265. return &detail, nil
  2266. }
  2267. func UpdateDrugAutoReduceRecordInfo(patient_id int64, record_date int64, drug_id int64, detail *models.DrugAutomaticReduceDetail) error {
  2268. err := XTWriteDB().Model(&models.DrugAutomaticReduceDetail{}).Where("patient_id = ? and record_date = ? and drug_id = ?", patient_id, record_date, drug_id).Update(map[string]interface{}{"patient_id": detail.PatientId, "record_time": detail.RecordTime, "drug_id": detail.DrugId, "count": detail.Count, "count_unit": detail.CountUnit, "warehouse_info_id": detail.WarehouseInfoId, "advice_id": detail.AdviceId, "storehouse_id": detail.StorehouseId}).Error
  2269. return err
  2270. }
  2271. func GetNewDrugWarehouseOutInfoSix(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64, warehouse_info_id int64) (models.DrugWarehouseOutInfo, error) {
  2272. info := models.DrugWarehouseOutInfo{}
  2273. var err error
  2274. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1 and advice_id = ? and warehouse_info_id = ?", drugid, patient_id, record_date, org_id, advice_id, warehouse_info_id).Find(&info).Error
  2275. return info, err
  2276. }
  2277. func GetNewDrugWarehouseOutInfo(drugid int64, patient_id int64, record_date int64, org_id int64) (models.DrugWarehouseOutInfo, error) {
  2278. info := models.DrugWarehouseOutInfo{}
  2279. var err error
  2280. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1 ", drugid, patient_id, record_date, org_id).Find(&info).Error
  2281. return info, err
  2282. }
  2283. func UpdateSigleDrugWarehouseOutInfo(id int64, info *models.DrugWarehouseOutInfo) error {
  2284. err := XTWriteDB().Model(models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "mtime": time.Now().Unix(), "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "warehouse_info_id": info.WarehouseInfoId, "number": info.Number, "batch_number": info.BatchNumber, "advice_id": info.AdviceId, "storehouse_id": info.StorehouseId, "last_price": info.LastPrice, "over_count": info.OverCount}).Error
  2285. return err
  2286. }
  2287. func UpdateSigleDrugWarehouseOutInfoTwo(id int64, info *models.DrugWarehouseOutInfo) error {
  2288. err := XTWriteDB().Model(models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "mtime": time.Now().Unix(), "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "warehouse_info_id": info.WarehouseInfoId, "number": info.Number, "batch_number": info.BatchNumber, "advice_id": info.AdviceId, "storehouse_id": info.StorehouseId, "last_price": info.LastPrice}).Error
  2289. return err
  2290. }
  2291. func FindDrugWarehouseOutInfoCount(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  2292. err = XTReadDB().Where("drug_id = ? and patient_id = ? and system_time = ? and user_org_id = ? and consumable_type = 3 and status = 1 and advice_id = ?", drugid, patient_id, record_date, org_id, advice_id).Find(&flow).Error
  2293. return flow, err
  2294. }
  2295. func FindDrugSettleWarehouseOutInfoCount(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  2296. err = XTReadDB().Where("drug_id = ? and patient_id = ? and system_time = ? and user_org_id = ? and consumable_type = 15 and status = 1 and advice_id = ?", drugid, patient_id, record_date, org_id, advice_id).Find(&flow).Error
  2297. return flow, err
  2298. }
  2299. func FindDrugCancelOutInfo(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  2300. err = XTReadDB().Where("drug_id = ? and patient_id = ? and system_time = ? and user_org_id = ? and consumable_type = 7 and status = 1 and advice_id = ?", drugid, patient_id, record_date, org_id, advice_id).Find(&flow).Error
  2301. return flow, err
  2302. }
  2303. func FindDrugCancelStock(return_date int64, org_id int64) (*models.DrugCancelStock, error) {
  2304. stock := models.DrugCancelStock{}
  2305. var err error
  2306. err = XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_date, org_id).Find(&stock).Error
  2307. if err == gorm.ErrRecordNotFound {
  2308. return nil, err
  2309. }
  2310. if err != nil {
  2311. return nil, err
  2312. }
  2313. return &stock, nil
  2314. }
  2315. func FindLastDrugCancelStock(advice_date int64, org_id int64) (models.DrugCancelStock, error) {
  2316. cancelStock := models.DrugCancelStock{}
  2317. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", advice_date, org_id).Find(&cancelStock).Error
  2318. return cancelStock, err
  2319. }
  2320. func GetAllSumDrugWarehouseOut(patient_id int64, user_org_id int64, advice_date int64) (advice []*models.HisDoctorAdvice, err error) {
  2321. err = XTReadDB().Where("patient_id = ? and user_org_id = ? and advice_date = ? and status = 1", patient_id, user_org_id, advice_date).Find(&advice).Error
  2322. return advice, err
  2323. }
  2324. func UpdateDrugWarehouseOutByPatientId(patient_id int64, advice_date int64, orgid int64, info models.DrugWarehouseOutInfo) error {
  2325. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", patient_id, advice_date, orgid).Update(map[string]interface{}{"count": info.Count, "count_unit": info.CountUnit}).Error
  2326. return err
  2327. }
  2328. func FindeGoodInfo(orgid int64, id int64) (models.GoodInformation, error) {
  2329. information := models.GoodInformation{}
  2330. err := XTReadDB().Where("org_id = ? and id = ? and status = 1", orgid, id).Find(&information).Error
  2331. return information, err
  2332. }
  2333. func UpdateHisPrescriptionProjectStatus(project_id int64, record_time int64, user_org_id int64, patient_id int64) (models.HisPrescriptionProject, error) {
  2334. project := models.HisPrescriptionProject{}
  2335. err := XTWriteDB().Model(&project).Where("project_id = ? and record_date = ? and user_org_id = ? and patient_id = ? and status = 1", project_id, record_time, user_org_id, patient_id).Update(map[string]interface{}{"is_out": 1}).Error
  2336. return project, err
  2337. }
  2338. // 药品自动出库 递归方式 deliver_number_one已经转成最小数量
  2339. func HisSettleDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo, deliver_number_one int64) (err error) {
  2340. fmt.Println("deliver_number_one -----------------------------------------", deliver_number_one)
  2341. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  2342. var deliver_number int64 = 0
  2343. //var deliver_number_one int64 = 0
  2344. var stock_number int64 = 0
  2345. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  2346. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  2347. //prescribingCount_temp := strconv.FormatFloat(math.Abs(prescribingCount), 'f', 0, 64)
  2348. //prescribe_count, _ := strconv.ParseInt(prescribingCount_temp, 10, 64)
  2349. var drug_price float64
  2350. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2351. deliver_number = count * drup.MinNumber
  2352. } else {
  2353. deliver_number = count
  2354. }
  2355. //if advice.PrescribingNumberUnit == drup.MaxUnit {
  2356. // deliver_number_one = prescribe_count * drup.MinNumber
  2357. //
  2358. //} else {
  2359. // deliver_number_one = prescribe_count
  2360. //
  2361. //}
  2362. if advice.PrescribingNumberUnit == drup.MaxUnit {
  2363. drug_price = drup.RetailPrice
  2364. }
  2365. fmt.Println(drug_price)
  2366. if advice.PrescribingNumberUnit == drup.MinUnit {
  2367. drug_price = drup.MinPrice
  2368. }
  2369. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  2370. drug_price = drup.RetailPrice
  2371. }
  2372. // 根据先进先出原则,查询最先入库的批次,进行出库
  2373. // 如果没有对应的库存,则报错
  2374. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  2375. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2376. if lastWarehouse.StockMinNumber >= drup.MinNumber && lastWarehouse.MaxUnit != lastWarehouse.MinUnit {
  2377. var stockMax int64
  2378. var stockMin int64
  2379. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  2380. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  2381. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  2382. UpdateMinNumber(lastWarehouse.ID, stockMin)
  2383. }
  2384. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  2385. if err != nil {
  2386. return err
  2387. }
  2388. // 将该批次的剩余库存数量转换为拆零数量
  2389. // 将该批次的剩余库存数量转换为拆零数量
  2390. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  2391. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2392. }
  2393. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  2394. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  2395. }
  2396. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  2397. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  2398. }
  2399. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  2400. fmt.Println("剩余库存2332233223232323", stock_number)
  2401. fmt.Println("出库数量233232332323232", deliver_number)
  2402. if stock_number >= deliver_number {
  2403. var maxNumber int64 = 0
  2404. var minNumber int64 = 0
  2405. maxNumber = deliver_number / drup.MinNumber
  2406. minNumber = deliver_number % drup.MinNumber
  2407. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  2408. minNumber = maxNumber
  2409. }
  2410. if drup.MaxUnit != drup.MinUnit {
  2411. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  2412. return errors.New("库存数量不足")
  2413. }
  2414. }
  2415. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  2416. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  2417. }
  2418. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  2419. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2420. }
  2421. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  2422. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  2423. }
  2424. if warehouse.StockMaxNumber < 0 {
  2425. warehouse.StockMaxNumber = 0
  2426. }
  2427. if warehouse.StockMinNumber < 0 {
  2428. warehouse.StockMinNumber = 0
  2429. }
  2430. warehouse.Mtime = time.Now().Unix()
  2431. //fmt.Println("minnU目标而332322323323233223323223我", minNumber)
  2432. //fmt.Println("max_number232232332232322wo", maxNumber)
  2433. //fmt.Println(" warehouse.StockMinNumber", warehouse.StockMinNumber)
  2434. if warehouse.MaxUnit != warehouse.MinUnit {
  2435. if warehouse.StockMinNumber < minNumber {
  2436. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2437. if warehouse.MaxUnit != warehouse.MinUnit {
  2438. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2439. }
  2440. } else {
  2441. if minNumber > 0 {
  2442. if (warehouse.StockMinNumber - minNumber) >= 0 {
  2443. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2444. }
  2445. }
  2446. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  2447. if warehouse.StockMinNumber > 0 {
  2448. if warehouse.StockMinNumber-deliver_number >= 0 {
  2449. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2450. }
  2451. }
  2452. }
  2453. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2454. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  2455. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2456. }
  2457. }
  2458. }
  2459. }
  2460. if warehouse.MinUnit == warehouse.MaxUnit {
  2461. //fmt.Println("minNUMBER233223233223322323",minNumber)
  2462. //fmt.Println("warehouse.StockMinNumber",warehouse.StockMinNumber)
  2463. //fmt.Println("warehouse.StockMaxNumber",warehouse.StockMaxNumber)
  2464. if warehouse.StockMaxNumber > 0 && warehouse.StockMinNumber == 0 && warehouse.StockMaxNumber >= minNumber {
  2465. warehouse.StockMaxNumber = warehouse.StockMaxNumber - minNumber
  2466. }
  2467. if warehouse.StockMaxNumber == 0 && warehouse.StockMinNumber > 0 && warehouse.StockMinNumber >= minNumber {
  2468. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2469. }
  2470. if warehouse.StockMaxNumber > 0 && warehouse.StockMinNumber > 0 && (warehouse.StockMaxNumber+warehouse.StockMinNumber) > minNumber {
  2471. var out_false int64 = 0
  2472. if warehouse.StockMinNumber >= minNumber {
  2473. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2474. out_false = 1
  2475. }
  2476. fmt.Println("out_false233223233223wo", out_false)
  2477. if warehouse.StockMinNumber < minNumber && out_false == 0 {
  2478. var over_total int64
  2479. over_total = warehouse.StockMinNumber
  2480. warehouse.StockMinNumber = 0
  2481. warehouse.StockMaxNumber = warehouse.StockMaxNumber - (minNumber - over_total)
  2482. }
  2483. }
  2484. //if warehouse.StockMaxNumber < minNumber && warehouse.StockMaxNumber < minNumber && (warehouse.StockMaxNumber + warehouse.StockMinNumber) > minNumber {
  2485. // var over_count int64
  2486. // over_count = warehouse.StockMinNumber
  2487. // warehouse.StockMinNumber = 0
  2488. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - (minNumber -over_count)
  2489. //
  2490. //}
  2491. }
  2492. if drup.MaxUnit != drup.MinUnit {
  2493. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2494. return errors.New("库存数量不足")
  2495. }
  2496. }
  2497. if warehouse.StockMinNumber <= 0 {
  2498. warehouse.StockMinNumber = 0
  2499. }
  2500. //扣减库存数据
  2501. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2502. if errThree != nil {
  2503. return errThree
  2504. }
  2505. //查询剩余库存
  2506. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  2507. var sum_count int64
  2508. for _, its := range stockInfo {
  2509. if its.MaxUnit == drup.MaxUnit {
  2510. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  2511. }
  2512. sum_count += its.StockMaxNumber + its.StockMinNumber
  2513. }
  2514. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2515. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2516. WarehouseOutId: warehouseout.ID,
  2517. Status: 1,
  2518. Ctime: time.Now().Unix(),
  2519. Remark: warehouse.Remark,
  2520. OrgId: orgID,
  2521. Type: 5,
  2522. Manufacturer: warehouse.Manufacturer,
  2523. Dealer: warehouse.Dealer,
  2524. IsSys: 1,
  2525. SysRecordTime: advice.RecordDate,
  2526. DrugId: advice.DrugId,
  2527. Number: warehouse.Number,
  2528. BatchNumber: warehouse.BatchNumber,
  2529. Price: warehouse.RetailPrice,
  2530. CountUnit: drup.MinUnit,
  2531. RetailPrice: warehouse.RetailPrice,
  2532. ProductDate: warehouse.ProductDate,
  2533. ExpiryDate: warehouse.ExpiryDate,
  2534. PatientId: advice.PatientId,
  2535. Count: deliver_number, //出最小单位
  2536. WarehouseInfoId: warehouse.ID,
  2537. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2538. StorehouseId: storeConfig.DrugStorehouseOut,
  2539. IsCheck: 1,
  2540. OverCount: sum_count,
  2541. AdviceId: advice.ID,
  2542. OrderId: warehouseout.OrderId,
  2543. }
  2544. //查询今日该药品该患者是否有出库数据
  2545. lastDrugOutInfo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  2546. fmt.Println("lastDrugOutInfo0000000293293292392392393293239239", lastDrugOutInfo)
  2547. if lastDrugOutInfo.ID == 0 {
  2548. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2549. if errOne != nil {
  2550. return errOne
  2551. }
  2552. }
  2553. if lastDrugOutInfo.ID > 0 {
  2554. var his_count int64
  2555. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  2556. his_count = lastDrugOutInfo.Count * drup.MinNumber
  2557. }
  2558. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  2559. his_count = lastDrugOutInfo.Count
  2560. }
  2561. fmt.Println("deliver_numberwode8888888823832832832832832832823823-------------------------", deliver_number)
  2562. fmt.Println("his_countnide-------------------------------------------------------", his_count)
  2563. if deliver_number != his_count {
  2564. //warehouseOutInfo.Count = deliver_number_one
  2565. errThree := UpdateSigleDrugWarehouseOutInfo(lastDrugOutInfo.ID, warehouseOutInfo)
  2566. if errThree != nil {
  2567. return errThree
  2568. }
  2569. }
  2570. }
  2571. var out_count int64
  2572. var cancel_count int64
  2573. //查询出库数据
  2574. infoCountList, _ := FindDrugSettleWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2575. //查询退库数据
  2576. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2577. for _, item := range infoCountList {
  2578. out_count += item.Count
  2579. }
  2580. for _, item := range cancelInfoList {
  2581. cancel_count += item.Count
  2582. }
  2583. fmt.Println("deliver_number_one", deliver_number_one)
  2584. fmt.Println("out_count", out_count)
  2585. fmt.Println("cancel_count", cancel_count)
  2586. //如果本次出库数据大于历史出库数据 新增1条流水
  2587. if deliver_number_one > (out_count - cancel_count) {
  2588. drugflow := models.DrugFlow{
  2589. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2590. WarehouseOutId: warehouseout.ID,
  2591. DrugId: advice.DrugId,
  2592. Number: warehouse.Number,
  2593. ProductDate: warehouse.ProductDate,
  2594. ExpireDate: warehouse.ExpiryDate,
  2595. Count: deliver_number_one - (out_count - cancel_count), //按最小单位计算,
  2596. Price: warehouse.RetailPrice,
  2597. Status: 1,
  2598. Ctime: time.Now().Unix(),
  2599. UserOrgId: orgID,
  2600. Manufacturer: warehouse.Manufacturer,
  2601. Dealer: warehouse.Dealer,
  2602. BatchNumber: warehouse.BatchNumber,
  2603. MaxUnit: drup.MinUnit,
  2604. ConsumableType: 15,
  2605. IsEdit: 1,
  2606. Creator: advice.ExecutionStaff,
  2607. IsSys: 1,
  2608. PatientId: advice.PatientId,
  2609. SystemTime: advice.AdviceDate,
  2610. WarehousingDetailId: warehouse.ID,
  2611. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2612. StorehouseId: storeConfig.DrugStorehouseOut,
  2613. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2614. OverCount: sum_count,
  2615. AdviceId: advice.ID,
  2616. LastPrice: warehouse.Price,
  2617. OrderId: warehouseout.OrderId,
  2618. }
  2619. CreateDrugFlowOne(drugflow)
  2620. //出库数量相加
  2621. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2622. }
  2623. if deliver_number == (out_count - cancel_count) {
  2624. drugflow := models.DrugFlow{
  2625. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2626. WarehouseOutId: warehouseout.ID,
  2627. DrugId: advice.DrugId,
  2628. Number: warehouse.Number,
  2629. ProductDate: warehouse.ProductDate,
  2630. ExpireDate: warehouse.ExpiryDate,
  2631. Count: deliver_number, //按最小单位计算,
  2632. Price: warehouse.RetailPrice,
  2633. Status: 1,
  2634. Ctime: time.Now().Unix(),
  2635. UserOrgId: orgID,
  2636. Manufacturer: warehouse.Manufacturer,
  2637. Dealer: warehouse.Dealer,
  2638. BatchNumber: warehouse.BatchNumber,
  2639. MaxUnit: drup.MinUnit,
  2640. ConsumableType: 15,
  2641. IsEdit: 1,
  2642. Creator: advice.ExecutionStaff,
  2643. IsSys: 1,
  2644. PatientId: advice.PatientId,
  2645. SystemTime: advice.AdviceDate,
  2646. WarehousingDetailId: warehouse.ID,
  2647. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2648. StorehouseId: storeConfig.DrugStorehouseOut,
  2649. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2650. OverCount: sum_count,
  2651. AdviceId: advice.ID,
  2652. LastPrice: warehouse.Price,
  2653. OrderId: warehouseout.OrderId,
  2654. }
  2655. CreateDrugFlowOne(drugflow)
  2656. //出库数量相加
  2657. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2658. }
  2659. fmt.Println("出库数据", deliver_number_one)
  2660. fmt.Println("马文强out_count233232232ooqw", out_count)
  2661. fmt.Println("马cancel_count33232232ooqw", cancel_count)
  2662. //如果本次出库数据小于历史出库数据 新增1条退库流水
  2663. if deliver_number_one < (out_count - cancel_count) {
  2664. operation_time := time.Now().Unix()
  2665. ctime := time.Now().Unix()
  2666. timeStr := time.Now().Format("2006-01-02")
  2667. timeArr := strings.Split(timeStr, "-")
  2668. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  2669. total = total + 1
  2670. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  2671. cancelStock := models.DrugCancelStock{
  2672. OrderNumber: orderNumber,
  2673. OperaTime: operation_time,
  2674. OrgId: advice.UserOrgId,
  2675. Creater: advice.ExecutionStaff,
  2676. Ctime: ctime,
  2677. Status: 1,
  2678. ReturnTime: advice.AdviceDate,
  2679. Dealer: 0,
  2680. Manufacturer: 0,
  2681. Type: 5,
  2682. StorehouseId: storeConfig.DrugStorehouseOut,
  2683. IsCheck: 1,
  2684. OrderId: warehouseout.OrderId,
  2685. }
  2686. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2687. if errCode == gorm.ErrRecordNotFound {
  2688. AddSigleDrugCancelStock(&cancelStock)
  2689. }
  2690. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2691. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  2692. dealer, _ := GetDealerById(warehouse.Dealer)
  2693. //cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  2694. cancelStockInfo := models.DrugCancelStockInfo{
  2695. OrderNumber: lastDrugCancelStock.OrderNumber,
  2696. CancelStockId: lastDrugCancelStock.ID,
  2697. DrugId: advice.DrugId,
  2698. Count: (out_count - cancel_count) - deliver_number,
  2699. Status: 1,
  2700. Ctime: ctime,
  2701. OrgId: advice.UserOrgId,
  2702. Type: 5,
  2703. Manufacturer: manufactureName.ManufacturerName,
  2704. Dealer: dealer.DealerName,
  2705. Total: 0,
  2706. RetailPrice: warehouse.RetailPrice,
  2707. RetailTotalPrice: warehouse.RetailTotalPrice,
  2708. Price: warehouse.Price,
  2709. RegisterAccount: "",
  2710. Remark: warehouse.Remark,
  2711. BatchNumber: warehouse.BatchNumber,
  2712. MaxUnit: drup.MinUnit,
  2713. ProductDate: warehouse.ProductDate,
  2714. ExpiryDate: warehouse.ExpiryDate,
  2715. BatchNumberId: warehouse.ID,
  2716. StorehouseId: storeConfig.DrugStorehouseOut,
  2717. IsCheck: 1,
  2718. OrderId: warehouseout.OrderId,
  2719. }
  2720. CreatedCancelStock(cancelStockInfo)
  2721. flow := models.DrugFlow{
  2722. WarehousingId: warehouse.ID,
  2723. DrugId: warehouse.DrugId,
  2724. Number: "",
  2725. BatchNumber: warehouse.BatchNumber,
  2726. Count: out_count - cancel_count - deliver_number,
  2727. UserOrgId: advice.UserOrgId,
  2728. PatientId: advice.PatientId,
  2729. SystemTime: advice.AdviceDate,
  2730. ConsumableType: 7,
  2731. IsSys: 0,
  2732. WarehousingOrder: "",
  2733. WarehouseOutId: 0,
  2734. WarehouseOutOrderNumber: "",
  2735. IsEdit: 0,
  2736. CancelStockId: cancelStock.ID,
  2737. CancelOrderNumber: lastDrugCancelStock.OrderNumber,
  2738. Manufacturer: warehouse.Manufacturer,
  2739. Dealer: warehouse.Dealer,
  2740. Creator: advice.ExecutionStaff,
  2741. UpdateCreator: 0,
  2742. Status: 1,
  2743. Ctime: time.Now().Unix(),
  2744. Mtime: 0,
  2745. Price: warehouse.RetailPrice,
  2746. WarehousingDetailId: warehouse.ID,
  2747. WarehouseOutDetailId: 0,
  2748. CancelOutDetailId: lastDrugCancelStock.ID,
  2749. ExpireDate: warehouse.ExpiryDate,
  2750. ProductDate: warehouse.ProductDate,
  2751. MaxUnit: drup.MinUnit,
  2752. MinUnit: "",
  2753. AdviceId: advice.ID,
  2754. StorehouseId: storeConfig.DrugStorehouseOut,
  2755. LastPrice: warehouse.Price,
  2756. OverCount: sum_count,
  2757. OrderId: warehouseout.OrderId,
  2758. }
  2759. CreateDrugFlowOne(flow)
  2760. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  2761. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  2762. }
  2763. //查询是否存在数据
  2764. details := &models.DrugAutomaticReduceDetail{
  2765. WarehouseOutId: warehouseout.ID,
  2766. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2767. PatientId: advice.PatientId,
  2768. Ctime: time.Now().Unix(),
  2769. Mtime: time.Now().Unix(),
  2770. Status: 1,
  2771. RecordTime: advice.RecordDate,
  2772. OrgId: orgID,
  2773. DrugId: advice.DrugId,
  2774. Count: deliver_number,
  2775. CountUnit: drup.MinUnit,
  2776. WarehouseInfoId: warehouse.ID,
  2777. StorehouseId: storeConfig.DrugStorehouseOut,
  2778. AdviceId: advice.ID,
  2779. }
  2780. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2781. if errTwo != nil {
  2782. return errTwo
  2783. }
  2784. return nil
  2785. } else {
  2786. info := models.XtDrugWarehouseInfo{
  2787. ID: warehouse.ID,
  2788. WarehousingId: warehouse.WarehousingId,
  2789. DrugId: warehouse.DrugId,
  2790. Number: warehouse.Number,
  2791. ProductDate: warehouse.ProductDate,
  2792. ExpiryDate: warehouse.ExpiryDate,
  2793. WarehousingCount: warehouse.WarehousingCount,
  2794. Price: warehouse.RetailPrice,
  2795. TotalPrice: warehouse.TotalPrice,
  2796. Dealer: warehouse.Dealer,
  2797. Manufacturer: warehouse.Manufacturer,
  2798. Remark: warehouse.Remark,
  2799. Ctime: warehouse.Ctime,
  2800. Mtime: warehouse.Mtime,
  2801. Status: 1,
  2802. OrgId: warehouse.OrgId,
  2803. IsReturn: warehouse.IsReturn,
  2804. WarehousingOrder: warehouse.WarehousingOrder,
  2805. Type: warehouse.Type,
  2806. RetailPrice: warehouse.RetailPrice,
  2807. RetailTotalPrice: warehouse.RetailPrice,
  2808. StockMaxNumber: 0,
  2809. StockMinNumber: 0,
  2810. BatchNumber: warehouse.BatchNumber,
  2811. MaxUnit: warehouse.MaxUnit,
  2812. WarehousingInfoId: warehouse.WarehousingInfoId,
  2813. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2814. StorehouseId: storeConfig.DrugStorehouseOut,
  2815. IsCheck: 1,
  2816. }
  2817. //扣减库存
  2818. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2819. if errThree != nil {
  2820. return errThree
  2821. }
  2822. //查询剩余库存
  2823. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  2824. var sum_count int64
  2825. for _, its := range stockInfo {
  2826. if its.MaxUnit == drup.MaxUnit {
  2827. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  2828. }
  2829. sum_count += its.StockMaxNumber + its.StockMinNumber
  2830. }
  2831. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2832. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2833. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2834. WarehouseOutId: warehouseout.ID,
  2835. Status: 1,
  2836. Ctime: time.Now().Unix(),
  2837. Remark: warehouse.Remark,
  2838. OrgId: orgID,
  2839. Type: 5,
  2840. Manufacturer: warehouse.Manufacturer,
  2841. Dealer: warehouse.Dealer,
  2842. IsSys: 1,
  2843. SysRecordTime: advice.RecordDate,
  2844. DrugId: advice.DrugId,
  2845. Number: warehouse.Number,
  2846. BatchNumber: warehouse.BatchNumber,
  2847. Price: warehouse.RetailPrice,
  2848. CountUnit: drup.MinUnit,
  2849. ProductDate: warehouse.ProductDate,
  2850. ExpiryDate: warehouse.ExpiryDate,
  2851. PatientId: advice.PatientId,
  2852. Count: stock_number,
  2853. WarehouseInfoId: warehouse.ID,
  2854. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2855. StorehouseId: storeConfig.DrugStorehouseOut,
  2856. IsCheck: 1,
  2857. OverCount: sum_count,
  2858. AdviceId: advice.ID,
  2859. OrderId: warehouseout.OrderId,
  2860. }
  2861. //查询是否存在出库数据
  2862. lastDrugOutInfoTwo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  2863. //_, errCode := GetSigleDrugWarehouseOutInfo(advice.PatientId, advice.AdviceDate, orgID)
  2864. //fmt.Println("errcoedee232233223233232323232323233223232323322323232323232323",errCode)
  2865. if lastDrugOutInfoTwo.ID == 0 {
  2866. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2867. if errOne != nil {
  2868. return errOne
  2869. }
  2870. }
  2871. if lastDrugOutInfoTwo.ID > 0 {
  2872. lastDrugOutInfo, _ := GetSigleDrugWarehouseOutInfoOne(advice.PatientId, advice.AdviceDate, orgID)
  2873. fmt.Println("中国人民肌肤32323223332332232322323喔", lastDrugOutInfo)
  2874. var his_count int64
  2875. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  2876. his_count = lastDrugOutInfo.Count * drup.MinNumber
  2877. }
  2878. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  2879. his_count = lastDrugOutInfo.Count
  2880. }
  2881. fmt.Println("deliver_number3232323232wowooww", deliver_number)
  2882. fmt.Println("his_count99999992i2222wo", his_count)
  2883. if deliver_number != his_count {
  2884. errThree := UpdateSingleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo, advice.ID)
  2885. if errThree != nil {
  2886. return errThree
  2887. }
  2888. }
  2889. }
  2890. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  2891. details := &models.DrugAutomaticReduceDetail{
  2892. WarehouseOutId: warehouseout.ID,
  2893. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  2894. PatientId: advice.PatientId,
  2895. Ctime: time.Now().Unix(),
  2896. Mtime: time.Now().Unix(),
  2897. Status: 1,
  2898. RecordTime: advice.RecordDate,
  2899. OrgId: orgID,
  2900. DrugId: advice.DrugId,
  2901. Count: stock_number,
  2902. CountUnit: drup.MinUnit,
  2903. WarehouseInfoId: warehouse.ID,
  2904. StorehouseId: storeConfig.DrugStorehouseOut,
  2905. AdviceId: advice.ID,
  2906. }
  2907. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  2908. if errcodeThree == gorm.ErrRecordNotFound {
  2909. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2910. if errTwo != nil {
  2911. return errTwo
  2912. }
  2913. }
  2914. if errcodeThree == nil {
  2915. UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  2916. }
  2917. var out_count int64
  2918. var cancel_count int64
  2919. //查询出库数据
  2920. infoCountList, _ := FindDrugSettleWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2921. //查询退库数据
  2922. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2923. for _, item := range infoCountList {
  2924. out_count += item.Count
  2925. }
  2926. for _, item := range cancelInfoList {
  2927. cancel_count += item.Count
  2928. }
  2929. fmt.Println("出库数量", deliver_number)
  2930. fmt.Println("out_coutn233232322323wo", cancel_count)
  2931. fmt.Println("out_count99988233232", out_count)
  2932. //如果本次出库数据大于历史出库数据 新增1条流水
  2933. if deliver_number > (out_count - cancel_count) {
  2934. drugflow := models.DrugFlow{
  2935. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2936. WarehouseOutId: warehouseout.ID,
  2937. DrugId: advice.DrugId,
  2938. Number: warehouse.Number,
  2939. ProductDate: warehouse.ProductDate,
  2940. ExpireDate: warehouse.ExpiryDate,
  2941. Count: stock_number,
  2942. Price: warehouse.RetailPrice,
  2943. Status: 1,
  2944. Ctime: time.Now().Unix(),
  2945. UserOrgId: orgID,
  2946. Manufacturer: warehouse.Manufacturer,
  2947. Dealer: warehouse.Dealer,
  2948. BatchNumber: warehouse.BatchNumber,
  2949. MaxUnit: drup.MinUnit,
  2950. ConsumableType: 15,
  2951. IsEdit: 1,
  2952. Creator: advice.ExecutionStaff,
  2953. IsSys: 1,
  2954. PatientId: advice.PatientId,
  2955. WarehousingDetailId: warehouse.ID,
  2956. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2957. StorehouseId: storeConfig.DrugStorehouseOut,
  2958. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2959. OverCount: sum_count,
  2960. AdviceId: advice.ID,
  2961. LastPrice: warehouse.Price,
  2962. SystemTime: advice.AdviceDate,
  2963. OrderId: warehouseout.OrderId,
  2964. }
  2965. CreateDrugFlowOne(drugflow)
  2966. //出库数量相加
  2967. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2968. }
  2969. if deliver_number < (out_count - cancel_count) {
  2970. fmt.Println("即那里3232232323")
  2971. operation_time := time.Now().Unix()
  2972. ctime := time.Now().Unix()
  2973. timeStr := time.Now().Format("2006-01-02")
  2974. timeArr := strings.Split(timeStr, "-")
  2975. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  2976. total = total + 1
  2977. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  2978. cancelStock := models.DrugCancelStock{
  2979. OrderNumber: orderNumber,
  2980. OperaTime: operation_time,
  2981. OrgId: advice.UserOrgId,
  2982. Creater: advice.ExecutionStaff,
  2983. Ctime: ctime,
  2984. Status: 1,
  2985. ReturnTime: advice.AdviceDate,
  2986. Dealer: 0,
  2987. Manufacturer: 0,
  2988. Type: 5,
  2989. StorehouseId: storeConfig.DrugStorehouseOut,
  2990. IsCheck: 1,
  2991. OrderId: warehouseout.OrderId,
  2992. }
  2993. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2994. if errCode == gorm.ErrRecordNotFound {
  2995. AddSigleDrugCancelStock(&cancelStock)
  2996. }
  2997. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2998. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  2999. dealer, _ := GetDealerById(warehouse.Dealer)
  3000. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  3001. cancelStockInfo := models.DrugCancelStockInfo{
  3002. OrderNumber: cancelInfo.OrderNumber,
  3003. CancelStockId: lastDrugCancelStock.ID,
  3004. DrugId: advice.DrugId,
  3005. Count: (out_count - cancel_count) - deliver_number,
  3006. Status: 1,
  3007. Ctime: ctime,
  3008. OrgId: advice.UserOrgId,
  3009. Type: 5,
  3010. Manufacturer: manufactureName.ManufacturerName,
  3011. Dealer: dealer.DealerName,
  3012. Total: 0,
  3013. RetailPrice: warehouse.RetailPrice,
  3014. RetailTotalPrice: warehouse.RetailTotalPrice,
  3015. Price: warehouse.Price,
  3016. RegisterAccount: "",
  3017. Remark: warehouse.Remark,
  3018. BatchNumber: warehouse.BatchNumber,
  3019. MaxUnit: drup.MinUnit,
  3020. ProductDate: warehouse.ProductDate,
  3021. ExpiryDate: warehouse.ExpiryDate,
  3022. BatchNumberId: warehouse.ID,
  3023. StorehouseId: storeConfig.DrugStorehouseOut,
  3024. IsCheck: 1,
  3025. OrderId: warehouseout.OrderId,
  3026. }
  3027. CreatedCancelStock(cancelStockInfo)
  3028. flow := models.DrugFlow{
  3029. WarehousingId: warehouse.ID,
  3030. DrugId: warehouse.DrugId,
  3031. Number: "",
  3032. BatchNumber: warehouse.BatchNumber,
  3033. Count: out_count - cancel_count - deliver_number,
  3034. UserOrgId: advice.UserOrgId,
  3035. PatientId: advice.PatientId,
  3036. SystemTime: advice.AdviceDate,
  3037. ConsumableType: 7,
  3038. IsSys: 0,
  3039. WarehousingOrder: "",
  3040. WarehouseOutId: 0,
  3041. WarehouseOutOrderNumber: "",
  3042. IsEdit: 0,
  3043. CancelStockId: cancelStock.ID,
  3044. CancelOrderNumber: lastDrugCancelStock.OrderNumber,
  3045. Manufacturer: warehouse.Manufacturer,
  3046. Dealer: warehouse.Dealer,
  3047. Creator: advice.ExecutionStaff,
  3048. UpdateCreator: 0,
  3049. Status: 1,
  3050. Ctime: time.Now().Unix(),
  3051. Mtime: 0,
  3052. Price: warehouse.RetailPrice,
  3053. WarehousingDetailId: warehouse.ID,
  3054. WarehouseOutDetailId: 0,
  3055. CancelOutDetailId: lastDrugCancelStock.ID,
  3056. ExpireDate: warehouse.ExpiryDate,
  3057. ProductDate: warehouse.ProductDate,
  3058. MaxUnit: drup.MinUnit,
  3059. MinUnit: "",
  3060. AdviceId: advice.ID,
  3061. StorehouseId: storeConfig.DrugStorehouseOut,
  3062. LastPrice: warehouse.Price,
  3063. OverCount: sum_count,
  3064. OrderId: warehouseout.OrderId,
  3065. }
  3066. CreateDrugFlowOne(flow)
  3067. //退库数量增加
  3068. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  3069. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  3070. }
  3071. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3072. prescribingNumber_two_temp := deliver_number - stock_number
  3073. overPlusNumber := float64(prescribingNumber_two_temp)
  3074. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  3075. advice.PrescribingNumberUnit = drup.MinUnit
  3076. fmt.Println("剩余库存9999999233232", overPlusNumber)
  3077. HisSettleDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, deliver_number_one)
  3078. }
  3079. return
  3080. }
  3081. // 耗材出库
  3082. func ConsumablesGoodDeliveryFivety(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  3083. //开事务
  3084. tx := XTWriteDB().Begin()
  3085. defer func() {
  3086. if err != nil {
  3087. utils.ErrorLog("事务失败,原因为: %v", err)
  3088. tx.Rollback()
  3089. } else {
  3090. tx.Commit()
  3091. }
  3092. }()
  3093. var deliver_number int64 = 0
  3094. var stock_number int64 = 0
  3095. var maxNumber int64 = 0
  3096. deliver_number = goods.Count
  3097. // 根据先进先出原则,查询最先入库的批次,进行出库
  3098. // 如果没有对应的库存,则报错
  3099. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  3100. if err != nil {
  3101. return errors.New("库存数量不足")
  3102. }
  3103. stock_number = warehouse.StockCount
  3104. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3105. if stock_number >= deliver_number {
  3106. maxNumber = goods.Count
  3107. //出库
  3108. warehouse.StockCount = warehouse.StockCount - maxNumber
  3109. warehouse.Mtime = time.Now().Unix()
  3110. if warehouse.StockCount < 0 {
  3111. return errors.New("库存数量不足")
  3112. }
  3113. var info models.WarehousingInfo
  3114. err := tx.Model(&info).Where("id = ?", warehouse.ID).Updates(map[string]interface{}{"stock_count": warehouse.StockCount}).Error
  3115. if err != nil {
  3116. return err
  3117. }
  3118. //更新his_prescripton_project出库状态值
  3119. project := models.HisPrescriptionProject{}
  3120. err = tx.Model(&project).Where("project_id = ? and record_date = ? and user_org_id = ? and patient_id = ? and status = 1", goods.GoodId, record_time, orgID, patient_id).Update(map[string]interface{}{"is_out": 1}).Error
  3121. if err != nil {
  3122. return err
  3123. }
  3124. //查询剩余库存
  3125. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  3126. var sum_count int64
  3127. for _, item := range goodList {
  3128. sum_count += item.StockCount
  3129. }
  3130. warehouseOutInfo := &models.WarehouseOutInfo{
  3131. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3132. WarehouseOutId: warehouseOut.ID,
  3133. WarehouseInfotId: warehouse.ID,
  3134. Status: 1,
  3135. Ctime: time.Now().Unix(),
  3136. Remark: warehouse.Remark,
  3137. OrgId: orgID,
  3138. Type: 1,
  3139. Manufacturer: warehouse.Manufacturer,
  3140. Dealer: warehouse.Dealer,
  3141. IsSys: 1,
  3142. SysRecordTime: record_time,
  3143. GoodTypeId: goods.GoodTypeId,
  3144. GoodId: goods.GoodId,
  3145. PatientId: patient_id,
  3146. Number: warehouse.Number,
  3147. LicenseNumber: warehouse.LicenseNumber,
  3148. Price: warehouse.PackingPrice,
  3149. ExpiryDate: warehouse.ExpiryDate,
  3150. ProductDate: warehouse.ProductDate,
  3151. ProjectId: goods.ProjectId,
  3152. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3153. StorehouseId: goods.StorehouseId,
  3154. IsCheck: 1,
  3155. OverCount: sum_count,
  3156. RegisterNumber: warehouse.RegisterNumber,
  3157. }
  3158. warehouseOutInfo.Count = count
  3159. if orgID == 9671 || orgID == 10265 {
  3160. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  3161. warehouseOutInfo.Price = goodsInfo.PackingPrice
  3162. }
  3163. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  3164. if errcodes == gorm.ErrRecordNotFound {
  3165. err := tx.Create(&warehouseOutInfo).Error
  3166. if err != nil {
  3167. return err
  3168. }
  3169. } else if errcodes == nil {
  3170. outInfoOne, _ := GetWarehouseOutInfoIsExistTwo(goods.GoodId, patient_id, record_time, goods.ProjectId)
  3171. if count != outInfoOne.Count {
  3172. outInfo := models.WarehouseOutInfo{}
  3173. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", goods.GoodId, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": warehouseOutInfo.WarehouseOutId, "WarehouseOutOrderNumber": warehouseOutInfo.WarehouseOutOrderNumber, "sys_record_time": warehouseOutInfo.SysRecordTime, "good_type_id": warehouseOutInfo.GoodTypeId, "patient_id": warehouseOutInfo.PatientId, "consumable_type": warehouseOutInfo.ConsumableType, "count": warehouseOutInfo.Count, "price": warehouseOutInfo.Price, "dealer": warehouseOutInfo.Dealer, "manufacturer": warehouseOutInfo.Manufacturer, "number": warehouseOutInfo.Number, "license_number": warehouseOutInfo.LicenseNumber, "over_count": warehouseOutInfo.OverCount}).Error
  3174. if err != nil {
  3175. return err
  3176. }
  3177. }
  3178. }
  3179. lastOut, _ := FindWarehouseOutInfoByPatientId(patient_id, record_time, goods.GoodId, orgID)
  3180. //查询已经出库的数据
  3181. flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  3182. var out_count int64
  3183. var out_count_one int64
  3184. for _, item := range flowGood {
  3185. out_count += item.Count
  3186. }
  3187. //查询退库数据
  3188. flowGoodTwo, _ := GetStockFlowIsBatchNumberThree(patient_id, record_time, goods.GoodId)
  3189. for _, item := range flowGoodTwo {
  3190. out_count_one += item.Count
  3191. }
  3192. //如果本次出库数据大于历史出库数据 新增1条流水
  3193. if count > (out_count - out_count_one) {
  3194. flow := models.VmStockFlow{
  3195. WarehouseOutId: warehouseOut.ID,
  3196. WarehousingId: warehouse.ID,
  3197. GoodId: goods.GoodId,
  3198. Number: warehouse.Number,
  3199. ProductDate: warehouse.ProductDate,
  3200. ExpireDate: warehouse.ExpiryDate,
  3201. Count: count - out_count + out_count_one,
  3202. Price: warehouse.PackingPrice,
  3203. Status: 1,
  3204. Ctime: time.Now().Unix(),
  3205. UserOrgId: orgID,
  3206. Manufacturer: warehouse.Manufacturer,
  3207. Dealer: warehouse.Dealer,
  3208. LicenseNumber: warehouse.LicenseNumber,
  3209. IsEdit: 2,
  3210. Creator: warehouseOut.Creater,
  3211. SystemTime: record_time,
  3212. ConsumableType: 3,
  3213. WarehouseOutDetailId: lastOut.ID,
  3214. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3215. IsSys: 1,
  3216. PatientId: patient_id,
  3217. ProjectId: goods.ProjectId,
  3218. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3219. StorehouseId: goods.StorehouseId,
  3220. OverCount: sum_count,
  3221. BuyPrice: warehouse.Price,
  3222. RegisterNumber: warehouse.RegisterNumber,
  3223. }
  3224. if orgID == 9671 || orgID == 10265 {
  3225. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  3226. flow.Price = goodsInfo.PackingPrice
  3227. flow.BuyPrice = goodsInfo.BuyPrice
  3228. }
  3229. err := tx.Create(&flow).Error
  3230. if err != nil {
  3231. return err
  3232. }
  3233. }
  3234. //如果本次出库数据小于历史出库数据 新增1条退库流水
  3235. if count < (out_count - out_count_one) {
  3236. operation_time := time.Now().Unix()
  3237. //创建退库单
  3238. timeStr := time.Now().Format("2006-01-02")
  3239. timeArr := strings.Split(timeStr, "-")
  3240. total, _ := FindAllCancelStockTotal(orgID)
  3241. total = total + 1
  3242. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  3243. cancelStock := models.CancelStock{
  3244. OrderNumber: orderNumber,
  3245. OperaTime: operation_time,
  3246. OrgId: orgID,
  3247. Creater: warehouseOut.Creater,
  3248. Ctime: time.Now().Unix(),
  3249. Status: 1,
  3250. ReturnTime: record_time,
  3251. Type: 1,
  3252. StorehouseId: goods.StorehouseId,
  3253. IsCheck: 1,
  3254. }
  3255. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  3256. if msgerrkonde == gorm.ErrRecordNotFound {
  3257. err := tx.Create(&cancelStock).Error
  3258. if err != nil {
  3259. return err
  3260. }
  3261. }
  3262. cancel, _ := GetLastCancelStockById(orgID)
  3263. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  3264. deaerler, _ := GetDealerById(warehouse.Dealer)
  3265. cancelStockInfo := models.CancelStockInfo{
  3266. GoodId: goods.GoodId,
  3267. CancelStockId: cancel.ID,
  3268. GoodTypeId: goods.GoodTypeId,
  3269. Count: out_count - out_count_one - count,
  3270. Price: warehouse.PackingPrice,
  3271. Total: 0,
  3272. ProductDate: warehouse.ProductDate,
  3273. ExpiryDate: warehouse.ExpiryDate,
  3274. Ctime: time.Now().Unix(),
  3275. Status: 1,
  3276. OrgId: orgID,
  3277. OrderNumber: cancel.OrderNumber,
  3278. Type: 0,
  3279. Dealer: deaerler.DealerName,
  3280. Manufacturer: manufacturer.ManufacturerName,
  3281. Number: warehouse.Number,
  3282. RegisterAccount: "",
  3283. Remark: "",
  3284. WarehouseInfoId: warehouse.ID,
  3285. PatientId: patient_id,
  3286. RecordDate: record_time,
  3287. StorehouseId: goods.StorehouseId,
  3288. IsCheck: 1,
  3289. }
  3290. if orgID == 9671 || orgID == 10265 {
  3291. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  3292. cancelStockInfo.Price = goodsInfo.PackingPrice
  3293. }
  3294. err := tx.Create(&cancelStockInfo).Error
  3295. if err != nil {
  3296. return err
  3297. }
  3298. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  3299. flow := models.VmStockFlow{
  3300. WarehousingId: warehouse.ID,
  3301. GoodId: goods.GoodId,
  3302. Number: warehouse.Number,
  3303. LicenseNumber: warehouse.LicenseNumber,
  3304. Count: out_count - out_count_one - count,
  3305. UserOrgId: orgID,
  3306. PatientId: patient_id,
  3307. SystemTime: record_time,
  3308. ConsumableType: 7,
  3309. IsSys: 1,
  3310. WarehousingOrder: "",
  3311. WarehouseOutId: lastOut.WarehouseOutId,
  3312. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  3313. IsEdit: 0,
  3314. CancelStockId: cancel.ID,
  3315. CancelOrderNumber: cancel.OrderNumber,
  3316. Manufacturer: manufacturer.ID,
  3317. Dealer: 0,
  3318. Creator: warehouseOut.Creater,
  3319. UpdateCreator: 0,
  3320. Status: 1,
  3321. Ctime: time.Now().Unix(),
  3322. Mtime: 0,
  3323. Price: warehouse.PackingPrice,
  3324. WarehousingDetailId: warehouse.ID,
  3325. WarehouseOutDetailId: lastOut.ID,
  3326. CancelOutDetailId: cancelInfo.ID,
  3327. ProductDate: warehouse.ProductDate,
  3328. ExpireDate: warehouse.ExpiryDate,
  3329. StorehouseId: goods.StorehouseId,
  3330. BuyPrice: warehouse.Price,
  3331. ProjectId: goods.ProjectId,
  3332. OverCount: sum_count,
  3333. RegisterNumber: warehouse.RegisterNumber,
  3334. }
  3335. if orgID == 9671 || orgID == 10265 {
  3336. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  3337. flow.Price = goodsInfo.PackingPrice
  3338. flow.BuyPrice = goodsInfo.BuyPrice
  3339. }
  3340. err = tx.Create(&flow).Error
  3341. if err != nil {
  3342. return err
  3343. }
  3344. }
  3345. details := models.BloodAutomaticReduceDetail{
  3346. WarehouseOutId: lastOut.ID,
  3347. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  3348. PatientId: patient_id,
  3349. Ctime: time.Now().Unix(),
  3350. Mtime: time.Now().Unix(),
  3351. Status: 1,
  3352. RecordTime: record_time,
  3353. OrgId: orgID,
  3354. GoodId: goods.GoodId,
  3355. GoodTypeId: goods.GoodTypeId,
  3356. Count: count,
  3357. ProjectId: goods.ProjectId,
  3358. StorehouseId: goods.StorehouseId,
  3359. }
  3360. ////查询该耗材已经出库的数量
  3361. _, errcode := GetAutoMaticReduceDetailTwenty(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  3362. if errcode == gorm.ErrRecordNotFound {
  3363. err := tx.Create(&details).Error
  3364. if err != nil {
  3365. return err
  3366. }
  3367. } else if errcode == nil {
  3368. detail := models.BloodAutomaticReduceDetail{}
  3369. err := tx.Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? and good_type_id = ?", orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3370. if err != nil {
  3371. return err
  3372. }
  3373. err = tx.Create(&detail).Error
  3374. if err != nil {
  3375. return err
  3376. }
  3377. }
  3378. return err
  3379. } else {
  3380. // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3381. warehouse.StockCount = 0
  3382. warehouse.Mtime = time.Now().Unix()
  3383. errThree := UpDateWarehouseInfoByStock(&warehouse)
  3384. if errThree != nil {
  3385. return errThree
  3386. }
  3387. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  3388. var sum_count int64
  3389. for _, item := range goodList {
  3390. sum_count += item.StockCount
  3391. }
  3392. warehouseOutInfo := &models.WarehouseOutInfo{
  3393. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3394. WarehouseOutId: warehouseOut.ID,
  3395. WarehouseInfotId: warehouse.ID,
  3396. Status: 1,
  3397. Ctime: time.Now().Unix(),
  3398. Remark: warehouse.Remark,
  3399. OrgId: orgID,
  3400. Type: 1,
  3401. Manufacturer: warehouse.Manufacturer,
  3402. Dealer: warehouse.Dealer,
  3403. IsSys: 1,
  3404. SysRecordTime: record_time,
  3405. GoodTypeId: goods.GoodTypeId,
  3406. GoodId: goods.GoodId,
  3407. PatientId: patient_id,
  3408. Number: warehouse.Number,
  3409. LicenseNumber: warehouse.LicenseNumber,
  3410. Price: warehouse.PackingPrice,
  3411. ExpiryDate: warehouse.ExpiryDate,
  3412. ProductDate: warehouse.ProductDate,
  3413. ProjectId: goods.ProjectId,
  3414. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3415. StorehouseId: goods.StorehouseId,
  3416. IsCheck: 1,
  3417. OverCount: sum_count,
  3418. RegisterNumber: warehouse.RegisterNumber,
  3419. }
  3420. warehouseOutInfo.Count = stock_number
  3421. if orgID == 9671 || orgID == 10265 {
  3422. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  3423. warehouseOutInfo.Price = goodsInfo.PackingPrice
  3424. }
  3425. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  3426. if errcodes == gorm.ErrRecordNotFound {
  3427. outInfo := models.WarehouseOutInfo{}
  3428. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", goods.GoodId, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": warehouseOutInfo.WarehouseOutId, "WarehouseOutOrderNumber": warehouseOutInfo.WarehouseOutOrderNumber, "sys_record_time": warehouseOutInfo.SysRecordTime, "good_type_id": warehouseOutInfo.GoodTypeId, "patient_id": warehouseOutInfo.PatientId, "consumable_type": warehouseOutInfo.ConsumableType, "count": warehouseOutInfo.Count, "price": warehouseOutInfo.Price, "dealer": warehouseOutInfo.Dealer, "manufacturer": warehouseOutInfo.Manufacturer, "number": warehouseOutInfo.Number, "license_number": warehouseOutInfo.LicenseNumber, "over_count": warehouseOutInfo.OverCount}).Error
  3429. if err != nil {
  3430. return err
  3431. }
  3432. } else if errcodes == nil {
  3433. goods.Count = deliver_number - stock_number
  3434. //更新数量为 该批次剩余数量 + 还有未出的数量
  3435. warehouseOutInfo.Count = stock_number
  3436. outInfoOne, _ := GetWarehouseOutInfoIsExistTwo(goods.GoodId, patient_id, record_time, goods.ProjectId)
  3437. if count != outInfoOne.Count {
  3438. outInfo := models.WarehouseOutInfo{}
  3439. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", goods.GoodId, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": warehouseOutInfo.WarehouseOutId, "WarehouseOutOrderNumber": warehouseOutInfo.WarehouseOutOrderNumber, "sys_record_time": warehouseOutInfo.SysRecordTime, "good_type_id": warehouseOutInfo.GoodTypeId, "patient_id": warehouseOutInfo.PatientId, "consumable_type": warehouseOutInfo.ConsumableType, "count": warehouseOutInfo.Count, "price": warehouseOutInfo.Price, "dealer": warehouseOutInfo.Dealer, "manufacturer": warehouseOutInfo.Manufacturer, "number": warehouseOutInfo.Number, "license_number": warehouseOutInfo.LicenseNumber, "over_count": warehouseOutInfo.OverCount}).Error
  3440. if err != nil {
  3441. return err
  3442. }
  3443. }
  3444. }
  3445. lastOut, _ := FindWarehouseOutInfoByPatientId(patient_id, record_time, goods.GoodId, orgID)
  3446. //查询该该批次已经出库的数据
  3447. flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  3448. var out_count int64
  3449. var out_count_one int64
  3450. for _, item := range flowGood {
  3451. out_count += item.Count
  3452. }
  3453. flowGoodTwo, _ := GetStockFlowIsBatchNumberThree(patient_id, record_time, goods.GoodId)
  3454. for _, item := range flowGoodTwo {
  3455. out_count_one += item.Count
  3456. }
  3457. //如果出库数量 大于 历史出库数据 新增1条流水
  3458. if count > out_count-out_count_one {
  3459. flow := models.VmStockFlow{
  3460. WarehouseOutId: warehouseOut.ID,
  3461. WarehousingId: warehouse.ID,
  3462. GoodId: goods.GoodId,
  3463. Number: warehouse.Number,
  3464. ProductDate: warehouse.ProductDate,
  3465. ExpireDate: warehouse.ExpiryDate,
  3466. Count: stock_number,
  3467. Price: warehouse.PackingPrice,
  3468. Status: 1,
  3469. Ctime: time.Now().Unix(),
  3470. UserOrgId: orgID,
  3471. Manufacturer: warehouse.Manufacturer,
  3472. Dealer: warehouse.Dealer,
  3473. LicenseNumber: warehouse.LicenseNumber,
  3474. IsEdit: 2,
  3475. Creator: warehouseOut.Creater,
  3476. SystemTime: record_time,
  3477. ConsumableType: 3,
  3478. WarehouseOutDetailId: lastOut.ID,
  3479. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3480. IsSys: 1,
  3481. PatientId: patient_id,
  3482. ProjectId: goods.ProjectId,
  3483. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3484. StorehouseId: goods.StorehouseId,
  3485. OverCount: sum_count,
  3486. BuyPrice: warehouse.Price,
  3487. RegisterNumber: warehouse.RegisterNumber,
  3488. }
  3489. if orgID == 9671 || orgID == 10265 {
  3490. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  3491. flow.Price = goodsInfo.PackingPrice
  3492. flow.BuyPrice = goodsInfo.BuyPrice
  3493. }
  3494. err := tx.Create(&flow).Error
  3495. if err != nil {
  3496. return err
  3497. }
  3498. }
  3499. //退库
  3500. if count < out_count-out_count_one {
  3501. operation_time := time.Now().Unix()
  3502. //创建退库单
  3503. timeStr := time.Now().Format("2006-01-02")
  3504. timeArr := strings.Split(timeStr, "-")
  3505. total, _ := FindAllCancelStockTotal(orgID)
  3506. total = total + 1
  3507. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  3508. cancelStock := models.CancelStock{
  3509. OrderNumber: orderNumber,
  3510. OperaTime: operation_time,
  3511. OrgId: orgID,
  3512. Creater: warehouseOut.Creater,
  3513. Ctime: time.Now().Unix(),
  3514. Status: 1,
  3515. ReturnTime: record_time,
  3516. Type: 1,
  3517. StorehouseId: goods.StorehouseId,
  3518. IsCheck: 1,
  3519. }
  3520. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  3521. if msgerrkonde == gorm.ErrRecordNotFound {
  3522. err := tx.Create(&cancelStock).Error
  3523. if err != nil {
  3524. return err
  3525. }
  3526. }
  3527. cancel, _ := GetLastCancelStockById(orgID)
  3528. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  3529. deaerler, _ := GetDealerById(warehouse.Dealer)
  3530. cancelStockInfo := models.CancelStockInfo{
  3531. GoodId: goods.GoodId,
  3532. CancelStockId: cancel.ID,
  3533. GoodTypeId: goods.GoodTypeId,
  3534. Count: out_count - out_count_one - count,
  3535. Price: warehouse.PackingPrice,
  3536. Total: 0,
  3537. ProductDate: warehouse.ProductDate,
  3538. ExpiryDate: warehouse.ExpiryDate,
  3539. Ctime: time.Now().Unix(),
  3540. Status: 1,
  3541. OrgId: orgID,
  3542. OrderNumber: cancel.OrderNumber,
  3543. Type: 0,
  3544. Dealer: deaerler.DealerName,
  3545. Manufacturer: manufacturer.ManufacturerName,
  3546. Number: warehouse.Number,
  3547. RegisterAccount: "",
  3548. Remark: "",
  3549. WarehouseInfoId: warehouse.ID,
  3550. PatientId: patient_id,
  3551. RecordDate: record_time,
  3552. StorehouseId: goods.StorehouseId,
  3553. IsCheck: 1,
  3554. }
  3555. if orgID == 9671 || orgID == 10265 {
  3556. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  3557. cancelStockInfo.Price = goodsInfo.PackingPrice
  3558. }
  3559. err := tx.Create(&cancelStockInfo).Error
  3560. if err != nil {
  3561. return err
  3562. }
  3563. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  3564. flow := models.VmStockFlow{
  3565. WarehousingId: warehouse.ID,
  3566. GoodId: goods.GoodId,
  3567. Number: warehouse.Number,
  3568. LicenseNumber: warehouse.LicenseNumber,
  3569. Count: out_count - count,
  3570. UserOrgId: orgID,
  3571. PatientId: patient_id,
  3572. SystemTime: record_time,
  3573. ConsumableType: 7,
  3574. IsSys: 1,
  3575. WarehousingOrder: "",
  3576. WarehouseOutId: lastOut.WarehouseOutId,
  3577. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  3578. IsEdit: 0,
  3579. CancelStockId: cancel.ID,
  3580. CancelOrderNumber: cancel.OrderNumber,
  3581. Manufacturer: manufacturer.ID,
  3582. Dealer: 0,
  3583. Creator: warehouseOut.Creater,
  3584. UpdateCreator: 0,
  3585. Status: 1,
  3586. Ctime: time.Now().Unix(),
  3587. Mtime: 0,
  3588. Price: warehouse.PackingPrice,
  3589. WarehousingDetailId: warehouse.ID,
  3590. WarehouseOutDetailId: lastOut.ID,
  3591. CancelOutDetailId: cancelInfo.ID,
  3592. ProductDate: warehouse.ProductDate,
  3593. ExpireDate: warehouse.ExpiryDate,
  3594. StorehouseId: goods.StorehouseId,
  3595. BuyPrice: warehouse.Price,
  3596. ProjectId: goods.ProjectId,
  3597. OverCount: sum_count,
  3598. RegisterNumber: warehouse.RegisterNumber,
  3599. }
  3600. if orgID == 9671 || orgID == 10265 {
  3601. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  3602. flow.Price = goodsInfo.PackingPrice
  3603. flow.BuyPrice = goodsInfo.BuyPrice
  3604. }
  3605. err = tx.Create(&flow).Error
  3606. if err != nil {
  3607. return err
  3608. }
  3609. }
  3610. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  3611. goods.Count = deliver_number - stock_number
  3612. ConsumablesGoodDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  3613. }
  3614. return err
  3615. }
  3616. func ConsumablesGoodDeliveryNew(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  3617. //开事务
  3618. tx := XTWriteDB().Begin()
  3619. if err != nil {
  3620. utils.ErrorLog("事务失败,原因为: %v", err)
  3621. tx.Rollback()
  3622. } else {
  3623. tx.Commit()
  3624. }
  3625. var deliver_number int64 = 0
  3626. var stock_number int64 = 0
  3627. var maxNumber int64 = 0
  3628. deliver_number = goods.Count
  3629. // 根据先进先出原则,查询最先入库的批次,进行出库
  3630. // 如果没有对应的库存,则报错
  3631. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  3632. if goods.Count > 0 {
  3633. if err != nil {
  3634. return errors.New("库存数量不足")
  3635. }
  3636. }
  3637. stock_number = warehouse.StockCount
  3638. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  3639. if stock_number >= deliver_number {
  3640. maxNumber = goods.Count
  3641. //出库
  3642. warehouse.StockCount = warehouse.StockCount - maxNumber
  3643. warehouse.Mtime = time.Now().Unix()
  3644. if warehouse.StockCount < 0 {
  3645. return errors.New("库存数量不足")
  3646. }
  3647. err := tx.Save(&warehouse).Error
  3648. if err != nil {
  3649. return err
  3650. }
  3651. //更新his_prescripton_project出库状态值
  3652. UpdateHisPrescriptionProjectStatus(goods.GoodId, record_time, orgID, patient_id)
  3653. var info []*models.WarehousingInfo
  3654. err = tx.Where("good_id = ? and org_id = ? and is_check =1 and status= 1 and stock_count > 0 and storehouse_id = ?", goods.GoodId, orgID, goods.StorehouseId).Find(&info).Error
  3655. var sum_count int64
  3656. for _, item := range info {
  3657. sum_count += item.StockCount
  3658. }
  3659. warehouseOutInfo := &models.WarehouseOutInfo{
  3660. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3661. WarehouseOutId: warehouseOut.ID,
  3662. WarehouseInfotId: warehouse.ID,
  3663. Status: 1,
  3664. Ctime: time.Now().Unix(),
  3665. Remark: warehouse.Remark,
  3666. OrgId: orgID,
  3667. Type: 1,
  3668. Manufacturer: warehouse.Manufacturer,
  3669. Dealer: warehouse.Dealer,
  3670. IsSys: 1,
  3671. SysRecordTime: record_time,
  3672. GoodTypeId: goods.GoodTypeId,
  3673. GoodId: goods.GoodId,
  3674. PatientId: patient_id,
  3675. Number: warehouse.Number,
  3676. LicenseNumber: warehouse.LicenseNumber,
  3677. Price: warehouse.PackingPrice,
  3678. ExpiryDate: warehouse.ExpiryDate,
  3679. ProductDate: warehouse.ProductDate,
  3680. ProjectId: goods.ProjectId,
  3681. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3682. StorehouseId: goods.StorehouseId,
  3683. IsCheck: 1,
  3684. OverCount: sum_count,
  3685. RegisterNumber: warehouse.RegisterNumber,
  3686. }
  3687. warehouseOutInfo.Count = count
  3688. if orgID == 9671 || orgID == 10265 {
  3689. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  3690. warehouseOutInfo.Price = goodsInfo.PackingPrice
  3691. }
  3692. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  3693. if errcodes == gorm.ErrRecordNotFound {
  3694. err := tx.Create(&warehouseOutInfo).Error
  3695. if err != nil {
  3696. return err
  3697. }
  3698. } else if errcodes == nil {
  3699. outInfoOne, _ := GetWarehouseOutInfoIsExistTwo(goods.GoodId, patient_id, record_time, goods.ProjectId)
  3700. if count != outInfoOne.Count {
  3701. outInfo := models.WarehouseOutInfo{}
  3702. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", goods.GoodId, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": warehouseOutInfo.WarehouseOutId, "WarehouseOutOrderNumber": warehouseOutInfo.WarehouseOutOrderNumber, "sys_record_time": warehouseOutInfo.SysRecordTime, "good_type_id": warehouseOutInfo.GoodTypeId, "patient_id": warehouseOutInfo.PatientId, "consumable_type": warehouseOutInfo.ConsumableType, "count": warehouseOutInfo.Count, "price": warehouseOutInfo.Price, "dealer": warehouseOutInfo.Dealer, "manufacturer": warehouseOutInfo.Manufacturer, "number": warehouseOutInfo.Number, "license_number": warehouseOutInfo.LicenseNumber, "over_count": warehouseOutInfo.OverCount}).Error
  3703. if err != nil {
  3704. return err
  3705. }
  3706. }
  3707. }
  3708. lastOut, _ := FindWarehouseOutInfoByPatientId(patient_id, record_time, goods.GoodId, orgID)
  3709. //查询已经出库的数据
  3710. flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  3711. var out_count int64
  3712. var out_count_one int64
  3713. for _, item := range flowGood {
  3714. out_count += item.Count
  3715. }
  3716. //查询退库数据
  3717. flowGoodTwo, _ := GetStockFlowIsBatchNumberThree(patient_id, record_time, goods.GoodId)
  3718. for _, item := range flowGoodTwo {
  3719. out_count_one += item.Count
  3720. }
  3721. //如果本次出库数据大于历史出库数据 新增1条流水
  3722. if count > (out_count - out_count_one) {
  3723. flow := models.VmStockFlow{
  3724. WarehouseOutId: warehouseOut.ID,
  3725. WarehousingId: warehouse.ID,
  3726. GoodId: goods.GoodId,
  3727. Number: warehouse.Number,
  3728. ProductDate: warehouse.ProductDate,
  3729. ExpireDate: warehouse.ExpiryDate,
  3730. Count: count - out_count + out_count_one,
  3731. Price: warehouse.PackingPrice,
  3732. Status: 1,
  3733. Ctime: time.Now().Unix(),
  3734. UserOrgId: orgID,
  3735. Manufacturer: warehouse.Manufacturer,
  3736. Dealer: warehouse.Dealer,
  3737. LicenseNumber: warehouse.LicenseNumber,
  3738. IsEdit: 2,
  3739. Creator: warehouseOut.Creater,
  3740. SystemTime: record_time,
  3741. ConsumableType: 3,
  3742. WarehouseOutDetailId: lastOut.ID,
  3743. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3744. IsSys: 1,
  3745. PatientId: patient_id,
  3746. ProjectId: goods.ProjectId,
  3747. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3748. StorehouseId: goods.StorehouseId,
  3749. OverCount: sum_count,
  3750. BuyPrice: warehouse.Price,
  3751. RegisterNumber: warehouse.RegisterNumber,
  3752. }
  3753. if orgID == 9671 || orgID == 10265 {
  3754. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  3755. flow.Price = goodsInfo.PackingPrice
  3756. flow.BuyPrice = goodsInfo.BuyPrice
  3757. }
  3758. err := tx.Create(&flow).Error
  3759. if err != nil {
  3760. return err
  3761. }
  3762. }
  3763. //如果本次出库数据小于历史出库数据 新增1条退库流水
  3764. if count < (out_count - out_count_one) {
  3765. operation_time := time.Now().Unix()
  3766. //创建退库单
  3767. timeStr := time.Now().Format("2006-01-02")
  3768. timeArr := strings.Split(timeStr, "-")
  3769. total, _ := FindAllCancelStockTotal(orgID)
  3770. total = total + 1
  3771. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  3772. cancelStock := models.CancelStock{
  3773. OrderNumber: orderNumber,
  3774. OperaTime: operation_time,
  3775. OrgId: orgID,
  3776. Creater: warehouseOut.Creater,
  3777. Ctime: time.Now().Unix(),
  3778. Status: 1,
  3779. ReturnTime: record_time,
  3780. Type: 1,
  3781. StorehouseId: goods.StorehouseId,
  3782. IsCheck: 1,
  3783. }
  3784. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  3785. if msgerrkonde == gorm.ErrRecordNotFound {
  3786. err := tx.Create(&cancelStock).Error
  3787. if err != nil {
  3788. return err
  3789. }
  3790. }
  3791. cancel, _ := GetLastCancelStockById(orgID)
  3792. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  3793. deaerler, _ := GetDealerById(warehouse.Dealer)
  3794. cancelStockInfo := models.CancelStockInfo{
  3795. GoodId: goods.GoodId,
  3796. CancelStockId: cancel.ID,
  3797. GoodTypeId: goods.GoodTypeId,
  3798. Count: out_count - out_count_one - count,
  3799. Price: warehouse.PackingPrice,
  3800. Total: 0,
  3801. ProductDate: warehouse.ProductDate,
  3802. ExpiryDate: warehouse.ExpiryDate,
  3803. Ctime: time.Now().Unix(),
  3804. Status: 1,
  3805. OrgId: orgID,
  3806. OrderNumber: cancel.OrderNumber,
  3807. Type: 0,
  3808. Dealer: deaerler.DealerName,
  3809. Manufacturer: manufacturer.ManufacturerName,
  3810. Number: warehouse.Number,
  3811. RegisterAccount: "",
  3812. Remark: "",
  3813. WarehouseInfoId: warehouse.ID,
  3814. PatientId: patient_id,
  3815. RecordDate: record_time,
  3816. StorehouseId: goods.StorehouseId,
  3817. IsCheck: 1,
  3818. }
  3819. if orgID == 9671 || orgID == 10265 {
  3820. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  3821. cancelStockInfo.Price = goodsInfo.PackingPrice
  3822. }
  3823. err := tx.Create(&cancelStockInfo).Error
  3824. if err != nil {
  3825. return err
  3826. }
  3827. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  3828. flow := models.VmStockFlow{
  3829. WarehousingId: warehouse.ID,
  3830. GoodId: goods.GoodId,
  3831. Number: warehouse.Number,
  3832. LicenseNumber: warehouse.LicenseNumber,
  3833. Count: out_count - out_count_one - count,
  3834. UserOrgId: orgID,
  3835. PatientId: patient_id,
  3836. SystemTime: record_time,
  3837. ConsumableType: 7,
  3838. IsSys: 1,
  3839. WarehousingOrder: "",
  3840. WarehouseOutId: lastOut.WarehouseOutId,
  3841. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  3842. IsEdit: 0,
  3843. CancelStockId: cancel.ID,
  3844. CancelOrderNumber: cancel.OrderNumber,
  3845. Manufacturer: manufacturer.ID,
  3846. Dealer: 0,
  3847. Creator: warehouseOut.Creater,
  3848. UpdateCreator: 0,
  3849. Status: 1,
  3850. Ctime: time.Now().Unix(),
  3851. Mtime: 0,
  3852. Price: warehouse.PackingPrice,
  3853. WarehousingDetailId: warehouse.ID,
  3854. WarehouseOutDetailId: lastOut.ID,
  3855. CancelOutDetailId: cancelInfo.ID,
  3856. ProductDate: warehouse.ProductDate,
  3857. ExpireDate: warehouse.ExpiryDate,
  3858. StorehouseId: goods.StorehouseId,
  3859. BuyPrice: warehouse.Price,
  3860. ProjectId: goods.ProjectId,
  3861. OverCount: sum_count,
  3862. RegisterNumber: warehouse.RegisterNumber,
  3863. }
  3864. if orgID == 9671 || orgID == 10265 {
  3865. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  3866. flow.Price = goodsInfo.PackingPrice
  3867. flow.BuyPrice = goodsInfo.BuyPrice
  3868. }
  3869. err = tx.Create(&flow).Error
  3870. if err != nil {
  3871. return err
  3872. }
  3873. }
  3874. details := models.BloodAutomaticReduceDetail{
  3875. WarehouseOutId: lastOut.ID,
  3876. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  3877. PatientId: patient_id,
  3878. Ctime: time.Now().Unix(),
  3879. Mtime: time.Now().Unix(),
  3880. Status: 1,
  3881. RecordTime: record_time,
  3882. OrgId: orgID,
  3883. GoodId: goods.GoodId,
  3884. GoodTypeId: goods.GoodTypeId,
  3885. Count: count,
  3886. ProjectId: goods.ProjectId,
  3887. StorehouseId: goods.StorehouseId,
  3888. }
  3889. ////查询该耗材已经出库的数量
  3890. _, errcode := GetAutoMaticReduceDetailTwenty(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  3891. if errcode == gorm.ErrRecordNotFound {
  3892. err := tx.Create(&details).Error
  3893. if err != nil {
  3894. return err
  3895. }
  3896. } else if errcode == nil {
  3897. detail := models.BloodAutomaticReduceDetail{}
  3898. err := tx.Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? and good_type_id = ?", orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3899. if err != nil {
  3900. return err
  3901. }
  3902. err = tx.Create(&detail).Error
  3903. if err != nil {
  3904. return err
  3905. }
  3906. }
  3907. return nil
  3908. } else {
  3909. // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  3910. warehouse.StockCount = 0
  3911. warehouse.Mtime = time.Now().Unix()
  3912. err := tx.Save(&warehouse).Error
  3913. if err != nil {
  3914. return err
  3915. }
  3916. var info []*models.WarehousingInfo
  3917. err = tx.Where("good_id = ? and org_id = ? and is_check =1 and status= 1 and stock_count > 0 and storehouse_id = ?", goods.GoodId, orgID, goods.StorehouseId).Find(&info).Error
  3918. var sum_count int64
  3919. for _, item := range info {
  3920. sum_count += item.StockCount
  3921. }
  3922. warehouseOutInfo := &models.WarehouseOutInfo{
  3923. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  3924. WarehouseOutId: warehouseOut.ID,
  3925. WarehouseInfotId: warehouse.ID,
  3926. Status: 1,
  3927. Ctime: time.Now().Unix(),
  3928. Remark: warehouse.Remark,
  3929. OrgId: orgID,
  3930. Type: 1,
  3931. Manufacturer: warehouse.Manufacturer,
  3932. Dealer: warehouse.Dealer,
  3933. IsSys: 1,
  3934. SysRecordTime: record_time,
  3935. GoodTypeId: goods.GoodTypeId,
  3936. GoodId: goods.GoodId,
  3937. PatientId: patient_id,
  3938. Number: warehouse.Number,
  3939. LicenseNumber: warehouse.LicenseNumber,
  3940. Price: warehouse.PackingPrice,
  3941. ExpiryDate: warehouse.ExpiryDate,
  3942. ProductDate: warehouse.ProductDate,
  3943. ProjectId: goods.ProjectId,
  3944. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  3945. StorehouseId: goods.StorehouseId,
  3946. IsCheck: 1,
  3947. OverCount: sum_count,
  3948. RegisterNumber: warehouse.RegisterNumber,
  3949. }
  3950. warehouseOutInfo.Count = stock_number
  3951. if orgID == 9671 || orgID == 10265 {
  3952. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  3953. warehouseOutInfo.Price = goodsInfo.PackingPrice
  3954. }
  3955. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  3956. if errcodes == gorm.ErrRecordNotFound {
  3957. errors := tx.Create(warehouseOutInfo).Error
  3958. if errors != nil {
  3959. return errors
  3960. }
  3961. } else if errcodes == nil {
  3962. goods.Count = deliver_number - stock_number
  3963. //更新数量为 该批次剩余数量 + 还有未出的数量
  3964. warehouseOutInfo.Count = stock_number
  3965. outInfoOne, _ := GetWarehouseOutInfoIsExistTwo(goods.GoodId, patient_id, record_time, goods.ProjectId)
  3966. if count != outInfoOne.Count {
  3967. outInfo := models.WarehouseOutInfo{}
  3968. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", goods.GoodId, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": warehouseOutInfo.WarehouseOutId, "WarehouseOutOrderNumber": warehouseOutInfo.WarehouseOutOrderNumber, "sys_record_time": warehouseOutInfo.SysRecordTime, "good_type_id": warehouseOutInfo.GoodTypeId, "patient_id": warehouseOutInfo.PatientId, "consumable_type": warehouseOutInfo.ConsumableType, "count": warehouseOutInfo.Count, "price": warehouseOutInfo.Price, "dealer": warehouseOutInfo.Dealer, "manufacturer": warehouseOutInfo.Manufacturer, "number": warehouseOutInfo.Number, "license_number": warehouseOutInfo.LicenseNumber, "over_count": warehouseOutInfo.OverCount}).Error
  3969. if err != nil {
  3970. return err
  3971. }
  3972. }
  3973. }
  3974. lastOut, _ := FindWarehouseOutInfoByPatientId(patient_id, record_time, goods.GoodId, orgID)
  3975. //查询该该批次已经出库的数据
  3976. flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  3977. var out_count int64
  3978. var out_count_one int64
  3979. for _, item := range flowGood {
  3980. out_count += item.Count
  3981. }
  3982. flowGoodTwo, _ := GetStockFlowIsBatchNumberThree(patient_id, record_time, goods.GoodId)
  3983. for _, item := range flowGoodTwo {
  3984. out_count_one += item.Count
  3985. }
  3986. //如果出库数量 大于 历史出库数据 新增1条流水
  3987. if count > out_count-out_count_one {
  3988. flow := models.VmStockFlow{
  3989. WarehouseOutId: warehouseOut.ID,
  3990. WarehousingId: warehouse.ID,
  3991. GoodId: goods.GoodId,
  3992. Number: warehouse.Number,
  3993. ProductDate: warehouse.ProductDate,
  3994. ExpireDate: warehouse.ExpiryDate,
  3995. Count: stock_number,
  3996. Price: warehouse.PackingPrice,
  3997. Status: 1,
  3998. Ctime: time.Now().Unix(),
  3999. UserOrgId: orgID,
  4000. Manufacturer: warehouse.Manufacturer,
  4001. Dealer: warehouse.Dealer,
  4002. LicenseNumber: warehouse.LicenseNumber,
  4003. IsEdit: 2,
  4004. Creator: warehouseOut.Creater,
  4005. SystemTime: record_time,
  4006. ConsumableType: 3,
  4007. WarehouseOutDetailId: lastOut.ID,
  4008. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  4009. IsSys: 1,
  4010. PatientId: patient_id,
  4011. ProjectId: goods.ProjectId,
  4012. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  4013. StorehouseId: goods.StorehouseId,
  4014. OverCount: sum_count,
  4015. BuyPrice: warehouse.Price,
  4016. RegisterNumber: warehouse.RegisterNumber,
  4017. }
  4018. if orgID == 9671 || orgID == 10265 {
  4019. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  4020. flow.Price = goodsInfo.PackingPrice
  4021. flow.BuyPrice = goodsInfo.BuyPrice
  4022. }
  4023. err := tx.Create(&flow).Error
  4024. if err != nil {
  4025. return err
  4026. }
  4027. }
  4028. //退库
  4029. if count < out_count-out_count_one {
  4030. operation_time := time.Now().Unix()
  4031. //创建退库单
  4032. timeStr := time.Now().Format("2006-01-02")
  4033. timeArr := strings.Split(timeStr, "-")
  4034. total, _ := FindAllCancelStockTotal(orgID)
  4035. total = total + 1
  4036. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  4037. cancelStock := models.CancelStock{
  4038. OrderNumber: orderNumber,
  4039. OperaTime: operation_time,
  4040. OrgId: orgID,
  4041. Creater: warehouseOut.Creater,
  4042. Ctime: time.Now().Unix(),
  4043. Status: 1,
  4044. ReturnTime: record_time,
  4045. Type: 1,
  4046. StorehouseId: goods.StorehouseId,
  4047. IsCheck: 1,
  4048. }
  4049. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  4050. if msgerrkonde == gorm.ErrRecordNotFound {
  4051. err := tx.Create(&cancelStock).Error
  4052. if err != nil {
  4053. return err
  4054. }
  4055. }
  4056. cancel, _ := GetLastCancelStockById(orgID)
  4057. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  4058. deaerler, _ := GetDealerById(warehouse.Dealer)
  4059. cancelStockInfo := models.CancelStockInfo{
  4060. GoodId: goods.GoodId,
  4061. CancelStockId: cancel.ID,
  4062. GoodTypeId: goods.GoodTypeId,
  4063. Count: out_count - out_count_one - count,
  4064. Price: warehouse.PackingPrice,
  4065. Total: 0,
  4066. ProductDate: warehouse.ProductDate,
  4067. ExpiryDate: warehouse.ExpiryDate,
  4068. Ctime: time.Now().Unix(),
  4069. Status: 1,
  4070. OrgId: orgID,
  4071. OrderNumber: cancel.OrderNumber,
  4072. Type: 0,
  4073. Dealer: deaerler.DealerName,
  4074. Manufacturer: manufacturer.ManufacturerName,
  4075. Number: warehouse.Number,
  4076. RegisterAccount: "",
  4077. Remark: "",
  4078. WarehouseInfoId: warehouse.ID,
  4079. PatientId: patient_id,
  4080. RecordDate: record_time,
  4081. StorehouseId: goods.StorehouseId,
  4082. IsCheck: 1,
  4083. }
  4084. if orgID == 9671 || orgID == 10265 {
  4085. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  4086. cancelStockInfo.Price = goodsInfo.PackingPrice
  4087. }
  4088. err := tx.Create(&cancelStockInfo).Error
  4089. if err != nil {
  4090. return err
  4091. }
  4092. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  4093. flow := models.VmStockFlow{
  4094. WarehousingId: warehouse.ID,
  4095. GoodId: goods.GoodId,
  4096. Number: warehouse.Number,
  4097. LicenseNumber: warehouse.LicenseNumber,
  4098. Count: out_count - count,
  4099. UserOrgId: orgID,
  4100. PatientId: patient_id,
  4101. SystemTime: record_time,
  4102. ConsumableType: 7,
  4103. IsSys: 1,
  4104. WarehousingOrder: "",
  4105. WarehouseOutId: lastOut.WarehouseOutId,
  4106. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  4107. IsEdit: 0,
  4108. CancelStockId: cancel.ID,
  4109. CancelOrderNumber: cancel.OrderNumber,
  4110. Manufacturer: manufacturer.ID,
  4111. Dealer: 0,
  4112. Creator: warehouseOut.Creater,
  4113. UpdateCreator: 0,
  4114. Status: 1,
  4115. Ctime: time.Now().Unix(),
  4116. Mtime: 0,
  4117. Price: warehouse.PackingPrice,
  4118. WarehousingDetailId: warehouse.ID,
  4119. WarehouseOutDetailId: lastOut.ID,
  4120. CancelOutDetailId: cancelInfo.ID,
  4121. ProductDate: warehouse.ProductDate,
  4122. ExpireDate: warehouse.ExpiryDate,
  4123. StorehouseId: goods.StorehouseId,
  4124. BuyPrice: warehouse.Price,
  4125. ProjectId: goods.ProjectId,
  4126. OverCount: sum_count,
  4127. RegisterNumber: warehouse.RegisterNumber,
  4128. }
  4129. if orgID == 9671 || orgID == 10265 {
  4130. goodsInfo, _ := FindeGoodInfo(orgID, goods.GoodId)
  4131. flow.Price = goodsInfo.PackingPrice
  4132. flow.BuyPrice = goodsInfo.BuyPrice
  4133. }
  4134. err = tx.Create(&flow).Error
  4135. if err != nil {
  4136. return err
  4137. }
  4138. }
  4139. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  4140. goods.Count = deliver_number - stock_number
  4141. ConsumablesGoodDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  4142. }
  4143. return nil
  4144. }
  4145. func UpdateDrugWarehouseOutInfoTen(patient_id int64, sys_record_time int64, advice_id int64) error {
  4146. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("patient_id = ? and sys_record_time = ? and status=1 and advice_id = ?", patient_id, sys_record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  4147. return err
  4148. }
  4149. func UpdateBasePrice(id int64, min_price float64) error {
  4150. err := XTWriteDB().Model(models.XtBaseDrug{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"min_price": min_price}).Error
  4151. return err
  4152. }
  4153. func UpdateGoodInfoByPrice(id int64, org_id int64, price float64, packingprice float64) error {
  4154. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id=? and org_id = ? and status =1", id, org_id).Updates(map[string]interface{}{"buy_price": price, "packing_price": packingprice}).Error
  4155. return err
  4156. }