bill-produce-excel.go 10 KB

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