report-purchase-excel.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "strconv"
  6. _ "image/gif"
  7. _ "image/jpeg"
  8. _ "image/png"
  9. "github.com/xuri/excelize/v2"
  10. )
  11. type ReportPurchaseExcel struct {
  12. Offset int
  13. Row int
  14. Title string //标题
  15. Excel *excelize.File
  16. SheetName string
  17. AlignCenterStyle int
  18. Content *model.PurchaseBill
  19. BudgetCount float64
  20. RealCount float64
  21. }
  22. func (b *ReportPurchaseExcel) drawTitle() error {
  23. marginLeft := excelize.PageMarginLeft(0.15)
  24. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  25. tileIndex := b.Offset + 1
  26. startCell := fmt.Sprintf("A%d", tileIndex)
  27. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("L%d", tileIndex))
  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, tileIndex, 23)
  42. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  43. return nil
  44. }
  45. func (b *ReportPurchaseExcel) drawSubTitles() error {
  46. row := b.Offset + 2
  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("L%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(row, "类别:"+b.Content.Type)
  88. drawRight(row, "单号:"+b.Content.SerialNumber)
  89. b.Excel.SetRowHeight(b.SheetName, row, 21)
  90. //第二行
  91. drawLeft(row+1, "供应商名称:"+b.Content.Supplier)
  92. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  93. drawRight(row+1, "下单时间:"+timeformat)
  94. b.Excel.SetRowHeight(b.SheetName, row+1, 21)
  95. //第三行
  96. drawLeft(row+2, "产品名称:"+b.Content.ProductName)
  97. status := ""
  98. if b.Content.Status == "complete" {
  99. status = fmt.Sprintf("已完成(%d)", b.Content.ConfirmCount)
  100. }
  101. if b.Content.Status == "created" {
  102. status = "进行中"
  103. }
  104. drawRight(row+2, "状态:"+status)
  105. b.Excel.SetRowHeight(b.SheetName, row+2, 21)
  106. return nil
  107. }
  108. func (b *ReportPurchaseExcel) drawTableTitle() error {
  109. row := b.Offset + 5
  110. //A采购项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I
  111. var drawCol = func(prefix string, value string) error {
  112. left1Cell := fmt.Sprintf("%s%d", prefix, row)
  113. left2Cell := fmt.Sprintf("%s%d", prefix, row+1)
  114. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  115. if err != nil {
  116. return err
  117. }
  118. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  119. if err != nil {
  120. return err
  121. }
  122. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  123. }
  124. var drawCol2 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
  125. left1Cell := fmt.Sprintf("%s%d", prefix1, row)
  126. left2Cell := fmt.Sprintf("%s%d", prefix2, row)
  127. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  128. if err != nil {
  129. return err
  130. }
  131. if err != nil {
  132. fmt.Println(err)
  133. return err
  134. }
  135. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  136. if err != nil {
  137. return err
  138. }
  139. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  140. val2Cel := fmt.Sprintf("%s%d", prefix1, row+1)
  141. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  142. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  143. val3Cel := fmt.Sprintf("%s%d", prefix2, row+1)
  144. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  145. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  146. return nil
  147. }
  148. drawCol("A", "采购项目")
  149. drawCol("B", "规格(克)")
  150. drawCol("E", "数量")
  151. drawCol("F", "完成数量")
  152. drawCol2("C", "D", "尺寸(mm)", "长", "宽")
  153. drawCol2("C", "D", "尺寸(mm)", "长", "宽")
  154. unit1 := "-"
  155. unit2 := "-"
  156. // ??? for ragne
  157. if len(b.Content.Paper) > 0 {
  158. if b.Content.Paper[0].PriceUnit != "" {
  159. unit1 = b.Content.Paper[0].PriceUnit
  160. } else if b.Content.Paper[0].Price2Unit != "" {
  161. unit2 = b.Content.Paper[0].Price2Unit
  162. } else {
  163. unit1 = "元/张"
  164. unit2 = "元/吨"
  165. }
  166. if b.Content.Paper[0].PriceUnit != "" && b.Content.Paper[0].Price2Unit != "" {
  167. unit1 = b.Content.Paper[0].PriceUnit
  168. unit2 = b.Content.Paper[0].Price2Unit
  169. }
  170. }
  171. drawCol2("G", "H", "单价", unit1, unit2)
  172. drawCol("I", "预算金额")
  173. drawCol("J", "实际金额")
  174. drawCol("K", "交货时间")
  175. drawCol("L", "备注")
  176. return nil
  177. }
  178. func (b *ReportPurchaseExcel) drawTableContent() error {
  179. row := b.Offset + 7
  180. b.Row = row
  181. var DrawRow = func(rowIndex int, values ...string) {
  182. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}
  183. for i, c := range charas {
  184. v := ""
  185. if i < len(values) {
  186. v = values[i]
  187. }
  188. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  189. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  190. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  191. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  192. }
  193. }
  194. papers := b.Content.Paper
  195. if len(papers) > 0 {
  196. for _, paper := range papers {
  197. deliveryTime := paper.DeliveryTime.Local().Format("2006-01-02")
  198. confirmCount := "-"
  199. realAmount := "-"
  200. price, err := strconv.ParseFloat(paper.Price, 64)
  201. if err != nil {
  202. price = 0.00
  203. }
  204. // 预算金额
  205. budgetAmount := fmt.Sprintf("%.2f", float64(paper.Count)*price)
  206. b.FormatToEmpty(&budgetAmount)
  207. b.BudgetCount += float64(paper.Count) * price
  208. // 实际完成数
  209. confirmCount = fmt.Sprintf("%d", b.Content.ConfirmCount)
  210. b.FormatToEmpty(&confirmCount)
  211. // 实际金额
  212. realAmount = fmt.Sprintf("%.2f", float64(b.Content.ConfirmCount)*price)
  213. b.FormatToEmpty(&realAmount)
  214. b.RealCount += float64(b.Content.ConfirmCount) * price
  215. DrawRow(row, paper.Name, paper.Norm, paper.Height, paper.Width, fmt.Sprintf("%d", paper.Count), confirmCount, paper.Price, paper.Price2, budgetAmount, realAmount, deliveryTime, paper.Remark)
  216. row++
  217. b.Row++
  218. }
  219. }
  220. return nil
  221. }
  222. func (b *ReportPurchaseExcel) Draws() {
  223. b.drawTitle()
  224. b.drawSubTitles()
  225. b.drawTableTitle()
  226. b.drawTableContent()
  227. }
  228. func NewReportPurchaseExcel(f *excelize.File) *ReportPurchaseExcel {
  229. border := []excelize.Border{
  230. {Type: "top", Style: 1, Color: "000000"},
  231. {Type: "left", Style: 1, Color: "000000"},
  232. {Type: "right", Style: 1, Color: "000000"},
  233. {Type: "bottom", Style: 1, Color: "000000"},
  234. }
  235. styleLeft, _ := f.NewStyle(&excelize.Style{
  236. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  237. Border: border,
  238. Font: &excelize.Font{Size: 10},
  239. })
  240. b := &ReportPurchaseExcel{
  241. Title: "原材料采购单",
  242. SheetName: "Sheet1",
  243. Excel: f,
  244. Offset: 0,
  245. AlignCenterStyle: styleLeft,
  246. }
  247. f.SetColWidth(b.SheetName, "A", "A", 12)
  248. f.SetColWidth(b.SheetName, "B", "K", 9.5)
  249. f.SetColWidth(b.SheetName, "L", "L", 10)
  250. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  251. return b
  252. }
  253. func (b *ReportPurchaseExcel) FormatToEmpty(str *string) {
  254. if *str == "0" || *str == "0.00" {
  255. *str = ""
  256. }
  257. }