bill-purchase-excel.go 12 KB

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