bill-produce-excel.go 25 KB

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