From 155f70979af20ca520a55b89c6ec8cd46c43f8a5 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 31 七月 2024 15:47:33 +0800
Subject: [PATCH] 产品流水表增加多单位存储

---
 service/inventory_report_forms.go |  143 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 110 insertions(+), 33 deletions(-)

diff --git a/service/inventory_report_forms.go b/service/inventory_report_forms.go
index e83af14..5775cc8 100644
--- a/service/inventory_report_forms.go
+++ b/service/inventory_report_forms.go
@@ -87,7 +87,9 @@
 		data.Cost = material.Cost
 		data.Unit = material.Unit
 		data.Value = material.Amount.Mul(material.Cost)
-		data.ProductType = material.CategoryName
+		data.ProductCategory = material.CategoryName
+		data.ProductType = material.Type
+		data.ProductSpecs = material.Specs
 		for _, amount := range productAmounts {
 			if material.ID == amount.ProductId {
 				data.Amount = data.Amount.Add(amount.Amount)
@@ -109,6 +111,14 @@
 			}
 		}
 		data.AvailableNumber = data.Amount.Sub(available)
+
+		if *material.MoreUnit {
+			data.AmountMoreUnits = CreateMoreUnit(data.Amount, material.MoreUnitList)
+			data.AvailableNumberMoreUnits = CreateMoreUnit(data.AvailableNumber, material.MoreUnitList)
+			data.InMoreUnits = CreateMoreUnit(data.In, material.MoreUnitList)
+			data.OutMoreUnits = CreateMoreUnit(data.Out, material.MoreUnitList)
+		}
+
 		result = append(result, data)
 	}
 	return
