new_warehouse_service.go 112KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961
  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. // 药品自动出库 递归方式
  1904. func HisSettleDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo, prescribingCount float64) (err error) {
  1905. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1906. var deliver_number int64 = 0
  1907. var deliver_number_one int64 = 0
  1908. var stock_number int64 = 0
  1909. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  1910. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1911. prescribingCount_temp := strconv.FormatFloat(math.Abs(prescribingCount), 'f', 0, 64)
  1912. prescribe_count, _ := strconv.ParseInt(prescribingCount_temp, 10, 64)
  1913. var drug_price float64
  1914. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1915. deliver_number = count * drup.MinNumber
  1916. } else {
  1917. deliver_number = count
  1918. }
  1919. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1920. deliver_number_one = prescribe_count * drup.MinNumber
  1921. } else {
  1922. deliver_number_one = prescribe_count
  1923. }
  1924. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1925. drug_price = drup.RetailPrice
  1926. }
  1927. fmt.Println(drug_price)
  1928. if advice.PrescribingNumberUnit == drup.MinUnit {
  1929. drug_price = drup.MinPrice
  1930. }
  1931. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  1932. drug_price = drup.RetailPrice
  1933. }
  1934. // 根据先进先出原则,查询最先入库的批次,进行出库
  1935. // 如果没有对应的库存,则报错
  1936. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  1937. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1938. if lastWarehouse.StockMinNumber >= drup.MinNumber && lastWarehouse.MaxUnit != lastWarehouse.MinUnit {
  1939. var stockMax int64
  1940. var stockMin int64
  1941. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  1942. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  1943. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  1944. UpdateMinNumber(lastWarehouse.ID, stockMin)
  1945. }
  1946. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1947. if err != nil {
  1948. return err
  1949. }
  1950. // 将该批次的剩余库存数量转换为拆零数量
  1951. // 将该批次的剩余库存数量转换为拆零数量
  1952. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1953. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1954. }
  1955. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  1956. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  1957. }
  1958. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1959. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1960. }
  1961. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1962. fmt.Println("剩余库存2332233223232323", stock_number)
  1963. fmt.Println("出库数量233232332323232", deliver_number)
  1964. if stock_number >= deliver_number {
  1965. var maxNumber int64 = 0
  1966. var minNumber int64 = 0
  1967. maxNumber = deliver_number / drup.MinNumber
  1968. minNumber = deliver_number % drup.MinNumber
  1969. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1970. minNumber = maxNumber
  1971. }
  1972. if drup.MaxUnit != drup.MinUnit {
  1973. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  1974. return errors.New("库存数量不足")
  1975. }
  1976. }
  1977. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1978. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  1979. }
  1980. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1981. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1982. }
  1983. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1984. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1985. }
  1986. if warehouse.StockMaxNumber < 0 {
  1987. warehouse.StockMaxNumber = 0
  1988. }
  1989. if warehouse.StockMinNumber < 0 {
  1990. warehouse.StockMinNumber = 0
  1991. }
  1992. warehouse.Mtime = time.Now().Unix()
  1993. //fmt.Println("minnU目标而332322323323233223323223我", minNumber)
  1994. //fmt.Println("max_number232232332232322wo", maxNumber)
  1995. //fmt.Println(" warehouse.StockMinNumber", warehouse.StockMinNumber)
  1996. if warehouse.MaxUnit != warehouse.MinUnit {
  1997. if warehouse.StockMinNumber < minNumber {
  1998. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1999. if warehouse.MaxUnit != warehouse.MinUnit {
  2000. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  2001. }
  2002. } else {
  2003. if minNumber > 0 {
  2004. if (warehouse.StockMinNumber - minNumber) >= 0 {
  2005. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2006. }
  2007. }
  2008. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  2009. if warehouse.StockMinNumber > 0 {
  2010. if warehouse.StockMinNumber-deliver_number >= 0 {
  2011. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2012. }
  2013. }
  2014. }
  2015. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2016. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  2017. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2018. }
  2019. }
  2020. }
  2021. }
  2022. if warehouse.MinUnit == warehouse.MaxUnit {
  2023. //fmt.Println("minNUMBER233223233223322323",minNumber)
  2024. //fmt.Println("warehouse.StockMinNumber",warehouse.StockMinNumber)
  2025. //fmt.Println("warehouse.StockMaxNumber",warehouse.StockMaxNumber)
  2026. if warehouse.StockMaxNumber > 0 && warehouse.StockMinNumber == 0 && warehouse.StockMaxNumber >= minNumber {
  2027. warehouse.StockMaxNumber = warehouse.StockMaxNumber - minNumber
  2028. }
  2029. if warehouse.StockMaxNumber == 0 && warehouse.StockMinNumber > 0 && warehouse.StockMinNumber >= minNumber {
  2030. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2031. }
  2032. if warehouse.StockMaxNumber > 0 && warehouse.StockMinNumber > 0 && (warehouse.StockMaxNumber+warehouse.StockMinNumber) > minNumber {
  2033. var out_false int64 = 0
  2034. if warehouse.StockMinNumber >= minNumber {
  2035. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  2036. out_false = 1
  2037. }
  2038. fmt.Println("out_false233223233223wo", out_false)
  2039. if warehouse.StockMinNumber < minNumber && out_false == 0 {
  2040. var over_total int64
  2041. over_total = warehouse.StockMinNumber
  2042. warehouse.StockMinNumber = 0
  2043. warehouse.StockMaxNumber = warehouse.StockMaxNumber - (minNumber - over_total)
  2044. }
  2045. }
  2046. //if warehouse.StockMaxNumber < minNumber && warehouse.StockMaxNumber < minNumber && (warehouse.StockMaxNumber + warehouse.StockMinNumber) > minNumber {
  2047. // var over_count int64
  2048. // over_count = warehouse.StockMinNumber
  2049. // warehouse.StockMinNumber = 0
  2050. // warehouse.StockMaxNumber = warehouse.StockMaxNumber - (minNumber -over_count)
  2051. //
  2052. //}
  2053. }
  2054. if drup.MaxUnit != drup.MinUnit {
  2055. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2056. return errors.New("库存数量不足")
  2057. }
  2058. }
  2059. if warehouse.StockMinNumber <= 0 {
  2060. warehouse.StockMinNumber = 0
  2061. }
  2062. //扣减库存数据
  2063. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2064. if errThree != nil {
  2065. return errThree
  2066. }
  2067. //查询剩余库存
  2068. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  2069. var sum_count int64
  2070. for _, its := range stockInfo {
  2071. if its.MaxUnit == drup.MaxUnit {
  2072. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  2073. }
  2074. sum_count += its.StockMaxNumber + its.StockMinNumber
  2075. }
  2076. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2077. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2078. WarehouseOutId: warehouseout.ID,
  2079. Status: 1,
  2080. Ctime: time.Now().Unix(),
  2081. Remark: warehouse.Remark,
  2082. OrgId: orgID,
  2083. Type: 5,
  2084. Manufacturer: warehouse.Manufacturer,
  2085. Dealer: warehouse.Dealer,
  2086. IsSys: 1,
  2087. SysRecordTime: advice.RecordDate,
  2088. DrugId: advice.DrugId,
  2089. Number: warehouse.Number,
  2090. BatchNumber: warehouse.BatchNumber,
  2091. Price: warehouse.RetailPrice,
  2092. CountUnit: drup.MinUnit,
  2093. RetailPrice: warehouse.RetailPrice,
  2094. ProductDate: warehouse.ProductDate,
  2095. ExpiryDate: warehouse.ExpiryDate,
  2096. PatientId: advice.PatientId,
  2097. Count: deliver_number, //出最小单位
  2098. WarehouseInfoId: warehouse.ID,
  2099. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2100. StorehouseId: storeConfig.DrugStorehouseOut,
  2101. IsCheck: 1,
  2102. OverCount: sum_count,
  2103. AdviceId: advice.ID,
  2104. OrderId: warehouseout.OrderId,
  2105. }
  2106. //查询今日该药品该患者是否有出库数据
  2107. lastDrugOutInfo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  2108. fmt.Println("lastDrugOutInfo0000000293293292392392393293239239", lastDrugOutInfo)
  2109. if lastDrugOutInfo.ID == 0 {
  2110. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2111. if errOne != nil {
  2112. return errOne
  2113. }
  2114. }
  2115. if lastDrugOutInfo.ID > 0 {
  2116. var his_count int64
  2117. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  2118. his_count = lastDrugOutInfo.Count * drup.MinNumber
  2119. }
  2120. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  2121. his_count = lastDrugOutInfo.Count
  2122. }
  2123. fmt.Println("deliver_numberwode8888888823832832832832832832823823-------------------------", deliver_number)
  2124. fmt.Println("his_countnide-------------------------------------------------------", his_count)
  2125. if deliver_number != his_count {
  2126. //warehouseOutInfo.Count = deliver_number_one
  2127. errThree := UpdateSigleDrugWarehouseOutInfo(lastDrugOutInfo.ID, warehouseOutInfo)
  2128. if errThree != nil {
  2129. return errThree
  2130. }
  2131. }
  2132. }
  2133. var out_count int64
  2134. var cancel_count int64
  2135. //查询出库数据
  2136. infoCountList, _ := FindDrugSettleWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2137. //查询退库数据
  2138. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2139. for _, item := range infoCountList {
  2140. out_count += item.Count
  2141. }
  2142. for _, item := range cancelInfoList {
  2143. cancel_count += item.Count
  2144. }
  2145. fmt.Println("deliver_number_one", deliver_number)
  2146. fmt.Println("out_count", out_count)
  2147. fmt.Println("cancel_count", cancel_count)
  2148. //如果本次出库数据大于历史出库数据 新增1条流水
  2149. if deliver_number_one > (out_count - cancel_count) {
  2150. drugflow := models.DrugFlow{
  2151. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2152. WarehouseOutId: warehouseout.ID,
  2153. DrugId: advice.DrugId,
  2154. Number: warehouse.Number,
  2155. ProductDate: warehouse.ProductDate,
  2156. ExpireDate: warehouse.ExpiryDate,
  2157. Count: deliver_number_one - (out_count - cancel_count), //按最小单位计算,
  2158. Price: warehouse.RetailPrice,
  2159. Status: 1,
  2160. Ctime: time.Now().Unix(),
  2161. UserOrgId: orgID,
  2162. Manufacturer: warehouse.Manufacturer,
  2163. Dealer: warehouse.Dealer,
  2164. BatchNumber: warehouse.BatchNumber,
  2165. MaxUnit: drup.MinUnit,
  2166. ConsumableType: 15,
  2167. IsEdit: 1,
  2168. Creator: advice.ExecutionStaff,
  2169. IsSys: 1,
  2170. PatientId: advice.PatientId,
  2171. SystemTime: advice.AdviceDate,
  2172. WarehousingDetailId: warehouse.ID,
  2173. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2174. StorehouseId: storeConfig.DrugStorehouseOut,
  2175. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2176. OverCount: sum_count,
  2177. AdviceId: advice.ID,
  2178. LastPrice: warehouse.Price,
  2179. OrderId: warehouseout.OrderId,
  2180. }
  2181. CreateDrugFlowOne(drugflow)
  2182. //出库数量相加
  2183. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2184. }
  2185. if deliver_number == (out_count - cancel_count) {
  2186. drugflow := models.DrugFlow{
  2187. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2188. WarehouseOutId: warehouseout.ID,
  2189. DrugId: advice.DrugId,
  2190. Number: warehouse.Number,
  2191. ProductDate: warehouse.ProductDate,
  2192. ExpireDate: warehouse.ExpiryDate,
  2193. Count: deliver_number, //按最小单位计算,
  2194. Price: warehouse.RetailPrice,
  2195. Status: 1,
  2196. Ctime: time.Now().Unix(),
  2197. UserOrgId: orgID,
  2198. Manufacturer: warehouse.Manufacturer,
  2199. Dealer: warehouse.Dealer,
  2200. BatchNumber: warehouse.BatchNumber,
  2201. MaxUnit: drup.MinUnit,
  2202. ConsumableType: 15,
  2203. IsEdit: 1,
  2204. Creator: advice.ExecutionStaff,
  2205. IsSys: 1,
  2206. PatientId: advice.PatientId,
  2207. SystemTime: advice.AdviceDate,
  2208. WarehousingDetailId: warehouse.ID,
  2209. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2210. StorehouseId: storeConfig.DrugStorehouseOut,
  2211. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2212. OverCount: sum_count,
  2213. AdviceId: advice.ID,
  2214. LastPrice: warehouse.Price,
  2215. OrderId: warehouseout.OrderId,
  2216. }
  2217. CreateDrugFlowOne(drugflow)
  2218. //出库数量相加
  2219. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2220. }
  2221. //如果本次出库数据小于历史出库数据 新增1条退库流水
  2222. if deliver_number_one < (out_count - cancel_count) {
  2223. operation_time := time.Now().Unix()
  2224. ctime := time.Now().Unix()
  2225. timeStr := time.Now().Format("2006-01-02")
  2226. timeArr := strings.Split(timeStr, "-")
  2227. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  2228. total = total + 1
  2229. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  2230. cancelStock := models.DrugCancelStock{
  2231. OrderNumber: orderNumber,
  2232. OperaTime: operation_time,
  2233. OrgId: advice.UserOrgId,
  2234. Creater: advice.ExecutionStaff,
  2235. Ctime: ctime,
  2236. Status: 1,
  2237. ReturnTime: advice.AdviceDate,
  2238. Dealer: 0,
  2239. Manufacturer: 0,
  2240. Type: 5,
  2241. StorehouseId: storeConfig.DrugStorehouseOut,
  2242. IsCheck: 1,
  2243. OrderId: warehouseout.OrderId,
  2244. }
  2245. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2246. if errCode == gorm.ErrRecordNotFound {
  2247. AddSigleDrugCancelStock(&cancelStock)
  2248. }
  2249. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2250. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  2251. dealer, _ := GetDealerById(warehouse.Dealer)
  2252. //cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  2253. cancelStockInfo := models.DrugCancelStockInfo{
  2254. OrderNumber: lastDrugCancelStock.OrderNumber,
  2255. CancelStockId: lastDrugCancelStock.ID,
  2256. DrugId: advice.DrugId,
  2257. Count: (out_count - cancel_count) - deliver_number,
  2258. Status: 1,
  2259. Ctime: ctime,
  2260. OrgId: advice.UserOrgId,
  2261. Type: 5,
  2262. Manufacturer: manufactureName.ManufacturerName,
  2263. Dealer: dealer.DealerName,
  2264. Total: 0,
  2265. RetailPrice: warehouse.RetailPrice,
  2266. RetailTotalPrice: warehouse.RetailTotalPrice,
  2267. Price: warehouse.Price,
  2268. RegisterAccount: "",
  2269. Remark: warehouse.Remark,
  2270. BatchNumber: warehouse.BatchNumber,
  2271. MaxUnit: drup.MinUnit,
  2272. ProductDate: warehouse.ProductDate,
  2273. ExpiryDate: warehouse.ExpiryDate,
  2274. BatchNumberId: warehouse.ID,
  2275. StorehouseId: storeConfig.DrugStorehouseOut,
  2276. IsCheck: 1,
  2277. OrderId: warehouseout.OrderId,
  2278. }
  2279. CreatedCancelStock(cancelStockInfo)
  2280. flow := models.DrugFlow{
  2281. WarehousingId: warehouse.ID,
  2282. DrugId: warehouse.DrugId,
  2283. Number: "",
  2284. BatchNumber: warehouse.BatchNumber,
  2285. Count: out_count - cancel_count - deliver_number,
  2286. UserOrgId: advice.UserOrgId,
  2287. PatientId: advice.PatientId,
  2288. SystemTime: advice.AdviceDate,
  2289. ConsumableType: 7,
  2290. IsSys: 0,
  2291. WarehousingOrder: "",
  2292. WarehouseOutId: 0,
  2293. WarehouseOutOrderNumber: "",
  2294. IsEdit: 0,
  2295. CancelStockId: cancelStock.ID,
  2296. CancelOrderNumber: lastDrugCancelStock.OrderNumber,
  2297. Manufacturer: warehouse.Manufacturer,
  2298. Dealer: warehouse.Dealer,
  2299. Creator: advice.ExecutionStaff,
  2300. UpdateCreator: 0,
  2301. Status: 1,
  2302. Ctime: time.Now().Unix(),
  2303. Mtime: 0,
  2304. Price: warehouse.RetailPrice,
  2305. WarehousingDetailId: warehouse.ID,
  2306. WarehouseOutDetailId: 0,
  2307. CancelOutDetailId: lastDrugCancelStock.ID,
  2308. ExpireDate: warehouse.ExpiryDate,
  2309. ProductDate: warehouse.ProductDate,
  2310. MaxUnit: drup.MinUnit,
  2311. MinUnit: "",
  2312. AdviceId: advice.ID,
  2313. StorehouseId: storeConfig.DrugStorehouseOut,
  2314. LastPrice: warehouse.Price,
  2315. OverCount: sum_count,
  2316. OrderId: warehouseout.OrderId,
  2317. }
  2318. CreateDrugFlowOne(flow)
  2319. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  2320. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  2321. }
  2322. //查询是否存在数据
  2323. details := &models.DrugAutomaticReduceDetail{
  2324. WarehouseOutId: warehouseout.ID,
  2325. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2326. PatientId: advice.PatientId,
  2327. Ctime: time.Now().Unix(),
  2328. Mtime: time.Now().Unix(),
  2329. Status: 1,
  2330. RecordTime: advice.RecordDate,
  2331. OrgId: orgID,
  2332. DrugId: advice.DrugId,
  2333. Count: deliver_number,
  2334. CountUnit: drup.MinUnit,
  2335. WarehouseInfoId: warehouse.ID,
  2336. StorehouseId: storeConfig.DrugStorehouseOut,
  2337. AdviceId: advice.ID,
  2338. }
  2339. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2340. if errTwo != nil {
  2341. return errTwo
  2342. }
  2343. return nil
  2344. } else {
  2345. info := models.XtDrugWarehouseInfo{
  2346. ID: warehouse.ID,
  2347. WarehousingId: warehouse.WarehousingId,
  2348. DrugId: warehouse.DrugId,
  2349. Number: warehouse.Number,
  2350. ProductDate: warehouse.ProductDate,
  2351. ExpiryDate: warehouse.ExpiryDate,
  2352. WarehousingCount: warehouse.WarehousingCount,
  2353. Price: warehouse.RetailPrice,
  2354. TotalPrice: warehouse.TotalPrice,
  2355. Dealer: warehouse.Dealer,
  2356. Manufacturer: warehouse.Manufacturer,
  2357. Remark: warehouse.Remark,
  2358. Ctime: warehouse.Ctime,
  2359. Mtime: warehouse.Mtime,
  2360. Status: 1,
  2361. OrgId: warehouse.OrgId,
  2362. IsReturn: warehouse.IsReturn,
  2363. WarehousingOrder: warehouse.WarehousingOrder,
  2364. Type: warehouse.Type,
  2365. RetailPrice: warehouse.RetailPrice,
  2366. RetailTotalPrice: warehouse.RetailPrice,
  2367. StockMaxNumber: 0,
  2368. StockMinNumber: 0,
  2369. BatchNumber: warehouse.BatchNumber,
  2370. MaxUnit: warehouse.MaxUnit,
  2371. WarehousingInfoId: warehouse.WarehousingInfoId,
  2372. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2373. StorehouseId: storeConfig.DrugStorehouseOut,
  2374. IsCheck: 1,
  2375. }
  2376. //扣减库存
  2377. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2378. if errThree != nil {
  2379. return errThree
  2380. }
  2381. //查询剩余库存
  2382. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  2383. var sum_count int64
  2384. for _, its := range stockInfo {
  2385. if its.MaxUnit == drup.MaxUnit {
  2386. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  2387. }
  2388. sum_count += its.StockMaxNumber + its.StockMinNumber
  2389. }
  2390. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2391. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2392. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2393. WarehouseOutId: warehouseout.ID,
  2394. Status: 1,
  2395. Ctime: time.Now().Unix(),
  2396. Remark: warehouse.Remark,
  2397. OrgId: orgID,
  2398. Type: 5,
  2399. Manufacturer: warehouse.Manufacturer,
  2400. Dealer: warehouse.Dealer,
  2401. IsSys: 1,
  2402. SysRecordTime: advice.RecordDate,
  2403. DrugId: advice.DrugId,
  2404. Number: warehouse.Number,
  2405. BatchNumber: warehouse.BatchNumber,
  2406. Price: warehouse.RetailPrice,
  2407. CountUnit: drup.MinUnit,
  2408. ProductDate: warehouse.ProductDate,
  2409. ExpiryDate: warehouse.ExpiryDate,
  2410. PatientId: advice.PatientId,
  2411. Count: stock_number,
  2412. WarehouseInfoId: warehouse.ID,
  2413. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2414. StorehouseId: storeConfig.DrugStorehouseOut,
  2415. IsCheck: 1,
  2416. OverCount: sum_count,
  2417. AdviceId: advice.ID,
  2418. OrderId: warehouseout.OrderId,
  2419. }
  2420. //查询是否存在出库数据
  2421. lastDrugOutInfoTwo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  2422. //_, errCode := GetSigleDrugWarehouseOutInfo(advice.PatientId, advice.AdviceDate, orgID)
  2423. //fmt.Println("errcoedee232233223233232323232323233223232323322323232323232323",errCode)
  2424. if lastDrugOutInfoTwo.ID == 0 {
  2425. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2426. if errOne != nil {
  2427. return errOne
  2428. }
  2429. }
  2430. if lastDrugOutInfoTwo.ID > 0 {
  2431. lastDrugOutInfo, _ := GetSigleDrugWarehouseOutInfoOne(advice.PatientId, advice.AdviceDate, orgID)
  2432. fmt.Println("中国人民肌肤32323223332332232322323喔", lastDrugOutInfo)
  2433. var his_count int64
  2434. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  2435. his_count = lastDrugOutInfo.Count * drup.MinNumber
  2436. }
  2437. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  2438. his_count = lastDrugOutInfo.Count
  2439. }
  2440. fmt.Println("deliver_number3232323232wowooww", deliver_number)
  2441. fmt.Println("his_count99999992i2222wo", his_count)
  2442. if deliver_number != his_count {
  2443. errThree := UpdateSingleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo, advice.ID)
  2444. if errThree != nil {
  2445. return errThree
  2446. }
  2447. }
  2448. }
  2449. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  2450. details := &models.DrugAutomaticReduceDetail{
  2451. WarehouseOutId: warehouseout.ID,
  2452. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  2453. PatientId: advice.PatientId,
  2454. Ctime: time.Now().Unix(),
  2455. Mtime: time.Now().Unix(),
  2456. Status: 1,
  2457. RecordTime: advice.RecordDate,
  2458. OrgId: orgID,
  2459. DrugId: advice.DrugId,
  2460. Count: stock_number,
  2461. CountUnit: drup.MinUnit,
  2462. WarehouseInfoId: warehouse.ID,
  2463. StorehouseId: storeConfig.DrugStorehouseOut,
  2464. AdviceId: advice.ID,
  2465. }
  2466. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  2467. if errcodeThree == gorm.ErrRecordNotFound {
  2468. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2469. if errTwo != nil {
  2470. return errTwo
  2471. }
  2472. }
  2473. if errcodeThree == nil {
  2474. UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  2475. }
  2476. var out_count int64
  2477. var cancel_count int64
  2478. //查询出库数据
  2479. infoCountList, _ := FindDrugSettleWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2480. //查询退库数据
  2481. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2482. for _, item := range infoCountList {
  2483. out_count += item.Count
  2484. }
  2485. for _, item := range cancelInfoList {
  2486. cancel_count += item.Count
  2487. }
  2488. fmt.Println("出库数量", deliver_number)
  2489. fmt.Println("out_coutn233232322323wo", cancel_count)
  2490. fmt.Println("out_count99988233232", out_count)
  2491. //如果本次出库数据大于历史出库数据 新增1条流水
  2492. if deliver_number > (out_count - cancel_count) {
  2493. drugflow := models.DrugFlow{
  2494. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2495. WarehouseOutId: warehouseout.ID,
  2496. DrugId: advice.DrugId,
  2497. Number: warehouse.Number,
  2498. ProductDate: warehouse.ProductDate,
  2499. ExpireDate: warehouse.ExpiryDate,
  2500. Count: stock_number,
  2501. Price: warehouse.RetailPrice,
  2502. Status: 1,
  2503. Ctime: time.Now().Unix(),
  2504. UserOrgId: orgID,
  2505. Manufacturer: warehouse.Manufacturer,
  2506. Dealer: warehouse.Dealer,
  2507. BatchNumber: warehouse.BatchNumber,
  2508. MaxUnit: drup.MinUnit,
  2509. ConsumableType: 15,
  2510. IsEdit: 1,
  2511. Creator: advice.ExecutionStaff,
  2512. IsSys: 1,
  2513. PatientId: advice.PatientId,
  2514. WarehousingDetailId: warehouse.ID,
  2515. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2516. StorehouseId: storeConfig.DrugStorehouseOut,
  2517. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2518. OverCount: sum_count,
  2519. AdviceId: advice.ID,
  2520. LastPrice: warehouse.Price,
  2521. SystemTime: advice.AdviceDate,
  2522. OrderId: warehouseout.OrderId,
  2523. }
  2524. CreateDrugFlowOne(drugflow)
  2525. //出库数量相加
  2526. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2527. }
  2528. if deliver_number < (out_count - cancel_count) {
  2529. fmt.Println("即那里3232232323")
  2530. operation_time := time.Now().Unix()
  2531. ctime := time.Now().Unix()
  2532. timeStr := time.Now().Format("2006-01-02")
  2533. timeArr := strings.Split(timeStr, "-")
  2534. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  2535. total = total + 1
  2536. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  2537. cancelStock := models.DrugCancelStock{
  2538. OrderNumber: orderNumber,
  2539. OperaTime: operation_time,
  2540. OrgId: advice.UserOrgId,
  2541. Creater: advice.ExecutionStaff,
  2542. Ctime: ctime,
  2543. Status: 1,
  2544. ReturnTime: advice.AdviceDate,
  2545. Dealer: 0,
  2546. Manufacturer: 0,
  2547. Type: 5,
  2548. StorehouseId: storeConfig.DrugStorehouseOut,
  2549. IsCheck: 1,
  2550. OrderId: warehouseout.OrderId,
  2551. }
  2552. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2553. if errCode == gorm.ErrRecordNotFound {
  2554. AddSigleDrugCancelStock(&cancelStock)
  2555. }
  2556. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2557. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  2558. dealer, _ := GetDealerById(warehouse.Dealer)
  2559. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  2560. cancelStockInfo := models.DrugCancelStockInfo{
  2561. OrderNumber: cancelInfo.OrderNumber,
  2562. CancelStockId: lastDrugCancelStock.ID,
  2563. DrugId: advice.DrugId,
  2564. Count: (out_count - cancel_count) - deliver_number,
  2565. Status: 1,
  2566. Ctime: ctime,
  2567. OrgId: advice.UserOrgId,
  2568. Type: 5,
  2569. Manufacturer: manufactureName.ManufacturerName,
  2570. Dealer: dealer.DealerName,
  2571. Total: 0,
  2572. RetailPrice: warehouse.RetailPrice,
  2573. RetailTotalPrice: warehouse.RetailTotalPrice,
  2574. Price: warehouse.Price,
  2575. RegisterAccount: "",
  2576. Remark: warehouse.Remark,
  2577. BatchNumber: warehouse.BatchNumber,
  2578. MaxUnit: drup.MinUnit,
  2579. ProductDate: warehouse.ProductDate,
  2580. ExpiryDate: warehouse.ExpiryDate,
  2581. BatchNumberId: warehouse.ID,
  2582. StorehouseId: storeConfig.DrugStorehouseOut,
  2583. IsCheck: 1,
  2584. OrderId: warehouseout.OrderId,
  2585. }
  2586. CreatedCancelStock(cancelStockInfo)
  2587. flow := models.DrugFlow{
  2588. WarehousingId: warehouse.ID,
  2589. DrugId: warehouse.DrugId,
  2590. Number: "",
  2591. BatchNumber: warehouse.BatchNumber,
  2592. Count: out_count - cancel_count - deliver_number,
  2593. UserOrgId: advice.UserOrgId,
  2594. PatientId: advice.PatientId,
  2595. SystemTime: advice.AdviceDate,
  2596. ConsumableType: 7,
  2597. IsSys: 0,
  2598. WarehousingOrder: "",
  2599. WarehouseOutId: 0,
  2600. WarehouseOutOrderNumber: "",
  2601. IsEdit: 0,
  2602. CancelStockId: cancelStock.ID,
  2603. CancelOrderNumber: lastDrugCancelStock.OrderNumber,
  2604. Manufacturer: warehouse.Manufacturer,
  2605. Dealer: warehouse.Dealer,
  2606. Creator: advice.ExecutionStaff,
  2607. UpdateCreator: 0,
  2608. Status: 1,
  2609. Ctime: time.Now().Unix(),
  2610. Mtime: 0,
  2611. Price: warehouse.RetailPrice,
  2612. WarehousingDetailId: warehouse.ID,
  2613. WarehouseOutDetailId: 0,
  2614. CancelOutDetailId: lastDrugCancelStock.ID,
  2615. ExpireDate: warehouse.ExpiryDate,
  2616. ProductDate: warehouse.ProductDate,
  2617. MaxUnit: drup.MinUnit,
  2618. MinUnit: "",
  2619. AdviceId: advice.ID,
  2620. StorehouseId: storeConfig.DrugStorehouseOut,
  2621. LastPrice: warehouse.Price,
  2622. OverCount: sum_count,
  2623. OrderId: warehouseout.OrderId,
  2624. }
  2625. CreateDrugFlowOne(flow)
  2626. //退库数量增加
  2627. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  2628. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  2629. }
  2630. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2631. prescribingNumber_two_temp := deliver_number - stock_number
  2632. overPlusNumber := float64(prescribingNumber_two_temp)
  2633. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2634. advice.PrescribingNumberUnit = drup.MinUnit
  2635. fmt.Println("剩余库存9999999233232", overPlusNumber)
  2636. HisSettleDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, prescribingCount)
  2637. }
  2638. return
  2639. }