report-purchase-excel.go 8.3 KB

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