Selaa lähdekoodia

update:导出出库单,多选后按照收货单位分页打印

zoie 9 kuukautta sitten
vanhempi
commit
1fa2051877
1 muutettua tiedostoa jossa 201 lisäystä ja 179 poistoa
  1. 201 179
      controllers/stock_template.go

+ 201 - 179
controllers/stock_template.go

@@ -3638,208 +3638,232 @@ func (c StockTemplateController) StockOutExport() {
 		c.Error(500, err, err.Error())
 		return
 	}
+	transportRecordWordMap := make(map[string][]map[string]interface{})
+	for _, row := range list {
+		receiving_unit := utils.ToString(row["receiving_unit"])
+		if _, ok := transportRecordWordMap[receiving_unit]; !ok {
+			list2 := make([]map[string]interface{}, 0)
+			list2 = append(list2, row)
+			transportRecordWordMap[receiving_unit] = list2
+		}else {
+			transportRecordWordMap[receiving_unit] = append(transportRecordWordMap[receiving_unit], row)
+		}
+	}
+
+
 	reqData.DeptID = deptId
 	reqData.DeptName = beegouser.GetDeptName(c.Ctx)
 	reqData.SetCreateBy(beegouser.GetUserId(c.Ctx))
 	reqData.SetDeptId(deptId)
-	code, err := barcodeServ.Insert(reqData)
-	if err != nil {
-		err = errors.New("生成条码失败")
-		c.Error(500, err, err.Error())
-		return
-	}
-	cs, _ := code128.Encode(code)
-	// 创建一个要输出数据的文件
-	imgPath := "ofile/img/" + code + ".png"
-	file, _ := os.Create(imgPath)
-	defer file.Close()
-
-	// 设置图片像素大小
-	qrCode, _ := barcode.Scale(cs, 205, 50)
-	// 将code128的条形码编码为png图片
-	png.Encode(file, qrCode)
-
-	cols := []float64{100, 80, 85, 70, 70, 30, 35, 35, 50}
-
-	header := []string{"品种", "生产企业", "规格", "批号", "有效期", "单位", "数量", "单价", "总价"}
-	rows := [][]string{}
-	var totalMoney float64
-	var totalQuantity int
-	for _, row := range list {
-		temp := []string{}
-		product := utils.ToString(row[models.FieldProductName])
-		enterprise := utils.ToString(row[models.FieldEnterpriseName])
-		spec := utils.ToString(row[models.FieldSpecName])
-		batchNumber := utils.ToString(row[models.FieldBatchNumber])
-		temp = append(temp, product)
-		temp = append(temp, fmt.Sprintf("%s", enterprise))
-		temp = append(temp, spec)
-		temp = append(temp, batchNumber)
-		temp = append(temp, utils.ToString(row[models.FieldExpiryDate]))
-		temp = append(temp, utils.ToString(row[models.FieldUnitName]))
-		temp = append(temp, fmt.Sprintf("%d", utils.ToInt(row["quantity"])))
-		temp = append(temp, fmt.Sprintf("%.01f", utils.ToFloat64(row["unit_price"])))
-		temp = append(temp, fmt.Sprintf("%.01f", utils.ToFloat64(row["money"])))
-		totalMoney += utils.ToFloat64(row["money"])
-		totalQuantity += utils.ToInt(row["quantity"])
-		rows = append(rows, temp)
-	}
-
 	pdf := &gopdf.GoPdf{}
 	pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}})
 	pdf.SetMarginTop(40)
 	pdf.SetMarginBottom(40)
-
-	pdf.AddPage()
-	pdf.AddTTFFont("simsun", "static/fonts/MiSans-Medium.ttf")
-
-	pdf.SetMargins(2, 5, 5, 5)
-	pdf.SetFont("simsun", "", 10)
-	//pdf.SetX(20)
-	//pdf.SetY(20)
-	//pdf.Text("[退货出库]")
-	pdf.SetX(420)
-	pdf.SetY(60)
-	pdf.Text(code)
-
-	imgPath2 := "ofile/img/" + code + ".jpg"
-	png2j.Con2jpg(imgPath, imgPath2)
-	pdf.Image(imgPath2, 390, 15, &gopdf.Rect{W: 200, H: 35})
-
 	titleStr := reqData.DeptName + "出库单"
