bill-produce-excel.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "strings"
  6. "github.com/xuri/excelize/v2"
  7. )
  8. type ProduceBillExcel struct {
  9. Offset int
  10. Row int
  11. Title string //标题
  12. Excel *excelize.File
  13. SheetName string
  14. AlignCenterStyle int
  15. Content *model.ProduceBill
  16. Signatures []*model.Signature
  17. IsPdf string
  18. RowMap map[string]int
  19. RowWidthArray []float64
  20. RowsHeightArray []map[int]float64
  21. }
  22. // 批量设置行高
  23. func (b *ProduceBillExcel) setRowsHeight() {
  24. for _, rowHeight := range b.RowsHeightArray {
  25. for row, height := range rowHeight {
  26. b.Excel.SetRowHeight(b.SheetName, row, height)
  27. }
  28. }
  29. }
  30. // 获取范围内单元格的宽度 A:F
  31. func (b *ProduceBillExcel) getRangeWidth(r string) float64 {
  32. rg := strings.Split(r, ":")
  33. if len(rg) == 1 {
  34. start := b.RowMap[rg[0]]
  35. return b.RowWidthArray[start]
  36. } else if len(rg) == 2 {
  37. start := b.RowMap[rg[0]]
  38. end := b.RowMap[rg[1]]
  39. rowr := b.RowWidthArray[start : end+1]
  40. width := 0.0
  41. for _, v := range rowr {
  42. width += v
  43. }
  44. return width
  45. }
  46. return 0.0
  47. }
  48. func (b *ProduceBillExcel) drawTitle() error {
  49. b.Row++
  50. //是打印
  51. startCell := fmt.Sprintf("A%d", b.Row)
  52. marginLeft := excelize.PageMarginLeft(0.1)
  53. endCell := fmt.Sprintf("L%d", b.Row)
  54. if b.Content.IsPrint {
  55. b.RowMap = map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7, "I": 8, "J": 9, "K": 10, "L": 11}
  56. b.RowWidthArray = []float64{11, 11, 11, 10, 7, 10, 7, 6.5, 9, 9, 12, 14}
  57. b.Excel.SetColWidth(b.SheetName, "A", "C", 11)
  58. b.Excel.SetColWidth(b.SheetName, "D", "D", 10)
  59. b.Excel.SetColWidth(b.SheetName, "E", "E", 7)
  60. b.Excel.SetColWidth(b.SheetName, "F", "F", 10)
  61. b.Excel.SetColWidth(b.SheetName, "G", "G", 7)
  62. b.Excel.SetColWidth(b.SheetName, "H", "H", 6.5)
  63. b.Excel.SetColWidth(b.SheetName, "I", "J", 9)
  64. b.Excel.SetColWidth(b.SheetName, "K", "K", 12)
  65. b.Excel.SetColWidth(b.SheetName, "L", "L", 14)
  66. } else if b.Content.IsLam {
  67. // 是覆膜
  68. b.RowMap = map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7, "I": 8}
  69. b.RowWidthArray = []float64{14, 14, 14, 10, 11, 11, 12, 12, 16}
  70. endCell = fmt.Sprintf("I%d", b.Row)
  71. marginLeft = excelize.PageMarginLeft(0.2)
  72. b.Excel.SetColWidth(b.SheetName, "A", "C", 14)
  73. // 覆膜规格
  74. b.Excel.SetColWidth(b.SheetName, "D", "D", 10)
  75. b.Excel.SetColWidth(b.SheetName, "E", "F", 12)
  76. b.Excel.SetColWidth(b.SheetName, "G", "H", 12)
  77. b.Excel.SetColWidth(b.SheetName, "I", "I", 16)
  78. } else if b.Content.IsDuib {
  79. // 是对裱
  80. b.RowMap = map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7, "I": 8}
  81. b.RowWidthArray = []float64{14, 14, 14, 12, 7, 12, 12, 12, 16}
  82. endCell = fmt.Sprintf("I%d", b.Row)
  83. marginLeft = excelize.PageMarginLeft(0.2)
  84. b.Excel.SetColWidth(b.SheetName, "A", "C", 14)
  85. b.Excel.SetColWidth(b.SheetName, "D", "D", 12)
  86. b.Excel.SetColWidth(b.SheetName, "E", "E", 10)
  87. b.Excel.SetColWidth(b.SheetName, "F", "H", 12)
  88. b.Excel.SetColWidth(b.SheetName, "I", "I", 16)
  89. } else {
  90. // 不是打印
  91. endCell = fmt.Sprintf("H%d", b.Row)
  92. marginLeft = excelize.PageMarginLeft(0.4)
  93. b.Excel.SetColWidth(b.SheetName, "A", "B", 17)
  94. b.Excel.SetColWidth(b.SheetName, "C", "C", 12)
  95. b.Excel.SetColWidth(b.SheetName, "D", "D", 10)
  96. b.Excel.SetColWidth(b.SheetName, "E", "G", 12)
  97. b.Excel.SetColWidth(b.SheetName, "H", "H", 20)
  98. }
  99. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  100. err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
  101. if err != nil {
  102. return err
  103. }
  104. style, err := b.Excel.NewStyle(&excelize.Style{
  105. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  106. Font: &excelize.Font{Bold: true, Size: 18}})
  107. if err != nil {
  108. return err
  109. }
  110. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  111. if err != nil {
  112. return err
  113. }
  114. b.Excel.SetRowHeight(b.SheetName, b.Row, 23)
  115. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  116. return nil
  117. }
  118. func (b *ProduceBillExcel) drawSubTitles() error {
  119. b.Row++
  120. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  121. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  122. Font: &excelize.Font{Size: 11}})
  123. if err != nil {
  124. return err
  125. }
  126. styleRight, err := b.Excel.NewStyle(&excelize.Style{
  127. Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
  128. Font: &excelize.Font{Size: 11}})
  129. if err != nil {
  130. return err
  131. }
  132. drawLeft := func(rowIndex int, value string) error {
  133. //左边1
  134. left1Cell := fmt.Sprintf("A%d", rowIndex)
  135. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("I%d", rowIndex))
  136. if err != nil {
  137. return err
  138. }
  139. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  140. if err != nil {
  141. return err
  142. }
  143. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  144. // 设置行高
  145. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{rowIndex: getRowHeight(value, b.getRangeWidth("A:I"))})
  146. return nil
  147. }
  148. drawRight := func(rowIndex int, value string) error {
  149. right1Cell := fmt.Sprintf("J%d", rowIndex)
  150. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("L%d", rowIndex))
  151. if err != nil {
  152. return err
  153. }
  154. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  155. if err != nil {
  156. return err
  157. }
  158. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  159. return nil
  160. }
  161. drawLall := func(rowIndex int, value string) error {
  162. //左边1
  163. left1Cell := fmt.Sprintf("A%d", rowIndex)
  164. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("L%d", rowIndex))
  165. if err != nil {
  166. return err
  167. }
  168. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  169. if err != nil {
  170. return err
  171. }
  172. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  173. // 设置行高
  174. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{rowIndex: getRowHeight(value, b.getRangeWidth("A:L"))})
  175. return nil
  176. }
  177. if !b.Content.IsPrint {
  178. drawLeft = func(rowIndex int, value string) error {
  179. //左边1
  180. left1Cell := fmt.Sprintf("A%d", rowIndex)
  181. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("F%d", rowIndex))
  182. if err != nil {
  183. return err
  184. }
  185. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  186. if err != nil {
  187. return err
  188. }
  189. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  190. // 设置行高
  191. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{rowIndex: getRowHeight(value, b.getRangeWidth("A:F"))})
  192. return nil
  193. }
  194. drawRight = func(rowIndex int, value string) error {
  195. right1Cell := fmt.Sprintf("G%d", rowIndex)
  196. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("H%d", rowIndex))
  197. if err != nil {
  198. return err
  199. }
  200. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  201. if err != nil {
  202. return err
  203. }
  204. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  205. return nil
  206. }
  207. drawLall = func(rowIndex int, value string) error {
  208. //左边1
  209. left1Cell := fmt.Sprintf("A%d", rowIndex)
  210. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("H%d", rowIndex))
  211. if err != nil {
  212. return err
  213. }
  214. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  215. if err != nil {
  216. return err
  217. }
  218. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  219. // 设置行高
  220. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{rowIndex: getRowHeight(value, b.getRangeWidth("A:H"))})
  221. return nil
  222. }
  223. if b.Content.IsLam || b.Content.IsDuib {
  224. drawLeft = func(rowIndex int, value string) error {
  225. //左边1
  226. left1Cell := fmt.Sprintf("A%d", rowIndex)
  227. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("F%d", rowIndex))
  228. if err != nil {
  229. return err
  230. }
  231. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  232. if err != nil {
  233. return err
  234. }
  235. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  236. // 设置行高
  237. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{rowIndex: getRowHeight(value, b.getRangeWidth("A:F"))})
  238. return nil
  239. }
  240. drawRight = func(rowIndex int, value string) error {
  241. right1Cell := fmt.Sprintf("G%d", rowIndex)
  242. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("I%d", rowIndex))
  243. if err != nil {
  244. return err
  245. }
  246. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  247. if err != nil {
  248. return err
  249. }
  250. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  251. return nil
  252. }
  253. drawLall = func(rowIndex int, value string) error {
  254. //左边1
  255. left1Cell := fmt.Sprintf("A%d", rowIndex)
  256. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("I%d", rowIndex))
  257. if err != nil {
  258. return err
  259. }
  260. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  261. if err != nil {
  262. return err
  263. }
  264. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  265. // 设置行高
  266. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{rowIndex: getRowHeight(value, b.getRangeWidth("A:I"))})
  267. return nil
  268. }
  269. }
  270. }
  271. //第一行
  272. drawLeft(b.Row, "类别:"+b.Content.Type)
  273. drawRight(b.Row, "单号:"+b.Content.SerialNumber)
  274. //第二行
  275. drawLeft(b.Row+1, "供应商名称:"+b.Content.Supplier)
  276. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  277. drawRight(b.Row+1, "下单时间:"+timeformat)
  278. //第三行
  279. drawLeft(b.Row+2, "产品名称:"+b.Content.ProductName)
  280. status := ""
  281. if b.Content.Status == "complete" {
  282. status = "已完成"
  283. }
  284. if b.Content.Status == "created" {
  285. status = "进行中"
  286. }
  287. drawRight(b.Row+2, "状态:"+status)
  288. // 第四行
  289. drawLall(b.Row+3, "包含工序:"+b.Content.CompProduceName)
  290. return nil
  291. }
  292. func (b *ProduceBillExcel) drawTableTitle() error {
  293. b.Row += 4
  294. var drawCol = func(prefix string, value string) error {
  295. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  296. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  297. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  298. if err != nil {
  299. return err
  300. }
  301. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  302. if err != nil {
  303. return err
  304. }
  305. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  306. }
  307. var drawCol3 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
  308. left1Cell := fmt.Sprintf("%s%d", prefix1, b.Row)
  309. left2Cell := fmt.Sprintf("%s%d", prefix2, b.Row)
  310. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  311. if err != nil {
  312. return err
  313. }
  314. if err != nil {
  315. fmt.Println(err)
  316. return err
  317. }
  318. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  319. if err != nil {
  320. return err
  321. }
  322. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  323. val2Cel := fmt.Sprintf("%s%d", prefix1, b.Row+1)
  324. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  325. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  326. val3Cel := fmt.Sprintf("%s%d", prefix2, b.Row+1)
  327. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  328. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  329. return nil
  330. }
  331. unit := b.Content.Produces[0].Unit
  332. if b.Content.IsPrint {
  333. drawCol("A", "加工项目")
  334. drawCol("B", "规格")
  335. drawCol("C", "纸张")
  336. drawCol("D", "来纸尺寸")
  337. drawCol("E", "来纸数量")
  338. drawCol("F", "印刷尺寸")
  339. drawCol("G", "下单数量")
  340. drawCol("H", "单位")
  341. drawCol("I", "单价")
  342. drawCol("J", "预算金额")
  343. drawCol("K", "交货时间")
  344. drawCol("L", "备注")
  345. } else if b.Content.IsLam {
  346. drawCol("A", "加工项目")
  347. drawCol("B", "规格")
  348. drawCol("C", "覆膜尺寸")
  349. drawCol("D", "下单数量")
  350. unit2 := b.Content.Produces[0].Unit2
  351. drawCol3("E", "F", "单价", unit, unit2)
  352. drawCol("G", "预算金额")
  353. drawCol("H", "交货时间")
  354. drawCol("I", "备注")
  355. } else if b.Content.IsDuib {
  356. drawCol("A", "加工项目")
  357. drawCol("B", "规格")
  358. drawCol("C", "对裱尺寸")
  359. drawCol("D", "下单数量")
  360. drawCol("E", "单位")
  361. drawCol("F", "单价")
  362. drawCol("G", "预算金额")
  363. drawCol("H", "交货时间")
  364. drawCol("I", "备注")
  365. } else {
  366. drawCol("A", "加工项目")
  367. drawCol("B", "规格")
  368. drawCol("C", "下单数量")
  369. drawCol("D", "单位")
  370. drawCol("E", "单价")
  371. drawCol("F", "预算金额")
  372. drawCol("G", "交货时间")
  373. drawCol("H", "备注")
  374. }
  375. return nil
  376. }
  377. func (b *ProduceBillExcel) drawTableContent() error {
  378. b.Row += 2
  379. var DrawRow = func(rowIndex int, values ...string) float64 {
  380. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}
  381. if !b.Content.IsPrint {
  382. charas = []string{"A", "B", "C", "D", "E", "F", "G", "H"}
  383. if b.Content.IsLam || b.Content.IsDuib {
  384. charas = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"}
  385. }
  386. }
  387. // 获取改行最大行高
  388. max := getRowHeight(values[0], b.getRangeWidth(charas[0]))
  389. for i, c := range charas {
  390. v := ""
  391. if i < len(values) {
  392. v = values[i]
  393. }
  394. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  395. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  396. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  397. if getRowHeight(v, b.getRangeWidth(c)) > max {
  398. max = getRowHeight(v, b.getRangeWidth(c))
  399. }
  400. }
  401. return max
  402. }
  403. produces := b.Content.Produces
  404. if len(produces) > 0 {
  405. for _, produce := range produces {
  406. realCount := ""
  407. price := produce.OrderPrice
  408. priceStr := fmt.Sprintf("%.3f", price)
  409. b.FormatToEmpty(&priceStr)
  410. // 预算金额
  411. budgetAmount := fmt.Sprintf("%.3f", produce.OrderPrice*float64(produce.OrderCount))
  412. b.FormatToEmpty(&budgetAmount)
  413. // 实际金额
  414. realPrice := ""
  415. // 实际完成数
  416. realCount = fmt.Sprintf("%d", produce.ConfirmCount)
  417. b.FormatToEmpty(&realCount)
  418. realPrice = fmt.Sprintf("%.3f", produce.OrderPrice*float64(produce.ConfirmCount))
  419. b.FormatToEmpty(&realPrice)
  420. deliveryTime := produce.DeliveryTime.Local().Format("2006-01-02")
  421. paperCount := fmt.Sprintf("%d", produce.PaperCount)
  422. b.FormatToEmpty(&paperCount)
  423. rowMaxHeight := 0.0
  424. if b.Content.IsPrint {
  425. rowMaxHeight = DrawRow(b.Row, produce.Name, produce.Norm, produce.Paper, produce.PaperSize, paperCount, produce.PrintSize, fmt.Sprintf("%d", produce.OrderCount), produce.Unit, priceStr, budgetAmount, deliveryTime, produce.Remark)
  426. } else if b.Content.IsLam {
  427. // 覆膜
  428. rowMaxHeight = DrawRow(b.Row, produce.Name, produce.Norm, produce.PrintSize, fmt.Sprintf("%d", produce.OrderCount), priceStr, fmt.Sprintf("%.3f", produce.Price2), budgetAmount, deliveryTime, produce.Remark)
  429. } else if b.Content.IsDuib {
  430. // 对裱
  431. rowMaxHeight = DrawRow(b.Row, produce.Name, produce.Norm, produce.PaperSize, fmt.Sprintf("%d", produce.OrderCount), produce.Unit, priceStr, budgetAmount, deliveryTime, produce.Remark)
  432. } else {
  433. rowMaxHeight = DrawRow(b.Row, produce.Name, produce.Norm, fmt.Sprintf("%d", produce.OrderCount), produce.Unit, priceStr, budgetAmount, deliveryTime, produce.Remark)
  434. }
  435. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: rowMaxHeight})
  436. b.Row++
  437. }
  438. }
  439. return nil
  440. }
  441. func (b *ProduceBillExcel) drawTableFooter() error {
  442. border := []excelize.Border{
  443. {Type: "top", Style: 1, Color: "000000"},
  444. {Type: "left", Style: 1, Color: "000000"},
  445. {Type: "right", Style: 1, Color: "000000"},
  446. {Type: "bottom", Style: 1, Color: "000000"},
  447. }
  448. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  449. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  450. Border: border,
  451. })
  452. sendToStartCell := fmt.Sprintf("A%d", b.Row)
  453. sendToEndCell := fmt.Sprintf("I%d", b.Row)
  454. supplierStartCell := fmt.Sprintf("J%d", b.Row)
  455. supplierEndCell := fmt.Sprintf("L%d", b.Row)
  456. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("送货地址:"+b.Content.SendTo, b.getRangeWidth("A:I"))})
  457. if !b.Content.IsPrint {
  458. sendToEndCell = fmt.Sprintf("E%d", b.Row)
  459. supplierStartCell = fmt.Sprintf("F%d", b.Row)
  460. supplierEndCell = fmt.Sprintf("H%d", b.Row)
  461. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("送货地址:"+b.Content.SendTo, b.getRangeWidth("A:E"))})
  462. if b.Content.IsLam || b.Content.IsDuib {
  463. sendToEndCell = fmt.Sprintf("F%d", b.Row)
  464. supplierStartCell = fmt.Sprintf("G%d", b.Row)
  465. supplierEndCell = fmt.Sprintf("I%d", b.Row)
  466. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("送货地址:"+b.Content.SendTo, b.getRangeWidth("A:F"))})
  467. }
  468. }
  469. b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
  470. b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
  471. b.Excel.SetCellValue(b.SheetName, sendToStartCell, "送货地址:"+b.Content.SendTo)
  472. b.Excel.MergeCell(b.SheetName, supplierStartCell, supplierEndCell)
  473. b.Excel.SetCellStyle(b.SheetName, supplierStartCell, supplierEndCell, styleLeft)
  474. b.Excel.SetCellValue(b.SheetName, supplierStartCell, "供应商签字:")
  475. return nil
  476. }
  477. func (b *ProduceBillExcel) drawSupplierRemark() error {
  478. b.Row++
  479. border := []excelize.Border{
  480. {Type: "top", Style: 1, Color: "000000"},
  481. {Type: "left", Style: 1, Color: "000000"},
  482. {Type: "right", Style: 1, Color: "000000"},
  483. {Type: "bottom", Style: 1, Color: "000000"},
  484. }
  485. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  486. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  487. Border: border,
  488. })
  489. sendToStartCell := fmt.Sprintf("A%d", b.Row)
  490. sendToEndCell := fmt.Sprintf("L%d", b.Row)
  491. // 设置行高
  492. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("供应商备注:"+b.Content.SupplierRemark, b.getRangeWidth("A:L"))})
  493. if !b.Content.IsPrint {
  494. sendToEndCell = fmt.Sprintf("H%d", b.Row)
  495. // 设置行高
  496. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("供应商备注:"+b.Content.SupplierRemark, b.getRangeWidth("A:H"))})
  497. if b.Content.IsLam || b.Content.IsDuib {
  498. sendToEndCell = fmt.Sprintf("I%d", b.Row)
  499. // 设置行高
  500. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("供应商备注:"+b.Content.SupplierRemark, b.getRangeWidth("A:I"))})
  501. }
  502. }
  503. b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
  504. b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
  505. b.Excel.SetCellValue(b.SheetName, sendToStartCell, "供应商备注:"+b.Content.SupplierRemark)
  506. return nil
  507. }
  508. func (b *ProduceBillExcel) drawTableSignature() error {
  509. b.Row += 2
  510. style1, _ := b.Excel.NewStyle(&excelize.Style{
  511. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  512. // Border: border,
  513. })
  514. // 制单人
  515. billUserCellS := fmt.Sprintf("A%d", b.Row+1)
  516. billUserCellE := fmt.Sprintf("B%d", b.Row+1)
  517. b.Excel.MergeCell(b.SheetName, billUserCellS, billUserCellE)
  518. b.Excel.SetCellValue(b.SheetName, billUserCellS, "制单人:"+b.Content.UserName)
  519. fontNum := "G"
  520. fontENum := "H"
  521. image1s := "I"
  522. image2s := "K"
  523. image1e := "J"
  524. image2e := "L"
  525. if !b.Content.IsPrint {
  526. fontNum = "E"
  527. fontENum = "E"
  528. image1s = "F"
  529. image2s = "H"
  530. image1e = "G"
  531. image2e = "H"
  532. if b.Content.IsLam || b.Content.IsDuib {
  533. fontNum = "E"
  534. fontENum = "E"
  535. image1s = "F"
  536. image2s = "H"
  537. image1e = "G"
  538. image2e = "I"
  539. }
  540. }
  541. fontCell := fmt.Sprintf("%s%d", fontNum, b.Row)
  542. fontEndCell := fmt.Sprintf("%s%d", fontENum, b.Row+2)
  543. imageCell1 := fmt.Sprintf("%s%d", image1s, b.Row)
  544. imageEndCell1 := fmt.Sprintf("%s%d", image1e, b.Row+2)
  545. imageCell2 := fmt.Sprintf("%s%d", image2s, b.Row)
  546. imageEndCell2 := fmt.Sprintf("%s%d", image2e, b.Row+2)
  547. b.Excel.MergeCell(b.SheetName, fontCell, fontEndCell)
  548. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  549. b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
  550. // 签字图片1 I10-J12
  551. b.Excel.MergeCell(b.SheetName, imageCell1, imageEndCell1)
  552. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  553. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  554. // 签字图片2 K10-L12
  555. b.Excel.MergeCell(b.SheetName, imageCell2, imageEndCell2)
  556. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  557. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  558. // 状态为已审核时,签字
  559. if b.Content.Reviewed == 1 {
  560. imageCells := []string{imageCell1, imageCell2}
  561. if len(b.Signatures) > 0 {
  562. for k, sign := range b.Signatures {
  563. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Path, sign.Format)
  564. }
  565. }
  566. }
  567. return nil
  568. }
  569. func (b *ProduceBillExcel) Draws() {
  570. b.drawTitle()
  571. b.drawSubTitles()
  572. b.drawTableTitle()
  573. b.drawTableContent()
  574. b.drawTableFooter()
  575. if len(b.Content.SupplierRemark) > 1 {
  576. b.drawSupplierRemark()
  577. }
  578. b.drawTableSignature()
  579. // 设置行高
  580. b.setRowsHeight()
  581. }
  582. func NewProduceBill(f *excelize.File) *ProduceBillExcel {
  583. border := []excelize.Border{
  584. {Type: "top", Style: 1, Color: "000000"},
  585. {Type: "left", Style: 1, Color: "000000"},
  586. {Type: "right", Style: 1, Color: "000000"},
  587. {Type: "bottom", Style: 1, Color: "000000"},
  588. }
  589. styleLeft, _ := f.NewStyle(&excelize.Style{
  590. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  591. Border: border,
  592. })
  593. b := &ProduceBillExcel{
  594. Title: "中鱼互动加工单",
  595. SheetName: "Sheet1",
  596. Excel: f,
  597. Offset: 0,
  598. AlignCenterStyle: styleLeft,
  599. Signatures: make([]*model.Signature, 0),
  600. RowMap: map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7},
  601. RowWidthArray: []float64{17, 17, 12, 10, 12, 12, 12, 20},
  602. RowsHeightArray: make([]map[int]float64, 0),
  603. }
  604. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  605. return b
  606. }
  607. func (b *ProduceBillExcel) FormatToEmpty(str *string) {
  608. if *str == "0" || *str == "0.000" {
  609. *str = "-"
  610. }
  611. }
  612. func (b *ProduceBillExcel) SetContent(content interface{}) {
  613. b.Content = content.(*model.ProduceBill)
  614. }
  615. func (b *ProduceBillExcel) SetTitle(title string) {
  616. b.Title = title
  617. }
  618. func (b *ProduceBillExcel) SetSheetName(name string) {
  619. b.SheetName = name
  620. }
  621. func (b *ProduceBillExcel) SetRow(row int) {
  622. b.Row = row
  623. }
  624. func (b *ProduceBillExcel) SetIsPdf(isPdf string) {
  625. b.IsPdf = isPdf
  626. }
  627. func (b *ProduceBillExcel) GetRow() int {
  628. return b.Row
  629. }
  630. func (b *ProduceBillExcel) SetSignatures(sign interface{}) {
  631. b.Signatures = sign.([]*model.Signature)
  632. }