Browse Source

update dwonload execl

animeic 2 years ago
parent
commit
8724904a9b

BIN
boxcost/__debug_bin


+ 19 - 9
boxcost/api/bill-produce-excel.go

@@ -1,6 +1,7 @@
 package api
 
 import (
+	"box-cost/db/model"
 	"fmt"
 
 	"github.com/xuri/excelize/v2"
@@ -16,6 +17,8 @@ type ProduceBillExcel struct {
 	SheetName string
 
 	AlignCenterStyle int
+
+	Content *model.ProduceBill
 }
 
 func (b *ProduceBillExcel) drawTitle() error {
@@ -88,17 +91,18 @@ func (b *ProduceBillExcel) drawSubTitles() error {
 	}
 
 	//第一行
-	drawLeft(row, "类别:纸张类")
-	drawRight(row, "单号:ZZL-000003")
+	drawLeft(row, "类别:"+b.Content.Type)
+	drawRight(row, "单号:"+b.Content.SerialNumber)
 	b.Excel.SetRowHeight(b.SheetName, row, 21)
 
 	//第二行
-	drawLeft(row+1, "供应商名称:正大文博")
-	drawRight(row+1, "下单时间:2022年12月07号 19:17:27")
+	drawLeft(row+1, "供应商名称:"+b.Content.Supplier)
+	timeformat := b.Content.CreateTime.Format("2006年01月02号 15:04:05")
+	drawRight(row+1, "下单时间:"+timeformat)
 	b.Excel.SetRowHeight(b.SheetName, row+1, 21)
 
 	//第三行
-	drawLeft(row+2, "产品名称:测试3000盒-3333")
+	drawLeft(row+2, "产品名称:"+b.Content.ProductName)
 	drawRight(row+2, "")
 	b.Excel.SetRowHeight(b.SheetName, row+2, 21)
 
@@ -167,9 +171,15 @@ func (b *ProduceBillExcel) drawTableContent() error {
 			b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
 		}
 	}
-	DrawRow(row, "单色印刷", "按文件要求", "太阳白卡", "0*0", "0*0", "3000", "400", "2022-12-07", "")
-	DrawRow(row + 1)
-	DrawRow(row + 2)
+
+	produces := b.Content.Produces
+	if len(produces) > 0 {
+		for _, produce := range produces {
+			deliveryTime := produce.DeliveryTime.Format("2006-01-02")
+			DrawRow(row, produce.Name, produce.Norm, produce.Paper, produce.PaperSize, produce.PrintSize, fmt.Sprintf("%d", produce.Count), produce.Price, deliveryTime, "")
+			row++
+		}
+	}
 
 	return nil
 }
