bill-product-excel.go 12 KB

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