浏览代码

历史排班

28169 10 个月前
父节点
当前提交
791562a239

+ 118 - 81
controllers/his_api_controller.go 查看文件

@@ -3380,11 +3380,6 @@ func (c *HisApiController) CreateHisPrescription() {
3380 3380
 									var is_user_total int64
3381 3381
 									var no_user_total int64
3382 3382
 
3383
-									//fmt.Println("s-------------------------------------------", s.PrescribingNumber)
3384
-									//fmt.Println("s.PrescribingNumberUnit------------------", s.PrescribingNumberUnit)
3385
-									//fmt.Println("drug.MaxUnit", drug.MaxUnit)
3386
-									//fmt.Println("drug.MinUnit", drug.MinUnit)
3387
-
3388 3383
 									if s.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
3389 3384
 
3390 3385
 										prescribingNumberTenty := strconv.FormatFloat(s.PrescribingNumber, 'f', -1, 64)
@@ -3425,45 +3420,41 @@ func (c *HisApiController) CreateHisPrescription() {
3425 3420
 
3426 3421
 									}
3427 3422
 
3428
-									//fmt.Println("is_user_total-----------------------------", is_user_total)
3429
-									//fmt.Println("no_user_total-----------------------------", no_user_total)
3423
+									fmt.Println("is_user_total-----------------------------", is_user_total)
3424
+									fmt.Println("no_user_total-----------------------------", no_user_total)
3430 3425
 
3431 3426
 									if is_user_total != no_user_total {
3432 3427
 
3433 3428
 										if len(druginfo) > 0 {
3434 3429
 											//回退库存
3435
-											for _, it := range druginfo {
3436
-												if it.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
3437
-
3438
-													service.ModifyDrugWarehouseInfoStockMaxNumber(it.Count, it.DrugId, it.OrgId, it.WarehouseInfoId)
3439
-
3440
-												}
3441
-												if it.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
3442
-
3443
-													service.ModifyDrugWarehouseInfoStockMinNumber(it.Count, it.DrugId, it.OrgId, it.WarehouseInfoId)
3444
-
3445
-												}
3446
-												if it.CountUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
3447
-
3448
-													service.ModifyDrugWarehouseInfoStockMaxNumber(it.Count, it.DrugId, it.OrgId, it.WarehouseInfoId)
3449
-
3450
-												}
3451
-											}
3430
+											//for _, it := range druginfo {
3431
+											//	if it.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
3432
+											//
3433
+											//		service.ModifyDrugWarehouseInfoStockMaxNumber(it.Count, it.DrugId, it.OrgId, it.WarehouseInfoId)
3434
+											//
3435
+											//	}
3436
+											//	if it.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
3437
+											//
3438
+											//		service.ModifyDrugWarehouseInfoStockMinNumber(it.Count, it.DrugId, it.OrgId, it.WarehouseInfoId)
3439
+											//
3440
+											//	}
3441
+											//	if it.CountUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
3442
+											//
3443
+											//		service.ModifyDrugWarehouseInfoStockMaxNumber(it.Count, it.DrugId, it.OrgId, it.WarehouseInfoId)
3444
+											//
3445
+											//	}
3446
+											//}
3452 3447
 
3453 3448
 											//删除记录
3454
-											service.DeleteNewDrugAutoWarehouseSeven(s.DrugId, patient_id, recordDateTime, s.ID)
3449
+											//service.DeleteNewDrugAutoWarehouseSeven(s.DrugId, patient_id, recordDateTime, s.ID)
3455 3450
 
3456 3451
 										}
3457 3452
 
3458
-										service.CreateHisDoctorAdviceOne(&s, tx)
3459
-
3460
-										advicelast, _ := service.GetLastHisAdvicesList(s.PatientId, s.AdviceDate, s.UserOrgId, tx)
3461
-
3462 3453
 										//出库
3463 3454
 										if drug.IsUse == 2 {
3464 3455
 											creater := adminInfo.AdminUser.Id
3465 3456
 											newadviceInfo := &models.HisDoctorAdviceInfo{
3466
-												ID:                    advicelast.ID,
3457
+												ID:                    s.ID,
3467 3458
 												UserOrgId:             s.UserOrgId,
3468 3459
 												PatientId:             s.PatientId,
3469 3460
 												HisPatientId:          s.HisPatientId,
@@ -3529,7 +3520,80 @@ func (c *HisApiController) CreateHisPrescription() {
3529 3520
 												DrugWayCount:          s.DrugWayCount,
3530 3521
 											}
3531 3522
 
3532
-											service.NewHisDrugsDelivery(s.UserOrgId, creater, newadviceInfo, tx)
3523
+											var history_count int64
3524
+											var local_count int64
3525
+											//查找已经开过的医嘱信息
3526
+											adviceHistory, _ := service.GetHistory(s.ID, s.UserOrgId, tx)
3527
+
3528
+											if s.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
3529
+
3530
+												prescribingNumberTenty := strconv.FormatFloat(s.PrescribingNumber, 'f', -1, 64)
3531
+												prescribingNumberSevenTy, _ := strconv.ParseInt(prescribingNumberTenty, 10, 64)
3532
+
3533
+												local_count += prescribingNumberSevenTy * drug.MinNumber
3534
+											}
3535
+
3536
+											if s.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
3537
+
3538
+												prescribingNumberTenty := strconv.FormatFloat(s.PrescribingNumber, 'f', -1, 64)
3539
+												prescribingNumberSevenTy, _ := strconv.ParseInt(prescribingNumberTenty, 10, 64)
3540
+												local_count += prescribingNumberSevenTy
3541
+											}
3542
+
3543
+											if s.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
3544
+
3545
+												prescribingNumberTenty := strconv.FormatFloat(s.PrescribingNumber, 'f', -1, 64)
3546
+												prescribingNumberSevenTy, _ := strconv.ParseInt(prescribingNumberTenty, 10, 64)
3547
+												local_count += prescribingNumberSevenTy
3548
+											}
3549
+
3550
+											if adviceHistory.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
3551
+
3552
+												prescribingNumberTenty := strconv.FormatFloat(adviceHistory.PrescribingNumber, 'f', -1, 64)
3553
+												prescribingNumberSevenTy, _ := strconv.ParseInt(prescribingNumberTenty, 10, 64)
3554
+
3555
+												history_count += prescribingNumberSevenTy * drug.MinNumber
3556
+											}
3557
+
3558
+											if adviceHistory.PrescribingNumberUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
3559
+
3560
+												prescribingNumberTenty := strconv.FormatFloat(adviceHistory.PrescribingNumber, 'f', -1, 64)
3561
+												prescribingNumberSevenTy, _ := strconv.ParseInt(prescribingNumberTenty, 10, 64)
3562
+												history_count += prescribingNumberSevenTy
3563
+											}
3564
+
3565
+											if adviceHistory.PrescribingNumberUnit == drug.MaxUnit && drug.MaxUnit == drug.MinUnit {
3566
+
3567
+												prescribingNumberTenty := strconv.FormatFloat(adviceHistory.PrescribingNumber, 'f', -1, 64)
3568
+												prescribingNumberSevenTy, _ := strconv.ParseInt(prescribingNumberTenty, 10, 64)
3569
+												history_count += prescribingNumberSevenTy
3570
+											}
3571
+
3572
+											//fmt.Println("local_count0000000000000000000000000000000000000000000", local_count)
3573
+											//
3574
+											//fmt.Println("history_count999999999999999999999999999999999999999", history_count)
3575
+
3576
+											//如果当前出库数量大于历史出库数量则需要出库
3577
+											if local_count > history_count {
3578
+												//按最小单位出库
3579
+												var cha_count = local_count - history_count
3580
+
3581
+												//fmt.Println("cha_count000000000000000000000000000", cha_count)
3582
+
3583
+												newadviceInfo.PrescribingNumber = float64(cha_count)
3584
+												newadviceInfo.PrescribingNumberUnit = drug.MinUnit
3585
+												service.NewHisDrugsDelivery(s.UserOrgId, creater, newadviceInfo, tx)
3586
+											}
3587
+
3588
+											//如果当前出库数量小于历史出库数量则需要退库
3589
+											if local_count < history_count {
3590
+
3591
+												var cha_count = history_count - local_count
3592
+												//fmt.Println("退库----------------------------", cha_count)
3593
+												service.NewHisDrugCancelInfo(s.UserOrgId, creater, newadviceInfo, cha_count, drug, tx)
3594
+
3595
+												service.NewHisDrugAutoCancelInfo(s.UserOrgId, creater, newadviceInfo, cha_count, drug, tx)
3596
+											}
3533 3597
 
3534 3598
 										}
3535 3599
 
@@ -3551,6 +3615,25 @@ func (c *HisApiController) CreateHisPrescription() {
3551 3615
 										over, _ := service.FindNewOverCount(s.DrugId, s.UserOrgId, storeConfig.DrugStorehouseOut, tx)
3552 3616
 										service.UpdateNewActOut(over.ID, over.SumInCount, over.FlushCount, over.SumCancelCount, tx)
3553 3617
 
3618
+										//创建医嘱
3619
+										service.CreateHisDoctorAdviceOne(&s, tx)
3620
+
3621
+										adviceObj, _ := service.GetLastHisDoctorAdviceById(s.PatientId, s.UserOrgId, s.AdviceDate, s.DrugId, tx)
3622
+
3623
+										outInfoTwo, _ := service.GetDrugWarehouseOutInfoByAdviceId(s.PatientId, s.UserOrgId, s.AdviceDate, s.DrugId, tx)
3624
+
3625
+										if len(outInfoTwo) > 0 {
3626
+											for _, item := range outInfoTwo {
3627
+												service.UpdateDrugOutInfoByAdviceId(item.ID, adviceObj.ID, tx)
3628
+											}
3629
+										}
3630
+
3631
+										drugAutoDetail, _ := service.GetDrugAutoDetail(s.PatientId, s.UserOrgId, s.AdviceDate, s.DrugId, tx)
3632
+										if len(drugAutoDetail) > 0 {
3633
+											for _, item := range drugAutoDetail {
3634
+												service.UpdateDrugAutoDetail(item.ID, adviceObj.ID, tx)
3635
+											}
3636
+										}
3554 3637
 									}
