From eb3a28fa790ee41bb3755b2b4aa789996c4365ca Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期一, 13 十一月 2023 10:30:38 +0800
Subject: [PATCH] 在库与可用查询修改

---
 controllers/operation.go               |    7 ++-
 controllers/report_forms_controller.go |   54 ++++++++++++++++++++++-----
 controllers/reorder_rule_controller.go |    4 +-
 proto/product_inventory/server.go      |    3 +
 4 files changed, 52 insertions(+), 16 deletions(-)

diff --git a/controllers/operation.go b/controllers/operation.go
index a541b4d..08031a9 100644
--- a/controllers/operation.go
+++ b/controllers/operation.go
@@ -1,6 +1,7 @@
 package controllers
 
 import (
+	"context"
 	"encoding/json"
 	"errors"
 	"fmt"
@@ -684,7 +685,7 @@
 		return
 	}
 	if operation.SourceNumber != "" {
-		go UpdateSalesDetailStatus(c, operation.SourceNumber)
+		go UpdateSalesDetailStatus(operation.SourceNumber)
 	}
 	util.ResponseFormat(c, code.Success, "鎿嶄綔鎴愬姛")
 }
@@ -706,9 +707,9 @@
 	}
 }
 
-func UpdateSalesDetailStatus(ctx *gin.Context, number string) {
+func UpdateSalesDetailStatus(number string) {
 	client := product_inventory.NewProductInventoryServiceClient(ProductInventoryServiceConn)
-	_, err := client.UpdateSalesDetailStatus(ctx, &product_inventory.UpdateSalesDetailStatusRequest{
+	_, err := client.UpdateSalesDetailStatus(context.Background(), &product_inventory.UpdateSalesDetailStatusRequest{
 		Number:            number,
 		SalesDetailStatus: "宸插嚭搴�",
 	})
diff --git a/controllers/reorder_rule_controller.go b/controllers/reorder_rule_controller.go
index bbb446c..a2b1dbe 100644
--- a/controllers/reorder_rule_controller.go
+++ b/controllers/reorder_rule_controller.go
@@ -119,7 +119,7 @@
 		rule.Prediction = rule.Amount.Add(rule.Prediction)
 	}
 	//鍑哄簱灏辩华
-	operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal}
+	operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal, constvar.BaseOperationTypeDisuse}
 	amount, err = GetProductAmount(productIds, nil, locationIds, status, operationType)
 	if err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "鏌ヨ閲嶈璐ц鍒欏垪琛ㄥけ璐�")
@@ -221,7 +221,7 @@
 	}
 	prediction = amount.Add(prediction)
 	//鍑哄簱灏辩华
-	operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal}
+	operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal, constvar.BaseOperationTypeDisuse}
 	list, err = GetProductAmount(productIds, nil, locationIds, status, operationType)
 	if err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "鏌ヨ閲嶈璐ц鍒欏垪琛ㄥけ璐�")
diff --git a/controllers/report_forms_controller.go b/controllers/report_forms_controller.go
index 7370006..c137692 100644
--- a/controllers/report_forms_controller.go
+++ b/controllers/report_forms_controller.go
@@ -3,6 +3,7 @@
 import (
 	"fmt"
 	"github.com/gin-gonic/gin"
+	"github.com/shopspring/decimal"
 	"wms/constvar"
 	"wms/extend/code"
 	"wms/extend/util"
@@ -12,6 +13,13 @@
 )
 
 type ReportFormsController struct {
+}
+
+type Detail struct {
+	ProductId string                   `json:"productId"`
+	Amount    decimal.Decimal          `json:"amount"`
+	Status    constvar.OperationStatus `json:"status"`
+	//ProductName string                   `json:"productName"`
 }
 
 // GetInventoryForms
@@ -54,7 +62,6 @@
 		return
 	}
 
-	//鏌ヨ鍑哄叆搴撴暟閲�
 	locations, err := models.NewLocationSearch().SetJointName(params.WarehouseCode).FindNotTotal()
 	if err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "鏌ヨ浠撳簱浣嶇疆澶辫触")
@@ -64,19 +71,37 @@
 	for _, location := range locations {
 		locationIds = append(locationIds, location.Id)
 	}
