report-product-excel.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. b.Row++
  24. startCell := fmt.Sprintf("A%d", b.Row)
  25. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("J%d", b.Row))
  26. if err != nil {
  27. return err
  28. }
  29. style, err := b.Excel.NewStyle(&excelize.Style{
  30. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  31. Font: &excelize.Font{Bold: true, Size: 18}})
  32. if err != nil {
  33. return err
  34. }
  35. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  36. if err != nil {
  37. return err
  38. }
  39. b.Excel.SetRowHeight(b.SheetName, b.Row, 23)
  40. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  41. return nil
  42. }
  43. func (b *ReportProductExcel) drawSubTitles() error {
  44. b.Row++
  45. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  46. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  47. Font: &excelize.Font{Size: 11}})
  48. if err != nil {
  49. return err
  50. }
  51. styleRight, err := b.Excel.NewStyle(&excelize.Style{
  52. Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
  53. Font: &excelize.Font{Size: 11}})
  54. if err != nil {
  55. return err
  56. }
  57. var drawLeft = func(rowIndex int, value string) error {
  58. //左边1
  59. left1Cell := fmt.Sprintf("A%d", rowIndex)
  60. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("F%d", rowIndex))
  61. if err != nil {
  62. return err
  63. }
  64. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  65. if err != nil {
  66. return err
  67. }
  68. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  69. return nil
  70. }
  71. var drawRight = func(rowIndex int, value string) error {
  72. right1Cell := fmt.Sprintf("G%d", rowIndex)
  73. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("J%d", rowIndex))
  74. if err != nil {
  75. return err
  76. }
  77. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  78. if err != nil {
  79. return err
  80. }
  81. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  82. return nil
  83. }
  84. //第一行
  85. drawLeft(b.Row, "需求方(甲方):"+b.Title+"订单")
  86. drawRight(b.Row, "单号:"+b.Content.SerialNumber)
  87. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  88. //第二行
  89. drawLeft(b.Row+1, "供应方(乙方):"+b.Content.Supplier)
  90. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  91. drawRight(b.Row+1, "下单时间:"+timeformat)
  92. b.Excel.SetRowHeight(b.SheetName, b.Row+1, 21)
  93. //第三行
  94. drawLeft(b.Row+2, "产品名称:"+b.Content.ProductName)
  95. status := ""
  96. if b.Content.Status == "complete" {
  97. status = "已完成"
  98. }
  99. if b.Content.Status == "created" {
  100. status = "进行中"
  101. }
  102. drawRight(b.Row+2, "状态:"+status)
  103. b.Excel.SetRowHeight(b.SheetName, b.Row+2, 21)
  104. return nil
  105. }
  106. func (b *ReportProductExcel) drawTableTitle() error {
  107. b.Row++
  108. // 品名 规格 数量 单位 单价 金额
  109. var drawCol = func(prefix string, value string) error {
  110. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  111. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  112. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  113. if err != nil {
  114. return err
  115. }
  116. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  117. if err != nil {
  118. return err
  119. }
  120. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  121. }
  122. // a采购项目 b规格 c下单数量 d单位 e完成数量 f单价 g预算金额 h实际金额 i交货时间 j备注
  123. drawCol("A", "采购项目")
  124. drawCol("B", "规格")
  125. drawCol("C", "下单数量")
  126. drawCol("D", "单位")
  127. drawCol("E", "完成数量")
  128. drawCol("F", "单价")
  129. drawCol("G", "预算金额")
  130. drawCol("H", "实际金额")
  131. drawCol("I", "交货时间")
  132. drawCol("J", "备注")
  133. return nil
  134. }
  135. func (b *ReportProductExcel) drawTableContent() error {
  136. b.Row += 2
  137. var DrawRow = func(rowIndex int, values ...string) {
  138. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}
  139. for i, c := range charas {
  140. v := ""
  141. if i < len(values) {
  142. v = values[i]
  143. }
  144. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  145. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  146. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  147. // var magN float64 = 1
  148. // lenv := len([]rune(v))
  149. // magN = math.Ceil(float64(lenv) / 10)
  150. // b.Excel.SetRowHeight(b.SheetName, rowIndex, 21*magN)
  151. b.Excel.SetRowHeight(b.SheetName, rowIndex, 46)
  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. budgetAmount := fmt.Sprintf("%.3f", float64(product.OrderCount)*product.OrderPrice)
  162. b.FormatToEmpty(&budgetAmount)
  163. // 实际完成数
  164. realCount = fmt.Sprintf("%d", product.ConfirmCount)
  165. b.FormatToEmpty(&realCount)
  166. // 实际金额
  167. realPrice = fmt.Sprintf("%.3f", float64(product.ConfirmCount)*product.OrderPrice)
  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.OrderPrice)
  172. b.FormatToEmpty(&price)
  173. DrawRow(b.Row, product.Name, product.Norm, orderCount, product.Unit, realCount, price, budgetAmount, realPrice, deliveryTime, product.Remark)
  174. b.Row++
  175. b.BudgetAmount += float64(product.OrderCount) * product.OrderPrice
  176. b.RealAmount += float64(product.ConfirmCount) * product.OrderPrice
  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. // 签字位置
  196. b.Row += 2
  197. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  198. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  199. Font: &excelize.Font{Size: 11}})
  200. if err != nil {
  201. return err
  202. }
  203. var drawLeft = func(rowIndex int, value string) error {
  204. //左边1
  205. left1Cell := fmt.Sprintf("A%d", rowIndex)
  206. err := b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("E%d", rowIndex))
  207. if err != nil {
  208. return err
  209. }
  210. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  211. if err != nil {
  212. return err
  213. }
  214. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  215. return nil
  216. }
  217. var drawRight = func(rowIndex int, value string) error {
  218. right1Cell := fmt.Sprintf("F%d", rowIndex)
  219. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("J%d", rowIndex))
  220. if err != nil {
  221. return err
  222. }
  223. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleLeft)
  224. if err != nil {
  225. return err
  226. }
  227. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  228. return nil
  229. }
  230. //第一行
  231. drawLeft(b.Row, "甲方:"+b.Title)
  232. drawRight(b.Row, "乙方:"+b.Content.Supplier)
  233. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  234. //第二行
  235. drawLeft(b.Row+1, "联系人:")
  236. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  237. drawRight(b.Row+1, "联系人:")
  238. b.Excel.SetRowHeight(b.SheetName, b.Row+1, 21)
  239. //第三行
  240. drawLeft(b.Row+2, "时间:"+timeformat)
  241. drawRight(b.Row+2, "时间:"+timeformat)
  242. b.Excel.SetRowHeight(b.SheetName, b.Row+2, 21)
  243. b.Row += 2
  244. return nil
  245. }
  246. func (b *ReportProductExcel) Draws() {
  247. b.drawTitle()
  248. b.drawSubTitles()
  249. b.drawTableTitle()
  250. b.drawTableContent()
  251. b.drawRemark()
  252. }
  253. func NewReportProductBill(f *excelize.File) *ReportProductExcel {
  254. border := []excelize.Border{
  255. {Type: "top", Style: 1, Color: "000000"},
  256. {Type: "left", Style: 1, Color: "000000"},
  257. {Type: "right", Style: 1, Color: "000000"},
  258. {Type: "bottom", Style: 1, Color: "000000"},
  259. }
  260. styleLeft, _ := f.NewStyle(&excelize.Style{
  261. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  262. Border: border,
  263. Font: &excelize.Font{Size: 10},
  264. })
  265. b := &ReportProductExcel{
  266. Title: "原材料采购单",
  267. SheetName: "Sheet1",
  268. Excel: f,
  269. Offset: 0,
  270. AlignCenterStyle: styleLeft,
  271. }
  272. // f.SetColWidth(b.SheetName, "A", "J", 11.5)
  273. f.SetColWidth(b.SheetName, "A", "A", 17)
  274. f.SetColWidth(b.SheetName, "B", "B", 12)
  275. f.SetColWidth(b.SheetName, "C", "H", 10.5)
  276. f.SetColWidth(b.SheetName, "I", "J", 12)
  277. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0.25), excelize.PageMarginRight(0))
  278. return b
  279. }
  280. func (b *ReportProductExcel) FormatToEmpty(str *string) {
  281. if *str == "0" || *str == "0.000" {
  282. *str = ""
  283. }
  284. }