Browse Source

耗材出库

张保健 3 years ago
parent
commit
f7464473f5

+ 8 - 0
models/dialysis.go View File

@@ -993,6 +993,14 @@ func (DialysisBeforePrepare) TableName() string {
993 993
 	return "dialysis_before_prepare"
994 994
 }
995 995
 
996
+type DialysisBeforePrepareGoods struct {
997
+	GoodId       int64  `gorm:"column:good_id" json:"good_id" form:"good_id"`
998
+	Count       int64  `gorm:"column:count" json:"count" form:"count"`
999
+	GoodTypeId   int64  `gorm:"column:good_type_id" json:"good_type_id" form:"good_type_id"`
1000
+	TypeName   	 string  `gorm:"column:type_name" json:"type_name" form:"type_name"`
1001
+	SpecificationName   string  `gorm:"column:specification_name" json:"specification_name" form:"specification_name"`
1002
+}
1003
+
996 1004
 type QueueCallConfig struct {
997 1005
 	ID         int64 `gorm:"column:id" json:"id" form:"id"`
998 1006
 	OrgId      int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`

+ 5 - 0
models/service/stock_service.go View File

@@ -1255,6 +1255,11 @@ func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
1255 1255
 	return
1256 1256
 }
1257 1257
 
1258
+func UpDateWarehouseInfoByStock(warehouseInfo *models.WarehousingInfo) (err error) {
1259
+	err = writeDb.Save(&warehouseInfo).Error
1260
+	return
1261
+}
1262
+
1258 1263
 func UpDateWarehouseStatus(id int64) (err error) {
1259 1264
 	err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
1260 1265
 	return

+ 4 - 4
models/stock_models.go View File

@@ -80,10 +80,7 @@ type WarehousingInfo struct {
80 80
 	ExpiryDate       int64       `gorm:"column:expiry_date" json:"expiry_date"`
81 81
 	WarehousingCount int64       `gorm:"column:warehousing_count" json:"warehousing_count"`
82 82
 	WarehousingUnit  string      `gorm:"column:warehousing_unit" json:"warehousing_unit"`
83
-	MaxUnit 		 string       `gorm:"column:max_unit" json:"max_unit"`
84
-	MinUnit 		 string       `gorm:"column:min_unit" json:"min_unit"`
85
-	StockMaxNumber   int64       `gorm:"column:stock_max_number" json:"stock_max_number"`
86
-	StockMinNumber   int64       `gorm:"column:stock_min_number" json:"stock_min_number"`
83
+	StockNumber   	 int64       `gorm:"column:stock_number" json:"stock_number"`
87 84
 	Price            float64     `gorm:"column:price" json:"price"`
88 85
 	TotalPrice       float64     `gorm:"column:total_price" json:"total_price"`
89 86
 	Dealer           int64       `gorm:"column:dealer" json:"dealer"`
@@ -131,6 +128,7 @@ func (WarehouseOut) TableName() string {
131 128
 type WarehouseOutInfo struct {
132 129
 	ID                      int64        `gorm:"column:id" json:"id"`
133 130
 	WarehouseOutId          int64        `gorm:"column:warehouse_out_id" json:"warehouse_out_id"`
131
+	WarehouseInfotId        int64        `gorm:"column:warehouse_info_id" json:"warehouse_info_id"`
134 132
 	GoodId                  int64        `gorm:"column:good_id" json:"good_id"`
135 133
 	GoodTypeId              int64        `gorm:"column:good_type_id" json:"good_type_id"`
136 134
 	WarehousingOutTarget    int64        `gorm:"column:warehousing_out_target" json:"warehousing_out_target"`
@@ -153,6 +151,7 @@ type WarehouseOutInfo struct {
153 151
 	Manufacturer            int64        `gorm:"column:manufacturer" json:"manufacturer"`
154 152
 	IsSys                   int64        `gorm:"column:is_sys" json:"is_sys"`
155 153
 	SysRecordTime           int64        `gorm:"column:sys_record_time" json:"sys_record_time"`
154
+	PatientId				int64 		 `gorm:"column:patient_id" json:"patient_id"`
156 155
 }
157 156
 
158 157
 func (WarehouseOutInfo) TableName() string {
@@ -308,6 +307,7 @@ type AutomaticReduceDetail struct {
308 307
 	GoodsType               GoodsType `gorm:"ForeignKey:GoodTypeId;AssociationForeignKey:ID" json:"type"`
309 308
 	Patients                Patients  `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
310 309
 	Count                   int64     `gorm:"column:count" json:"count"`
310
+	Type                    int64     `gorm:"column:type" json:"type"`
311 311
 }
