bill-produce-excel.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. startRow := b.Row
  164. endRow := b.Row
  165. for _, produce := range produces {
  166. confirmCount := "-"
  167. price := produce.Price
  168. priceStr := fmt.Sprintf("%.3f", price)
  169. b.FormatToEmpty(&priceStr)
  170. // 预算金额
  171. orderRealPrice := "-"
  172. // 实际金额
  173. realPrice := "-"
  174. if b.Content.Status == "complete" {
  175. // 实际完成数
  176. confirmCount = fmt.Sprintf("%d", produce.ConfirmCount)
  177. b.FormatToEmpty(&confirmCount)
  178. }
  179. deliveryTime := produce.DeliveryTime.Local().Format("2006-01-02")
  180. DrawRow(row, produce.Name, produce.Norm, produce.Paper, produce.PaperSize, produce.PrintSize, fmt.Sprintf("%d", produce.OrderCount), confirmCount, priceStr, orderRealPrice, realPrice, deliveryTime, produce.Remark)
  181. endRow = b.Row
  182. row++
  183. b.Row++
  184. }
  185. // 多个工序 合并预算价格、实际价格
  186. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("I%d", startRow), fmt.Sprintf("I%d", endRow))
  187. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("I%d", startRow), b.Content.BudgetAmount)
  188. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("J%d", startRow), fmt.Sprintf("J%d", endRow))
  189. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("J%d", startRow), b.Content.RealAmount)
  190. }
  191. return nil
  192. }
  193. func (b *ProduceBillExcel) drawTableFooter() error {
  194. // row := b.Offset + 8
  195. // b.Row++
  196. row := b.Row
  197. left1Cell := fmt.Sprintf("A%d", row)
  198. border := []excelize.Border{
  199. {Type: "top", Style: 1, Color: "000000"},
  200. {Type: "left", Style: 1, Color: "000000"},
  201. {Type: "right", Style: 1, Color: "000000"},
  202. {Type: "bottom", Style: 1, Color: "000000"},
  203. }
  204. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  205. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  206. Border: border,
  207. })
  208. b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  209. b.Excel.SetCellValue(b.SheetName, left1Cell, "送货地址")
  210. addCel := fmt.Sprintf("B%d", row)
  211. b.Excel.MergeCell(b.SheetName, addCel, fmt.Sprintf("E%d", row))
  212. b.Excel.SetCellStyle(b.SheetName, addCel, fmt.Sprintf("E%d", row), styleLeft)
  213. b.Excel.SetCellValue(b.SheetName, addCel, b.Content.SendTo)
  214. sureCel := fmt.Sprintf("F%d", row)
  215. b.Excel.MergeCell(b.SheetName, sureCel, fmt.Sprintf("L%d", row))
  216. b.Excel.SetCellStyle(b.SheetName, sureCel, fmt.Sprintf("L%d", row), styleLeft)
  217. b.Excel.SetCellValue(b.SheetName, sureCel, "供应商签字:")
  218. b.Excel.SetRowHeight(b.SheetName, row, 21)
  219. return nil
  220. }
  221. func (b *ProduceBillExcel) drawTableSignature() error {
  222. // row := b.Offset + 11
  223. b.Row += 2
  224. row := b.Row
  225. style1, _ := b.Excel.NewStyle(&excelize.Style{
  226. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  227. // Border: border,
  228. })
  229. // 制单人
  230. billUserCell := fmt.Sprintf("A%d", row+1)
  231. b.Excel.SetCellValue(b.SheetName, billUserCell, "制单人:")
  232. billUservCell := fmt.Sprintf("B%d", row+1)
  233. b.Excel.SetCellValue(b.SheetName, billUservCell, b.Content.UserName)
  234. fontCell := fmt.Sprintf("H%d", row)
  235. imageCell1 := fmt.Sprintf("I%d", row)
  236. imageCell2 := fmt.Sprintf("K%d", row)
  237. b.Excel.MergeCell(b.SheetName, fontCell, fmt.Sprintf("H%d", row+2))
  238. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  239. b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
  240. // 签字图片1 I10-J12
  241. b.Excel.MergeCell(b.SheetName, imageCell1, fmt.Sprintf("J%d", row+2))
  242. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  243. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  244. // 签字图片2 K10-L12
  245. b.Excel.MergeCell(b.SheetName, imageCell2, fmt.Sprintf("L%d", row+2))
  246. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  247. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  248. // 状态为已审核时,签字
  249. if b.Content.Reviewed == 1 {
  250. imageCells := []string{imageCell1, imageCell2}
  251. if len(b.Signatures) > 0 {
  252. for k, sign := range b.Signatures {
  253. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Path, sign.Format)
  254. }
  255. }
  256. }
  257. b.Excel.SetRowHeight(b.SheetName, row, 21)
  258. return nil
  259. }
  260. func (b *ProduceBillExcel) Draws() {
  261. b.drawTitle()
  262. b.drawSubTitles()
  263. b.drawTableTitle()
  264. b.drawTableContent()
  265. b.drawTableFooter()
  266. b.drawTableSignature()
  267. }
  268. func NewProduceBill(f *excelize.File) *ProduceBillExcel {
  269. border := []excelize.Border{
  270. {Type: "top", Style: 1, Color: "000000"},
  271. {Type: "left", Style: 1, Color: "000000"},
  272. {Type: "right", Style: 1, Color: "000000"},
  273. {Type: "bottom", Style: 1, Color: "000000"},
  274. }
  275. styleLeft, _ := f.NewStyle(&excelize.Style{
  276. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  277. Border: border,
  278. })
  279. b := &ProduceBillExcel{
  280. Title: "中鱼互动加工单",
  281. SheetName: "Sheet1",
  282. Excel: f,
  283. Offset: 0,
  284. AlignCenterStyle: styleLeft,
  285. Signatures: make([]*model.Signature, 0),
  286. }
  287. // f.SetColWidth(b.SheetName, "A", "A", 17)
  288. // f.SetColWidth(b.SheetName, "B", "B", 13)
  289. // f.SetColWidth(b.SheetName, "C", "G", 9.5)
  290. // f.SetColWidth(b.SheetName, "H", "I", 12)
  291. f.SetColWidth(b.SheetName, "A", "A", 12)
  292. f.SetColWidth(b.SheetName, "B", "K", 9.5)
  293. // f.SetColWidth(b.SheetName, "I", "J", 8)
  294. f.SetColWidth(b.SheetName, "L", "L", 10)
  295. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  296. return b
  297. }
  298. func (b *ProduceBillExcel) FormatToEmpty(str *string) {
  299. if *str == "0" || *str == "0.000" {
  300. *str = ""
  301. }
  302. }