bill-produce-excel.go 16 KB

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