3555 3638
 								}
3556 3639
 
@@ -3587,53 +3670,7 @@ func (c *HisApiController) CreateHisPrescription() {
3587 3670
 								timeFormat := tempTime.Format("20060102150405")
3588 3671
 								s.FeedetlSn = timeFormat + strconv.FormatInt(int64(randNum), 10) + "-" + "1" + "-" + strconv.FormatInt(s.ID, 10)
3589 3672
 								service.CreateHisDoctorAdviceOne(&s, tx)
3590
-								//list, _ := service.GetHisToday(s.PatientId, s.AdviceDate, s.UserOrgId, s.DrugId, tx)
3591
-								//var count_total int64
3592
-								//var count_one int64
3593
-								//for _, its := range list {
3594
-								//	baseDrug, _ := service.GetNewBaseDrugMedical(its.DrugId, tx)
3595
-								//	if its.PrescribingNumberUnit == baseDrug.MaxUnit && baseDrug.MaxUnit != baseDrug.MinUnit {
3596
-								//		prescribingNumberTenty := strconv.FormatFloat(its.PrescribingNumber, 'f', -1, 64)
3597
-								//		prescribingNumberSevenTy, _ := strconv.ParseInt(prescribingNumberTenty, 10, 64)
3598
-								//		count_total += prescribingNumberSevenTy * baseDrug.MinNumber
3599
-								//	}
3600
-								//
3601
-								//	if its.PrescribingNumberUnit == baseDrug.MinUnit && baseDrug.MaxUnit != baseDrug.MinUnit {
3602
-								//		prescribingNumberTenty := strconv.FormatFloat(its.PrescribingNumber, 'f', -1, 64)
3603
-								//		prescribingNumberSevenTy, _ := strconv.ParseInt(prescribingNumberTenty, 10, 64)
3604
-								//		count_total += prescribingNumberSevenTy
3605
-								//	}
3606
-								//
3607
-								//	if its.PrescribingNumberUnit == baseDrug.MaxUnit && baseDrug.MaxUnit == baseDrug.MinUnit {
3608
-								//		prescribingNumberTenty := strconv.FormatFloat(its.PrescribingNumber, 'f', -1, 64)
3609
-								//		prescribingNumberSevenTy, _ := strconv.ParseInt(prescribingNumberTenty, 10, 64)
3610
-								//		count_total += prescribingNumberSevenTy
3611
-								//	}
3612
-								//
3613
-								//	drugAuto, _ := service.GetLastDrugAuto(its.ID, its.DrugId, its.AdviceDate, its.PatientId,its.UserOrgId, tx)
3614
-								//
3615
-								//	if drugAuto.CountUnit == baseDrug.MaxUnit && baseDrug.MaxUnit != baseDrug.MinUnit {
3616
-								//
3617
-								//		count_one = drugAuto.Count * baseDrug.MinNumber
3618
-								//	}
3619
-								//
3620
-								//	if drugAuto.CountUnit == baseDrug.MinUnit && baseDrug.MaxUnit != baseDrug.MinUnit {
3621
-								//
3622
-								//		count_one = drugAuto.Count
3623
-								//	}
3624
-								//
3625
-								//	if drugAuto.CountUnit == baseDrug.MaxUnit && baseDrug.MaxUnit == baseDrug.MinUnit {
3626
-								//
3627
-								//		count_one = drugAuto.Count
3628
-								//	}
3629
-								//	fmt.Println("count_one==================", count_one)
3630
-								//	fmt.Println("count_total==================", count_total)
3631
-								//	if count_total != count_one {
3632
-								//
3633
-								//		service.UpdateTodayHis(its.DrugId, its.AdviceDate, its.PatientId, its.UserOrgId, baseDrug.MinUnit, count_total, its.ID, tx)
3634
-								//	}
3635
-								//
3636
-								//}
3673
+
3637 3674
 								tx.Commit()
3638 3675
 
3639 3676
 								keySix := strconv.FormatInt(adminInfo.CurrentOrgId, 10) + ":" + strconv.FormatInt(theTime.Unix(), 10) + ":his_advices_list_all"
@@ -5933,8 +5970,8 @@ func (c *HisApiController) DeleteProject() {
5933 5970
 					//查询已出库数量
5934 5971
 					dialysisInfo, _ := service.GetGoodDialysisOutInfoSix(adminInfo.CurrentOrgId, info.PatientId, info.SysRecordTime, project.ProjectId)
5935 5972
 
5936
-					fmt.Println("aiwohonguo --------------------", prescribingNumber)
5937
-					fmt.Println("aiwohonguo --------------------", dialysisInfo.Count)
5973
+					//fmt.Println("aiwohonguo --------------------", prescribingNumber)
5974
+					//fmt.Println("aiwohonguo --------------------", dialysisInfo.Count)
5938 5975
 					if prescribingNumber < dialysisInfo.Count {
5939 5976
 						//改变数量
5940 5977
 						service.UpdateAutoGood(adminInfo.CurrentOrgId, info.PatientId, info.SysRecordTime, project.ProjectId, id, prescribingNumber)
@@ -7372,7 +7409,7 @@ func (c *HisApiController) setAdviceWithJSON(advice *models.HisDoctorAdviceInfo,
7372 7409
 							advice.CheckState = 2
7373 7410
 						}
7374 7411
 					} else {
7375
-						if hisAdvice.SingleDose != advice.SingleDose || hisAdvice.DeliveryWay != advice.DeliveryWay || hisAdvice.ExecutionFrequency != advice.ExecutionFrequency || hisAdvice.Day != advice.Day || hisAdvice.PrescribingNumber != advice.PrescribingNumber || hisAdvice.Price != advice.Price || hisAdvice.Remark != advice.Remark || hisAdvice.PrescribingNumberUnit != advice.PrescribingNumberUnit {
7412
+						if hisAdvice.SingleDoseUnit != advice.SingleDoseUnit || hisAdvice.SingleDose != advice.SingleDose || hisAdvice.DeliveryWay != advice.DeliveryWay || hisAdvice.ExecutionFrequency != advice.ExecutionFrequency || hisAdvice.Day != advice.Day || hisAdvice.PrescribingNumber != advice.PrescribingNumber || hisAdvice.Price != advice.Price || hisAdvice.Remark != advice.Remark || hisAdvice.PrescribingNumberUnit != advice.PrescribingNumberUnit {
7376 7413
 							advice.ExecutionTime = 0
7377 7414
 							advice.ExecutionStaff = 0
7378 7415
 							advice.ExecutionState = 2

+ 10 - 9
controllers/sign_api_controller.go 查看文件

@@ -1223,7 +1223,7 @@ func (this *SignApiController) ToAutoDrug() {
1223 1223
 
1224 1224
 func (this *SignApiController) ToAutoDiagnose() {
1225 1225
 
1226
-	orgId := this.GetAdminUserInfo().CurrentOrgId
1226
+	//orgId := this.GetAdminUserInfo().CurrentOrgId
1227 1227
 
1228 1228
 	//order, _ := service.GetDialysisOrderTotalCount()
1229 1229
 	//
@@ -1238,23 +1238,24 @@ func (this *SignApiController) ToAutoDiagnose() {
1238 1238
 	//	service.UpdateAllPatient(item.BloodId, item.Diagnose, item.UserOrgId)
1239 1239
 	//}
1240 1240
 
1241
-	list, _ := service.GetAllDialysisOrder(10469)
1242
-	for _, item := range list {
1243
-		schedule, _ := service.GetLastScheduleByUserOrg(item.PatientId, item.DialysisDate, orgId, item.BedId, item.ZoneId, item.SchedualType)
1244
-		service.UpdateScheduleByOrder(item.PatientId, item.DialysisDate, orgId, item.BedId, item.ZoneId, item.SchedualType, schedule.ID)
1245
-	}
1241
+	//list, _ := service.GetAllDialysisOrder(10469)
1242
+	//for _, item := range list {
1243
+	//	schedule, _ := service.GetLastScheduleByUserOrg(item.PatientId, item.DialysisDate, orgId, item.BedId, item.ZoneId, item.SchedualType)
1244
+	//	service.UpdateScheduleByOrder(item.PatientId, item.DialysisDate, orgId, item.BedId, item.ZoneId, item.SchedualType, schedule.ID)
1245
+	//}
1246 1246
 
1247
-	//drugList, _ := service.GetAllBaseDrugList(10571)
1247
+	//drugList, _ := service.GetAllBaseDrugList(10653)
1248 1248
 	//for _, item := range drugList {
1249
-	//	//service.UpdateDrugWarehouseInfoByDrug(item.ID, item.MinPrice, item.OrgId)
1249
+	//	service.UpdateDrugWarehouseInfoByDrug(item.ID, item.MinPrice, item.OrgId)
1250 1250
 	//
1251
-	//	service.UpdateHisDoctorAdviceOne(item.ID, item.MinPrice, item.OrgId)
1251
+	//	//service.UpdateHisDoctorAdviceOne(item.ID, item.MinPrice, item.OrgId)
1252 1252
 	//}
1253 1253
 
1254 1254
 	//goodList, _ := service.GetAllGoodList(10571)
1255 1255
 	//for _, item := range goodList {
1256 1256
 	//	service.UpdasteGoodWarehouseInfoByGood(item.ID, item.PackingPrice, item.OrgId)
1257 1257
 	//}
1258
+
1258 1259
 	this.ServeSuccessJSON(map[string]interface{}{
1259 1260
 		"msg": "ok",
1260 1261
 	})

+ 18 - 8
models/inspection_models.go 查看文件

@@ -237,17 +237,27 @@ func (XtPatientsTenOne) TableName() string {
237 237
 	return "xt_patients"
238 238
 }
239 239
 
240
+type InspectionValue struct {
241
+	Name  string `gorm:"-" json:"name" form:"name"`
242
+	Value string `gorm:"-" json:"value" form:"value"`
243
+}
244
+
240 245
 type InspectionTen struct {
241
-	ID        int64  `gorm:"column:id" json:"id" form:"id"`
242
-	PatientId int64  `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
243
-	Name      string `gorm:"-" json:"name" form:"name"`
244
-	Ipth      string `gorm:"-" json:"ipth" form:"ipth"`
245
-	Xuejia    string `gorm:"-" json:"xuejia" form:"xuejia"`
246
-	Xuelin    string `gorm:"-" json:"xuelin" form:"xuelin"`
247
-	Bdb       string `gorm:"-" json:"bdb" form:"bdb"`
248
-	Xhdb      string `gorm:"-" json:"xhdb" form:"xhdb"`
246
+	ID        int64             `gorm:"column:id" json:"id" form:"id"`
247
+	PatientId int64             `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
248
+	Name      string            `gorm:"-" json:"name" form:"name"`
249
+	Values    []InspectionValue `gorm:"-" json:"values" form:"values"`
249 250
 }
250 251
 
251 252
 func (InspectionTen) TableName() string {
252 253
 	return "xt_inspection"
253 254
 }
255
+
256
+type InspectionTenOne struct {
257
+	ID           int64  `gorm:"-" json:"id" form:"id"`
258
+	PatientId    int64  `gorm:"-" json:"patient_id" form:"patient_id"`
259
+	Name         string `gorm:"-" json:"name" form:"name"`
260
+	ItemName     string `gorm:"-" json:"item_name" form:"item_name"`
261
+	InspectValue string `gorm:"-" json:"inspect_value" form:"inspect_value"`
262
+	ItemID       string `gorm:"-" json:"item_id" form:"item_id"`
263
+}

+ 154 - 0
service/his_service.go 查看文件

@@ -3118,3 +3118,157 @@ func UpdateGood(good_id int64, user_org_id int64, packing_price float64) (models
3118 3118
 
3119 3119
 	return goodInfo, err
3120 3120
 }
3121
+
3122
+func GetHistory(id int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
3123
+
3124
+	adviceInfo := models.HisDoctorAdviceInfo{}
3125
+
3126
+	err := tx.Where("id = ? and user_org_id = ? and status=1", id, user_org_id).Find(&adviceInfo).Error
3127
+	if err != gorm.ErrRecordNotFound {
3128
+		if err != nil {
3129
+			tx.Rollback()
3130
+			return adviceInfo, err
3131
+		}
3132
+	}
3133
+	return adviceInfo, err
3134
+}
3135
+
3136
+func GetLastHisDoctorAdviceById(patient_id int64, user_org_id int64, advice_date int64, drug_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
3137
+
3138
+	adviceInfo := models.HisDoctorAdviceInfo{}
3139
+
3140
+	err := tx.Where("patient_id = ? and user_org_id = ? and advice_date = ? and drug_id = ? and status =1", patient_id, user_org_id, advice_date, drug_id).Last(&adviceInfo).Error
3141
+
3142
+	if err != gorm.ErrRecordNotFound {
3143
+		if err != nil {
3144
+			tx.Rollback()
3145
+			return adviceInfo, err
3146
+		}
3147
+	}
3148
+	return adviceInfo, err
3149
+}
3150
+
3151
+func GetDrugWarehouseOutInfoByAdviceId(patient_id int64, user_org_id int64, sys_record_date int64, drug_id int64, tx *gorm.DB) (outInfo []*models.DrugWarehouseOutInfo, err error) {
3152
+
3153
+	err = tx.Where("patient_id = ? and org_id =? and sys_record_time = ? and drug_id = ? and status=1 and advice_id = 0", patient_id, user_org_id, sys_record_date, drug_id).Find(&outInfo).Error
3154
+
3155
+	if err != gorm.ErrRecordNotFound {
3156
+		if err != nil {
3157
+			tx.Rollback()
3158
+			return outInfo, err
3159
+		}
3160
+	}
3161
+	return outInfo, err
3162
+}
3163
+
3164
+func GetDrugWarehouseOutInfoByAdviceIdTwo(patient_id int64, user_org_id int64, sys_record_date int64, drug_id int64, advice_id int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
3165
+
3166
+	outInfo := models.DrugWarehouseOutInfo{}
3167
+	err = tx.Where("patient_id = ? and org_id =? and sys_record_time = ? and drug_id = ? and status=1 and advice_id = ?", patient_id, user_org_id, sys_record_date, drug_id, advice_id).Order("id desc").First(&outInfo).Error
3168
+	if err != gorm.ErrRecordNotFound {
3169
+		if err != nil {
3170
+			tx.Rollback()
3171
+			return outInfo, err
3172
+		}
3173
+	}
3174
+	return outInfo, err
3175
+}
3176
+
3177
+func GetDrugAutoOutInfoByAdviceIdTwo(patient_id int64, user_org_id int64, sys_record_date int64, drug_id int64, advice_id int64, tx *gorm.DB) (models.DrugAutomaticReduceDetail, error) {
3178
+
3179
+	outInfo := models.DrugAutomaticReduceDetail{}
3180
+	err = tx.Where("patient_id = ? and org_id =? and record_time = ? and drug_id = ? and status=1 and advice_id = ?", patient_id, user_org_id, sys_record_date, drug_id, advice_id).Order("id desc").First(&outInfo).Error
3181
+	if err != gorm.ErrRecordNotFound {
3182
+		if err != nil {
3183
+			tx.Rollback()
3184
+			return outInfo, err
3185
+		}
3186
+	}
3187
+	return outInfo, err
3188
+}
3189
+
3190
+func GetDrugAutoDetail(patient_id int64, org_id int64, record_time int64, drug_id int64, tx *gorm.DB) (automaticReduceDetail []*models.DrugAutomaticReduceDetail, err error) {
3191
+
3192
+	err = tx.Where("patient_id = ? and org_id = ? and record_time =? and drug_id = ? and status =1 and advice_id = 0", patient_id, org_id, record_time, drug_id).Find(&automaticReduceDetail).Error
3193
+
3194
+	if err != gorm.ErrRecordNotFound {
3195
+		if err != nil {
3196
+			tx.Rollback()
3197
+			return automaticReduceDetail, err
3198
+		}
3199
+	}
3200
+	return automaticReduceDetail, err
3201
+}
3202
+
3203
+func UpdateDrugAutoDetail(id int64, advice_id int64, tx *gorm.DB) error {
3204
+
3205
+	drugAutomaticReduceDetail := models.DrugAutomaticReduceDetail{}
3206
+
3207
+	err := tx.Model(&drugAutomaticReduceDetail).Where("id = ? and status=1", id).Updates(map[string]interface{}{"advice_id": advice_id}).Error
3208
+
3209
+	if err != nil {
3210
+		tx.Rollback()
3211
+		return err
3212
+	}
3213
+	return err
3214
+}
3215
+
3216
+func UpdateDrugOutInfoByAdviceId(id int64, advice_id int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
3217
+
3218
+	info := models.DrugWarehouseOutInfo{}
3219
+
3220
+	err := tx.Model(&info).Where("id = ? and status=1", id).Updates(map[string]interface{}{"advice_id": advice_id}).Error
3221
+
3222
+	if err != nil {
3223
+		tx.Rollback()
3224
+		return info, err
3225
+	}
3226
+
3227
+	return info, err
3228
+}
3229
+
3230
+func ModifyDrugWarehouseOutInfoByNumber(id int64, cha_count int64, tx *gorm.DB) error {
3231
+
3232
+	err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count - ?", cha_count)).Error
3233
+
3234
+	err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status =1", id).UpdateColumn("over_count", gorm.Expr("over_count + ?", cha_count)).Error
3235
+
3236
+	if err != nil {
3237
+		tx.Rollback()
3238
+		return err
3239
+	}
3240
+	return err
3241
+}
3242
+
3243
+func ModifyDrugAuToWarehouseOut(id int64, cha_count int64, tx *gorm.DB) error {
3244
+
3245
+	err := tx.Model(&models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count - ?", cha_count)).Error
3246
+
3247
+	if err != nil {
3248
+		tx.Rollback()
3249
+		return err
3250
+	}
3251
+	return err
3252
+}
3253
+
3254
+func DeleteDrugWarehouseOutInfoByNumber(id int64, tx *gorm.DB) error {
3255
+
3256
+	err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"status": 0}).Error
3257
+
3258
+	if err != nil {
3259
+		tx.Rollback()
3260
+		return err
3261
+	}
3262
+	return err
3263
+}
3264
+
3265
+func DeleteDrugWarehouseAutoByNumber(id int64, tx *gorm.DB) error {
3266
+
3267
+	err := tx.Model(&models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"status": 0}).Error
3268
+
3269
+	if err != nil {
3270
+		tx.Rollback()
3271
+		return err
3272
+	}
3273
+	return err
3274
+}

+ 840 - 86
service/new_warehouse_service.go 查看文件

@@ -3857,12 +3857,6 @@ func NewBloodHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehous
3857 3857
 		//扣减库存数据
3858 3858
 		UpDateNewDrugWarehouseInfoByStock(&warehouse, tx)
3859 3859
 
3860
-		//更改字典里面的价格
3861
-		//if advice.UserOrgId == 10265 || advice.UserOrgId == 10215 {
3862
-		//
3863
-		//	UpdateNewBasePrice(lastWarehouse.DrugId, lastWarehouse.RetailPrice, tx)
3864
-		//}
3865
-		//
3866 3860
 		////查询剩余库存
3867 3861
 		stockInfo, _ := GetNewDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId, tx)
3868 3862
 		var sum_count int64
@@ -3873,55 +3867,54 @@ func NewBloodHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehous
3873 3867
 			sum_count += its.StockMaxNumber + its.StockMinNumber
3874 3868
 		}
3875 3869
 
3876
-		if orgID != 10188 && orgID != 10217 && orgID != 3877 {
3877
-			warehouseOutInfo := &models.DrugWarehouseOutInfo{
3878
-				WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
3879
-				WarehouseOutId:          warehouseout.ID,
3880
-				Status:                  1,
3881
-				Ctime:                   advice.RecordDate,
3882
-				Mtime:                   time.Now().Unix(),
3883
-				Remark:                  warehouse.Remark,
3884
-				OrgId:                   orgID,
3885
-				Type:                    1,
3886
-				Manufacturer:            warehouse.Manufacturer,
3887
-				Dealer:                  warehouse.Dealer,
3888
-				IsSys:                   1,
3889
-				SysRecordTime:           advice.RecordDate,
3890
-				DrugId:                  advice.DrugId,
3891
-				Number:                  warehouse.Number,
3892
-				BatchNumber:             warehouse.BatchNumber,
3893
-				Price:                   warehouse.RetailPrice,
3894
-				CountUnit:               drup.MinUnit,
3895
-				RetailPrice:             warehouse.RetailPrice,
3896
-				ProductDate:             warehouse.ProductDate,
3897
-				ExpiryDate:              warehouse.ExpiryDate,
3898
-				PatientId:               advice.PatientId,
3899
-				Count:                   deliver_number, //出最小单位
3900
-				WarehouseInfoId:         warehouse.ID,
3901
-				SupplyWarehouseId:       warehouse.SupplyWarehouseId,
3902
-				StorehouseId:            storeConfig.DrugStorehouseOut,
3903
-				IsCheck:                 1,
3904
-				OverCount:               sum_count,
3905
-				AdviceId:                advice.ID,
3906
-			}
3870
+		warehouseOutInfo := &models.DrugWarehouseOutInfo{
3871
+			WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
3872
+			WarehouseOutId:          warehouseout.ID,
3873
+			Status:                  1,
3874
+			Ctime:                   advice.RecordDate,
3875
+			Mtime:                   time.Now().Unix(),
3876
+			Remark:                  warehouse.Remark,
3877
+			OrgId:                   orgID,
3878
+			Type:                    1,
3879
+			Manufacturer:            warehouse.Manufacturer,
3880
+			Dealer:                  warehouse.Dealer,
3881
+			IsSys:                   1,
3882
+			SysRecordTime:           advice.RecordDate,
3883
+			DrugId:                  advice.DrugId,
3884
+			Number:                  warehouse.Number,
3885
+			BatchNumber:             warehouse.BatchNumber,
3886
+			Price:                   warehouse.RetailPrice,
3887
+			CountUnit:               drup.MinUnit,
3888
+			RetailPrice:             warehouse.RetailPrice,
3889
+			ProductDate:             warehouse.ProductDate,
3890
+			ExpiryDate:              warehouse.ExpiryDate,
3891
+			PatientId:               advice.PatientId,
3892
+			Count:                   deliver_number, //出最小单位
3893
+			WarehouseInfoId:         warehouse.ID,
3894
+			SupplyWarehouseId:       warehouse.SupplyWarehouseId,
3895
+			StorehouseId:            storeConfig.DrugStorehouseOut,
3896
+			IsCheck:                 1,
3897
+			OverCount:               sum_count,
3898
+			AdviceId:                advice.ID,
3899
+		}
3907 3900
 
3908
-			//查询今日该药品该患者是否有出库数据
3909
-			lastDrugOutInfo, _ := GetNewNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
3901
+		//查询今日该药品该患者是否有出库数据
3902
+		lastDrugOutInfo, _ := GetNewNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
3910 3903
 
3911
-			if lastDrugOutInfo.ID == 0 {
3912
-				//删除在新增
3913
-				UpdateNewDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
3914
-				AddNewSigleDrugWarehouseOutInfo(warehouseOutInfo, tx)
3915
-			}
3916
-			if lastDrugOutInfo.ID > 0 {
3904
+		if lastDrugOutInfo.ID == 0 {
3905
+			//删除在新增
3906
+			UpdateNewDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
3907
+			AddNewSigleDrugWarehouseOutInfo(warehouseOutInfo, tx)
3908
+		}
3909
+		if lastDrugOutInfo.ID > 0 {
3917 3910
 
3918
-				//删除在新增
3919
-				UpdateNewDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
3911
+			//删除在新增
3912
+			UpdateNewDrugWarehouseOutInfoTen(lastDrugOutInfo.PatientId, lastDrugOutInfo.SysRecordTime, advice.ID, tx)
3920 3913
 
3921
-				AddNewSigleDrugWarehouseOutInfo(warehouseOutInfo, tx)
3914
+			AddNewSigleDrugWarehouseOutInfo(warehouseOutInfo, tx)
3922 3915
 
3923
-			}
3924 3916
 		}
3917
+
3925 3918
 		//
3926 3919
 		var out_count int64
3927 3920
 		var cancel_count int64
@@ -3938,44 +3931,18 @@ func NewBloodHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehous
3938 3931
 			cancel_count += item.Count
3939 3932
 		}
3940 3933
 
3941
-		if deliver_number_one-(out_count-cancel_count) >= 0 {
3942
-			if (deliver_number_one - out_count - cancel_count) != 0 {
3934
+		fmt.Println("已经出库数据", out_count)
3943 3935
 
3944
-				if orgID == 10217 || orgID == 10188 || orgID == 3877 {
3945
-					warehouseOutInfo := &models.DrugWarehouseOutInfo{
3946
-						WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
3947
-						WarehouseOutId:          warehouseout.ID,
3948
-						Status:                  1,
3949
-						Ctime:                   advice.RecordDate,
3950
-						Mtime:                   time.Now().Unix(),
3951
-						Remark:                  warehouse.Remark,
3952
-						OrgId:                   orgID,
3953
-						Type:                    1,
3954
-						Manufacturer:            warehouse.Manufacturer,
3955
-						Dealer:                  warehouse.Dealer,
3956
-						IsSys:                   1,
3957
-						SysRecordTime:           advice.RecordDate,
3958
-						DrugId:                  advice.DrugId,
3959
-						Number:                  warehouse.Number,
3960
-						BatchNumber:             warehouse.BatchNumber,
3961
-						Price:                   warehouse.RetailPrice,
3962
-						CountUnit:               drup.MinUnit,
3963
-						RetailPrice:             warehouse.RetailPrice,
3964
-						ProductDate:             warehouse.ProductDate,
3965
-						ExpiryDate:              warehouse.ExpiryDate,
3966
-						PatientId:               advice.PatientId,
3967
-						Count:                   deliver_number_one - (out_count - cancel_count), //出最小单位
3968
-						WarehouseInfoId:         warehouse.ID,
3969
-						SupplyWarehouseId:       warehouse.SupplyWarehouseId,
3970
-						StorehouseId:            storeConfig.DrugStorehouseOut,
3971
-						IsCheck:                 1,
3972
-						OverCount:               sum_count,
3973
-						AdviceId:                advice.ID,
3974
-					}
3936
+		fmt.Println("已经退库数据", cancel_count)
3975 3937
 
3976
-					AddNewSigleDrugWarehouseOutInfo(warehouseOutInfo, tx)
3938
+		fmt.Println("当前正在出库的数据", deliver_number_one)
3939
+
3940
+		fmt.Println("差", deliver_number_one-out_count-cancel_count)
3941
+
3942
+		//如果本次出库数据大于历史出库数据 新增1条退库流水
3943
+		if deliver_number_one-(out_count-cancel_count) >= 0 {
3944
+			if (deliver_number_one - out_count - cancel_count) != 0 {
3977 3945
 
3978
-				}
3979 3946
 				lastDrugOutInfo, _ := GetNewNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
3980 3947
 
3981 3948
 				drugflow := models.DrugFlow{
@@ -4143,8 +4110,6 @@ func NewBloodHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehous
4143 4110
 			StorehouseId:            storeConfig.DrugStorehouseOut,
4144 4111
 			AdviceId:                advice.ID,
4145 4112
 		}
4146
-
4147
-		fmt.Println("天激发阿凡达阿道夫阿道夫阿道夫阿道夫阿道夫阿道夫阿道夫阿道夫阿道夫")
4148 4113
 		AddNewSigleDrugAutoReduceRecordInfo(details, tx)
4149 4114
 
4150 4115
 		return nil
@@ -4477,3 +4442,792 @@ func NewBloodHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehous
4477 4442
 
4478 4443
 	return
4479 4444
 }
4445
+
4446
+func HisStoryBloodHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.HisDoctorAdviceInfo, hisprescribingNumber float64, unit string, tx *gorm.DB) (err error) {
4447
+
4448
+	// 判断处方里药品单位是拆零单位还是包装单位,	如果是包装单位,则根据规格,将包装数量转为拆零数量
4449
+	var deliver_number int64 = 0
4450
+
4451
+	var stock_number int64 = 0
4452
+
4453
+	prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
4454
+	count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
4455
+
4456
+	if advice.PrescribingNumberUnit == drup.MaxUnit {
4457
+		deliver_number = count * drup.MinNumber
4458
+
4459
+	} else {
4460
+		deliver_number = count
4461
+
4462
+	}
4463
+
4464
+	// 根据先进先出原则,查询最先入库的批次,进行出库
4465
+	// 如果没有对应的库存,则报错
4466
+	storeConfig, _ := GetNewAllStoreHouseConfig(advice.UserOrgId, tx)
4467
+	lastWarehouse, _ := FindNewLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut, tx)
4468
+
4469
+	if lastWarehouse.MaxUnit != lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
4470
+		if lastWarehouse.StockMinNumber >= drup.MinNumber {
4471
+			var stockMax int64
4472
+			var stockMin int64
4473
+			stockMax = lastWarehouse.StockMinNumber / drup.MinNumber
4474
+			stockMin = lastWarehouse.StockMinNumber % drup.MinNumber
4475
+
4476
+			ChangeNewMaxNumber(lastWarehouse.ID, stockMax, tx)
4477
+			UpdateNewMinNumber(lastWarehouse.ID, stockMin, tx)
4478
+
4479
+		}
4480
+	}
4481
+
4482
+	if lastWarehouse.MaxUnit == lastWarehouse.MinUnit && drup.MaxUnit != drup.MinUnit && lastWarehouse.MinUnit != "" {
4483
+
4484
+		lastWarehouse.StockMaxNumber = lastWarehouse.StockMaxNumber + lastWarehouse.StockMinNumber
4485
+		ChangeNewMaxNumberOne(lastWarehouse.ID, lastWarehouse.StockMaxNumber, tx)
4486
+
4487
+	}
4488
+
4489
+	warehouse, _ := FindNewLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut, tx)
4490
+
4491
+	// 将该批次的剩余库存数量转换为拆零数量
4492
+	if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit != drup.MinUnit {
4493
+
4494
+		stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
4495
+
4496
+	}
4497
+	if warehouse.MaxUnit == drup.MinUnit && drup.MaxUnit != drup.MinUnit {
4498
+
4499
+		stock_number = warehouse.StockMaxNumber + warehouse.StockMinNumber
4500
+
4501
+	}
4502
+	if warehouse.MaxUnit == drup.MaxUnit && drup.MaxUnit == drup.MinUnit {
4503
+
4504
+		stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
4505
+
4506
+	}
4507
+
4508
+	//fmt.Println("stock_number", stock_number)
4509
+	//fmt.Println("deliver_number=============", deliver_number)
4510
+	// 当库存数量大于或等于出库数量的话,则正常出库该批次
4511
+	if stock_number >= deliver_number {
4512
+
4513
+		var maxNumber int64 = 0
4514
+		var minNumber int64 = 0
4515
+
4516
+		maxNumber = deliver_number / drup.MinNumber
4517
+		minNumber = deliver_number % drup.MinNumber
4518
+
4519
+		if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
4520
+			minNumber = maxNumber
4521
+		}
4522
+
4523
+		if drup.MaxUnit != drup.MinUnit {
4524
+			if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
4525
+				return errors.New("库存数量不足")
4526
+			}
4527
+		}
4528
+
4529
+		if drup.MinUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
4530
+
4531
+			warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber*drup.MinNumber
4532
+		}
4533
+
4534
+		if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit != drup.MinUnit {
4535
+
4536
+			warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
4537
+		}
4538
+		if drup.MaxUnit == warehouse.MaxUnit && drup.MaxUnit == drup.MinUnit {
4539
+
4540
+			warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
4541
+		}
4542
+
4543
+		if warehouse.StockMaxNumber < 0 {
4544
+			warehouse.StockMaxNumber = 0
4545
+		}
4546
+		if warehouse.StockMinNumber < 0 {
4547
+			warehouse.StockMinNumber = 0
4548
+		}
4549
+
4550
+		warehouse.Mtime = time.Now().Unix()
4551
+
4552
+		//fmt.Println("hahhahahah", warehouse.StockMinNumber)
4553
+		//fmt.Println("howowowoowow", minNumber)
4554
+		if warehouse.StockMinNumber < minNumber {
4555
+
4556
+			warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
4557
+			if warehouse.MaxUnit != warehouse.MinUnit {
4558
+				warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
4559
+			}
4560
+			if minNumber == 2 && warehouse.MaxUnit == warehouse.MinUnit && warehouse.StockMinNumber == 0 {
4561
+				warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
4562
+			}
4563
+		} else {
4564
+
4565
+			if minNumber > 0 {
4566
+				if (warehouse.StockMinNumber - minNumber) >= 0 {
4567
+					warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
4568
+				}
4569
+
4570
+			}
4571
+
4572
+			if minNumber == 0 && maxNumber != 1 && warehouse.StockMaxNumber <= 0 {
4573
+
4574
+				if warehouse.StockMinNumber > 0 {
4575
+					if warehouse.StockMinNumber-deliver_number >= 0 {
4576
+						warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
4577
+					}
4578
+
4579
+				}
4580
+
4581
+			}
4582
+
4583
+		}
4584
+
4585
+		//fmt.Println("maxNumber", maxNumber)
4586
+		//fmt.Println("minNumber", maxNumber)
4587
+		if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit && warehouse.MaxUnit != warehouse.MinUnit {
4588
+			if (warehouse.StockMinNumber - deliver_number) >= 0 {
4589
+
4590
+				warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
4591
+			}
4592
+
4593
+		}
4594
+
4595
+		if drup.MaxUnit != drup.MinUnit {
4596
+			if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
4597
+				return errors.New("库存数量不足")
4598
+			}
4599
+		}
4600
+		if warehouse.StockMinNumber <= 0 {
4601
+			warehouse.StockMinNumber = 0
4602
+		}
4603
+
4604
+		//fmt.Println("warehouse.StockMaxNumber-------------------", warehouse.StockMaxNumber)
4605
+		//
4606
+		//fmt.Println("warehouse.StockMinNumber-------------------", warehouse.StockMinNumber)
4607
+
4608
+		//扣减库存数据
4609
+		UpDateNewDrugWarehouseInfoByStock(&warehouse, tx)
4610
+
4611
+		////查询剩余库存
4612
+		stockInfo, _ := GetNewDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId, tx)
4613
+		var sum_count int64
4614
+		for _, its := range stockInfo {
4615
+			if its.MaxUnit == drup.MaxUnit {
4616
+				its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
4617
+			}
4618
+			sum_count += its.StockMaxNumber + its.StockMinNumber
4619
+		}
4620
+
4621
+		warehouseOutInfo := &models.DrugWarehouseOutInfo{
4622
+			WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
4623
+			WarehouseOutId:          warehouseout.ID,
4624
+			Status:                  1,
4625
+			Ctime:                   advice.RecordDate,
4626
+			Mtime:                   time.Now().Unix(),
4627
+			Remark:                  warehouse.Remark,
4628
+			OrgId:                   orgID,
4629
+			Type:                    1,
4630
+			Manufacturer:            warehouse.Manufacturer,
4631
+			Dealer:                  warehouse.Dealer,
4632
+			IsSys:                   1,
4633
+			SysRecordTime:           advice.RecordDate,
4634
+			DrugId:                  advice.DrugId,
4635
+			Number:                  warehouse.Number,
4636
+			BatchNumber:             warehouse.BatchNumber,
4637
+			Price:                   warehouse.RetailPrice,
4638
+			CountUnit:               drup.MinUnit,
4639
+			RetailPrice:             warehouse.RetailPrice,
4640
+			ProductDate:             warehouse.ProductDate,
4641
+			ExpiryDate:              warehouse.ExpiryDate,
4642
+			PatientId:               advice.PatientId,
4643
+			Count:                   deliver_number, //出最小单位
4644
+			WarehouseInfoId:         warehouse.ID,
4645
+			SupplyWarehouseId:       warehouse.SupplyWarehouseId,
4646
+			StorehouseId:            storeConfig.DrugStorehouseOut,
4647
+			IsCheck:                 1,
4648
+			OverCount:               sum_count,
4649
+			AdviceId:                advice.ID,
4650
+		}
4651
+
4652
+		//查询今日该药品该患者是否有出库数据
4653
+		lastDrugOutInfo, _ := GetNewNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
4654
+
4655
+		if lastDrugOutInfo.ID == 0 {
4656
+			AddNewSigleDrugWarehouseOutInfo(warehouseOutInfo, tx)
4657
+		}
4658
+		//如果有数量则新增数量
4659
+		if lastDrugOutInfo.ID > 0 {
4660
+
4661
+			UpdateSigleDrugWarehouseOutInfoDetail(lastDrugOutInfo.ID, warehouseOutInfo.Count, tx)
4662
+
4663
+		}
4664
+
4665
+		drugflow := models.DrugFlow{
4666
+			WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
4667
+			WarehouseOutId:          warehouseout.ID,
4668
+			DrugId:                  advice.DrugId,
4669
+			Number:                  warehouse.Number,
4670
+			ProductDate:             warehouse.ProductDate,
4671
+			ExpireDate:              warehouse.ExpiryDate,
4672
+			Count:                   deliver_number, //按最小单位计算,
4673
+			Price:                   warehouse.RetailPrice,
4674
+			Status:                  1,
4675
+			Ctime:                   advice.AdviceDate,
4676
+			Mtime:                   time.Now().Unix(),
4677
+			UserOrgId:               orgID,
4678
+			Manufacturer:            warehouse.Manufacturer,
4679
+			Dealer:                  warehouse.Dealer,
4680
+			BatchNumber:             warehouse.BatchNumber,
4681
+			MaxUnit:                 drup.MinUnit,
4682
+			ConsumableType:          3,
4683
+			IsEdit:                  1,
4684
+			Creator:                 advice.ExecutionStaff,
4685
+			IsSys:                   1,
4686
+			PatientId:               advice.PatientId,
4687
+			SystemTime:              advice.AdviceDate,
4688
+			WarehousingDetailId:     warehouse.ID,
4689
+			SupplyWarehouseId:       warehouse.SupplyWarehouseId,
4690
+			StorehouseId:            storeConfig.DrugStorehouseOut,
4691
+			WarehouseOutDetailId:    lastDrugOutInfo.ID,
4692
+			OverCount:               sum_count,
4693
+			AdviceId:                advice.ID,
4694
+			LastPrice:               warehouse.Price,
4695
+		}
4696
+
4697
+		CreateNewDrugFlowOne(drugflow, tx)
4698
+
4699
+		AddNewDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count, tx)
4700
+
4701
+		//查询是否存在数据
4702
+		details := &models.DrugAutomaticReduceDetail{
4703
+			WarehouseOutId:          warehouseout.ID,
4704
+			WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
4705
+			PatientId:               advice.PatientId,
4706
+			Ctime:                   advice.RecordDate,
4707
+			Mtime:                   time.Now().Unix(),
4708
+			Status:                  1,
4709
+			RecordTime:              advice.RecordDate,
4710
+			OrgId:                   orgID,
4711
+			DrugId:                  advice.DrugId,
4712
+			Count:                   deliver_number,
4713
+			CountUnit:               drup.MinUnit,
4714
+			WarehouseInfoId:         warehouse.ID,
4715
+			StorehouseId:            storeConfig.DrugStorehouseOut,
4716
+			AdviceId:                advice.ID,
4717
+		}
4718
+
4719
+		//查找是否有数据
4720
+		autoMaticReduceDetail, _ := GetDrugAutoMaticReduceDetail(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
4721
+
4722
+		if autoMaticReduceDetail.ID == 0 {
4723
+			AddNewSigleDrugAutoReduceRecordInfo(details, tx)
4724
+		}
4725
+
4726
+		if autoMaticReduceDetail.ID > 0 {
4727
+			UpdateNewSingleDrugAutoReduceRecordInfo(autoMaticReduceDetail.ID, deliver_number, tx)
4728
+		}
4729
+
4730
+		return nil
4731
+
4732
+	} else {
4733
+
4734
+		info := models.XtDrugWarehouseInfo{
4735
+			ID:                warehouse.ID,
4736
+			WarehousingId:     warehouse.WarehousingId,
4737
+			DrugId:            warehouse.DrugId,
4738
+			Number:            warehouse.Number,
4739
+			ProductDate:       warehouse.ProductDate,
4740
+			ExpiryDate:        warehouse.ExpiryDate,
4741
+			WarehousingCount:  warehouse.WarehousingCount,
4742
+			Price:             warehouse.RetailPrice,
4743
+			TotalPrice:        warehouse.TotalPrice,
4744
+			Dealer:            warehouse.Dealer,
4745
+			Manufacturer:      warehouse.Manufacturer,
4746
+			Remark:            warehouse.Remark,
4747
+			Ctime:             warehouse.Ctime,
4748
+			Mtime:             warehouse.Mtime,
4749
+			Status:            1,
4750
+			OrgId:             warehouse.OrgId,
4751
+			IsReturn:          warehouse.IsReturn,
4752
+			WarehousingOrder:  warehouse.WarehousingOrder,
4753
+			Type:              warehouse.Type,
4754
+			RetailPrice:       warehouse.RetailPrice,
4755
+			RetailTotalPrice:  warehouse.RetailPrice,
4756
+			StockMaxNumber:    0,
4757
+			StockMinNumber:    0,
4758
+			BatchNumber:       warehouse.BatchNumber,
4759
+			MaxUnit:           warehouse.MaxUnit,
4760
+			MinUnit:           drup.MinUnit,
4761
+			WarehousingInfoId: warehouse.WarehousingInfoId,
4762
+			SupplyWarehouseId: warehouse.SupplyWarehouseId,
4763
+			StorehouseId:      storeConfig.DrugStorehouseOut,
4764
+			IsCheck:           1,
4765
+		}
4766
+		//扣减库存
4767
+		UpDateNewDrugWarehouseInfoByStock(&info, tx)
4768
+
4769
+		//查询剩余库存
4770
+		stockInfo, _ := GetNewDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId, tx)
4771
+		var sum_count int64
4772
+		for _, its := range stockInfo {
4773
+			if its.MaxUnit == drup.MaxUnit {
4774
+				its.StockMaxNumber = its.StockMaxNumber * drup.MinNumber
4775
+			}
4776
+			sum_count += its.StockMaxNumber + its.StockMinNumber
4777
+		}
4778
+
4779
+		// 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
4780
+		warehouseOutInfo := &models.DrugWarehouseOutInfo{
4781
+			WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
4782
+			WarehouseOutId:          warehouseout.ID,
4783
+			Status:                  1,
4784
+			Ctime:                   advice.RecordDate,
4785
+			Mtime:                   time.Now().Unix(),
4786
+			Remark:                  warehouse.Remark,
4787
+			OrgId:                   orgID,
4788
+			Type:                    1,
4789
+			Manufacturer:            warehouse.Manufacturer,
4790
+			Dealer:                  warehouse.Dealer,
4791
+			IsSys:                   1,
4792
+			SysRecordTime:           advice.RecordDate,
4793
+			DrugId:                  advice.DrugId,
4794
+			Number:                  warehouse.Number,
4795
+			BatchNumber:             warehouse.BatchNumber,
4796
+			Price:                   warehouse.RetailPrice,
4797
+			CountUnit:               drup.MinUnit,
4798
+			ProductDate:             warehouse.ProductDate,
4799
+			ExpiryDate:              warehouse.ExpiryDate,
4800
+			PatientId:               advice.PatientId,
4801
+			Count:                   stock_number,
4802
+			WarehouseInfoId:         warehouse.ID,
4803
+			SupplyWarehouseId:       warehouse.SupplyWarehouseId,
4804
+			StorehouseId:            storeConfig.DrugStorehouseOut,
4805
+			IsCheck:                 1,
4806
+			OverCount:               sum_count,
4807
+			AdviceId:                advice.ID,
4808
+		}
4809
+
4810
+		//查询今日该药品该患者是否有出库数据
4811
+		lastDrugOutInfo, _ := GetNewNewDrugWarehouseOutInfoSix(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
4812
+
4813
+		if lastDrugOutInfo.ID == 0 {
4814
+			AddNewSigleDrugWarehouseOutInfo(warehouseOutInfo, tx)
4815
+		}
4816
+		//如果有数量则新增数量
4817
+		if lastDrugOutInfo.ID > 0 {
4818
+
4819
+			UpdateSigleDrugWarehouseOutInfoDetail(lastDrugOutInfo.ID, warehouseOutInfo.Count, tx)
4820
+
4821
+		}
4822
+
4823
+		//查询是否存在数据
4824
+		details := &models.DrugAutomaticReduceDetail{
4825
+			WarehouseOutId:          warehouseout.ID,
4826
+			WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
4827
+			PatientId:               advice.PatientId,
4828
+			Ctime:                   advice.RecordDate,
4829
+			Mtime:                   time.Now().Unix(),
4830
+			Status:                  1,
4831
+			RecordTime:              advice.RecordDate,
4832
+			OrgId:                   orgID,
4833
+			DrugId:                  advice.DrugId,
4834
+			Count:                   stock_number,
4835
+			CountUnit:               drup.MinUnit,
4836
+			WarehouseInfoId:         warehouse.ID,
4837
+			StorehouseId:            storeConfig.DrugStorehouseOut,
4838
+			AdviceId:                advice.ID,
4839
+		}
4840
+
4841
+		//查找是否有数据
4842
+		autoMaticReduceDetail, _ := GetDrugAutoMaticReduceDetail(advice.DrugId, advice.PatientId, advice.RecordDate, advice.UserOrgId, advice.ID, warehouse.ID, tx)
4843
+
4844
+		if autoMaticReduceDetail.ID == 0 {
4845
+			AddNewSigleDrugAutoReduceRecordInfo(details, tx)
4846
+		}
4847
+
4848
+		if autoMaticReduceDetail.ID > 0 {
4849
+			UpdateNewSingleDrugAutoReduceRecordInfo(autoMaticReduceDetail.ID, deliver_number, tx)
4850
+		}
4851
+
4852
+		drugflow := models.DrugFlow{
4853
+			WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
4854
+			WarehouseOutId:          warehouseout.ID,
4855
+			DrugId:                  advice.DrugId,
4856
+			Number:                  warehouse.Number,
4857
+			ProductDate:             warehouse.ProductDate,
4858
+			ExpireDate:              warehouse.ExpiryDate,
4859
+			Count:                   stock_number,
4860
+			Price:                   warehouse.RetailPrice,
4861
+			Status:                  1,
4862
+			Ctime:                   advice.AdviceDate,
4863
+			Mtime:                   time.Now().Unix(),
4864
+			UserOrgId:               orgID,
4865
+			Manufacturer:            warehouse.Manufacturer,
4866
+			Dealer:                  warehouse.Dealer,
4867
+			BatchNumber:             warehouse.BatchNumber,
4868
+			MaxUnit:                 drup.MinUnit,
4869
+			ConsumableType:          3,
4870
+			IsEdit:                  1,
4871
+			Creator:                 advice.ExecutionStaff,
4872
+			IsSys:                   1,
4873
+			PatientId:               advice.PatientId,
4874
+			WarehousingDetailId:     warehouse.ID,
4875
+			SupplyWarehouseId:       warehouse.SupplyWarehouseId,
4876
+			StorehouseId:            storeConfig.DrugStorehouseOut,
4877
+			WarehouseOutDetailId:    lastDrugOutInfo.ID,
4878
+			OverCount:               sum_count,
4879
+			AdviceId:                advice.ID,
4880
+			LastPrice:               warehouse.Price,
4881
+			SystemTime:              advice.AdviceDate,
4882
+		}
4883
+
4884
+		CreateNewDrugFlowOne(drugflow, tx)
4885
+		//出库数量相加
4886
+		AddNewDrugCount(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, drugflow.Count, tx)
4887
+
4888
+		// 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
4889
+		prescribingNumber_two_temp := deliver_number - stock_number
4890
+
4891
+		overPlusNumber := float64(prescribingNumber_two_temp)
4892
+
4893
+		advice.PrescribingNumber = float64(prescribingNumber_two_temp)
4894
+		advice.PrescribingNumberUnit = drup.MinUnit
4895
+
4896
+		HisStoryBloodHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, hisprescribingNumber, unit, tx)
4897
+	}
4898
+
4899
+	return
4900
+}
4901
+
4902
+func NewHisDrugCancelInfo(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, cha_count int64, drug models.BaseDrugLibSeven, tx *gorm.DB) (err error) {
4903
+
4904
+	storeConfig, _ := GetNewAllStoreHouseConfig(advice.UserOrgId, tx)
4905
+
4906
+	//查找患者最后一条的退库数据是否满足要求
4907
+	var out_count int64
4908
+	drugOutInfo, _ := GetDrugWarehouseOutInfoByAdviceIdTwo(advice.PatientId, advice.UserOrgId, advice.AdviceDate, advice.DrugId, advice.ID, tx)
4909
+
4910
+	if drugOutInfo.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
4911
+
4912
+		out_count = drugOutInfo.Count * drug.MinNumber
4913
+	}
4914
+
4915
+	if drugOutInfo.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
4916
+
4917
+		out_count = drugOutInfo.Count
4918
+	}
4919
+
4920
+	if drugOutInfo.CountUnit == drug.MinUnit && drug.MaxUnit == drug.MinUnit {
4921
+
4922
+		out_count = drugOutInfo.Count
4923
+	}
4924
+
4925
+	//如果最后1条出库数量大于退库数量,则直接退库,满足退库要求
4926
+	if out_count >= cha_count {
4927
+		//退库 退回 cha_count 的数量
4928
+		ModifyNewDrugWarehouseInfoStockMinNumber(cha_count, drugOutInfo.DrugId, drugOutInfo.OrgId, drugOutInfo.WarehouseInfoId, tx)
4929
+
4930
+		if out_count == cha_count {
4931
+			//删除该出库单
4932
+			DeleteDrugWarehouseOutInfoByNumber(drugOutInfo.ID, tx)
4933
+		}
4934
+
4935
+		//减去出库单里面的数量
4936
+		ModifyDrugWarehouseOutInfoByNumber(drugOutInfo.ID, cha_count, tx)
4937
+
4938
+		//创建退库单
4939
+		//查询剩余库存
4940
+		stockInfo, _ := GetNewDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId, tx)
4941
+		var sum_count int64
4942
+		for _, its := range stockInfo {
4943
+			if its.MaxUnit == drug.MaxUnit {
4944
+				its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
4945
+			}
4946
+			sum_count += its.StockMaxNumber + its.StockMinNumber
4947
+		}
4948
+
4949
+		//创建退库单
4950
+		operation_time := time.Now().Unix()
4951
+		ctime := time.Now().Unix()
4952
+		timeStr := time.Now().Format("2006-01-02")
4953
+		timeArr := strings.Split(timeStr, "-")
4954
+		total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
4955
+		total = total + 1
4956
+		orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
4957
+		cancelStock := models.DrugCancelStock{
4958
+			OrderNumber:  orderNumber,
4959
+			OperaTime:    operation_time,
4960
+			OrgId:        advice.UserOrgId,
4961
+			Creater:      advice.ExecutionStaff,
4962
+			Ctime:        ctime,
4963
+			Status:       1,
4964
+			ReturnTime:   advice.AdviceDate,
4965
+			Dealer:       0,
4966
+			Manufacturer: 0,
4967
+			Type:         1,
4968
+			StorehouseId: storeConfig.DrugStorehouseOut,
4969
+			IsCheck:      1,
4970
+		}
4971
+
4972
+		_, errCode := FindNewDrugCancelStock(advice.AdviceDate, advice.UserOrgId, tx)
4973
+		if errCode == gorm.ErrRecordNotFound {
4974
+			AddNewSigleDrugCancelStock(&cancelStock, tx)
4975
+		}
4976
+		lastDrugCancelStock, _ := FindNewLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId, tx)
4977
+
4978
+		manufactureName, _ := GetNewManufactureById(drugOutInfo.Manufacturer, tx)
4979
+		dealer, _ := GetNewDealerById(drugOutInfo.Dealer, tx)
4980
+		cancelInfo, _ := GetNewLastDrugCancelStockById(advice.UserOrgId, tx)
4981
+		cancelStockInfo := models.DrugCancelStockInfo{
4982
+			OrderNumber:      cancelInfo.OrderNumber,
4983
+			CancelStockId:    lastDrugCancelStock.ID,
4984
+			DrugId:           advice.DrugId,
4985
+			Count:            cha_count,
4986
+			Status:           1,
4987
+			Ctime:            advice.AdviceDate,
4988
+			Mtime:            time.Now().Unix(),
4989
+			OrgId:            advice.UserOrgId,
4990
+			Type:             1,
4991
+			Manufacturer:     manufactureName.ManufacturerName,
4992
+			Dealer:           dealer.DealerName,
4993
+			Total:            0,
4994
+			RetailPrice:      drugOutInfo.RetailPrice,
4995
+			RetailTotalPrice: drugOutInfo.RetailTotalPrice,
4996
+			Price:            drugOutInfo.Price,
4997
+			RegisterAccount:  "",
4998
+			Remark:           drugOutInfo.Remark,
4999
+			BatchNumber:      drugOutInfo.BatchNumber,
5000
+			MaxUnit:          drugOutInfo.CountUnit,
5001
+			ProductDate:      drugOutInfo.ProductDate,
5002
+			ExpiryDate:       drugOutInfo.ExpiryDate,
5003
+			BatchNumberId:    drugOutInfo.WarehouseInfoId,
5004
+			StorehouseId:     storeConfig.DrugStorehouseOut,
5005
+			IsCheck:          1,
5006
+		}
5007
+		CreatedNewCancelStock(cancelStockInfo, tx)
5008
+
5009
+		flow := models.DrugFlow{
5010
+			WarehousingId:           drugOutInfo.WarehouseInfoId,
5011
+			DrugId:                  drugOutInfo.DrugId,
5012
+			Number:                  "",
5013
+			BatchNumber:             drugOutInfo.BatchNumber,
5014
+			Count:                   cha_count,
5015
+			UserOrgId:               advice.UserOrgId,
5016
+			PatientId:               advice.PatientId,
5017
+			SystemTime:              advice.AdviceDate,
5018
+			ConsumableType:          7,
5019
+			IsSys:                   0,
5020
+			WarehousingOrder:        "",
5021
+			WarehouseOutId:          0,
5022
+			WarehouseOutOrderNumber: "",
5023
+			IsEdit:                  0,
5024
+			CancelStockId:           cancelStock.ID,
5025
+			CancelOrderNumber:       cancelStock.OrderNumber,
5026
+			Manufacturer:            drugOutInfo.Manufacturer,
5027
+			Dealer:                  drugOutInfo.Dealer,
5028
+			Creator:                 advice.ExecutionStaff,
5029
+			UpdateCreator:           0,
5030
+			Status:                  1,
5031
+			Ctime:                   advice.AdviceDate,
5032
+			Mtime:                   time.Now().Unix(),
5033
+			Price:                   drugOutInfo.RetailPrice,
5034
+			WarehousingDetailId:     drugOutInfo.ID,
5035
+			WarehouseOutDetailId:    0,
5036
+			CancelOutDetailId:       lastDrugCancelStock.ID,
5037
+			ExpireDate:              drugOutInfo.ExpiryDate,
5038
+			ProductDate:             drugOutInfo.ProductDate,
5039
+			MaxUnit:                 drugOutInfo.CountUnit,
5040
+			MinUnit:                 "",
5041
+			AdviceId:                advice.ID,
5042
+			StorehouseId:            storeConfig.DrugStorehouseOut,
5043
+			LastPrice:               drugOutInfo.Price,
5044
+			OverCount:               sum_count,
5045
+		}
5046
+		CreateNewDrugFlowOne(flow, tx)
5047
+
5048
+		AddNewCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count, tx)
5049
+
5050
+		ReduceNewDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count, tx)
5051
+
5052
+	}
5053
+
5054
+	//如果出库的数据小于这个退库的数量差,则先该批次出库数量出完
5055
+	if out_count < cha_count {
5056
+
5057
+		//把该出库单的数量退完
5058
+		ModifyNewDrugWarehouseInfoStockMinNumber(out_count, drugOutInfo.DrugId, drugOutInfo.OrgId, drugOutInfo.WarehouseInfoId, tx)
5059
+
5060
+		//删除该出库单
5061
+		DeleteDrugWarehouseOutInfoByNumber(drugOutInfo.ID, tx)
5062
+
5063
+		//创建退库单
5064
+		//查询剩余库存
5065
+		stockInfo, _ := GetNewDrugAllStockInfo(storeConfig.DrugStorehouseOut, orgID, advice.DrugId, tx)
5066
+		var sum_count int64
5067
+		for _, its := range stockInfo {
5068
+			if its.MaxUnit == drug.MaxUnit {
5069
+				its.StockMaxNumber = its.StockMaxNumber * drug.MinNumber
5070
+			}
5071
+			sum_count += its.StockMaxNumber + its.StockMinNumber
5072
+		}
5073
+
5074
+		//创建退库单
5075
+		operation_time := time.Now().Unix()
5076
+		ctime := time.Now().Unix()
5077
+		timeStr := time.Now().Format("2006-01-02")
5078
+		timeArr := strings.Split(timeStr, "-")
5079
+		total, _ := FindAllDrugCancelStockTotal(advice.UserOrgId)
5080
+		total = total + 1
5081
+		orderNumber := "CKTKD" + strconv.FormatInt(advice.UserOrgId, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000" + strconv.FormatInt(total, 10)
5082
+		cancelStock := models.DrugCancelStock{
5083
+			OrderNumber:  orderNumber,
5084
+			OperaTime:    operation_time,
5085
+			OrgId:        advice.UserOrgId,
5086
+			Creater:      advice.ExecutionStaff,
5087
+			Ctime:        ctime,
5088
+			Status:       1,
5089
+			ReturnTime:   advice.AdviceDate,
5090
+			Dealer:       0,
5091
+			Manufacturer: 0,
5092
+			Type:         1,
5093
+			StorehouseId: storeConfig.DrugStorehouseOut,
5094
+			IsCheck:      1,
5095
+		}
5096
+
5097
+		_, errCode := FindNewDrugCancelStock(advice.AdviceDate, advice.UserOrgId, tx)
5098
+		if errCode == gorm.ErrRecordNotFound {
5099
+			AddNewSigleDrugCancelStock(&cancelStock, tx)
5100
+		}
5101
+		lastDrugCancelStock, _ := FindNewLastDrugCancelStock(advice.AdviceDate, advice.UserOrgId, tx)
5102
+
5103
+		manufactureName, _ := GetNewManufactureById(drugOutInfo.Manufacturer, tx)
5104
+		dealer, _ := GetNewDealerById(drugOutInfo.Dealer, tx)
5105
+		cancelInfo, _ := GetNewLastDrugCancelStockById(advice.UserOrgId, tx)
5106
+		cancelStockInfo := models.DrugCancelStockInfo{
5107
+			OrderNumber:      cancelInfo.OrderNumber,
5108
+			CancelStockId:    lastDrugCancelStock.ID,
5109
+			DrugId:           advice.DrugId,
5110
+			Count:            out_count,
5111
+			Status:           1,
5112
+			Ctime:            advice.AdviceDate,
5113
+			Mtime:            time.Now().Unix(),
5114
+			OrgId:            advice.UserOrgId,
5115
+			Type:             1,
5116
+			Manufacturer:     manufactureName.ManufacturerName,
5117
+			Dealer:           dealer.DealerName,
5118
+			Total:            0,
5119
+			RetailPrice:      drugOutInfo.RetailPrice,
5120
+			RetailTotalPrice: drugOutInfo.RetailTotalPrice,
5121
+			Price:            drugOutInfo.Price,
5122
+			RegisterAccount:  "",
5123
+			Remark:           drugOutInfo.Remark,
5124
+			BatchNumber:      drugOutInfo.BatchNumber,
5125
+			MaxUnit:          drugOutInfo.CountUnit,
5126
+			ProductDate:      drugOutInfo.ProductDate,
5127
+			ExpiryDate:       drugOutInfo.ExpiryDate,
5128
+			BatchNumberId:    drugOutInfo.WarehouseInfoId,
5129
+			StorehouseId:     storeConfig.DrugStorehouseOut,
5130
+			IsCheck:          1,
5131
+		}
5132
+		CreatedNewCancelStock(cancelStockInfo, tx)
5133
+
5134
+		flow := models.DrugFlow{
5135
+			WarehousingId:           drugOutInfo.WarehouseInfoId,
5136
+			DrugId:                  drugOutInfo.DrugId,
5137
+			Number:                  "",
5138
+			BatchNumber:             drugOutInfo.BatchNumber,
5139
+			Count:                   out_count,
5140
+			UserOrgId:               advice.UserOrgId,
5141
+			PatientId:               advice.PatientId,
5142
+			SystemTime:              advice.AdviceDate,
5143
+			ConsumableType:          7,
5144
+			IsSys:                   0,
5145
+			WarehousingOrder:        "",
5146
+			WarehouseOutId:          0,
5147
+			WarehouseOutOrderNumber: "",
5148
+			IsEdit:                  0,
5149
+			CancelStockId:           cancelStock.ID,
5150
+			CancelOrderNumber:       cancelStock.OrderNumber,
5151
+			Manufacturer:            drugOutInfo.Manufacturer,
5152
+			Dealer:                  drugOutInfo.Dealer,
5153
+			Creator:                 advice.ExecutionStaff,
5154
+			UpdateCreator:           0,
5155
+			Status:                  1,
5156
+			Ctime:                   advice.AdviceDate,
5157
+			Mtime:                   time.Now().Unix(),
5158
+			Price:                   drugOutInfo.RetailPrice,
5159
+			WarehousingDetailId:     drugOutInfo.ID,
5160
+			WarehouseOutDetailId:    0,
5161
+			CancelOutDetailId:       lastDrugCancelStock.ID,
5162
+			ExpireDate:              drugOutInfo.ExpiryDate,
5163
+			ProductDate:             drugOutInfo.ProductDate,
5164
+			MaxUnit:                 drugOutInfo.CountUnit,
5165
+			MinUnit:                 "",
5166
+			AdviceId:                advice.ID,
5167
+			StorehouseId:            storeConfig.DrugStorehouseOut,
5168
+			LastPrice:               drugOutInfo.Price,
5169
+			OverCount:               sum_count,
5170
+		}
5171
+		CreateNewDrugFlowOne(flow, tx)
5172
+
5173
+		AddNewCancelSumCountOne(storeConfig.DrugStorehouseOut, advice.DrugId, advice.UserOrgId, flow.Count, tx)
5174
+
5175
+		ReduceNewDrugCountTwo(advice.DrugId, orgID, storeConfig.DrugStorehouseOut, flow.Count, tx)
5176
+
5177
+		//计算还有多少没有退库
5178
+		var over_count = cha_count - out_count
5179
+
5180
+		NewHisDrugCancelInfo(orgID, creater, advice, over_count, drug, tx)
5181
+	}
5182
+	return err
5183
+
5184
+}
5185
+
5186
+func NewHisDrugAutoCancelInfo(orgID int64, creater int64, advice *models.HisDoctorAdviceInfo, cha_count int64, drug models.BaseDrugLibSeven, tx *gorm.DB) error {
5187
+
5188
+	//查找患者最后一条的退库数据是否满足要求
5189
+	var out_count int64
5190
+	drugOutInfo, _ := GetDrugAutoOutInfoByAdviceIdTwo(advice.PatientId, advice.UserOrgId, advice.AdviceDate, advice.DrugId, advice.ID, tx)
5191
+
5192
+	if drugOutInfo.CountUnit == drug.MaxUnit && drug.MaxUnit != drug.MinUnit {
5193
+
5194
+		out_count = drugOutInfo.Count * drug.MinNumber
5195
+	}
5196
+
5197
+	if drugOutInfo.CountUnit == drug.MinUnit && drug.MaxUnit != drug.MinUnit {
5198
+
5199
+		out_count = drugOutInfo.Count
5200
+	}
5201
+
5202
+	if drugOutInfo.CountUnit == drug.MinUnit && drug.MaxUnit == drug.MinUnit {
5203
+
5204
+		out_count = drugOutInfo.Count
5205
+	}
5206
+
5207
+	//fmt.Println("out_count++++==++", out_count)
5208
+	//fmt.Println("cha_count-------", cha_count)
5209
+
5210
+	//如果最后1条出库数量大于退库数量,则直接退库,满足退库要求
5211
+	if out_count >= cha_count {
5212
+		//减去出库单里面的数量
5213
+		ModifyDrugAuToWarehouseOut(drugOutInfo.ID, cha_count, tx)
5214
+
5215
+		if out_count == cha_count {
5216
+			//删除该出库单
5217
+			DeleteDrugWarehouseAutoByNumber(drugOutInfo.ID, tx)
5218
+		}
5219
+	}
5220
+
5221
+	//如果出库的数据小于这个退库的数量差,则先该批次出库数量出完
5222
+	if out_count < cha_count {
5223
+
5224
+		//删除该出库单
5225
+		DeleteDrugWarehouseAutoByNumber(drugOutInfo.ID, tx)
5226
+
5227
+		//计算还有多少没有退库
5228
+		var over_count = cha_count - out_count
5229
+
5230
+		NewHisDrugAutoCancelInfo(orgID, creater, advice, over_count, drug, tx)
5231
+	}
5232
+	return err
5233
+}

