bill-purchase-excel.go 13 KB

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