bill-purchase-excel.go 15 KB

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