+ 39 - 0
service/stock_service.go 查看文件

@@ -3473,6 +3473,20 @@ func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm
3473 3473
 	}
3474 3474
 	return err
3475 3475
 }
3476
+
3477
+func UpdateSigleDrugWarehouseOutInfoDetail(id int64, count int64, tx *gorm.DB) error {
3478
+
3479
+	err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
3480
+
3481
+	err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("over_count", gorm.Expr("over_count - ?", count)).Error
3482
+	if err != nil {
3483
+		tx.Rollback()
3484
+		return err
3485
+	}
3486
+
3487
+	return err
3488
+}
3489
+
3476 3490
 func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
3477 3491
 	ut := writeDb.Begin()
3478 3492
 	err := ut.Create(&detail).Error
@@ -3490,6 +3504,31 @@ func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail)
3490 3504
 
3491 3505
 }
3492 3506
 
3507
+func GetDrugAutoMaticReduceDetail(drug_id int64, patient_id int64, record_time int64, org_id int64, adivce_id int64, warehouse_info_id int64, tx *gorm.DB) (models.DrugAutomaticReduceDetail, error) {
3508
+
3509
+	detail := models.DrugAutomaticReduceDetail{}
3510
+	err := tx.Where("drug_id = ? and patient_id =? and record_time =? and org_id = ? and advice_id = ? and warehouse_info_id =?", drug_id, patient_id, record_time, org_id, adivce_id, warehouse_info_id).Find(&detail).Error
3511
+
3512
+	if err != gorm.ErrRecordNotFound {
3513
+		if err != nil {
3514
+			tx.Rollback()
3515
+			return detail, err
3516
+		}
3517
+	}
3518
+	return detail, err
3519
+}
3520
+
3521
+func UpdateNewSingleDrugAutoReduceRecordInfo(id int64, count int64, tx *gorm.DB) error {
3522
+
3523
+	err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
3524
+
3525
+	if err != nil {
3526
+		tx.Rollback()
3527
+		return err
3528
+	}
3529
+	return err
3530
+}
3531
+
3493 3532
 func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
3494 3533
 
3495 3534
 	err := tx.Create(&detail).Error

+ 1 - 2
service/warhouse_service.go 查看文件

@@ -7889,8 +7889,7 @@ func NewHisDrugsDelivery(orgID int64, creater int64, advice *models.HisDoctorAdv
7889 7889
 		drup, _ := FindNewBaseDrugLibRecord(orgID, advice.DrugId, tx)
7890 7890
 		if drup.ID > 0 {
7891 7891
 			prescribingNumber := advice.PrescribingNumber
7892
-			fmt.Println(prescribingNumber)
7893
-			NewBloodHisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, prescribingNumber, advice.PrescribingNumberUnit, tx)
7892
+			HisStoryBloodHisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, prescribingNumber, advice.PrescribingNumberUnit, tx)
7894 7893
 
7895 7894
 		} else {
7896 7895
 			return errors.New("药品信息不存在")