312 312
 
313 313
 func (AutomaticReduceDetail) TableName() string {

+ 5 - 0
service/mobile_dialysis_service.go View File

@@ -1469,6 +1469,11 @@ func FindConsumablesByDate(orgID int64, patient_id int64, recordDate int64) (con
1469 1469
 	return
1470 1470
 }
1471 1471
 
1472
+func FindConsumablesByDateTwo(orgID int64, patient_id int64, recordDate int64) (consumables []*models.DialysisBeforePrepare, err error) {
1473
+	err = readDb.Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1 AND count > 0", orgID, patient_id, recordDate).Find(&consumables).Error
1474
+	return
1475
+}
1476
+
1472 1477
 //func FindLastConsumables(orgID int64, patient_id int64, recordDate int64) (consumables []*models.DialysisBeforePrepare, err error){
1473 1478
 //	err = readDb.Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1", orgID, patient_id,recordDate).Find(&consumables).Error
1474 1479
 //	return

+ 15 - 2
service/stock_service.go View File

@@ -453,13 +453,16 @@ func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err
453 453
 
454 454
 func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
455 455
 	err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
456
-
457 456
 	return info, err
457
+}
458 458
 
459
+func FindFirstWarehousingInfoByStock(good_id int64,good_type_id int64) (info models.WarehousingInfo, err error) {
460
+	err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0", order).Order("ctime").First(&info).Error
461
+	return info, err
459 462
 }
460 463
 
461 464
 // 出库时先进先出,找出最先入库的批次
462
-func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
465
+func  (drug_id int64) (info models.WarehousingInfo, err error) {
463 466
 	err = readDb.Model(&models.WarehousingInfo{}).Where("drup_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) ", drug_id).Order("ctime").First(&info).Error
464 467
 
465 468
 	return info, err
@@ -1601,6 +1604,11 @@ func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
1601 1604
 	return err
1602 1605
 }
1603 1606
 
1607
+func UpDateWarehouseInfoByStockDelete(id int64,count int64) (err error) {
1608
+	err = DB.Model(&models.WarehousingInfo{}).Where("id = ?",info.ID).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
1609
+	return err
1610
+}
1611
+
1604 1612
 func UpDateDrugWarehouseOutStatus(id int64) (err error) {
1605 1613
 	err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
1606 1614
 	return
@@ -2062,6 +2070,11 @@ func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, o
2062 2070
 	return
2063 2071
 }
2064 2072
 
2073
+func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64,patient_id int64) (out []*models.WarehouseOutInfo, err error) {
2074
+	err = readDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND sys_record_time = ? AND patient_id = ?", org_id, good_type_id, good_id, record_date, patient_id).Order("ctime desc").Find(&out).Error
2075
+	return
2076
+}
2077
+
2065 2078
 func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
2066 2079
 	err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND  drug_id = ? AND is_sys = 1 AND  (warehouse_out_id = ? or warehouse_out_order_number = ?)", org_id, drug_id, out_id, number).First(&out).Error
2067 2080
 	return

+ 314 - 0
service/warhouse_service.go View File

@@ -229,4 +229,318 @@ func DrugDeliverInfo(orgID int64,prescribingNumber float64,warehouseout *models.
229 229
 		advice.PrescribingNumberUnit = drup.MinUnit
230 230
 		DrugDeliverInfo(orgID ,prescribingNumber,warehouseout,drup ,advice )
231 231
 	}
