bill-product-excel.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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"},
  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"), 21)})
  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. status := ""
  151. if b.Content.Status == "complete" {
  152. status = "已完成"
  153. }
  154. if b.Content.Status == "created" {
  155. status = "进行中"
  156. }
  157. drawRight(b.Row+2, "状态:"+status)
  158. b.Excel.SetRowHeight(b.SheetName, b.Row+2, 21)
  159. // 第四行
  160. drawLall(b.Row+3, "包含工序:"+b.Content.CompProduceName)
  161. // 设置行高
  162. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row + 3: getRowHeight("包含工序:"+b.Content.CompProduceName, b.getRangeWidth("A:F"), 21)})
  163. return nil
  164. }
  165. func (b *ProductBillExcel) drawTableTitle() error {
  166. b.Row += 4
  167. // 品名 规格 数量 单位 单价 金额
  168. var drawCol = func(prefix string, value string) error {
  169. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  170. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  171. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  172. if err != nil {
  173. return err
  174. }
  175. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  176. if err != nil {
  177. return err
  178. }
  179. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  180. }
  181. drawCol("A", "采购项目")
  182. drawCol("B", "规格")
  183. drawCol("C", "尺寸")
  184. drawCol("D", "下单数量")
  185. drawCol("E", "单位")
  186. drawCol("F", "单价")
  187. drawCol("G", "预算金额")
  188. drawCol("H", "交货时间")
  189. drawCol("I", "备注")
  190. return nil
  191. }
  192. func (b *ProductBillExcel) drawTableContent() error {
  193. b.Row += 2
  194. var DrawRow = func(rowIndex int, values ...string) float64 {
  195. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"}
  196. // 获取改行最大行高
  197. max := getRowHeight(values[0], b.getRangeWidth(charas[0]))
  198. for i, c := range charas {
  199. v := ""
  200. if i < len(values) {
  201. v = values[i]
  202. }
  203. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  204. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  205. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  206. // var magN float64 = 1
  207. // lenv := len([]rune(v))
  208. // magN = math.Ceil(float64(lenv) / 10)
  209. // if lenv%10 > 5 {
  210. // magN = math.Ceil(float64(lenv) / 10)
  211. // } else {
  212. // n := math.Floor(float64(lenv) / 10)
  213. // if n > 0 {
  214. // magN = n
  215. // }
  216. // }
  217. // b.Excel.SetRowHeight(b.SheetName, rowIndex, 21*magN)
  218. if getRowHeight(v, b.getRangeWidth(c)) > max {
  219. max = getRowHeight(v, b.getRangeWidth(c))
  220. }
  221. b.Excel.SetRowHeight(b.SheetName, rowIndex, 50)
  222. }
  223. return max
  224. }
  225. products := b.Content.Products
  226. if len(products) > 0 {
  227. for _, product := range products {
  228. deliveryTime := product.DeliveryTime.Local().Format("2006-01-02")
  229. realCount := "-"
  230. realPrice := "-"
  231. // 预算金额
  232. budgetAmount := fmt.Sprintf("%.3f", float64(product.OrderCount)*product.OrderPrice)
  233. b.FormatToEmpty(&budgetAmount)
  234. // 实际完成数
  235. realCount = fmt.Sprintf("%d", product.ConfirmCount)
  236. b.FormatToEmpty(&realCount)
  237. // 实际金额
  238. realPrice = fmt.Sprintf("%.3f", float64(product.ConfirmCount)*product.OrderPrice)
  239. b.FormatToEmpty(&realPrice)
  240. // a采购项目 b规格 c下单数量 d单位 e完成数量 f单价 g预算金额 h实际金额 i交货时间 j备注
  241. orderCount := fmt.Sprintf("%d", product.OrderCount)
  242. price := fmt.Sprintf("%.3f", product.OrderPrice)
  243. b.FormatToEmpty(&price)
  244. DrawRow(b.Row, product.Name, product.Norm, product.Size, orderCount, product.Unit, price, budgetAmount, deliveryTime, product.Remark)
  245. // rowMaxHeight := DrawRow(b.Row, product.Name, product.Norm, product.Size, orderCount, product.Unit, price, budgetAmount, deliveryTime, product.Remark)
  246. // 设置行高
  247. // b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: rowMaxHeight})
  248. b.Row++
  249. }
  250. }
  251. return nil
  252. }
  253. func (b *ProductBillExcel) drawTableFooter() error {
  254. // left1Cell := fmt.Sprintf("A%d", b.Row)
  255. border := []excelize.Border{
  256. {Type: "top", Style: 1, Color: "000000"},
  257. {Type: "left", Style: 1, Color: "000000"},
  258. {Type: "right", Style: 1, Color: "000000"},
  259. {Type: "bottom", Style: 1, Color: "000000"},
  260. }
  261. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  262. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  263. Border: border,
  264. })
  265. sendToStartCell := fmt.Sprintf("A%d", b.Row)
  266. sendToEndCell := fmt.Sprintf("F%d", b.Row)
  267. supplierStartCell := fmt.Sprintf("G%d", b.Row)
  268. supplierEndCell := fmt.Sprintf("I%d", b.Row)
  269. b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
  270. b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
  271. b.Excel.SetCellValue(b.SheetName, sendToStartCell, "送货地址:"+b.Content.SendTo)
  272. b.Excel.MergeCell(b.SheetName, supplierStartCell, supplierEndCell)
  273. // 设置行高
  274. // b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("送货地址:"+b.Content.SendTo, b.getRangeWidth("A:F"))})
  275. b.Excel.SetCellStyle(b.SheetName, supplierStartCell, supplierEndCell, styleLeft)
  276. b.Excel.SetCellValue(b.SheetName, supplierStartCell, " 供应商签字:")
  277. b.Excel.SetRowHeight(b.SheetName, b.Row, 32)
  278. return nil
  279. }
  280. func (b *ProductBillExcel) drawSupplierRemark() error {
  281. b.Row++
  282. border := []excelize.Border{
  283. {Type: "top", Style: 1, Color: "000000"},
  284. {Type: "left", Style: 1, Color: "000000"},
  285. {Type: "right", Style: 1, Color: "000000"},
  286. {Type: "bottom", Style: 1, Color: "000000"},
  287. }
  288. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  289. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  290. Border: border,
  291. })
  292. sendToStartCell := fmt.Sprintf("A%d", b.Row)
  293. sendToEndCell := fmt.Sprintf("I%d", b.Row)
  294. b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
  295. b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
  296. b.Excel.SetCellValue(b.SheetName, sendToStartCell, "供应商备注:"+b.Content.SupplierRemark)
  297. b.Excel.SetRowHeight(b.SheetName, b.Row, 32)
  298. return nil
  299. }
  300. func (b *ProductBillExcel) drawRemark() error {
  301. // 填备注
  302. b.Row++
  303. remarkTitleCell := fmt.Sprintf("A%d", b.Row)
  304. b.Excel.SetCellValue(b.SheetName, remarkTitleCell, "备注:")
  305. b.Row++
  306. remarkContentScell := fmt.Sprintf("A%d", b.Row)
  307. // 备注内容
  308. reg := regexp.MustCompile(`\n`)
  309. remarkRowNum := len(reg.FindAllString(b.Content.Remark, -1)) + 1
  310. b.Row += remarkRowNum
  311. remarkContentEcell := fmt.Sprintf("J%d", b.Row)
  312. b.Excel.MergeCell(b.SheetName, remarkContentScell, remarkContentEcell)
  313. b.Excel.SetCellValue(b.SheetName, remarkContentScell, b.Content.Remark)
  314. return nil
  315. }
  316. func (b *ProductBillExcel) drawTableSignature() error {
  317. b.Row += 2
  318. // row := b.Row
  319. style1, _ := b.Excel.NewStyle(&excelize.Style{
  320. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  321. })
  322. // 制单人
  323. billUserCellS := fmt.Sprintf("A%d", b.Row+1)
  324. billUserCellE := fmt.Sprintf("B%d", b.Row+1)
  325. b.Excel.MergeCell(b.SheetName, billUserCellS, billUserCellE)
  326. b.Excel.SetCellValue(b.SheetName, billUserCellS, "制单人:"+b.Content.UserName)
  327. fontNum := "F"
  328. image1s := "G"
  329. image2s := "I"
  330. image1e := "H"
  331. image2e := "I"
  332. fontCell := fmt.Sprintf("%s%d", fontNum, b.Row)
  333. fontEndCell := fmt.Sprintf("%s%d", fontNum, b.Row+2)
  334. imageCell1 := fmt.Sprintf("%s%d", image1s, b.Row)
  335. imageEndCell1 := fmt.Sprintf("%s%d", image1e, b.Row+2)
  336. imageCell2 := fmt.Sprintf("%s%d", image2s, b.Row)
  337. imageEndCell2 := fmt.Sprintf("%s%d", image2e, b.Row+2)
  338. b.Excel.MergeCell(b.SheetName, fontCell, fontEndCell)
  339. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  340. b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
  341. // 签字图片1 I10-J12
  342. b.Excel.MergeCell(b.SheetName, imageCell1, imageEndCell1)
  343. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  344. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  345. // 签字图片2 K10-L12
  346. b.Excel.MergeCell(b.SheetName, imageCell2, imageEndCell2)
  347. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  348. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  349. if b.Content.Reviewed == 1 {
  350. imageCells := []string{imageCell1, imageCell2}
  351. if len(b.Signatures) > 0 {
  352. for k, sign := range b.Signatures {
  353. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Path, sign.Format)
  354. }
  355. }
  356. }
  357. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  358. return nil
  359. }
  360. func (b *ProductBillExcel) Draws() {
  361. b.drawTitle()
  362. b.drawSubTitles()
  363. b.drawTableTitle()
  364. b.drawTableContent()
  365. b.drawTableFooter()
  366. if len(b.Content.SupplierRemark) > 1 {
  367. b.drawSupplierRemark()
  368. }
  369. b.drawRemark()
  370. b.drawTableSignature()
  371. // 设置行高
  372. b.setRowsHeight()
  373. }
  374. func NewProductBill(f *excelize.File) *ProductBillExcel {
  375. border := []excelize.Border{
  376. {Type: "top", Style: 1, Color: "000000"},
  377. {Type: "left", Style: 1, Color: "000000"},
  378. {Type: "right", Style: 1, Color: "000000"},
  379. {Type: "bottom", Style: 1, Color: "000000"},
  380. }
  381. styleLeft, _ := f.NewStyle(&excelize.Style{
  382. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  383. Border: border,
  384. Font: &excelize.Font{Size: 10},
  385. })
  386. b := &ProductBillExcel{
  387. Title: "原材料采购单",
  388. SheetName: "Sheet1",
  389. Excel: f,
  390. Offset: 0,
  391. AlignCenterStyle: styleLeft,
  392. Signatures: make([]*model.Signature, 0),
  393. RowMap: map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7, "I": 8},
  394. RowWidthArray: []float64{16, 12, 12, 12, 12, 12, 12, 12, 18},
  395. RowsHeightArray: make([]map[int]float64, 0),
  396. }
  397. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0.25), excelize.PageMarginRight(0))
  398. return b
  399. }
  400. func (b *ProductBillExcel) FormatToEmpty(str *string) {
  401. if *str == "0" || *str == "0.000" {
  402. *str = "-"
  403. }
  404. }
  405. func (b *ProductBillExcel) SetContent(content interface{}) {
  406. b.Content = content.(*model.ProductBill)
  407. }
  408. func (b *ProductBillExcel) SetTitle(title string) {
  409. b.Title = title
  410. }
  411. func (b *ProductBillExcel) SetSheetName(name string) {
  412. b.SheetName = name
  413. }
  414. func (b *ProductBillExcel) SetRow(row int) {
  415. b.Row = row
  416. }
  417. func (b *ProductBillExcel) SetIsPdf(isPdf string) {
  418. b.IsPdf = isPdf
  419. }
  420. func (b *ProductBillExcel) GetRow() int {
  421. return b.Row
  422. }
  423. func (b *ProductBillExcel) SetSignatures(sign interface{}) {
  424. b.Signatures = sign.([]*model.Signature)
  425. }