report-product-excel.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "regexp"
  6. _ "image/gif"
  7. _ "image/jpeg"
  8. _ "image/png"
  9. "github.com/xuri/excelize/v2"
  10. )
  11. type ReportProductExcel struct {
  12. Offset int
  13. Row int
  14. Title string //标题
  15. Excel *excelize.File
  16. SheetName string
  17. AlignCenterStyle int
  18. Content *model.ProductBill
  19. BudgetAmount float64
  20. RealAmount float64
  21. }
  22. func (b *ReportProductExcel) drawTitle() error {
  23. marginLeft := excelize.PageMarginLeft(0.3)
  24. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  25. b.Row++
  26. startCell := fmt.Sprintf("A%d", b.Row)
  27. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("J%d", b.Row))
  28. if err != nil {
  29. return err
  30. }
  31. style, err := b.Excel.NewStyle(&excelize.Style{
  32. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  33. Font: &excelize.Font{Bold: true, Size: 18}})
  34. if err != nil {
  35. return err
  36. }
  37. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  38. if err != nil {
  39. return err
  40. }
  41. b.Excel.SetRowHeight(b.SheetName, b.Row, 23)
  42. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  43. return nil
  44. }
  45. func (b *ReportProductExcel) drawSubTitles() error {
  46. b.Row++
  47. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  48. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  49. Font: &excelize.Font{Size: 11}})
  50. if err != nil {
  51. return err
  52. }
  53. styleRight, err := b.Excel.NewStyle(&excelize.Style{
  54. Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
  55. Font: &excelize.Font{Size: 11}})
  56. if err != nil {
  57. return err
  58. }
  59. var drawLeft = func(rowIndex int, value string) error {
  60. //左边1
  61. left1Cell := fmt.Sprintf("A%d", rowIndex)
  62. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("E%d", rowIndex))
  63. if err != nil {
  64. return err
  65. }
  66. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  67. if err != nil {
  68. return err
  69. }
  70. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  71. return nil
  72. }
  73. var drawRight = func(rowIndex int, value string) error {
  74. right1Cell := fmt.Sprintf("F%d", rowIndex)
  75. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("J%d", rowIndex))
  76. if err != nil {
  77. return err
  78. }
  79. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  80. if err != nil {
  81. return err
  82. }
  83. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  84. return nil
  85. }
  86. //第一行
  87. drawLeft(b.Row, "类别:"+b.Content.Type)
  88. drawRight(b.Row, "单号:"+b.Content.SerialNumber)
  89. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  90. b.Row++
  91. //第二行
  92. drawLeft(b.Row, "供应商名称:"+b.Content.Supplier)
  93. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  94. drawRight(b.Row, "下单时间:"+timeformat)
  95. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  96. b.Row++
  97. //第三行
  98. drawLeft(b.Row, "产品名称:"+b.Content.ProductName)
  99. status := ""
  100. if b.Content.Status == "complete" {
  101. status = fmt.Sprintf("已完成(%d)", b.Content.ConfirmCount)
  102. }
  103. if b.Content.Status == "created" {
  104. status = "进行中"
  105. }
  106. drawRight(b.Row, "状态:"+status)
  107. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  108. return nil
  109. }
  110. func (b *ReportProductExcel) drawTableTitle() error {
  111. b.Row++
  112. // 品名 规格 数量 单位 单价 金额
  113. var drawCol = func(prefix string, value string) error {
  114. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  115. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  116. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  117. if err != nil {
  118. return err
  119. }
  120. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  121. if err != nil {
  122. return err
  123. }
  124. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  125. }
  126. // a采购项目 b规格 c下单数量 d单位 e完成数量 f单价 g预算金额 h实际金额 i交货时间 j备注
  127. drawCol("A", "采购项目")
  128. drawCol("B", "规格")
  129. drawCol("C", "下单数量")
  130. drawCol("D", "单位")
  131. drawCol("E", "完成数量")
  132. drawCol("F", "单价")
  133. drawCol("G", "预算金额")
  134. drawCol("H", "实际金额")
  135. drawCol("I", "交货时间")
  136. drawCol("J", "备注")
  137. return nil
  138. }
  139. func (b *ReportProductExcel) drawTableContent() error {
  140. b.Row += 2
  141. var DrawRow = func(rowIndex int, values ...string) {
  142. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}
  143. for i, c := range charas {
  144. v := ""
  145. if i < len(values) {
  146. v = values[i]
  147. }
  148. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  149. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  150. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  151. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  152. }
  153. }
  154. products := b.Content.Products
  155. if len(products) > 0 {
  156. for _, product := range products {
  157. deliveryTime := product.DeliveryTime.Local().Format("2006-01-02")
  158. realCount := ""
  159. realPrice := ""
  160. // 预算金额
  161. orderPrice := fmt.Sprintf("%.3f", float64(product.OrderCount)*product.Price)
  162. b.FormatToEmpty(&orderPrice)
  163. // 实际完成数
  164. realCount = fmt.Sprintf("%d", product.ConfirmCount)
  165. b.FormatToEmpty(&realCount)
  166. // 实际金额
  167. realPrice = fmt.Sprintf("%.3f", float64(product.ConfirmCount)*product.Price)
  168. b.FormatToEmpty(&realPrice)
  169. // a采购项目 b规格 c下单数量 d单位 e完成数量 f单价 g预算金额 h实际金额 i交货时间 j备注
  170. orderCount := fmt.Sprintf("%d", product.OrderCount)
  171. price := fmt.Sprintf("%.3f", product.Price)
  172. b.FormatToEmpty(&price)
  173. DrawRow(b.Row, product.Name, product.Norm, orderCount, product.Unit, realCount, price, orderPrice, realPrice, deliveryTime, product.Remark)
  174. b.Row++
  175. b.BudgetAmount += float64(product.OrderCount) * product.Price
  176. b.RealAmount += float64(product.ConfirmCount) * product.Price
  177. }
  178. }
  179. return nil
  180. }
  181. func (b *ReportProductExcel) drawRemark() error {
  182. // 填备注
  183. b.Row++
  184. remarkTitleCell := fmt.Sprintf("A%d", b.Row)
  185. b.Excel.SetCellValue(b.SheetName, remarkTitleCell, "备注:")
  186. b.Row++
  187. remarkContentScell := fmt.Sprintf("A%d", b.Row)
  188. // 根据换行符确定多少行单元格
  189. reg := regexp.MustCompile(`\n`)
  190. remarkRowNum := len(reg.FindAllString(b.Content.Remark, -1)) + 1
  191. b.Row += remarkRowNum
  192. remarkContentEcell := fmt.Sprintf("J%d", b.Row)
  193. b.Excel.MergeCell(b.SheetName, remarkContentScell, remarkContentEcell)
  194. b.Excel.SetCellValue(b.SheetName, remarkContentScell, b.Content.Remark)
  195. return nil
  196. }
  197. func (b *ReportProductExcel) Draws() {
  198. b.drawTitle()
  199. b.drawSubTitles()
  200. b.drawTableTitle()
  201. b.drawTableContent()
  202. b.drawRemark()
  203. }
  204. func NewReportProductBill(f *excelize.File) *ReportProductExcel {
  205. border := []excelize.Border{
  206. {Type: "top", Style: 1, Color: "000000"},
  207. {Type: "left", Style: 1, Color: "000000"},
  208. {Type: "right", Style: 1, Color: "000000"},
  209. {Type: "bottom", Style: 1, Color: "000000"},
  210. }
  211. styleLeft, _ := f.NewStyle(&excelize.Style{
  212. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  213. Border: border,
  214. Font: &excelize.Font{Size: 10},
  215. })
  216. b := &ReportProductExcel{
  217. Title: "原材料采购单",
  218. SheetName: "Sheet1",
  219. Excel: f,
  220. Offset: 0,
  221. AlignCenterStyle: styleLeft,
  222. }
  223. f.SetColWidth(b.SheetName, "A", "J", 11.5)
  224. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  225. return b
  226. }
  227. func (b *ReportProductExcel) FormatToEmpty(str *string) {
  228. if *str == "0" || *str == "0.000" {
  229. *str = ""
  230. }
  231. }