report-excel.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/log"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/xuri/excelize/v2"
  8. )
  9. type ReportExcel struct {
  10. Row int
  11. Title string //标题
  12. SupplierName string //供应商名
  13. TimeRange []string
  14. Excel *excelize.File
  15. SheetName string
  16. AlignCenterStyle int
  17. Content []map[string]interface{}
  18. }
  19. func (b *ReportExcel) drawTitle() error {
  20. b.Row++
  21. // 设置外边距
  22. startCell := fmt.Sprintf("A%d", b.Row)
  23. endCell := fmt.Sprintf("J%d", b.Row)
  24. b.Excel.SetColWidth(b.SheetName, "A", "A", 10)
  25. b.Excel.SetColWidth(b.SheetName, "B", "B", 16)
  26. b.Excel.SetColWidth(b.SheetName, "C", "D", 20)
  27. b.Excel.SetColWidth(b.SheetName, "E", "H", 18)
  28. b.Excel.SetColWidth(b.SheetName, "I", "I", 35)
  29. b.Excel.SetColWidth(b.SheetName, "J", "J", 16)
  30. err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
  31. if err != nil {
  32. return err
  33. }
  34. style, err := b.Excel.NewStyle(&excelize.Style{
  35. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  36. Font: &excelize.Font{Bold: true, Size: 18}})
  37. if err != nil {
  38. return err
  39. }
  40. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  41. if err != nil {
  42. return err
  43. }
  44. b.Excel.SetRowHeight(b.SheetName, b.Row, 35)
  45. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  46. return nil
  47. }
  48. func (b *ReportExcel) drawSubTitles() error {
  49. b.Row++
  50. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  51. Alignment: &excelize.Alignment{Horizontal: "left", 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. left1Cell := fmt.Sprintf("A%d", rowIndex)
  58. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("J%d", rowIndex))
  59. if err != nil {
  60. return err
  61. }
  62. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  63. if err != nil {
  64. return err
  65. }
  66. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  67. return nil
  68. }
  69. timeString := ""
  70. if len(b.TimeRange) == 2 {
  71. timeString = fmt.Sprintf(" ( %s~%s ) ", b.TimeRange[0], b.TimeRange[1])
  72. }
  73. drawLeft(b.Row, "单位名称:"+b.SupplierName+timeString)
  74. b.Excel.SetRowHeight(b.SheetName, b.Row, 35)
  75. return nil
  76. }
  77. func (b *ReportExcel) drawTableTitle() error {
  78. b.Row++
  79. var drawCol = func(prefix string, value string) error {
  80. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  81. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  82. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  83. if err != nil {
  84. return err
  85. }
  86. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  87. if err != nil {
  88. return err
  89. }
  90. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  91. }
  92. drawCol("A", "序号")
  93. drawCol("B", "日期")
  94. drawCol("C", "采购或加工项目")
  95. drawCol("D", "规格")
  96. drawCol("E", "尺寸")
  97. drawCol("F", "数量")
  98. drawCol("G", "单价")
  99. drawCol("H", "金额")
  100. drawCol("I", "产品名称")
  101. drawCol("J", "对账状态")
  102. // b.Excel.SetRowHeight(b.SheetName, b.Row, 35)
  103. return nil
  104. }
  105. func (b *ReportExcel) drawTableContent() error {
  106. b.Row += 2
  107. var DrawRow = func(rowIndex int, values ...string) {
  108. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}
  109. for i, c := range charas {
  110. v := ""
  111. if i < len(values) {
  112. v = values[i]
  113. }
  114. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  115. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  116. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  117. b.Excel.SetRowHeight(b.SheetName, rowIndex, 46)
  118. }
  119. }
  120. lists := b.Content
  121. if len(lists) > 0 {
  122. index := 0
  123. var totalPrice float64 = 0.00
  124. var totalCount int = 0
  125. for _, list := range lists {
  126. data, err := json.Marshal(list)
  127. if err != nil {
  128. log.Error(err)
  129. continue
  130. }
  131. if list["billType"] == "purchase" {
  132. purchase := model.PurchaseBill{}
  133. err = json.Unmarshal(data, &purchase)
  134. if err != nil {
  135. log.Error(err)
  136. panic(err)
  137. }
  138. record := "否"
  139. if purchase.IsRecord == nil {
  140. record = "否"
  141. } else if *purchase.IsRecord {
  142. record = "是"
  143. }
  144. for _, paper := range purchase.Paper {
  145. index++
  146. completeTime := purchase.CompleteTime.Local().Format("2006-01-02")
  147. // 实际完成数
  148. realCount := fmt.Sprintf("%d", paper.ConfirmCount)
  149. b.FormatToEmpty(&realCount)
  150. // 实际金额
  151. // !10.27 如果是固定价格
  152. _realPrice := paper.OrderPrice * float64(paper.ConfirmCount)
  153. if paper.IsFix == nil {
  154. _fix := false
  155. paper.IsFix = &_fix
  156. }
  157. if *paper.IsFix {
  158. _realPrice = paper.OrderPrice * float64(paper.OrderCount)
  159. }
  160. realPrice := fmt.Sprintf("%.3f", _realPrice)
  161. b.FormatToEmpty(&realPrice)
  162. DrawRow(b.Row, fmt.Sprintf("%d", index), completeTime, paper.Name, paper.Norm, fmt.Sprintf("%s*%s", paper.Height, paper.Width), fmt.Sprintf("%d", paper.ConfirmCount), fmt.Sprintf("%.3f", paper.OrderPrice), realPrice, purchase.ProductName, record)
  163. totalPrice += _realPrice
  164. totalCount += paper.ConfirmCount
  165. b.Row++
  166. }
  167. }
  168. if list["billType"] == "produce" {
  169. produce := model.ProduceBill{}
  170. err = json.Unmarshal(data, &produce)
  171. if err != nil {
  172. log.Error(err)
  173. panic(err)
  174. }
  175. record := "否"
  176. if produce.IsRecord == nil {
  177. record = "否"
  178. } else if *produce.IsRecord {
  179. record = "是"
  180. }
  181. for _, pproduce := range produce.Produces {
  182. index++
  183. completeTime := produce.CompleteTime.Local().Format("2006-01-02")
  184. // 实际完成数
  185. realCount := fmt.Sprintf("%d", pproduce.ConfirmCount)
  186. b.FormatToEmpty(&realCount)
  187. // 实际金额
  188. // !10.27 如果是固定价格
  189. _realPrice := pproduce.OrderPrice * float64(pproduce.ConfirmCount)
  190. if pproduce.IsFix == nil {
  191. _fix := false
  192. pproduce.IsFix = &_fix
  193. }
  194. if *pproduce.IsFix {
  195. _realPrice = pproduce.OrderPrice * float64(pproduce.OrderCount)
  196. }
  197. realPrice := fmt.Sprintf("%.3f", _realPrice)
  198. b.FormatToEmpty(&realPrice)
  199. DrawRow(b.Row, fmt.Sprintf("%d", index), completeTime, pproduce.Name, pproduce.Norm, pproduce.PrintSize, fmt.Sprintf("%d", pproduce.ConfirmCount), fmt.Sprintf("%.3f", pproduce.OrderPrice), realPrice, produce.ProductName, record)
  200. totalPrice += _realPrice
  201. totalCount += pproduce.ConfirmCount
  202. b.Row++
  203. }
  204. }
  205. if list["billType"] == "product" {
  206. product := model.ProductBill{}
  207. err = json.Unmarshal(data, &product)
  208. if err != nil {
  209. log.Error(err)
  210. panic(err)
  211. }
  212. record := "否"
  213. if product.IsRecord == nil {
  214. record = "否"
  215. } else if *product.IsRecord {
  216. record = "是"
  217. }
  218. for _, pproduct := range product.Products {
  219. index++
  220. completeTime := product.CompleteTime.Local().Format("2006-01-02")
  221. // 实际完成数
  222. realCount := fmt.Sprintf("%d", pproduct.ConfirmCount)
  223. b.FormatToEmpty(&realCount)
  224. // 实际金额
  225. // !10.27 如果是固定价格
  226. _realPrice := pproduct.OrderPrice * float64(pproduct.ConfirmCount)
  227. if pproduct.IsFix == nil {
  228. _fix := false
  229. pproduct.IsFix = &_fix
  230. }
  231. if *pproduct.IsFix {
  232. _realPrice = pproduct.OrderPrice * float64(pproduct.OrderCount)
  233. }
  234. realPrice := fmt.Sprintf("%.3f", _realPrice)
  235. b.FormatToEmpty(&realPrice)
  236. DrawRow(b.Row, fmt.Sprintf("%d", index), completeTime, pproduct.Name, pproduct.Norm, "-", fmt.Sprintf("%d", pproduct.ConfirmCount), fmt.Sprintf("%.3f", pproduct.OrderPrice), realPrice, product.ProductName, record)
  237. totalPrice += _realPrice
  238. totalCount += pproduct.ConfirmCount
  239. b.Row++
  240. }
  241. }
  242. }
  243. DrawRow(b.Row, "", "", "", "", "", "", "", "", "")
  244. startCell := fmt.Sprintf("%s%d", "A", b.Row)
  245. endCell := fmt.Sprintf("%s%d", "B", b.Row)
  246. FCell := fmt.Sprintf("%s%d", "F", b.Row)
  247. HCell := fmt.Sprintf("%s%d", "H", b.Row)
  248. b.Excel.MergeCell(b.SheetName, startCell, endCell)
  249. b.Excel.SetCellValue(b.SheetName, startCell, "合计")
  250. // 数量汇总
  251. b.Excel.SetCellValue(b.SheetName, FCell, totalCount)
  252. // 金额汇总
  253. b.Excel.SetCellValue(b.SheetName, HCell, fmt.Sprintf("%.3f", totalPrice))
  254. }
  255. return nil
  256. }
  257. func (b *ReportExcel) Draws() {
  258. b.drawTitle()
  259. b.drawSubTitles()
  260. b.drawTableTitle()
  261. b.drawTableContent()
  262. }
  263. func NewReportExcel(f *excelize.File) *ReportExcel {
  264. border := []excelize.Border{
  265. {Type: "top", Style: 1, Color: "000000"},
  266. {Type: "left", Style: 1, Color: "000000"},
  267. {Type: "right", Style: 1, Color: "000000"},
  268. {Type: "bottom", Style: 1, Color: "000000"},
  269. }
  270. styleLeft, _ := f.NewStyle(&excelize.Style{
  271. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  272. Border: border,
  273. })
  274. b := &ReportExcel{
  275. Title: "供应商统计报表",
  276. SupplierName: "",
  277. SheetName: "Sheet1",
  278. Excel: f,
  279. AlignCenterStyle: styleLeft,
  280. }
  281. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  282. return b
  283. }
  284. func (b *ReportExcel) FormatToEmpty(str *string) {
  285. if *str == "0" || *str == "0.000" {
  286. *str = ""
  287. }
  288. }