report-purchase-excel.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "math"
  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. BudgetAmount float64
  20. RealAmount float64
  21. }
  22. func (b *ReportPurchaseExcel) drawTitle() error {
  23. b.Row++
  24. marginLeft := excelize.PageMarginLeft(0.15)
  25. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  26. startCell := fmt.Sprintf("A%d", b.Row)
  27. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("L%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 *ReportPurchaseExcel) 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("F%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("G%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(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 = "已完成"
  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 *ReportPurchaseExcel) drawTableTitle() error {
  111. b.Row++
  112. //A采购项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I
  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. var drawCol2 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
  127. left1Cell := fmt.Sprintf("%s%d", prefix1, b.Row)
  128. left2Cell := fmt.Sprintf("%s%d", prefix2, b.Row)
  129. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  130. if err != nil {
  131. return err
  132. }
  133. if err != nil {
  134. fmt.Println(err)
  135. return err
  136. }
  137. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  138. if err != nil {
  139. return err
  140. }
  141. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  142. val2Cel := fmt.Sprintf("%s%d", prefix1, b.Row+1)
  143. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  144. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  145. val3Cel := fmt.Sprintf("%s%d", prefix2, b.Row+1)
  146. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  147. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  148. return nil
  149. }
  150. drawCol("A", "采购项目")
  151. drawCol("B", "规格(克)")
  152. drawCol("E", "数量")
  153. drawCol("F", "完成数量")
  154. drawCol2("C", "D", "尺寸(mm)", "长", "宽")
  155. drawCol2("C", "D", "尺寸(mm)", "长", "宽")
  156. unit1 := "-"
  157. unit2 := "-"
  158. // ??? for ragne
  159. if len(b.Content.Paper) > 0 {
  160. if b.Content.Paper[0].PriceUnit != "" {
  161. unit1 = b.Content.Paper[0].PriceUnit
  162. } else if b.Content.Paper[0].Price2Unit != "" {
  163. unit2 = b.Content.Paper[0].Price2Unit
  164. } else {
  165. unit1 = "元/张"
  166. unit2 = "元/吨"
  167. }
  168. if b.Content.Paper[0].PriceUnit != "" && b.Content.Paper[0].Price2Unit != "" {
  169. unit1 = b.Content.Paper[0].PriceUnit
  170. unit2 = b.Content.Paper[0].Price2Unit
  171. }
  172. }
  173. drawCol2("G", "H", "单价", unit1, unit2)
  174. drawCol("I", "预算金额")
  175. drawCol("J", "实际金额")
  176. drawCol("K", "交货时间")
  177. drawCol("L", "备注")
  178. return nil
  179. }
  180. func (b *ReportPurchaseExcel) drawTableContent() error {
  181. // row := b.Offset + 7
  182. // b.Row = row
  183. b.Row += 2
  184. var DrawRow = func(rowIndex int, values ...string) {
  185. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}
  186. for i, c := range charas {
  187. v := ""
  188. if i < len(values) {
  189. v = values[i]
  190. }
  191. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  192. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  193. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  194. var magN float64 = 1
  195. lenv := len([]rune(v))
  196. magN = math.Ceil(float64(lenv) / 10)
  197. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21*magN)
  198. }
  199. }
  200. papers := b.Content.Paper
  201. if len(papers) > 0 {
  202. for _, paper := range papers {
  203. deliveryTime := paper.DeliveryTime.Local().Format("2006-01-02")
  204. realCount := ""
  205. realPrice := ""
  206. price := fmt.Sprintf("%.3f", paper.OrderPrice)
  207. price2 := fmt.Sprintf("%.3f", paper.Price2)
  208. // 预算金额
  209. budgetAmount := fmt.Sprintf("%.3f", paper.OrderPrice*float64(paper.OrderCount))
  210. b.FormatToEmpty(&budgetAmount)
  211. // 实际完成数
  212. realCount = fmt.Sprintf("%d", paper.ConfirmCount)
  213. b.FormatToEmpty(&realCount)
  214. // 实际金额
  215. realPrice = fmt.Sprintf("%.3f", paper.OrderPrice*float64(paper.ConfirmCount))
  216. b.FormatToEmpty(&realPrice)
  217. DrawRow(b.Row, paper.Name, paper.Norm, paper.Height, paper.Width, fmt.Sprintf("%d", paper.OrderCount), realCount, price, price2, budgetAmount, realPrice, deliveryTime, paper.Remark)
  218. b.Row++
  219. b.BudgetAmount += paper.OrderPrice * float64(paper.OrderCount)
  220. b.RealAmount += paper.OrderPrice * float64(paper.ConfirmCount)
  221. }
  222. }
  223. return nil
  224. }
  225. func (b *ReportPurchaseExcel) Draws() {
  226. b.drawTitle()
  227. b.drawSubTitles()
  228. b.drawTableTitle()
  229. b.drawTableContent()
  230. }
  231. func NewReportPurchaseExcel(f *excelize.File) *ReportPurchaseExcel {
  232. border := []excelize.Border{
  233. {Type: "top", Style: 1, Color: "000000"},
  234. {Type: "left", Style: 1, Color: "000000"},
  235. {Type: "right", Style: 1, Color: "000000"},
  236. {Type: "bottom", Style: 1, Color: "000000"},
  237. }
  238. styleLeft, _ := f.NewStyle(&excelize.Style{
  239. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  240. Border: border,
  241. Font: &excelize.Font{Size: 10},
  242. })
  243. b := &ReportPurchaseExcel{
  244. Title: "原材料采购单",
  245. SheetName: "Sheet1",
  246. Excel: f,
  247. Offset: 0,
  248. AlignCenterStyle: styleLeft,
  249. }
  250. // f.SetColWidth(b.SheetName, "A", "A", 12)
  251. // f.SetColWidth(b.SheetName, "B", "K", 9.5)
  252. // f.SetColWidth(b.SheetName, "L", "L", 10)
  253. f.SetColWidth(b.SheetName, "A", "A", 15)
  254. f.SetColWidth(b.SheetName, "B", "B", 15)
  255. f.SetColWidth(b.SheetName, "C", "G", 7)
  256. // f.SetColWidth(b.SheetName, "C", "H", 9.5)
  257. f.SetColWidth(b.SheetName, "H", "K", 10)
  258. f.SetColWidth(b.SheetName, "L", "L", 11)
  259. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  260. return b
  261. }
  262. func (b *ReportPurchaseExcel) FormatToEmpty(str *string) {
  263. if *str == "0" || *str == "0.000" {
  264. *str = ""
  265. }
  266. }