bill-purchase-excel.go 12 KB

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