bill-process-excel.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "regexp"
  6. _ "image/gif"
  7. _ "image/jpeg"
  8. _ "image/png"
  9. "github.com/xuri/excelize/v2"
  10. )
  11. type ProcessBillExcel struct {
  12. Offset int
  13. Row int
  14. Title string //标题
  15. Excel *excelize.File
  16. SheetName string
  17. AlignCenterStyle int
  18. Content *model.PurchaseBill
  19. Signatures []*model.Signature
  20. }
  21. func (b *ProcessBillExcel) drawTitle() error {
  22. marginLeft := excelize.PageMarginLeft(0.3)
  23. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  24. tileIndex := b.Offset + 1
  25. startCell := fmt.Sprintf("A%d", tileIndex)
  26. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("J%d", tileIndex))
  27. if err != nil {
  28. return err
  29. }
  30. style, err := b.Excel.NewStyle(&excelize.Style{
  31. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  32. Font: &excelize.Font{Bold: true, Size: 18}})
  33. if err != nil {
  34. return err
  35. }
  36. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  37. if err != nil {
  38. return err
  39. }
  40. b.Excel.SetRowHeight(b.SheetName, tileIndex, 23)
  41. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  42. return nil
  43. }
  44. func (b *ProcessBillExcel) drawSubTitles() error {
  45. row := b.Offset + 2
  46. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  47. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  48. Font: &excelize.Font{Size: 11}})
  49. if err != nil {
  50. return err
  51. }
  52. styleRight, err := b.Excel.NewStyle(&excelize.Style{
  53. Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
  54. Font: &excelize.Font{Size: 11}})
  55. if err != nil {
  56. return err
  57. }
  58. var drawLeft = func(rowIndex int, value string) error {
  59. //左边1
  60. left1Cell := fmt.Sprintf("A%d", rowIndex)
  61. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("E%d", rowIndex))
  62. if err != nil {
  63. return err
  64. }
  65. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  66. if err != nil {
  67. return err
  68. }
  69. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  70. return nil
  71. }
  72. var drawRight = func(rowIndex int, value string) error {
  73. right1Cell := fmt.Sprintf("F%d", rowIndex)
  74. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("J%d", rowIndex))
  75. if err != nil {
  76. return err
  77. }
  78. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  79. if err != nil {
  80. return err
  81. }
  82. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  83. return nil
  84. }
  85. //第一行
  86. drawLeft(row, "类别:"+b.Content.Type)
  87. drawRight(row, "单号:"+b.Content.SerialNumber)
  88. b.Excel.SetRowHeight(b.SheetName, row, 21)
  89. //第二行
  90. drawLeft(row+1, "供应商名称:"+b.Content.Supplier)
  91. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  92. drawRight(row+1, "下单时间:"+timeformat)
  93. b.Excel.SetRowHeight(b.SheetName, row+1, 21)
  94. //第三行
  95. drawLeft(row+2, "产品名称:"+b.Content.ProductName)
  96. status := ""
  97. if b.Content.Status == "complete" {
  98. status = fmt.Sprintf("已完成(%d)", b.Content.ConfirmCount)
  99. }
  100. if b.Content.Status == "created" {
  101. status = "进行中"
  102. }
  103. drawRight(row+2, "状态:"+status)
  104. b.Excel.SetRowHeight(b.SheetName, row+2, 21)
  105. return nil
  106. }
  107. func (b *ProcessBillExcel) drawTableTitle() error {
  108. row := b.Offset + 5
  109. // 品名 规格 数量 单位 单价 金额
  110. var drawCol = func(prefix string, value string) error {
  111. left1Cell := fmt.Sprintf("%s%d", prefix, row)
  112. left2Cell := fmt.Sprintf("%s%d", prefix, row+1)
  113. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  114. if err != nil {
  115. return err
  116. }
  117. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  118. if err != nil {
  119. return err
  120. }
  121. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  122. }
  123. // a采购项目 b规格 c下单数量 d单位 e完成数量 f单价 g预算金额 h实际金额 i交货时间 j备注
  124. drawCol("A", "采购项目")
  125. drawCol("B", "规格")
  126. drawCol("C", "下单数量")
  127. drawCol("D", "单位")
  128. drawCol("E", "完成数量")
  129. drawCol("F", "单价")
  130. drawCol("G", "预算金额")
  131. drawCol("H", "实际金额")
  132. drawCol("I", "交货时间")
  133. drawCol("J", "备注")
  134. return nil
  135. }
  136. func (b *ProcessBillExcel) drawTableContent() error {
  137. row := b.Offset + 7
  138. b.Row = row
  139. var DrawRow = func(rowIndex int, values ...string) {
  140. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}
  141. for i, c := range charas {
  142. v := ""
  143. if i < len(values) {
  144. v = values[i]
  145. }
  146. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  147. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  148. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  149. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  150. }
  151. }
  152. ps := b.Content.Process
  153. if ps != nil {
  154. deliveryTime := ps.DeliveryTime.Local().Format("2006-01-02")
  155. confirmCount := "-"
  156. realAmount := "-"
  157. // 预算金额
  158. budgetAmount := fmt.Sprintf("%.2f", b.Content.BudgetAmount)
  159. b.FormatToEmpty(&budgetAmount)
  160. // 实际完成数
  161. confirmCount = fmt.Sprintf("%d", ps.ConfirmCount)
  162. b.FormatToEmpty(&confirmCount)
  163. // 实际金额
  164. realAmount = fmt.Sprintf("%.2f", b.Content.RealAmount)
  165. b.FormatToEmpty(&realAmount)
  166. // a采购项目 b规格 c下单数量 d单位 e完成数量 f单价 g预算金额 h实际金额 i交货时间 j备注
  167. orderCount := fmt.Sprintf("%d", ps.OrderCount)
  168. price := fmt.Sprintf("%.2f", ps.Price)
  169. b.FormatToEmpty(&price)
  170. DrawRow(row, ps.Name, ps.Norm, orderCount, ps.Unit, confirmCount, price, budgetAmount, realAmount, deliveryTime, ps.Remark)
  171. row++
  172. b.Row++
  173. }
  174. return nil
  175. }
  176. func (b *ProcessBillExcel) drawTableFooter() error {
  177. left1Cell := fmt.Sprintf("A%d", b.Row)
  178. border := []excelize.Border{
  179. {Type: "top", Style: 1, Color: "000000"},
  180. {Type: "left", Style: 1, Color: "000000"},
  181. {Type: "right", Style: 1, Color: "000000"},
  182. {Type: "bottom", Style: 1, Color: "000000"},
  183. }
  184. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  185. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  186. Border: border,
  187. })
  188. b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  189. b.Excel.SetCellValue(b.SheetName, left1Cell, "送货地址:")
  190. addCel := fmt.Sprintf("B%d", b.Row)
  191. b.Excel.MergeCell(b.SheetName, addCel, fmt.Sprintf("E%d", b.Row))
  192. b.Excel.SetCellStyle(b.SheetName, addCel, fmt.Sprintf("E%d", b.Row), styleLeft)
  193. b.Excel.SetCellValue(b.SheetName, addCel, b.Content.SendTo)
  194. sureCel := fmt.Sprintf("F%d", b.Row)
  195. b.Excel.MergeCell(b.SheetName, sureCel, fmt.Sprintf("J%d", b.Row))
  196. b.Excel.SetCellStyle(b.SheetName, sureCel, fmt.Sprintf("J%d", b.Row), styleLeft)
  197. b.Excel.SetCellValue(b.SheetName, sureCel, "供应商签字:")
  198. b.Excel.SetRowHeight(b.SheetName, b.Row, 30)
  199. return nil
  200. }
  201. func (b *ProcessBillExcel) drawRemark() error {
  202. // 填备注
  203. b.Row++
  204. remarkTitleCell := fmt.Sprintf("A%d", b.Row)
  205. b.Excel.SetCellValue(b.SheetName, remarkTitleCell, "备注:")
  206. b.Row++
  207. remarkContentScell := fmt.Sprintf("A%d", b.Row)
  208. // 预留五行备注内容 // TODO 获取内容换行符个数然后动态确定内容占用多少行单元格
  209. reg := regexp.MustCompile(`\n`)
  210. remarkRowNum := len(reg.FindAllString(b.Content.Remark, -1)) + 1
  211. b.Row += remarkRowNum
  212. remarkContentEcell := fmt.Sprintf("J%d", b.Row)
  213. b.Excel.MergeCell(b.SheetName, remarkContentScell, remarkContentEcell)
  214. b.Excel.SetCellValue(b.SheetName, remarkContentScell, b.Content.Remark)
  215. return nil
  216. }
  217. func (b *ProcessBillExcel) drawTableSignature() error {
  218. b.Row += 2
  219. style1, _ := b.Excel.NewStyle(&excelize.Style{
  220. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  221. // Border: border,
  222. })
  223. // 制单人
  224. billUserCell := fmt.Sprintf("A%d", b.Row+1)
  225. b.Excel.SetCellValue(b.SheetName, billUserCell, "制单人:")
  226. billUservCell := fmt.Sprintf("B%d", b.Row+1)
  227. b.Excel.SetCellValue(b.SheetName, billUservCell, b.Content.UserName)
  228. fontCell := fmt.Sprintf("F%d", b.Row)
  229. imageCell1 := fmt.Sprintf("G%d", b.Row)
  230. imageCell2 := fmt.Sprintf("I%d", b.Row)
  231. eRow := b.Row + 2
  232. b.Excel.MergeCell(b.SheetName, fontCell, fmt.Sprintf("F%d", eRow))
  233. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  234. b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
  235. b.Excel.MergeCell(b.SheetName, imageCell1, fmt.Sprintf("H%d", eRow))
  236. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  237. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  238. b.Excel.MergeCell(b.SheetName, imageCell2, fmt.Sprintf("J%d", eRow))
  239. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  240. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  241. // 状态为已审核时,签字
  242. // `{
  243. // "x_scale": 0.12,
  244. // "y_scale": 0.12,
  245. // "x_offset": 30,
  246. // "print_obj": true,
  247. // "lock_aspect_ratio": false,
  248. // "locked": false,
  249. // "positioning": "oncell"
  250. // }`
  251. if b.Content.Reviewed == 1 {
  252. imageCells := []string{imageCell1, imageCell2}
  253. if len(b.Signatures) > 0 {
  254. for k, sign := range b.Signatures {
  255. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Path, sign.Format)
  256. }
  257. }
  258. }
  259. return nil
  260. }
  261. func (b *ProcessBillExcel) Draws() {
  262. b.drawTitle()
  263. b.drawSubTitles()
  264. b.drawTableTitle()
  265. b.drawTableContent()
  266. b.drawTableFooter()
  267. b.drawRemark()
  268. b.drawTableSignature()
  269. }
  270. func NewProcessBill(f *excelize.File) *ProcessBillExcel {
  271. border := []excelize.Border{
  272. {Type: "top", Style: 1, Color: "000000"},
  273. {Type: "left", Style: 1, Color: "000000"},
  274. {Type: "right", Style: 1, Color: "000000"},
  275. {Type: "bottom", Style: 1, Color: "000000"},
  276. }
  277. styleLeft, _ := f.NewStyle(&excelize.Style{
  278. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  279. Border: border,
  280. Font: &excelize.Font{Size: 10},
  281. })
  282. b := &ProcessBillExcel{
  283. Title: "原材料采购单",
  284. SheetName: "Sheet1",
  285. Excel: f,
  286. Offset: 0,
  287. AlignCenterStyle: styleLeft,
  288. Signatures: make([]*model.Signature, 0),
  289. }
  290. f.SetColWidth(b.SheetName, "A", "J", 11.5)
  291. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  292. return b
  293. }
  294. func (b *ProcessBillExcel) FormatToEmpty(str *string) {
  295. if *str == "0" || *str == "0.00" {
  296. *str = ""
  297. }
  298. }
  299. func (b *ProcessBillExcel) PrintPurchType() string {
  300. return "process"
  301. }
  302. func (b *ProcessBillExcel) SetContent(content *model.PurchaseBill) {
  303. b.Content = content
  304. }
  305. func (b *ProcessBillExcel) SetTitle(title string) {
  306. b.Title = title
  307. }
  308. func (b *ProcessBillExcel) SetSignatures(sign []*model.Signature) {
  309. b.Signatures = sign
  310. }