bill-product-excel.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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("F%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("G%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)
  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("A%d", b.Row)
  189. b.Excel.MergeCell(b.SheetName, addCel, fmt.Sprintf("G%d", b.Row))
  190. b.Excel.SetCellStyle(b.SheetName, addCel, fmt.Sprintf("G%d", b.Row), styleLeft)
  191. // b.Excel.MergeCell(b.SheetName, addCel, fmt.Sprintf("E%d", b.Row))
  192. // b.Excel.SetCellStyle(b.SheetName, addCel, fmt.Sprintf("E%d", b.Row), styleLeft)
  193. b.Excel.SetCellValue(b.SheetName, addCel, "送货地址:"+b.Content.SendTo)
  194. // sureCel := fmt.Sprintf("F%d", b.Row)
  195. sureCel := fmt.Sprintf("H%d", b.Row)
  196. b.Excel.MergeCell(b.SheetName, sureCel, fmt.Sprintf("J%d", b.Row))
  197. b.Excel.SetCellStyle(b.SheetName, sureCel, fmt.Sprintf("J%d", b.Row), styleLeft)
  198. b.Excel.SetCellValue(b.SheetName, sureCel, " 收货人:")
  199. b.Excel.SetRowHeight(b.SheetName, b.Row, 30)
  200. return nil
  201. }
  202. func (b *ProductBillExcel) drawRemark() error {
  203. // 填备注
  204. b.Row++
  205. remarkTitleCell := fmt.Sprintf("A%d", b.Row)
  206. b.Excel.SetCellValue(b.SheetName, remarkTitleCell, "备注:")
  207. b.Row++
  208. remarkContentScell := fmt.Sprintf("A%d", b.Row)
  209. // 预留五行备注内容
  210. reg := regexp.MustCompile(`\n`)
  211. remarkRowNum := len(reg.FindAllString(b.Content.Remark, -1)) + 1
  212. b.Row += remarkRowNum
  213. remarkContentEcell := fmt.Sprintf("J%d", b.Row)
  214. b.Excel.MergeCell(b.SheetName, remarkContentScell, remarkContentEcell)
  215. b.Excel.SetCellValue(b.SheetName, remarkContentScell, b.Content.Remark)
  216. // 签字位置
  217. b.Row += 2
  218. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  219. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  220. Font: &excelize.Font{Size: 11}})
  221. if err != nil {
  222. return err
  223. }
  224. var drawLeft = func(rowIndex int, value string) error {
  225. //左边1
  226. left1Cell := fmt.Sprintf("A%d", rowIndex)
  227. err := b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("E%d", rowIndex))
  228. if err != nil {
  229. return err
  230. }
  231. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  232. if err != nil {
  233. return err
  234. }
  235. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  236. return nil
  237. }
  238. var drawRight = func(rowIndex int, value string) error {
  239. right1Cell := fmt.Sprintf("F%d", rowIndex)
  240. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("J%d", rowIndex))
  241. if err != nil {
  242. return err
  243. }
  244. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleLeft)
  245. if err != nil {
  246. return err
  247. }
  248. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  249. return nil
  250. }
  251. //第一行
  252. drawLeft(b.Row, "甲方:"+b.Title)
  253. drawRight(b.Row, "乙方:"+b.Content.Supplier)
  254. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  255. //第二行
  256. drawLeft(b.Row+1, "联系人:")
  257. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号")
  258. drawRight(b.Row+1, "联系人:")
  259. b.Excel.SetRowHeight(b.SheetName, b.Row+1, 21)
  260. //第三行
  261. drawLeft(b.Row+2, "时间:"+timeformat)
  262. drawRight(b.Row+2, "时间:"+timeformat)
  263. b.Excel.SetRowHeight(b.SheetName, b.Row+2, 21)
  264. b.Row += 2
  265. return nil
  266. }
  267. func (b *ProductBillExcel) drawTableSignature() error {
  268. b.Row += 2
  269. style1, _ := b.Excel.NewStyle(&excelize.Style{
  270. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  271. // Border: border,
  272. })
  273. // 制单人
  274. billUserCell := fmt.Sprintf("A%d", b.Row+1)
  275. b.Excel.SetCellValue(b.SheetName, billUserCell, "制单人:"+b.Content.UserName)
  276. // billUservCell := fmt.Sprintf("B%d", b.Row+1)
  277. // b.Excel.SetCellValue(b.SheetName, billUservCell, b.Content.UserName)
  278. fontCell := fmt.Sprintf("F%d", b.Row)
  279. imageCell1 := fmt.Sprintf("G%d", b.Row)
  280. imageCell2 := fmt.Sprintf("I%d", b.Row)
  281. eRow := b.Row + 2
  282. b.Excel.MergeCell(b.SheetName, fontCell, fmt.Sprintf("F%d", eRow))
  283. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  284. b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
  285. b.Excel.MergeCell(b.SheetName, imageCell1, fmt.Sprintf("H%d", eRow))
  286. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  287. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  288. b.Excel.MergeCell(b.SheetName, imageCell2, fmt.Sprintf("J%d", eRow))
  289. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  290. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  291. // 状态为已审核时,签字
  292. // `{
  293. // "x_scale": 0.12,
  294. // "y_scale": 0.12,
  295. // "x_offset": 30,
  296. // "print_obj": true,
  297. // "lock_aspect_ratio": false,
  298. // "locked": false,
  299. // "positioning": "oncell"
  300. // }`
  301. if b.Content.Reviewed == 1 {
  302. imageCells := []string{imageCell1, imageCell2}
  303. if len(b.Signatures) > 0 {
  304. for k, sign := range b.Signatures {
  305. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Path, sign.Format)
  306. }
  307. }
  308. }
  309. return nil
  310. }
  311. func (b *ProductBillExcel) Draws() {
  312. b.drawTitle()
  313. b.drawSubTitles()
  314. b.drawTableTitle()
  315. b.drawTableContent()
  316. b.drawTableFooter()
  317. b.drawRemark()
  318. b.drawTableSignature()
  319. }
  320. func NewProductBill(f *excelize.File) *ProductBillExcel {
  321. border := []excelize.Border{
  322. {Type: "top", Style: 1, Color: "000000"},
  323. {Type: "left", Style: 1, Color: "000000"},
  324. {Type: "right", Style: 1, Color: "000000"},
  325. {Type: "bottom", Style: 1, Color: "000000"},
  326. }
  327. styleLeft, _ := f.NewStyle(&excelize.Style{
  328. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  329. Border: border,
  330. Font: &excelize.Font{Size: 10},
  331. })
  332. b := &ProductBillExcel{
  333. Title: "原材料采购单",
  334. SheetName: "Sheet1",
  335. Excel: f,
  336. Offset: 0,
  337. AlignCenterStyle: styleLeft,
  338. Signatures: make([]*model.Signature, 0),
  339. }
  340. // f.SetColWidth(b.SheetName, "A", "J", 11.5)
  341. f.SetColWidth(b.SheetName, "A", "A", 17)
  342. f.SetColWidth(b.SheetName, "B", "B", 12)
  343. f.SetColWidth(b.SheetName, "C", "H", 10.5)
  344. f.SetColWidth(b.SheetName, "I", "J", 12)
  345. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0.25), excelize.PageMarginRight(0))
  346. return b
  347. }
  348. func (b *ProductBillExcel) FormatToEmpty(str *string) {
  349. if *str == "0" || *str == "0.000" {
  350. *str = ""
  351. }
  352. }
  353. func (b *ProductBillExcel) SetContent(content interface{}) {
  354. b.Content = content.(*model.ProductBill)
  355. }
  356. func (b *ProductBillExcel) SetTitle(title string) {
  357. b.Title = title
  358. }
  359. func (b *ProductBillExcel) SetRow(row int) {
  360. b.Row = row
  361. }
  362. func (b *ProductBillExcel) GetRow() int {
  363. return b.Row
  364. }
  365. func (b *ProductBillExcel) SetSignatures(sign interface{}) {
  366. b.Signatures = sign.([]*model.Signature)
  367. }