232
+	return nil
233
+}
234
+
235
+// 耗材出库
236
+func ConsumablesDeliveryTotal(orgID int64,patient_id int64,record_time int64,goods []models.DialysisBeforePrepareGoods) (err error) {
237
+	//查询该患者当天已经出库的耗材信息
238
+	goods_yc,_ := FindConsumablesByDateTwo(orgID, patient_id, record_time)
239
+	// 和新请求的出库数据进行对比,分出那些是继续出库的,那些是需要删除出库的
240
+	for i := len(goods_yc) - 1; i >= 0; i--{
241
+		goods_yc_temp := goods_yc[i]
242
+		for j := len(goods) - 1; j >= 0; j-- {
243
+			goods_temp := goods[j]
244
+			// 已经出库和新请求出库都存在该耗材,则判断出库数量,分成是继续出库,还是删除出库
245
+			if goods_yc_temp.GoodTypeId == goods_temp.GoodTypeId && goods_yc_temp.GoodId == goods_temp.GoodId{
246
+				// 已经出库和新请求出库的出库数量一致,则清除两个结构体里的数据(既不出库,也不删除出库)
247
+				if goods_yc_temp.Count == goods_temp.Count {
248
+					goods_yc = append(goods_yc[:i],goods_yc[i+1:]...)
249
+					goods = append(goods[:j],goods[j+1:]...)
250
+				}
251
+
252
+				// 如果已经出库的数量 大于 新请求出库的数量,则代表需要删除出库
253
+				if goods_yc_temp.Count > goods_temp.Count {
254
+					temp_count := goods_yc_temp.Count - goods_temp.Count
255
+					goods_yc[i].Count = temp_count
256
+					goods = append(goods[:j],goods[j+1:]...)
257
+				}
258
+
259
+				// 如果已经出库的数量 小于 新请求出库的梳理,则代表需要增加出库
260
+				if goods_yc_temp.Count < goods_temp.Count {
261
+					temp_count := goods_temp.Count - goods_yc_temp.Count
262
+					goods[j].Count = temp_count
263
+					goods_yc = append(goods_yc[:i],goods_yc[i+1:]...)
264
+				}
265
+			}
266
+		}
267
+	}
268
+
269
+	// goods_yc 这个数据就是需要已经出库了,但是现在需要删除出库的耗材数据
270
+	// goods 这个数据就是需要出库的耗材的数据
271
+	if len(goods) > 0 {
272
+		out, err := FindStockOutByIsSys(orgID, 1, record_time)
273
+		if err == gorm.ErrRecordNotFound {
274
+			//没有记录,则创建出库单
275
+			timeStr := time.Now().Format("2006-01-02")
276
+			timeArr := strings.Split(timeStr, "-")
277
+			total, _ :=  FindAllWarehouseOut(orgID)
278
+			total = total + 1
279
+			warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
280
+			number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
281
+			number = number + total
282
+			warehousing_out_order = "CKD" + strconv.FormatInt(number, 10)
283
+			warehouseOut := models.WarehouseOut{
284
+				WarehouseOutOrderNumber: warehousing_out_order,
285
+				OperationTime:           time.Now().Unix(),
286
+				OrgId:                   orgID,
287
+				Creater:                 0,
288
+				Ctime:                   time.Now().Unix(),
289
+				Status:                  1,
290
+				WarehouseOutTime:        record_time,
291
+				Dealer:                  0,
292
+				Manufacturer:            0,
293
+				Type:                    1,
294
+				IsSys:                   1,
295
+			}
296
+			err := AddSigleWarehouseOut(&warehouseOut)
297
+			if err != nil {
298
+				utils.TraceLog("创建出库单失败 err = %v", err)
299
+				return err
300
+			} else {
301
+				out = warehouseOut
302
+			}
303
+		}
304
+		for _,good := range goods {
305
+			ConsumablesDelivery(orgID ,patient_id ,record_time ,good,out )
306
+		}
307
+	}
308
+
309
+	if len(goods_yc) > 0 {
310
+		for _,good_yc := range goods_yc{
311
+			ConsumablesDeliveryDelete(orgID ,patient_id ,record_time ,good_yc ,out )
312
+		}
313
+	} 
314
+	
315
+	return nil 
316
+}
317
+
318
+// 耗材出库
319
+func ConsumablesDelivery(orgID int64,patient_id int64,record_time int64,goods models.DialysisBeforePrepareGoods,warehouseOut models.WarehouseOut) (err error) {
320
+	// 判断处方里药品单位是拆零单位还是包装单位,	如果是包装单位,则根据规格,将包装数量转为拆零数量
321
+	var deliver_number int64 = 0
322
+	var stock_number int64 = 0
323
+
324
+	deliver_number = good.Count
325
+	
326
+
327
+	// 根据先进先出原则,查询最先入库的批次,进行出库
328
+	// 如果没有对应的库存,则报错
329
+	warehouse, err := FindFirstWarehousingInfoByStock(goods.GoodId,goods.GoodTypeId)
330
+	if err != nil {
331
+		return err
332
+	}
333
+
334
+	// 将该批次的剩余库存数量转换为拆零数量
335
+	stock_number = warehouse.StockCount
336
+	// 当库存数量大于或等于出库数量的话,则正常出库该批次
337
+	if stock_number >= deliver_number {
338
+		warehouseOutInfo := &models.WarehouseOutInfo{
339
+			WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
340
+			WarehouseOutId:          warehouseOut.ID,
341
+			WarehouseInfoId:		 warehouse.ID,
342
+			Status:                  1,
343
+			Ctime:                   time.Now().Unix(),
344
+			Remark:                  "",
345
+			OrgId:                   orgID,
346
+			Type:                    1,
347
+			Manufacturer:            0,
348
+			Dealer:                  0,
349
+			IsSys:                   1,
350
+			SysRecordTime:           record_time,
351
+			GoodTypeId:              goods..GoodTypeId,
352
+			GoodId:                  goods.GoodId,
353
+			PatientId:				 patient_id,
354
+		}
355
+		warehouseOutInfo.Count = goods.Count
356
+		stockInInfo, _ := FindLastStockInInfoRecord(goods.GoodId, orgID)
357
+		warehouseOutInfo.Price = stockInInfo.Price
358
+		errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
359
+		if errOne != nil {
360
+			return errOne
361
+		} else {
362
+			details := &models.AutomaticReduceDetail{
363
+				WarehouseOutId:          warehouseOutInfo.ID,
364
+				WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
365
+				PatientId:               patient_id,
366
+				Ctime:                   time.Now().Unix(),
367
+				Mtime:                   time.Now().Unix(),
368
+				Status:                  1,
369
+				RecordTime:              record_time,
370
+				OrgId:                   orgID,
371
+				GoodId:                  goods.GoodId,
372
+				GoodTypeId:              goods.GoodTypeId,
373
+				Count:                   goods.Count,
374
+			}
375
+			errTwo := service.AddSigleAutoReduceRecordInfo(details)
376
+			if errTwo != nil {
377
+				return errTwo
378
+			}
379
+		}
380
+
381
+		// 出库完成后,要减去对应批次的库存数量
382
+		
383
+		maxNumber = goods.Count
384
+		
385
+
386
+		if warehouse.StockMaxNumber < maxNumber {
387
+			return errors.New("库存数量不足")
388
+		}
389
+
390
+		warehouse.StockNumber = warehouse.StockMaxNumber - maxNumber
391
+		warehouse.Mtime = time.Now().Unix()
392
+	
393
+
394
+		if warehouse.StockNumber < 0 {
395
+			return errors.New("库存数量不足")
396
+		}
397
+		errThree := UpDateWarehouseInfoByStock(&warehouse)
398
+		if errThree != nil {
399
+			return errThree
400
+		}
401
+
402
+		return nil
403
+	}  else {
404
+		// 当改批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
405
+		warehouseOutInfo := &models.WarehouseOutInfo{
406
+			WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
407
+			WarehouseOutId:          warehouseOut.ID,
408
+			WarehouseInfoId:		 warehouse.ID,
409
+			Status:                  1,
410
+			Ctime:                   time.Now().Unix(),
411
+			Remark:                  "",
412
+			OrgId:                   orgID,
413
+			Type:                    1,
414
+			Manufacturer:            0,
415
+			Dealer:                  0,
416
+			IsSys:                   1,
417
+			SysRecordTime:           record_time,
418
+			GoodTypeId:              goods..GoodTypeId,
419
+			GoodId:                  goods.GoodId,
420
+			PatientId:				 patient_id,
421
+		}
422
+		warehouseOutInfo.Count = stock_number
423
+		stockInInfo, _ := service.FindLastStockInInfoRecord(goods.GoodId, orgID)
424
+		warehouseOutInfo.Price = stockInInfo.Price
425
+		errOne := service.AddSigleWarehouseOutInfo(warehouseOutInfo)
426
+		if errOne != nil {
427
+			return errOne
428
+		} else {
429
+			details := &models.AutomaticReduceDetail{
430
+				WarehouseOutId:          warehouseOutInfo.ID,
431
+				WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
432
+				PatientId:               patient_id,
433
+				Ctime:                   time.Now().Unix(),
434
+				Mtime:                   time.Now().Unix(),
435
+				Status:                  1,
436
+				RecordTime:              record_time,
437
+				OrgId:                   orgID,
438
+				GoodId:                  goods.GoodId,
439
+				GoodTypeId:              goods.GoodTypeId,
440
+				Count:                   goods.Count,
441
+			}
442
+			errTwo := service.AddSigleAutoReduceRecordInfo(details)
443
+			if errTwo != nil {
444
+				return errTwo
445
+			}
446
+		}
447
+		// 出库完成后,要将该批次库存清零
448
+		warehouse.StockNumber = 0
449
+		warehouse.Mtime = time.Now().Unix()
450
+		errThree := UpDateWarehouseInfoByStock(&warehouse)
451
+		if errThree != nil {
452
+			return errThree
453
+		}
454
+		// 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
455
+		goods.Count := deliver_number - stock_number
456
+	
457
+		ConsumablesDelivery(orgID ,patient_id ,record_time ,good,out )
458
+	}
459
+	return nil
460
+}
461
+
462
+// 耗材出库删除
463
+func ConsumablesDeliveryDelete(orgID int64,patient_id int64,record_time int64,good_yc models.DialysisBeforePrepare,warehouseOut models.WarehouseOut) (err error) {
464
+	// 先根据相关信息查询当天该耗材的出库信息
465
+	warehouseOutInfos,err :=FindStockOutInfoByStock(orgID,good_yc.GoodTypeId,good_yc.GoodId,record_time,patient_id)
466
+	if err != nil {
467
+		return err
468
+	}
469
+	
470
+	var delete_count int64 = 0
471
+
472
+	for _ , ware := range warehouseOutInfos{
473
+		// 判断当前出库的数据和删除出库数量
474
+		if good_yc.Count <= ware.Count {
475
+			delete_count = good_yc.Count
476
+		} else {
477
+			delete_count = ware.Count
478
+		}
479
+		// 在出库记录表里记录退库详情
480
+		warehouseOutInfo := &models.WarehouseOutInfo{
481
+			WarehouseOutOrderNumber: warehouseOut.WarehouseOutOrderNumber,
482
+			WarehouseOutId:          warehouseOut.ID,
483
+			WarehouseInfoId:		 warehouse.ID,
484
+			Status:                  1,
485
+			Ctime:                   time.Now().Unix(),
486
+			Remark:                  "",
487
+			OrgId:                   orgID,
488
+			Type:                    1,
489
+			Manufacturer:            0,
490
+			Dealer:                  0,
491
+			IsSys:                   2,
492
+			SysRecordTime:           record_time,
493
+			GoodTypeId:              goods..GoodTypeId,
494
+			GoodId:                  goods.GoodId,
495
+			PatientId:				 patient_id,
496
+		}
497
+		warehouseOutInfo.Count = delete_count
498
+		stockInInfo, _ := FindLastStockInInfoRecord(goods.GoodId, orgID)
499
+		warehouseOutInfo.Price = stockInInfo.Price
500
+		errOne := AddSigleWarehouseOutInfo(warehouseOutInfo)
501
+		if errOne != nil {
502
+			return errOne
503
+		} else {
504
+			details := &models.AutomaticReduceDetail{
505
+				WarehouseOutId:          warehouseOutInfo.ID,
506
+				WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
507
+				PatientId:               patient_id,
508
+				Ctime:                   time.Now().Unix(),
509
+				Mtime:                   time.Now().Unix(),
510
+				Status:                  1,
511
+				RecordTime:              record_time,
512
+				OrgId:                   orgID,
513
+				GoodId:                  goods.GoodId,
514
+				GoodTypeId:              goods.GoodTypeId,
515
+				Count:                   delete_count,
516
+				Type:					 2,
517
+			}
518
+			errTwo := AddSigleAutoReduceRecordInfo(details)
519
+			if errTwo != nil {
520
+				return errTwo
521
+			}
522
+		}
523
+
524
+		// 删除出库完成后,要增加对应批次的库存数量
525
+			
526
+		warehouse.StockNumber = warehouse.StockMaxNumber - maxNumber
527
+		warehouse.Mtime = time.Now().Unix()
528
+		
529
+
530
+		errThree := UpDateWarehouseInfoByStockDelete(ware.WarehouseInfoId,delete_count)
531
+		if errThree != nil {
532
+			return errThree
533
+		}
534
+
535
+		// 增加了对应的库存后,看看还有多少需要退库的
536
+		good_yc.Count = goods_yc - delete_count 
537
+		if good_yc.Count == 0 {
538
+			return nil
539
+		}
540
+	}
541
+	if good_yd.Count == 0 {
542
+		return nil 
543
+	}  else {
544
+		return errors.New("退库和出库数据不匹配")
545
+	}
232 546
 }