bill-product-excel.go 12 KB

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