bill-process-excel.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. _ "image/gif"
  6. _ "image/jpeg"
  7. _ "image/png"
  8. "github.com/xuri/excelize/v2"
  9. )
  10. type ProcessBillExcel struct {
  11. Offset int
  12. Row int
  13. Title string //标题
  14. Excel *excelize.File
  15. SheetName string
  16. AlignCenterStyle int
  17. Content *model.PurchaseBill
  18. Signatures []*model.Signature
  19. }
  20. func (b *ProcessBillExcel) drawTitle() error {
  21. tileIndex := b.Offset + 1
  22. startCell := fmt.Sprintf("A%d", tileIndex)
  23. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("I%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 *ProcessBillExcel) 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("I%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 *ProcessBillExcel) drawTableTitle() error {
  105. row := b.Offset + 5
  106. // 品名 规格 数量 单位 单价 金额
  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. drawCol("A", "采购项目")
  121. drawCol("B", "规格")
  122. drawCol("C", "数量")
  123. drawCol("D", "单位")
  124. drawCol("E", "单价")
  125. drawCol("F", "预算金额")
  126. drawCol("G", "实际金额")
  127. drawCol("H", "交货时间")
  128. drawCol("I", "备注")
  129. return nil
  130. }
  131. func (b *ProcessBillExcel) drawTableContent() error {
  132. row := b.Offset + 7
  133. b.Row = row
  134. var DrawRow = func(rowIndex int, values ...string) {
  135. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"}
  136. for i, c := range charas {
  137. v := ""
  138. if i < len(values) {
  139. v = values[i]
  140. }
  141. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  142. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  143. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  144. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  145. }
  146. }
  147. process := b.Content.Process
  148. if len(process) > 0 {
  149. for _, ps := range process {
  150. deliveryTime := ps.DeliveryTime.Local().Format("2006-01-02")
  151. confirmCount := "-"
  152. realAmount := "-"
  153. // 预算金额
  154. budgetAmount := fmt.Sprintf("%.2f", float64(ps.Count)*ps.Price)
  155. b.FormatToEmpty(&budgetAmount)
  156. if b.Content.Status == "complete" {
  157. // 实际完成数
  158. confirmCount = fmt.Sprintf("%d", b.Content.ConfirmCount)
  159. b.FormatToEmpty(&confirmCount)
  160. // 实际金额
  161. realAmount = fmt.Sprintf("%.2f", float64(b.Content.ConfirmCount)*ps.Price)
  162. b.FormatToEmpty(&realAmount)
  163. }
  164. // 采购项目 规格 数量 单位 单价 预算金额 实际金额 交货时间 备注
  165. count := fmt.Sprintf("%d", ps.Count)
  166. price := fmt.Sprintf("%.2f", ps.Price)
  167. b.FormatToEmpty(&price)
  168. DrawRow(row, ps.Name, ps.Norm, count, ps.Unit, price, budgetAmount, realAmount, deliveryTime, ps.Remark)
  169. row++
  170. b.Row++
  171. }
  172. }
  173. return nil
  174. }
  175. func (b *ProcessBillExcel) drawTableFooter() error {
  176. left1Cell := fmt.Sprintf("A%d", b.Row)
  177. border := []excelize.Border{
  178. {Type: "top", Style: 1, Color: "000000"},
  179. {Type: "left", Style: 1, Color: "000000"},
  180. {Type: "right", Style: 1, Color: "000000"},
  181. {Type: "bottom", Style: 1, Color: "000000"},
  182. }
  183. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  184. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  185. Border: border,
  186. })
  187. b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  188. b.Excel.SetCellValue(b.SheetName, left1Cell, "送货地址:")
  189. addCel := fmt.Sprintf("B%d", b.Row)
  190. b.Excel.MergeCell(b.SheetName, addCel, fmt.Sprintf("E%d", b.Row))
  191. b.Excel.SetCellStyle(b.SheetName, addCel, fmt.Sprintf("E%d", b.Row), styleLeft)
  192. b.Excel.SetCellValue(b.SheetName, addCel, b.Content.SendTo)
  193. sureCel := fmt.Sprintf("F%d", b.Row)
  194. b.Excel.MergeCell(b.SheetName, sureCel, fmt.Sprintf("I%d", b.Row))
  195. b.Excel.SetCellStyle(b.SheetName, sureCel, fmt.Sprintf("I%d", b.Row), styleLeft)
  196. b.Excel.SetCellValue(b.SheetName, sureCel, "供应商签字:")
  197. b.Excel.SetRowHeight(b.SheetName, b.Row, 30)
  198. return nil
  199. }
  200. func (b *ProcessBillExcel) drawRemark() error {
  201. // 填备注
  202. remarkTitleCell := fmt.Sprintf("A%d", b.Row)
  203. b.Excel.SetCellValue(b.SheetName, remarkTitleCell, "备注:")
  204. b.Row++
  205. remarkContentScell := fmt.Sprintf("A%d", b.Row)
  206. // 预留五行备注内容 // TODO 获取内容换行符个数然后动态确定内容占用多少行单元格
  207. b.Row += 5
  208. remarkContentEcell := fmt.Sprintf("L%d", b.Row)
  209. b.Excel.MergeCell(b.SheetName, remarkContentScell, remarkContentEcell)
  210. b.Excel.SetCellValue(b.SheetName, remarkContentScell, b.Content.Remark)
  211. return nil
  212. }
  213. func (b *ProcessBillExcel) drawTableSignature() error {
  214. b.Row += 2
  215. style1, _ := b.Excel.NewStyle(&excelize.Style{
  216. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  217. // Border: border,
  218. })
  219. fontCell := fmt.Sprintf("E%d", b.Row)
  220. imageCell1 := fmt.Sprintf("F%d", b.Row)
  221. imageCell2 := fmt.Sprintf("H%d", b.Row)
  222. eRow := b.Row + 2
  223. b.Excel.MergeCell(b.SheetName, fontCell, fmt.Sprintf("E%d", eRow))
  224. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  225. b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
  226. b.Excel.MergeCell(b.SheetName, imageCell1, fmt.Sprintf("F%d", eRow))
  227. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  228. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  229. b.Excel.MergeCell(b.SheetName, imageCell2, fmt.Sprintf("H%d", eRow))
  230. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  231. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  232. // 状态为已审核时,签字
  233. // `{
  234. // "x_scale": 0.12,
  235. // "y_scale": 0.12,
  236. // "x_offset": 30,
  237. // "print_obj": true,
  238. // "lock_aspect_ratio": false,
  239. // "locked": false,
  240. // "positioning": "oncell"
  241. // }`
  242. if b.Content.Reviewed == 1 {
  243. imageCells := []string{imageCell1, imageCell2}
  244. if len(b.Signatures) > 0 {
  245. for k, sign := range b.Signatures {
  246. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Path, sign.Format)
  247. }
  248. }
  249. }
  250. return nil
  251. }
  252. func (b *ProcessBillExcel) Draws() {
  253. b.drawTitle()
  254. b.drawSubTitles()
  255. b.drawTableTitle()
  256. b.drawTableContent()
  257. b.drawTableFooter()
  258. b.drawRemark()
  259. b.drawTableSignature()
  260. }
  261. func NewProcessBill(f *excelize.File) *ProcessBillExcel {
  262. border := []excelize.Border{
  263. {Type: "top", Style: 1, Color: "000000"},
  264. {Type: "left", Style: 1, Color: "000000"},
  265. {Type: "right", Style: 1, Color: "000000"},
  266. {Type: "bottom", Style: 1, Color: "000000"},
  267. }
  268. styleLeft, _ := f.NewStyle(&excelize.Style{
  269. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  270. Border: border,
  271. Font: &excelize.Font{Size: 10},
  272. })
  273. b := &ProcessBillExcel{
  274. Title: "原材料采购单",
  275. SheetName: "Sheet1",
  276. Excel: f,
  277. Offset: 0,
  278. AlignCenterStyle: styleLeft,
  279. Signatures: make([]*model.Signature, 0),
  280. }
  281. f.SetColWidth(b.SheetName, "A", "I", 14)
  282. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  283. return b
  284. }
  285. func (b *ProcessBillExcel) FormatToEmpty(str *string) {
  286. if *str == "0" || *str == "0.00" {
  287. *str = ""
  288. }
  289. }
  290. func (b *ProcessBillExcel) PrintPurchType() string {
  291. return "process"
  292. }
  293. func (b *ProcessBillExcel) SetContent(content *model.PurchaseBill) {
  294. b.Content = content
  295. }
  296. func (b *ProcessBillExcel) SetTitle(title string) {
  297. b.Title = title
  298. }
  299. func (b *ProcessBillExcel) SetSignatures(sign []*model.Signature) {
  300. b.Signatures = sign
  301. }