bill-purchase-excel.go 11 KB

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