bill-purchase-excel.go 11 KB

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