bill-product-excel.go 12 KB

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