-	pdf.SetFont("simsun", "", 22)
-	textw, _ := pdf.MeasureTextWidth(titleStr)
-	pdf.SetX((595 / 2) - (textw / 2) - 50)
-	pdf.SetY(40)
-	pdf.Text(titleStr)
-
-	pdf.SetFont("simsun", "", 10)
-	// 第一行
-	curx, y := 20., pdf.GetY()
-	y += 60
-	pdf.SetXY(curx, y)
-	pdf.Text("收货单位:" + reqData.ReceivingUnit)
 
-	curx = 220
-	pdf.SetXY(curx, y)
-	pdf.Text("收货人/电话:")
+	for receiving_unit, list3 := range transportRecordWordMap {
+		reqData.ReceivingUnit = receiving_unit
+		var code string
+		code, err = barcodeServ.Insert(reqData)
+		if err != nil {
+			err = errors.New("生成条码失败")
+			c.Error(500, err, err.Error())
+			return
+		}
+		cs, _ := code128.Encode(code)
+		// 创建一个要输出数据的文件
+		imgPath := "ofile/img/" + code + ".png"
+		file, _ := os.Create(imgPath)
+		defer file.Close()
+
+		// 设置图片像素大小
+		qrCode, _ := barcode.Scale(cs, 205, 50)
+		// 将code128的条形码编码为png图片
+		png.Encode(file, qrCode)
+
+		cols := []float64{100, 80, 85, 70, 70, 30, 35, 35, 50}
+
+		header := []string{"品种", "生产企业", "规格", "批号", "有效期", "单位", "数量", "单价", "总价"}
+		rows := [][]string{}
+		var totalMoney float64
+		var totalQuantity int
+		for _, row := range list3 {
+			temp := []string{}
+			product := utils.ToString(row[models.FieldProductName])
+			enterprise := utils.ToString(row[models.FieldEnterpriseName])
+			spec := utils.ToString(row[models.FieldSpecName])
+			batchNumber := utils.ToString(row[models.FieldBatchNumber])
+			temp = append(temp, product)
+			temp = append(temp, fmt.Sprintf("%s", enterprise))
+			temp = append(temp, spec)
+			temp = append(temp, batchNumber)
+			temp = append(temp, utils.ToString(row[models.FieldExpiryDate]))
+			temp = append(temp, utils.ToString(row[models.FieldUnitName]))
+			temp = append(temp, fmt.Sprintf("%d", utils.ToInt(row["quantity"])))
+			temp = append(temp, fmt.Sprintf("%.01f", utils.ToFloat64(row["unit_price"])))
+			temp = append(temp, fmt.Sprintf("%.01f", utils.ToFloat64(row["money"])))
+			totalMoney += utils.ToFloat64(row["money"])
+			totalQuantity += utils.ToInt(row["quantity"])
+			rows = append(rows, temp)
+		}
 
-	curx = 330
-	pdf.SetXY(curx, y)
-	pdf.Text("日期:" + reqData.Date)
 
-	curx = 430
-	pdf.SetXY(curx, y)
-	pdf.Text("单据:" + code)
+		pdf.AddPage()
+		pdf.AddTTFFont("simsun", "static/fonts/MiSans-Medium.ttf")
+
+		pdf.SetMargins(2, 5, 5, 5)
+		pdf.SetFont("simsun", "", 10)
+		//pdf.SetX(20)
+		//pdf.SetY(20)
+		//pdf.Text("[退货出库]")
+		pdf.SetX(420)
+		pdf.SetY(60)
+		pdf.Text(code)
+
+		imgPath2 := "ofile/img/" + code + ".jpg"
+		png2j.Con2jpg(imgPath, imgPath2)
+		pdf.Image(imgPath2, 390, 15, &gopdf.Rect{W: 200, H: 35})
+		defer func() {
+			os.Remove(imgPath)
+			os.Remove(imgPath2)
+		}()
+
+		pdf.SetFont("simsun", "", 22)
+		textw, _ := pdf.MeasureTextWidth(titleStr)
+		pdf.SetX((595 / 2) - (textw / 2) - 50)
+		pdf.SetY(40)
+		pdf.Text(titleStr)
+
+		pdf.SetFont("simsun", "", 10)
+		// 第一行
+		curx, y := 20., pdf.GetY()
+		y += 60
+		pdf.SetXY(curx, y)
+		pdf.Text("收货单位:" + reqData.ReceivingUnit)
+
+		curx = 220
+		pdf.SetXY(curx, y)
+		pdf.Text("收货人/电话:")
+
+		curx = 330
+		pdf.SetXY(curx, y)
+		pdf.Text("日期:" + reqData.Date)
+
+		curx = 430
+		pdf.SetXY(curx, y)
+		pdf.Text("单据:" + code)
+
+		curx = 20
+		y += 10
+		pdf.SetXY(curx, y)
+		h := 20.0
+		x := curx
+		pdf.SetFont("simsun", "", 9)
+		for i := 0; i < len(header); i++ {
+			utils.RectFillColorMultiCell(pdf, header[i], 9, x, y, cols[i], h, 0, 0, 0, gopdf.Center, gopdf.Middle)
+			x += cols[i]
+		}
+		y += h
+		pdf.SetXY(curx, y)
+		pdf.SetFont("simsun", "", 8)
+
+		for _, row := range rows {
+			curx, y = pdf.GetX(), pdf.GetY()
+			x = curx
+			height := h
+			for i, txt := range row {
+				lineTexts, _ := pdf.SplitText(txt, cols[i])
+				//lineHt := h * float64(len(lineTexts))
+				if len(lineTexts) > 1 {
+					lineTexts, _ = pdf.SplitText(txt, cols[i]-4)
+					lineHt := h * float64(len(lineTexts)) * 0.6
+					if lineHt > height {
+						height = lineHt
+					}
+				}
+			}
 
-	curx = 20
-	y += 10
-	pdf.SetXY(curx, y)
-	h := 20.0
-	x := curx
-	pdf.SetFont("simsun", "", 9)
-	for i := 0; i < len(header); i++ {
-		utils.RectFillColorMultiCell(pdf, header[i], 9, x, y, cols[i], h, 0, 0, 0, gopdf.Center, gopdf.Middle)
-		x += cols[i]
-	}
-	y += h
-	pdf.SetXY(curx, y)
-	pdf.SetFont("simsun", "", 8)
+			for i, txt := range row {
+				width := cols[i]
+				utils.RectFillColorMultiCell(pdf, txt, 9, x, y, width, height, 0, 0, 0, gopdf.Center, gopdf.Middle)
+				x += width
+			}
+			pdf.SetNewY(y+height, height)
+			pdf.SetX(curx)
+		}
+		curx, y = 20, pdf.GetY()
+		x = curx
+		if pdf.GetY() < 30 {
+			pdf.SetY(30)
+		}
+		rows2 := []string{"金额(大写)", utils.AmountConvert(totalMoney, true), fmt.Sprintf("%.1f", totalMoney)}
+		cols2 := [3]float64{}
+		cols2[0] = cols[0]
+		cols2[1] = cols[1] + cols[2] + cols[3]
+		cols2[2] = cols[4] + cols[5] + cols[6] + cols[7] + cols[8]
+		for i, txt := range rows2 {
+			width := cols2[i]
+			utils.RectFillColorMultiCell(pdf, txt, 9, x, y, width, h, 0, 0, 0, gopdf.Center, gopdf.Middle)
+			x += width
+		}
+		pdf.SetNewY(y, 20)
+		pdf.SetX(curx)
 
-	for _, row := range rows {
-		curx, y = pdf.GetX(), pdf.GetY()
+		curx, y = 20, pdf.GetY()
+		y += 20
 		x = curx
-		height := h
-		for i, txt := range row {
-			lineTexts, _ := pdf.SplitText(txt, cols[i])
-			//lineHt := h * float64(len(lineTexts))
-			if len(lineTexts) > 1 {
-				lineTexts, _ = pdf.SplitText(txt, cols[i]-4)
-				lineHt := h * float64(len(lineTexts)) * 0.6
-				if lineHt > height {
-					height = lineHt
-				}
-			}
+		if pdf.GetY() < 30 {
+			pdf.SetY(30)
 		}
+		rows3 := []string{"数量", fmt.Sprintf("%d", totalQuantity)}
+		cols3 := [2]float64{}
+		cols3[0] = cols[0]
+		cols3[1] = cols[1] + cols[2] + cols[3] + cols[4] + cols[5] + cols[6] + cols[7] + cols[8]
+		for i, txt := range rows3 {
+			width := cols3[i]
+			utils.RectFillColorMultiCell(pdf, txt, 9, x, y, width, h, 0, 0, 0, gopdf.Center, gopdf.Middle)
+			x += width
+		}
+		pdf.SetNewY(y, 20)
+		pdf.SetX(curx)
 
-		for i, txt := range row {
-			width := cols[i]
-			utils.RectFillColorMultiCell(pdf, txt, 9, x, y, width, height, 0, 0, 0, gopdf.Center, gopdf.Middle)
+		curx, y = 20, pdf.GetY()
+		y += 20
+		x = curx
+		if pdf.GetY() < 30 {
+			pdf.SetY(30)
+		}
+		rows4 := []string{"备注:"}
+		cols4 := [1]float64{}
+		cols4[0] = cols[0] + cols[1] + cols[2] + cols[3] + cols[4] + cols[5] + cols[6] + cols[7] + cols[8]
+		for i, txt := range rows4 {
+			width := cols4[i]
+			utils.RectFillColorMultiCell(pdf, txt, 9, x, y, width, h, 0, 0, 0, gopdf.Left, gopdf.Middle)
 			x += width
 		}
-		pdf.SetNewY(y+height, height)
+		pdf.SetNewY(y, 20)
+
+		if pdf.GetY() < 30 {
+			pdf.SetY(30)
+		}
+		y = pdf.GetY()
 		pdf.SetX(curx)
-	}
-	curx, y = 20, pdf.GetY()
-	x = curx
-	if pdf.GetY() < 30 {
-		pdf.SetY(30)
-	}
-	rows2 := []string{"金额(大写)", utils.AmountConvert(totalMoney, true), fmt.Sprintf("%.1f", totalMoney)}
-	cols2 := [3]float64{}
-	cols2[0] = cols[0]
-	cols2[1] = cols[1] + cols[2] + cols[3]
-	cols2[2] = cols[4] + cols[5] + cols[6] + cols[7] + cols[8]
-	for i, txt := range rows2 {
-		width := cols2[i]
-		utils.RectFillColorMultiCell(pdf, txt, 9, x, y, width, h, 0, 0, 0, gopdf.Center, gopdf.Middle)
-		x += width
-	}
-	pdf.SetNewY(y, 20)
-	pdf.SetX(curx)
+		y += 40
+		pdf.SetFont("simsun", "", 10)
+		curx = 40
+		pdf.SetXY(curx, y)
+		pdf.Text("主管:")
 
-	curx, y = 20, pdf.GetY()
-	y += 20
-	x = curx
-	if pdf.GetY() < 30 {
-		pdf.SetY(30)
-	}
-	rows3 := []string{"数量", fmt.Sprintf("%d", totalQuantity)}
-	cols3 := [2]float64{}
-	cols3[0] = cols[0]
-	cols3[1] = cols[1] + cols[2] + cols[3] + cols[4] + cols[5] + cols[6] + cols[7] + cols[8]
-	for i, txt := range rows3 {
-		width := cols3[i]
-		utils.RectFillColorMultiCell(pdf, txt, 9, x, y, width, h, 0, 0, 0, gopdf.Center, gopdf.Middle)
-		x += width
-	}
-	pdf.SetNewY(y, 20)
-	pdf.SetX(curx)
+		curx = 200
+		pdf.SetXY(curx, y)
+		pdf.Text("制单:")
 
-	curx, y = 20, pdf.GetY()
-	y += 20
-	x = curx
-	if pdf.GetY() < 30 {
-		pdf.SetY(30)
-	}
-	rows4 := []string{"备注:"}
-	cols4 := [1]float64{}
-	cols4[0] = cols[0] + cols[1] + cols[2] + cols[3] + cols[4] + cols[5] + cols[6] + cols[7] + cols[8]
-	for i, txt := range rows4 {
-		width := cols4[i]
-		utils.RectFillColorMultiCell(pdf, txt, 9, x, y, width, h, 0, 0, 0, gopdf.Left, gopdf.Middle)
-		x += width
-	}
-	pdf.SetNewY(y, 20)
+		curx = 400
+		pdf.SetXY(curx, y)
+		pdf.Text("库管:")
 
-	if pdf.GetY() < 30 {
-		pdf.SetY(30)
 	}
-	y = pdf.GetY()
-	pdf.SetX(curx)
-	y += 40
-	pdf.SetFont("simsun", "", 10)
-	curx = 40
-	pdf.SetXY(curx, y)
-	pdf.Text("主管:")
 
-	curx = 200
-	pdf.SetXY(curx, y)
-	pdf.Text("制单:")
-
-	curx = 400
-	pdf.SetXY(curx, y)
-	pdf.Text("库管:")
 
 	filename := titleStr + time.Now().Format("20060102150405") + ".pdf"
 	// 保存文件
@@ -3848,8 +3872,6 @@ func (c StockTemplateController) StockOutExport() {
 	}
 	defer func() {
 		os.Remove("ofile/" + filename)
-		os.Remove(imgPath)
-		os.Remove(imgPath2)
 	}()
 
 	// 返回生成的 Excel 文件