new_warehouse_service.go 109KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889
  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. if deliver_number < (out_count - cancel_count) {
  1674. operation_time := time.Now().Unix()
  1675. ctime := time.Now().Unix()
  1676. timeStr := time.Now().Format("2006-01-02")
  1677. timeArr := strings.Split(timeStr, "-")
  1678. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  1679. total = total + 1
  1680. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  1681. cancelStock := models.DrugCancelStock{
  1682. OrderNumber: orderNumber,
  1683. OperaTime: operation_time,
  1684. OrgId: advice.UserOrgId,
  1685. Creater: advice.ExecutionStaff,
  1686. Ctime: ctime,
  1687. Status: 1,
  1688. ReturnTime: advice.AdviceDate,
  1689. Dealer: 0,
  1690. Manufacturer: 0,
  1691. Type: 1,
  1692. StorehouseId: storeConfig.DrugStorehouseOut,
  1693. IsCheck: 1,
  1694. }
  1695. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1696. if errCode == gorm.ErrRecordNotFound {
  1697. AddSigleDrugCancelStock(&cancelStock)
  1698. }
  1699. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  1700. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  1701. dealer, _ := GetDealerById(warehouse.Dealer)
  1702. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  1703. cancelStockInfo := models.DrugCancelStockInfo{
  1704. OrderNumber: cancelInfo.OrderNumber,
  1705. CancelStockId: lastDrugCancelStock.ID,
  1706. DrugId: advice.DrugId,
  1707. Count: (out_count - cancel_count) - deliver_number,
  1708. Status: 1,
  1709. Ctime: ctime,
  1710. OrgId: advice.UserOrgId,
  1711. Type: 1,
  1712. Manufacturer: manufactureName.ManufacturerName,
  1713. Dealer: dealer.DealerName,
  1714. Total: 0,
  1715. RetailPrice: warehouse.RetailPrice,
  1716. RetailTotalPrice: warehouse.RetailTotalPrice,
  1717. Price: warehouse.Price,
  1718. RegisterAccount: "",
  1719. Remark: warehouse.Remark,
  1720. BatchNumber: warehouse.BatchNumber,
  1721. MaxUnit: drup.MinUnit,
  1722. ProductDate: warehouse.ProductDate,
  1723. ExpiryDate: warehouse.ExpiryDate,
  1724. BatchNumberId: warehouse.ID,
  1725. StorehouseId: storeConfig.DrugStorehouseOut,
  1726. IsCheck: 1,
  1727. }
  1728. CreatedCancelStock(cancelStockInfo)
  1729. flow := models.DrugFlow{
  1730. WarehousingId: warehouse.ID,
  1731. DrugId: warehouse.DrugId,
  1732. Number: "",
  1733. BatchNumber: warehouse.BatchNumber,
  1734. Count: out_count - cancel_count - deliver_number,
  1735. UserOrgId: advice.UserOrgId,
  1736. PatientId: advice.PatientId,
  1737. SystemTime: advice.AdviceDate,
  1738. ConsumableType: 7,
  1739. IsSys: 0,
  1740. WarehousingOrder: "",
  1741. WarehouseOutId: 0,
  1742. WarehouseOutOrderNumber: "",
  1743. IsEdit: 0,
  1744. CancelStockId: cancelStock.ID,
  1745. CancelOrderNumber: cancelStock.OrderNumber,
  1746. Manufacturer: warehouse.Manufacturer,
  1747. Dealer: warehouse.Dealer,
  1748. Creator: advice.ExecutionStaff,
  1749. UpdateCreator: 0,
  1750. Status: 1,
  1751. Ctime: time.Now().Unix(),
  1752. Mtime: 0,
  1753. Price: warehouse.RetailPrice,
  1754. WarehousingDetailId: warehouse.ID,
  1755. WarehouseOutDetailId: 0,
  1756. CancelOutDetailId: lastDrugCancelStock.ID,
  1757. ExpireDate: warehouse.ExpiryDate,
  1758. ProductDate: warehouse.ProductDate,
  1759. MaxUnit: drup.MinUnit,
  1760. MinUnit: "",
  1761. AdviceId: advice.ID,
  1762. StorehouseId: storeConfig.DrugStorehouseOut,
  1763. LastPrice: warehouse.Price,
  1764. OverCount: sum_count,
  1765. }
  1766. CreateDrugFlowOne(flow)
  1767. //退库数量增加
  1768. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  1769. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  1770. }
  1771. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1772. prescribingNumber_two_temp := deliver_number - stock_number
  1773. overPlusNumber := float64(prescribingNumber_two_temp)
  1774. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  1775. advice.PrescribingNumberUnit = drup.MinUnit
  1776. BloodHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  1777. }
  1778. return
  1779. }
  1780. func GetSigleDrugWarehouseOutInfoSix(patient_id int64, advice_date int64, orgid int64, advice_id int64) (*models.XtDrugWarehouseOutInfo, error) {
  1781. info := models.XtDrugWarehouseOutInfo{}
  1782. var err error
  1783. 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
  1784. if err == gorm.ErrRecordNotFound {
  1785. return nil, err
  1786. }
  1787. if err != nil {
  1788. return nil, err
  1789. }
  1790. return &info, nil
  1791. }
  1792. func GetSigleDrugWarehouseOutInfo(patient_id int64, advice_date int64, orgid int64) (*models.XtDrugWarehouseOutInfo, error) {
  1793. info := models.XtDrugWarehouseOutInfo{}
  1794. var err error
  1795. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", patient_id, advice_date, orgid).Find(&info).Error
  1796. if err == gorm.ErrRecordNotFound {
  1797. return nil, err
  1798. }
  1799. if err != nil {
  1800. return nil, err
  1801. }
  1802. return &info, nil
  1803. }
  1804. func GetSigleDrugWarehouseOutInfoOne(patient_id int64, advice_date int64, orgid int64) (models.XtDrugWarehouseOutInfo, error) {
  1805. info := models.XtDrugWarehouseOutInfo{}
  1806. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", patient_id, advice_date, orgid).Find(&info).Error
  1807. return info, err
  1808. }
  1809. func UpdateSingleDrugWarehouseOutInfoSix(patient_id int64, advice_date int64, orgid int64, info *models.DrugWarehouseOutInfo, advice_id int64) error {
  1810. err := XTWriteDB().Model(&models.XtDrugWarehouseOutInfo{}).Where("patient_id = ? and sys_record_time = ? and org_id = ? and advice_id = ?", 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
  1811. return err
  1812. }
  1813. func UpdateSingleDrugWarehouseOutInfo(patient_id int64, advice_date int64, orgid int64, info *models.DrugWarehouseOutInfo) error {
  1814. 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
  1815. return err
  1816. }
  1817. func FindDrugAutoReduceRecordInfo(patient_id int64, advice_date int64, orgid int64) (*models.DrugAutomaticReduceDetail, error) {
  1818. detail := models.DrugAutomaticReduceDetail{}
  1819. var err error
  1820. err = XTReadDB().Where("patient_id = ? and record_time = ? and org_id = ?", patient_id, advice_date, orgid).Find(&detail).Error
  1821. if err == gorm.ErrRecordNotFound {
  1822. return nil, err
  1823. }
  1824. if err != nil {
  1825. return nil, err
  1826. }
  1827. return &detail, nil
  1828. }
  1829. func UpdateDrugAutoReduceRecordInfo(patient_id int64, record_date int64, drug_id int64, detail *models.DrugAutomaticReduceDetail) error {
  1830. 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
  1831. return err
  1832. }
  1833. func GetNewDrugWarehouseOutInfoSix(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64, warehouse_info_id int64) (models.DrugWarehouseOutInfo, error) {
  1834. info := models.DrugWarehouseOutInfo{}
  1835. var err error
  1836. 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
  1837. return info, err
  1838. }
  1839. func GetNewDrugWarehouseOutInfo(drugid int64, patient_id int64, record_date int64, org_id int64) (models.DrugWarehouseOutInfo, error) {
  1840. info := models.DrugWarehouseOutInfo{}
  1841. var err error
  1842. 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
  1843. return info, err
  1844. }
  1845. func UpdateSigleDrugWarehouseOutInfo(id int64, info *models.DrugWarehouseOutInfo) error {
  1846. 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
  1847. return err
  1848. }
  1849. func UpdateSigleDrugWarehouseOutInfoTwo(id int64, info *models.DrugWarehouseOutInfo) error {
  1850. 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
  1851. return err
  1852. }
  1853. func FindDrugWarehouseOutInfoCount(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  1854. 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
  1855. return flow, err
  1856. }
  1857. func FindDrugSettleWarehouseOutInfoCount(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  1858. 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
  1859. return flow, err
  1860. }
  1861. func FindDrugCancelOutInfo(drugid int64, patient_id int64, record_date int64, org_id int64, advice_id int64) (flow []*models.DrugFlow, err error) {
  1862. 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
  1863. return flow, err
  1864. }
  1865. func FindDrugCancelStock(return_date int64, org_id int64) (*models.DrugCancelStock, error) {
  1866. stock := models.DrugCancelStock{}
  1867. var err error
  1868. err = XTReadDB().Where("return_date = ? and org_id = ? and status = 1", return_date, org_id).Find(&stock).Error
  1869. if err == gorm.ErrRecordNotFound {
  1870. return nil, err
  1871. }
  1872. if err != nil {
  1873. return nil, err
  1874. }
  1875. return &stock, nil
  1876. }
  1877. func FindLastDrugCancelStock(advice_date int64, org_id int64) (models.DrugCancelStock, error) {
  1878. cancelStock := models.DrugCancelStock{}
  1879. err := XTReadDB().Where("return_date = ? and org_id = ? and status = 1", advice_date, org_id).Find(&cancelStock).Error
  1880. return cancelStock, err
  1881. }
  1882. func GetAllSumDrugWarehouseOut(patient_id int64, user_org_id int64, advice_date int64) (advice []*models.HisDoctorAdvice, err error) {
  1883. 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
  1884. return advice, err
  1885. }
  1886. func UpdateDrugWarehouseOutByPatientId(patient_id int64, advice_date int64, orgid int64, info models.DrugWarehouseOutInfo) error {
  1887. 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
  1888. return err
  1889. }
  1890. func FindeGoodInfo(orgid int64, id int64) (models.GoodInformation, error) {
  1891. information := models.GoodInformation{}
  1892. err := XTReadDB().Where("org_id = ? and id = ? and status = 1", orgid, id).Find(&information).Error
  1893. return information, err
  1894. }
  1895. func UpdateHisPrescriptionProjectStatus(project_id int64, record_time int64, user_org_id int64, patient_id int64) (models.HisPrescriptionProject, error) {
  1896. project := models.HisPrescriptionProject{}
  1897. 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
  1898. return project, err
  1899. }
  1900. // 药品自动出库 递归方式
  1901. func HisSettleDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  1902. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1903. var deliver_number int64 = 0
  1904. var stock_number int64 = 0
  1905. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  1906. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  1907. var drug_price float64
  1908. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1909. deliver_number = count * drup.MinNumber
  1910. } else {
  1911. deliver_number = count
  1912. }
  1913. if advice.PrescribingNumberUnit == drup.MaxUnit {
  1914. drug_price = drup.RetailPrice
  1915. }
  1916. fmt.Println(drug_price)
  1917. if advice.PrescribingNumberUnit == drup.MinUnit {
  1918. drug_price = drup.MinPrice
  1919. }
  1920. if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
  1921. drug_price = drup.RetailPrice
  1922. }
  1923. // 根据先进先出原则,查询最先入库的批次,进行出库
  1924. // 如果没有对应的库存,则报错
  1925. storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
  1926. lastWarehouse, _ := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1927. if lastWarehouse.StockMinNumber >= drup.MinNumber {
  1928. var stockMax int64
  1929. var stockMin int64
  1930. stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
  1931. stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
  1932. ChangeMaxNumber(lastWarehouse.ID, stockMax)
  1933. UpdateMinNumber(lastWarehouse.ID, stockMin)
  1934. }
  1935. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
  1936. if err != nil {
  1937. return err
  1938. }
  1939. // 将该批次的剩余库存数量转换为拆零数量
  1940. // 将该批次的剩余库存数量转换为拆零数量
  1941. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1942. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1943. }
  1944. if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
  1945. stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
  1946. }
  1947. if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1948. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  1949. }
  1950. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1951. fmt.Println("stock_nbumber3223323232", stock_number)
  1952. fmt.Println("deliver_number", deliver_number)
  1953. if stock_number >= deliver_number {
  1954. var maxNumber int64 = 0
  1955. var minNumber int64 = 0
  1956. maxNumber = deliver_number / drup.MinNumber
  1957. minNumber = deliver_number % drup.MinNumber
  1958. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  1959. minNumber = maxNumber
  1960. }
  1961. if drup.MaxUnit != drup.MinUnit {
  1962. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  1963. return errors.New("库存数量不足")
  1964. }
  1965. }
  1966. if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1967. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
  1968. }
  1969. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
  1970. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1971. }
  1972. if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
  1973. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  1974. }
  1975. if warehouse.StockMaxNumber < 0 {
  1976. warehouse.StockMaxNumber = 0
  1977. }
  1978. if warehouse.StockMinNumber < 0 {
  1979. warehouse.StockMinNumber = 0
  1980. }
  1981. warehouse.Mtime = time.Now().Unix()
  1982. fmt.Println("minnU目标而332322323323233223323223我", minNumber)
  1983. fmt.Println("max_number232232332232322wo", maxNumber)
  1984. fmt.Println(" warehouse.StockMinNumber", warehouse.StockMinNumber)
  1985. if warehouse.StockMinNumber < minNumber {
  1986. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  1987. if warehouse.MaxUnit != warehouse.MinUnit {
  1988. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  1989. }
  1990. } else {
  1991. if minNumber > 0 {
  1992. if (warehouse.StockMinNumber - minNumber) >= 0 {
  1993. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  1994. }
  1995. }
  1996. if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
  1997. if warehouse.StockMinNumber > 0 {
  1998. if warehouse.StockMinNumber-deliver_number >= 0 {
  1999. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2000. }
  2001. }
  2002. }
  2003. }
  2004. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  2005. if (warehouse.StockMinNumber - deliver_number) >= 0 {
  2006. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  2007. }
  2008. }
  2009. if drup.MaxUnit != drup.MinUnit {
  2010. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  2011. return errors.New("库存数量不足")
  2012. }
  2013. }
  2014. if warehouse.StockMinNumber <= 0 {
  2015. warehouse.StockMinNumber = 0
  2016. }
  2017. //扣减库存数据
  2018. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  2019. if errThree != nil {
  2020. return errThree
  2021. }
  2022. //查询剩余库存
  2023. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  2024. var sum_count int64
  2025. for _, its := range stockInfo {
  2026. if its.MaxUnit == drup.MaxUnit {
  2027. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  2028. }
  2029. sum_count += its.StockMaxNumber + its.StockMinNumber
  2030. }
  2031. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2032. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2033. WarehouseOutId: warehouseout.ID,
  2034. Status: 1,
  2035. Ctime: time.Now().Unix(),
  2036. Remark: warehouse.Remark,
  2037. OrgId: orgID,
  2038. Type: 5,
  2039. Manufacturer: warehouse.Manufacturer,
  2040. Dealer: warehouse.Dealer,
  2041. IsSys: 1,
  2042. SysRecordTime: advice.RecordDate,
  2043. DrugId: advice.DrugId,
  2044. Number: warehouse.Number,
  2045. BatchNumber: warehouse.BatchNumber,
  2046. Price: warehouse.RetailPrice,
  2047. CountUnit: drup.MinUnit,
  2048. RetailPrice: warehouse.RetailPrice,
  2049. ProductDate: warehouse.ProductDate,
  2050. ExpiryDate: warehouse.ExpiryDate,
  2051. PatientId: advice.PatientId,
  2052. Count: deliver_number, //出最小单位
  2053. WarehouseInfoId: warehouse.ID,
  2054. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2055. StorehouseId: storeConfig.DrugStorehouseOut,
  2056. IsCheck: 1,
  2057. OverCount: sum_count,
  2058. AdviceId: advice.ID,
  2059. OrderId: warehouseout.OrderId,
  2060. }
  2061. //查询今日该药品该患者是否有出库数据
  2062. lastDrugOutInfo, _ := GetNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID)
  2063. if lastDrugOutInfo.ID == 0 {
  2064. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2065. if errOne != nil {
  2066. return errOne
  2067. }
  2068. }
  2069. if lastDrugOutInfo.ID > 0 {
  2070. var his_count int64
  2071. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  2072. his_count = lastDrugOutInfo.Count * drup.MinNumber
  2073. }
  2074. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  2075. his_count = lastDrugOutInfo.Count
  2076. }
  2077. if deliver_number != his_count {
  2078. errThree := UpdateSigleDrugWarehouseOutInfo(lastDrugOutInfo.ID, warehouseOutInfo)
  2079. if errThree != nil {
  2080. return errThree
  2081. }
  2082. }
  2083. }
  2084. var out_count int64
  2085. var cancel_count int64
  2086. //查询出库数据
  2087. infoCountList, _ := FindDrugSettleWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2088. //查询退库数据
  2089. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2090. for _, item := range infoCountList {
  2091. out_count += item.Count
  2092. }
  2093. for _, item := range cancelInfoList {
  2094. cancel_count += item.Count
  2095. }
  2096. fmt.Println("deliver_number", deliver_number)
  2097. fmt.Println("out_count", out_count)
  2098. fmt.Println("cancel_count", cancel_count)
  2099. //如果本次出库数据大于历史出库数据 新增1条流水
  2100. if deliver_number > (out_count - cancel_count) {
  2101. drugflow := models.DrugFlow{
  2102. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2103. WarehouseOutId: warehouseout.ID,
  2104. DrugId: advice.DrugId,
  2105. Number: warehouse.Number,
  2106. ProductDate: warehouse.ProductDate,
  2107. ExpireDate: warehouse.ExpiryDate,
  2108. Count: deliver_number - (out_count - cancel_count), //按最小单位计算,
  2109. Price: warehouse.RetailPrice,
  2110. Status: 1,
  2111. Ctime: time.Now().Unix(),
  2112. UserOrgId: orgID,
  2113. Manufacturer: warehouse.Manufacturer,
  2114. Dealer: warehouse.Dealer,
  2115. BatchNumber: warehouse.BatchNumber,
  2116. MaxUnit: drup.MinUnit,
  2117. ConsumableType: 15,
  2118. IsEdit: 1,
  2119. Creator: advice.ExecutionStaff,
  2120. IsSys: 1,
  2121. PatientId: advice.PatientId,
  2122. SystemTime: advice.AdviceDate,
  2123. WarehousingDetailId: warehouse.ID,
  2124. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2125. StorehouseId: storeConfig.DrugStorehouseOut,
  2126. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2127. OverCount: sum_count,
  2128. AdviceId: advice.ID,
  2129. LastPrice: warehouse.Price,
  2130. OrderId: warehouseout.OrderId,
  2131. }
  2132. CreateDrugFlowOne(drugflow)
  2133. //出库数量相加
  2134. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2135. }
  2136. if deliver_number == (out_count - cancel_count) {
  2137. drugflow := models.DrugFlow{
  2138. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2139. WarehouseOutId: warehouseout.ID,
  2140. DrugId: advice.DrugId,
  2141. Number: warehouse.Number,
  2142. ProductDate: warehouse.ProductDate,
  2143. ExpireDate: warehouse.ExpiryDate,
  2144. Count: deliver_number, //按最小单位计算,
  2145. Price: warehouse.RetailPrice,
  2146. Status: 1,
  2147. Ctime: time.Now().Unix(),
  2148. UserOrgId: orgID,
  2149. Manufacturer: warehouse.Manufacturer,
  2150. Dealer: warehouse.Dealer,
  2151. BatchNumber: warehouse.BatchNumber,
  2152. MaxUnit: drup.MinUnit,
  2153. ConsumableType: 15,
  2154. IsEdit: 1,
  2155. Creator: advice.ExecutionStaff,
  2156. IsSys: 1,
  2157. PatientId: advice.PatientId,
  2158. SystemTime: advice.AdviceDate,
  2159. WarehousingDetailId: warehouse.ID,
  2160. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2161. StorehouseId: storeConfig.DrugStorehouseOut,
  2162. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2163. OverCount: sum_count,
  2164. AdviceId: advice.ID,
  2165. LastPrice: warehouse.Price,
  2166. OrderId: warehouseout.OrderId,
  2167. }
  2168. CreateDrugFlowOne(drugflow)
  2169. //出库数量相加
  2170. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2171. }
  2172. //如果本次出库数据小于历史出库数据 新增1条退库流水
  2173. if deliver_number < (out_count - cancel_count) {
  2174. operation_time := time.Now().Unix()
  2175. ctime := time.Now().Unix()
  2176. timeStr := time.Now().Format("2006-01-02")
  2177. timeArr := strings.Split(timeStr, "-")
  2178. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  2179. total = total + 1
  2180. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  2181. cancelStock := models.DrugCancelStock{
  2182. OrderNumber: orderNumber,
  2183. OperaTime: operation_time,
  2184. OrgId: advice.UserOrgId,
  2185. Creater: advice.ExecutionStaff,
  2186. Ctime: ctime,
  2187. Status: 1,
  2188. ReturnTime: advice.AdviceDate,
  2189. Dealer: 0,
  2190. Manufacturer: 0,
  2191. Type: 5,
  2192. StorehouseId: storeConfig.DrugStorehouseOut,
  2193. IsCheck: 1,
  2194. OrderId: warehouseout.OrderId,
  2195. }
  2196. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2197. if errCode == gorm.ErrRecordNotFound {
  2198. AddSigleDrugCancelStock(&cancelStock)
  2199. }
  2200. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2201. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  2202. dealer, _ := GetDealerById(warehouse.Dealer)
  2203. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  2204. cancelStockInfo := models.DrugCancelStockInfo{
  2205. OrderNumber: cancelInfo.OrderNumber,
  2206. CancelStockId: lastDrugCancelStock.ID,
  2207. DrugId: advice.DrugId,
  2208. Count: (out_count - cancel_count) - deliver_number,
  2209. Status: 1,
  2210. Ctime: ctime,
  2211. OrgId: advice.UserOrgId,
  2212. Type: 5,
  2213. Manufacturer: manufactureName.ManufacturerName,
  2214. Dealer: dealer.DealerName,
  2215. Total: 0,
  2216. RetailPrice: warehouse.RetailPrice,
  2217. RetailTotalPrice: warehouse.RetailTotalPrice,
  2218. Price: warehouse.Price,
  2219. RegisterAccount: "",
  2220. Remark: warehouse.Remark,
  2221. BatchNumber: warehouse.BatchNumber,
  2222. MaxUnit: drup.MinUnit,
  2223. ProductDate: warehouse.ProductDate,
  2224. ExpiryDate: warehouse.ExpiryDate,
  2225. BatchNumberId: warehouse.ID,
  2226. StorehouseId: storeConfig.DrugStorehouseOut,
  2227. IsCheck: 1,
  2228. OrderId: warehouseout.OrderId,
  2229. }
  2230. CreatedCancelStock(cancelStockInfo)
  2231. flow := models.DrugFlow{
  2232. WarehousingId: warehouse.ID,
  2233. DrugId: warehouse.DrugId,
  2234. Number: "",
  2235. BatchNumber: warehouse.BatchNumber,
  2236. Count: out_count - cancel_count - deliver_number,
  2237. UserOrgId: advice.UserOrgId,
  2238. PatientId: advice.PatientId,
  2239. SystemTime: advice.AdviceDate,
  2240. ConsumableType: 7,
  2241. IsSys: 0,
  2242. WarehousingOrder: "",
  2243. WarehouseOutId: 0,
  2244. WarehouseOutOrderNumber: "",
  2245. IsEdit: 0,
  2246. CancelStockId: cancelStock.ID,
  2247. CancelOrderNumber: lastDrugCancelStock.OrderNumber,
  2248. Manufacturer: warehouse.Manufacturer,
  2249. Dealer: warehouse.Dealer,
  2250. Creator: advice.ExecutionStaff,
  2251. UpdateCreator: 0,
  2252. Status: 1,
  2253. Ctime: time.Now().Unix(),
  2254. Mtime: 0,
  2255. Price: warehouse.RetailPrice,
  2256. WarehousingDetailId: warehouse.ID,
  2257. WarehouseOutDetailId: 0,
  2258. CancelOutDetailId: lastDrugCancelStock.ID,
  2259. ExpireDate: warehouse.ExpiryDate,
  2260. ProductDate: warehouse.ProductDate,
  2261. MaxUnit: drup.MinUnit,
  2262. MinUnit: "",
  2263. AdviceId: advice.ID,
  2264. StorehouseId: storeConfig.DrugStorehouseOut,
  2265. LastPrice: warehouse.Price,
  2266. OverCount: sum_count,
  2267. OrderId: warehouseout.OrderId,
  2268. }
  2269. CreateDrugFlowOne(flow)
  2270. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  2271. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  2272. }
  2273. //查询是否存在数据
  2274. details := &models.DrugAutomaticReduceDetail{
  2275. WarehouseOutId: warehouseout.ID,
  2276. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2277. PatientId: advice.PatientId,
  2278. Ctime: time.Now().Unix(),
  2279. Mtime: time.Now().Unix(),
  2280. Status: 1,
  2281. RecordTime: advice.RecordDate,
  2282. OrgId: orgID,
  2283. DrugId: advice.DrugId,
  2284. Count: deliver_number,
  2285. CountUnit: drup.MinUnit,
  2286. WarehouseInfoId: warehouse.ID,
  2287. StorehouseId: storeConfig.DrugStorehouseOut,
  2288. AdviceId: advice.ID,
  2289. }
  2290. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2291. if errTwo != nil {
  2292. return errTwo
  2293. }
  2294. return nil
  2295. } else {
  2296. info := models.XtDrugWarehouseInfo{
  2297. ID: warehouse.ID,
  2298. WarehousingId: warehouse.WarehousingId,
  2299. DrugId: warehouse.DrugId,
  2300. Number: warehouse.Number,
  2301. ProductDate: warehouse.ProductDate,
  2302. ExpiryDate: warehouse.ExpiryDate,
  2303. WarehousingCount: warehouse.WarehousingCount,
  2304. Price: warehouse.RetailPrice,
  2305. TotalPrice: warehouse.TotalPrice,
  2306. Dealer: warehouse.Dealer,
  2307. Manufacturer: warehouse.Manufacturer,
  2308. Remark: warehouse.Remark,
  2309. Ctime: warehouse.Ctime,
  2310. Mtime: warehouse.Mtime,
  2311. Status: 1,
  2312. OrgId: warehouse.OrgId,
  2313. IsReturn: warehouse.IsReturn,
  2314. WarehousingOrder: warehouse.WarehousingOrder,
  2315. Type: warehouse.Type,
  2316. RetailPrice: warehouse.RetailPrice,
  2317. RetailTotalPrice: warehouse.RetailPrice,
  2318. StockMaxNumber: 0,
  2319. StockMinNumber: 0,
  2320. BatchNumber: warehouse.BatchNumber,
  2321. MaxUnit: warehouse.MaxUnit,
  2322. WarehousingInfoId: warehouse.WarehousingInfoId,
  2323. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2324. StorehouseId: storeConfig.DrugStorehouseOut,
  2325. IsCheck: 1,
  2326. }
  2327. //扣减库存
  2328. errThree := UpDateDrugWarehouseInfoByStock(&info)
  2329. if errThree != nil {
  2330. return errThree
  2331. }
  2332. //查询剩余库存
  2333. stockInfo, _ := GetDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId)
  2334. var sum_count int64
  2335. for _, its := range stockInfo {
  2336. if its.MaxUnit == drup.MaxUnit {
  2337. its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
  2338. }
  2339. sum_count += its.StockMaxNumber + its.StockMinNumber
  2340. }
  2341. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  2342. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  2343. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2344. WarehouseOutId: warehouseout.ID,
  2345. Status: 1,
  2346. Ctime: time.Now().Unix(),
  2347. Remark: warehouse.Remark,
  2348. OrgId: orgID,
  2349. Type: 5,
  2350. Manufacturer: warehouse.Manufacturer,
  2351. Dealer: warehouse.Dealer,
  2352. IsSys: 1,
  2353. SysRecordTime: advice.RecordDate,
  2354. DrugId: advice.DrugId,
  2355. Number: warehouse.Number,
  2356. BatchNumber: warehouse.BatchNumber,
  2357. Price: warehouse.RetailPrice,
  2358. CountUnit: drup.MinUnit,
  2359. ProductDate: warehouse.ProductDate,
  2360. ExpiryDate: warehouse.ExpiryDate,
  2361. PatientId: advice.PatientId,
  2362. Count: stock_number,
  2363. WarehouseInfoId: warehouse.ID,
  2364. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2365. StorehouseId: storeConfig.DrugStorehouseOut,
  2366. IsCheck: 1,
  2367. OverCount: sum_count,
  2368. AdviceId: advice.ID,
  2369. OrderId: warehouseout.OrderId,
  2370. }
  2371. //查询是否存在出库数据
  2372. _, errCode := GetSigleDrugWarehouseOutInfo(advice.PatientId, advice.AdviceDate, orgID)
  2373. if errCode == gorm.ErrRecordNotFound {
  2374. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  2375. if errOne != nil {
  2376. return errOne
  2377. }
  2378. }
  2379. if errCode == nil {
  2380. lastDrugOutInfo, _ := GetSigleDrugWarehouseOutInfoOne(advice.PatientId, advice.AdviceDate, orgID)
  2381. var his_count int64
  2382. if lastDrugOutInfo.CountUnit == drup.MaxUnit {
  2383. his_count = lastDrugOutInfo.Count * drup.MinNumber
  2384. }
  2385. if lastDrugOutInfo.CountUnit == drup.MinUnit {
  2386. his_count = lastDrugOutInfo.Count
  2387. }
  2388. if deliver_number != his_count {
  2389. errThree := UpdateSingleDrugWarehouseOutInfoSix(advice.PatientId, advice.AdviceDate, orgID, warehouseOutInfo, advice.ID)
  2390. if errThree != nil {
  2391. return errThree
  2392. }
  2393. }
  2394. }
  2395. lastDrugOutInfo, _ := FindLastDrugWarehouseOutInfo(orgID)
  2396. details := &models.DrugAutomaticReduceDetail{
  2397. WarehouseOutId: warehouseout.ID,
  2398. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  2399. PatientId: advice.PatientId,
  2400. Ctime: time.Now().Unix(),
  2401. Mtime: time.Now().Unix(),
  2402. Status: 1,
  2403. RecordTime: advice.RecordDate,
  2404. OrgId: orgID,
  2405. DrugId: advice.DrugId,
  2406. Count: stock_number,
  2407. CountUnit: drup.MinUnit,
  2408. WarehouseInfoId: warehouse.ID,
  2409. StorehouseId: storeConfig.DrugStorehouseOut,
  2410. AdviceId: advice.ID,
  2411. }
  2412. _, errcodeThree := FindDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId)
  2413. if errcodeThree == gorm.ErrRecordNotFound {
  2414. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  2415. if errTwo != nil {
  2416. return errTwo
  2417. }
  2418. }
  2419. if errcodeThree == nil {
  2420. UpdateDrugAutoReduceRecordInfo(advice.PatientId, advice.RecordDate, advice.DrugId, details)
  2421. }
  2422. var out_count int64
  2423. var cancel_count int64
  2424. //查询出库数据
  2425. infoCountList, _ := FindDrugSettleWarehouseOutInfoCount(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2426. //查询退库数据
  2427. cancelInfoList, _ := FindDrugCancelOutInfo(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID)
  2428. for _, item := range infoCountList {
  2429. out_count += item.Count
  2430. }
  2431. for _, item := range cancelInfoList {
  2432. cancel_count += item.Count
  2433. }
  2434. //如果本次出库数据大于历史出库数据 新增1条流水
  2435. if deliver_number > (out_count - cancel_count) {
  2436. drugflow := models.DrugFlow{
  2437. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  2438. WarehouseOutId: warehouseout.ID,
  2439. DrugId: advice.DrugId,
  2440. Number: warehouse.Number,
  2441. ProductDate: warehouse.ProductDate,
  2442. ExpireDate: warehouse.ExpiryDate,
  2443. Count: stock_number,
  2444. Price: warehouse.RetailPrice,
  2445. Status: 1,
  2446. Ctime: time.Now().Unix(),
  2447. UserOrgId: orgID,
  2448. Manufacturer: warehouse.Manufacturer,
  2449. Dealer: warehouse.Dealer,
  2450. BatchNumber: warehouse.BatchNumber,
  2451. MaxUnit: drup.MinUnit,
  2452. ConsumableType: 15,
  2453. IsEdit: 1,
  2454. Creator: advice.ExecutionStaff,
  2455. IsSys: 1,
  2456. PatientId: advice.PatientId,
  2457. WarehousingDetailId: warehouse.ID,
  2458. SupplyWarehouseId: warehouse.SupplyWarehouseId,
  2459. StorehouseId: storeConfig.DrugStorehouseOut,
  2460. WarehouseOutDetailId: lastDrugOutInfo.ID,
  2461. OverCount: sum_count,
  2462. AdviceId: advice.ID,
  2463. LastPrice: warehouse.Price,
  2464. SystemTime: advice.AdviceDate,
  2465. OrderId: warehouseout.OrderId,
  2466. }
  2467. CreateDrugFlowOne(drugflow)
  2468. //出库数量相加
  2469. AddDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count)
  2470. }
  2471. if deliver_number < (out_count - cancel_count) {
  2472. operation_time := time.Now().Unix()
  2473. ctime := time.Now().Unix()
  2474. timeStr := time.Now().Format("2006-01-02")
  2475. timeArr := strings.Split(timeStr, "-")
  2476. total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
  2477. total = total + 1
  2478. orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
  2479. cancelStock := models.DrugCancelStock{
  2480. OrderNumber: orderNumber,
  2481. OperaTime: operation_time,
  2482. OrgId: advice.UserOrgId,
  2483. Creater: advice.ExecutionStaff,
  2484. Ctime: ctime,
  2485. Status: 1,
  2486. ReturnTime: advice.AdviceDate,
  2487. Dealer: 0,
  2488. Manufacturer: 0,
  2489. Type: 5,
  2490. StorehouseId: storeConfig.DrugStorehouseOut,
  2491. IsCheck: 1,
  2492. OrderId: warehouseout.OrderId,
  2493. }
  2494. _, errCode := FindDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2495. if errCode == gorm.ErrRecordNotFound {
  2496. AddSigleDrugCancelStock(&cancelStock)
  2497. }
  2498. lastDrugCancelStock, _ := FindLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId)
  2499. manufactureName, _ := GetManufactureById(warehouse.Manufacturer)
  2500. dealer, _ := GetDealerById(warehouse.Dealer)
  2501. cancelInfo, _ := GetLastDrugCancelStockById(advice.UserOrgId)
  2502. cancelStockInfo := models.DrugCancelStockInfo{
  2503. OrderNumber: cancelInfo.OrderNumber,
  2504. CancelStockId: lastDrugCancelStock.ID,
  2505. DrugId: advice.DrugId,
  2506. Count: (out_count - cancel_count) - deliver_number,
  2507. Status: 1,
  2508. Ctime: ctime,
  2509. OrgId: advice.UserOrgId,
  2510. Type: 5,
  2511. Manufacturer: manufactureName.ManufacturerName,
  2512. Dealer: dealer.DealerName,
  2513. Total: 0,
  2514. RetailPrice: warehouse.RetailPrice,
  2515. RetailTotalPrice: warehouse.RetailTotalPrice,
  2516. Price: warehouse.Price,
  2517. RegisterAccount: "",
  2518. Remark: warehouse.Remark,
  2519. BatchNumber: warehouse.BatchNumber,
  2520. MaxUnit: drup.MinUnit,
  2521. ProductDate: warehouse.ProductDate,
  2522. ExpiryDate: warehouse.ExpiryDate,
  2523. BatchNumberId: warehouse.ID,
  2524. StorehouseId: storeConfig.DrugStorehouseOut,
  2525. IsCheck: 1,
  2526. OrderId: warehouseout.OrderId,
  2527. }
  2528. CreatedCancelStock(cancelStockInfo)
  2529. flow := models.DrugFlow{
  2530. WarehousingId: warehouse.ID,
  2531. DrugId: warehouse.DrugId,
  2532. Number: "",
  2533. BatchNumber: warehouse.BatchNumber,
  2534. Count: out_count - cancel_count - deliver_number,
  2535. UserOrgId: advice.UserOrgId,
  2536. PatientId: advice.PatientId,
  2537. SystemTime: advice.AdviceDate,
  2538. ConsumableType: 7,
  2539. IsSys: 0,
  2540. WarehousingOrder: "",
  2541. WarehouseOutId: 0,
  2542. WarehouseOutOrderNumber: "",
  2543. IsEdit: 0,
  2544. CancelStockId: cancelStock.ID,
  2545. CancelOrderNumber: lastDrugCancelStock.OrderNumber,
  2546. Manufacturer: warehouse.Manufacturer,
  2547. Dealer: warehouse.Dealer,
  2548. Creator: advice.ExecutionStaff,
  2549. UpdateCreator: 0,
  2550. Status: 1,
  2551. Ctime: time.Now().Unix(),
  2552. Mtime: 0,
  2553. Price: warehouse.RetailPrice,
  2554. WarehousingDetailId: warehouse.ID,
  2555. WarehouseOutDetailId: 0,
  2556. CancelOutDetailId: lastDrugCancelStock.ID,
  2557. ExpireDate: warehouse.ExpiryDate,
  2558. ProductDate: warehouse.ProductDate,
  2559. MaxUnit: drup.MinUnit,
  2560. MinUnit: "",
  2561. AdviceId: advice.ID,
  2562. StorehouseId: storeConfig.DrugStorehouseOut,
  2563. LastPrice: warehouse.Price,
  2564. OverCount: sum_count,
  2565. OrderId: warehouseout.OrderId,
  2566. }
  2567. CreateDrugFlowOne(flow)
  2568. //退库数量增加
  2569. AddCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count)
  2570. ReduceDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count)
  2571. }
  2572. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  2573. prescribingNumber_two_temp := deliver_number - stock_number
  2574. overPlusNumber := float64(prescribingNumber_two_temp)
  2575. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  2576. advice.PrescribingNumberUnit = drup.MinUnit
  2577. HisSettleDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  2578. }
  2579. return
  2580. }