bill-purchase-excel.go 11 KB

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