bill-product-excel.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "math"
  6. _ "image/gif"
  7. _ "image/jpeg"
  8. _ "image/png"
  9. "github.com/xuri/excelize/v2"
  10. )
  11. type ProductBillExcel struct {
  12. Offset int
  13. Row int
  14. Title string //标题
  15. Excel *excelize.File
  16. SheetName string
  17. AlignCenterStyle int
  18. Content *model.ProductBill
  19. Signatures []*model.Signature
  20. IsPdf string
  21. }
  22. func (b *ProductBillExcel) drawTitle() error {
  23. b.Row++
  24. startCell := fmt.Sprintf("A%d", b.Row)
  25. endCell := fmt.Sprintf("H%d", b.Row)
  26. marginLeft := excelize.PageMarginLeft(0.35)
  27. b.Excel.SetColWidth(b.SheetName, "A", "A", 17)
  28. b.Excel.SetColWidth(b.SheetName, "B", "B", 14)
  29. b.Excel.SetColWidth(b.SheetName, "C", "G", 12)
  30. b.Excel.SetColWidth(b.SheetName, "H", "H", 22)
  31. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  32. err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
  33. if err != nil {
  34. return err
  35. }
  36. style, err := b.Excel.NewStyle(&excelize.Style{
  37. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  38. Font: &excelize.Font{Bold: true, Size: 18}})
  39. if err != nil {
  40. return err
  41. }
  42. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  43. if err != nil {
  44. return err
  45. }
  46. b.Excel.SetRowHeight(b.SheetName, b.Row, 23)
  47. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  48. return nil
  49. }
  50. func (b *ProductBillExcel) drawSubTitles() error {
  51. b.Row++
  52. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  53. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  54. Font: &excelize.Font{Size: 11}})
  55. if err != nil {
  56. return err
  57. }
  58. styleRight, err := b.Excel.NewStyle(&excelize.Style{
  59. Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
  60. Font: &excelize.Font{Size: 11}})
  61. if err != nil {
  62. return err
  63. }
  64. drawLeft := func(rowIndex int, value string) error {
  65. //左边1
  66. left1Cell := fmt.Sprintf("A%d", rowIndex)
  67. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("F%d", rowIndex))
  68. if err != nil {
  69. return err
  70. }
  71. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  72. if err != nil {
  73. return err
  74. }
  75. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  76. return nil
  77. }
  78. drawRight := func(rowIndex int, value string) error {
  79. right1Cell := fmt.Sprintf("G%d", rowIndex)
  80. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("H%d", rowIndex))
  81. if err != nil {
  82. return err
  83. }
  84. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  85. if err != nil {
  86. return err
  87. }
  88. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  89. return nil
  90. }
  91. //第一行
  92. drawLeft(b.Row, "类别:"+b.Content.Type)
  93. drawRight(b.Row, "单号:"+b.Content.SerialNumber)
  94. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  95. //第二行
  96. drawLeft(b.Row+1, "供应商名称:"+b.Content.Supplier)
  97. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  98. drawRight(b.Row+1, "下单时间:"+timeformat)
  99. b.Excel.SetRowHeight(b.SheetName, b.Row+1, 21)
  100. //第三行
  101. drawLeft(b.Row+2, "产品名称:"+b.Content.ProductName)
  102. status := ""
  103. if b.Content.Status == "complete" {
  104. status = "已完成"
  105. }
  106. if b.Content.Status == "created" {
  107. status = "进行中"
  108. }
  109. drawRight(b.Row+2, "状态:"+status)
  110. b.Excel.SetRowHeight(b.SheetName, b.Row+2, 21)
  111. return nil
  112. }
  113. func (b *ProductBillExcel) drawTableTitle() error {
  114. b.Row += 3
  115. // 品名 规格 数量 单位 单价 金额
  116. var drawCol = func(prefix string, value string) error {
  117. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  118. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  119. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  120. if err != nil {
  121. return err
  122. }
  123. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  124. if err != nil {
  125. return err
  126. }
  127. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  128. }
  129. drawCol("A", "采购项目")
  130. drawCol("B", "规格")
  131. drawCol("C", "下单数量")
  132. drawCol("D", "单位")
  133. drawCol("E", "单价")
  134. drawCol("F", "预算金额")
  135. drawCol("G", "交货时间")
  136. drawCol("H", "备注")
  137. return nil
  138. }
  139. func (b *ProductBillExcel) drawTableContent() error {
  140. b.Row += 2
  141. var DrawRow = func(rowIndex int, values ...string) {
  142. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H"}
  143. for i, c := range charas {
  144. v := ""
  145. if i < len(values) {
  146. v = values[i]
  147. }
  148. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  149. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  150. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  151. var magN float64 = 1
  152. lenv := len([]rune(v))
  153. if lenv%10 > 5 {
  154. magN = math.Ceil(float64(lenv) / 10)
  155. } else {
  156. n := math.Floor(float64(lenv) / 10)
  157. if n > 0 {
  158. magN = n
  159. }
  160. }
  161. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21*magN)
  162. }
  163. }
  164. products := b.Content.Products
  165. if len(products) > 0 {
  166. for _, product := range products {
  167. deliveryTime := product.DeliveryTime.Local().Format("2006-01-02")
  168. realCount := "-"
  169. realPrice := "-"
  170. // 预算金额
  171. budgetAmount := fmt.Sprintf("%.3f", float64(product.OrderCount)*product.OrderPrice)
  172. b.FormatToEmpty(&budgetAmount)
  173. // 实际完成数
  174. realCount = fmt.Sprintf("%d", product.ConfirmCount)
  175. b.FormatToEmpty(&realCount)
  176. // 实际金额
  177. realPrice = fmt.Sprintf("%.3f", float64(product.ConfirmCount)*product.OrderPrice)
  178. b.FormatToEmpty(&realPrice)
  179. // a采购项目 b规格 c下单数量 d单位 e完成数量 f单价 g预算金额 h实际金额 i交货时间 j备注
  180. orderCount := fmt.Sprintf("%d", product.OrderCount)
  181. price := fmt.Sprintf("%.3f", product.OrderPrice)
  182. b.FormatToEmpty(&price)
  183. DrawRow(b.Row, product.Name, product.Norm, orderCount, product.Unit, price, budgetAmount, deliveryTime, product.Remark)
  184. b.Row++
  185. }
  186. }
  187. return nil
  188. }
  189. func (b *ProductBillExcel) drawTableFooter() error {
  190. // left1Cell := fmt.Sprintf("A%d", b.Row)
  191. border := []excelize.Border{
  192. {Type: "top", Style: 1, Color: "000000"},
  193. {Type: "left", Style: 1, Color: "000000"},
  194. {Type: "right", Style: 1, Color: "000000"},
  195. {Type: "bottom", Style: 1, Color: "000000"},
  196. }
  197. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  198. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  199. Border: border,
  200. })
  201. sendToStartCell := fmt.Sprintf("A%d", b.Row)
  202. // sendToEndCell := fmt.Sprintf("G%d", b.Row)
  203. // supplierStartCell := fmt.Sprintf("H%d", b.Row)
  204. // supplierEndCell := fmt.Sprintf("J%d", b.Row)
  205. // !!isPdf
  206. // if b.IsPdf == "true" {
  207. sendToEndCell := fmt.Sprintf("E%d", b.Row)
  208. supplierStartCell := fmt.Sprintf("F%d", b.Row)
  209. supplierEndCell := fmt.Sprintf("H%d", b.Row)
  210. // }
  211. b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
  212. b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
  213. b.Excel.SetCellValue(b.SheetName, sendToStartCell, "送货地址:"+b.Content.SendTo)
  214. b.Excel.MergeCell(b.SheetName, supplierStartCell, supplierEndCell)
  215. b.Excel.SetCellStyle(b.SheetName, supplierStartCell, supplierEndCell, styleLeft)
  216. b.Excel.SetCellValue(b.SheetName, supplierStartCell, " 供应商签字:")
  217. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  218. return nil
  219. }
  220. func (b *ProductBillExcel) drawTableSignature() error {
  221. b.Row += 2
  222. // row := b.Row
  223. style1, _ := b.Excel.NewStyle(&excelize.Style{
  224. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  225. })
  226. // 制单人
  227. billUserCellS := fmt.Sprintf("A%d", b.Row+1)
  228. billUserCellE := fmt.Sprintf("B%d", b.Row+1)
  229. b.Excel.MergeCell(b.SheetName, billUserCellS, billUserCellE)
  230. b.Excel.SetCellValue(b.SheetName, billUserCellS, "制单人:"+b.Content.UserName)
  231. fontNum := "E"
  232. image1s := "F"
  233. image2s := "H"
  234. image1e := "G"
  235. image2e := "H"
  236. fontCell := fmt.Sprintf("%s%d", fontNum, b.Row)
  237. fontEndCell := fmt.Sprintf("%s%d", fontNum, b.Row+2)
  238. imageCell1 := fmt.Sprintf("%s%d", image1s, b.Row)
  239. imageEndCell1 := fmt.Sprintf("%s%d", image1e, b.Row+2)
  240. imageCell2 := fmt.Sprintf("%s%d", image2s, b.Row)
  241. imageEndCell2 := fmt.Sprintf("%s%d", image2e, b.Row+2)
  242. b.Excel.MergeCell(b.SheetName, fontCell, fontEndCell)
  243. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  244. b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
  245. // 签字图片1 I10-J12
  246. b.Excel.MergeCell(b.SheetName, imageCell1, imageEndCell1)
  247. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  248. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  249. // 签字图片2 K10-L12
  250. b.Excel.MergeCell(b.SheetName, imageCell2, imageEndCell2)
  251. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  252. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  253. if b.Content.Reviewed == 1 {
  254. imageCells := []string{imageCell1, imageCell2}
  255. if len(b.Signatures) > 0 {
  256. for k, sign := range b.Signatures {
  257. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Path, sign.Format)
  258. }
  259. }
  260. }
  261. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  262. return nil
  263. }
  264. func (b *ProductBillExcel) Draws() {
  265. b.drawTitle()
  266. b.drawSubTitles()
  267. b.drawTableTitle()
  268. b.drawTableContent()
  269. b.drawTableFooter()
  270. b.drawTableSignature()
  271. }
  272. func NewProductBill(f *excelize.File) *ProductBillExcel {
  273. border := []excelize.Border{
  274. {Type: "top", Style: 1, Color: "000000"},
  275. {Type: "left", Style: 1, Color: "000000"},
  276. {Type: "right", Style: 1, Color: "000000"},
  277. {Type: "bottom", Style: 1, Color: "000000"},
  278. }
  279. styleLeft, _ := f.NewStyle(&excelize.Style{
  280. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  281. Border: border,
  282. Font: &excelize.Font{Size: 10},
  283. })
  284. b := &ProductBillExcel{
  285. Title: "原材料采购单",
  286. SheetName: "Sheet1",
  287. Excel: f,
  288. Offset: 0,
  289. AlignCenterStyle: styleLeft,
  290. Signatures: make([]*model.Signature, 0),
  291. }
  292. // f.SetColWidth(b.SheetName, "A", "A", 17)
  293. // f.SetColWidth(b.SheetName, "B", "B", 12)
  294. // f.SetColWidth(b.SheetName, "C", "H", 10.5)
  295. // f.SetColWidth(b.SheetName, "I", "J", 12)
  296. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0.25), excelize.PageMarginRight(0))
  297. return b
  298. }
  299. func (b *ProductBillExcel) FormatToEmpty(str *string) {
  300. if *str == "0" || *str == "0.000" {
  301. *str = ""
  302. }
  303. }
  304. func (b *ProductBillExcel) SetContent(content interface{}) {
  305. b.Content = content.(*model.ProductBill)
  306. }
  307. func (b *ProductBillExcel) SetTitle(title string) {
  308. b.Title = title
  309. }
  310. func (b *ProductBillExcel) SetRow(row int) {
  311. b.Row = row
  312. }
  313. func (b *ProductBillExcel) SetIsPdf(isPdf string) {
  314. b.IsPdf = isPdf
  315. }
  316. func (b *ProductBillExcel) GetRow() int {
  317. return b.Row
  318. }
  319. func (b *ProductBillExcel) SetSignatures(sign interface{}) {
  320. b.Signatures = sign.([]*model.Signature)
  321. }