report-produce-excel.go 8.7 KB

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