bill-product-excel.go 12 KB

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