@@ -198,7 +208,7 @@ func (b *ProduceBillExcel) drawTableFooter() error {
 
 	b.Excel.MergeCell(b.SheetName, addCel, fmt.Sprintf("E%d", row))
 	b.Excel.SetCellStyle(b.SheetName, addCel, fmt.Sprintf("E%d", row), styleLeft)
-	b.Excel.SetCellValue(b.SheetName, addCel, "崇州泗潍路333号--联系电话13882239198")
+	b.Excel.SetCellValue(b.SheetName, addCel, b.Content.SendTo)
 
 	sureCel := fmt.Sprintf("F%d", row)
 	b.Excel.MergeCell(b.SheetName, sureCel, fmt.Sprintf("I%d", row))

+ 1 - 0
boxcost/api/bill-produce.go

@@ -180,6 +180,7 @@ func DownProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	f.SetDefaultFont("宋体")
 
 	billExcel := NewProduceBill(f)
+	billExcel.Content = &bill
 	//设置对应的数据
 	billExcel.Draws()
 

+ 18 - 10
boxcost/api/bill-purchase-excel.go

@@ -1,6 +1,7 @@
 package api
 
 import (
+	"box-cost/db/model"
 	"fmt"
 
 	"github.com/xuri/excelize/v2"
@@ -16,6 +17,8 @@ type PurchaseBillExcel struct {
 	SheetName string
 
 	AlignCenterStyle int
+
+	Content *model.PurchaseBill
 }
 
 func (b *PurchaseBillExcel) drawTitle() error {
@@ -86,19 +89,19 @@ func (b *PurchaseBillExcel) drawSubTitles() error {
 		b.Excel.SetCellValue(b.SheetName, right1Cell, value)
 		return nil
 	}
-
 	//第一行
-	drawLeft(row, "类别:纸张类")
-	drawRight(row, "单号:ZZL-000003")
+	drawLeft(row, "类别:"+b.Content.Type)
+	drawRight(row, "单号:"+b.Content.SerialNumber)
 	b.Excel.SetRowHeight(b.SheetName, row, 21)
 
 	//第二行
-	drawLeft(row+1, "供应商名称:正大文博")
-	drawRight(row+1, "下单时间:2022年12月07号 19:17:27")
+	drawLeft(row+1, "供应商名称:"+b.Content.Supplier)
+	timeformat := b.Content.CreateTime.Format("2006年01月02号 15:04:05")
+	drawRight(row+1, "下单时间:"+timeformat)
 	b.Excel.SetRowHeight(b.SheetName, row+1, 21)
 
 	//第三行
-	drawLeft(row+2, "产品名称:测试3000盒-3333")
+	drawLeft(row+2, "产品名称:"+b.Content.ProductName)
 	drawRight(row+2, "")
 	b.Excel.SetRowHeight(b.SheetName, row+2, 21)
 
@@ -182,9 +185,14 @@ func (b *PurchaseBillExcel) drawTableContent() error {
 			b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
 		}
 	}
-	DrawRow(row, "太阳白卡", "250g", "800", "800", "3000", "0.864", "5400", "2022-12-07", "")
-	DrawRow(row + 1)
-	DrawRow(row + 2)
+	papers := b.Content.Paper
+	if len(papers) > 0 {
+		for _, paper := range papers {
+			deliveryTime := paper.DeliveryTime.Format("2006-01-02")
+			DrawRow(row, paper.Name, paper.Norm, paper.Height, paper.Width, fmt.Sprintf("%d", paper.Count), paper.Price, paper.Price2, deliveryTime, "")
+			row++
+		}
+	}
 
 	return nil
 }
@@ -213,7 +221,7 @@ func (b *PurchaseBillExcel) drawTableFooter() error {
 
 	b.Excel.MergeCell(b.SheetName, addCel, fmt.Sprintf("E%d", row))
 	b.Excel.SetCellStyle(b.SheetName, addCel, fmt.Sprintf("E%d", row), styleLeft)
-	b.Excel.SetCellValue(b.SheetName, addCel, "崇州泗潍路333号--联系电话13882239198")
+	b.Excel.SetCellValue(b.SheetName, addCel, b.Content.SendTo)
 
 	sureCel := fmt.Sprintf("F%d", row)
 	b.Excel.MergeCell(b.SheetName, sureCel, fmt.Sprintf("I%d", row))

+ 2 - 0
boxcost/api/bill.go

@@ -148,6 +148,8 @@ func DownLoadBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	f.SetDefaultFont("宋体")
 
 	billExcel := NewPurchaseBill(f)
+	billExcel.Content = &bill
+
 	//设置对应的数据
 	billExcel.Draws()
 

+ 96 - 0
boxcost/api/plan.go

@@ -9,6 +9,7 @@ import (
 	"time"
 
 	"github.com/gin-gonic/gin"
+	"github.com/xuri/excelize/v2"
 	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/bson/primitive"
 )
@@ -30,6 +31,101 @@ func ProductPlan(r *GinRouter) {
 
 	// 删除生产计划
 	r.POST("/plan/delete/:id", DelProductPlan)
+
+	// 下载部件打印单
+	r.GET("/bill/plan/download", DownLoadPlan)
+
+}
+
+func DownLoadPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	_packId := c.Query("id")
+	compId := c.Query("compId")
+	packId, err := primitive.ObjectIDFromHex(_packId)
+	if err != nil {
+		return nil, errors.New("packId错误")
+	}
+	plan := model.ProductPlan{}
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionProductPlan,
+		Query:       repo.Map{"pack._id": packId},
+	}, &plan)
+	if !found || err != nil {
+		return nil, errors.New("数据未找到")
+	}
+	// 获取部件单据
+	curComp := &model.PackComponent{}
+	for _, comp := range plan.Pack.Components {
+		if comp.Id == compId {
+			curComp = comp
+		}
+	}
+	if curComp.Id == "" {
+		return nil, errors.New("该组件不存在")
+	}
+
+	// 获取bill
+	if len(curComp.Mats) == 0 {
+		return nil, errors.New("该组件数据不存在")
+	}
+	f := excelize.NewFile()
+	// Create a new sheet.
+	index := f.NewSheet("Sheet1")
+	f.SetActiveSheet(index)
+	f.SetDefaultFont("宋体")
+
+	//
+	for _, mat := range curComp.Mats {
+		// 采购单
+		_purchaseId := mat.BillId
+		purchaseId, err := primitive.ObjectIDFromHex(_purchaseId)
+		if err == nil {
+			purchase := model.PurchaseBill{}
+			found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+				CollectName: repo.CollectionBillPurchase,
+				Query:       repo.Map{"_id": purchaseId},
+			}, &purchase)
+			if found {
+				purchaseExcel := NewPurchaseBill(f)
+				purchaseExcel.Content = &purchase
+				//设置对应的数据
+				purchaseExcel.Draws()
+				purchaseExcel.Offset += 15
+			}
+		}
+		if len(mat.Crafts) > 0 {
+			for _, carft := range mat.Crafts {
+				// 加工单
+				_produceId := carft.BillId
+				produceId, err := primitive.ObjectIDFromHex(_produceId)
+				if err == nil {
+					produce := model.ProduceBill{}
+					found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+						CollectName: repo.CollectionBillProduce,
+						Query:       repo.Map{"_id": produceId},
+					}, &produce)
+					if found {
+						billExcel := NewProduceBill(f)
+						billExcel.Content = &produce
+						//设置对应的数据
+						billExcel.Draws()
+						billExcel.Offset += 15
+					}
+
+				}
+			}
+		}
+	}
+
+	c.Header("Content-Type", "application/octet-stream")
+	c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
+	c.Header("Content-Transfer-Encoding", "binary")
+
+	err = f.Write(c.Writer)
+	if err != nil {
+		return nil, err
+	}
+
+	return nil, nil
 }
 
 // 创建生产计划

