bill-produce-excel.go 15 KB

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