report-purchase-excel.go 8.3 KB

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