new_warehouse_service.go 113KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965
  1. package service
  2. import (
  3. "XT_New/models"
  4. "errors"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "math"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. //耗材出库
  13. func ConsumablesGoodDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  14. //开事务
  15. var deliver_number int64 = 0
  16. var stock_number int64 = 0
  17. var maxNumber int64 = 0
  18. deliver_number = goods.Count
  19. // 根据先进先出原则,查询最先入库的批次,进行出库
  20. // 如果没有对应的库存,则报错
  21. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId, goods.StorehouseId)
  22. if err != nil {
  23. return errors.New("库存数量不足")
  24. }
  25. stock_number = warehouse.StockCount
  26. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  27. if stock_number >= deliver_number {
  28. maxNumber = goods.Count
  29. //出库
  30. warehouse.StockCount = warehouse.StockCount - maxNumber
  31. warehouse.Mtime = time.Now().Unix()
  32. if warehouse.StockCount < 0 {
  33. return errors.New("库存数量不足")
  34. }
  35. errThree := UpDateWarehouseInfoByStock(&warehouse)
  36. if errThree != nil {
  37. return errThree
  38. }
  39. //更新his_prescripton_project出库状态值
  40. UpdateHisPrescriptionProjectStatus(goods.GoodId, record_time, orgID, patient_id)
  41. //查询剩余库存
  42. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  43. var sum_count int64
  44. for _, item := range goodList {
  45. sum_count += item.StockCount
  46. }
  47. warehouseOutInfo := &models.WarehouseOutInfo{
  48. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  49. WarehouseOutId: warehouseOut.ID,
  50. WarehouseInfotId: warehouse.ID,
  51. Status: 1,
  52. Ctime: time.Now().Unix(),
  53. Remark: warehouse.Remark,
  54. OrgId: orgID,
  55. Type: 1,
  56. Manufacturer: warehouse.Manufacturer,
  57. Dealer: warehouse.Dealer,
  58. IsSys: 1,
  59. SysRecordTime: record_time,
  60. GoodTypeId: goods.GoodTypeId,
  61. GoodId: goods.GoodId,
  62. PatientId: patient_id,
  63. Number: warehouse.Number,
  64. LicenseNumber: warehouse.LicenseNumber,
  65. Price: warehouse.PackingPrice,
  66. ExpiryDate: warehouse.ExpiryDate,
  67. ProductDate: warehouse.ProductDate,
  68. ProjectId: goods.ProjectId,
  69. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  70. StorehouseId: goods.StorehouseId,
  71. IsCheck: 1,
  72. OverCount: sum_count,
  73. RegisterNumber: warehouse.RegisterNumber,
  74. }
  75. warehouseOutInfo.Count = count
  76. if orgID == 9671 || orgID == 10265 {
  77. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  78. warehouseOutInfo.Price = goodsInfo.PackingPrice
  79. }
  80. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  81. if errcodes == gorm.ErrRecordNotFound {
  82. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  83. if errOne != nil {
  84. return errOne
  85. }
  86. } else if errcodes == nil {
  87. outInfoOne, _ := GetWarehouseOutInfoIsExistTwo(goods.GoodId, patient_id, record_time, goods.ProjectId)
  88. if count != outInfoOne.Count {
  89. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  90. }
  91. }
  92. lastOut, _ := FindWarehouseOutInfoByPatientId(patient_id, record_time, goods.GoodId, orgID)
  93. //查询已经出库的数据
  94. flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  95. var out_count int64
  96. var out_count_one int64
  97. for _, item := range flowGood {
  98. out_count += item.Count
  99. }
  100. //查询退库数据
  101. flowGoodTwo, _ := GetStockFlowIsBatchNumberThree(patient_id, record_time, goods.GoodId)
  102. for _, item := range flowGoodTwo {
  103. out_count_one += item.Count
  104. }
  105. //如果本次出库数据大于历史出库数据 新增1条流水
  106. if count > (out_count - out_count_one) {
  107. flow := models.VmStockFlow{
  108. WarehouseOutId: warehouseOut.ID,
  109. WarehousingId: warehouse.ID,
  110. GoodId: goods.GoodId,
  111. Number: warehouse.Number,
  112. ProductDate: warehouse.ProductDate,
  113. ExpireDate: warehouse.ExpiryDate,
  114. Count: count - out_count + out_count_one,
  115. Price: warehouse.PackingPrice,
  116. Status: 1,
  117. Ctime: time.Now().Unix(),
  118. UserOrgId: orgID,
  119. Manufacturer: warehouse.Manufacturer,
  120. Dealer: warehouse.Dealer,
  121. LicenseNumber: warehouse.LicenseNumber,
  122. IsEdit: 2,
  123. Creator: warehouseOut.Creater,
  124. SystemTime: record_time,
  125. ConsumableType: 3,
  126. WarehouseOutDetailId: lastOut.ID,
  127. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  128. IsSys: 1,
  129. PatientId: patient_id,
  130. ProjectId: goods.ProjectId,
  131. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  132. StorehouseId: goods.StorehouseId,
  133. OverCount: sum_count,
  134. BuyPrice: warehouse.Price,
  135. RegisterNumber: warehouse.RegisterNumber,
  136. }
  137. if orgID == 9671 || orgID == 10265 {
  138. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  139. flow.Price = goodsInfo.PackingPrice
  140. flow.BuyPrice = goodsInfo.BuyPrice
  141. }
  142. errThre := CreateStockFlowOne(flow)
  143. if errThre != nil {
  144. return errThre
  145. }
  146. }
  147. //如果本次出库数据小于历史出库数据 新增1条退库流水
  148. if count < (out_count - out_count_one) {
  149. operation_time := time.Now().Unix()
  150. //创建退库单
  151. timeStr := time.Now().Format("2006-01-02")
  152. timeArr := strings.Split(timeStr, "-")
  153. total, _ := FindAllCancelStockTotal(orgID)
  154. total = total + 1
  155. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  156. cancelStock := models.CancelStock{
  157. OrderNumber: orderNumber,
  158. OperaTime: operation_time,
  159. OrgId: orgID,
  160. Creater: warehouseOut.Creater,
  161. Ctime: time.Now().Unix(),
  162. Status: 1,
  163. ReturnTime: record_time,
  164. Type: 1,
  165. StorehouseId: goods.StorehouseId,
  166. IsCheck: 1,
  167. }
  168. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  169. if msgerrkonde == gorm.ErrRecordNotFound {
  170. AddSigleCancelStock(&cancelStock)
  171. }
  172. cancel, _ := GetLastCancelStockById(orgID)
  173. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  174. deaerler, _ := GetDealerById(warehouse.Dealer)
  175. cancelStockInfo := models.CancelStockInfo{
  176. GoodId: goods.GoodId,
  177. CancelStockId: cancel.ID,
  178. GoodTypeId: goods.GoodTypeId,
  179. Count: out_count - out_count_one - count,
  180. Price: warehouse.PackingPrice,
  181. Total: 0,
  182. ProductDate: warehouse.ProductDate,
  183. ExpiryDate: warehouse.ExpiryDate,
  184. Ctime: time.Now().Unix(),
  185. Status: 1,
  186. OrgId: orgID,
  187. OrderNumber: cancel.OrderNumber,
  188. Type: 0,
  189. Dealer: deaerler.DealerName,
  190. Manufacturer: manufacturer.ManufacturerName,
  191. Number: warehouse.Number,
  192. RegisterAccount: "",
  193. Remark: "",
  194. WarehouseInfoId: warehouse.ID,
  195. PatientId: patient_id,
  196. RecordDate: record_time,
  197. StorehouseId: goods.StorehouseId,
  198. IsCheck: 1,
  199. }
  200. if orgID == 9671 || orgID == 10265 {
  201. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  202. cancelStockInfo.Price = goodsInfo.PackingPrice
  203. }
  204. CreateCancelStockInfoOne(&cancelStockInfo)
  205. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  206. flow := models.VmStockFlow{
  207. WarehousingId: warehouse.ID,
  208. GoodId: goods.GoodId,
  209. Number: warehouse.Number,
  210. LicenseNumber: warehouse.LicenseNumber,
  211. Count: out_count - out_count_one - count,
  212. UserOrgId: orgID,
  213. PatientId: patient_id,
  214. SystemTime: record_time,
  215. ConsumableType: 7,
  216. IsSys: 1,
  217. WarehousingOrder: "",
  218. WarehouseOutId: lastOut.WarehouseOutId,
  219. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  220. IsEdit: 0,
  221. CancelStockId: cancel.ID,
  222. CancelOrderNumber: cancel.OrderNumber,
  223. Manufacturer: manufacturer.ID,
  224. Dealer: 0,
  225. Creator: warehouseOut.Creater,
  226. UpdateCreator: 0,
  227. Status: 1,
  228. Ctime: time.Now().Unix(),
  229. Mtime: 0,
  230. Price: warehouse.PackingPrice,
  231. WarehousingDetailId: warehouse.ID,
  232. WarehouseOutDetailId: lastOut.ID,
  233. CancelOutDetailId: cancelInfo.ID,
  234. ProductDate: warehouse.ProductDate,
  235. ExpireDate: warehouse.ExpiryDate,
  236. StorehouseId: goods.StorehouseId,
  237. BuyPrice: warehouse.Price,
  238. ProjectId: goods.ProjectId,
  239. OverCount: sum_count,
  240. RegisterNumber: warehouse.RegisterNumber,
  241. }
  242. if orgID == 9671 || orgID == 10265 {
  243. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  244. flow.Price = goodsInfo.PackingPrice
  245. flow.BuyPrice = goodsInfo.BuyPrice
  246. }
  247. CreateStockFlowOne(flow)
  248. }
  249. details := models.BloodAutomaticReduceDetail{
  250. WarehouseOutId: lastOut.ID,
  251. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  252. PatientId: patient_id,
  253. Ctime: time.Now().Unix(),
  254. Mtime: time.Now().Unix(),
  255. Status: 1,
  256. RecordTime: record_time,
  257. OrgId: orgID,
  258. GoodId: goods.GoodId,
  259. GoodTypeId: goods.GoodTypeId,
  260. Count: count,
  261. ProjectId: goods.ProjectId,
  262. StorehouseId: goods.StorehouseId,
  263. }
  264. ////查询该耗材已经出库的数量
  265. _, errcode := GetAutoMaticReduceDetailTwenty(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  266. if errcode == gorm.ErrRecordNotFound {
  267. errTwo := CreateAutoReduceRecord(&details)
  268. if errTwo != nil {
  269. return errTwo
  270. }
  271. } else if errcode == nil {
  272. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  273. CreateAutoReduceRecord(&details)
  274. }
  275. return nil
  276. } else {
  277. // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  278. warehouse.StockCount = 0
  279. warehouse.Mtime = time.Now().Unix()
  280. errThree := UpDateWarehouseInfoByStock(&warehouse)
  281. if errThree != nil {
  282. return errThree
  283. }
  284. goodList, _ := GetAllGoodSumCount(goods.GoodId, orgID, goods.StorehouseId)
  285. var sum_count int64
  286. for _, item := range goodList {
  287. sum_count += item.StockCount
  288. }
  289. warehouseOutInfo := &models.WarehouseOutInfo{
  290. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  291. WarehouseOutId: warehouseOut.ID,
  292. WarehouseInfotId: warehouse.ID,
  293. Status: 1,
  294. Ctime: time.Now().Unix(),
  295. Remark: warehouse.Remark,
  296. OrgId: orgID,
  297. Type: 1,
  298. Manufacturer: warehouse.Manufacturer,
  299. Dealer: warehouse.Dealer,
  300. IsSys: 1,
  301. SysRecordTime: record_time,
  302. GoodTypeId: goods.GoodTypeId,
  303. GoodId: goods.GoodId,
  304. PatientId: patient_id,
  305. Number: warehouse.Number,
  306. LicenseNumber: warehouse.LicenseNumber,
  307. Price: warehouse.PackingPrice,
  308. ExpiryDate: warehouse.ExpiryDate,
  309. ProductDate: warehouse.ProductDate,
  310. ProjectId: goods.ProjectId,
  311. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  312. StorehouseId: goods.StorehouseId,
  313. IsCheck: 1,
  314. OverCount: sum_count,
  315. RegisterNumber: warehouse.RegisterNumber,
  316. }
  317. warehouseOutInfo.Count = stock_number
  318. if orgID == 9671 || orgID == 10265 {
  319. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  320. warehouseOutInfo.Price = goodsInfo.PackingPrice
  321. }
  322. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time, goods.ProjectId)
  323. //fmt.Println("errcodes23332322332323223", errcodes, goods.ProjectId)
  324. if errcodes == gorm.ErrRecordNotFound {
  325. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  326. if errOne != nil {
  327. return errOne
  328. }
  329. } else if errcodes == nil {
  330. goods.Count = deliver_number - stock_number
  331. //更新数量为 该批次剩余数量 + 还有未出的数量
  332. warehouseOutInfo.Count = stock_number
  333. outInfoOne, _ := GetWarehouseOutInfoIsExistTwo(goods.GoodId, patient_id, record_time, goods.ProjectId)
  334. if count != outInfoOne.Count {
  335. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time, goods.ProjectId)
  336. }
  337. }
  338. lastOut, _ := FindWarehouseOutInfoByPatientId(patient_id, record_time, goods.GoodId, orgID)
  339. //查询该该批次已经出库的数据
  340. flowGood, _ := GetStockFlowIsBatchNumber(warehouse.ID, patient_id, record_time, goods.GoodId)
  341. var out_count int64
  342. var out_count_one int64
  343. for _, item := range flowGood {
  344. out_count += item.Count
  345. }
  346. flowGoodTwo, _ := GetStockFlowIsBatchNumberThree(patient_id, record_time, goods.GoodId)
  347. for _, item := range flowGoodTwo {
  348. out_count_one += item.Count
  349. }
  350. //如果出库数量 大于 历史出库数据 新增1条流水
  351. if count > out_count-out_count_one {
  352. flow := models.VmStockFlow{
  353. WarehouseOutId: warehouseOut.ID,
  354. WarehousingId: warehouse.ID,
  355. GoodId: goods.GoodId,
  356. Number: warehouse.Number,
  357. ProductDate: warehouse.ProductDate,
  358. ExpireDate: warehouse.ExpiryDate,
  359. Count: stock_number,
  360. Price: warehouse.PackingPrice,
  361. Status: 1,
  362. Ctime: time.Now().Unix(),
  363. UserOrgId: orgID,
  364. Manufacturer: warehouse.Manufacturer,
  365. Dealer: warehouse.Dealer,
  366. LicenseNumber: warehouse.LicenseNumber,
  367. IsEdit: 2,
  368. Creator: warehouseOut.Creater,
  369. SystemTime: record_time,
  370. ConsumableType: 3,
  371. WarehouseOutDetailId: lastOut.ID,
  372. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  373. IsSys: 1,
  374. PatientId: patient_id,
  375. ProjectId: goods.ProjectId,
  376. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  377. StorehouseId: goods.StorehouseId,
  378. OverCount: sum_count,
  379. BuyPrice: warehouse.Price,
  380. RegisterNumber: warehouse.RegisterNumber,
  381. }
  382. if orgID == 9671 || orgID == 10265 {
  383. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  384. flow.Price = goodsInfo.PackingPrice
  385. flow.BuyPrice = goodsInfo.BuyPrice
  386. }
  387. errThre := CreateStockFlowOne(flow)
  388. if errThre != nil {
  389. return errThre
  390. }
  391. }
  392. //退库
  393. if count < out_count-out_count_one {
  394. operation_time := time.Now().Unix()
  395. //创建退库单
  396. timeStr := time.Now().Format("2006-01-02")
  397. timeArr := strings.Split(timeStr, "-")
  398. total, _ := FindAllCancelStockTotal(orgID)
  399. total = total + 1
  400. orderNumber := "CKTKD" + strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  401. cancelStock := models.CancelStock{
  402. OrderNumber: orderNumber,
  403. OperaTime: operation_time,
  404. OrgId: orgID,
  405. Creater: warehouseOut.Creater,
  406. Ctime: time.Now().Unix(),
  407. Status: 1,
  408. ReturnTime: record_time,
  409. Type: 1,
  410. StorehouseId: goods.StorehouseId,
  411. IsCheck: 1,
  412. }
  413. _, msgerrkonde := GetCancelStockDetailByOrderNumberOne(record_time, orgID)
  414. if msgerrkonde == gorm.ErrRecordNotFound {
  415. AddSigleCancelStock(&cancelStock)
  416. }
  417. cancel, _ := GetLastCancelStockById(orgID)
  418. manufacturer, _ := GetManufactureById(warehouse.Manufacturer)
  419. deaerler, _ := GetDealerById(warehouse.Dealer)
  420. cancelStockInfo := models.CancelStockInfo{
  421. GoodId: goods.GoodId,
  422. CancelStockId: cancel.ID,
  423. GoodTypeId: goods.GoodTypeId,
  424. Count: out_count - out_count_one - count,
  425. Price: warehouse.PackingPrice,
  426. Total: 0,
  427. ProductDate: warehouse.ProductDate,
  428. ExpiryDate: warehouse.ExpiryDate,
  429. Ctime: time.Now().Unix(),
  430. Status: 1,
  431. OrgId: orgID,
  432. OrderNumber: cancel.OrderNumber,
  433. Type: 0,
  434. Dealer: deaerler.DealerName,
  435. Manufacturer: manufacturer.ManufacturerName,
  436. Number: warehouse.Number,
  437. RegisterAccount: "",
  438. Remark: "",
  439. WarehouseInfoId: warehouse.ID,
  440. PatientId: patient_id,
  441. RecordDate: record_time,
  442. StorehouseId: goods.StorehouseId,
  443. IsCheck: 1,
  444. }
  445. if orgID == 9671 || orgID == 10265 {
  446. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  447. cancelStockInfo.Price = goodsInfo.PackingPrice
  448. }
  449. CreateCancelStockInfoOne(&cancelStockInfo)
  450. cancelInfo, _ := GetLastCancelStockInfoByGoodId(goods.GoodId)
  451. flow := models.VmStockFlow{
  452. WarehousingId: warehouse.ID,
  453. GoodId: goods.GoodId,
  454. Number: warehouse.Number,
  455. LicenseNumber: warehouse.LicenseNumber,
  456. Count: out_count - count,
  457. UserOrgId: orgID,
  458. PatientId: patient_id,
  459. SystemTime: record_time,
  460. ConsumableType: 7,
  461. IsSys: 1,
  462. WarehousingOrder: "",
  463. WarehouseOutId: lastOut.WarehouseOutId,
  464. WarehouseOutOrderNumber: lastOut.WarehouseOutOrderNumber,
  465. IsEdit: 0,
  466. CancelStockId: cancel.ID,
  467. CancelOrderNumber: cancel.OrderNumber,
  468. Manufacturer: manufacturer.ID,
  469. Dealer: 0,
  470. Creator: warehouseOut.Creater,
  471. UpdateCreator: 0,
  472. Status: 1,
  473. Ctime: time.Now().Unix(),
  474. Mtime: 0,
  475. Price: warehouse.PackingPrice,
  476. WarehousingDetailId: warehouse.ID,
  477. WarehouseOutDetailId: lastOut.ID,
  478. CancelOutDetailId: cancelInfo.ID,
  479. ProductDate: warehouse.ProductDate,
  480. ExpireDate: warehouse.ExpiryDate,
  481. StorehouseId: goods.StorehouseId,
  482. BuyPrice: warehouse.Price,
  483. ProjectId: goods.ProjectId,
  484. OverCount: sum_count,
  485. RegisterNumber: warehouse.RegisterNumber,
  486. }
  487. if orgID == 9671 || orgID == 10265 {
  488. goodsInfo, _ := FindeGoodInfo(orgID, goods.ProjectId)
  489. flow.Price = goodsInfo.PackingPrice
  490. flow.BuyPrice = goodsInfo.BuyPrice
  491. }
  492. CreateStockFlowOne(flow)
  493. }
  494. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  495. goods.Count = deliver_number - stock_number
  496. ConsumablesGoodDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  497. }
  498. return nil
  499. }
  500. func FindWarehouseOutInfoByPatientId(patient_id int64, record_time int64, good_id int64, org_id int64) (models.WarehouseOutInfo, error) {
  501. outInfo := models.WarehouseOutInfo{}
  502. 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
  503. return outInfo, err
  504. }
  505. func FindWarehouseOutInfoByPatientIdTwo(patient_id int64, record_time int64, good_id int64, org_id int64, project_id int64) (models.WarehouseOutInfo, error) {
  506. outInfo := models.WarehouseOutInfo{}
  507. 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
  508. return outInfo, err
  509. }
  510. func UpdateDrugStockCount(drug_id int64, org_id int64, storehouse_id int64, flush_count int64) error {
  511. drug := models.XtDrugStockCount{}
  512. ut := XTWriteDB().Begin()
  513. 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
  514. if err != nil {
  515. ut.Rollback()
  516. return err
  517. }
  518. ut.Commit()
  519. return err
  520. }
  521. func AddDrugCount(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
  522. ut := XTWriteDB().Begin()
  523. 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
  524. if err != nil {
  525. ut.Rollback()
  526. return err
  527. }
  528. 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
  529. if err != nil {
  530. ut.Rollback()
  531. return err
  532. }
  533. ut.Commit()
  534. return err
  535. }
  536. func ReduceDrugCount(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
  537. ut := XTWriteDB().Begin()
  538. 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
  539. if err != nil {
  540. ut.Rollback()
  541. return err
  542. }
  543. 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
  544. if err != nil {
  545. ut.Rollback()
  546. return err
  547. }
  548. ut.Commit()
  549. return err
  550. }
  551. func ReduceDrugCountTwo(drug_id int64, org_id int64, storehouse_id int64, out_count int64) error {
  552. ut := XTWriteDB().Begin()
  553. 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
  554. if err != nil {
  555. ut.Rollback()
  556. return err
  557. }
  558. ut.Commit()
  559. return err
  560. }
  561. // 药品自动出库 递归方式
  562. func BloodDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  563. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  564. var deliver_number int64 = 0
  565. var stock_number int64 = 0
  566. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  567. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  568. var drug_price float64
  569. if advice.PrescribingNumberUnit == drup.MaxUnit {
  570. deliver_number = count * drup.MinNumber
  571. } else {
  572. deliver_number = count
  573. }
  574. if advice.PrescribingNumberUnit == drup.MaxUnit {
  575. drug_price = drup.RetailPrice
  576. }
  577. fmt.Println(drug_price)
  578. if advice.PrescribingNumberUnit == drup.MinUnit {
  579. drug_price = drup.MinPrice
  580. }
  581. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  582. drug_price = drup.RetailPrice
  583. }
  584. // 根据先进先出原则,查询最先入库的批次,进行出库
  585. // 如果没有对应的库存,则报错
  586. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  587. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  588. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  589. var stockMax int64
  590. var stockMin int64
  591. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  592. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  593. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  594. UpdateMinNumber(lastWarehouse.ID, stockMin)
  595. }
  596. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  597. if err != nil {
  598. return err
  599. }
  600. // 将该批次的剩余库存数量转换为拆零数量
  601. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  602. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  603. if stock_number >= deliver_number {
  604. var maxNumber int64 = 0
  605. var minNumber int64 = 0
  606. maxNumber = deliver_number / drup.MinNumber
  607. minNumber = deliver_number % drup.MinNumber
  608. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  609. minNumber = maxNumber
  610. }
  611. if drup.MaxUnit != drup.MinUnit {
  612. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  613. return errors.New("库存数量不足")
  614. }
  615. }
  616. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  617. if warehouse.StockMaxNumber < 0 {
  618. warehouse.StockMaxNumber = 0
  619. }
  620. if warehouse.StockMinNumber < 0 {
  621. warehouse.StockMinNumber = 0
  622. }
  623. warehouse.Mtime = time.Now().Unix()
  624. if warehouse.StockMinNumber < minNumber {
  625. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  626. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  627. } else {
  628. if minNumber > 0 {
  629. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  630. }
  631. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  632. if warehouse.StockMinNumber > 0 {
  633. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  634. }
  635. }
  636. }
  637. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  638. if (warehouse.StockMinNumber - deliver_number) > 0 {
  639. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  640. }
  641. }
  642. if drup.MaxUnit != drup.MinUnit {
  643. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  644. return errors.New("库存数量不足")
  645. }
  646. }
  647. if warehouse.StockMinNumber <= 0 {
  648. warehouse.StockMinNumber = 0
  649. }
  650. //扣减库存数据
  651. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  652. if errThree != nil {
  653. return errThree
  654. }
  655. //查询剩余库存
  656. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  657. var sum_count int64
  658. for _, its := range stockInfo {
  659. if its.MaxUnit == drup.MaxUnit {
  660. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  661. }
  662. sum_count += its.StockMaxNumber + its.StockMinNumber
  663. }
  664. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  665. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  666. WarehouseOutId: warehouseout.ID,
  667. Status: 1,
  668. Ctime: time.Now().Unix(),
  669. Remark: warehouse.Remark,
  670. OrgId: orgID,
  671. Type: 1,
  672. Manufacturer: warehouse.Manufacturer,
  673. Dealer: warehouse.Dealer,
  674. IsSys: 1,
  675. SysRecordTime: advice.RecordDate,
  676. DrugId: advice.DrugId,
  677. Number: warehouse.Number,
  678. BatchNumber: warehouse.BatchNumber,
  679. Price: warehouse.RetailPrice,
  680. CountUnit: drup.MinUnit,
  681. RetailPrice: warehouse.RetailPrice,
  682. ProductDate: warehouse.ProductDate,
  683. ExpiryDate: warehouse.ExpiryDate,
  684. PatientId: advice.PatientId,
  685. Count: deliver_number, //出最小单位
  686. WarehouseInfoId: warehouse.ID,
  687. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  688. StorehouseId: storeConfig.DrugStorehouseOut,
  689. IsCheck: 1,
  690. OverCount: sum_count,
  691. AdviceId: advice.ID,
  692. }
  693. //查询今日该药品该患者是否有出库数据
  694. lastDrugOutInfo, errThreeCode := GetNewDrugWarehouseOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId)
  695. if errThreeCode == gorm.ErrRecordNotFound {
  696. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  697. if errOne != nil {
  698. return errOne
  699. }
  700. }
  701. if errThreeCode == nil {
  702. errThree := UpdateSigleDrugWarehouseOutInfo(lastDrugOutInfo.ID, warehouseOutInfo)
  703. if errThree != nil {
  704. return errThree
  705. }
  706. }
  707. var out_count int64
  708. var cancel_count int64
  709. //查询出库数据
  710. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  711. //查询退库数据
  712. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  713. for _, item := range infoCountList {
  714. out_count += item.Count
  715. }
  716. for _, item := range cancelInfoList {
  717. cancel_count += item.Count
  718. }
  719. //如果本次出库数据大于历史出库数据 新增1条流水
  720. if deliver_number > (out_count - cancel_count) {
  721. drugflow := models.DrugFlow{
  722. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  723. WarehouseOutId: warehouseout.ID,
  724. DrugId: advice.DrugId,
  725. Number: warehouse.Number,
  726. ProductDate: warehouse.ProductDate,
  727. ExpireDate: warehouse.ExpiryDate,
  728. Count: deliver_number - (out_count - cancel_count), //按最小单位计算,
  729. Price: warehouse.RetailPrice,
  730. Status: 1,
  731. Ctime: time.Now().Unix(),
  732. UserOrgId: orgID,
  733. Manufacturer: warehouse.Manufacturer,
  734. Dealer: warehouse.Dealer,
  735. BatchNumber: warehouse.BatchNumber,
  736. MaxUnit: drup.MinUnit,
  737. ConsumableType: 3,
  738. IsEdit: 1,
  739. Creator: advice.ExecutionStaff,
  740. IsSys: 1,
  741. PatientId: advice.PatientId,
  742. SystemTime: advice.AdviceDate,
  743. WarehousingDetailId: warehouse.ID,
  744. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  745. StorehouseId: storeConfig.DrugStorehouseOut,
  746. WarehouseOutDetailId: lastDrugOutInfo.ID,
  747. OverCount: sum_count,
  748. AdviceId: advice.ID,
  749. LastPrice: warehouse.Price,
  750. }
  751. CreateDrugFlowOne(drugflow)
  752. //出库数量相加
  753. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  754. }
  755. //如果本次出库数据小于历史出库数据 新增1条退库流水
  756. if deliver_number < (out_count - cancel_count) {
  757. operation_time := time.Now().Unix()
  758. ctime := time.Now().Unix()
  759. timeStr := time.Now().Format("2006-01-02")
  760. timeArr := strings.Split(timeStr, "-")
  761. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  762. total = total + 1
  763. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  764. cancelStock := models.DrugCancelStock{
  765. OrderNumber: orderNumber,
  766. OperaTime: operation_time,
  767. OrgId: advice.UserOrgId,
  768. Creater: advice.ExecutionStaff,
  769. Ctime: ctime,
  770. Status: 1,
  771. ReturnTime: advice.AdviceDate,
  772. Dealer: 0,
  773. Manufacturer: 0,
  774. Type: 1,
  775. StorehouseId: storeConfig.DrugStorehouseOut,
  776. IsCheck: 1,
  777. }
  778. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  779. if errCode == gorm.ErrRecordNotFound {
  780. AddSigleDrugCancelStock(&cancelStock)
  781. }
  782. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  783. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  784. dealer, _ := GetDealerById(warehouse.Dealer)
  785. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  786. cancelStockInfo := models.DrugCancelStockInfo{
  787. OrderNumber: cancelInfo.OrderNumber,
  788. CancelStockId: lastDrugCancelStock.ID,
  789. DrugId: advice.DrugId,
  790. Count: (out_count - cancel_count) - deliver_number,
  791. Status: 1,
  792. Ctime: ctime,
  793. OrgId: advice.UserOrgId,
  794. Type: 1,
  795. Manufacturer: manufactureName.ManufacturerName,
  796. Dealer: dealer.DealerName,
  797. Total: 0,
  798. RetailPrice: warehouse.RetailPrice,
  799. RetailTotalPrice: warehouse.RetailTotalPrice,
  800. Price: warehouse.Price,
  801. RegisterAccount: "",
  802. Remark: warehouse.Remark,
  803. BatchNumber: warehouse.BatchNumber,
  804. MaxUnit: drup.MinUnit,
  805. ProductDate: warehouse.ProductDate,
  806. ExpiryDate: warehouse.ExpiryDate,
  807. BatchNumberId: warehouse.ID,
  808. StorehouseId: storeConfig.DrugStorehouseOut,
  809. IsCheck: 1,
  810. }
  811. CreatedCancelStock(cancelStockInfo)
  812. flow := models.DrugFlow{
  813. WarehousingId: warehouse.ID,
  814. DrugId: warehouse.DrugId,
  815. Number: "",
  816. BatchNumber: warehouse.BatchNumber,
  817. Count: (out_count - cancel_count) - deliver_number,
  818. UserOrgId: advice.UserOrgId,
  819. PatientId: advice.PatientId,
  820. SystemTime: advice.AdviceDate,
  821. ConsumableType: 7,
  822. IsSys: 1,
  823. WarehousingOrder: "",
  824. WarehouseOutId: 0,
  825. WarehouseOutOrderNumber: "",
  826. IsEdit: 0,
  827. CancelStockId: cancelStock.ID,
  828. CancelOrderNumber: cancelStock.OrderNumber,
  829. Manufacturer: warehouse.Manufacturer,
  830. Dealer: warehouse.Dealer,
  831. Creator: advice.ExecutionStaff,
  832. UpdateCreator: 0,
  833. Status: 1,
  834. Ctime: time.Now().Unix(),
  835. Mtime: 0,
  836. Price: warehouse.RetailPrice,
  837. WarehousingDetailId: warehouse.ID,
  838. WarehouseOutDetailId: 0,
  839. CancelOutDetailId: lastDrugCancelStock.ID,
  840. ExpireDate: warehouse.ExpiryDate,
  841. ProductDate: warehouse.ProductDate,
  842. MaxUnit: drup.MinUnit,
  843. MinUnit: "",
  844. AdviceId: advice.ID,
  845. StorehouseId: storeConfig.DrugStorehouseOut,
  846. LastPrice: warehouse.Price,
  847. }
  848. CreateDrugFlowOne(flow)
  849. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  850. //出库数量减
  851. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  852. }
  853. //查询是否存在数据
  854. details := &models.DrugAutomaticReduceDetail{
  855. WarehouseOutId: warehouseout.ID,
  856. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  857. PatientId: advice.PatientId,
  858. Ctime: time.Now().Unix(),
  859. Mtime: time.Now().Unix(),
  860. Status: 1,
  861. RecordTime: advice.RecordDate,
  862. OrgId: orgID,
  863. DrugId: advice.DrugId,
  864. Count: deliver_number,
  865. CountUnit: drup.MinUnit,
  866. WarehouseInfoId: warehouse.ID,
  867. StorehouseId: storeConfig.DrugStorehouseOut,
  868. AdviceId: advice.ID,
  869. }
  870. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  871. if errTwo != nil {
  872. return errTwo
  873. }
  874. return nil
  875. } else {
  876. info := models.XtDrugWarehouseInfo{
  877. ID: warehouse.ID,
  878. WarehousingId: warehouse.WarehousingId,
  879. DrugId: warehouse.DrugId,
  880. Number: warehouse.Number,
  881. ProductDate: warehouse.ProductDate,
  882. ExpiryDate: warehouse.ExpiryDate,
  883. WarehousingCount: warehouse.WarehousingCount,
  884. Price: warehouse.RetailPrice,
  885. TotalPrice: warehouse.TotalPrice,
  886. Dealer: warehouse.Dealer,
  887. Manufacturer: warehouse.Manufacturer,
  888. Remark: warehouse.Remark,
  889. Ctime: warehouse.Ctime,
  890. Mtime: warehouse.Mtime,
  891. Status: 1,
  892. OrgId: warehouse.OrgId,
  893. IsReturn: warehouse.IsReturn,
  894. WarehousingOrder: warehouse.WarehousingOrder,
  895. Type: warehouse.Type,
  896. RetailPrice: warehouse.RetailPrice,
  897. RetailTotalPrice: warehouse.RetailPrice,
  898. StockMaxNumber: 0,
  899. StockMinNumber: 0,
  900. BatchNumber: warehouse.BatchNumber,
  901. MaxUnit: warehouse.MaxUnit,
  902. WarehousingInfoId: warehouse.WarehousingInfoId,
  903. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  904. StorehouseId: storeConfig.DrugStorehouseOut,
  905. IsCheck: 1,
  906. }
  907. //扣减库存
  908. errThree := UpDateDrugWarehouseInfoByStock(&info)
  909. if errThree != nil {
  910. return errThree
  911. }
  912. //查询剩余库存
  913. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  914. var sum_count int64
  915. for _, its := range stockInfo {
  916. if its.MaxUnit == drup.MaxUnit {
  917. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  918. }
  919. sum_count += its.StockMaxNumber + its.StockMinNumber
  920. }
  921. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  922. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  923. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  924. WarehouseOutId: warehouseout.ID,
  925. Status: 1,
  926. Ctime: time.Now().Unix(),
  927. Remark: warehouse.Remark,
  928. OrgId: orgID,
  929. Type: 1,
  930. Manufacturer: warehouse.Manufacturer,
  931. Dealer: warehouse.Dealer,
  932. IsSys: 1,
  933. SysRecordTime: advice.RecordDate,
  934. DrugId: advice.DrugId,
  935. Number: warehouse.Number,
  936. BatchNumber: warehouse.BatchNumber,
  937. Price: warehouse.RetailPrice,
  938. CountUnit: drup.MinUnit,
  939. ProductDate: warehouse.ProductDate,
  940. ExpiryDate: warehouse.ExpiryDate,
  941. PatientId: advice.PatientId,
  942. Count: stock_number,
  943. WarehouseInfoId: warehouse.ID,
  944. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  945. StorehouseId: storeConfig.DrugStorehouseOut,
  946. IsCheck: 1,
  947. OverCount: sum_count,
  948. AdviceId: advice.ID,
  949. }
  950. //查询是否存在出库数据
  951. _, errCode := GetSigleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, advice.ID)
  952. if errCode == gorm.ErrRecordNotFound {
  953. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  954. if errOne != nil {
  955. return errOne
  956. }
  957. }
  958. if errCode == nil {
  959. UpdateSingleDrugWarehouseOutInfo(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo)
  960. }
  961. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  962. details := &models.DrugAutomaticReduceDetail{
  963. WarehouseOutId: warehouseout.ID,
  964. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  965. PatientId: advice.PatientId,
  966. Ctime: time.Now().Unix(),
  967. Mtime: time.Now().Unix(),
  968. Status: 1,
  969. RecordTime: advice.RecordDate,
  970. OrgId: orgID,
  971. DrugId: advice.DrugId,
  972. Count: stock_number,
  973. CountUnit: drup.MinUnit,
  974. WarehouseInfoId: warehouse.ID,
  975. StorehouseId: storeConfig.DrugStorehouseOut,
  976. AdviceId: advice.ID,
  977. }
  978. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  979. if errcodeThree == gorm.ErrRecordNotFound {
  980. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  981. if errTwo != nil {
  982. return errTwo
  983. }
  984. }
  985. if errcodeThree == nil {
  986. UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  987. }
  988. var out_count int64
  989. var cancel_count int64
  990. //查询出库数据
  991. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  992. //查询退库数据
  993. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  994. for _, item := range infoCountList {
  995. out_count += item.Count
  996. }
  997. for _, item := range cancelInfoList {
  998. cancel_count += item.Count
  999. }
  1000. //如果本次出库数据大于历史出库数据 新增1条流水
  1001. if deliver_number > (out_count - cancel_count) {
  1002. drugflow := models.DrugFlow{
  1003. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1004. WarehouseOutId: warehouseout.ID,
  1005. DrugId: advice.DrugId,
  1006. Number: warehouse.Number,
  1007. ProductDate: warehouse.ProductDate,
  1008. ExpireDate: warehouse.ExpiryDate,
  1009. Count: stock_number,
  1010. Price: warehouse.RetailPrice,
  1011. Status: 1,
  1012. Ctime: time.Now().Unix(),
  1013. UserOrgId: orgID,
  1014. Manufacturer: warehouse.Manufacturer,
  1015. Dealer: warehouse.Dealer,
  1016. BatchNumber: warehouse.BatchNumber,
  1017. MaxUnit: drup.MinUnit,
  1018. ConsumableType: 3,
  1019. IsEdit: 1,
  1020. Creator: advice.ExecutionStaff,
  1021. IsSys: 1,
  1022. PatientId: advice.PatientId,
  1023. WarehousingDetailId: warehouse.ID,
  1024. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1025. StorehouseId: storeConfig.DrugStorehouseOut,
  1026. WarehouseOutDetailId: lastDrugOutInfo.ID,
  1027. OverCount: sum_count,
  1028. AdviceId: advice.ID,
  1029. }
  1030. CreateDrugFlowOne(drugflow)
  1031. //出库数量相加
  1032. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  1033. }
  1034. if deliver_number < (out_count - cancel_count) {
  1035. operation_time := time.Now().Unix()
  1036. ctime := time.Now().Unix()
  1037. timeStr := time.Now().Format("2006-01-02")
  1038. timeArr := strings.Split(timeStr, "-")
  1039. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  1040. total = total + 1
  1041. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1042. cancelStock := models.DrugCancelStock{
  1043. OrderNumber: orderNumber,
  1044. OperaTime: operation_time,
  1045. OrgId: advice.UserOrgId,
  1046. Creater: advice.ExecutionStaff,
  1047. Ctime: ctime,
  1048. Status: 1,
  1049. ReturnTime: advice.AdviceDate,
  1050. Dealer: 0,
  1051. Manufacturer: 0,
  1052. Type: 1,
  1053. StorehouseId: storeConfig.DrugStorehouseOut,
  1054. IsCheck: 1,
  1055. }
  1056. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1057. if errCode == gorm.ErrRecordNotFound {
  1058. AddSigleDrugCancelStock(&cancelStock)
  1059. }
  1060. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1061. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  1062. dealer, _ := GetDealerById(warehouse.Dealer)
  1063. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  1064. cancelStockInfo := models.DrugCancelStockInfo{
  1065. OrderNumber: cancelInfo.OrderNumber,
  1066. CancelStockId: lastDrugCancelStock.ID,
  1067. DrugId: advice.DrugId,
  1068. Count: (out_count - cancel_count) - deliver_number,
  1069. Status: 1,
  1070. Ctime: ctime,
  1071. OrgId: advice.UserOrgId,
  1072. Type: 1,
  1073. Manufacturer: manufactureName.ManufacturerName,
  1074. Dealer: dealer.DealerName,
  1075. Total: 0,
  1076. RetailPrice: warehouse.RetailPrice,
  1077. RetailTotalPrice: warehouse.RetailTotalPrice,
  1078. Price: warehouse.Price,
  1079. RegisterAccount: "",
  1080. Remark: warehouse.Remark,
  1081. BatchNumber: warehouse.BatchNumber,
  1082. MaxUnit: drup.MinUnit,
  1083. ProductDate: warehouse.ProductDate,
  1084. ExpiryDate: warehouse.ExpiryDate,
  1085. BatchNumberId: warehouse.ID,
  1086. StorehouseId: storeConfig.DrugStorehouseOut,
  1087. IsCheck: 1,
  1088. }
  1089. CreatedCancelStock(cancelStockInfo)
  1090. flow := models.DrugFlow{
  1091. WarehousingId: warehouse.ID,
  1092. DrugId: warehouse.DrugId,
  1093. Number: "",
  1094. BatchNumber: warehouse.BatchNumber,
  1095. Count: (out_count - cancel_count) - deliver_number,
  1096. UserOrgId: advice.UserOrgId,
  1097. PatientId: advice.PatientId,
  1098. SystemTime: advice.AdviceDate,
  1099. ConsumableType: 7,
  1100. IsSys: 1,
  1101. WarehousingOrder: "",
  1102. WarehouseOutId: 0,
  1103. WarehouseOutOrderNumber: "",
  1104. IsEdit: 0,
  1105. CancelStockId: cancelStock.ID,
  1106. CancelOrderNumber: cancelStock.OrderNumber,
  1107. Manufacturer: warehouse.Manufacturer,
  1108. Dealer: warehouse.Dealer,
  1109. Creator: advice.ExecutionStaff,
  1110. UpdateCreator: 0,
  1111. Status: 1,
  1112. Ctime: time.Now().Unix(),
  1113. Mtime: 0,
  1114. Price: warehouse.Price,
  1115. WarehousingDetailId: warehouse.ID,
  1116. WarehouseOutDetailId: 0,
  1117. CancelOutDetailId: lastDrugCancelStock.ID,
  1118. ExpireDate: warehouse.ExpiryDate,
  1119. ProductDate: warehouse.ProductDate,
  1120. MaxUnit: drup.MinUnit,
  1121. MinUnit: "",
  1122. AdviceId: advice.ID,
  1123. StorehouseId: storeConfig.DrugStorehouseOut,
  1124. }
  1125. CreateDrugFlowOne(flow)
  1126. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  1127. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  1128. }
  1129. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1130. prescribingNumber_two_temp := deliver_number - stock_number
  1131. overPlusNumber := float64(prescribingNumber_two_temp)
  1132. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  1133. advice.PrescribingNumberUnit = drup.MinUnit
  1134. BloodDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  1135. }
  1136. return
  1137. }
  1138. // 药品自动出库 递归方式
  1139. func BloodHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  1140. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1141. var deliver_number int64 = 0
  1142. var stock_number int64 = 0
  1143. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  1144. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1145. var drug_price float64
  1146. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1147. deliver_number = count * drup.MinNumber
  1148. } else {
  1149. deliver_number = count
  1150. }
  1151. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1152. drug_price = drup.RetailPrice
  1153. }
  1154. fmt.Println(drug_price)
  1155. if advice.PrescribingNumberUnit == drup.MinUnit {
  1156. drug_price = drup.MinPrice
  1157. }
  1158. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  1159. drug_price = drup.RetailPrice
  1160. }
  1161. // 根据先进先出原则,查询最先入库的批次,进行出库
  1162. // 如果没有对应的库存,则报错
  1163. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  1164. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1165. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  1166. var stockMax int64
  1167. var stockMin int64
  1168. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  1169. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  1170. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  1171. UpdateMinNumber(lastWarehouse.ID, stockMin)
  1172. }
  1173. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1174. if err != nil {
  1175. return err
  1176. }
  1177. // 将该批次的剩余库存数量转换为拆零数量
  1178. // 将该批次的剩余库存数量转换为拆零数量
  1179. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1180. //fmt.Println("进来1")
  1181. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1182. //fmt.Println("剩余库存1", stock_number)
  1183. }
  1184. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  1185. //fmt.Println("进来2")
  1186. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  1187. //fmt.Println("剩余库存2", stock_number)
  1188. }
  1189. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1190. //fmt.Println("进来3")
  1191. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1192. //fmt.Println("剩余库存3", stock_number)
  1193. }
  1194. //fmt.Println("剩余库存23333333333333333333333333333wo", stock_number)
  1195. //fmt.Println("出库数量2333333333333333333333333333333333333wo", deliver_number)
  1196. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1197. if stock_number >= deliver_number {
  1198. var maxNumber int64 = 0
  1199. var minNumber int64 = 0
  1200. maxNumber = deliver_number / drup.MinNumber
  1201. minNumber = deliver_number % drup.MinNumber
  1202. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1203. minNumber = maxNumber
  1204. }
  1205. if drup.MaxUnit != drup.MinUnit {
  1206. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  1207. return errors.New("库存数量不足")
  1208. }
  1209. }
  1210. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1211. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  1212. }
  1213. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1214. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1215. }
  1216. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1217. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1218. }
  1219. if warehouse.StockMaxNumber < 0 {
  1220. warehouse.StockMaxNumber = 0
  1221. }
  1222. if warehouse.StockMinNumber < 0 {
  1223. warehouse.StockMinNumber = 0
  1224. }
  1225. warehouse.Mtime = time.Now().Unix()
  1226. fmt.Println("minnU目标而332322323323233223323223我", minNumber)
  1227. fmt.Println("max_number232232332232322wo", maxNumber)
  1228. fmt.Println(" warehouse.StockMinNumber", warehouse.StockMinNumber)
  1229. if warehouse.StockMinNumber < minNumber {
  1230. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1231. if warehouse.MaxUnit != warehouse.MinUnit {
  1232. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  1233. }
  1234. } else {
  1235. if minNumber > 0 {
  1236. if (warehouse.StockMinNumber - minNumber) >= 0 {
  1237. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  1238. }
  1239. }
  1240. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  1241. if warehouse.StockMinNumber > 0 {
  1242. if warehouse.StockMinNumber-deliver_number >= 0 {
  1243. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  1244. }
  1245. }
  1246. }
  1247. }
  1248. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  1249. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  1250. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  1251. }
  1252. }
  1253. if drup.MaxUnit != drup.MinUnit {
  1254. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  1255. return errors.New("库存数量不足")
  1256. }
  1257. }
  1258. if warehouse.StockMinNumber <= 0 {
  1259. warehouse.StockMinNumber = 0
  1260. }
  1261. //扣减库存数据
  1262. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  1263. if errThree != nil {
  1264. return errThree
  1265. }
  1266. //查询剩余库存
  1267. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  1268. var sum_count int64
  1269. for _, its := range stockInfo {
  1270. if its.MaxUnit == drup.MaxUnit {
  1271. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  1272. }
  1273. sum_count += its.StockMaxNumber + its.StockMinNumber
  1274. }
  1275. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1276. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1277. WarehouseOutId: warehouseout.ID,
  1278. Status: 1,
  1279. Ctime: time.Now().Unix(),
  1280. Remark: warehouse.Remark,
  1281. OrgId: orgID,
  1282. Type: 1,
  1283. Manufacturer: warehouse.Manufacturer,
  1284. Dealer: warehouse.Dealer,
  1285. IsSys: 1,
  1286. SysRecordTime: advice.RecordDate,
  1287. DrugId: advice.DrugId,
  1288. Number: warehouse.Number,
  1289. BatchNumber: warehouse.BatchNumber,
  1290. Price: warehouse.RetailPrice,
  1291. CountUnit: drup.MinUnit,
  1292. RetailPrice: warehouse.RetailPrice,
  1293. ProductDate: warehouse.ProductDate,
  1294. ExpiryDate: warehouse.ExpiryDate,
  1295. PatientId: advice.PatientId,
  1296. Count: deliver_number, //出最小单位
  1297. WarehouseInfoId: warehouse.ID,
  1298. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1299. StorehouseId: storeConfig.DrugStorehouseOut,
  1300. IsCheck: 1,
  1301. OverCount: sum_count,
  1302. AdviceId: advice.ID,
  1303. }
  1304. //查询今日该药品该患者是否有出库数据
  1305. lastDrugOutInfo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  1306. if lastDrugOutInfo.ID == 0 {
  1307. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1308. if errOne != nil {
  1309. return errOne
  1310. }
  1311. }
  1312. if lastDrugOutInfo.ID > 0 {
  1313. var his_count int64
  1314. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  1315. his_count = lastDrugOutInfo.Count * drup.MinNumber
  1316. }
  1317. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  1318. his_count = lastDrugOutInfo.Count
  1319. }
  1320. if deliver_number != his_count {
  1321. errThree := UpdateSigleDrugWarehouseOutInfo(lastDrugOutInfo.ID, warehouseOutInfo)
  1322. if errThree != nil {
  1323. return errThree
  1324. }
  1325. }
  1326. }
  1327. var out_count int64
  1328. var cancel_count int64
  1329. //查询出库数据
  1330. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1331. //查询退库数据
  1332. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1333. for _, item := range infoCountList {
  1334. out_count += item.Count
  1335. }
  1336. for _, item := range cancelInfoList {
  1337. cancel_count += item.Count
  1338. }
  1339. fmt.Println("deliver_number", deliver_number)
  1340. fmt.Println("out_count", out_count)
  1341. fmt.Println("cancel_count", cancel_count)
  1342. //如果本次出库数据大于历史出库数据 新增1条流水
  1343. if deliver_number > (out_count - cancel_count) {
  1344. drugflow := models.DrugFlow{
  1345. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1346. WarehouseOutId: warehouseout.ID,
  1347. DrugId: advice.DrugId,
  1348. Number: warehouse.Number,
  1349. ProductDate: warehouse.ProductDate,
  1350. ExpireDate: warehouse.ExpiryDate,
  1351. Count: deliver_number - (out_count - cancel_count), //按最小单位计算,
  1352. Price: warehouse.RetailPrice,
  1353. Status: 1,
  1354. Ctime: time.Now().Unix(),
  1355. UserOrgId: orgID,
  1356. Manufacturer: warehouse.Manufacturer,
  1357. Dealer: warehouse.Dealer,
  1358. BatchNumber: warehouse.BatchNumber,
  1359. MaxUnit: drup.MinUnit,
  1360. ConsumableType: 3,
  1361. IsEdit: 1,
  1362. Creator: advice.ExecutionStaff,
  1363. IsSys: 1,
  1364. PatientId: advice.PatientId,
  1365. SystemTime: advice.AdviceDate,
  1366. WarehousingDetailId: warehouse.ID,
  1367. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1368. StorehouseId: storeConfig.DrugStorehouseOut,
  1369. WarehouseOutDetailId: lastDrugOutInfo.ID,
  1370. OverCount: sum_count,
  1371. AdviceId: advice.ID,
  1372. LastPrice: warehouse.Price,
  1373. }
  1374. CreateDrugFlowOne(drugflow)
  1375. //出库数量相加
  1376. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  1377. }
  1378. //如果本次出库数据小于历史出库数据 新增1条退库流水
  1379. if deliver_number < (out_count - cancel_count) {
  1380. operation_time := time.Now().Unix()
  1381. ctime := time.Now().Unix()
  1382. timeStr := time.Now().Format("2006-01-02")
  1383. timeArr := strings.Split(timeStr, "-")
  1384. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  1385. total = total + 1
  1386. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1387. cancelStock := models.DrugCancelStock{
  1388. OrderNumber: orderNumber,
  1389. OperaTime: operation_time,
  1390. OrgId: advice.UserOrgId,
  1391. Creater: advice.ExecutionStaff,
  1392. Ctime: ctime,
  1393. Status: 1,
  1394. ReturnTime: advice.AdviceDate,
  1395. Dealer: 0,
  1396. Manufacturer: 0,
  1397. Type: 1,
  1398. StorehouseId: storeConfig.DrugStorehouseOut,
  1399. IsCheck: 1,
  1400. }
  1401. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1402. if errCode == gorm.ErrRecordNotFound {
  1403. AddSigleDrugCancelStock(&cancelStock)
  1404. }
  1405. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1406. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  1407. dealer, _ := GetDealerById(warehouse.Dealer)
  1408. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  1409. cancelStockInfo := models.DrugCancelStockInfo{
  1410. OrderNumber: cancelInfo.OrderNumber,
  1411. CancelStockId: lastDrugCancelStock.ID,
  1412. DrugId: advice.DrugId,
  1413. Count: (out_count - cancel_count) - deliver_number,
  1414. Status: 1,
  1415. Ctime: ctime,
  1416. OrgId: advice.UserOrgId,
  1417. Type: 1,
  1418. Manufacturer: manufactureName.ManufacturerName,
  1419. Dealer: dealer.DealerName,
  1420. Total: 0,
  1421. RetailPrice: warehouse.RetailPrice,
  1422. RetailTotalPrice: warehouse.RetailTotalPrice,
  1423. Price: warehouse.Price,
  1424. RegisterAccount: "",
  1425. Remark: warehouse.Remark,
  1426. BatchNumber: warehouse.BatchNumber,
  1427. MaxUnit: drup.MinUnit,
  1428. ProductDate: warehouse.ProductDate,
  1429. ExpiryDate: warehouse.ExpiryDate,
  1430. BatchNumberId: warehouse.ID,
  1431. StorehouseId: storeConfig.DrugStorehouseOut,
  1432. IsCheck: 1,
  1433. }
  1434. CreatedCancelStock(cancelStockInfo)
  1435. flow := models.DrugFlow{
  1436. WarehousingId: warehouse.ID,
  1437. DrugId: warehouse.DrugId,
  1438. Number: "",
  1439. BatchNumber: warehouse.BatchNumber,
  1440. Count: out_count - cancel_count - deliver_number,
  1441. UserOrgId: advice.UserOrgId,
  1442. PatientId: advice.PatientId,
  1443. SystemTime: advice.AdviceDate,
  1444. ConsumableType: 7,
  1445. IsSys: 0,
  1446. WarehousingOrder: "",
  1447. WarehouseOutId: 0,
  1448. WarehouseOutOrderNumber: "",
  1449. IsEdit: 0,
  1450. CancelStockId: cancelStock.ID,
  1451. CancelOrderNumber: cancelStock.OrderNumber,
  1452. Manufacturer: warehouse.Manufacturer,
  1453. Dealer: warehouse.Dealer,
  1454. Creator: advice.ExecutionStaff,
  1455. UpdateCreator: 0,
  1456. Status: 1,
  1457. Ctime: time.Now().Unix(),
  1458. Mtime: 0,
  1459. Price: warehouse.RetailPrice,
  1460. WarehousingDetailId: warehouse.ID,
  1461. WarehouseOutDetailId: 0,
  1462. CancelOutDetailId: lastDrugCancelStock.ID,
  1463. ExpireDate: warehouse.ExpiryDate,
  1464. ProductDate: warehouse.ProductDate,
  1465. MaxUnit: drup.MinUnit,
  1466. MinUnit: "",
  1467. AdviceId: advice.ID,
  1468. StorehouseId: storeConfig.DrugStorehouseOut,
  1469. LastPrice: warehouse.Price,
  1470. OverCount: sum_count,
  1471. }
  1472. CreateDrugFlowOne(flow)
  1473. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  1474. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  1475. }
  1476. //查询是否存在数据
  1477. details := &models.DrugAutomaticReduceDetail{
  1478. WarehouseOutId: warehouseout.ID,
  1479. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1480. PatientId: advice.PatientId,
  1481. Ctime: time.Now().Unix(),
  1482. Mtime: time.Now().Unix(),
  1483. Status: 1,
  1484. RecordTime: advice.RecordDate,
  1485. OrgId: orgID,
  1486. DrugId: advice.DrugId,
  1487. Count: deliver_number,
  1488. CountUnit: drup.MinUnit,
  1489. WarehouseInfoId: warehouse.ID,
  1490. StorehouseId: storeConfig.DrugStorehouseOut,
  1491. AdviceId: advice.ID,
  1492. }
  1493. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1494. if errTwo != nil {
  1495. return errTwo
  1496. }
  1497. return nil
  1498. } else {
  1499. info := models.XtDrugWarehouseInfo{
  1500. ID: warehouse.ID,
  1501. WarehousingId: warehouse.WarehousingId,
  1502. DrugId: warehouse.DrugId,
  1503. Number: warehouse.Number,
  1504. ProductDate: warehouse.ProductDate,
  1505. ExpiryDate: warehouse.ExpiryDate,
  1506. WarehousingCount: warehouse.WarehousingCount,
  1507. Price: warehouse.RetailPrice,
  1508. TotalPrice: warehouse.TotalPrice,
  1509. Dealer: warehouse.Dealer,
  1510. Manufacturer: warehouse.Manufacturer,
  1511. Remark: warehouse.Remark,
  1512. Ctime: warehouse.Ctime,
  1513. Mtime: warehouse.Mtime,
  1514. Status: 1,
  1515. OrgId: warehouse.OrgId,
  1516. IsReturn: warehouse.IsReturn,
  1517. WarehousingOrder: warehouse.WarehousingOrder,
  1518. Type: warehouse.Type,
  1519. RetailPrice: warehouse.RetailPrice,
  1520. RetailTotalPrice: warehouse.RetailPrice,
  1521. StockMaxNumber: 0,
  1522. StockMinNumber: 0,
  1523. BatchNumber: warehouse.BatchNumber,
  1524. MaxUnit: warehouse.MaxUnit,
  1525. WarehousingInfoId: warehouse.WarehousingInfoId,
  1526. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1527. StorehouseId: storeConfig.DrugStorehouseOut,
  1528. IsCheck: 1,
  1529. }
  1530. //扣减库存
  1531. errThree := UpDateDrugWarehouseInfoByStock(&info)
  1532. if errThree != nil {
  1533. return errThree
  1534. }
  1535. //查询剩余库存
  1536. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  1537. var sum_count int64
  1538. for _, its := range stockInfo {
  1539. if its.MaxUnit == drup.MaxUnit {
  1540. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  1541. }
  1542. sum_count += its.StockMaxNumber + its.StockMinNumber
  1543. }
  1544. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1545. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  1546. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1547. WarehouseOutId: warehouseout.ID,
  1548. Status: 1,
  1549. Ctime: time.Now().Unix(),
  1550. Remark: warehouse.Remark,
  1551. OrgId: orgID,
  1552. Type: 1,
  1553. Manufacturer: warehouse.Manufacturer,
  1554. Dealer: warehouse.Dealer,
  1555. IsSys: 1,
  1556. SysRecordTime: advice.RecordDate,
  1557. DrugId: advice.DrugId,
  1558. Number: warehouse.Number,
  1559. BatchNumber: warehouse.BatchNumber,
  1560. Price: warehouse.RetailPrice,
  1561. CountUnit: drup.MinUnit,
  1562. ProductDate: warehouse.ProductDate,
  1563. ExpiryDate: warehouse.ExpiryDate,
  1564. PatientId: advice.PatientId,
  1565. Count: stock_number,
  1566. WarehouseInfoId: warehouse.ID,
  1567. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1568. StorehouseId: storeConfig.DrugStorehouseOut,
  1569. IsCheck: 1,
  1570. OverCount: sum_count,
  1571. AdviceId: advice.ID,
  1572. }
  1573. //查询是否存在出库数据
  1574. _, errCode := GetSigleDrugWarehouseOutInfo(advice.PatientId, advice.AdviceDate, orgID)
  1575. if errCode == gorm.ErrRecordNotFound {
  1576. fmt.Println("j你俩3233223323232323233wo")
  1577. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  1578. if errOne != nil {
  1579. return errOne
  1580. }
  1581. }
  1582. if errCode == nil {
  1583. lastDrugOutInfo, _ := GetSigleDrugWarehouseOutInfoOne(advice.PatientId, advice.AdviceDate, orgID)
  1584. var his_count int64
  1585. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  1586. his_count = lastDrugOutInfo.Count * drup.MinNumber
  1587. }
  1588. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  1589. his_count = lastDrugOutInfo.Count
  1590. }
  1591. if deliver_number != his_count {
  1592. errThree := UpdateSingleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo, advice.ID)
  1593. if errThree != nil {
  1594. return errThree
  1595. }
  1596. }
  1597. }
  1598. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  1599. details := &models.DrugAutomaticReduceDetail{
  1600. WarehouseOutId: warehouseout.ID,
  1601. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1602. PatientId: advice.PatientId,
  1603. Ctime: time.Now().Unix(),
  1604. Mtime: time.Now().Unix(),
  1605. Status: 1,
  1606. RecordTime: advice.RecordDate,
  1607. OrgId: orgID,
  1608. DrugId: advice.DrugId,
  1609. Count: stock_number,
  1610. CountUnit: drup.MinUnit,
  1611. WarehouseInfoId: warehouse.ID,
  1612. StorehouseId: storeConfig.DrugStorehouseOut,
  1613. AdviceId: advice.ID,
  1614. }
  1615. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  1616. if errcodeThree == gorm.ErrRecordNotFound {
  1617. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  1618. if errTwo != nil {
  1619. return errTwo
  1620. }
  1621. }
  1622. if errcodeThree == nil {
  1623. UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  1624. }
  1625. var out_count int64
  1626. var cancel_count int64
  1627. //查询出库数据
  1628. infoCountList, _ := FindDrugWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1629. //查询退库数据
  1630. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  1631. for _, item := range infoCountList {
  1632. out_count += item.Count
  1633. }
  1634. for _, item := range cancelInfoList {
  1635. cancel_count += item.Count
  1636. }
  1637. //如果本次出库数据大于历史出库数据 新增1条流水
  1638. if deliver_number > (out_count - cancel_count) {
  1639. drugflow := models.DrugFlow{
  1640. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  1641. WarehouseOutId: warehouseout.ID,
  1642. DrugId: advice.DrugId,
  1643. Number: warehouse.Number,
  1644. ProductDate: warehouse.ProductDate,
  1645. ExpireDate: warehouse.ExpiryDate,
  1646. Count: stock_number,
  1647. Price: warehouse.RetailPrice,
  1648. Status: 1,
  1649. Ctime: time.Now().Unix(),
  1650. UserOrgId: orgID,
  1651. Manufacturer: warehouse.Manufacturer,
  1652. Dealer: warehouse.Dealer,
  1653. BatchNumber: warehouse.BatchNumber,
  1654. MaxUnit: drup.MinUnit,
  1655. ConsumableType: 3,
  1656. IsEdit: 1,
  1657. Creator: advice.ExecutionStaff,
  1658. IsSys: 1,
  1659. PatientId: advice.PatientId,
  1660. WarehousingDetailId: warehouse.ID,
  1661. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  1662. StorehouseId: storeConfig.DrugStorehouseOut,
  1663. WarehouseOutDetailId: lastDrugOutInfo.ID,
  1664. OverCount: sum_count,
  1665. AdviceId: advice.ID,
  1666. LastPrice: warehouse.Price,
  1667. SystemTime: advice.AdviceDate,
  1668. }
  1669. CreateDrugFlowOne(drugflow)
  1670. //出库数量相加
  1671. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  1672. }
  1673. fmt.Println("libai", out_count)
  1674. fmt.Println("cancel", cancel_count)
  1675. fmt.Println("deliver_number", deliver_number)
  1676. if deliver_number < (out_count - cancel_count) {
  1677. operation_time := time.Now().Unix()
  1678. ctime := time.Now().Unix()
  1679. timeStr := time.Now().Format("2006-01-02")
  1680. timeArr := strings.Split(timeStr, "-")
  1681. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  1682. total = total + 1
  1683. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1684. cancelStock := models.DrugCancelStock{
  1685. OrderNumber: orderNumber,
  1686. OperaTime: operation_time,
  1687. OrgId: advice.UserOrgId,
  1688. Creater: advice.ExecutionStaff,
  1689. Ctime: ctime,
  1690. Status: 1,
  1691. ReturnTime: advice.AdviceDate,
  1692. Dealer: 0,
  1693. Manufacturer: 0,
  1694. Type: 1,
  1695. StorehouseId: storeConfig.DrugStorehouseOut,
  1696. IsCheck: 1,
  1697. }
  1698. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1699. if errCode == gorm.ErrRecordNotFound {
  1700. AddSigleDrugCancelStock(&cancelStock)
  1701. }
  1702. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1703. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  1704. dealer, _ := GetDealerById(warehouse.Dealer)
  1705. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  1706. cancelStockInfo := models.DrugCancelStockInfo{
  1707. OrderNumber: cancelInfo.OrderNumber,
  1708. CancelStockId: lastDrugCancelStock.ID,
  1709. DrugId: advice.DrugId,
  1710. Count: (out_count - cancel_count) - deliver_number,
  1711. Status: 1,
  1712. Ctime: ctime,
  1713. OrgId: advice.UserOrgId,
  1714. Type: 1,
  1715. Manufacturer: manufactureName.ManufacturerName,
  1716. Dealer: dealer.DealerName,
  1717. Total: 0,
  1718. RetailPrice: warehouse.RetailPrice,
  1719. RetailTotalPrice: warehouse.RetailTotalPrice,
  1720. Price: warehouse.Price,
  1721. RegisterAccount: "",
  1722. Remark: warehouse.Remark,
  1723. BatchNumber: warehouse.BatchNumber,
  1724. MaxUnit: drup.MinUnit,
  1725. ProductDate: warehouse.ProductDate,
  1726. ExpiryDate: warehouse.ExpiryDate,
  1727. BatchNumberId: warehouse.ID,
  1728. StorehouseId: storeConfig.DrugStorehouseOut,
  1729. IsCheck: 1,
  1730. }
  1731. CreatedCancelStock(cancelStockInfo)
  1732. flow := models.DrugFlow{
  1733. WarehousingId: warehouse.ID,
  1734. DrugId: warehouse.DrugId,
  1735. Number: "",
  1736. BatchNumber: warehouse.BatchNumber,
  1737. Count: out_count - cancel_count - deliver_number,
  1738. UserOrgId: advice.UserOrgId,
  1739. PatientId: advice.PatientId,
  1740. SystemTime: advice.AdviceDate,
  1741. ConsumableType: 7,
  1742. IsSys: 0,
  1743. WarehousingOrder: "",
  1744. WarehouseOutId: 0,
  1745. WarehouseOutOrderNumber: "",
  1746. IsEdit: 0,
  1747. CancelStockId: cancelStock.ID,
  1748. CancelOrderNumber: cancelStock.OrderNumber,
  1749. Manufacturer: warehouse.Manufacturer,
  1750. Dealer: warehouse.Dealer,
  1751. Creator: advice.ExecutionStaff,
  1752. UpdateCreator: 0,
  1753. Status: 1,
  1754. Ctime: time.Now().Unix(),
  1755. Mtime: 0,
  1756. Price: warehouse.RetailPrice,
  1757. WarehousingDetailId: warehouse.ID,
  1758. WarehouseOutDetailId: 0,
  1759. CancelOutDetailId: lastDrugCancelStock.ID,
  1760. ExpireDate: warehouse.ExpiryDate,
  1761. ProductDate: warehouse.ProductDate,
  1762. MaxUnit: drup.MinUnit,
  1763. MinUnit: "",
  1764. AdviceId: advice.ID,
  1765. StorehouseId: storeConfig.DrugStorehouseOut,
  1766. LastPrice: warehouse.Price,
  1767. OverCount: sum_count,
  1768. }
  1769. CreateDrugFlowOne(flow)
  1770. //退库数量增加
  1771. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  1772. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  1773. }
  1774. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1775. prescribingNumber_two_temp := deliver_number - stock_number
  1776. overPlusNumber := float64(prescribingNumber_two_temp)
  1777. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  1778. advice.PrescribingNumberUnit = drup.MinUnit
  1779. BloodHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  1780. }
  1781. return
  1782. }
  1783. func GetSigleDrugWarehouseOutInfoSix(patient_id int64, advice_date int64, orgid int64, advice_id int64) (*models.XtDrugWarehouseOutInfo, error) {
  1784. info := models.XtDrugWarehouseOutInfo{}
  1785. var err error
  1786. 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
  1787. if err == gorm.ErrRecordNotFound {
  1788. return nil, err
  1789. }
  1790. if err != nil {
  1791. return nil, err
  1792. }
  1793. return &info, nil
  1794. }
  1795. func GetSigleDrugWarehouseOutInfo(patient_id int64, advice_date int64, orgid int64) (*models.XtDrugWarehouseOutInfo, error) {
  1796. info := models.XtDrugWarehouseOutInfo{}
  1797. var err error
  1798. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", patient_id, advice_date, orgid).Find(&info).Error
  1799. if err == gorm.ErrRecordNotFound {
  1800. return nil, err
  1801. }
  1802. if err != nil {
  1803. return nil, err
  1804. }
  1805. return &info, nil
  1806. }
  1807. func GetSigleDrugWarehouseOutInfoOne(patient_id int64, advice_date int64, orgid int64) (models.XtDrugWarehouseOutInfo, error) {
  1808. info := models.XtDrugWarehouseOutInfo{}
  1809. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", patient_id, advice_date, orgid).Find(&info).Error
  1810. return info, err
  1811. }
  1812. func UpdateSingleDrugWarehouseOutInfoSix(patient_id int64, advice_date int64, orgid int64, info *models.DrugWarehouseOutInfo, advice_id int64) error {
  1813. 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
  1814. return err
  1815. }
  1816. func UpdateSingleDrugWarehouseOutInfo(patient_id int64, advice_date int64, orgid int64, info *models.DrugWarehouseOutInfo) error {
  1817. 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
  1818. return err
  1819. }
  1820. func FindDrugAutoReduceRecordInfo(patient_id int64, advice_date int64, orgid int64) (*models.DrugAutomaticReduceDetail, error) {
  1821. detail := models.DrugAutomaticReduceDetail{}
  1822. var err error
  1823. err = XTReadDB().Where("patient_id = ? and record_time = ? and org_id = ?", patient_id, advice_date, orgid).Find(&detail).Error
  1824. if err == gorm.ErrRecordNotFound {
  1825. return nil, err
  1826. }
  1827. if err != nil {
  1828. return nil, err
  1829. }
  1830. return &detail, nil
  1831. }
  1832. func UpdateDrugAutoReduceRecordInfo(patient_id int64, record_date int64, drug_id int64, detail *models.DrugAutomaticReduceDetail) error {
  1833. 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
  1834. return err
  1835. }
  1836. func GetNewDrugWarehouseOutInfoSix(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64, warehouse_info_id int64) (models.DrugWarehouseOutInfo, error) {
  1837. info := models.DrugWarehouseOutInfo{}
  1838. var err error
  1839. 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
  1840. return info, err
  1841. }
  1842. func GetNewDrugWarehouseOutInfo(drugid int64, patient_id int64, record_date int64, org_id int64) (models.DrugWarehouseOutInfo, error) {
  1843. info := models.DrugWarehouseOutInfo{}
  1844. var err error
  1845. 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
  1846. return info, err
  1847. }
  1848. func UpdateSigleDrugWarehouseOutInfo(id int64, info *models.DrugWarehouseOutInfo) error {
  1849. 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
  1850. return err
  1851. }
  1852. func UpdateSigleDrugWarehouseOutInfoTwo(id int64, info *models.DrugWarehouseOutInfo) error {
  1853. 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
  1854. return err
  1855. }
  1856. func FindDrugWarehouseOutInfoCount(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  1857. 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
  1858. return flow, err
  1859. }
  1860. func FindDrugSettleWarehouseOutInfoCount(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  1861. 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
  1862. return flow, err
  1863. }
  1864. func FindDrugCancelOutInfo(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  1865. 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
  1866. return flow, err
  1867. }
  1868. func FindDrugCancelStock(return_date int64, org_id int64) (*models.DrugCancelStock, error) {
  1869. stock := models.DrugCancelStock{}
  1870. var err error
  1871. err = XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_date, org_id).Find(&stock).Error
  1872. if err == gorm.ErrRecordNotFound {
  1873. return nil, err
  1874. }
  1875. if err != nil {
  1876. return nil, err
  1877. }
  1878. return &stock, nil
  1879. }
  1880. func FindLastDrugCancelStock(advice_date int64, org_id int64) (models.DrugCancelStock, error) {
  1881. cancelStock := models.DrugCancelStock{}
  1882. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", advice_date, org_id).Find(&cancelStock).Error
  1883. return cancelStock, err
  1884. }
  1885. func GetAllSumDrugWarehouseOut(patient_id int64, user_org_id int64, advice_date int64) (advice []*models.HisDoctorAdvice, err error) {
  1886. 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
  1887. return advice, err
  1888. }
  1889. func UpdateDrugWarehouseOutByPatientId(patient_id int64, advice_date int64, orgid int64, info models.DrugWarehouseOutInfo) error {
  1890. 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
  1891. return err
  1892. }
  1893. func FindeGoodInfo(orgid int64, id int64) (models.GoodInformation, error) {
  1894. information := models.GoodInformation{}
  1895. err := XTReadDB().Where("org_id = ? and id = ? and status = 1", orgid, id).Find(&information).Error
  1896. return information, err
  1897. }
  1898. func UpdateHisPrescriptionProjectStatus(project_id int64, record_time int64, user_org_id int64, patient_id int64) (models.HisPrescriptionProject, error) {
  1899. project := models.HisPrescriptionProject{}
  1900. 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
  1901. return project, err
  1902. }
  1903. // 药品自动出库 递归方式 deliver_number_one已经转成最小数量
  1904. func HisSettleDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo, deliver_number_one int64) (err error) {
  1905. fmt.Println("deliver_number_one -----------------------------------------", deliver_number_one)
  1906. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1907. var deliver_number int64 = 0
  1908. //var deliver_number_one int64 = 0
  1909. var stock_number int64 = 0
  1910. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  1911. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1912. //prescribingCount_temp := strconv.FormatFloat(math.Abs(prescribingCount), 'f', 0, 64)
  1913. //prescribe_count, _ := strconv.ParseInt(prescribingCount_temp, 10, 64)
  1914. var drug_price float64
  1915. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1916. deliver_number = count * drup.MinNumber
  1917. } else {
  1918. deliver_number = count
  1919. }
  1920. //if advice.PrescribingNumberUnit == drup.MaxUnit {
  1921. // deliver_number_one = prescribe_count * drup.MinNumber
  1922. //
  1923. //} else {
  1924. // deliver_number_one = prescribe_count
  1925. //
  1926. //}
  1927. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1928. drug_price = drup.RetailPrice
  1929. }
  1930. fmt.Println(drug_price)
  1931. if advice.PrescribingNumberUnit == drup.MinUnit {
  1932. drug_price = drup.MinPrice
  1933. }
  1934. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  1935. drug_price = drup.RetailPrice
  1936. }
  1937. // 根据先进先出原则,查询最先入库的批次,进行出库
  1938. // 如果没有对应的库存,则报错
  1939. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  1940. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1941. if lastWarehouse.StockMinNumber >= drup.MinNumber && lastWarehouse.MaxUnit != lastWarehouse.MinUnit {
  1942. var stockMax int64
  1943. var stockMin int64
  1944. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  1945. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  1946. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  1947. UpdateMinNumber(lastWarehouse.ID, stockMin)
  1948. }
  1949. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1950. if err != nil {
  1951. return err
  1952. }
  1953. // 将该批次的剩余库存数量转换为拆零数量
  1954. // 将该批次的剩余库存数量转换为拆零数量
  1955. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1956. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1957. }
  1958. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  1959. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  1960. }
  1961. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1962. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1963. }
  1964. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1965. fmt.Println("剩余库存2332233223232323", stock_number)
  1966. fmt.Println("出库数量233232332323232", deliver_number)
  1967. if stock_number >= deliver_number {
  1968. var maxNumber int64 = 0
  1969. var minNumber int64 = 0
  1970. maxNumber = deliver_number / drup.MinNumber
  1971. minNumber = deliver_number % drup.MinNumber
  1972. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1973. minNumber = maxNumber
  1974. }
  1975. if drup.MaxUnit != drup.MinUnit {
  1976. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  1977. return errors.New("库存数量不足")
  1978. }
  1979. }
  1980. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1981. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  1982. }
  1983. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1984. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1985. }
  1986. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1987. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1988. }
  1989. if warehouse.StockMaxNumber < 0 {
  1990. warehouse.StockMaxNumber = 0
  1991. }
  1992. if warehouse.StockMinNumber < 0 {
  1993. warehouse.StockMinNumber = 0
  1994. }
  1995. warehouse.Mtime = time.Now().Unix()
  1996. //fmt.Println("minnU目标而332322323323233223323223我", minNumber)
  1997. //fmt.Println("max_number232232332232322wo", maxNumber)
  1998. //fmt.Println(" warehouse.StockMinNumber", warehouse.StockMinNumber)
  1999. if warehouse.MaxUnit != warehouse.MinUnit {
  2000. if warehouse.StockMinNumber < minNumber {
  2001. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  2002. if warehouse.MaxUnit != warehouse.MinUnit {
  2003. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2004. }
  2005. } else {
  2006. if minNumber > 0 {
  2007. if (warehouse.StockMinNumber - minNumber) >= 0 {
  2008. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2009. }
  2010. }
  2011. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  2012. if warehouse.StockMinNumber > 0 {
  2013. if warehouse.StockMinNumber-deliver_number >= 0 {
  2014. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2015. }
  2016. }
  2017. }
  2018. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2019. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  2020. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2021. }
  2022. }
  2023. }
  2024. }
  2025. if warehouse.MinUnit == warehouse.MaxUnit {
  2026. //fmt.Println("minNUMBER233223233223322323",minNumber)
  2027. //fmt.Println("warehouse.StockMinNumber",warehouse.StockMinNumber)
  2028. //fmt.Println("warehouse.StockMaxNumber",warehouse.StockMaxNumber)
  2029. if warehouse.StockMaxNumber > 0 && warehouse.StockMinNumber == 0 && warehouse.StockMaxNumber >= minNumber {
  2030. warehouse.StockMaxNumber = warehouse.StockMaxNumber - minNumber
  2031. }
  2032. if warehouse.StockMaxNumber == 0 && warehouse.StockMinNumber > 0 && warehouse.StockMinNumber >= minNumber {
  2033. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2034. }
  2035. if warehouse.StockMaxNumber > 0 && warehouse.StockMinNumber > 0 && (warehouse.StockMaxNumber+warehouse.StockMinNumber) > minNumber {
  2036. var out_false int64 = 0
  2037. if warehouse.StockMinNumber >= minNumber {
  2038. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2039. out_false = 1
  2040. }
  2041. fmt.Println("out_false233223233223wo", out_false)
  2042. if warehouse.StockMinNumber < minNumber && out_false == 0 {
  2043. var over_total int64
  2044. over_total = warehouse.StockMinNumber
  2045. warehouse.StockMinNumber = 0
  2046. warehouse.StockMaxNumber = warehouse.StockMaxNumber - (minNumber - over_total)
  2047. }
  2048. }
  2049. //if warehouse.StockMaxNumber < minNumber && warehouse.StockMaxNumber < minNumber && (warehouse.StockMaxNumber + warehouse.StockMinNumber) > minNumber {
  2050. // var over_count int64
  2051. // over_count = warehouse.StockMinNumber
  2052. // warehouse.StockMinNumber = 0
  2053. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - (minNumber -over_count)
  2054. //
  2055. //}
  2056. }
  2057. if drup.MaxUnit != drup.MinUnit {
  2058. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2059. return errors.New("库存数量不足")
  2060. }
  2061. }
  2062. if warehouse.StockMinNumber <= 0 {
  2063. warehouse.StockMinNumber = 0
  2064. }
  2065. //扣减库存数据
  2066. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2067. if errThree != nil {
  2068. return errThree
  2069. }
  2070. //查询剩余库存
  2071. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  2072. var sum_count int64
  2073. for _, its := range stockInfo {
  2074. if its.MaxUnit == drup.MaxUnit {
  2075. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  2076. }
  2077. sum_count += its.StockMaxNumber + its.StockMinNumber
  2078. }
  2079. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2080. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2081. WarehouseOutId: warehouseout.ID,
  2082. Status: 1,
  2083. Ctime: time.Now().Unix(),
  2084. Remark: warehouse.Remark,
  2085. OrgId: orgID,
  2086. Type: 5,
  2087. Manufacturer: warehouse.Manufacturer,
  2088. Dealer: warehouse.Dealer,
  2089. IsSys: 1,
  2090. SysRecordTime: advice.RecordDate,
  2091. DrugId: advice.DrugId,
  2092. Number: warehouse.Number,
  2093. BatchNumber: warehouse.BatchNumber,
  2094. Price: warehouse.RetailPrice,
  2095. CountUnit: drup.MinUnit,
  2096. RetailPrice: warehouse.RetailPrice,
  2097. ProductDate: warehouse.ProductDate,
  2098. ExpiryDate: warehouse.ExpiryDate,
  2099. PatientId: advice.PatientId,
  2100. Count: deliver_number, //出最小单位
  2101. WarehouseInfoId: warehouse.ID,
  2102. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2103. StorehouseId: storeConfig.DrugStorehouseOut,
  2104. IsCheck: 1,
  2105. OverCount: sum_count,
  2106. AdviceId: advice.ID,
  2107. OrderId: warehouseout.OrderId,
  2108. }
  2109. //查询今日该药品该患者是否有出库数据
  2110. lastDrugOutInfo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  2111. fmt.Println("lastDrugOutInfo0000000293293292392392393293239239", lastDrugOutInfo)
  2112. if lastDrugOutInfo.ID == 0 {
  2113. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2114. if errOne != nil {
  2115. return errOne
  2116. }
  2117. }
  2118. if lastDrugOutInfo.ID > 0 {
  2119. var his_count int64
  2120. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  2121. his_count = lastDrugOutInfo.Count * drup.MinNumber
  2122. }
  2123. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  2124. his_count = lastDrugOutInfo.Count
  2125. }
  2126. fmt.Println("deliver_numberwode8888888823832832832832832832823823-------------------------", deliver_number)
  2127. fmt.Println("his_countnide-------------------------------------------------------", his_count)
  2128. if deliver_number != his_count {
  2129. //warehouseOutInfo.Count = deliver_number_one
  2130. errThree := UpdateSigleDrugWarehouseOutInfo(lastDrugOutInfo.ID, warehouseOutInfo)
  2131. if errThree != nil {
  2132. return errThree
  2133. }
  2134. }
  2135. }
  2136. var out_count int64
  2137. var cancel_count int64
  2138. //查询出库数据
  2139. infoCountList, _ := FindDrugSettleWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2140. //查询退库数据
  2141. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2142. for _, item := range infoCountList {
  2143. out_count += item.Count
  2144. }
  2145. for _, item := range cancelInfoList {
  2146. cancel_count += item.Count
  2147. }
  2148. fmt.Println("deliver_number_one", deliver_number_one)
  2149. fmt.Println("out_count", out_count)
  2150. fmt.Println("cancel_count", cancel_count)
  2151. //如果本次出库数据大于历史出库数据 新增1条流水
  2152. if deliver_number_one > (out_count - cancel_count) {
  2153. drugflow := models.DrugFlow{
  2154. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2155. WarehouseOutId: warehouseout.ID,
  2156. DrugId: advice.DrugId,
  2157. Number: warehouse.Number,
  2158. ProductDate: warehouse.ProductDate,
  2159. ExpireDate: warehouse.ExpiryDate,
  2160. Count: deliver_number_one - (out_count - cancel_count), //按最小单位计算,
  2161. Price: warehouse.RetailPrice,
  2162. Status: 1,
  2163. Ctime: time.Now().Unix(),
  2164. UserOrgId: orgID,
  2165. Manufacturer: warehouse.Manufacturer,
  2166. Dealer: warehouse.Dealer,
  2167. BatchNumber: warehouse.BatchNumber,
  2168. MaxUnit: drup.MinUnit,
  2169. ConsumableType: 15,
  2170. IsEdit: 1,
  2171. Creator: advice.ExecutionStaff,
  2172. IsSys: 1,
  2173. PatientId: advice.PatientId,
  2174. SystemTime: advice.AdviceDate,
  2175. WarehousingDetailId: warehouse.ID,
  2176. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2177. StorehouseId: storeConfig.DrugStorehouseOut,
  2178. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2179. OverCount: sum_count,
  2180. AdviceId: advice.ID,
  2181. LastPrice: warehouse.Price,
  2182. OrderId: warehouseout.OrderId,
  2183. }
  2184. CreateDrugFlowOne(drugflow)
  2185. //出库数量相加
  2186. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2187. }
  2188. if deliver_number == (out_count - cancel_count) {
  2189. drugflow := models.DrugFlow{
  2190. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2191. WarehouseOutId: warehouseout.ID,
  2192. DrugId: advice.DrugId,
  2193. Number: warehouse.Number,
  2194. ProductDate: warehouse.ProductDate,
  2195. ExpireDate: warehouse.ExpiryDate,
  2196. Count: deliver_number, //按最小单位计算,
  2197. Price: warehouse.RetailPrice,
  2198. Status: 1,
  2199. Ctime: time.Now().Unix(),
  2200. UserOrgId: orgID,
  2201. Manufacturer: warehouse.Manufacturer,
  2202. Dealer: warehouse.Dealer,
  2203. BatchNumber: warehouse.BatchNumber,
  2204. MaxUnit: drup.MinUnit,
  2205. ConsumableType: 15,
  2206. IsEdit: 1,
  2207. Creator: advice.ExecutionStaff,
  2208. IsSys: 1,
  2209. PatientId: advice.PatientId,
  2210. SystemTime: advice.AdviceDate,
  2211. WarehousingDetailId: warehouse.ID,
  2212. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2213. StorehouseId: storeConfig.DrugStorehouseOut,
  2214. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2215. OverCount: sum_count,
  2216. AdviceId: advice.ID,
  2217. LastPrice: warehouse.Price,
  2218. OrderId: warehouseout.OrderId,
  2219. }
  2220. CreateDrugFlowOne(drugflow)
  2221. //出库数量相加
  2222. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2223. }
  2224. fmt.Println("出库数据", deliver_number_one)
  2225. fmt.Println("马文强out_count233232232ooqw", out_count)
  2226. fmt.Println("马cancel_count33232232ooqw", cancel_count)
  2227. //如果本次出库数据小于历史出库数据 新增1条退库流水
  2228. if deliver_number_one < (out_count - cancel_count) {
  2229. operation_time := time.Now().Unix()
  2230. ctime := time.Now().Unix()
  2231. timeStr := time.Now().Format("2006-01-02")
  2232. timeArr := strings.Split(timeStr, "-")
  2233. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  2234. total = total + 1
  2235. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  2236. cancelStock := models.DrugCancelStock{
  2237. OrderNumber: orderNumber,
  2238. OperaTime: operation_time,
  2239. OrgId: advice.UserOrgId,
  2240. Creater: advice.ExecutionStaff,
  2241. Ctime: ctime,
  2242. Status: 1,
  2243. ReturnTime: advice.AdviceDate,
  2244. Dealer: 0,
  2245. Manufacturer: 0,
  2246. Type: 5,
  2247. StorehouseId: storeConfig.DrugStorehouseOut,
  2248. IsCheck: 1,
  2249. OrderId: warehouseout.OrderId,
  2250. }
  2251. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2252. if errCode == gorm.ErrRecordNotFound {
  2253. AddSigleDrugCancelStock(&cancelStock)
  2254. }
  2255. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2256. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  2257. dealer, _ := GetDealerById(warehouse.Dealer)
  2258. //cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  2259. cancelStockInfo := models.DrugCancelStockInfo{
  2260. OrderNumber: lastDrugCancelStock.OrderNumber,
  2261. CancelStockId: lastDrugCancelStock.ID,
  2262. DrugId: advice.DrugId,
  2263. Count: (out_count - cancel_count) - deliver_number,
  2264. Status: 1,
  2265. Ctime: ctime,
  2266. OrgId: advice.UserOrgId,
  2267. Type: 5,
  2268. Manufacturer: manufactureName.ManufacturerName,
  2269. Dealer: dealer.DealerName,
  2270. Total: 0,
  2271. RetailPrice: warehouse.RetailPrice,
  2272. RetailTotalPrice: warehouse.RetailTotalPrice,
  2273. Price: warehouse.Price,
  2274. RegisterAccount: "",
  2275. Remark: warehouse.Remark,
  2276. BatchNumber: warehouse.BatchNumber,
  2277. MaxUnit: drup.MinUnit,
  2278. ProductDate: warehouse.ProductDate,
  2279. ExpiryDate: warehouse.ExpiryDate,
  2280. BatchNumberId: warehouse.ID,
  2281. StorehouseId: storeConfig.DrugStorehouseOut,
  2282. IsCheck: 1,
  2283. OrderId: warehouseout.OrderId,
  2284. }
  2285. CreatedCancelStock(cancelStockInfo)
  2286. flow := models.DrugFlow{
  2287. WarehousingId: warehouse.ID,
  2288. DrugId: warehouse.DrugId,
  2289. Number: "",
  2290. BatchNumber: warehouse.BatchNumber,
  2291. Count: out_count - cancel_count - deliver_number,
  2292. UserOrgId: advice.UserOrgId,
  2293. PatientId: advice.PatientId,
  2294. SystemTime: advice.AdviceDate,
  2295. ConsumableType: 7,
  2296. IsSys: 0,
  2297. WarehousingOrder: "",
  2298. WarehouseOutId: 0,
  2299. WarehouseOutOrderNumber: "",
  2300. IsEdit: 0,
  2301. CancelStockId: cancelStock.ID,
  2302. CancelOrderNumber: lastDrugCancelStock.OrderNumber,
  2303. Manufacturer: warehouse.Manufacturer,
  2304. Dealer: warehouse.Dealer,
  2305. Creator: advice.ExecutionStaff,
  2306. UpdateCreator: 0,
  2307. Status: 1,
  2308. Ctime: time.Now().Unix(),
  2309. Mtime: 0,
  2310. Price: warehouse.RetailPrice,
  2311. WarehousingDetailId: warehouse.ID,
  2312. WarehouseOutDetailId: 0,
  2313. CancelOutDetailId: lastDrugCancelStock.ID,
  2314. ExpireDate: warehouse.ExpiryDate,
  2315. ProductDate: warehouse.ProductDate,
  2316. MaxUnit: drup.MinUnit,
  2317. MinUnit: "",
  2318. AdviceId: advice.ID,
  2319. StorehouseId: storeConfig.DrugStorehouseOut,
  2320. LastPrice: warehouse.Price,
  2321. OverCount: sum_count,
  2322. OrderId: warehouseout.OrderId,
  2323. }
  2324. CreateDrugFlowOne(flow)
  2325. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  2326. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  2327. }
  2328. //查询是否存在数据
  2329. details := &models.DrugAutomaticReduceDetail{
  2330. WarehouseOutId: warehouseout.ID,
  2331. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2332. PatientId: advice.PatientId,
  2333. Ctime: time.Now().Unix(),
  2334. Mtime: time.Now().Unix(),
  2335. Status: 1,
  2336. RecordTime: advice.RecordDate,
  2337. OrgId: orgID,
  2338. DrugId: advice.DrugId,
  2339. Count: deliver_number,
  2340. CountUnit: drup.MinUnit,
  2341. WarehouseInfoId: warehouse.ID,
  2342. StorehouseId: storeConfig.DrugStorehouseOut,
  2343. AdviceId: advice.ID,
  2344. }
  2345. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2346. if errTwo != nil {
  2347. return errTwo
  2348. }
  2349. return nil
  2350. } else {
  2351. info := models.XtDrugWarehouseInfo{
  2352. ID: warehouse.ID,
  2353. WarehousingId: warehouse.WarehousingId,
  2354. DrugId: warehouse.DrugId,
  2355. Number: warehouse.Number,
  2356. ProductDate: warehouse.ProductDate,
  2357. ExpiryDate: warehouse.ExpiryDate,
  2358. WarehousingCount: warehouse.WarehousingCount,
  2359. Price: warehouse.RetailPrice,
  2360. TotalPrice: warehouse.TotalPrice,
  2361. Dealer: warehouse.Dealer,
  2362. Manufacturer: warehouse.Manufacturer,
  2363. Remark: warehouse.Remark,
  2364. Ctime: warehouse.Ctime,
  2365. Mtime: warehouse.Mtime,
  2366. Status: 1,
  2367. OrgId: warehouse.OrgId,
  2368. IsReturn: warehouse.IsReturn,
  2369. WarehousingOrder: warehouse.WarehousingOrder,
  2370. Type: warehouse.Type,
  2371. RetailPrice: warehouse.RetailPrice,
  2372. RetailTotalPrice: warehouse.RetailPrice,
  2373. StockMaxNumber: 0,
  2374. StockMinNumber: 0,
  2375. BatchNumber: warehouse.BatchNumber,
  2376. MaxUnit: warehouse.MaxUnit,
  2377. WarehousingInfoId: warehouse.WarehousingInfoId,
  2378. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2379. StorehouseId: storeConfig.DrugStorehouseOut,
  2380. IsCheck: 1,
  2381. }
  2382. //扣减库存
  2383. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2384. if errThree != nil {
  2385. return errThree
  2386. }
  2387. //查询剩余库存
  2388. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  2389. var sum_count int64
  2390. for _, its := range stockInfo {
  2391. if its.MaxUnit == drup.MaxUnit {
  2392. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  2393. }
  2394. sum_count += its.StockMaxNumber + its.StockMinNumber
  2395. }
  2396. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2397. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2398. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2399. WarehouseOutId: warehouseout.ID,
  2400. Status: 1,
  2401. Ctime: time.Now().Unix(),
  2402. Remark: warehouse.Remark,
  2403. OrgId: orgID,
  2404. Type: 5,
  2405. Manufacturer: warehouse.Manufacturer,
  2406. Dealer: warehouse.Dealer,
  2407. IsSys: 1,
  2408. SysRecordTime: advice.RecordDate,
  2409. DrugId: advice.DrugId,
  2410. Number: warehouse.Number,
  2411. BatchNumber: warehouse.BatchNumber,
  2412. Price: warehouse.RetailPrice,
  2413. CountUnit: drup.MinUnit,
  2414. ProductDate: warehouse.ProductDate,
  2415. ExpiryDate: warehouse.ExpiryDate,
  2416. PatientId: advice.PatientId,
  2417. Count: stock_number,
  2418. WarehouseInfoId: warehouse.ID,
  2419. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2420. StorehouseId: storeConfig.DrugStorehouseOut,
  2421. IsCheck: 1,
  2422. OverCount: sum_count,
  2423. AdviceId: advice.ID,
  2424. OrderId: warehouseout.OrderId,
  2425. }
  2426. //查询是否存在出库数据
  2427. lastDrugOutInfoTwo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  2428. //_, errCode := GetSigleDrugWarehouseOutInfo(advice.PatientId, advice.AdviceDate, orgID)
  2429. //fmt.Println("errcoedee232233223233232323232323233223232323322323232323232323",errCode)
  2430. if lastDrugOutInfoTwo.ID == 0 {
  2431. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2432. if errOne != nil {
  2433. return errOne
  2434. }
  2435. }
  2436. if lastDrugOutInfoTwo.ID > 0 {
  2437. lastDrugOutInfo, _ := GetSigleDrugWarehouseOutInfoOne(advice.PatientId, advice.AdviceDate, orgID)
  2438. fmt.Println("中国人民肌肤32323223332332232322323喔", lastDrugOutInfo)
  2439. var his_count int64
  2440. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  2441. his_count = lastDrugOutInfo.Count * drup.MinNumber
  2442. }
  2443. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  2444. his_count = lastDrugOutInfo.Count
  2445. }
  2446. fmt.Println("deliver_number3232323232wowooww", deliver_number)
  2447. fmt.Println("his_count99999992i2222wo", his_count)
  2448. if deliver_number != his_count {
  2449. errThree := UpdateSingleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo, advice.ID)
  2450. if errThree != nil {
  2451. return errThree
  2452. }
  2453. }
  2454. }
  2455. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  2456. details := &models.DrugAutomaticReduceDetail{
  2457. WarehouseOutId: warehouseout.ID,
  2458. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  2459. PatientId: advice.PatientId,
  2460. Ctime: time.Now().Unix(),
  2461. Mtime: time.Now().Unix(),
  2462. Status: 1,
  2463. RecordTime: advice.RecordDate,
  2464. OrgId: orgID,
  2465. DrugId: advice.DrugId,
  2466. Count: stock_number,
  2467. CountUnit: drup.MinUnit,
  2468. WarehouseInfoId: warehouse.ID,
  2469. StorehouseId: storeConfig.DrugStorehouseOut,
  2470. AdviceId: advice.ID,
  2471. }
  2472. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  2473. if errcodeThree == gorm.ErrRecordNotFound {
  2474. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2475. if errTwo != nil {
  2476. return errTwo
  2477. }
  2478. }
  2479. if errcodeThree == nil {
  2480. UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  2481. }
  2482. var out_count int64
  2483. var cancel_count int64
  2484. //查询出库数据
  2485. infoCountList, _ := FindDrugSettleWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2486. //查询退库数据
  2487. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2488. for _, item := range infoCountList {
  2489. out_count += item.Count
  2490. }
  2491. for _, item := range cancelInfoList {
  2492. cancel_count += item.Count
  2493. }
  2494. fmt.Println("出库数量", deliver_number)
  2495. fmt.Println("out_coutn233232322323wo", cancel_count)
  2496. fmt.Println("out_count99988233232", out_count)
  2497. //如果本次出库数据大于历史出库数据 新增1条流水
  2498. if deliver_number > (out_count - cancel_count) {
  2499. drugflow := models.DrugFlow{
  2500. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2501. WarehouseOutId: warehouseout.ID,
  2502. DrugId: advice.DrugId,
  2503. Number: warehouse.Number,
  2504. ProductDate: warehouse.ProductDate,
  2505. ExpireDate: warehouse.ExpiryDate,
  2506. Count: stock_number,
  2507. Price: warehouse.RetailPrice,
  2508. Status: 1,
  2509. Ctime: time.Now().Unix(),
  2510. UserOrgId: orgID,
  2511. Manufacturer: warehouse.Manufacturer,
  2512. Dealer: warehouse.Dealer,
  2513. BatchNumber: warehouse.BatchNumber,
  2514. MaxUnit: drup.MinUnit,
  2515. ConsumableType: 15,
  2516. IsEdit: 1,
  2517. Creator: advice.ExecutionStaff,
  2518. IsSys: 1,
  2519. PatientId: advice.PatientId,
  2520. WarehousingDetailId: warehouse.ID,
  2521. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2522. StorehouseId: storeConfig.DrugStorehouseOut,
  2523. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2524. OverCount: sum_count,
  2525. AdviceId: advice.ID,
  2526. LastPrice: warehouse.Price,
  2527. SystemTime: advice.AdviceDate,
  2528. OrderId: warehouseout.OrderId,
  2529. }
  2530. CreateDrugFlowOne(drugflow)
  2531. //出库数量相加
  2532. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2533. }
  2534. if deliver_number < (out_count - cancel_count) {
  2535. fmt.Println("即那里3232232323")
  2536. operation_time := time.Now().Unix()
  2537. ctime := time.Now().Unix()
  2538. timeStr := time.Now().Format("2006-01-02")
  2539. timeArr := strings.Split(timeStr, "-")
  2540. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  2541. total = total + 1
  2542. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  2543. cancelStock := models.DrugCancelStock{
  2544. OrderNumber: orderNumber,
  2545. OperaTime: operation_time,
  2546. OrgId: advice.UserOrgId,
  2547. Creater: advice.ExecutionStaff,
  2548. Ctime: ctime,
  2549. Status: 1,
  2550. ReturnTime: advice.AdviceDate,
  2551. Dealer: 0,
  2552. Manufacturer: 0,
  2553. Type: 5,
  2554. StorehouseId: storeConfig.DrugStorehouseOut,
  2555. IsCheck: 1,
  2556. OrderId: warehouseout.OrderId,
  2557. }
  2558. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2559. if errCode == gorm.ErrRecordNotFound {
  2560. AddSigleDrugCancelStock(&cancelStock)
  2561. }
  2562. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2563. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  2564. dealer, _ := GetDealerById(warehouse.Dealer)
  2565. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  2566. cancelStockInfo := models.DrugCancelStockInfo{
  2567. OrderNumber: cancelInfo.OrderNumber,
  2568. CancelStockId: lastDrugCancelStock.ID,
  2569. DrugId: advice.DrugId,
  2570. Count: (out_count - cancel_count) - deliver_number,
  2571. Status: 1,
  2572. Ctime: ctime,
  2573. OrgId: advice.UserOrgId,
  2574. Type: 5,
  2575. Manufacturer: manufactureName.ManufacturerName,
  2576. Dealer: dealer.DealerName,
  2577. Total: 0,
  2578. RetailPrice: warehouse.RetailPrice,
  2579. RetailTotalPrice: warehouse.RetailTotalPrice,
  2580. Price: warehouse.Price,
  2581. RegisterAccount: "",
  2582. Remark: warehouse.Remark,
  2583. BatchNumber: warehouse.BatchNumber,
  2584. MaxUnit: drup.MinUnit,
  2585. ProductDate: warehouse.ProductDate,
  2586. ExpiryDate: warehouse.ExpiryDate,
  2587. BatchNumberId: warehouse.ID,
  2588. StorehouseId: storeConfig.DrugStorehouseOut,
  2589. IsCheck: 1,
  2590. OrderId: warehouseout.OrderId,
  2591. }
  2592. CreatedCancelStock(cancelStockInfo)
  2593. flow := models.DrugFlow{
  2594. WarehousingId: warehouse.ID,
  2595. DrugId: warehouse.DrugId,
  2596. Number: "",
  2597. BatchNumber: warehouse.BatchNumber,
  2598. Count: out_count - cancel_count - deliver_number,
  2599. UserOrgId: advice.UserOrgId,
  2600. PatientId: advice.PatientId,
  2601. SystemTime: advice.AdviceDate,
  2602. ConsumableType: 7,
  2603. IsSys: 0,
  2604. WarehousingOrder: "",
  2605. WarehouseOutId: 0,
  2606. WarehouseOutOrderNumber: "",
  2607. IsEdit: 0,
  2608. CancelStockId: cancelStock.ID,
  2609. CancelOrderNumber: lastDrugCancelStock.OrderNumber,
  2610. Manufacturer: warehouse.Manufacturer,
  2611. Dealer: warehouse.Dealer,
  2612. Creator: advice.ExecutionStaff,
  2613. UpdateCreator: 0,
  2614. Status: 1,
  2615. Ctime: time.Now().Unix(),
  2616. Mtime: 0,
  2617. Price: warehouse.RetailPrice,
  2618. WarehousingDetailId: warehouse.ID,
  2619. WarehouseOutDetailId: 0,
  2620. CancelOutDetailId: lastDrugCancelStock.ID,
  2621. ExpireDate: warehouse.ExpiryDate,
  2622. ProductDate: warehouse.ProductDate,
  2623. MaxUnit: drup.MinUnit,
  2624. MinUnit: "",
  2625. AdviceId: advice.ID,
  2626. StorehouseId: storeConfig.DrugStorehouseOut,
  2627. LastPrice: warehouse.Price,
  2628. OverCount: sum_count,
  2629. OrderId: warehouseout.OrderId,
  2630. }
  2631. CreateDrugFlowOne(flow)
  2632. //退库数量增加
  2633. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  2634. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  2635. }
  2636. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2637. prescribingNumber_two_temp := deliver_number - stock_number
  2638. overPlusNumber := float64(prescribingNumber_two_temp)
  2639. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2640. advice.PrescribingNumberUnit = drup.MinUnit
  2641. fmt.Println("剩余库存9999999233232", overPlusNumber)
  2642. HisSettleDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, deliver_number_one)
  2643. }
  2644. return
  2645. }