bill-produce-excel.go 9.6 KB

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