bill-product-excel.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "regexp"
  6. "strings"
  7. _ "image/gif"
  8. _ "image/jpeg"
  9. _ "image/png"
  10. "github.com/xuri/excelize/v2"
  11. )
  12. type ProductBillExcel struct {
  13. Offset int
  14. Row int
  15. Title string //标题
  16. Excel *excelize.File
  17. SheetName string
  18. AlignCenterStyle int
  19. Content *model.ProductBill
  20. Signatures []*model.Signature
  21. IsPdf string
  22. RowMap map[string]int
  23. RowWidthArray []float64
  24. RowsHeightArray []map[int]float64
  25. }
  26. // 批量设置行高
  27. func (b *ProductBillExcel) setRowsHeight() {
  28. for _, rowHeight := range b.RowsHeightArray {
  29. for row, height := range rowHeight {
  30. b.Excel.SetRowHeight(b.SheetName, row, height)
  31. }
  32. }
  33. }
  34. // 获取范围内单元格的宽度 A:F
  35. func (b *ProductBillExcel) getRangeWidth(r string) float64 {
  36. rg := strings.Split(r, ":")
  37. if len(rg) == 1 {
  38. start := b.RowMap[rg[0]]
  39. return b.RowWidthArray[start]
  40. } else if len(rg) == 2 {
  41. start := b.RowMap[rg[0]]
  42. end := b.RowMap[rg[1]]
  43. rowr := b.RowWidthArray[start : end+1]
  44. width := 0.0
  45. for _, v := range rowr {
  46. width += v
  47. }
  48. return width
  49. }
  50. return 0.0
  51. }
  52. func (b *ProductBillExcel) drawTitle() error {
  53. b.Row++
  54. startCell := fmt.Sprintf("A%d", b.Row)
  55. endCell := fmt.Sprintf("I%d", b.Row)
  56. marginLeft := excelize.PageMarginLeft(0.00)
  57. if b.IsPdf == "true" {
  58. marginLeft = excelize.PageMarginLeft(0.1)
  59. }
  60. b.Excel.SetColWidth(b.SheetName, "A", "A", 16)
  61. b.Excel.SetColWidth(b.SheetName, "B", "H", 12)
  62. b.Excel.SetColWidth(b.SheetName, "I", "I", 18)
  63. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  64. err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
  65. if err != nil {
  66. return err
  67. }
  68. style, err := b.Excel.NewStyle(&excelize.Style{
  69. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  70. Font: &excelize.Font{Bold: true, Size: 18}})
  71. if err != nil {
  72. return err
  73. }
  74. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  75. if err != nil {
  76. return err
  77. }
  78. b.Excel.SetRowHeight(b.SheetName, b.Row, 23)
  79. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  80. return nil
  81. }
  82. func (b *ProductBillExcel) drawSubTitles() error {
  83. b.Row++
  84. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  85. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  86. Font: &excelize.Font{Size: 11}})
  87. if err != nil {
  88. return err
  89. }
  90. styleRight, err := b.Excel.NewStyle(&excelize.Style{
  91. Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
  92. Font: &excelize.Font{Size: 11}})
  93. if err != nil {
  94. return err
  95. }
  96. drawLeft := func(rowIndex int, value string) error {
  97. //左边1
  98. left1Cell := fmt.Sprintf("A%d", rowIndex)
  99. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("F%d", rowIndex))
  100. if err != nil {
  101. return err
  102. }
  103. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  104. if err != nil {
  105. return err
  106. }
  107. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  108. return nil
  109. }
  110. drawRight := func(rowIndex int, value string) error {
  111. right1Cell := fmt.Sprintf("G%d", rowIndex)
  112. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("I%d", rowIndex))
  113. if err != nil {
  114. return err
  115. }
  116. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  117. if err != nil {
  118. return err
  119. }
  120. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  121. return nil
  122. }
  123. drawLall := func(rowIndex int, value string) error {
  124. //左边1
  125. left1Cell := fmt.Sprintf("A%d", rowIndex)
  126. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("F%d", rowIndex))
  127. if err != nil {
  128. return err
  129. }
  130. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  131. if err != nil {
  132. return err
  133. }
  134. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  135. return nil
  136. }
  137. //第一行
  138. drawLeft(b.Row, "类别:"+b.Content.Type)
  139. drawRight(b.Row, "单号:"+b.Content.SerialNumber)
  140. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  141. //第二行
  142. supplierContent := fmt.Sprintf("供应商名称:%s", b.Content.Supplier)
  143. drawLeft(b.Row+1, supplierContent)
  144. // 设置行高
  145. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row + 1: getRowHeight(supplierContent, b.getRangeWidth("A:F"))})
  146. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  147. drawRight(b.Row+1, "下单时间:"+timeformat)
  148. //第三行
  149. drawLeft(b.Row+2, "产品名称:"+b.Content.ProductName)
  150. // 设置行高
  151. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row + 2: getRowHeight("产品名称:"+b.Content.ProductName, b.getRangeWidth("A:F"))})
  152. status := ""
  153. if b.Content.Status == "complete" {
  154. status = "已完成"
  155. }
  156. if b.Content.Status == "created" {
  157. status = "进行中"
  158. }
  159. drawRight(b.Row+2, "状态:"+status)
  160. b.Excel.SetRowHeight(b.SheetName, b.Row+2, 21)
  161. // 第四行
  162. drawLall(b.Row+3, "包含工序:"+b.Content.CompProduceName)
  163. // 设置行高
  164. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row + 3: getRowHeight("包含工序:"+b.Content.CompProduceName, b.getRangeWidth("A:F"))})
  165. return nil
  166. }
  167. func (b *ProductBillExcel) drawTableTitle() error {
  168. b.Row += 4
  169. // 品名 规格 数量 单位 单价 金额
  170. var drawCol = func(prefix string, value string) error {
  171. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  172. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  173. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  174. if err != nil {
  175. return err
  176. }
  177. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  178. if err != nil {
  179. return err
  180. }
  181. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  182. }
  183. drawCol("A", "采购项目")
  184. drawCol("B", "规格")
  185. drawCol("C", "尺寸")
  186. drawCol("D", "下单数量")
  187. drawCol("E", "单位")
  188. drawCol("F", "单价")
  189. drawCol("G", "预算金额")
  190. drawCol("H", "交货时间")
  191. drawCol("I", "备注")
  192. return nil
  193. }
  194. func (b *ProductBillExcel) drawTableContent() error {
  195. b.Row += 2
  196. var DrawRow = func(rowIndex int, values ...string) float64 {
  197. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"}
  198. // 获取改行最大行高
  199. max := getRowHeight(values[0], b.getRangeWidth(charas[0]))
  200. for i, c := range charas {
  201. v := ""
  202. if i < len(values) {
  203. v = values[i]
  204. }
  205. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  206. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  207. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  208. if getRowHeight(v, b.getRangeWidth(c)) > max {
  209. max = getRowHeight(v, b.getRangeWidth(c))
  210. }
  211. }
  212. return max
  213. }
  214. products := b.Content.Products
  215. if len(products) > 0 {
  216. for _, product := range products {
  217. deliveryTime := product.DeliveryTime.Local().Format("2006-01-02")
  218. realCount := "-"
  219. realPrice := "-"
  220. // 预算金额
  221. budgetAmount := fmt.Sprintf("%.3f", float64(product.OrderCount)*product.OrderPrice)
  222. b.FormatToEmpty(&budgetAmount)
  223. // 实际完成数
  224. realCount = fmt.Sprintf("%d", product.ConfirmCount)
  225. b.FormatToEmpty(&realCount)
  226. // 实际金额
  227. realPrice = fmt.Sprintf("%.3f", float64(product.ConfirmCount)*product.OrderPrice)
  228. b.FormatToEmpty(&realPrice)
  229. // a采购项目 b规格 c下单数量 d单位 e完成数量 f单价 g预算金额 h实际金额 i交货时间 j备注
  230. orderCount := fmt.Sprintf("%d", product.OrderCount)
  231. price := fmt.Sprintf("%.3f", product.OrderPrice)
  232. b.FormatToEmpty(&price)
  233. // DrawRow(b.Row, product.Name, product.Norm, product.Size, orderCount, product.Unit, price, budgetAmount, deliveryTime, product.Remark)
  234. rowMaxHeight := DrawRow(b.Row, product.Name, product.Norm, product.Size, orderCount, product.Unit, price, budgetAmount, deliveryTime, product.Remark)
  235. // 设置行高
  236. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: rowMaxHeight})
  237. b.Row++
  238. }
  239. }
  240. return nil
  241. }
  242. func (b *ProductBillExcel) drawTableFooter() error {
  243. // left1Cell := fmt.Sprintf("A%d", b.Row)
  244. border := []excelize.Border{
  245. {Type: "top", Style: 1, Color: "000000"},
  246. {Type: "left", Style: 1, Color: "000000"},
  247. {Type: "right", Style: 1, Color: "000000"},
  248. {Type: "bottom", Style: 1, Color: "000000"},
  249. }
  250. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  251. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  252. Border: border,
  253. })
  254. sendToStartCell := fmt.Sprintf("A%d", b.Row)
  255. sendToEndCell := fmt.Sprintf("F%d", b.Row)
  256. supplierStartCell := fmt.Sprintf("G%d", b.Row)
  257. supplierEndCell := fmt.Sprintf("I%d", b.Row)
  258. b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
  259. b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
  260. b.Excel.SetCellValue(b.SheetName, sendToStartCell, "送货地址:"+b.Content.SendTo)
  261. b.Excel.MergeCell(b.SheetName, supplierStartCell, supplierEndCell)
  262. // 设置行高
  263. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("送货地址:"+b.Content.SendTo, b.getRangeWidth("A:F"))})
  264. b.Excel.SetCellStyle(b.SheetName, supplierStartCell, supplierEndCell, styleLeft)
  265. b.Excel.SetCellValue(b.SheetName, supplierStartCell, " 供应商签字:")
  266. return nil
  267. }
  268. func (b *ProductBillExcel) drawSupplierRemark() error {
  269. b.Row++
  270. border := []excelize.Border{
  271. {Type: "top", Style: 1, Color: "000000"},
  272. {Type: "left", Style: 1, Color: "000000"},
  273. {Type: "right", Style: 1, Color: "000000"},
  274. {Type: "bottom", Style: 1, Color: "000000"},
  275. }
  276. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  277. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  278. Border: border,
  279. })
  280. sendToStartCell := fmt.Sprintf("A%d", b.Row)
  281. sendToEndCell := fmt.Sprintf("I%d", b.Row)
  282. b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
  283. b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
  284. b.Excel.SetCellValue(b.SheetName, sendToStartCell, "供应商备注:"+b.Content.SupplierRemark)
  285. // 设置行高
  286. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("供应商备注:"+b.Content.SupplierRemark, b.getRangeWidth("A:I"))})
  287. return nil
  288. }
  289. func (b *ProductBillExcel) drawRemark() error {
  290. // 填备注
  291. b.Row++
  292. remarkTitleCell := fmt.Sprintf("A%d", b.Row)
  293. b.Excel.SetCellValue(b.SheetName, remarkTitleCell, "备注:")
  294. b.Row++
  295. remarkContentScell := fmt.Sprintf("A%d", b.Row)
  296. // 备注内容
  297. reg := regexp.MustCompile(`\n`)
  298. remarkRowNum := len(reg.FindAllString(b.Content.Remark, -1)) + 1
  299. b.Row += remarkRowNum
  300. remarkContentEcell := fmt.Sprintf("J%d", b.Row)
  301. b.Excel.MergeCell(b.SheetName, remarkContentScell, remarkContentEcell)
  302. b.Excel.SetCellValue(b.SheetName, remarkContentScell, b.Content.Remark)
  303. return nil
  304. }
  305. func (b *ProductBillExcel) drawTableSignature() error {
  306. b.Row += 2
  307. // row := b.Row
  308. style1, _ := b.Excel.NewStyle(&excelize.Style{
  309. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  310. })
  311. // 制单人
  312. billUserCellS := fmt.Sprintf("A%d", b.Row+1)
  313. billUserCellE := fmt.Sprintf("B%d", b.Row+1)
  314. b.Excel.MergeCell(b.SheetName, billUserCellS, billUserCellE)
  315. b.Excel.SetCellValue(b.SheetName, billUserCellS, "制单人:"+b.Content.UserName)
  316. fontNum := "F"
  317. image1s := "G"
  318. image2s := "I"
  319. image1e := "H"
  320. image2e := "I"
  321. fontCell := fmt.Sprintf("%s%d", fontNum, b.Row)
  322. fontEndCell := fmt.Sprintf("%s%d", fontNum, b.Row+2)
  323. imageCell1 := fmt.Sprintf("%s%d", image1s, b.Row)
  324. imageEndCell1 := fmt.Sprintf("%s%d", image1e, b.Row+2)
  325. imageCell2 := fmt.Sprintf("%s%d", image2s, b.Row)
  326. imageEndCell2 := fmt.Sprintf("%s%d", image2e, b.Row+2)
  327. b.Excel.MergeCell(b.SheetName, fontCell, fontEndCell)
  328. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  329. b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
  330. // 签字图片1 I10-J12
  331. b.Excel.MergeCell(b.SheetName, imageCell1, imageEndCell1)
  332. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  333. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  334. // 签字图片2 K10-L12
  335. b.Excel.MergeCell(b.SheetName, imageCell2, imageEndCell2)
  336. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  337. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  338. if b.Content.Reviewed == 1 {
  339. imageCells := []string{imageCell1, imageCell2}
  340. if len(b.Signatures) > 0 {
  341. for k, sign := range b.Signatures {
  342. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Path, sign.Format)
  343. }
  344. }
  345. }
  346. return nil
  347. }
  348. func (b *ProductBillExcel) Draws() {
  349. b.drawTitle()
  350. b.drawSubTitles()
  351. b.drawTableTitle()
  352. b.drawTableContent()
  353. b.drawTableFooter()
  354. if len(b.Content.SupplierRemark) > 1 {
  355. b.drawSupplierRemark()
  356. }
  357. b.drawRemark()
  358. b.drawTableSignature()
  359. // 设置行高
  360. b.setRowsHeight()
  361. }
  362. func NewProductBill(f *excelize.File) *ProductBillExcel {
  363. border := []excelize.Border{
  364. {Type: "top", Style: 1, Color: "000000"},
  365. {Type: "left", Style: 1, Color: "000000"},
  366. {Type: "right", Style: 1, Color: "000000"},
  367. {Type: "bottom", Style: 1, Color: "000000"},
  368. }
  369. styleLeft, _ := f.NewStyle(&excelize.Style{
  370. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  371. Border: border,
  372. Font: &excelize.Font{Size: 10},
  373. })
  374. b := &ProductBillExcel{
  375. Title: "原材料采购单",
  376. SheetName: "Sheet1",
  377. Excel: f,
  378. Offset: 0,
  379. AlignCenterStyle: styleLeft,
  380. Signatures: make([]*model.Signature, 0),
  381. RowMap: map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7, "I": 8},
  382. RowWidthArray: []float64{16, 12, 12, 12, 12, 12, 12, 12, 18},
  383. RowsHeightArray: make([]map[int]float64, 0),
  384. }
  385. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0.25), excelize.PageMarginRight(0))
  386. return b
  387. }
  388. func (b *ProductBillExcel) FormatToEmpty(str *string) {
  389. if *str == "0" || *str == "0.000" {
  390. *str = "-"
  391. }
  392. }
  393. func (b *ProductBillExcel) SetContent(content interface{}) {
  394. b.Content = content.(*model.ProductBill)
  395. }
  396. func (b *ProductBillExcel) SetTitle(title string) {
  397. b.Title = title
  398. }
  399. func (b *ProductBillExcel) SetSheetName(name string) {
  400. b.SheetName = name
  401. }
  402. func (b *ProductBillExcel) SetRow(row int) {
  403. b.Row = row
  404. }
  405. func (b *ProductBillExcel) SetIsPdf(isPdf string) {
  406. b.IsPdf = isPdf
  407. }
  408. func (b *ProductBillExcel) GetRow() int {
  409. return b.Row
  410. }
  411. func (b *ProductBillExcel) SetSignatures(sign interface{}) {
  412. b.Signatures = sign.([]*model.Signature)
  413. }