Browse Source

11月8日库存管理

XMLWAN 2 years ago
parent
commit
c70fc990cb

+ 17 - 12
controllers/supply_order_api_contorller.go View File

@@ -2077,6 +2077,7 @@ func (this *SupplyOrderApiController) GetAllOrderCountList() {
2077 2077
 	purcaseOrder, _ := service.GetAllPurcaseOrderById(id, orgId)
2078 2078
 	//获取购货单的数据
2079 2079
 	goodOrder, err := service.GetAllGoodOrderByIdSix(id, orgId)
2080
+	fmt.Println("2323232332232323232332", goodOrder)
2080 2081
 	drugList, err := service.GetSupplyDrugList(orgId)
2081 2082
 
2082 2083
 	if err == nil {
@@ -2708,12 +2709,17 @@ func (this *SupplyOrderApiController) SaveGoodReturnOrder() {
2708 2709
 	}
2709 2710
 
2710 2711
 	for _, item := range warehouseCancel {
2712
+		if item.SupplyWarehouseDetailInfo > 0 {
2713
+			item.Type = 1
2714
+		} else {
2715
+			item.Type = 2
2716
+		}
2711 2717
 		service.CreateCancelReturnOrder(item)
2712 2718
 		//查询该商品在退库单中是否存在
2713 2719
 		_, errcodes := service.GetSupplyWarehouseOutIsExsit(item.WarehouseOutId, item.ProjectId, orgId)
2714 2720
 		//如果不存在则情况退库单的关联信息
2715 2721
 		if errcodes == gorm.ErrRecordNotFound {
2716
-			service.ModfySupplyCancel(item.WarehouseCancelId, orgId)
2722
+			service.ModfySupplyCancel(item.WarehouseCancelId, orgId, item.ProjectId)
2717 2723
 
2718 2724
 		}
2719 2725
 
@@ -3034,7 +3040,7 @@ func (this *SupplyOrderApiController) UpdateGoodReturn() {
3034 3040
 						Status:                  1,
3035 3041
 						Mtime:                   0,
3036 3042
 						WarehouseCancelId:       warehouse_out_id,
3037
-						Type:                    1,
3043
+						Type:                    2,
3038 3044
 						ReturnNumber:            return_number,
3039 3045
 						WarehouseOutId:          warehouse_out_id,
3040 3046
 						SupplySpecificationName: supply_specification_name,
@@ -3054,11 +3060,13 @@ func (this *SupplyOrderApiController) UpdateGoodReturn() {
3054 3060
 
3055 3061
 		if len(updateWarehouseCancel) > 0 {
3056 3062
 			for _, item := range updateWarehouseCancel {
3063
+
3057 3064
 				service.UpdateWarehouseCancelOrder(item)
3058 3065
 			}
3059 3066
 		}
3060 3067
 		if len(warehouseCancel) > 0 {
3061 3068
 			for _, item := range warehouseCancel {
3069
+
3062 3070
 				service.CreateCancelReturnOrder(item)
3063 3071
 			}
3064 3072
 		}
@@ -3277,14 +3285,14 @@ func (this *SupplyOrderApiController) CheckReturnOrder() {
3277 3285
 	//获取退库单详情
3278 3286
 	list, _ := service.GetSupplyCancelOrderDetail(id, orgId)
3279 3287
 	for _, item := range list {
3280
-
3288
+		fmt.Println("itme.233232323232323232323232323232232332", item.Type)
3281 3289
 		var total_count int64
3282 3290
 		var prescribing_number_total int64
3283 3291
 		//药品
3284 3292
 		if item.IsSource == 1 {
3285 3293
 
3286 3294
 			//获取药品库存
3287
-			info, _ := service.GetDrugTotalCountTwenTy(item.ProjectId, item.SupplyWarehouseDetailInfo)
3295
+			info, _ := service.GetDrugTotalCountTwenTy(item.ProjectId, item.SupplyWarehouseDetailInfo, item.Type)
3288 3296
 
3289 3297
 			for _, it := range info {
3290 3298
 
@@ -3399,12 +3407,13 @@ func (this *SupplyOrderApiController) CheckReturnOrder() {
3399 3407
 				SupplyCancelOutId:       id,
3400 3408
 				SupplyWarehouseId:       warehouse_out_id,
3401 3409
 				SysRecordTime:           record_date,
3410
+				IsSource:                item.Type,
3402 3411
 			}
3403 3412
 
3404 3413
 			drup, _ := service.FindBaseDrugLibRecord(orgId, item.ProjectId)
3405 3414
 			if drup.ID > 0 {
3406 3415
 				prescribingNumber := item.SupplyCount
3407
-				service.AutoDrugDeliverInfoNight(orgId, prescribingNumber, &warehouseOut, &drup, warehouseOutInfo)
3416
+				service.AutoDrugDeliverInfoNight(orgId, prescribingNumber, &warehouseOut, &drup, warehouseOutInfo, warehouse_out_id, id)
3408 3417
 			}
3409 3418
 
3410 3419
 			//修改原数据
@@ -3417,7 +3426,7 @@ func (this *SupplyOrderApiController) CheckReturnOrder() {
3417 3426
 		//耗材
3418 3427
 		if item.IsSource == 2 {
3419 3428
 			// 查询该耗材是否有库存
3420
-			warehouseOne, _ := service.FindWarehousingInfoTwenTy(item.ProjectId, item.SupplyWarehouseDetailInfo)
3429
+			warehouseOne, _ := service.FindWarehousingInfoTwenTy(item.ProjectId, item.SupplyWarehouseDetailInfo, item.Type)
3421 3430
 
3422 3431
 			// 如果出库数量大于该批次剩余库存数量
3423 3432
 			if item.SupplyCount > warehouseOne.StockCount {
@@ -3490,6 +3499,7 @@ func (this *SupplyOrderApiController) CheckReturnOrder() {
3490 3499
 				SupplyCancelOutId:       id,
3491 3500
 				OrgId:                   orgId,
3492 3501
 				SupplyWarehouseId:       warehouse_out_id,
3502
+				IsSource:                item.Type,
3493 3503
 			}
3494 3504
 			//出库逻辑
3495 3505
 			parseDateErr := service.ConsumablesDeliveryNight(orgId, record_date, info, &warehouseOut, item.SupplyCount, creater, warehouse_out_id, id)
@@ -3499,11 +3509,6 @@ func (this *SupplyOrderApiController) CheckReturnOrder() {
3499 3509
 				this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateStockInFail)
3500 3510
 				return
3501 3511
 			}
3502
-			//更改原退库数据
3503
-			//order := models.SpSupplierWarehousingCancelOrder{
3504
-			//	SourceCount: item.SourceCount - item.SupplyCount,
3505
-			//}
3506
-			//service.UpdateSupplyCancelById(item.ID, order)
3507 3512
 		}
3508 3513
 	}
3509 3514
 	cancel := models.SpSupplierWarehouseCancel{
@@ -3512,7 +3517,7 @@ func (this *SupplyOrderApiController) CheckReturnOrder() {
3512 3517
 		CheckTime: record_date,
3513 3518
 	}
3514 3519
 	err := service.CheckReturnOrder(id, orgId, cancel)
3515
-
3520
+	fmt.Println("3232233232322332232323232323322332232323232323")
3516 3521
 	if err == nil {
3517 3522
 		this.ServeSuccessJSON(map[string]interface{}{
3518 3523
 			"msg":        "1",

+ 1 - 0
models/drug_stock.go View File

@@ -154,6 +154,7 @@ type DrugWarehouseOutInfo struct {
154 154
 	AdviceId                int64   `gorm:"column:advice_id" json:"advice_id" form:"advice_id"`
155 155
 	SupplyCancelOutId       int64   `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
156 156
 	SupplyWarehouseId       int64   `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
157
+	IsSource                int64   `gorm:"column:is_source" json:"is_source" form:"is_source"`
157 158
 }
158 159
 
159 160
 func (DrugWarehouseOutInfo) TableName() string {

+ 1 - 0
models/stock_models.go View File

@@ -242,6 +242,7 @@ type WarehouseOutInfo struct {
242 242
 	ProjectId               int64        `gorm:"column:project_id" json:"project_id" form:"project_id"`
243 243
 	SupplyCancelOutId       int64        `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
244 244
 	SupplyWarehouseId       int64        `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
245
+	IsSource                int64        `gorm:"column:is_source" json:"is_source" form:"is_source"`
245 246
 }
246 247
 
247 248
 func (WarehouseOutInfo) TableName() string {

+ 2 - 0
models/supply.models.go View File

@@ -71,6 +71,7 @@ type SpWarehouseInfo struct {
71 71
 	Status                    int64 `gorm:"column:status" json:"status" form:"status"`
72 72
 	OrgId                     int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
73 73
 	SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
74
+	SupplyWarehouseId         int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
74 75
 }
75 76
 
76 77
 func (SpWarehouseInfo) TableName() string {
@@ -88,6 +89,7 @@ type SpDrugWarehouseInfo struct {
88 89
 	Status                    int64  `gorm:"column:status" json:"status" form:"status"`
89 90
 	OrgId                     int64  `gorm:"column:org_id" json:"org_id" form:"org_id"`
90 91
 	SupplyWarehouseDetailInfo int64  `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
92
+	SupplyWarehouseId         int64  `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
91 93
 }
92 94
 
93 95
 func (SpDrugWarehouseInfo) TableName() string {

+ 1 - 1
service/gobal_config_service.go View File

@@ -715,7 +715,7 @@ func GetDrugStockOutFlow(drugid int64, startime int64, endtime int64, page int64
715 715
 
716 716
 func GetBatchOrderDetail(drugid int64, orgid int64, page int64, limit int64) (drug []*models.DrugWarehouseInfo, total int64, err error) {
717 717
 	offset := (page - 1) * limit
718
-	err = XTReadDB().Model(&drug).Where("drug_id = ? and org_id = ?", drugid, orgid).Count(&total).Offset(offset).Limit(limit).Find(&drug).Error
718
+	err = XTReadDB().Model(&drug).Where("drug_id = ? and org_id = ? and status = 1", drugid, orgid).Count(&total).Offset(offset).Limit(limit).Find(&drug).Error
719 719
 	return drug, total, err
720 720
 }
721 721
 

+ 11 - 0
service/stock_service.go View File

@@ -772,6 +772,17 @@ func FindLastDrugWarehousingInfoByID(drug_id int64) (info models.XtDrugWarehouse
772 772
 	return info, err
773 773
 }
774 774
 
775
+func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64) (info models.XtDrugWarehouseInfo, err error) {
776
+	if is_source == 1 {
777
+		err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and supply_warehouse_id = ? ", drug_id, warehouse_out_id).Order("ctime").First(&info).Error
778
+	}
779
+
780
+	if is_source == 2 || is_source == 0 {
781
+		err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0)", drug_id).Order("ctime").First(&info).Error
782
+	}
783
+	return info, err
784
+}
785
+
775 786
 // 出库时先进先出,找出最先入库的批次
776 787
 func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
777 788
 	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

+ 43 - 16
service/supply_service.go View File

@@ -729,7 +729,7 @@ func GetGoodReturnOrderDetail(id int64, orgid int64) (order []*models.SpSupplier
729 729
 
730 730
 func UpdateWarehouseCancelOrder(order *models.SpSupplierWarehousingCancelOrder) error {
731 731
 
732
-	err = XTWriteDB().Model(&order).Where("id = ? and status = 1", order.ID).Updates(map[string]interface{}{"manufacturer_id": order.ManufacturerId, "order_number": order.OrderNumber, "project_id": order.ProjectId, "is_source": order.IsSource, "count": order.Count, "price": order.Price, "remark": order.Remark, "rate_of_concession": order.RateOfConcession, "discount_amount": order.DiscountAmount, "type": order.Type, "supply_specification_name": order.SupplySpecificationName, "supply_type": order.SupplyType, "supply_total": order.SupplyTotal, "supply_manufacturer": order.SupplyManufacturer, "name": order.Name, "supply_unit": order.SupplyUnit, "supply_license_number": order.SupplyLicenseNumber, "supply_count": order.SupplyCount, "deposit_rate": order.DepositRate}).Error
732
+	err = XTWriteDB().Model(&order).Where("id = ? and status = 1", order.ID).Updates(map[string]interface{}{"manufacturer_id": order.ManufacturerId, "order_number": order.OrderNumber, "project_id": order.ProjectId, "is_source": order.IsSource, "count": order.Count, "price": order.Price, "remark": order.Remark, "rate_of_concession": order.RateOfConcession, "discount_amount": order.DiscountAmount, "supply_specification_name": order.SupplySpecificationName, "supply_type": order.SupplyType, "supply_total": order.SupplyTotal, "supply_manufacturer": order.SupplyManufacturer, "name": order.Name, "supply_unit": order.SupplyUnit, "supply_license_number": order.SupplyLicenseNumber, "supply_count": order.SupplyCount, "deposit_rate": order.DepositRate}).Error
733 733
 	return err
734 734
 }
735 735
 
@@ -929,7 +929,7 @@ func GetDrugWarehouseOutById(orgid int64, record_date int64) (*models.DrugWareho
929 929
 
930 930
 	out := models.DrugWarehouseOut{}
931 931
 	var err error
932
-	err = XTReadDB().Where("org_id = ? and warehouse_out_time = ?", orgid, record_date).Find(&out).Error
932
+	err = XTReadDB().Where("org_id = ? and warehouse_out_time = ? and status =1", orgid, record_date).Find(&out).Error
933 933
 	if err == gorm.ErrRecordNotFound {
934 934
 		return nil, err
935 935
 	}
@@ -1026,25 +1026,46 @@ func ModfySupplyWarehouseOut(warehousing_id int64, orgid int64) error {
1026 1026
 	return err
1027 1027
 }
1028 1028
 
1029
-func FindWarehousingInfoTwenTy(goodId int64, supply_warehouse_detail_info int64) (models.WarehousingInfo, error) {
1029
+func FindWarehousingInfoTwenTy(goodId int64, supply_warehouse_detail_info int64, source int64) (models.WarehousingInfo, error) {
1030
+	fmt.Println("source232232332232323232323", source)
1030 1031
 	info := models.WarehousingInfo{}
1031
-	err := XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and supply_warehouse_detail_info =?", goodId, supply_warehouse_detail_info).Find(&info).Error
1032
+	var err error
1033
+	if source == 1 {
1034
+		err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and supply_warehouse_detail_info =?", goodId, supply_warehouse_detail_info).Find(&info).Error
1035
+
1036
+	}
1037
+	if source == 2 {
1038
+		err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1", goodId).Find(&info).Error
1039
+
1040
+	}
1032 1041
 	return info, err
1033 1042
 }
1034 1043
 
1035
-func GetDrugTotalCountTwenTy(drugid int64, supply_warehouse_detail_info int64) (list []*models.VmDrugWarehouseInfo, err error) {
1044
+func GetDrugTotalCountTwenTy(drugid int64, supply_warehouse_detail_info int64, source int64) (list []*models.VmDrugWarehouseInfo, err error) {
1036 1045
 
1037
-	db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
1038
-	table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
1039
-	fmt.Println(table)
1040
-	if drugid > 0 {
1041
-		db = db.Where("x.drug_id = ?", drugid)
1042
-	}
1043
-	if supply_warehouse_detail_info > 0 {
1044
-		db = db.Where("x.supply_warehouse_detail_info = ?", supply_warehouse_detail_info)
1046
+	if source == 1 {
1047
+		db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
1048
+		table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
1049
+		fmt.Println(table)
1050
+		if drugid > 0 {
1051
+			db = db.Where("x.drug_id = ?", drugid)
1052
+		}
1053
+		if supply_warehouse_detail_info > 0 {
1054
+			db = db.Where("x.supply_warehouse_detail_info = ?", supply_warehouse_detail_info)
1055
+		}
1056
+		err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,d.min_number,d.max_unit,d.min_unit,x.max_unit as count_unit,x.supply_warehouse_detail_info").Joins("left join xt_base_drug as d on d.id = x.drug_id").Scan(&list).Error
1045 1057
 	}
1046
-	err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,d.min_number,d.max_unit,d.min_unit,x.max_unit as count_unit,x.supply_warehouse_detail_info").Joins("left join xt_base_drug as d on d.id = x.drug_id").Scan(&list).Error
1047 1058
 
1059
+	if source == 2 {
1060
+		db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
1061
+		table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
1062
+		fmt.Println(table)
1063
+		if drugid > 0 {
1064
+			db = db.Where("x.drug_id = ?", drugid)
1065
+		}
1066
+		err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,d.min_number,d.max_unit,d.min_unit,x.max_unit as count_unit,x.supply_warehouse_detail_info").Joins("left join xt_base_drug as d on d.id = x.drug_id").Scan(&list).Error
1067
+
1068
+	}
1048 1069
 	return list, err
1049 1070
 }
1050 1071
 
@@ -1062,11 +1083,11 @@ func GetSupplyWarehouseOutIsExsit(warehouse_out_id int64, project_id int64, orgi
1062 1083
 	return &out, nil
1063 1084
 }
1064 1085
 
1065
-func ModfySupplyCancel(id int64, orgid int64) error {
1086
+func ModfySupplyCancel(id int64, orgid int64, project_id int64) error {
1066 1087
 
1067 1088
 	err := XTWriteDB().Model(models.SpSupplierWarehouseCancel{}).Where("id = ? and user_org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"warehouse_out_id": 0}).Error
1068 1089
 
1069
-	err = XTWriteDB().Model(models.SpSupplierWarehousingCancelOrder{}).Where("warehouse_cancel_id = ? and user_org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"warehouse_info_id": 0, "warehouse_out_id": 0, "warehousing_id": 0, "good_number": "", "order_number": ""}).Error
1090
+	err = XTWriteDB().Model(models.SpSupplierWarehousingCancelOrder{}).Where("warehouse_cancel_id = ? and user_org_id = ? and status = 1 and project_id = ?", id, orgid, project_id).Updates(map[string]interface{}{"warehouse_info_id": 0, "warehouse_out_id": 0, "warehousing_id": 0, "good_number": "", "order_number": ""}).Error
1070 1091
 	return err
1071 1092
 }
1072 1093
 
@@ -1168,3 +1189,9 @@ func GetAllGoodWarehouseInfo(orgid int64) (info []*models.WarehousingInfo, err e
1168 1189
 	err = XTReadDB().Where("org_id = ? and status = 1 and supply_warehouse_id>0", orgid).Find(&info).Error
1169 1190
 	return info, err
1170 1191
 }
1192
+
1193
+func GetLastDrugWarehosueInfoByWarehouseOutId(orgid int64, warehouse_out_id int64) (models.XtDrugWarehouseInfo, error) {
1194
+	info := models.XtDrugWarehouseInfo{}
1195
+	err := XTReadDB().Model(&info).Where("user_org_id = ? and status = 1 and supply_warehouse_id = ?", orgid, warehouse_out_id).Find(&info).Error
1196
+	return info, err
1197
+}

+ 40 - 7
service/warhouse_service.go View File

@@ -806,6 +806,7 @@ func AutoDrugDeliverInfo(orgID int64, prescribingNumber int64, warehouseout *mod
806 806
 
807 807
 	fmt.Println("库存1", stock_number)
808 808
 	fmt.Println("c出库数量", deliver_number)
809
+
809 810
 	// 当库存数量大于或等于出库数量的话,则正常出库该批次
810 811
 	if stock_number >= deliver_number {
811 812
 
@@ -877,6 +878,9 @@ func AutoDrugDeliverInfo(orgID int64, prescribingNumber int64, warehouseout *mod
877 878
 		if advice.CountUnit == drup.MinUnit {
878 879
 			maxNumber = prescribingNumber / drup.MinNumber
879 880
 			minNumber = prescribingNumber % drup.MinNumber
881
+			if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
882
+				minNumber = maxNumber
883
+			}
880 884
 		} else {
881 885
 			maxNumber = prescribingNumber
882 886
 		}
@@ -895,6 +899,11 @@ func AutoDrugDeliverInfo(orgID int64, prescribingNumber int64, warehouseout *mod
895 899
 		} else {
896 900
 			warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
897 901
 		}
902
+		fmt.Println("ware233232322332323232", warehouse.StockMaxNumber)
903
+		fmt.Println("ware233232322332323232", warehouse.StockMinNumber)
904
+		if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
905
+			warehouse.StockMinNumber = 0
906
+		}
898 907
 
899 908
 		if warehouse.StockMaxNumber < 0 {
900 909
 			return errors.New("库存数量不足")
@@ -2406,9 +2415,13 @@ func NewHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout
2406 2415
 		}
2407 2416
 
2408 2417
 		warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
2418
+		if warehouse.StockMaxNumber < 0 {
2419
+			warehouse.StockMaxNumber = 0
2420
+		}
2409 2421
 		warehouse.Mtime = time.Now().Unix()
2410 2422
 
2411 2423
 		if warehouse.StockMinNumber < minNumber {
2424
+			fmt.Println("233323232233223")
2412 2425
 			warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
2413 2426
 			warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
2414 2427
 		} else {
@@ -2782,7 +2795,7 @@ func ConsumablesDeliveryNight(orgID int64, record_time int64, goods *models.Ware
2782 2795
 	// 根据先进先出原则,查询最先入库的批次,进行出库
2783 2796
 
2784 2797
 	// 如果没有对应的库存,则报错
2785
-	warehouse, err := FindFirstWarehousingInfoByStockNight(goods.GoodId, goods.GoodTypeId, warehouse_out_id)
2798
+	warehouse, err := FindFirstWarehousingInfoByStockNight(goods.GoodId, goods.GoodTypeId, warehouse_out_id, goods.IsSource)
2786 2799
 	if err != nil {
2787 2800
 
2788 2801
 		return err
@@ -2970,13 +2983,21 @@ func ConsumablesDeliveryNight(orgID int64, record_time int64, goods *models.Ware
2970 2983
 	return nil
2971 2984
 }
2972 2985
 
2973
-func FindFirstWarehousingInfoByStockNight(good_id int64, good_type_id int64, warehouse_out_id int64) (info models.WarehousingInfo, err error) {
2974
-	err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0 and supply_warehouse_id = ?", good_id, good_type_id, warehouse_out_id).Order("ctime").First(&info).Error
2986
+func FindFirstWarehousingInfoByStockNight(good_id int64, good_type_id int64, warehouse_out_id int64, is_source int64) (info models.WarehousingInfo, err error) {
2987
+
2988
+	fmt.Println("is_source2233232233232323232232323", is_source)
2989
+	if is_source == 1 {
2990
+		err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0 and supply_warehouse_id = ?", good_id, good_type_id, warehouse_out_id).Order("ctime").First(&info).Error
2991
+
2992
+	}
2993
+	if is_source == 2 || is_source == 0 {
2994
+		err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0", good_id, good_type_id).Order("ctime").First(&info).Error
2995
+	}
2975 2996
 	return info, err
2976 2997
 }
2977 2998
 
2978 2999
 // 药品出库 递归方式
2979
-func AutoDrugDeliverInfoNight(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo) (err error) {
3000
+func AutoDrugDeliverInfoNight(orgID int64, prescribingNumber int64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.DrugWarehouseOutInfo, warehouse_out_id int64, cancel_out_id int64) (err error) {
2980 3001
 
2981 3002
 	// 判断处方里药品单位是拆零单位还是包装单位,	如果是包装单位,则根据规格,将包装数量转为拆零数量
2982 3003
 	var deliver_number int64 = 0
@@ -2992,7 +3013,7 @@ func AutoDrugDeliverInfoNight(orgID int64, prescribingNumber int64, warehouseout
2992 3013
 	// 如果没有对应的库存,则报错
2993 3014
 	//开启事物
2994 3015
 
2995
-	warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId)
3016
+	warehouse, err := FindLastDrugWarehousingInfoByIDSix(advice.DrugId, warehouse_out_id, advice.IsSource)
2996 3017
 
2997 3018
 	if err != nil {
2998 3019
 
@@ -3077,16 +3098,25 @@ func AutoDrugDeliverInfoNight(orgID int64, prescribingNumber int64, warehouseout
3077 3098
 		if advice.CountUnit == drup.MinUnit {
3078 3099
 			maxNumber = prescribingNumber / drup.MinNumber
3079 3100
 			minNumber = prescribingNumber % drup.MinNumber
3101
+			if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
3102
+				minNumber = maxNumber
3103
+			}
3080 3104
 		} else {
3081 3105
 			maxNumber = prescribingNumber
3082 3106
 		}
3083
-
3107
+		fmt.Println("maxNUMBER23323232323323322323", maxNumber)
3108
+		fmt.Println("MIN_NUMBWEEEWEWEWWEEWEWEWWE", minNumber)
3109
+		fmt.Println("编辑55555555555555")
3084 3110
 		if warehouse.StockMaxNumber < maxNumber {
3085 3111
 
3086 3112
 			return errors.New("库存数量不足")
3087 3113
 		}
3088 3114
 
3089 3115
 		warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
3116
+		if warehouse.StockMaxNumber < 0 {
3117
+			warehouse.StockMaxNumber = 0
3118
+		}
3119
+
3090 3120
 		warehouse.Mtime = time.Now().Unix()
3091 3121
 
3092 3122
 		if warehouse.StockMinNumber < minNumber {
@@ -3099,6 +3129,9 @@ func AutoDrugDeliverInfoNight(orgID int64, prescribingNumber int64, warehouseout
3099 3129
 		if warehouse.StockMaxNumber < 0 {
3100 3130
 			return errors.New("库存数量不足")
3101 3131
 		}
3132
+		if warehouse.StockMaxNumber == 1 && warehouse.StockMinNumber == 1 && drup.MaxUnit == drup.MinUnit {
3133
+			warehouse.StockMinNumber = 0
3134
+		}
3102 3135
 
3103 3136
 		errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
3104 3137
 		if errThree != nil {
@@ -3182,7 +3215,7 @@ func AutoDrugDeliverInfoNight(orgID int64, prescribingNumber int64, warehouseout
3182 3215
 		prescribingNumber_two_temp := deliver_number - stock_number
3183 3216
 		advice.CountUnit = drup.MinUnit
3184 3217
 
3185
-		AutoDrugDeliverInfoNight(orgID, prescribingNumber_two_temp, warehouseout, drup, advice)
3218
+		AutoDrugDeliverInfoNight(orgID, prescribingNumber_two_temp, warehouseout, drup, advice, warehouse_out_id, cancel_out_id)
3186 3219
 	}
3187 3220
 
3188 3221
 	return