bill-product-excel.go 13 KB

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