-	var inHouse []models.OperationDetails
-	var outHouse []models.OperationDetails
+	//鏌ヨ鍦ㄥ簱鏁伴噺
+	productIds := make([]string, 0)
+	for _, material := range materials {
+		productIds = append(productIds, material.ID)
+	}
+	productAmounts, err := models.NewLocationProductAmountSearch().SetProductIds(productIds).SetLocationIds(locationIds).Find()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏌ヨ鍦ㄥ簱鏁伴噺澶辫触")
+		return
+	}
+
+	//鏌ヨ鍑哄叆搴撳氨缁暟閲�
+	var inHouse []Detail
+	var outHouse []Detail
 	dbIn := models.NewOperationDetailsSearch().Orm.Model(&models.OperationDetails{}).
+		Select("wms_operation_details.product_id,wms_operation_details.amount,wms_operation.status as status").
 		Joins("left join wms_operation ON wms_operation_details.operation_id=wms_operation.id").
-		Where("wms_operation.base_operation_type=?", constvar.BaseOperationTypeIncoming).
-		Where("wms_operation.status = ?", constvar.OperationStatus_Finish)
+		Where("wms_operation.base_operation_type in (?)", []constvar.BaseOperationType{constvar.BaseOperationTypeIncoming, constvar.BaseOperationTypeInternal}).
+		Where("wms_operation.status in (?)", []constvar.OperationStatus{constvar.OperationStatus_Finish})
 	dbOut := models.NewOperationDetailsSearch().Orm.Model(&models.OperationDetails{}).
+		Select("wms_operation_details.product_id,wms_operation_details.amount,wms_operation.status as status").
 		Joins("left join wms_operation ON wms_operation_details.operation_id=wms_operation.id").
-		Where("wms_operation.base_operation_type in (?)", []int{2, 4}).
-		Where("wms_operation.status = ?", constvar.OperationStatus_Finish)
+		Where("wms_operation.base_operation_type in (?)", []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal, constvar.BaseOperationTypeDisuse}).
+		Where("wms_operation.status in (?)", []constvar.OperationStatus{constvar.OperationStatus_Ready, constvar.OperationStatus_Finish})
 	if len(locationIds) > 0 {
 		dbIn.Where("wms_operation.to_location_id in (?)", locationIds)
 		dbOut.Where("wms_operation.from_location_id in (?)", locationIds)
+	}
+	if len(productIds) > 0 {
+		dbIn.Where("wms_operation_details.product_id in (?)", productIds)
+		dbOut.Where("wms_operation_details.product_id in (?)", productIds)
 	}
 	err = dbIn.Find(&inHouse).Error
 	if err != nil {
@@ -97,18 +122,27 @@
 		resp.Unit = material.Unit
 		resp.Value = material.Amount.Mul(material.Cost)
 		resp.ProductType = material.CategoryName
+		for _, amount := range productAmounts {
+			if material.ID == amount.ProductId {
+				resp.Amount = resp.Amount.Add(amount.Amount)
+			}
+		}
 		for _, details := range inHouse {
 			if material.ID == details.ProductId {
 				resp.In = resp.In.Add(details.Amount)
 			}
 		}
+		available := decimal.NewFromInt(0)
 		for _, details := range outHouse {
 			if material.ID == details.ProductId {
-				resp.Out = resp.Out.Add(details.Amount)
+				if details.Status == constvar.OperationStatus_Ready {
+					available = available.Add(details.Amount)
+				} else {
+					resp.Out = resp.Out.Add(details.Amount)
+				}
 			}
 		}
-		resp.Amount = resp.In.Sub(resp.Out)
-		resp.AvailableNumber = resp.Amount
+		resp.AvailableNumber = resp.Amount.Sub(available)
 		result = append(result, resp)
 	}
 	util.ResponseFormatList(c, code.Success, result, int(total))
diff --git a/proto/product_inventory/server.go b/proto/product_inventory/server.go
index a4c77bf..5abf83f 100644
--- a/proto/product_inventory/server.go
+++ b/proto/product_inventory/server.go
@@ -93,7 +93,7 @@
 		Joins("left join wms_operation on wms_operation_details.operation_id = wms_operation.id").
 		Where("wms_operation_details.product_id in (?)", productIds).
 		Where("wms_operation.from_location_id in (?)", locationIds).Where("wms_operation.status = ?", constvar.OperationStatus_Ready).
-		Where("wms_operation.base_operation_type in (?)", []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal}).
+		Where("wms_operation.base_operation_type in (?)", []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal, constvar.BaseOperationTypeDisuse}).
 		Find(&canUse).Error
 	if err != nil {
 		return nil, err
@@ -129,6 +129,7 @@
 				cu = cu.Add(info.Amount)
 			}
 		}
+		cu = at.Sub(cu)
 		p.AvailableNumber = cu.String()
 
 		products = append(products, &p)

--
Gitblit v1.8.0