bill-product-excel.go 15 KB

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