bill-purchase-excel.go 13 KB

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