bill-product-excel.go 16 KB

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