bill-produce-excel.go 13 KB

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