bill-purchase-excel.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "strconv"
  6. _ "image/gif"
  7. _ "image/jpeg"
  8. _ "image/png"
  9. "github.com/xuri/excelize/v2"
  10. )
  11. type PurchaseBillExcel struct {
  12. Offset int
  13. Title string //标题
  14. Excel *excelize.File
  15. SheetName string
  16. AlignCenterStyle int
  17. Content *model.PurchaseBill
  18. Signatures []*model.Signature
  19. }
  20. func (b *PurchaseBillExcel) drawTitle() error {
  21. marginLeft := excelize.PageMarginLeft(0.15)
  22. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  23. tileIndex := b.Offset + 1
  24. startCell := fmt.Sprintf("A%d", tileIndex)
  25. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("L%d", tileIndex))
  26. if err != nil {
  27. return err
  28. }
  29. style, err := b.Excel.NewStyle(&excelize.Style{
  30. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  31. Font: &excelize.Font{Bold: true, Size: 18}})
  32. if err != nil {
  33. return err
  34. }
  35. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  36. if err != nil {
  37. return err
  38. }
  39. b.Excel.SetRowHeight(b.SheetName, tileIndex, 23)
  40. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  41. return nil
  42. }
  43. func (b *PurchaseBillExcel) drawSubTitles() error {
  44. row := b.Offset + 2
  45. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  46. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  47. Font: &excelize.Font{Size: 11}})
  48. if err != nil {
  49. return err
  50. }
  51. styleRight, err := b.Excel.NewStyle(&excelize.Style{
  52. Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
  53. Font: &excelize.Font{Size: 11}})
  54. if err != nil {
  55. return err
  56. }
  57. var drawLeft = func(rowIndex int, value string) error {
  58. //左边1
  59. left1Cell := fmt.Sprintf("A%d", rowIndex)
  60. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("E%d", rowIndex))
  61. if err != nil {
  62. return err
  63. }
  64. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  65. if err != nil {
  66. return err
  67. }
  68. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  69. return nil
  70. }
  71. var drawRight = func(rowIndex int, value string) error {
  72. right1Cell := fmt.Sprintf("F%d", rowIndex)
  73. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("L%d", rowIndex))
  74. if err != nil {
  75. return err
  76. }
  77. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  78. if err != nil {
  79. return err
  80. }
  81. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  82. return nil
  83. }
  84. //第一行
  85. drawLeft(row, "类别:"+b.Content.Type)
  86. drawRight(row, "单号:"+b.Content.SerialNumber)
  87. b.Excel.SetRowHeight(b.SheetName, row, 21)
  88. //第二行
  89. drawLeft(row+1, "供应商名称:"+b.Content.Supplier)
  90. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  91. drawRight(row+1, "下单时间:"+timeformat)
  92. b.Excel.SetRowHeight(b.SheetName, row+1, 21)
  93. //第三行
  94. drawLeft(row+2, "产品名称:"+b.Content.ProductName)
  95. status := ""
  96. if b.Content.Status == "complete" {
  97. status = fmt.Sprintf("已完成(%d)", b.Content.ConfirmCount)
  98. }
  99. if b.Content.Status == "created" {
  100. status = "进行中"
  101. }
  102. drawRight(row+2, "状态:"+status)
  103. b.Excel.SetRowHeight(b.SheetName, row+2, 21)
  104. return nil
  105. }
  106. func (b *PurchaseBillExcel) drawTableTitle() error {
  107. row := b.Offset + 5
  108. //A采购项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I
  109. var drawCol = func(prefix string, value string) error {
  110. left1Cell := fmt.Sprintf("%s%d", prefix, row)
  111. left2Cell := fmt.Sprintf("%s%d", prefix, row+1)
  112. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  113. if err != nil {
  114. return err
  115. }
  116. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  117. if err != nil {
  118. return err
  119. }
  120. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  121. }
  122. var drawCol2 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
  123. left1Cell := fmt.Sprintf("%s%d", prefix1, row)
  124. left2Cell := fmt.Sprintf("%s%d", prefix2, row)
  125. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  126. if err != nil {
  127. return err
  128. }
  129. if err != nil {
  130. fmt.Println(err)
  131. return err
  132. }
  133. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  134. if err != nil {
  135. return err
  136. }
  137. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  138. val2Cel := fmt.Sprintf("%s%d", prefix1, row+1)
  139. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  140. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  141. val3Cel := fmt.Sprintf("%s%d", prefix2, row+1)
  142. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  143. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  144. return nil
  145. }
  146. drawCol("A", "采购项目")
  147. drawCol("B", "规格(克)")
  148. drawCol("E", "数量")
  149. drawCol("F", "完成数量")
  150. drawCol2("C", "D", "尺寸(mm)", "长", "宽")
  151. drawCol2("C", "D", "尺寸(mm)", "长", "宽")
  152. unit1 := "-"
  153. unit2 := "-"
  154. if len(b.Content.Paper) > 0 {
  155. if b.Content.Paper[0].PriceUnit != "" {
  156. unit1 = b.Content.Paper[0].PriceUnit
  157. } else if b.Content.Paper[0].Price2Unit != "" {
  158. unit2 = b.Content.Paper[0].Price2Unit
  159. } else {
  160. unit1 = "元/张"
  161. unit2 = "元/吨"
  162. }
  163. if b.Content.Paper[0].PriceUnit != "" && b.Content.Paper[0].Price2Unit != "" {
  164. unit1 = b.Content.Paper[0].PriceUnit
  165. unit2 = b.Content.Paper[0].Price2Unit
  166. }
  167. }
  168. drawCol2("G", "H", "单价", unit1, unit2)
  169. drawCol("I", "预算金额")
  170. drawCol("J", "实际金额")
  171. drawCol("K", "交货时间")
  172. drawCol("L", "备注")
  173. return nil
  174. }
  175. func (b *PurchaseBillExcel) drawTableContent() error {
  176. row := b.Offset + 7
  177. var DrawRow = func(rowIndex int, values ...string) {
  178. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}
  179. for i, c := range charas {
  180. v := ""
  181. if i < len(values) {
  182. v = values[i]
  183. }
  184. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  185. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  186. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  187. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  188. }
  189. }
  190. papers := b.Content.Paper
  191. if len(papers) > 0 {
  192. for _, paper := range papers {
  193. deliveryTime := paper.DeliveryTime.Local().Format("2006-01-02")
  194. confirmCount := "-"
  195. realAmount := "-"
  196. price, err := strconv.ParseFloat(paper.Price, 64)
  197. if err != nil {
  198. price = 0.00
  199. }
  200. // 预算金额
  201. budgetAmount := fmt.Sprintf("%.2f", float64(paper.Count)*price)
  202. b.FormatToEmpty(&budgetAmount)
  203. if b.Content.Status == "complete" {
  204. // 实际完成数
  205. confirmCount = fmt.Sprintf("%d", b.Content.ConfirmCount)
  206. b.FormatToEmpty(&confirmCount)
  207. // 实际金额
  208. realAmount = fmt.Sprintf("%.2f", float64(b.Content.ConfirmCount)*price)
  209. b.FormatToEmpty(&realAmount)
  210. }
  211. DrawRow(row, paper.Name, paper.Norm, paper.Height, paper.Width, fmt.Sprintf("%d", paper.Count), confirmCount, paper.Price, paper.Price2, budgetAmount, realAmount, deliveryTime, paper.Remark)
  212. row++
  213. }
  214. }
  215. return nil
  216. }
  217. func (b *PurchaseBillExcel) drawTableFooter() error {
  218. row := b.Offset + 8
  219. left1Cell := fmt.Sprintf("A%d", row)
  220. border := []excelize.Border{
  221. {Type: "top", Style: 1, Color: "000000"},
  222. {Type: "left", Style: 1, Color: "000000"},
  223. {Type: "right", Style: 1, Color: "000000"},
  224. {Type: "bottom", Style: 1, Color: "000000"},
  225. }
  226. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  227. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  228. Border: border,
  229. })
  230. b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  231. b.Excel.SetCellValue(b.SheetName, left1Cell, "送货地址")
  232. addCel := fmt.Sprintf("B%d", row)
  233. b.Excel.MergeCell(b.SheetName, addCel, fmt.Sprintf("E%d", row))
  234. b.Excel.SetCellStyle(b.SheetName, addCel, fmt.Sprintf("E%d", row), styleLeft)
  235. b.Excel.SetCellValue(b.SheetName, addCel, b.Content.SendTo)
  236. sureCel := fmt.Sprintf("F%d", row)
  237. b.Excel.MergeCell(b.SheetName, sureCel, fmt.Sprintf("L%d", row))
  238. b.Excel.SetCellStyle(b.SheetName, sureCel, fmt.Sprintf("L%d", row), styleLeft)
  239. b.Excel.SetCellValue(b.SheetName, sureCel, "供应商签字:")
  240. b.Excel.SetRowHeight(b.SheetName, row, 21)
  241. return nil
  242. }
  243. func (b *PurchaseBillExcel) drawTableSignature() error {
  244. row := b.Offset + 11
  245. // border := []excelize.Border{
  246. // {Type: "top", Style: 1, Color: "000000"},
  247. // {Type: "left", Style: 1, Color: "000000"},
  248. // {Type: "right", Style: 1, Color: "000000"},
  249. // {Type: "bottom", Style: 1, Color: "000000"},
  250. // }
  251. style1, _ := b.Excel.NewStyle(&excelize.Style{
  252. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  253. // Border: border,
  254. })
  255. fontCell := fmt.Sprintf("H%d", row)
  256. imageCell1 := fmt.Sprintf("I%d", row)
  257. imageCell2 := fmt.Sprintf("K%d", row)
  258. b.Excel.MergeCell(b.SheetName, fontCell, fmt.Sprintf("H%d", row+2))
  259. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  260. b.Excel.SetCellValue(b.SheetName, fontCell, "领导签字:")
  261. // 签字图片1 I10-J12
  262. b.Excel.MergeCell(b.SheetName, imageCell1, fmt.Sprintf("J%d", row+2))
  263. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  264. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  265. // 签字图片2 K10-L12
  266. b.Excel.MergeCell(b.SheetName, imageCell2, fmt.Sprintf("L%d", row+2))
  267. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  268. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  269. // 状态为已审核时,签字
  270. // `{
  271. // "x_scale": 0.12,
  272. // "y_scale": 0.12,
  273. // "x_offset": 30,
  274. // "print_obj": true,
  275. // "lock_aspect_ratio": false,
  276. // "locked": false,
  277. // "positioning": "oncell"
  278. // }`
  279. if b.Content.Reviewed == 1 {
  280. imageCells := []string{imageCell1, imageCell2}
  281. if len(b.Signatures) > 0 {
  282. for k, sign := range b.Signatures {
  283. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Url, sign.Format)
  284. }
  285. }
  286. }
  287. b.Excel.SetRowHeight(b.SheetName, row, 21)
  288. return nil
  289. }
  290. func (b *PurchaseBillExcel) Draws() {
  291. b.drawTitle()
  292. b.drawSubTitles()
  293. b.drawTableTitle()
  294. b.drawTableContent()
  295. b.drawTableFooter()
  296. b.drawTableSignature()
  297. }
  298. func NewPurchaseBill(f *excelize.File) *PurchaseBillExcel {
  299. border := []excelize.Border{
  300. {Type: "top", Style: 1, Color: "000000"},
  301. {Type: "left", Style: 1, Color: "000000"},
  302. {Type: "right", Style: 1, Color: "000000"},
  303. {Type: "bottom", Style: 1, Color: "000000"},
  304. }
  305. styleLeft, _ := f.NewStyle(&excelize.Style{
  306. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  307. Border: border,
  308. Font: &excelize.Font{Size: 10},
  309. })
  310. b := &PurchaseBillExcel{
  311. Title: "原材料采购单",
  312. SheetName: "Sheet1",
  313. Excel: f,
  314. Offset: 0,
  315. AlignCenterStyle: styleLeft,
  316. Signatures: make([]*model.Signature, 0),
  317. }
  318. f.SetColWidth(b.SheetName, "A", "A", 12)
  319. f.SetColWidth(b.SheetName, "B", "K", 9.5)
  320. // f.SetColWidth(b.SheetName, "C", "H", 9.5)
  321. f.SetColWidth(b.SheetName, "L", "L", 10)
  322. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  323. return b
  324. }
  325. func (b *PurchaseBillExcel) FormatToEmpty(str *string) {
  326. if *str == "0" || *str == "0.00" {
  327. *str = ""
  328. }
  329. }