@@ -121,29 +131,37 @@
 	locationIds []int,
 	err error) {
 	productAmounts = make([]*models.LocationProductAmount, 0)
-	if params.WarehouseCode != "" {
-		locations, err := models.NewLocationSearch().SetJointName(params.WarehouseCode).FindNotTotal()
-		if err != nil {
-			return nil, nil, nil, nil, err
-		}
+	locationSearch := models.NewLocationSearch()
+	//if params.WarehouseCode != "" {
+	//	locationSearch.SetJointName(params.WarehouseCode)
+	//}
+	if params.WarehouseId > 0 {
+		locationSearch.SetWarehouseId(params.WarehouseId)
+	}
 
-		for _, location := range locations {
-			locationIds = append(locationIds, location.Id)
-		}
-		productAmounts, err = models.NewLocationProductAmountSearch().SetLocationIds(locationIds).SetQuery("amount > 0").Find()
-		if err != nil {
-			return nil, nil, nil, nil, err
-		}
-		for _, amount := range productAmounts {
-			productIds = append(productIds, amount.ProductId)
-		}
+	locations, err := locationSearch.FindNotTotal()
+	if err != nil {
+		return nil, nil, nil, nil, err
+	}
+
+	for _, location := range locations {
+		locationIds = append(locationIds, location.Id)
+	}
+	productAmounts, err = models.NewLocationProductAmountSearch().
+		SetLocationIds(locationIds).SetQuery("amount > 0").
+		Find()
+	if err != nil {
+		return nil, nil, nil, nil, err
+	}
+	for _, amount := range productAmounts {
+		productIds = append(productIds, amount.ProductId)
 	}
 
 	//鏌ヨ浜у搧
 	search = models.NewMaterialSearch()
-	search.Orm = search.Orm.Model(&models.Material{}).
-		Select(`material.id, material.name, material.cost, material.amount, material.unit, wms_product_category.name as category_name `).
-		Joins("left join wms_product_category on material.category_id = wms_product_category.id")
+	search.Orm = search.Orm.Model(&models.Material{}). // material
+								Select(`material.id, material.name, material.cost, material.amount, material.unit, material.more_unit, material.more_unit_value, wms_product_category.name as category_name `).
+								Joins("left join wms_product_category on material.category_id = wms_product_category.id")
 	if len(params.CategoryIds) > 0 {
 		search.Orm.Where("material.category_id in (?)", params.CategoryIds)
 	}
@@ -195,26 +213,83 @@
 	var fileName string
 	f := excelize.NewFile()
 
+	unitData, unitIndexMap := GetAllUnits()
 	// 鑷畾涔夎〃澶�
-	headers := []string{"浜у搧", "浜у搧绫诲埆", "鍗曚綅鎴愭湰", "鎬讳环鍊�", "鍦ㄥ簱", "鍙敤搴撳瓨", "鍏ュ簱", "鍑哄簱", "鍗曚綅"}
-
+	headers := []string{"浜у搧", "浜у搧绫诲埆", "鍗曚綅鎴愭湰", "鎬讳环鍊�", "鍦ㄥ簱", "鍙敤搴撳瓨", "鍏ュ簱", "鍑哄簱"}
 	// 璁剧疆琛ㄥご
 	for i, header := range headers {
-		cell := getColumnAlphabet(i+1) + "1"
+		cell := getColumnAlphabet(i) + "1"
 		f.SetCellValue("Sheet1", cell, header)
 	}
 
+	f.SetCellValue("Sheet1", getColumnAlphabet(1)+"1", "浜у搧")
+	f.SetCellValue("Sheet1", getColumnAlphabet(2)+"1", "浜у搧绫诲埆")
+	f.SetCellValue("Sheet1", getColumnAlphabet(3)+"1", "鍗曚綅鎴愭湰")
+	f.SetCellValue("Sheet1", getColumnAlphabet(4)+"1", "鎬讳环鍊�")
+	f.SetCellValue("Sheet1", getColumnAlphabet(5)+"1", "鍦ㄥ簱")
+	f.MergeCell("Sheet1", getColumnAlphabet(5)+"1", getColumnAlphabet(7)+"1") // 鍚堝苟鍗曞厓鏍�
+	f.SetCellValue("Sheet1", getColumnAlphabet(8)+"1", "鍙敤搴撳瓨")
+	f.MergeCell("Sheet1", getColumnAlphabet(8)+"1", getColumnAlphabet(11)+"1") // 鍚堝苟鍗曞厓鏍�
+	f.SetCellValue("Sheet1", getColumnAlphabet(12)+"1", "鍏ュ簱")
+	f.MergeCell("Sheet1", getColumnAlphabet(12)+"1", getColumnAlphabet(15)+"1") // 鍚堝苟鍗曞厓鏍�
+	f.SetCellValue("Sheet1", getColumnAlphabet(16)+"1", "鍑哄簱")
+	f.MergeCell("Sheet1", getColumnAlphabet(16)+"1", getColumnAlphabet(19)+"1") // 鍚堝苟鍗曞厓鏍�
+
+	for i, header := range unitData {
+		f.SetCellValue("Sheet1", getColumnAlphabet(i+4)+"2", header)
+		f.SetCellValue("Sheet1", getColumnAlphabet(i+8)+"2", header)
+		f.SetCellValue("Sheet1", getColumnAlphabet(i+12)+"2", header)
+		f.SetCellValue("Sheet1", getColumnAlphabet(i+16)+"2", header)
+	}
+
+	// 璁剧疆琛ㄥご鏍峰紡
+	style := &excelize.Style{
+		Border: nil,
+		Fill: excelize.Fill{
+			Type:    "pattern",
+			Pattern: 1,
+			Shading: 0,
+		},
+		Font: &excelize.Font{
+			Bold: true,
+		},
+		Alignment: &excelize.Alignment{
+			Horizontal: "center",
+		},
+	}
+	titleStyle, err := f.NewStyle(style)
+	if err != nil {
+		return
+	}
+
+	lastColumn := getColumnAlphabet(19)
+	f.SetCellStyle("Sheet1", "A1", lastColumn+"2", titleStyle)
+	// 璁剧疆鍒楀
+	f.SetColWidth("Sheet1", "A", "D", 30)
+	f.SetColWidth("Sheet1", "E", lastColumn, 15)
+
 	for i, v := range dataList {
-		column := strconv.Itoa(i + 2)
-		f.SetCellValue("Sheet1", "A"+column, v.ProductName)
-		f.SetCellValue("Sheet1", "B"+column, v.ProductType)
-		f.SetCellValue("Sheet1", "C"+column, v.Cost)
-		f.SetCellValue("Sheet1", "D"+column, v.Value)
-		f.SetCellValue("Sheet1", "E"+column, v.Amount)
-		f.SetCellValue("Sheet1", "F"+column, v.AvailableNumber)
-		f.SetCellValue("Sheet1", "G"+column, v.In)
-		f.SetCellValue("Sheet1", "H"+column, v.Out)
-		f.SetCellValue("Sheet1", "I"+column, v.Unit)
+		column := strconv.Itoa(i + 3)
+		f.SetCellValue("Sheet1", getColumnAlphabet(0)+column, v.ProductName)
+		f.SetCellValue("Sheet1", getColumnAlphabet(1)+column, v.ProductType)
+		f.SetCellValue("Sheet1", getColumnAlphabet(2)+column, v.Cost)
+		f.SetCellValue("Sheet1", getColumnAlphabet(3)+column, v.Value)
+
+		f.SetCellValue("Sheet1", getColumnAlphabet(4)+column, v.Amount)
+		v.AmountMoreUnits = append(v.AmountMoreUnits, models.UnitItems{Amount: v.Amount, Unit: v.Unit})
+		FillMoreUnitToExcel(v.AmountMoreUnits, 5, i+3, unitIndexMap, f)
+
+		f.SetCellValue("Sheet1", getColumnAlphabet(8)+column, v.AvailableNumber)
+		v.AvailableNumberMoreUnits = append(v.AvailableNumberMoreUnits, models.UnitItems{Amount: v.AvailableNumber, Unit: v.Unit})
+		FillMoreUnitToExcel(v.AvailableNumberMoreUnits, 9, i+3, unitIndexMap, f)
+
+		f.SetCellValue("Sheet1", getColumnAlphabet(12)+column, v.In)
+		v.InMoreUnits = append(v.InMoreUnits, models.UnitItems{Amount: v.In, Unit: v.Unit})
+		FillMoreUnitToExcel(v.InMoreUnits, 13, i+3, unitIndexMap, f)
+
+		f.SetCellValue("Sheet1", getColumnAlphabet(16)+column, v.Out)
+		v.OutMoreUnits = append(v.OutMoreUnits, models.UnitItems{Amount: v.Out, Unit: v.Unit})
+		FillMoreUnitToExcel(v.OutMoreUnits, 17, i+3, unitIndexMap, f)
 	}
 
 	fileName = fmt.Sprintf("搴撳瓨鎶ヨ〃%s.xlsx", time.Now().Format("2006-01-02-1504"))
@@ -231,5 +306,7 @@
 	if index <= 26 {
 		return string(alphabet[index-1])
 	}
-	return getColumnAlphabet((index-1)/26) + getColumnAlphabet((index-1)%26+1)
+	quotient := (index - 1) / 26
+	remainder := (index - 1) % 26
+	return getColumnAlphabet(quotient) + getColumnAlphabet(remainder+1)
 }

--
Gitblit v1.8.0