bill-produce-excel.go 12 KB

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