bill-product-excel.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "regexp"
  6. "strings"
  7. _ "image/gif"
  8. _ "image/jpeg"
  9. _ "image/png"
  10. "github.com/xuri/excelize/v2"
  11. )
  12. type ProductBillExcel struct {
  13. Offset int
  14. Row int
  15. Title string //标题
  16. Excel *excelize.File
  17. SheetName string
  18. AlignCenterStyle int
  19. Content *model.ProductBill
  20. Signatures []*model.Signature
  21. IsPdf string
  22. RowMap map[string]int
  23. RowWidthArray []float64
  24. RowsHeightArray []map[int]float64
  25. }
  26. // 批量设置行高
  27. func (b *ProductBillExcel) setRowsHeight() {
  28. for _, rowHeight := range b.RowsHeightArray {
  29. for row, height := range rowHeight {
  30. b.Excel.SetRowHeight(b.SheetName, row, height)
  31. }
  32. }
  33. }
  34. // 获取范围内单元格的宽度 A:F
  35. func (b *ProductBillExcel) getRangeWidth(r string) float64 {
  36. rg := strings.Split(r, ":")
  37. if len(rg) == 1 {
  38. start := b.RowMap[rg[0]]
  39. return b.RowWidthArray[start]
  40. } else if len(rg) == 2 {
  41. start := b.RowMap[rg[0]]
  42. end := b.RowMap[rg[1]]
  43. rowr := b.RowWidthArray[start : end+1]
  44. width := 0.0
  45. for _, v := range rowr {
  46. width += v
  47. }
  48. return width
  49. }
  50. return 0.0
  51. }
  52. func (b *ProductBillExcel) drawTitle() error {
  53. b.Row++
  54. startCell := fmt.Sprintf("A%d", b.Row)
  55. endCell := fmt.Sprintf("I%d", b.Row)
  56. marginLeft := excelize.PageMarginLeft(0.00)
  57. if b.IsPdf == "true" {
  58. marginLeft = excelize.PageMarginLeft(0.1)
  59. }
  60. b.Excel.SetColWidth(b.SheetName, "A", "A", 16)
  61. b.Excel.SetColWidth(b.SheetName, "B", "H", 12)
  62. b.Excel.SetColWidth(b.SheetName, "I", "I", 18)
  63. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  64. err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
  65. if err != nil {
  66. return err
  67. }
  68. style, err := b.Excel.NewStyle(&excelize.Style{
  69. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  70. Font: &excelize.Font{Bold: true, Size: 18}})
  71. if err != nil {
  72. return err
  73. }
  74. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  75. if err != nil {
  76. return err
  77. }
  78. b.Excel.SetRowHeight(b.SheetName, b.Row, 23)
  79. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  80. return nil
  81. }
  82. func (b *ProductBillExcel) drawSubTitles() error {
  83. b.Row++
  84. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  85. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  86. Font: &excelize.Font{Size: 11}})
  87. if err != nil {
  88. return err
  89. }
  90. styleRight, err := b.Excel.NewStyle(&excelize.Style{
  91. Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
  92. Font: &excelize.Font{Size: 11}})
  93. if err != nil {
  94. return err
  95. }
  96. drawLeft := func(rowIndex int, value string) error {
  97. //左边1
  98. left1Cell := fmt.Sprintf("A%d", rowIndex)
  99. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("F%d", rowIndex))
  100. if err != nil {
  101. return err
  102. }
  103. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  104. if err != nil {
  105. return err
  106. }
  107. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  108. return nil
  109. }
  110. drawRight := func(rowIndex int, value string) error {
  111. right1Cell := fmt.Sprintf("G%d", rowIndex)
  112. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("I%d", rowIndex))
  113. if err != nil {
  114. return err
  115. }
  116. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  117. if err != nil {
  118. return err
  119. }
  120. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  121. return nil
  122. }
  123. drawLall := func(rowIndex int, value string) error {
  124. //左边1
  125. left1Cell := fmt.Sprintf("A%d", rowIndex)
  126. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("F%d", rowIndex))
  127. if err != nil {
  128. return err
  129. }
  130. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  131. if err != nil {
  132. return err
  133. }
  134. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  135. return nil
  136. }
  137. //第一行
  138. drawLeft(b.Row, "类别:"+b.Content.Type)
  139. drawRight(b.Row, "单号:"+b.Content.SerialNumber)
  140. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  141. //第二行
  142. supplierContent := fmt.Sprintf("供应商名称:%s", b.Content.Supplier)
  143. drawLeft(b.Row+1, supplierContent)
  144. // 设置行高
  145. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row + 1: getRowHeight(supplierContent, b.getRangeWidth("A:F"))})
  146. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  147. drawRight(b.Row+1, "下单时间:"+timeformat)
  148. //第三行
  149. drawLeft(b.Row+2, "产品名称:"+b.Content.ProductName)
  150. // 设置行高
  151. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row + 2: getRowHeight("产品名称:"+b.Content.ProductName, b.getRangeWidth("A:F"))})
  152. status := ""
  153. if b.Content.Status == "complete" {
  154. status = "已完成"
  155. }
  156. if b.Content.Status == "created" {
  157. status = "进行中"
  158. }
  159. drawRight(b.Row+2, "状态:"+status)
  160. b.Excel.SetRowHeight(b.SheetName, b.Row+2, 21)
  161. // 第四行
  162. drawLall(b.Row+3, "包含工序:"+b.Content.CompProduceName)
  163. // 设置行高
  164. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row + 3: getRowHeight("包含工序:"+b.Content.CompProduceName, b.getRangeWidth("A:F"))})
  165. return nil
  166. }
  167. func (b *ProductBillExcel) drawTableTitle() error {
  168. b.Row += 4
  169. // 品名 规格 数量 单位 单价 金额
  170. var drawCol = func(prefix string, value string) error {
  171. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  172. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  173. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  174. if err != nil {
  175. return err
  176. }
  177. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  178. if err != nil {
  179. return err
  180. }
  181. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  182. }
  183. drawCol("A", "采购项目")
  184. drawCol("B", "规格")
  185. drawCol("C", "尺寸")
  186. drawCol("D", "下单数量")
  187. drawCol("E", "单位")
  188. drawCol("F", "单价")
  189. drawCol("G", "预算金额")
  190. drawCol("H", "交货时间")
  191. drawCol("I", "备注")
  192. return nil
  193. }
  194. func (b *ProductBillExcel) drawTableContent() error {
  195. b.Row += 2
  196. var DrawRow = func(rowIndex int, values ...string) float64 {
  197. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"}
  198. // 获取改行最大行高
  199. max := getRowHeight(values[0], b.getRangeWidth(charas[0]))
  200. for i, c := range charas {
  201. v := ""
  202. if i < len(values) {
  203. v = values[i]
  204. }
  205. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  206. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  207. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  208. if getRowHeight(v, b.getRangeWidth(c)) > max {
  209. max = getRowHeight(v, b.getRangeWidth(c))
  210. }
  211. }
  212. return max
  213. }
  214. products := b.Content.Products
  215. if len(products) > 0 {
  216. for _, product := range products {
  217. deliveryTime := product.DeliveryTime.Local().Format("2006-01-02")
  218. realCount := "-"
  219. realPrice := "-"
  220. // 预算金额
  221. budgetAmount := fmt.Sprintf("%.3f", float64(product.OrderCount)*product.OrderPrice)
  222. b.FormatToEmpty(&budgetAmount)
  223. // 实际完成数
  224. realCount = fmt.Sprintf("%d", product.ConfirmCount)
  225. b.FormatToEmpty(&realCount)
  226. // 实际金额
  227. realPrice = fmt.Sprintf("%.3f", float64(product.ConfirmCount)*product.OrderPrice)
  228. // !10.27 如果是固定价格
  229. if product.IsFix == nil {
  230. _fix := false
  231. product.IsFix = &_fix
  232. }
  233. if *product.IsFix {
  234. realPrice = budgetAmount
  235. }
  236. b.FormatToEmpty(&realPrice)
  237. // a采购项目 b规格 c下单数量 d单位 e完成数量 f单价 g预算金额 h实际金额 i交货时间 j备注
  238. orderCount := fmt.Sprintf("%d", product.OrderCount)
  239. price := fmt.Sprintf("%.3f", product.OrderPrice)
  240. b.FormatToEmpty(&price)
  241. // DrawRow(b.Row, product.Name, product.Norm, product.Size, orderCount, product.Unit, price, budgetAmount, deliveryTime, product.Remark)
  242. rowMaxHeight := DrawRow(b.Row, product.Name, product.Norm, product.Size, orderCount, product.Unit, price, budgetAmount, deliveryTime, product.Remark)
  243. // 设置行高
  244. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: rowMaxHeight})
  245. b.Row++
  246. }
  247. }
  248. return nil
  249. }
  250. func (b *ProductBillExcel) drawTableFooter() error {
  251. // left1Cell := fmt.Sprintf("A%d", b.Row)
  252. border := []excelize.Border{
  253. {Type: "top", Style: 1, Color: "000000"},
  254. {Type: "left", Style: 1, Color: "000000"},
  255. {Type: "right", Style: 1, Color: "000000"},
  256. {Type: "bottom", Style: 1, Color: "000000"},
  257. }
  258. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  259. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  260. Border: border,
  261. })
  262. sendToStartCell := fmt.Sprintf("A%d", b.Row)
  263. sendToEndCell := fmt.Sprintf("F%d", b.Row)
  264. supplierStartCell := fmt.Sprintf("G%d", b.Row)
  265. supplierEndCell := fmt.Sprintf("I%d", b.Row)
  266. b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
  267. b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
  268. b.Excel.SetCellValue(b.SheetName, sendToStartCell, "送货地址:"+b.Content.SendTo)
  269. b.Excel.MergeCell(b.SheetName, supplierStartCell, supplierEndCell)
  270. // 设置行高
  271. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("送货地址:"+b.Content.SendTo, b.getRangeWidth("A:F"))})
  272. b.Excel.SetCellStyle(b.SheetName, supplierStartCell, supplierEndCell, styleLeft)
  273. b.Excel.SetCellValue(b.SheetName, supplierStartCell, " 供应商签字:")
  274. return nil
  275. }
  276. func (b *ProductBillExcel) drawSupplierRemark() error {
  277. b.Row++
  278. border := []excelize.Border{
  279. {Type: "top", Style: 1, Color: "000000"},
  280. {Type: "left", Style: 1, Color: "000000"},
  281. {Type: "right", Style: 1, Color: "000000"},
  282. {Type: "bottom", Style: 1, Color: "000000"},
  283. }
  284. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  285. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  286. Border: border,
  287. })
  288. sendToStartCell := fmt.Sprintf("A%d", b.Row)
  289. sendToEndCell := fmt.Sprintf("I%d", b.Row)
  290. b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
  291. b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
  292. b.Excel.SetCellValue(b.SheetName, sendToStartCell, "供应商备注:"+b.Content.SupplierRemark)
  293. // 设置行高
  294. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("供应商备注:"+b.Content.SupplierRemark, b.getRangeWidth("A:I"))})
  295. return nil
  296. }
  297. func (b *ProductBillExcel) drawRemark() error {
  298. // 填备注
  299. b.Row++
  300. remarkTitleCell := fmt.Sprintf("A%d", b.Row)
  301. b.Excel.SetCellValue(b.SheetName, remarkTitleCell, "备注:")
  302. b.Row++
  303. remarkContentScell := fmt.Sprintf("A%d", b.Row)
  304. // 备注内容
  305. reg := regexp.MustCompile(`\n`)
  306. remarkRowNum := len(reg.FindAllString(b.Content.Remark, -1)) + 1
  307. b.Row += remarkRowNum
  308. remarkContentEcell := fmt.Sprintf("J%d", b.Row)
  309. b.Excel.MergeCell(b.SheetName, remarkContentScell, remarkContentEcell)
  310. b.Excel.SetCellValue(b.SheetName, remarkContentScell, b.Content.Remark)
  311. return nil
  312. }
  313. func (b *ProductBillExcel) drawTableSignature() error {
  314. b.Row += 2
  315. // row := b.Row
  316. style1, _ := b.Excel.NewStyle(&excelize.Style{
  317. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  318. })
  319. // 制单人
  320. billUserCellS := fmt.Sprintf("A%d", b.Row+1)
  321. billUserCellE := fmt.Sprintf("B%d", b.Row+1)
  322. b.Excel.MergeCell(b.SheetName, billUserCellS, billUserCellE)
  323. b.Excel.SetCellValue(b.SheetName, billUserCellS, "制单人:"+b.Content.UserName)
  324. fontNum := "F"
  325. image1s := "G"
  326. image2s := "I"
  327. image1e := "H"
  328. image2e := "I"
  329. fontCell := fmt.Sprintf("%s%d", fontNum, b.Row)
  330. fontEndCell := fmt.Sprintf("%s%d", fontNum, b.Row+2)
  331. imageCell1 := fmt.Sprintf("%s%d", image1s, b.Row)
  332. imageEndCell1 := fmt.Sprintf("%s%d", image1e, b.Row+2)
  333. imageCell2 := fmt.Sprintf("%s%d", image2s, b.Row)
  334. imageEndCell2 := fmt.Sprintf("%s%d", image2e, b.Row+2)
  335. b.Excel.MergeCell(b.SheetName, fontCell, fontEndCell)
  336. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  337. b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
  338. // 签字图片1 I10-J12
  339. b.Excel.MergeCell(b.SheetName, imageCell1, imageEndCell1)
  340. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  341. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  342. // 签字图片2 K10-L12
  343. b.Excel.MergeCell(b.SheetName, imageCell2, imageEndCell2)
  344. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  345. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  346. if b.Content.Reviewed == 1 {
  347. imageCells := []string{imageCell1, imageCell2}
  348. if len(b.Signatures) > 0 {
  349. for k, sign := range b.Signatures {
  350. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Path, sign.Format)
  351. }
  352. }
  353. }
  354. return nil
  355. }
  356. func (b *ProductBillExcel) Draws() {
  357. b.drawTitle()
  358. b.drawSubTitles()
  359. b.drawTableTitle()
  360. b.drawTableContent()
  361. b.drawTableFooter()
  362. if len(b.Content.SupplierRemark) > 1 {
  363. b.drawSupplierRemark()
  364. }
  365. b.drawRemark()
  366. b.drawTableSignature()
  367. // 设置行高
  368. b.setRowsHeight()
  369. }
  370. func NewProductBill(f *excelize.File) *ProductBillExcel {
  371. border := []excelize.Border{
  372. {Type: "top", Style: 1, Color: "000000"},
  373. {Type: "left", Style: 1, Color: "000000"},
  374. {Type: "right", Style: 1, Color: "000000"},
  375. {Type: "bottom", Style: 1, Color: "000000"},
  376. }
  377. styleLeft, _ := f.NewStyle(&excelize.Style{
  378. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  379. Border: border,
  380. Font: &excelize.Font{Size: 10},
  381. })
  382. b := &ProductBillExcel{
  383. Title: "原材料采购单",
  384. SheetName: "Sheet1",
  385. Excel: f,
  386. Offset: 0,
  387. AlignCenterStyle: styleLeft,
  388. Signatures: make([]*model.Signature, 0),
  389. RowMap: map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7, "I": 8},
  390. RowWidthArray: []float64{16, 12, 12, 12, 12, 12, 12, 12, 18},
  391. RowsHeightArray: make([]map[int]float64, 0),
  392. }
  393. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0.25), excelize.PageMarginRight(0))
  394. return b
  395. }
  396. func (b *ProductBillExcel) FormatToEmpty(str *string) {
  397. if *str == "0" || *str == "0.000" {
  398. *str = "-"
  399. }
  400. }
  401. func (b *ProductBillExcel) SetContent(content interface{}) {
  402. b.Content = content.(*model.ProductBill)
  403. }
  404. func (b *ProductBillExcel) SetTitle(title string) {
  405. b.Title = title
  406. }
  407. func (b *ProductBillExcel) SetSheetName(name string) {
  408. b.SheetName = name
  409. }
  410. func (b *ProductBillExcel) SetRow(row int) {
  411. b.Row = row
  412. }
  413. func (b *ProductBillExcel) SetIsPdf(isPdf string) {
  414. b.IsPdf = isPdf
  415. }
  416. func (b *ProductBillExcel) GetRow() int {
  417. return b.Row
  418. }
  419. func (b *ProductBillExcel) SetSignatures(sign interface{}) {
  420. b.Signatures = sign.([]*model.Signature)
  421. }