bill-purchase-excel.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "strconv"
  6. _ "image/gif"
  7. _ "image/jpeg"
  8. _ "image/png"
  9. "github.com/xuri/excelize/v2"
  10. )
  11. type PurchaseBillExcel struct {
  12. Offset int
  13. Row int
  14. Title string //标题
  15. Excel *excelize.File
  16. SheetName string
  17. AlignCenterStyle int
  18. Content *model.PurchaseBill
  19. Signatures []*model.Signature
  20. }
  21. func (b *PurchaseBillExcel) drawTitle() error {
  22. marginLeft := excelize.PageMarginLeft(0.15)
  23. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  24. tileIndex := b.Offset + 1
  25. startCell := fmt.Sprintf("A%d", tileIndex)
  26. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("L%d", tileIndex))
  27. if err != nil {
  28. return err
  29. }
  30. style, err := b.Excel.NewStyle(&excelize.Style{
  31. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  32. Font: &excelize.Font{Bold: true, Size: 18}})
  33. if err != nil {
  34. return err
  35. }
  36. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  37. if err != nil {
  38. return err
  39. }
  40. b.Excel.SetRowHeight(b.SheetName, tileIndex, 23)
  41. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  42. return nil
  43. }
  44. func (b *PurchaseBillExcel) drawSubTitles() error {
  45. row := b.Offset + 2
  46. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  47. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  48. Font: &excelize.Font{Size: 11}})
  49. if err != nil {
  50. return err
  51. }
  52. styleRight, err := b.Excel.NewStyle(&excelize.Style{
  53. Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
  54. Font: &excelize.Font{Size: 11}})
  55. if err != nil {
  56. return err
  57. }
  58. var drawLeft = func(rowIndex int, value string) error {
  59. //左边1
  60. left1Cell := fmt.Sprintf("A%d", rowIndex)
  61. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("E%d", rowIndex))
  62. if err != nil {
  63. return err
  64. }
  65. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  66. if err != nil {
  67. return err
  68. }
  69. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  70. return nil
  71. }
  72. var drawRight = func(rowIndex int, value string) error {
  73. right1Cell := fmt.Sprintf("F%d", rowIndex)
  74. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("L%d", rowIndex))
  75. if err != nil {
  76. return err
  77. }
  78. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  79. if err != nil {
  80. return err
  81. }
  82. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  83. return nil
  84. }
  85. //第一行
  86. drawLeft(row, "类别:"+b.Content.Type)
  87. drawRight(row, "单号:"+b.Content.SerialNumber)
  88. b.Excel.SetRowHeight(b.SheetName, row, 21)
  89. //第二行
  90. drawLeft(row+1, "供应商名称:"+b.Content.Supplier)
  91. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  92. drawRight(row+1, "下单时间:"+timeformat)
  93. b.Excel.SetRowHeight(b.SheetName, row+1, 21)
  94. //第三行
  95. drawLeft(row+2, "产品名称:"+b.Content.ProductName)
  96. status := ""
  97. if b.Content.Status == "complete" {
  98. status = fmt.Sprintf("已完成(%d)", b.Content.ConfirmCount)
  99. }
  100. if b.Content.Status == "created" {
  101. status = "进行中"
  102. }
  103. drawRight(row+2, "状态:"+status)
  104. b.Excel.SetRowHeight(b.SheetName, row+2, 21)
  105. return nil
  106. }
  107. func (b *PurchaseBillExcel) drawTableTitle() error {
  108. row := b.Offset + 5
  109. //A采购项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I
  110. var drawCol = func(prefix string, value string) error {
  111. left1Cell := fmt.Sprintf("%s%d", prefix, row)
  112. left2Cell := fmt.Sprintf("%s%d", prefix, row+1)
  113. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  114. if err != nil {
  115. return err
  116. }
  117. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  118. if err != nil {
  119. return err
  120. }
  121. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  122. }
  123. var drawCol2 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
  124. left1Cell := fmt.Sprintf("%s%d", prefix1, row)
  125. left2Cell := fmt.Sprintf("%s%d", prefix2, row)
  126. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  127. if err != nil {
  128. return err
  129. }
  130. if err != nil {
  131. fmt.Println(err)
  132. return err
  133. }
  134. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  135. if err != nil {
  136. return err
  137. }
  138. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  139. val2Cel := fmt.Sprintf("%s%d", prefix1, row+1)
  140. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  141. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  142. val3Cel := fmt.Sprintf("%s%d", prefix2, row+1)
  143. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  144. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  145. return nil
  146. }
  147. drawCol("A", "采购项目")
  148. drawCol("B", "规格(克)")
  149. drawCol("E", "数量")
  150. drawCol("F", "完成数量")
  151. drawCol2("C", "D", "尺寸(mm)", "长", "宽")
  152. drawCol2("C", "D", "尺寸(mm)", "长", "宽")
  153. unit1 := "-"
  154. unit2 := "-"
  155. // ??? for ragne
  156. if len(b.Content.Paper) > 0 {
  157. if b.Content.Paper[0].PriceUnit != "" {
  158. unit1 = b.Content.Paper[0].PriceUnit
  159. } else if b.Content.Paper[0].Price2Unit != "" {
  160. unit2 = b.Content.Paper[0].Price2Unit
  161. } else {
  162. unit1 = "元/张"
  163. unit2 = "元/吨"
  164. }
  165. if b.Content.Paper[0].PriceUnit != "" && b.Content.Paper[0].Price2Unit != "" {
  166. unit1 = b.Content.Paper[0].PriceUnit
  167. unit2 = b.Content.Paper[0].Price2Unit
  168. }
  169. }
  170. drawCol2("G", "H", "单价", unit1, unit2)
  171. drawCol("I", "预算金额")
  172. drawCol("J", "实际金额")
  173. drawCol("K", "交货时间")
  174. drawCol("L", "备注")
  175. return nil
  176. }
  177. func (b *PurchaseBillExcel) drawTableContent() error {
  178. row := b.Offset + 7
  179. b.Row = row
  180. var DrawRow = func(rowIndex int, values ...string) {
  181. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}
  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. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  191. }
  192. }
  193. papers := b.Content.Paper
  194. if len(papers) > 0 {
  195. for _, paper := range papers {
  196. deliveryTime := paper.DeliveryTime.Local().Format("2006-01-02")
  197. confirmCount := "-"
  198. realAmount := "-"
  199. price, err := strconv.ParseFloat(paper.Price, 64)
  200. if err != nil {
  201. price = 0.00
  202. }
  203. // 预算金额
  204. budgetAmount := fmt.Sprintf("%.2f", float64(paper.Count)*price)
  205. b.FormatToEmpty(&budgetAmount)
  206. if b.Content.Status == "complete" {
  207. // 实际完成数
  208. confirmCount = fmt.Sprintf("%d", b.Content.ConfirmCount)
  209. b.FormatToEmpty(&confirmCount)
  210. // 实际金额
  211. realAmount = fmt.Sprintf("%.2f", float64(b.Content.ConfirmCount)*price)
  212. b.FormatToEmpty(&realAmount)
  213. }
  214. DrawRow(row, paper.Name, paper.Norm, paper.Height, paper.Width, fmt.Sprintf("%d", paper.Count), confirmCount, paper.Price, paper.Price2, budgetAmount, realAmount, deliveryTime, paper.Remark)
  215. row++
  216. b.Row++
  217. }
  218. }
  219. return nil
  220. }
  221. func (b *PurchaseBillExcel) drawTableFooter() error {
  222. // row := b.Offset + 8
  223. row := b.Row
  224. left1Cell := fmt.Sprintf("A%d", row)
  225. border := []excelize.Border{
  226. {Type: "top", Style: 1, Color: "000000"},
  227. {Type: "left", Style: 1, Color: "000000"},
  228. {Type: "right", Style: 1, Color: "000000"},
  229. {Type: "bottom", Style: 1, Color: "000000"},
  230. }
  231. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  232. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  233. Border: border,
  234. })
  235. b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  236. b.Excel.SetCellValue(b.SheetName, left1Cell, "送货地址")
  237. addCel := fmt.Sprintf("B%d", row)
  238. b.Excel.MergeCell(b.SheetName, addCel, fmt.Sprintf("E%d", row))
  239. b.Excel.SetCellStyle(b.SheetName, addCel, fmt.Sprintf("E%d", row), styleLeft)
  240. b.Excel.SetCellValue(b.SheetName, addCel, b.Content.SendTo)
  241. sureCel := fmt.Sprintf("F%d", row)
  242. b.Excel.MergeCell(b.SheetName, sureCel, fmt.Sprintf("L%d", row))
  243. b.Excel.SetCellStyle(b.SheetName, sureCel, fmt.Sprintf("L%d", row), styleLeft)
  244. b.Excel.SetCellValue(b.SheetName, sureCel, "供应商签字:")
  245. b.Excel.SetRowHeight(b.SheetName, row, 21)
  246. return nil
  247. }
  248. func (b *PurchaseBillExcel) drawTableSignature() error {
  249. // row := b.Offset + 11
  250. b.Row += 2
  251. row := b.Row
  252. // border := []excelize.Border{
  253. // {Type: "top", Style: 1, Color: "000000"},
  254. // {Type: "left", Style: 1, Color: "000000"},
  255. // {Type: "right", Style: 1, Color: "000000"},
  256. // {Type: "bottom", Style: 1, Color: "000000"},
  257. // }
  258. style1, _ := b.Excel.NewStyle(&excelize.Style{
  259. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  260. // Border: border,
  261. })
  262. fontCell := fmt.Sprintf("H%d", row)
  263. imageCell1 := fmt.Sprintf("I%d", row)
  264. imageCell2 := fmt.Sprintf("K%d", row)
  265. b.Excel.MergeCell(b.SheetName, fontCell, fmt.Sprintf("H%d", row+2))
  266. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  267. b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
  268. // 签字图片1 I10-J12
  269. b.Excel.MergeCell(b.SheetName, imageCell1, fmt.Sprintf("J%d", row+2))
  270. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  271. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  272. // 签字图片2 K10-L12
  273. b.Excel.MergeCell(b.SheetName, imageCell2, fmt.Sprintf("L%d", row+2))
  274. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  275. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  276. // 状态为已审核时,签字
  277. // `{
  278. // "x_scale": 0.12,
  279. // "y_scale": 0.12,
  280. // "x_offset": 30,
  281. // "print_obj": true,
  282. // "lock_aspect_ratio": false,
  283. // "locked": false,
  284. // "positioning": "oncell"
  285. // }`
  286. if b.Content.Reviewed == 1 {
  287. imageCells := []string{imageCell1, imageCell2}
  288. if len(b.Signatures) > 0 {
  289. for k, sign := range b.Signatures {
  290. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Url, sign.Format)
  291. }
  292. }
  293. }
  294. b.Excel.SetRowHeight(b.SheetName, row, 21)
  295. return nil
  296. }
  297. func (b *PurchaseBillExcel) Draws() {
  298. b.drawTitle()
  299. b.drawSubTitles()
  300. b.drawTableTitle()
  301. b.drawTableContent()
  302. b.drawTableFooter()
  303. b.drawTableSignature()
  304. }
  305. func NewPurchaseBill(f *excelize.File) *PurchaseBillExcel {
  306. border := []excelize.Border{
  307. {Type: "top", Style: 1, Color: "000000"},
  308. {Type: "left", Style: 1, Color: "000000"},
  309. {Type: "right", Style: 1, Color: "000000"},
  310. {Type: "bottom", Style: 1, Color: "000000"},
  311. }
  312. styleLeft, _ := f.NewStyle(&excelize.Style{
  313. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  314. Border: border,
  315. Font: &excelize.Font{Size: 10},
  316. })
  317. b := &PurchaseBillExcel{
  318. Title: "原材料采购单",
  319. SheetName: "Sheet1",
  320. Excel: f,
  321. Offset: 0,
  322. AlignCenterStyle: styleLeft,
  323. Signatures: make([]*model.Signature, 0),
  324. }
  325. f.SetColWidth(b.SheetName, "A", "A", 12)
  326. f.SetColWidth(b.SheetName, "B", "K", 9.5)
  327. // f.SetColWidth(b.SheetName, "C", "H", 9.5)
  328. f.SetColWidth(b.SheetName, "L", "L", 10)
  329. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  330. return b
  331. }
  332. func (b *PurchaseBillExcel) FormatToEmpty(str *string) {
  333. if *str == "0" || *str == "0.00" {
  334. *str = ""
  335. }
  336. }
  337. func (b *PurchaseBillExcel) PrintPurchType() string {
  338. return "process"
  339. }
  340. func (b *PurchaseBillExcel) SetContent(content *model.PurchaseBill) {
  341. b.Content = content
  342. }
  343. func (b *PurchaseBillExcel) SetTitle(title string) {
  344. b.Title = title
  345. }
  346. func (b *PurchaseBillExcel) SetSignatures(sign []*model.Signature) {
  347. b.Signatures = sign
  348. }
  349. type IPurchBill interface {
  350. PrintPurchType() string
  351. Draws()
  352. SetContent(*model.PurchaseBill)
  353. SetTitle(string)
  354. SetSignatures([]*model.Signature)
  355. }
  356. // func ExeclDraw(ipurch IPurchBill) {
  357. // ipurch.Draws()
  358. // }