From 52177a1d16cf22a928936368cb3c6c4b46ad356e Mon Sep 17 00:00:00 2001
From: muzexing <muzexing@qq.com>
Date: 星期四, 04 七月 2024 20:39:03 +0800
Subject: [PATCH] 出库/入库打印 修改bug
---
service/warehouse_month_forms.go | 132 ++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 126 insertions(+), 6 deletions(-)
diff --git a/service/warehouse_month_forms.go b/service/warehouse_month_forms.go
index 1885a16..3a60cd3 100644
--- a/service/warehouse_month_forms.go
+++ b/service/warehouse_month_forms.go
@@ -1,13 +1,15 @@
package service
import (
+ "fmt"
"github.com/shopspring/decimal"
+ "github.com/xuri/excelize/v2"
+ "strconv"
"time"
"wms/constvar"
"wms/models"
"wms/pkg/logx"
"wms/request"
- "wms/utils"
)
type WarehouseMonthFormsService struct{}
@@ -24,6 +26,9 @@
func (slf *WarehouseMonthFormsService) BuildSearch(params request.GetMonthStats) (search *models.WarehouseMonthStatsSearch) {
search = models.NewWarehouseMonthStatsSearch().SetKeyword(params.Keyword).SetDate(params.Date)
+ if params.Preload {
+ search = search.SetPreload(true)
+ }
return search
}
@@ -55,6 +60,107 @@
return
}
+func (slf *WarehouseMonthFormsService) Export(dataList []*models.WarehouseMonthStats) (filename string, err error) {
+ // 鍒涘缓涓�涓柊鐨� Excel 鏂囦欢
+ f := excelize.NewFile()
+
+ if err != nil {
+ logx.Errorf("NewSheet err:%v", err)
+ return "", err
+ }
+
+ headers, headerLen, inputTypes, outputTypes := slf.GetHeaders()
+ err = SetExcelHeader(headers, f)
+ if err != nil {
+ logx.Errorf("SetExcelHeader err:%v", err)
+ return "", err
+ }
+
+ //琛ㄥご鏍峰紡
+ style, err := SetHeaderStyle(f)
+ if err != nil {
+ return "", err
+ }
+
+ f.SetCellStyle("Sheet1", "A1", getColumnAlphabet(headerLen)+"2", style)
+ // 璁剧疆鍒楀
+ f.SetColWidth("Sheet1", "A", "F", 30)
+ f.SetColWidth("Sheet1", "G", getColumnAlphabet(headerLen), 15)
+
+ inputStart := 6
+ outputStart := 6 + len(inputTypes)
+ for i, v := range dataList {
+ column := strconv.Itoa(i + 3)
+ f.SetCellValue("Sheet1", "A"+column, v.ProductId)
+ f.SetCellValue("Sheet1", "B"+column, v.ProductName)
+ f.SetCellValue("Sheet1", "C"+column, v.EndAmount)
+ f.SetCellValue("Sheet1", "D"+column, v.BeginAmount)
+ f.SetCellValue("Sheet1", "E"+column, v.Unit)
+ f.SetCellValue("Sheet1", "F"+column, v.SalePrice)
+
+ slf.FillDealerTypeToExcel(v.InputItems, inputStart, i+3, inputTypes, f)
+ slf.FillDealerTypeToExcel(v.OutputItems, outputStart, i+3, outputTypes, f)
+ }
+
+ fileName := fmt.Sprintf("%s鏈堝害缁熻鎶ヨ〃%s.xlsx", "浠撳簱", time.Now().Format("2006-01-02-1504"))
+ if err := f.SaveAs(fileName); err != nil {
+ return fileName, err
+ }
+
+ return fileName, nil
+}
+
+func (slf *WarehouseMonthFormsService) GetHeaders() (headers []interface{}, headerLen int, inputTypes, outputTypes []string) {
+ // 鑷畾涔夎〃澶�
+
+ //鏌ヨ鍏ュ簱绫诲瀷
+ inputTypes = GetDictNameListByType(constvar.StorageType)
+ inputTypes = append(inputTypes, constvar.InputTotalHeader)
+
+ headerLen += len(inputTypes)
+
+ //鏌ヨ鍏ュ簱绫诲瀷
+ outputTypes = GetDictNameListByType(constvar.StockoutType)
+ outputTypes = append(outputTypes, constvar.OutPutTotalHeader)
+
+ headerLen += len(inputTypes)
+
+ headerLen += 6
+
+ headers = []interface{}{"鐗╂枡缂栧彿", "鐗╂枡缂栫爜", "鏈堟湯缁撳瓨", "鏈堝垵缁撳瓨", "鍗曚綅", "鍗曚环", map[string][]string{"鏈湀鍏ュ簱": inputTypes}, map[string][]string{"鏈湀鍑哄簱": outputTypes}}
+ return headers, headerLen, inputTypes, outputTypes
+}
+
+func (slf *WarehouseMonthFormsService) SumItems(items []*models.WarehouseStatsItems) (sum decimal.Decimal) {
+ for _, v := range items {
+ sum = sum.Add(v.Amount)
+ }
+ return sum
+}
+
+func (slf *WarehouseMonthFormsService) FillDealerTypeToExcel(items []*models.WarehouseStatsItems, startIndex int, column int, dealerTypes []string, f *excelize.File) {
+ columnStr := strconv.Itoa(column)
+ sum := slf.SumItems(items)
+ detailMap := models.WarehouseStatsItemMap(items)
+ for k := range detailMap {
+ if k == "" {
+ detailMap["鍏朵粬"] = detailMap[k]
+ delete(detailMap, k)
+ }
+ }
+
+ for i := 0; i < len(dealerTypes); i++ {
+ var amount decimal.Decimal
+ if detailMap[dealerTypes[i]] != nil {
+ amount = detailMap[dealerTypes[i]].Amount
+ } else if dealerTypes[i] == constvar.InputTotalHeader {
+ amount = sum
+ }
+ f.SetCellValue("Sheet1", getColumnAlphabet(startIndex+i)+columnStr, amount)
+ }
+ return
+}
+
func GetCurrentWarehouseStats(date string, warehouseId int, productIds []string) (statRecords []*models.WarehouseMonthStats, err error) {
//鏈湀鏈熷垵鏁伴噺/涓婃湀缁撲綑鏁伴噺
groupSumList, err := models.NewLocationProductAmountSearch().SetProductIds(productIds).GroupSum("product_id", "amount")
@@ -69,7 +175,12 @@
}
productMap := models.MaterialMap(products)
- beginTime, endTime := utils.GetLastMonthPeriod()
+ //鎸夐厤缃彇寮�濮嬫椂闂村拰缁撴潫鏃堕棿
+ beginTime, endTime, err := NewSystemConfigService().GetInventoryCutOffTime()
+ if err != nil {
+ logx.Errorf("MonthStats GetCurrentStats get GetInventoryCutOffTime err:%v", err)
+ return
+ }
inputMap, err := GetStatsMulti(beginTime, endTime, constvar.BaseOperationTypeIncoming, warehouseId)
if err != nil {
logx.Errorf("MonthStats GetStatsByOperationType input err:%v", err)
@@ -98,9 +209,9 @@
SalePrice: product.SalePrice,
EndAmount: amount,
InputAmount: SumMapAmount(inputMap[productId]),
- InputItems: GetDealerItems(inputMap[productId]),
+ InputItems: GetDealerItems(inputMap[productId], models.MonthStatsItemsTypeInput),
OutputAmount: SumMapAmount(outputMap[productId]),
- OutputItems: GetDealerItems(outputMap[productId]),
+ OutputItems: GetDealerItems(outputMap[productId], models.MonthStatsItemsTypeOutput),
Date: date,
}
statRecords = append(statRecords, &record)
@@ -110,11 +221,15 @@
}
func GetStatsMulti(beginTime, endTime time.Time, operationType constvar.BaseOperationType, warehouseId int) (m map[string]map[string]decimal.Decimal, err error) {
+ m = make(map[string]map[string]decimal.Decimal)
operationIds, err := models.NewOperationSearch().SetBaseOperationType(operationType).
SetFields("id").SetTimeBetween(beginTime, endTime).
SetWarehouseId(warehouseId).
FindIds()
if err != nil {
+ return
+ }
+ if len(operationIds) == 0 {
return
}
groupSumList, err := models.NewOperationDetailsSearch().SetOperationIds(operationIds).
@@ -140,10 +255,15 @@
return
}
-func GetDealerItems(m map[string]decimal.Decimal) (items []*models.WarehouseStatsItems) {
+func GetDealerItems(m map[string]decimal.Decimal, tp models.MonthStatsItemsType) (items []*models.WarehouseStatsItems) {
for k, v := range m {
+ name := k
+ if name == "" {
+ name = "鍏朵粬"
+ }
items = append(items, &models.WarehouseStatsItems{
- Name: k,
+ Type: tp,
+ Name: name,
Amount: v,
})
}
--
Gitblit v1.8.0