bill-purchase-excel.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. }
  175. if b.Content.Paper[0].Price2Unit != "" {
  176. unit2 = b.Content.Paper[0].Price2Unit
  177. }
  178. }
  179. drawCol("A", "采购项目")
  180. drawCol("B", "规格(克)")
  181. drawCol2("C", "D", "尺寸(mm)", "长", "宽")
  182. drawCol("E", "下单数量")
  183. drawCol2("F", "G", "单价", unit1, unit2)
  184. drawCol("H", "预算金额")
  185. drawCol("I", "交货时间")
  186. drawCol("J", "备注")
  187. return nil
  188. }
  189. func (b *PurchaseBillExcel) drawTableContent() error {
  190. b.Row += 2
  191. var DrawRow = func(rowIndex int, values ...string) {
  192. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}
  193. for i, c := range charas {
  194. v := ""
  195. if i < len(values) {
  196. v = values[i]
  197. }
  198. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  199. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  200. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  201. // var magN float64 = 1
  202. // lenv := len([]rune(v))
  203. // magN = math.Ceil(float64(lenv) / 10)
  204. // if lenv%10 > 5 {
  205. // magN = math.Ceil(float64(lenv) / 10)
  206. // } else {
  207. // n := math.Floor(float64(lenv) / 10)
  208. // if n > 0 {
  209. // magN = n
  210. // }
  211. // }
  212. // b.Excel.SetRowHeight(b.SheetName, rowIndex, 21*magN)
  213. b.Excel.SetRowHeight(b.SheetName, rowIndex, 46)
  214. }
  215. }
  216. papers := b.Content.Paper
  217. if len(papers) > 0 {
  218. for _, paper := range papers {
  219. deliveryTime := paper.DeliveryTime.Local().Format("2006-01-02")
  220. realCount := ""
  221. realPrice := ""
  222. price := fmt.Sprintf("%.3f", paper.OrderPrice)
  223. b.FormatToEmpty(&price)
  224. price2 := fmt.Sprintf("%.3f", paper.Price2)
  225. b.FormatToEmpty(&price2)
  226. // 预算金额
  227. budgetAmount := fmt.Sprintf("%.3f", paper.OrderPrice*float64(paper.OrderCount))
  228. b.FormatToEmpty(&budgetAmount)
  229. // 实际完成数
  230. realCount = fmt.Sprintf("%d", paper.ConfirmCount)
  231. b.FormatToEmpty(&realCount)
  232. // 实际金额
  233. realPrice = fmt.Sprintf("%.3f", paper.OrderPrice*float64(paper.ConfirmCount))
  234. b.FormatToEmpty(&realPrice)
  235. DrawRow(b.Row, paper.Name, paper.Norm, paper.Height, paper.Width, fmt.Sprintf("%d", paper.OrderCount), price, price2, budgetAmount, deliveryTime, paper.Remark)
  236. b.Row++
  237. }
  238. }
  239. return nil
  240. }
  241. func (b *PurchaseBillExcel) drawTableFooter() error {
  242. border := []excelize.Border{
  243. {Type: "top", Style: 1, Color: "000000"},
  244. {Type: "left", Style: 1, Color: "000000"},
  245. {Type: "right", Style: 1, Color: "000000"},
  246. {Type: "bottom", Style: 1, Color: "000000"},
  247. }
  248. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  249. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  250. Border: border,
  251. })
  252. sendToStartCell := fmt.Sprintf("A%d", b.Row)
  253. sendToEndCell := fmt.Sprintf("G%d", b.Row)
  254. supplierStartCell := fmt.Sprintf("H%d", b.Row)
  255. supplierEndCell := fmt.Sprintf("J%d", b.Row)
  256. b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
  257. b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
  258. b.Excel.SetCellValue(b.SheetName, sendToStartCell, "送货地址:"+b.Content.SendTo)
  259. b.Excel.MergeCell(b.SheetName, supplierStartCell, supplierEndCell)
  260. b.Excel.SetCellStyle(b.SheetName, supplierStartCell, supplierEndCell, styleLeft)
  261. b.Excel.SetCellValue(b.SheetName, supplierStartCell, "供应商签字:")
  262. b.Excel.SetRowHeight(b.SheetName, b.Row, 32)
  263. return nil
  264. }
  265. func (b *PurchaseBillExcel) drawSupplierRemark() error {
  266. b.Row++
  267. border := []excelize.Border{
  268. {Type: "top", Style: 1, Color: "000000"},
  269. {Type: "left", Style: 1, Color: "000000"},
  270. {Type: "right", Style: 1, Color: "000000"},
  271. {Type: "bottom", Style: 1, Color: "000000"},
  272. }
  273. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  274. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  275. Border: border,
  276. })
  277. sendToStartCell := fmt.Sprintf("A%d", b.Row)
  278. sendToEndCell := fmt.Sprintf("J%d", b.Row)
  279. b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
  280. b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
  281. b.Excel.SetCellValue(b.SheetName, sendToStartCell, "供应商备注:"+b.Content.SupplierRemark)
  282. b.Excel.SetRowHeight(b.SheetName, b.Row, 32)
  283. return nil
  284. }
  285. func (b *PurchaseBillExcel) drawTableSignature() error {
  286. b.Row += 2
  287. style1, _ := b.Excel.NewStyle(&excelize.Style{
  288. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  289. })
  290. // 制单人
  291. billUserCellS := fmt.Sprintf("A%d", b.Row+1)
  292. billUserCellE := fmt.Sprintf("B%d", b.Row+1)
  293. b.Excel.MergeCell(b.SheetName, billUserCellS, billUserCellE)
  294. b.Excel.SetCellValue(b.SheetName, billUserCellS, "制单人:"+b.Content.UserName)
  295. fontNum := "G"
  296. image1s := "H"
  297. image2s := "J"
  298. image1e := "I"
  299. image2e := "J"
  300. fontCell := fmt.Sprintf("%s%d", fontNum, b.Row)
  301. fontEndCell := fmt.Sprintf("%s%d", fontNum, b.Row+2)
  302. imageCell1 := fmt.Sprintf("%s%d", image1s, b.Row)
  303. imageEndCell1 := fmt.Sprintf("%s%d", image1e, b.Row+2)
  304. imageCell2 := fmt.Sprintf("%s%d", image2s, b.Row)
  305. imageEndCell2 := fmt.Sprintf("%s%d", image2e, b.Row+2)
  306. b.Excel.MergeCell(b.SheetName, fontCell, fontEndCell)
  307. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  308. b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
  309. // 签字图片1 I10-J12
  310. b.Excel.MergeCell(b.SheetName, imageCell1, imageEndCell1)
  311. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  312. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  313. // 签字图片2 K10-L12
  314. b.Excel.MergeCell(b.SheetName, imageCell2, imageEndCell2)
  315. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  316. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  317. if b.Content.Reviewed == 1 {
  318. imageCells := []string{imageCell1, imageCell2}
  319. if len(b.Signatures) > 0 {
  320. for k, sign := range b.Signatures {
  321. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Path, sign.Format)
  322. }
  323. }
  324. }
  325. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  326. return nil
  327. }
  328. func (b *PurchaseBillExcel) Draws() {
  329. b.drawTitle()
  330. b.drawSubTitles()
  331. b.drawTableTitle()
  332. b.drawTableContent()
  333. b.drawTableFooter()
  334. if len(b.Content.SupplierRemark) > 1 {
  335. b.drawSupplierRemark()
  336. }
  337. b.drawTableSignature()
  338. }
  339. func NewPurchaseBill(f *excelize.File) *PurchaseBillExcel {
  340. border := []excelize.Border{
  341. {Type: "top", Style: 1, Color: "000000"},
  342. {Type: "left", Style: 1, Color: "000000"},
  343. {Type: "right", Style: 1, Color: "000000"},
  344. {Type: "bottom", Style: 1, Color: "000000"},
  345. }
  346. styleLeft, _ := f.NewStyle(&excelize.Style{
  347. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  348. Border: border,
  349. Font: &excelize.Font{Size: 10},
  350. })
  351. b := &PurchaseBillExcel{
  352. Title: "原材料采购单",
  353. SheetName: "Sheet1",
  354. Excel: f,
  355. Offset: 0,
  356. AlignCenterStyle: styleLeft,
  357. Signatures: make([]*model.Signature, 0),
  358. }
  359. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  360. return b
  361. }
  362. func (b *PurchaseBillExcel) FormatToEmpty(str *string) {
  363. if *str == "0" || *str == "0.000" {
  364. *str = "-"
  365. }
  366. }
  367. func (b *PurchaseBillExcel) SetContent(content interface{}) {
  368. b.Content = content.(*model.PurchaseBill)
  369. }
  370. func (b *PurchaseBillExcel) SetTitle(title string) {
  371. b.Title = title
  372. }
  373. func (b *PurchaseBillExcel) SetSheetName(name string) {
  374. b.SheetName = name
  375. }
  376. func (b *PurchaseBillExcel) SetRow(row int) {
  377. b.Row = row
  378. }
  379. func (b *PurchaseBillExcel) SetIsPdf(isPdf string) {
  380. b.IsPdf = isPdf
  381. }
  382. func (b *PurchaseBillExcel) GetRow() int {
  383. return b.Row
  384. }
  385. func (b *PurchaseBillExcel) SetSignatures(sign interface{}) {
  386. b.Signatures = sign.([]*model.Signature)
  387. }