warhouse_service.go 79KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "Xcx_New/utils"
  5. "errors"
  6. "fmt"
  7. _ "fmt"
  8. "github.com/jinzhu/gorm"
  9. "math"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // 药品出库
  15. func DrugsDelivery(orgID int64, creater int64, advice *models.DoctorAdvice) (err error) {
  16. // 1.判断药品是否来自专用字典的药品库
  17. // 2.判断当天当前机构有没有创建出库单,没有则创建
  18. // 3.创建出库流程
  19. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  20. isHasWay := false
  21. record_time := int64(0)
  22. if advice.Way == 1 {
  23. isHasWay = true
  24. record_time = advice.RecordDate
  25. }
  26. if isHasWay {
  27. //判断当天当前机构有没有创建出库单,没有则创建
  28. out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  29. if err == gorm.ErrRecordNotFound {
  30. timeStr := time.Now().Format("2006-01-02")
  31. timeArr := strings.Split(timeStr, "-")
  32. total, _ := FindAllDrugWarehouseOut(orgID)
  33. total = total + 1
  34. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  35. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  36. number = number + total
  37. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  38. // creater := adminUserInfo.AdminUser.Id
  39. warehouseOut := models.DrugWarehouseOut{
  40. WarehouseOutOrderNumber: warehousing_out_order,
  41. OperationTime: time.Now().Unix(),
  42. OrgId: orgID,
  43. Creater: creater,
  44. Ctime: time.Now().Unix(),
  45. Status: 1,
  46. WarehouseOutTime: record_time,
  47. Dealer: 0,
  48. Manufacturer: 0,
  49. Type: 1,
  50. IsSys: 1,
  51. }
  52. err := AddSigleDrugWarehouseOut(&warehouseOut)
  53. if err != nil {
  54. utils.TraceLog("创建出库单失败 err = %v", err)
  55. return err
  56. } else {
  57. out = warehouseOut
  58. }
  59. }
  60. // 出库流程
  61. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  62. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  63. if drup.ID > 0 {
  64. prescribingNumber := advice.PrescribingNumber
  65. DrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  66. } else {
  67. return errors.New("药品信息不存在")
  68. }
  69. }
  70. return
  71. }
  72. // 药品出库 递归方式
  73. func DrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DoctorAdvice) (err error) {
  74. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  75. var deliver_number int64 = 0
  76. var stock_number int64 = 0
  77. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  78. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  79. fmt.Println("单位1", advice.PrescribingNumber)
  80. fmt.Println("单位2", drup.MaxUnit)
  81. if advice.PrescribingNumberUnit == drup.MaxUnit {
  82. deliver_number = count * drup.MinNumber
  83. } else {
  84. deliver_number = count
  85. }
  86. // 根据先进先出原则,查询最先入库的批次,进行出库
  87. // 如果没有对应的库存,则报错
  88. //开启事物
  89. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId)
  90. if err != nil {
  91. return err
  92. }
  93. // 将该批次的剩余库存数量转换为拆零数量
  94. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  95. fmt.Println("stock_number999999999999", stock_number)
  96. fmt.Println("deliver_number88888888888", deliver_number)
  97. //查找药品信息
  98. baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  99. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  100. if stock_number >= deliver_number {
  101. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  102. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  103. WarehouseOutId: warehouseout.ID,
  104. Status: 1,
  105. Ctime: time.Now().Unix(),
  106. Remark: warehouse.Remark,
  107. OrgId: orgID,
  108. Type: 1,
  109. Manufacturer: warehouse.Manufacturer,
  110. Dealer: warehouse.Dealer,
  111. IsSys: 1,
  112. SysRecordTime: advice.RecordDate,
  113. DrugId: advice.DrugId,
  114. Number: warehouse.Number,
  115. BatchNumber: warehouse.BatchNumber,
  116. Price: baseInfo.RetailPrice,
  117. CountUnit: advice.PrescribingNumberUnit,
  118. RetailPrice: baseInfo.RetailPrice,
  119. ProductDate: warehouse.ProductDate,
  120. ExpiryDate: warehouse.ExpiryDate,
  121. PatientId: advice.PatientId,
  122. Count: count,
  123. WarehouseInfoId: warehouse.ID,
  124. }
  125. fmt.Println("单据号2232322323232323232323232323232323", warehouseout.WarehouseOutOrderNumber)
  126. drugflow := models.DrugFlow{
  127. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  128. WarehouseOutId: warehouseout.ID,
  129. DrugId: advice.DrugId,
  130. Number: warehouse.Number,
  131. ProductDate: warehouse.ProductDate,
  132. ExpireDate: warehouse.ExpiryDate,
  133. Count: count,
  134. Price: baseInfo.RetailPrice,
  135. Status: 1,
  136. Ctime: time.Now().Unix(),
  137. UserOrgId: orgID,
  138. Manufacturer: warehouse.Manufacturer,
  139. Dealer: warehouse.Dealer,
  140. BatchNumber: warehouse.BatchNumber,
  141. MaxUnit: advice.PrescribingNumberUnit,
  142. ConsumableType: 3,
  143. IsEdit: 1,
  144. Creator: advice.ExecutionStaff,
  145. IsSys: 1,
  146. PatientId: advice.PatientId,
  147. }
  148. CreateDrugFlowOne(drugflow)
  149. //查询是否存在数据
  150. _, errcodes := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  151. if errcodes == gorm.ErrRecordNotFound {
  152. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  153. if errOne != nil {
  154. return errOne
  155. }
  156. } else if errcodes == nil {
  157. UpdatedDrugWarehouseOutInfo(warehouseOutInfo, advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId)
  158. }
  159. details := &models.DrugAutomaticReduceDetail{
  160. WarehouseOutId: warehouseout.ID,
  161. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  162. PatientId: advice.PatientId,
  163. Ctime: time.Now().Unix(),
  164. Mtime: time.Now().Unix(),
  165. Status: 1,
  166. RecordTime: advice.RecordDate,
  167. OrgId: orgID,
  168. DrugId: advice.DrugId,
  169. Count: count,
  170. CountUnit: advice.PrescribingNumberUnit,
  171. }
  172. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  173. if errTwo != nil {
  174. return errTwo
  175. }
  176. // 出库完成后,要减去对应批次的库存数量
  177. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  178. var maxNumber int64 = 0
  179. var minNumber int64 = 0
  180. fmt.Println("单位1111111111", advice.PrescribingNumberUnit)
  181. fmt.Println("单位222222222222222", drup.MaxUnit)
  182. if advice.PrescribingNumberUnit == drup.MaxUnit {
  183. maxNumber = count / drup.MinNumber
  184. minNumber = count % drup.MinNumber
  185. } else {
  186. minNumber = count
  187. }
  188. fmt.Println("maxNumber0000000000000", maxNumber)
  189. fmt.Println("minNumber999999999999999", minNumber)
  190. if warehouse.StockMaxNumber < maxNumber {
  191. return errors.New("库存数量不足")
  192. }
  193. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  194. warehouse.Mtime = time.Now().Unix()
  195. if warehouse.StockMinNumber < minNumber {
  196. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  197. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  198. } else {
  199. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  200. }
  201. if warehouse.StockMaxNumber < 0 {
  202. return errors.New("库存数量不足")
  203. }
  204. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  205. if errThree != nil {
  206. return errThree
  207. }
  208. return nil
  209. } else {
  210. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  211. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  212. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  213. WarehouseOutId: warehouseout.ID,
  214. Status: 1,
  215. Ctime: time.Now().Unix(),
  216. Remark: warehouse.Remark,
  217. OrgId: orgID,
  218. Type: 1,
  219. Manufacturer: warehouse.Manufacturer,
  220. Dealer: warehouse.Dealer,
  221. IsSys: 1,
  222. SysRecordTime: advice.RecordDate,
  223. DrugId: advice.DrugId,
  224. Number: warehouse.Number,
  225. BatchNumber: warehouse.BatchNumber,
  226. Price: baseInfo.RetailPrice,
  227. CountUnit: advice.PrescribingNumberUnit,
  228. ProductDate: warehouse.ProductDate,
  229. ExpiryDate: warehouse.ExpiryDate,
  230. PatientId: advice.PatientId,
  231. Count: count,
  232. WarehouseInfoId: warehouse.ID,
  233. }
  234. //查询是否存在数据
  235. _, errcodes := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  236. if errcodes == gorm.ErrRecordNotFound {
  237. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  238. if errOne != nil {
  239. return errOne
  240. }
  241. } else if errcodes == nil {
  242. UpdatedDrugWarehouseOutInfo(warehouseOutInfo, advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId)
  243. }
  244. details := &models.DrugAutomaticReduceDetail{
  245. WarehouseOutId: warehouseout.ID,
  246. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  247. PatientId: advice.PatientId,
  248. Ctime: time.Now().Unix(),
  249. Mtime: time.Now().Unix(),
  250. Status: 1,
  251. RecordTime: advice.RecordDate,
  252. OrgId: orgID,
  253. DrugId: advice.DrugId,
  254. Count: stock_number,
  255. CountUnit: advice.PrescribingNumberUnit,
  256. }
  257. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  258. if errTwo != nil {
  259. return errTwo
  260. }
  261. // 出库完成后,要将该批次库存清零
  262. warehouse.StockMaxNumber = 0
  263. warehouse.StockMinNumber = 0
  264. warehouse.Mtime = time.Now().Unix()
  265. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  266. if errThree != nil {
  267. return errThree
  268. }
  269. drugflow := models.DrugFlow{
  270. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  271. WarehouseOutId: warehouseout.ID,
  272. DrugId: advice.DrugId,
  273. Number: warehouse.Number,
  274. ProductDate: warehouse.ProductDate,
  275. ExpireDate: warehouse.ExpiryDate,
  276. Count: stock_number,
  277. Price: baseInfo.RetailPrice,
  278. Status: 1,
  279. Ctime: time.Now().Unix(),
  280. UserOrgId: orgID,
  281. Manufacturer: warehouse.Manufacturer,
  282. Dealer: warehouse.Dealer,
  283. BatchNumber: warehouse.BatchNumber,
  284. MaxUnit: advice.PrescribingNumberUnit,
  285. ConsumableType: 3,
  286. IsEdit: 1,
  287. Creator: advice.ExecutionStaff,
  288. IsSys: 1,
  289. PatientId: advice.PatientId,
  290. }
  291. CreateDrugFlowOne(drugflow)
  292. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  293. prescribingNumber_two_temp := deliver_number - stock_number
  294. overPlusNumber := float64(prescribingNumber_two_temp)
  295. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  296. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  297. DrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  298. }
  299. return
  300. }
  301. // 药品出库
  302. func HisDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo) (err error) {
  303. // 1.判断药品是否来自专用字典的药品库
  304. // 2.判断当天当前机构有没有创建出库单,没有则创建
  305. // 3.创建出库流程
  306. // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
  307. isHasWay := false
  308. record_time := int64(0)
  309. isHasWay = true
  310. record_time = advice.RecordDate
  311. if isHasWay {
  312. //判断当天当前机构有没有创建出库单,没有则创建
  313. out, err := FindDrugStockOutByIsSys(orgID, 1, record_time)
  314. if err == gorm.ErrRecordNotFound {
  315. timeStr := time.Now().Format("2006-01-02")
  316. timeArr := strings.Split(timeStr, "-")
  317. total, _ := FindAllDrugWarehouseOut(orgID)
  318. total = total + 1
  319. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  320. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  321. number = number + total
  322. warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
  323. // creater := adminUserInfo.AdminUser.Id
  324. warehouseOut := models.DrugWarehouseOut{
  325. WarehouseOutOrderNumber: warehousing_out_order,
  326. OperationTime: time.Now().Unix(),
  327. OrgId: orgID,
  328. Creater: creater,
  329. Ctime: time.Now().Unix(),
  330. Status: 1,
  331. WarehouseOutTime: record_time,
  332. Dealer: 0,
  333. Manufacturer: 0,
  334. Type: 1,
  335. IsSys: 1,
  336. }
  337. err := AddSigleDrugWarehouseOut(&warehouseOut)
  338. if err != nil {
  339. utils.TraceLog("创建出库单失败 err = %v", err)
  340. return err
  341. } else {
  342. out = warehouseOut
  343. }
  344. }
  345. // 出库流程
  346. // 1.查询改药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
  347. drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
  348. if drup.ID > 0 {
  349. prescribingNumber := advice.PrescribingNumber
  350. HisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice)
  351. } else {
  352. return errors.New("药品信息不存在")
  353. }
  354. }
  355. return
  356. }
  357. // 药品出库 递归方式
  358. func HisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo) (err error) {
  359. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  360. var deliver_number int64 = 0
  361. var stock_number int64 = 0
  362. prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
  363. count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
  364. if advice.PrescribingNumberUnit == drup.MaxUnit {
  365. deliver_number = count * drup.MinNumber
  366. } else {
  367. deliver_number = count
  368. }
  369. // 根据先进先出原则,查询最先入库的批次,进行出库
  370. // 如果没有对应的库存,则报错
  371. //开启事物
  372. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId)
  373. //查找药品信息
  374. baseInfo, _ := FindBaseDrugLibRecord(advice.UserOrgId, advice.DrugId)
  375. if err != nil {
  376. return err
  377. }
  378. // 将该批次的剩余库存数量转换为拆零数量
  379. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  380. fmt.Println("拆零数量2222222222222222222", stock_number)
  381. fmt.Println("出库数量55555555555555555555", deliver_number)
  382. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  383. if stock_number >= deliver_number {
  384. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  385. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  386. WarehouseOutId: warehouseout.ID,
  387. Status: 1,
  388. Ctime: time.Now().Unix(),
  389. Remark: warehouse.Remark,
  390. OrgId: orgID,
  391. Type: 1,
  392. Manufacturer: warehouse.Manufacturer,
  393. Dealer: warehouse.Dealer,
  394. IsSys: 1,
  395. SysRecordTime: advice.RecordDate,
  396. DrugId: advice.DrugId,
  397. Number: warehouse.Number,
  398. BatchNumber: warehouse.BatchNumber,
  399. Price: baseInfo.RetailPrice,
  400. CountUnit: baseInfo.MaxUnit,
  401. RetailPrice: baseInfo.RetailPrice,
  402. ProductDate: warehouse.ProductDate,
  403. ExpiryDate: warehouse.ExpiryDate,
  404. PatientId: advice.PatientId,
  405. WarehouseInfoId: warehouse.ID,
  406. }
  407. warehouseOutInfo.Count = count
  408. warehouseOutInfo.CountUnit = advice.PrescribingNumberUnit
  409. drugflow := models.DrugFlow{
  410. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  411. WarehouseOutId: warehouseout.ID,
  412. DrugId: advice.DrugId,
  413. Number: warehouse.Number,
  414. ProductDate: warehouse.ProductDate,
  415. ExpireDate: warehouse.ExpiryDate,
  416. Count: count,
  417. Price: baseInfo.RetailPrice,
  418. Status: 1,
  419. Ctime: time.Now().Unix(),
  420. UserOrgId: orgID,
  421. Manufacturer: warehouse.Manufacturer,
  422. Dealer: warehouse.Dealer,
  423. BatchNumber: warehouse.BatchNumber,
  424. MaxUnit: advice.PrescribingNumberUnit,
  425. ConsumableType: 3,
  426. IsEdit: 1,
  427. Creator: advice.ExecutionStaff,
  428. IsSys: 1,
  429. PatientId: advice.PatientId,
  430. }
  431. CreateDrugFlowOne(drugflow)
  432. //查询是否存在数据
  433. _, errcodes := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  434. if errcodes == gorm.ErrRecordNotFound {
  435. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  436. if errOne != nil {
  437. return errOne
  438. }
  439. } else if errcodes == nil {
  440. UpdatedDrugWarehouseOutInfo(warehouseOutInfo, advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId)
  441. }
  442. details := &models.DrugAutomaticReduceDetail{
  443. WarehouseOutId: warehouseout.ID,
  444. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  445. PatientId: advice.PatientId,
  446. Ctime: time.Now().Unix(),
  447. Mtime: time.Now().Unix(),
  448. Status: 1,
  449. RecordTime: advice.RecordDate,
  450. OrgId: orgID,
  451. DrugId: advice.DrugId,
  452. Count: count,
  453. CountUnit: advice.PrescribingNumberUnit,
  454. }
  455. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  456. if errTwo != nil {
  457. return errTwo
  458. }
  459. // 出库完成后,要减去对应批次的库存数量
  460. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  461. var maxNumber int64 = 0
  462. var minNumber int64 = 0
  463. if advice.PrescribingNumberUnit == drup.MinUnit {
  464. maxNumber = count / drup.MinNumber
  465. minNumber = count % drup.MinNumber
  466. } else {
  467. maxNumber = count
  468. }
  469. if warehouse.StockMaxNumber < maxNumber {
  470. return errors.New("库存数量不足")
  471. }
  472. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  473. warehouse.Mtime = time.Now().Unix()
  474. if warehouse.StockMinNumber < minNumber {
  475. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  476. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  477. } else {
  478. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  479. }
  480. if warehouse.StockMaxNumber < 0 {
  481. return errors.New("库存数量不足")
  482. }
  483. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  484. if errThree != nil {
  485. return errThree
  486. }
  487. return nil
  488. } else {
  489. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  490. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  491. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  492. WarehouseOutId: warehouseout.ID,
  493. Status: 1,
  494. Ctime: time.Now().Unix(),
  495. Remark: warehouse.Remark,
  496. OrgId: orgID,
  497. Type: 1,
  498. Manufacturer: warehouse.Manufacturer,
  499. Dealer: warehouse.Dealer,
  500. IsSys: 1,
  501. SysRecordTime: advice.RecordDate,
  502. DrugId: advice.DrugId,
  503. Number: warehouse.Number,
  504. BatchNumber: warehouse.BatchNumber,
  505. Price: baseInfo.RetailPrice,
  506. RetailPrice: baseInfo.RetailPrice,
  507. ProductDate: warehouse.ProductDate,
  508. ExpiryDate: warehouse.ExpiryDate,
  509. PatientId: advice.PatientId,
  510. WarehouseInfoId: warehouse.ID,
  511. }
  512. warehouseOutInfo.Count = count
  513. warehouseOutInfo.CountUnit = advice.PrescribingNumberUnit
  514. drugflow := models.DrugFlow{
  515. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  516. WarehouseOutId: warehouseout.ID,
  517. DrugId: advice.DrugId,
  518. Number: warehouse.Number,
  519. ProductDate: warehouse.ProductDate,
  520. ExpireDate: warehouse.ExpiryDate,
  521. Count: stock_number,
  522. Price: baseInfo.RetailPrice,
  523. Status: 1,
  524. Ctime: time.Now().Unix(),
  525. UserOrgId: orgID,
  526. Manufacturer: warehouse.Manufacturer,
  527. Dealer: warehouse.Dealer,
  528. BatchNumber: warehouse.BatchNumber,
  529. MaxUnit: advice.PrescribingNumberUnit,
  530. ConsumableType: 3,
  531. IsEdit: 1,
  532. Creator: advice.ExecutionStaff,
  533. IsSys: 1,
  534. PatientId: advice.PatientId,
  535. }
  536. CreateDrugFlowOne(drugflow)
  537. //查询是否存在数据
  538. _, errcodes := GetDrugWarehouseOutInfoIsExit(advice.DrugId, advice.PatientId, advice.RecordDate)
  539. if errcodes == gorm.ErrRecordNotFound {
  540. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  541. if errOne != nil {
  542. return errOne
  543. }
  544. } else if errcodes == nil {
  545. UpdatedDrugWarehouseOutInfo(warehouseOutInfo, advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId)
  546. }
  547. details := &models.DrugAutomaticReduceDetail{
  548. WarehouseOutId: warehouseout.ID,
  549. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  550. PatientId: advice.PatientId,
  551. Ctime: time.Now().Unix(),
  552. Mtime: time.Now().Unix(),
  553. Status: 1,
  554. RecordTime: advice.RecordDate,
  555. OrgId: orgID,
  556. DrugId: advice.DrugId,
  557. Count: count,
  558. CountUnit: advice.PrescribingNumberUnit,
  559. }
  560. errTwo := AddSigleDrugAutoReduceRecordInfo(details)
  561. if errTwo != nil {
  562. return errTwo
  563. }
  564. // 出库完成后,要将该批次库存清零
  565. warehouse.StockMaxNumber = 0
  566. warehouse.StockMinNumber = 0
  567. warehouse.Mtime = time.Now().Unix()
  568. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  569. if errThree != nil {
  570. return errThree
  571. }
  572. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  573. prescribingNumber_two_temp := deliver_number - stock_number
  574. overPlusNumber := float64(prescribingNumber_two_temp)
  575. advice.PrescribingNumber = float64(prescribingNumber_two_temp)
  576. advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
  577. HisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice)
  578. }
  579. return
  580. }
  581. // 药品出库 递归方式
  582. func AutoDrugDeliverInfo(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
  583. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  584. var deliver_number int64 = 0
  585. var stock_number int64 = 0
  586. fmt.Println("单位1", advice.CountUnit)
  587. fmt.Println("单位2", drup.MaxUnit)
  588. if advice.CountUnit == drup.MaxUnit {
  589. deliver_number = prescribingNumber * drup.MinNumber
  590. } else {
  591. deliver_number = prescribingNumber
  592. }
  593. // 根据先进先出原则,查询最先入库的批次,进行出库
  594. // 如果没有对应的库存,则报错
  595. //开启事物
  596. warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId)
  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. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  605. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  606. WarehouseOutId: warehouseout.ID,
  607. Status: 1,
  608. Ctime: time.Now().Unix(),
  609. Remark: advice.Remark,
  610. OrgId: orgID,
  611. Type: 1,
  612. Manufacturer: advice.Manufacturer,
  613. Dealer: advice.Dealer,
  614. IsSys: 0,
  615. SysRecordTime: advice.Ctime,
  616. DrugId: advice.DrugId,
  617. ExpiryDate: advice.ExpiryDate,
  618. ProductDate: advice.ProductDate,
  619. Number: advice.Number,
  620. BatchNumber: advice.BatchNumber,
  621. Count: advice.Count,
  622. RetailPrice: advice.RetailPrice,
  623. Price: advice.Price,
  624. WarehouseInfoId: advice.WarehouseInfoId,
  625. }
  626. warehouseOutInfo.Count = prescribingNumber
  627. warehouseOutInfo.CountUnit = advice.CountUnit
  628. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  629. if errOne != nil {
  630. return errOne
  631. }
  632. // 出库完成后,要减去对应批次的库存数量
  633. // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
  634. var maxNumber int64 = 0
  635. var minNumber int64 = 0
  636. if advice.CountUnit == drup.MinUnit {
  637. maxNumber = prescribingNumber / drup.MinNumber
  638. minNumber = prescribingNumber % drup.MinNumber
  639. } else {
  640. maxNumber = prescribingNumber
  641. }
  642. if warehouse.StockMaxNumber < maxNumber {
  643. return errors.New("库存数量不足")
  644. }
  645. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  646. warehouse.Mtime = time.Now().Unix()
  647. if warehouse.StockMinNumber < minNumber {
  648. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  649. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  650. } else {
  651. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  652. }
  653. if warehouse.StockMaxNumber < 0 {
  654. return errors.New("库存数量不足")
  655. }
  656. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  657. if errThree != nil {
  658. return errThree
  659. }
  660. return nil
  661. } else {
  662. // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  663. warehouseOutInfo := &models.DrugWarehouseOutInfo{
  664. WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
  665. WarehouseOutId: warehouseout.ID,
  666. Status: 1,
  667. Ctime: time.Now().Unix(),
  668. Remark: advice.Remark,
  669. OrgId: orgID,
  670. Type: 1,
  671. Manufacturer: advice.Manufacturer,
  672. Dealer: advice.Dealer,
  673. IsSys: 0,
  674. SysRecordTime: advice.Ctime,
  675. DrugId: advice.DrugId,
  676. ExpiryDate: advice.ExpiryDate,
  677. ProductDate: advice.ProductDate,
  678. Number: advice.Number,
  679. BatchNumber: advice.BatchNumber,
  680. Count: advice.Count,
  681. RetailPrice: advice.RetailPrice,
  682. Price: advice.Price,
  683. }
  684. warehouseOutInfo.Count = prescribingNumber
  685. warehouseOutInfo.CountUnit = advice.CountUnit
  686. errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
  687. if errOne != nil {
  688. return errOne
  689. }
  690. // 出库完成后,要将该批次库存清零
  691. warehouse.StockMaxNumber = 0
  692. warehouse.StockMinNumber = 0
  693. warehouse.Mtime = time.Now().Unix()
  694. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  695. if errThree != nil {
  696. return errThree
  697. }
  698. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  699. prescribingNumber_two_temp := deliver_number - stock_number
  700. advice.CountUnit = drup.MinUnit
  701. AutoDrugDeliverInfo(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
  702. }
  703. return
  704. }
  705. // 耗材出库
  706. func ConsumablesDeliveryTotal(orgID int64, patient_id int64, record_time int64, goods []*models.DialysisBeforePrepareGoods, goodOne []*models.NewDialysisBeforePrepareGoods, creator int64) (err error) {
  707. //查询该患者当天已经出库的耗材信息
  708. goods_yc, _ := FindConsumablesByDateTwo(orgID, patient_id, record_time)
  709. // 和新请求的出库数据进行对比,分出那些是继续出库的,那些是需要删除出库的
  710. for i := len(goods_yc) - 1; i >= 0; i-- {
  711. goods_yc_temp := goods_yc[i]
  712. for j := len(goods) - 1; j >= 0; j-- {
  713. goods_temp := goods[j]
  714. // 已经出库和新请求出库都存在该耗材,则判断出库数量,分成是继续出库,还是删除出库
  715. if goods_yc_temp.GoodTypeId == goods_temp.GoodTypeId && goods_yc_temp.GoodId == goods_temp.GoodId {
  716. // 已经出库和新请求出库的出库数量一致,则清除两个结构体里的数据(既不出库,也不删除出库)
  717. if goods_yc_temp.Count == goods_temp.Count {
  718. goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  719. goods = append(goods[:j], goods[j+1:]...)
  720. break
  721. }
  722. // 如果已经出库的数量 大于 新请求出库的数量,则代表需要删除出库
  723. if goods_yc_temp.Count > goods_temp.Count {
  724. temp_count := goods_yc_temp.Count - goods_temp.Count
  725. goods_yc[i].Count = temp_count
  726. goods = append(goods[:j], goods[j+1:]...)
  727. break
  728. }
  729. // 如果已经出库的数量 小于 新请求出库的梳理,则代表需要增加出库
  730. if goods_yc_temp.Count < goods_temp.Count {
  731. temp_count := goods_temp.Count - goods_yc_temp.Count
  732. //fmt.Println("988888888888888", temp_count)
  733. goods[j].Count = temp_count
  734. goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  735. //fmt.Println("888888888", goods_yc)
  736. break
  737. }
  738. }
  739. }
  740. }
  741. // goods_yc 这个数据就是需要已经出库了,但是现在需要删除出库的耗材数据
  742. // goods 这个数据就是需要出库的耗材的数据(新增的数据)
  743. fmt.Println("goods222222222222", goods)
  744. fmt.Println("goodsy999999999999", goods_yc)
  745. if len(goods) > 0 {
  746. out, err := FindStockOutByIsSys(orgID, 1, record_time)
  747. if err == gorm.ErrRecordNotFound {
  748. //没有记录,则创建出库单
  749. timeStr := time.Now().Format("2006-01-02")
  750. timeArr := strings.Split(timeStr, "-")
  751. total, _ := FindAllWarehouseOut(orgID)
  752. total = total + 1
  753. warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
  754. number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
  755. number = number + total
  756. warehousing_out_order = "CKD" + strconv.FormatInt(number, 10)
  757. warehouseOut := models.WarehouseOut{
  758. WarehouseOutOrderNumber: warehousing_out_order,
  759. OperationTime: time.Now().Unix(),
  760. OrgId: orgID,
  761. Creater: creator,
  762. Ctime: time.Now().Unix(),
  763. Status: 1,
  764. WarehouseOutTime: record_time,
  765. Dealer: 0,
  766. Manufacturer: 0,
  767. Type: 1,
  768. IsSys: 1,
  769. }
  770. err := AddSigleWarehouseOut(&warehouseOut)
  771. if err != nil {
  772. utils.TraceLog("创建出库单失败 err = %v", err)
  773. return err
  774. } else {
  775. out = warehouseOut
  776. }
  777. }
  778. for _, item := range goods {
  779. var newCount int64 = 0
  780. for _, it := range goodOne {
  781. if item.GoodTypeId == it.GoodTypeId && item.GoodId == it.GoodId {
  782. newCount = it.Count
  783. }
  784. }
  785. prepare := models.DialysisBeforePrepare{
  786. GoodTypeId: item.GoodTypeId,
  787. GoodId: item.GoodId,
  788. Count: item.Count,
  789. }
  790. ConsumablesDelivery(orgID, patient_id, record_time, &prepare, &out, newCount)
  791. }
  792. }
  793. if len(goods_yc) > 0 {
  794. for _, good_yc := range goods_yc {
  795. out, _ := FindStockOutByIsSys(orgID, 1, record_time)
  796. ConsumablesDeliveryDelete(orgID, patient_id, record_time, good_yc, &out)
  797. }
  798. }
  799. return nil
  800. }
  801. //耗材出库
  802. func ConsumablesDelivery(orgID int64, patient_id int64, record_time int64, goods *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut, count int64) (err error) {
  803. fmt.Println("count23232323232323232323232323", count)
  804. fmt.Println("hhhh2h3h3h2h23h32hh23h32h23", goods.Count)
  805. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  806. var deliver_number int64 = 0
  807. var stock_number int64 = 0
  808. var maxNumber int64 = 0
  809. deliver_number = goods.Count
  810. // 根据先进先出原则,查询最先入库的批次,进行出库
  811. // 如果没有对应的库存,则报错
  812. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId)
  813. if err != nil {
  814. return errors.New("库存数量不足")
  815. }
  816. // 将该批次的剩余库存数量转换为拆零数量
  817. stock_number = warehouse.StockCount
  818. //获取耗材信息
  819. info, _ := GetGoodInformationByGoodId(goods.GoodId)
  820. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  821. if stock_number >= deliver_number {
  822. warehouseOutInfo := &models.WarehouseOutInfo{
  823. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  824. WarehouseOutId: warehouseOut.ID,
  825. WarehouseInfotId: warehouse.ID,
  826. Status: 1,
  827. Ctime: time.Now().Unix(),
  828. Remark: warehouse.Remark,
  829. OrgId: orgID,
  830. Type: 1,
  831. Manufacturer: warehouse.Manufacturer,
  832. Dealer: warehouse.Dealer,
  833. IsSys: 1,
  834. SysRecordTime: record_time,
  835. GoodTypeId: goods.GoodTypeId,
  836. GoodId: goods.GoodId,
  837. PatientId: patient_id,
  838. Number: warehouse.Number,
  839. LicenseNumber: warehouse.LicenseNumber,
  840. Price: info.PackingPrice,
  841. ExpiryDate: warehouse.ExpiryDate,
  842. ProductDate: warehouse.ProductDate,
  843. }
  844. warehouseOutInfo.Count = count
  845. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, patient_id, record_time)
  846. if errcodes == gorm.ErrRecordNotFound {
  847. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  848. if errOne != nil {
  849. return errOne
  850. }
  851. } else if errcodes == nil {
  852. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, patient_id, record_time)
  853. }
  854. details := models.BloodAutomaticReduceDetail{
  855. WarehouseOutId: warehouseOutInfo.ID,
  856. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  857. PatientId: patient_id,
  858. Ctime: time.Now().Unix(),
  859. Mtime: time.Now().Unix(),
  860. Status: 1,
  861. RecordTime: record_time,
  862. OrgId: orgID,
  863. GoodId: goods.GoodId,
  864. GoodTypeId: goods.GoodTypeId,
  865. Count: count,
  866. }
  867. //查询当天耗材是否已经存在数据
  868. _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  869. if errcode == gorm.ErrRecordNotFound {
  870. errTwo := CreateAutoReduceRecord(&details)
  871. if errTwo != nil {
  872. return errTwo
  873. }
  874. //插入库存流水表
  875. flow := models.VmStockFlow{
  876. WarehouseOutId: warehouseOut.ID,
  877. WarehousingId: warehouse.ID,
  878. GoodId: goods.GoodId,
  879. Number: warehouse.Number,
  880. ProductDate: warehouse.ProductDate,
  881. ExpireDate: warehouse.ExpiryDate,
  882. Count: deliver_number,
  883. Price: info.PackingPrice,
  884. Status: 1,
  885. Ctime: time.Now().Unix(),
  886. UserOrgId: orgID,
  887. Manufacturer: info.Manufacturer,
  888. Dealer: info.Dealer,
  889. LicenseNumber: warehouse.LicenseNumber,
  890. IsEdit: 2,
  891. Creator: warehouseOut.Creater,
  892. SystemTime: record_time,
  893. ConsumableType: 3,
  894. WarehouseOutDetailId: warehouseOutInfo.ID,
  895. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  896. IsSys: 1,
  897. PatientId: patient_id,
  898. }
  899. //查询流水是否存在
  900. exsit, errflow := GetStockFlowIsExsit(warehouseOut.ID, patient_id, record_time, goods.GoodId)
  901. fmt.Println("什么数据233232323", errflow)
  902. if errflow == gorm.ErrRecordNotFound {
  903. errThre := CreateStockFlowOne(flow)
  904. if errThre != nil {
  905. return errThre
  906. }
  907. } else if errflow == nil {
  908. flow := models.VmStockFlow{
  909. ID: exsit.ID,
  910. WarehouseOutId: warehouseOut.ID,
  911. WarehousingId: warehouse.ID,
  912. GoodId: goods.GoodId,
  913. Number: warehouse.Number,
  914. ProductDate: warehouse.ProductDate,
  915. ExpireDate: warehouse.ExpiryDate,
  916. Count: exsit.Count + goods.Count,
  917. Price: info.PackingPrice,
  918. Status: 1,
  919. Ctime: time.Now().Unix(),
  920. UserOrgId: orgID,
  921. Manufacturer: info.Manufacturer,
  922. Dealer: info.Dealer,
  923. LicenseNumber: warehouse.LicenseNumber,
  924. IsEdit: 2,
  925. Creator: warehouseOut.Creater,
  926. SystemTime: record_time,
  927. ConsumableType: 3,
  928. WarehouseOutDetailId: warehouseOutInfo.ID,
  929. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  930. IsSys: 1,
  931. PatientId: patient_id,
  932. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  933. }
  934. errFour := UpdatedStockFlow(flow)
  935. if errFour != nil {
  936. return errFour
  937. }
  938. }
  939. } else if errcode == nil {
  940. flow := models.VmStockFlow{
  941. WarehouseOutId: warehouseOut.ID,
  942. WarehousingId: warehouse.ID,
  943. GoodId: goods.GoodId,
  944. Number: warehouse.Number,
  945. ProductDate: warehouse.ProductDate,
  946. ExpireDate: warehouse.ExpiryDate,
  947. Count: deliver_number,
  948. Price: info.PackingPrice,
  949. Status: 1,
  950. Ctime: time.Now().Unix(),
  951. UserOrgId: orgID,
  952. Manufacturer: info.Manufacturer,
  953. Dealer: info.Dealer,
  954. LicenseNumber: warehouse.LicenseNumber,
  955. IsEdit: 2,
  956. Creator: warehouseOut.Creater,
  957. SystemTime: record_time,
  958. ConsumableType: 3,
  959. WarehouseOutDetailId: warehouseOutInfo.ID,
  960. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  961. IsSys: 1,
  962. PatientId: patient_id,
  963. }
  964. //查询流水是否存在
  965. exsit, errflow := GetStockFlowIsExsit(warehouseOut.ID, patient_id, record_time, goods.GoodId)
  966. if errflow == gorm.ErrRecordNotFound {
  967. CreateStockFlowOne(flow)
  968. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  969. CreateAutoReduceRecord(&details)
  970. } else if errflow == nil {
  971. flow := models.VmStockFlow{
  972. ID: exsit.ID,
  973. WarehouseOutId: warehouseOut.ID,
  974. WarehousingId: warehouse.ID,
  975. GoodId: goods.GoodId,
  976. Number: warehouse.Number,
  977. ProductDate: warehouse.ProductDate,
  978. ExpireDate: warehouse.ExpiryDate,
  979. Count: exsit.Count + goods.Count,
  980. Price: info.PackingPrice,
  981. Status: 1,
  982. Ctime: time.Now().Unix(),
  983. UserOrgId: orgID,
  984. Manufacturer: info.Manufacturer,
  985. Dealer: info.Dealer,
  986. LicenseNumber: warehouse.LicenseNumber,
  987. IsEdit: 2,
  988. Creator: warehouseOut.Creater,
  989. SystemTime: record_time,
  990. ConsumableType: 3,
  991. WarehouseOutDetailId: warehouseOutInfo.ID,
  992. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  993. IsSys: 1,
  994. PatientId: patient_id,
  995. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  996. }
  997. UpdatedStockFlow(flow)
  998. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, goods.GoodId, goods.GoodTypeId)
  999. CreateAutoReduceRecord(&details)
  1000. }
  1001. }
  1002. maxNumber = goods.Count
  1003. if warehouse.StockCount < maxNumber {
  1004. return errors.New("库存数量不足")
  1005. }
  1006. warehouse.StockCount = warehouse.StockCount - maxNumber
  1007. warehouse.Mtime = time.Now().Unix()
  1008. if warehouse.StockCount < 0 {
  1009. return errors.New("库存数量不足")
  1010. }
  1011. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1012. if errThree != nil {
  1013. return errThree
  1014. }
  1015. return nil
  1016. } else {
  1017. fmt.Println("不进来22323232323232323232233223323223322323")
  1018. // 当刚批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1019. warehouseOutInfo := &models.WarehouseOutInfo{
  1020. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1021. WarehouseOutId: warehouseOut.ID,
  1022. WarehouseInfotId: warehouse.ID,
  1023. Status: 1,
  1024. Ctime: time.Now().Unix(),
  1025. Remark: warehouse.Remark,
  1026. OrgId: orgID,
  1027. Type: 1,
  1028. Manufacturer: warehouse.Manufacturer,
  1029. Dealer: warehouse.Dealer,
  1030. IsSys: 1,
  1031. SysRecordTime: record_time,
  1032. GoodTypeId: goods.GoodTypeId,
  1033. GoodId: goods.GoodId,
  1034. PatientId: patient_id,
  1035. Number: warehouse.Number,
  1036. LicenseNumber: warehouse.LicenseNumber,
  1037. Price: info.PackingPrice,
  1038. ExpiryDate: warehouse.ExpiryDate,
  1039. ProductDate: warehouse.ProductDate,
  1040. }
  1041. warehouseOutInfo.Count = stock_number
  1042. _, errcodes := GetWarehouseOutInfoIsExistOne(goods.GoodId, goods.PatientId, record_time)
  1043. fmt.Println("hhhhh2h323233223232323", errcodes)
  1044. if errcodes == gorm.ErrRecordNotFound {
  1045. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1046. if errOne != nil {
  1047. return errOne
  1048. }
  1049. //插入库存流水表
  1050. flow := models.VmStockFlow{
  1051. WarehouseOutId: warehouseOut.ID,
  1052. WarehousingId: warehouse.ID,
  1053. GoodId: goods.GoodId,
  1054. Number: warehouse.Number,
  1055. ProductDate: warehouse.ProductDate,
  1056. ExpireDate: warehouse.ExpiryDate,
  1057. Count: stock_number,
  1058. Price: info.PackingPrice,
  1059. Status: 1,
  1060. Ctime: time.Now().Unix(),
  1061. UserOrgId: orgID,
  1062. Manufacturer: info.Manufacturer,
  1063. Dealer: info.Dealer,
  1064. LicenseNumber: warehouse.LicenseNumber,
  1065. IsEdit: 2,
  1066. Creator: warehouseOut.Creater,
  1067. SystemTime: record_time,
  1068. ConsumableType: 3,
  1069. WarehouseOutDetailId: warehouseOutInfo.ID,
  1070. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1071. IsSys: 1,
  1072. PatientId: patient_id,
  1073. }
  1074. //查询流水是否存在
  1075. exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1076. if errflows == gorm.ErrRecordNotFound {
  1077. CreateStockFlowOne(flow)
  1078. } else if errflows == nil {
  1079. flow := models.VmStockFlow{
  1080. ID: exsit.ID,
  1081. WarehouseOutId: warehouseOut.ID,
  1082. WarehousingId: warehouse.ID,
  1083. GoodId: goods.GoodId,
  1084. Number: warehouse.Number,
  1085. ProductDate: warehouse.ProductDate,
  1086. ExpireDate: warehouse.ExpiryDate,
  1087. Count: exsit.Count + goods.Count,
  1088. Price: info.PackingPrice,
  1089. Status: 1,
  1090. Ctime: time.Now().Unix(),
  1091. UserOrgId: orgID,
  1092. Manufacturer: info.Manufacturer,
  1093. Dealer: info.Dealer,
  1094. LicenseNumber: warehouse.LicenseNumber,
  1095. IsEdit: 2,
  1096. Creator: warehouseOut.Creater,
  1097. SystemTime: record_time,
  1098. ConsumableType: 3,
  1099. WarehouseOutDetailId: warehouseOutInfo.ID,
  1100. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1101. IsSys: 1,
  1102. PatientId: patient_id,
  1103. ReturnCount: exsit.Count + (exsit.Count + deliver_number),
  1104. }
  1105. UpdatedStockFlow(flow)
  1106. }
  1107. } else if errcodes == nil {
  1108. fmt.Println("尽力3323232322323232323232323232323")
  1109. goods.Count = deliver_number - stock_number
  1110. //更新数量为 该批次剩余数量 + 还有未出的数量
  1111. warehouseOutInfo.Count = stock_number
  1112. UpdatedWarehouseOutInfo(warehouseOutInfo, goods.GoodId, goods.PatientId, record_time)
  1113. //插入库存流水表
  1114. flow := models.VmStockFlow{
  1115. WarehouseOutId: warehouseOut.ID,
  1116. WarehousingId: warehouse.ID,
  1117. GoodId: goods.GoodId,
  1118. Number: warehouse.Number,
  1119. ProductDate: warehouse.ProductDate,
  1120. ExpireDate: warehouse.ExpiryDate,
  1121. Count: stock_number,
  1122. Price: info.PackingPrice,
  1123. Status: 1,
  1124. Ctime: time.Now().Unix(),
  1125. UserOrgId: orgID,
  1126. Manufacturer: info.Manufacturer,
  1127. Dealer: info.Dealer,
  1128. LicenseNumber: warehouse.LicenseNumber,
  1129. IsEdit: 2,
  1130. Creator: warehouseOut.Creater,
  1131. SystemTime: record_time,
  1132. ConsumableType: 3,
  1133. WarehouseOutDetailId: warehouseOutInfo.ID,
  1134. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1135. IsSys: 1,
  1136. PatientId: patient_id,
  1137. }
  1138. //查询流水是否存在
  1139. exsit, errflows := GetStockFlowIsExsit(warehouse.ID, patient_id, record_time, goods.GoodId)
  1140. if errflows == gorm.ErrRecordNotFound {
  1141. CreateStockFlowOne(flow)
  1142. } else if errflows == nil {
  1143. fmt.Println("尽力3323232322323232323232323232323", exsit.Count+goods.Count)
  1144. flow := models.VmStockFlow{
  1145. ID: exsit.ID,
  1146. WarehouseOutId: warehouseOut.ID,
  1147. WarehousingId: warehouse.ID,
  1148. GoodId: goods.GoodId,
  1149. Number: warehouse.Number,
  1150. ProductDate: warehouse.ProductDate,
  1151. ExpireDate: warehouse.ExpiryDate,
  1152. Count: exsit.Count + goods.Count,
  1153. Price: info.PackingPrice,
  1154. Status: 1,
  1155. Ctime: time.Now().Unix(),
  1156. UserOrgId: orgID,
  1157. Manufacturer: info.Manufacturer,
  1158. Dealer: info.Dealer,
  1159. LicenseNumber: warehouse.LicenseNumber,
  1160. IsEdit: 2,
  1161. Creator: warehouseOut.Creater,
  1162. SystemTime: record_time,
  1163. ConsumableType: 3,
  1164. WarehouseOutDetailId: warehouseOutInfo.ID,
  1165. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1166. IsSys: 1,
  1167. PatientId: patient_id,
  1168. }
  1169. UpdatedStockFlow(flow)
  1170. }
  1171. }
  1172. // 出库完成后,要将该批次库存清零
  1173. warehouse.StockCount = 0
  1174. warehouse.Mtime = time.Now().Unix()
  1175. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1176. if errThree != nil {
  1177. return errThree
  1178. }
  1179. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1180. goods.Count = deliver_number - stock_number
  1181. ConsumablesDelivery(orgID, patient_id, record_time, goods, warehouseOut, count)
  1182. }
  1183. return nil
  1184. }
  1185. //耗材出库删除
  1186. func ConsumablesDeliveryDelete(orgID int64, patient_id int64, record_time int64, good_yc *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut) (err error) {
  1187. // 先根据相关信息查询当天该耗材的出库信息
  1188. warehouseOutInfos, err := FindStockOutInfoByStock(orgID, good_yc.GoodTypeId, good_yc.GoodId, record_time, patient_id)
  1189. if err != nil {
  1190. return err
  1191. }
  1192. var delete_count int64 = 0
  1193. for _, ware := range warehouseOutInfos {
  1194. // 判断当前出库的数据和删除出库数量
  1195. if good_yc.Count <= ware.Count {
  1196. delete_count = good_yc.Count
  1197. } else {
  1198. delete_count = ware.Count
  1199. }
  1200. // 在出库记录表里记录退库详情
  1201. warehouseOutInfo := &models.WarehouseOutInfo{
  1202. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1203. WarehouseOutId: warehouseOut.ID,
  1204. //WarehouseInfoId: warehouse.ID,
  1205. Status: 1,
  1206. Ctime: time.Now().Unix(),
  1207. Remark: "",
  1208. OrgId: orgID,
  1209. Type: 1,
  1210. Manufacturer: 0,
  1211. Dealer: 0,
  1212. IsSys: 2,
  1213. SysRecordTime: record_time,
  1214. GoodTypeId: good_yc.GoodTypeId,
  1215. GoodId: good_yc.GoodId,
  1216. PatientId: patient_id,
  1217. }
  1218. warehouseOutInfo.Count = ware.Count - delete_count
  1219. stockInInfo, _ := FindLastStockInInfoRecord(good_yc.GoodId, orgID)
  1220. warehouseOutInfo.Price = stockInInfo.Price
  1221. errOne := UpdateAutoMaticReduceDetail(good_yc.GoodId, good_yc.GoodTypeId, record_time, patient_id, orgID, warehouseOutInfo)
  1222. if errOne != nil {
  1223. return errOne
  1224. } else {
  1225. details := &models.AutomaticReduceDetail{
  1226. WarehouseOutId: warehouseOutInfo.ID,
  1227. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1228. PatientId: patient_id,
  1229. Ctime: time.Now().Unix(),
  1230. Mtime: time.Now().Unix(),
  1231. Status: 1,
  1232. RecordTime: record_time,
  1233. OrgId: orgID,
  1234. GoodId: good_yc.GoodId,
  1235. GoodTypeId: good_yc.GoodTypeId,
  1236. Count: warehouseOutInfo.Count,
  1237. Type: 2,
  1238. }
  1239. //查询是否当天已经存在数据
  1240. _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  1241. if errcode == gorm.ErrRecordNotFound {
  1242. errTwo := AddSigleAutoReduceRecordInfo(details)
  1243. if errTwo != nil {
  1244. return errTwo
  1245. }
  1246. } else if errcode == nil {
  1247. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  1248. AddSigleAutoReduceRecordInfo(details)
  1249. }
  1250. }
  1251. // 删除出库完成后,要增加对应批次的库存数量
  1252. errThree := UpDateWarehouseInfoByStockDelete(ware.WarehouseInfotId, delete_count)
  1253. if errThree != nil {
  1254. return errThree
  1255. }
  1256. // 增加了对应的库存后,看看还有多少需要退库的
  1257. good_yc.Count = good_yc.Count - delete_count
  1258. if good_yc.Count == 0 {
  1259. return nil
  1260. }
  1261. }
  1262. if good_yc.Count == 0 {
  1263. return nil
  1264. } else {
  1265. return errors.New("退库和出库数据不匹配")
  1266. }
  1267. }
  1268. func GetAutoReduceRecordInfoByPatientId(orgid int64, patient_id int64, recordTime int64) (autoReduce []*models.AutomaticReduceDetail, err error) {
  1269. err = XTReadDB().Model(&autoReduce).Where("org_id = ? and patient_id = ? and record_time = ? and status = 1", orgid, patient_id, recordTime).Find(&autoReduce).Error
  1270. return autoReduce, err
  1271. }
  1272. func DeleteDialysisBefor(orgid int64, patient_id int64, record_date int64, goodid int64, goodtypeid int64) error {
  1273. prepare := models.DialysisBeforePrepare{}
  1274. err := XTWriteDB().Model(&prepare).Where("user_org_id = ? and patient_id = ? and record_date = ? and status = 1 and good_id = ? and good_type_id = ?", orgid, patient_id, record_date, goodid, goodtypeid).Updates(map[string]interface{}{"status": 0, "count": 0, "mtime": time.Now().Unix()}).Error
  1275. return err
  1276. }
  1277. func DeleteDialysisBeforThree(orgid int64, patient_id int64, record_date int64) error {
  1278. prepare := models.DialysisBeforePrepare{}
  1279. err := XTWriteDB().Model(&prepare).Where("user_org_id = ? and patient_id = ? and record_date = ? and status = 1", orgid, patient_id, record_date).Updates(map[string]interface{}{"status": 0, "count": 0, "mtime": time.Now().Unix()}).Error
  1280. return err
  1281. }
  1282. //耗材出库
  1283. func ConsumablesDeliveryOne(orgID int64, record_time int64, goods *models.WarehouseOutInfo, warehouseOut *models.WarehouseOut, count int64, creator int64) (err error) {
  1284. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1285. var deliver_number int64 = 0
  1286. var stock_number int64 = 0
  1287. var maxNumber int64 = 0
  1288. deliver_number = goods.Count
  1289. fmt.Println("添加数量", deliver_number)
  1290. // 根据先进先出原则,查询最先入库的批次,进行出库
  1291. // 如果没有对应的库存,则报错
  1292. warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId, goods.GoodTypeId)
  1293. fmt.Println("err222222222222222222222", warehouse.StockCount)
  1294. if err != nil {
  1295. return err
  1296. }
  1297. // 将该批次的剩余库存数量转换为拆零数量
  1298. stock_number = warehouse.StockCount
  1299. fmt.Println("3434343434343434", stock_number)
  1300. fmt.Println("8888888888888888888", deliver_number)
  1301. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1302. if stock_number >= deliver_number {
  1303. fmt.Println("g2332232332323232232323322332", goods.Price)
  1304. warehouseOutInfo := &models.WarehouseOutInfo{
  1305. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1306. WarehouseOutId: warehouseOut.ID,
  1307. WarehouseInfotId: warehouse.ID,
  1308. Status: 1,
  1309. Ctime: time.Now().Unix(),
  1310. Remark: goods.Remark,
  1311. OrgId: orgID,
  1312. Type: 1,
  1313. Manufacturer: goods.Manufacturer,
  1314. Dealer: goods.Dealer,
  1315. IsSys: 0,
  1316. SysRecordTime: record_time,
  1317. GoodTypeId: goods.GoodTypeId,
  1318. GoodId: goods.GoodId,
  1319. ExpiryDate: goods.ExpiryDate,
  1320. ProductDate: goods.ProductDate,
  1321. Number: goods.Number,
  1322. Price: goods.Price,
  1323. LicenseNumber: goods.LicenseNumber,
  1324. }
  1325. warehouseOutInfo.Count = goods.Count
  1326. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1327. if errOne != nil {
  1328. return errOne
  1329. }
  1330. stockFlow := models.VmStockFlow{
  1331. WarehousingId: warehouse.ID,
  1332. GoodId: goods.GoodId,
  1333. Number: goods.Number,
  1334. LicenseNumber: goods.LicenseNumber,
  1335. Count: goods.Count,
  1336. UserOrgId: goods.OrgId,
  1337. PatientId: goods.PatientId,
  1338. SystemTime: record_time,
  1339. ConsumableType: 2,
  1340. IsSys: goods.IsSys,
  1341. WarehousingOrder: "",
  1342. WarehouseOutId: warehouseOut.ID,
  1343. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1344. IsEdit: 1,
  1345. CancelStockId: 0,
  1346. CancelOrderNumber: "",
  1347. Manufacturer: goods.Manufacturer,
  1348. Dealer: goods.Dealer,
  1349. Creator: creator,
  1350. UpdateCreator: 0,
  1351. Status: 1,
  1352. Ctime: time.Now().Unix(),
  1353. Mtime: 0,
  1354. Price: goods.Price,
  1355. WarehousingDetailId: 0,
  1356. WarehouseOutDetailId: warehouseOutInfo.ID,
  1357. CancelOutDetailId: 0,
  1358. ProductDate: goods.ProductDate,
  1359. ExpireDate: goods.ExpiryDate,
  1360. }
  1361. //创建出库明细
  1362. CreateStockFlowOne(stockFlow)
  1363. maxNumber = goods.Count
  1364. if warehouse.StockCount < maxNumber {
  1365. return errors.New("库存数量不足")
  1366. }
  1367. warehouse.StockCount = warehouse.StockCount - maxNumber
  1368. warehouse.Mtime = time.Now().Unix()
  1369. if warehouse.StockCount < 0 {
  1370. return errors.New("库存数量不足")
  1371. }
  1372. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1373. if errThree != nil {
  1374. return errThree
  1375. }
  1376. } else {
  1377. // 当改批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1378. warehouseOutInfo := &models.WarehouseOutInfo{
  1379. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1380. WarehouseOutId: warehouseOut.ID,
  1381. WarehouseInfotId: warehouse.ID,
  1382. Status: 1,
  1383. Ctime: time.Now().Unix(),
  1384. Remark: goods.Remark,
  1385. OrgId: orgID,
  1386. Type: 1,
  1387. Manufacturer: goods.Manufacturer,
  1388. Dealer: goods.Dealer,
  1389. IsSys: 0,
  1390. SysRecordTime: record_time,
  1391. GoodTypeId: goods.GoodTypeId,
  1392. GoodId: goods.GoodId,
  1393. ExpiryDate: goods.ExpiryDate,
  1394. ProductDate: goods.ProductDate,
  1395. Number: goods.Number,
  1396. Price: goods.Price,
  1397. LicenseNumber: goods.LicenseNumber,
  1398. }
  1399. warehouseOutInfo.Count = stock_number
  1400. info, errcodes := GetStockInRecoredByGoodIdOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId)
  1401. if errcodes == gorm.ErrRecordNotFound {
  1402. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1403. if errOne != nil {
  1404. return errOne
  1405. }
  1406. } else {
  1407. warehouseOutInfo.Count = info.Count + stock_number
  1408. UpdateAutoMaticReduceDetailOne(goods.GoodId, goods.GoodTypeId, goods.SysRecordTime, goods.OrgId, warehouseOutInfo)
  1409. }
  1410. stockFlow := models.VmStockFlow{
  1411. WarehousingId: warehouse.ID,
  1412. GoodId: goods.GoodId,
  1413. Number: goods.Number,
  1414. LicenseNumber: goods.LicenseNumber,
  1415. Count: goods.Count,
  1416. UserOrgId: goods.OrgId,
  1417. PatientId: 0,
  1418. SystemTime: record_time,
  1419. ConsumableType: 2,
  1420. IsSys: 0,
  1421. WarehousingOrder: "",
  1422. WarehouseOutId: warehouseOut.ID,
  1423. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1424. IsEdit: 1,
  1425. CancelStockId: 0,
  1426. CancelOrderNumber: "",
  1427. Manufacturer: goods.Manufacturer,
  1428. Dealer: goods.Dealer,
  1429. Creator: creator,
  1430. UpdateCreator: 0,
  1431. Status: 1,
  1432. Ctime: time.Now().Unix(),
  1433. Mtime: 0,
  1434. Price: goods.Price,
  1435. WarehousingDetailId: 0,
  1436. WarehouseOutDetailId: warehouseOutInfo.ID,
  1437. CancelOutDetailId: 0,
  1438. ProductDate: goods.ProductDate,
  1439. ExpireDate: goods.ExpiryDate,
  1440. }
  1441. //创建出库明细
  1442. CreateStockFlowOne(stockFlow)
  1443. // 出库完成后,要将该批次库存清零
  1444. warehouse.StockCount = 0
  1445. warehouse.Mtime = time.Now().Unix()
  1446. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1447. if errThree != nil {
  1448. return errThree
  1449. }
  1450. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1451. goods.Count = deliver_number - stock_number
  1452. fmt.Println("goods3333333333333333333333", goods.Count)
  1453. ConsumablesDeliveryOne(orgID, record_time, goods, warehouseOut, count, creator)
  1454. }
  1455. return nil
  1456. }
  1457. //耗材出库删除
  1458. func ConsumablesDeliveryDeleteOne(orgID int64, record_time int64, good_yc *models.DialysisBeforePrepare, warehouseOut *models.WarehouseOut) (err error) {
  1459. // 先根据相关信息查询当天该耗材的出库信息
  1460. warehouseOutInfos, err := FindStockOutInfoByStockOne(orgID, good_yc.GoodTypeId, good_yc.GoodId, record_time)
  1461. if err != nil {
  1462. return err
  1463. }
  1464. var delete_count int64 = 0
  1465. for _, ware := range warehouseOutInfos {
  1466. // 判断当前出库的数据和删除出库数量
  1467. if good_yc.Count <= ware.Count {
  1468. delete_count = good_yc.Count
  1469. } else {
  1470. delete_count = ware.Count
  1471. }
  1472. // 在出库记录表里记录退库详情
  1473. warehouseOutInfo := &models.WarehouseOutInfo{
  1474. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1475. WarehouseOutId: warehouseOut.ID,
  1476. //WarehouseInfoId: warehouse.ID,
  1477. Status: 1,
  1478. Ctime: time.Now().Unix(),
  1479. Remark: "",
  1480. OrgId: orgID,
  1481. Type: 1,
  1482. Manufacturer: 0,
  1483. Dealer: 0,
  1484. IsSys: 0,
  1485. SysRecordTime: record_time,
  1486. GoodTypeId: good_yc.GoodTypeId,
  1487. GoodId: good_yc.GoodId,
  1488. }
  1489. warehouseOutInfo.Count = delete_count
  1490. stockInInfo, _ := FindLastStockInInfoRecord(good_yc.GoodId, orgID)
  1491. warehouseOutInfo.Price = stockInInfo.Price
  1492. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1493. if errOne != nil {
  1494. return errOne
  1495. }
  1496. // 删除出库完成后,要增加对应批次的库存数量
  1497. errThree := UpDateWarehouseInfoByStockDelete(ware.WarehouseInfotId, delete_count)
  1498. if errThree != nil {
  1499. return errThree
  1500. }
  1501. // 增加了对应的库存后,看看还有多少需要退库的
  1502. good_yc.Count = good_yc.Count - delete_count
  1503. if good_yc.Count == 0 {
  1504. return nil
  1505. }
  1506. }
  1507. if good_yc.Count == 0 {
  1508. return nil
  1509. } else {
  1510. return errors.New("退库和出库数据不匹配")
  1511. }
  1512. }
  1513. //退库逻辑
  1514. func ConsumablesDeliveryTwo(orgID int64, goods *models.WarehouseOutInfo, count int64) (err error) {
  1515. // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
  1516. var deliver_number int64 = 0
  1517. var stock_number int64 = 0
  1518. var maxNumber int64 = 0
  1519. deliver_number = goods.Count
  1520. fmt.Println("添加数量", deliver_number)
  1521. // 根据先进先出原则,查询最先入库的批次,进行出库
  1522. // 查询除自己以为的库存
  1523. warehouse, err := FindFirstWarehousingInfoByGoodId(goods.GoodId, goods.ID)
  1524. if err != nil {
  1525. return err
  1526. }
  1527. // 将该批次的剩余库存数量转换为拆零数量
  1528. stock_number = warehouse.StockCount
  1529. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  1530. if stock_number >= deliver_number {
  1531. warehouseOutInfo := &models.WarehouseOutInfo{
  1532. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  1533. WarehouseOutId: goods.ID,
  1534. WarehouseInfotId: warehouse.ID,
  1535. Status: 1,
  1536. Ctime: time.Now().Unix(),
  1537. Remark: goods.Remark,
  1538. OrgId: orgID,
  1539. Type: 1,
  1540. Manufacturer: goods.Manufacturer,
  1541. Dealer: goods.Dealer,
  1542. IsSys: 0,
  1543. GoodTypeId: goods.GoodTypeId,
  1544. GoodId: goods.GoodId,
  1545. ExpiryDate: goods.ExpiryDate,
  1546. ProductDate: goods.ProductDate,
  1547. Number: goods.Number,
  1548. Price: goods.Price,
  1549. LicenseNumber: goods.LicenseNumber,
  1550. ConsumableType: 2,
  1551. }
  1552. warehouseOutInfo.Count = goods.Count
  1553. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1554. if errOne != nil {
  1555. return errOne
  1556. }
  1557. maxNumber = goods.Count
  1558. if warehouse.StockCount < maxNumber {
  1559. return errors.New("库存数量不足")
  1560. }
  1561. warehouse.StockCount = warehouse.StockCount - maxNumber
  1562. warehouse.Mtime = time.Now().Unix()
  1563. if warehouse.StockCount < 0 {
  1564. return errors.New("库存数量不足")
  1565. }
  1566. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1567. if errThree != nil {
  1568. return errThree
  1569. }
  1570. } else {
  1571. // 当改批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
  1572. warehouseOutInfo := &models.WarehouseOutInfo{
  1573. WarehouseOutOrderNumber: goods.WarehouseOutOrderNumber,
  1574. WarehouseOutId: goods.ID,
  1575. WarehouseInfotId: warehouse.ID,
  1576. Status: 1,
  1577. Ctime: time.Now().Unix(),
  1578. Remark: goods.Remark,
  1579. OrgId: orgID,
  1580. Type: 1,
  1581. Manufacturer: goods.Manufacturer,
  1582. Dealer: goods.Dealer,
  1583. IsSys: 0,
  1584. GoodTypeId: goods.GoodTypeId,
  1585. GoodId: goods.GoodId,
  1586. ExpiryDate: goods.ExpiryDate,
  1587. ProductDate: goods.ProductDate,
  1588. Number: goods.Number,
  1589. Price: goods.Price,
  1590. LicenseNumber: goods.LicenseNumber,
  1591. ConsumableType: 2,
  1592. }
  1593. warehouseOutInfo.Count = stock_number
  1594. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1595. if errOne != nil {
  1596. return errOne
  1597. }
  1598. // 出库完成后,要将该批次库存清零
  1599. warehouse.StockCount = 0
  1600. warehouse.Mtime = time.Now().Unix()
  1601. errThree := UpDateWarehouseInfoByStock(&warehouse)
  1602. if errThree != nil {
  1603. return errThree
  1604. }
  1605. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  1606. goods.Count = deliver_number - stock_number
  1607. ConsumablesDeliveryTwo(orgID, goods, count)
  1608. }
  1609. return nil
  1610. }
  1611. func DeleteWarehouseInfo(id int64) error {
  1612. err := XTWriteDB().Model(models.WarehousingInfo{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": 0}).Error
  1613. return err
  1614. }
  1615. //
  1616. ////当前数据比上一次出库数据少
  1617. //func ConsumablesDeliveryTotalSeven(orgID int64, patient_id int64, record_time int64, goods []*models.DialysisBeforePrepareGoods,creater int64,count int64) (err error) {
  1618. //
  1619. // fmt.Println("w我的中国馆公共区发电房阿道夫安抚安抚安抚安抚安抚安抚阿道夫阿道夫阿凡达阿道夫a",count)
  1620. // //查询该患者当天已经出库的耗材信息
  1621. // goods_yc, _ := FindConsumablesByDateThree(orgID, patient_id, record_time)
  1622. // // 和新请求的出库数据进行对比,分出那些是继续出库的,那些是需要删除出库的
  1623. // for i := len(goods_yc) - 1; i >= 0; i-- {
  1624. // goods_yc_temp := goods_yc[i]
  1625. // for j := len(goods) - 1; j >= 0; j-- {
  1626. // goods_temp := goods[j]
  1627. // // 已经出库和新请求出库都存在该耗材,则判断出库数量,分成是继续出库,还是删除出库
  1628. // if goods_yc_temp.GoodTypeId == goods_temp.GoodTypeId && goods_yc_temp.GoodId == goods_temp.GoodId {
  1629. // // 已经出库和新请求出库的出库数量一致,则清除两个结构体里的数据(既不出库,也不删除出库)
  1630. // if goods_yc_temp.Count == goods_temp.Count {
  1631. // goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  1632. // goods = append(goods[:j], goods[j+1:]...)
  1633. // break
  1634. // }
  1635. // // 如果已经出库的数量 大于 新请求出库的数量,则代表需要删除出库
  1636. // if goods_yc_temp.Count > goods_temp.Count {
  1637. //
  1638. // temp_count := goods_yc_temp.Count - goods_temp.Count
  1639. // goods_yc[i].Count = temp_count
  1640. // goods = append(goods[:j], goods[j+1:]...)
  1641. // break
  1642. // }
  1643. // // 如果已经出库的数量 小于 新请求出库的梳理,则代表需要增加出库
  1644. // if goods_yc_temp.Count < goods_temp.Count {
  1645. // temp_count := goods_temp.Count - goods_yc_temp.Count
  1646. // //fmt.Println("988888888888888", temp_count)
  1647. // goods[j].Count = temp_count
  1648. // goods_yc = append(goods_yc[:i], goods_yc[i+1:]...)
  1649. // //fmt.Println("888888888", goods_yc)
  1650. // break
  1651. // }
  1652. // }
  1653. // }
  1654. // }
  1655. //
  1656. // // goods_yc 这个数据就是需要已经出库了,但是现在需要删除出库的耗材数据
  1657. // // goods 这个数据就是需要出库的耗材的数据(新增的数据)
  1658. // fmt.Println("goodsy999999999999", goods_yc)
  1659. //
  1660. //
  1661. //
  1662. // //退库
  1663. // if len(goods_yc) > 0 {
  1664. // for _, good_yc := range goods_yc {
  1665. // out, _ := FindStockOutByIsSys(orgID, 1, record_time)
  1666. // ConsumablesDeliveryDeleteFour(orgID, record_time, good_yc, &out,patient_id,creater,count)
  1667. // }
  1668. // }
  1669. //
  1670. // return nil
  1671. //}
  1672. ////耗材出库删除
  1673. //func ConsumablesDeliveryDeleteFour(orgID int64, record_time int64, good_yc *models.BloodAutomaticReduceDetail, warehouseOut *models.WarehouseOut,patient_id int64,creater int64,count int64) (err error) {
  1674. //
  1675. // fmt.Println("count2323223884584854854854u5454785487547845785478758487545475475487,count",count)
  1676. // // 先根据相关信息查询当天该耗材的出库信息
  1677. // warehouseOutInfos, err := FindStockOutInfoByStockTwo(orgID, good_yc.GoodTypeId, good_yc.GoodId, record_time,good_yc.PatientId)
  1678. // fmt.Println("errr232323232323232323232323232",err)
  1679. // if err != nil {
  1680. // return err
  1681. // }
  1682. //
  1683. // var delete_count int64 = 0
  1684. //
  1685. // delete_count = warehouseOutInfos.Count - count
  1686. //
  1687. // fmt.Println("delete_count2323232",delete_count)
  1688. // // 在出库记录表里记录退库详情
  1689. // warehouseOutInfo := &models.WarehouseOutInfo{
  1690. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1691. // WarehouseOutId: warehouseOut.ID,
  1692. // Status: 1,
  1693. // Ctime: time.Now().Unix(),
  1694. // OrgId: orgID,
  1695. // Type: 1,
  1696. // IsSys: 1,
  1697. // SysRecordTime: record_time,
  1698. // GoodTypeId: good_yc.GoodTypeId,
  1699. // GoodId: good_yc.GoodId,
  1700. // PatientId:good_yc.PatientId,
  1701. // ConsumableType:2,
  1702. //
  1703. // }
  1704. // warehouseOutInfo.Count = count
  1705. //
  1706. // stockInInfo, _ := FindLastStockInInfoRecord(good_yc.GoodId, orgID)
  1707. // warehouseOutInfo.Price = stockInInfo.Price
  1708. // warehouseOutInfo.Dealer = stockInInfo.Dealer
  1709. // warehouseOutInfo.Manufacturer = stockInInfo.Manufacturer
  1710. // warehouseOutInfo.ExpiryDate = stockInInfo.ExpiryDate
  1711. // warehouseOutInfo.ProductDate = stockInInfo.ProductDate
  1712. // warehouseOutInfo.Number = stockInInfo.Number
  1713. // warehouseOutInfo.LicenseNumber = stockInInfo.LicenseNumber
  1714. // warehouseOutInfo.WarehouseInfotId = stockInInfo.ID
  1715. // //查找当天是否存在出库记录
  1716. //
  1717. // _, errcod := GetWarehouseOutInfoIsExistOne(good_yc.GoodId, good_yc.PatientId, record_time)
  1718. // fmt.Println("errcode2323223255556652324242424242424242424242424242242",errcod)
  1719. // if errcod == gorm.ErrRecordNotFound{
  1720. // errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1721. // //插入详情明细表
  1722. // stockFlow := models.VmStockFlow{
  1723. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1724. // WarehouseOutId: warehouseOut.ID,
  1725. // GoodId: good_yc.GoodId,
  1726. // Number: stockInInfo.Number,
  1727. // ProductDate: stockInInfo.ProductDate,
  1728. // ExpireDate: stockInInfo.ExpiryDate,
  1729. // Count: count,
  1730. // Price: stockInInfo.Price,
  1731. // Status: 1,
  1732. // Ctime: time.Now().Unix(),
  1733. // UserOrgId: good_yc.OrgId,
  1734. // Manufacturer: stockInInfo.Manufacturer,
  1735. // Dealer: stockInInfo.Dealer,
  1736. // LicenseNumber: stockInInfo.LicenseNumber,
  1737. // IsEdit: 2,
  1738. // Creator: creater,
  1739. // SystemTime: record_time,
  1740. // ConsumableType: 2,
  1741. // WarehousingDetailId:0,
  1742. // IsSys:1,
  1743. // UpdateCreator:creater,
  1744. // }
  1745. // //创建流水表
  1746. // err := CreateStockFlowOne(stockFlow)
  1747. // fmt.Println("h2h3h2323342i24i242i4u2i4242u42424",err)
  1748. // if errOne != nil {
  1749. // return errOne
  1750. // }
  1751. // }else if errcod == nil{
  1752. // UpdatedWarehouseOutInfo(warehouseOutInfo,good_yc.GoodId,good_yc.PatientId,record_time)
  1753. // //插入详情明细表
  1754. // stockFlow := models.VmStockFlow{
  1755. // WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1756. // WarehouseOutId: warehouseOut.ID,
  1757. // GoodId: good_yc.GoodId,
  1758. // Number: stockInInfo.Number,
  1759. // ProductDate: stockInInfo.ProductDate,
  1760. // ExpireDate: stockInInfo.ExpiryDate,
  1761. // Count: count,
  1762. // Price: stockInInfo.Price,
  1763. // Status: 1,
  1764. // Ctime: time.Now().Unix(),
  1765. // UserOrgId: good_yc.OrgId,
  1766. // Manufacturer: stockInInfo.Manufacturer,
  1767. // Dealer: stockInInfo.Dealer,
  1768. // LicenseNumber: stockInInfo.LicenseNumber,
  1769. // IsEdit: 2,
  1770. // Creator: creater,
  1771. // SystemTime: record_time,
  1772. // ConsumableType: 2,
  1773. // WarehousingDetailId:0,
  1774. // IsSys:1,
  1775. // UpdateCreator:creater,
  1776. // }
  1777. // //创建流水表
  1778. // CreateStockFlowOne(stockFlow)
  1779. // }
  1780. //
  1781. //
  1782. //
  1783. // //更改自动出库的表格
  1784. // details := models.BloodAutomaticReduceDetail{
  1785. // WarehouseOutId: warehouseOutInfo.ID,
  1786. // WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1787. // PatientId: patient_id,
  1788. // Ctime: time.Now().Unix(),
  1789. // Mtime: time.Now().Unix(),
  1790. // Status: 1,
  1791. // RecordTime: record_time,
  1792. // OrgId: orgID,
  1793. // GoodId: good_yc.GoodId,
  1794. // GoodTypeId: good_yc.GoodTypeId,
  1795. // Count: count,
  1796. // }
  1797. // //查询当天耗材是否已经存在数据
  1798. // _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  1799. //
  1800. // if errcode == gorm.ErrRecordNotFound {
  1801. // errTwo := CreateAutoReduceRecord(&details)
  1802. // if errTwo != nil {
  1803. // return errTwo
  1804. // }
  1805. // } else if errcode == nil {
  1806. // DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  1807. // CreateAutoReduceRecord(&details)
  1808. // }
  1809. // // 删除出库完成后,要增加对应批次的库存数量
  1810. // fmt.Println("deletecount2323232323232323232323232323",delete_count)
  1811. // errThree := UpDateWarehouseInfoByStockDelete(warehouseOutInfos.WarehouseInfotId, delete_count)
  1812. //
  1813. // if errThree != nil {
  1814. // return errThree
  1815. // }
  1816. //
  1817. //
  1818. // if good_yc.Count == 0 {
  1819. // return nil
  1820. // } else {
  1821. // return errors.New("退库和出库数据不匹配")
  1822. // }
  1823. //}
  1824. //耗材出库删除
  1825. func ConsumablesDeliveryDeleteFive(orgID int64, record_time int64, good_yc *models.BloodAutomaticReduceDetail, warehouseOut *models.WarehouseOut, patient_id int64, creater int64, count int64) (err error) {
  1826. fmt.Println("count23232323232323232323232323223223", count)
  1827. // 先根据相关信息查询当天该耗材的出库信息
  1828. warehouseOutInfos, err := FindStockOutInfoByStockTwo(orgID, good_yc.GoodTypeId, good_yc.GoodId, record_time, good_yc.PatientId)
  1829. fmt.Println("errr232323232323232323232323232", err)
  1830. if err != nil {
  1831. return err
  1832. }
  1833. var delete_count int64 = 0
  1834. delete_count = count - warehouseOutInfos.Count
  1835. // 判断当前出库的数据和删除出库数量
  1836. // 在出库记录表里记录退库详情
  1837. warehouseOutInfo := &models.WarehouseOutInfo{
  1838. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1839. WarehouseOutId: warehouseOut.ID,
  1840. Status: 1,
  1841. Ctime: time.Now().Unix(),
  1842. OrgId: orgID,
  1843. Type: 1,
  1844. IsSys: 1,
  1845. SysRecordTime: record_time,
  1846. GoodTypeId: good_yc.GoodTypeId,
  1847. GoodId: good_yc.GoodId,
  1848. PatientId: good_yc.PatientId,
  1849. ConsumableType: 2,
  1850. }
  1851. warehouseOutInfo.Count = count
  1852. stockInInfo, _ := FindLastStockInInfoRecord(good_yc.GoodId, orgID)
  1853. warehouseOutInfo.Price = stockInInfo.Price
  1854. warehouseOutInfo.Dealer = stockInInfo.Dealer
  1855. warehouseOutInfo.Manufacturer = stockInInfo.Manufacturer
  1856. warehouseOutInfo.ExpiryDate = stockInInfo.ExpiryDate
  1857. warehouseOutInfo.ProductDate = stockInInfo.ProductDate
  1858. warehouseOutInfo.Number = stockInInfo.Number
  1859. warehouseOutInfo.LicenseNumber = stockInInfo.LicenseNumber
  1860. warehouseOutInfo.WarehouseInfotId = stockInInfo.ID
  1861. //查找当天是否存在出库记录
  1862. _, errcod := GetWarehouseOutInfoIsExistOne(good_yc.GoodId, good_yc.PatientId, record_time)
  1863. fmt.Println("errcode2323223255556652324242424242424242424242424242242", errcod)
  1864. if errcod == gorm.ErrRecordNotFound {
  1865. errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
  1866. //插入详情明细表
  1867. stockFlow := models.VmStockFlow{
  1868. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1869. WarehouseOutId: warehouseOut.ID,
  1870. GoodId: good_yc.GoodId,
  1871. Number: stockInInfo.Number,
  1872. ProductDate: stockInInfo.ProductDate,
  1873. ExpireDate: stockInInfo.ExpiryDate,
  1874. Count: count,
  1875. Price: stockInInfo.Price,
  1876. Status: 1,
  1877. Ctime: time.Now().Unix(),
  1878. UserOrgId: good_yc.OrgId,
  1879. Manufacturer: stockInInfo.Manufacturer,
  1880. Dealer: stockInInfo.Dealer,
  1881. LicenseNumber: stockInInfo.LicenseNumber,
  1882. IsEdit: 2,
  1883. Creator: creater,
  1884. SystemTime: record_time,
  1885. ConsumableType: 3,
  1886. WarehousingDetailId: 0,
  1887. IsSys: 1,
  1888. UpdateCreator: creater,
  1889. }
  1890. //创建流水表
  1891. err := CreateStockFlowOne(stockFlow)
  1892. fmt.Println("h2h3h2323342i24i242i4u2i4242u42424", err)
  1893. if errOne != nil {
  1894. return errOne
  1895. }
  1896. } else if errcod == nil {
  1897. UpdatedWarehouseOutInfo(warehouseOutInfo, good_yc.GoodId, good_yc.PatientId, record_time)
  1898. //插入详情明细表
  1899. stockFlow := models.VmStockFlow{
  1900. WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
  1901. WarehouseOutId: warehouseOut.ID,
  1902. GoodId: good_yc.GoodId,
  1903. Number: stockInInfo.Number,
  1904. ProductDate: stockInInfo.ProductDate,
  1905. ExpireDate: stockInInfo.ExpiryDate,
  1906. Count: count,
  1907. Price: stockInInfo.Price,
  1908. Status: 1,
  1909. Ctime: time.Now().Unix(),
  1910. UserOrgId: good_yc.OrgId,
  1911. Manufacturer: stockInInfo.Manufacturer,
  1912. Dealer: stockInInfo.Dealer,
  1913. LicenseNumber: stockInInfo.LicenseNumber,
  1914. IsEdit: 2,
  1915. Creator: creater,
  1916. SystemTime: record_time,
  1917. ConsumableType: 3,
  1918. WarehousingDetailId: 0,
  1919. IsSys: 1,
  1920. UpdateCreator: creater,
  1921. }
  1922. //创建流水表
  1923. err := CreateStockFlowOne(stockFlow)
  1924. fmt.Println("h2h3h2323342i24i242i4u2i4242u42424", err)
  1925. }
  1926. //更改自动出库的表格
  1927. details := models.BloodAutomaticReduceDetail{
  1928. WarehouseOutId: warehouseOutInfo.ID,
  1929. WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
  1930. PatientId: patient_id,
  1931. Ctime: time.Now().Unix(),
  1932. Mtime: time.Now().Unix(),
  1933. Status: 1,
  1934. RecordTime: record_time,
  1935. OrgId: orgID,
  1936. GoodId: good_yc.GoodId,
  1937. GoodTypeId: good_yc.GoodTypeId,
  1938. Count: count,
  1939. }
  1940. //查询当天耗材是否已经存在数据
  1941. _, errcode := GetAutoMaticReduceDetail(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  1942. if errcode == gorm.ErrRecordNotFound {
  1943. errTwo := CreateAutoReduceRecord(&details)
  1944. if errTwo != nil {
  1945. return errTwo
  1946. }
  1947. } else if errcode == nil {
  1948. DeleteAutoRedeceDetailTwo(orgID, patient_id, record_time, good_yc.GoodId, good_yc.GoodTypeId)
  1949. CreateAutoReduceRecord(&details)
  1950. }
  1951. // 删除出库完成后,要增加对应批次的库存数量
  1952. errThree := UpDateWarehouseInfoByStockDeleteOne(warehouseOutInfos.WarehouseInfotId, delete_count)
  1953. fmt.Println("sql2323234343434343434343434343434343434343343434")
  1954. if errThree != nil {
  1955. return errThree
  1956. }
  1957. if good_yc.Count == 0 {
  1958. return nil
  1959. } else {
  1960. return errors.New("退库和出库数据不匹配")
  1961. }
  1962. }