bill-purchase-excel.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 = fmt.Sprintf("已完成(%d)", b.Content.ConfirmCount)
  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. // row := b.Offset + 5
  110. b.Row += 3
  111. //A采购项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I
  112. var drawCol = func(prefix string, value string) error {
  113. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  114. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  115. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  116. if err != nil {
  117. return err
  118. }
  119. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  120. if err != nil {
  121. return err
  122. }
  123. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  124. }
  125. var drawCol2 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
  126. left1Cell := fmt.Sprintf("%s%d", prefix1, b.Row)
  127. left2Cell := fmt.Sprintf("%s%d", prefix2, b.Row)
  128. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  129. if err != nil {
  130. return err
  131. }
  132. if err != nil {
  133. fmt.Println(err)
  134. return err
  135. }
  136. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  137. if err != nil {
  138. return err
  139. }
  140. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  141. val2Cel := fmt.Sprintf("%s%d", prefix1, b.Row+1)
  142. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  143. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  144. val3Cel := fmt.Sprintf("%s%d", prefix2, b.Row+1)
  145. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  146. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  147. return nil
  148. }
  149. drawCol("A", "采购项目")
  150. drawCol("B", "规格(克)")
  151. drawCol("E", "下单数量")
  152. drawCol("F", "完成数量")
  153. drawCol2("C", "D", "尺寸(mm)", "长", "宽")
  154. drawCol2("C", "D", "尺寸(mm)", "长", "宽")
  155. unit1 := "-"
  156. unit2 := "-"
  157. // ??? for ragne
  158. if len(b.Content.Paper) > 0 {
  159. if b.Content.Paper[0].PriceUnit != "" {
  160. unit1 = b.Content.Paper[0].PriceUnit
  161. } else if b.Content.Paper[0].Price2Unit != "" {
  162. unit2 = b.Content.Paper[0].Price2Unit
  163. } else {
  164. unit1 = "元/张"
  165. unit2 = "元/吨"
  166. }
  167. if b.Content.Paper[0].PriceUnit != "" && b.Content.Paper[0].Price2Unit != "" {
  168. unit1 = b.Content.Paper[0].PriceUnit
  169. unit2 = b.Content.Paper[0].Price2Unit
  170. }
  171. }
  172. drawCol2("G", "H", "单价", unit1, unit2)
  173. drawCol("I", "预算金额")
  174. drawCol("J", "实际金额")
  175. drawCol("K", "交货时间")
  176. drawCol("L", "备注")
  177. return nil
  178. }
  179. func (b *PurchaseBillExcel) drawTableContent() error {
  180. // row := b.Offset + 7
  181. // b.Row = row
  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", "K", "L"}
  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. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  194. }
  195. }
  196. papers := b.Content.Paper
  197. if len(papers) > 0 {
  198. for _, paper := range papers {
  199. deliveryTime := paper.DeliveryTime.Local().Format("2006-01-02")
  200. realCount := ""
  201. realPrice := ""
  202. price := fmt.Sprintf("%.3f", paper.Price)
  203. price2 := fmt.Sprintf("%.3f", paper.Price2)
  204. // 预算金额
  205. orderPrice := fmt.Sprintf("%.3f", paper.Price*float64(paper.OrderCount))
  206. b.FormatToEmpty(&orderPrice)
  207. if b.Content.Status == "complete" {
  208. // 实际完成数
  209. realCount = fmt.Sprintf("%d", paper.ConfirmCount)
  210. b.FormatToEmpty(&realCount)
  211. // 实际金额
  212. realPrice = fmt.Sprintf("%.3f", paper.Price*float64(paper.ConfirmCount))
  213. b.FormatToEmpty(&realPrice)
  214. }
  215. DrawRow(b.Row, paper.Name, paper.Norm, paper.Height, paper.Width, fmt.Sprintf("%d", paper.OrderCount), realCount, price, price2, orderPrice, realPrice, deliveryTime, paper.Remark)
  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. b.Row += 2
  250. row := b.Row
  251. style1, _ := b.Excel.NewStyle(&excelize.Style{
  252. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  253. })
  254. // 制单人
  255. billUserCell := fmt.Sprintf("A%d", row+1)
  256. b.Excel.SetCellValue(b.SheetName, billUserCell, "制单人:")
  257. billUservCell := fmt.Sprintf("B%d", row+1)
  258. b.Excel.SetCellValue(b.SheetName, billUservCell, b.Content.UserName)
  259. fontCell := fmt.Sprintf("H%d", row)
  260. imageCell1 := fmt.Sprintf("I%d", row)
  261. imageCell2 := fmt.Sprintf("K%d", row)
  262. b.Excel.MergeCell(b.SheetName, fontCell, fmt.Sprintf("H%d", row+2))
  263. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  264. b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
  265. // 签字图片1 I10-J12
  266. b.Excel.MergeCell(b.SheetName, imageCell1, fmt.Sprintf("J%d", row+2))
  267. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  268. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  269. // 签字图片2 K10-L12
  270. b.Excel.MergeCell(b.SheetName, imageCell2, fmt.Sprintf("L%d", row+2))
  271. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  272. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  273. // 状态为已审核时,签字
  274. // `{
  275. // "x_scale": 0.12,
  276. // "y_scale": 0.12,
  277. // "x_offset": 30,
  278. // "print_obj": true,
  279. // "lock_aspect_ratio": false,
  280. // "locked": false,
  281. // "positioning": "oncell"
  282. // }`
  283. if b.Content.Reviewed == 1 {
  284. imageCells := []string{imageCell1, imageCell2}
  285. if len(b.Signatures) > 0 {
  286. for k, sign := range b.Signatures {
  287. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Path, sign.Format)
  288. }
  289. }
  290. }
  291. b.Excel.SetRowHeight(b.SheetName, row, 21)
  292. return nil
  293. }
  294. func (b *PurchaseBillExcel) Draws() {
  295. b.drawTitle()
  296. b.drawSubTitles()
  297. b.drawTableTitle()
  298. b.drawTableContent()
  299. b.drawTableFooter()
  300. b.drawTableSignature()
  301. }
  302. func NewPurchaseBill(f *excelize.File) *PurchaseBillExcel {
  303. border := []excelize.Border{
  304. {Type: "top", Style: 1, Color: "000000"},
  305. {Type: "left", Style: 1, Color: "000000"},
  306. {Type: "right", Style: 1, Color: "000000"},
  307. {Type: "bottom", Style: 1, Color: "000000"},
  308. }
  309. styleLeft, _ := f.NewStyle(&excelize.Style{
  310. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  311. Border: border,
  312. Font: &excelize.Font{Size: 10},
  313. })
  314. b := &PurchaseBillExcel{
  315. Title: "原材料采购单",
  316. SheetName: "Sheet1",
  317. Excel: f,
  318. Offset: 0,
  319. AlignCenterStyle: styleLeft,
  320. Signatures: make([]*model.Signature, 0),
  321. }
  322. f.SetColWidth(b.SheetName, "A", "A", 12)
  323. f.SetColWidth(b.SheetName, "B", "K", 9.5)
  324. // f.SetColWidth(b.SheetName, "C", "H", 9.5)
  325. f.SetColWidth(b.SheetName, "L", "L", 10)
  326. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  327. return b
  328. }
  329. func (b *PurchaseBillExcel) FormatToEmpty(str *string) {
  330. if *str == "0" || *str == "0.000" {
  331. *str = ""
  332. }
  333. }