bill-produce-excel.go 10.0 KB

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