+ 96 - 0
boxcost/api/tmp.json

@@ -0,0 +1,96 @@
+{
+  "_id": "638f108e0027470dd3a42f64",
+  "compCounts": 1,
+  "components": {
+    "count": 1,
+    "id": "638f108e0027470dd3a42f60",
+    "mats": [
+      {
+        "crafts": [
+          {
+            "count": 1,
+            "craftInfo": {
+              "_id": "638ef06b0027470dd3a42f51",
+              "category": "普通印刷",
+              "createTime": "2022-12-06T15:34:03.326+08:00",
+              "name": "单色印刷",
+              "norm": "按文件要求",
+              "price": 0.018,
+              "unit": "张",
+              "updateTime": "2022-12-06T16:02:30.869+08:00"
+            },
+            "id": "638f108e0027470dd3a42f62",
+            "size": "0"
+          }
+        ],
+        "id": "638f108e0027470dd3a42f61",
+        "matInfo": {
+          "_id": "638eee5bf9039e0980fe5654",
+          "category": "纸张类",
+          "createTime": "2022-12-06T15:25:15.32+08:00",
+          "name": "太阳白卡",
+          "norm": "350g",
+          "price": 5400,
+          "unit": "吨",
+          "updateTime": "2022-12-06T15:25:15.32+08:00"
+        }
+      },
+      {
+        "crafts": [
+          {
+            "count": 1,
+            "craftInfo": {
+              "_id": "638470d24d585ba68be6cbaf",
+              "category": "粘盒",
+              "createTime": "2022-11-28T16:26:58.869+08:00",
+              "name": "粘盒",
+              "norm": "按文件要求",
+              "price": 0.08,
+              "unit": "张",
+              "updateTime": "2022-11-28T16:27:24.914+08:00"
+            },
+            "id": "1670402002385638470d24d585ba68be6cbaf1",
+            "size": "0"
+          }
+        ],
+        "id": "1670402002385638465894d585ba68be6cb9e",
+        "matInfo": {
+          "_id": "638465894d585ba68be6cb9e",
+          "category": "纸张类",
+          "createTime": "2022-11-28T15:38:49.329+08:00",
+          "name": "地龙灰底白",
+          "norm": "250g",
+          "price": 4450,
+          "unit": "吨",
+          "updateTime": "2022-11-28T15:38:49.329+08:00"
+        }
+      },
+      {
+        "crafts": [
+          {
+            "count": 1,
+            "craftInfo": {},
+            "size": "0"
+          }
+        ],
+        "id": "1670409094178635f6a9a7709d49772267fca",
+        "matInfo": {
+          "_id": "635f6a9a7709d49772267fca",
+          "category": "小物件",
+          "createTime": "2022-10-31T14:26:34.142+08:00",
+          "name": "金属手提扣",
+          "norm": "小物件规格11",
+          "price": 1,
+          "unit": "个",
+          "updateTime": "2022-10-31T14:26:34.142+08:00"
+        }
+      }
+    ],
+    "name": "333",
+    "uvSize": "0"
+  },
+  "createTime": "2022-12-06T17:51:10.885+08:00",
+  "designer": "1232",
+  "name": "测试包装",
+  "updateTime": "2022-12-08T09:56:53.766+08:00"
+}