report-produce-excel.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "math"
  6. "github.com/xuri/excelize/v2"
  7. )
  8. type ReportProduceExcel struct {
  9. Offset int
  10. Row int
  11. Title string //标题
  12. Excel *excelize.File
  13. SheetName string
  14. AlignCenterStyle int
  15. Content *model.ProduceBill
  16. BudgetAmount float64
  17. RealAmount float64
  18. }
  19. func (b *ReportProduceExcel) drawTitle() error {
  20. b.Row++
  21. // 设置外边距
  22. startCell := fmt.Sprintf("A%d", b.Row)
  23. endCell := fmt.Sprintf("L%d", b.Row)
  24. marginLeft := excelize.PageMarginLeft(0.15)
  25. if b.Content.IsPrint {
  26. // A加工项目 B规格 C纸张 D来纸尺寸 E印刷尺寸 F下单数量 G完成数量 H单价 I预算金额 J实际金额 K交货时间 L备注
  27. b.Excel.SetColWidth(b.SheetName, "A", "A", 16)
  28. b.Excel.SetColWidth(b.SheetName, "B", "B", 14)
  29. b.Excel.SetColWidth(b.SheetName, "C", "C", 10)
  30. b.Excel.SetColWidth(b.SheetName, "D", "E", 14)
  31. b.Excel.SetColWidth(b.SheetName, "F", "K", 12)
  32. b.Excel.SetColWidth(b.SheetName, "J", "J", 18)
  33. } else {
  34. // 不是打印
  35. // A加工项目 B规格 C下单数量 D完成数量 E单价 F预算金额 G实际金额 H交货时间 I备注
  36. endCell = fmt.Sprintf("G%d", b.Row)
  37. marginLeft = excelize.PageMarginLeft(0.6)
  38. b.Excel.SetColWidth(b.SheetName, "A", "A", 16)
  39. b.Excel.SetColWidth(b.SheetName, "B", "B", 14)
  40. b.Excel.SetColWidth(b.SheetName, "C", "H", 12)
  41. b.Excel.SetColWidth(b.SheetName, "I", "I", 18)
  42. // 是覆膜
  43. if b.Content.IsLam {
  44. // A加工项目 B规格 C覆膜尺寸 D下单数量 E完成数量 F元/吨,G元/张 H预算金额 I实际金额 J交货时间 K备注
  45. endCell = fmt.Sprintf("I%d", b.Row)
  46. marginLeft = excelize.PageMarginLeft(0.3)
  47. b.Excel.SetColWidth(b.SheetName, "A", "A", 16)
  48. b.Excel.SetColWidth(b.SheetName, "B", "C", 14)
  49. // 覆膜规格
  50. b.Excel.SetColWidth(b.SheetName, "D", "J", 12)
  51. b.Excel.SetColWidth(b.SheetName, "K", "K", 18)
  52. }
  53. }
  54. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  55. err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
  56. if err != nil {
  57. return err
  58. }
  59. style, err := b.Excel.NewStyle(&excelize.Style{
  60. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  61. Font: &excelize.Font{Bold: true, Size: 18}})
  62. if err != nil {
  63. return err
  64. }
  65. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  66. if err != nil {
  67. return err
  68. }
  69. b.Excel.SetRowHeight(b.SheetName, b.Row, 23)
  70. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  71. return nil
  72. }
  73. func (b *ReportProduceExcel) drawSubTitles() error {
  74. b.Row++
  75. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  76. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  77. Font: &excelize.Font{Size: 11}})
  78. if err != nil {
  79. return err
  80. }
  81. styleRight, err := b.Excel.NewStyle(&excelize.Style{
  82. Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
  83. Font: &excelize.Font{Size: 11}})
  84. if err != nil {
  85. return err
  86. }
  87. var drawLeft = func(rowIndex int, value string) error {
  88. //左边1
  89. left1Cell := fmt.Sprintf("A%d", rowIndex)
  90. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("I%d", rowIndex))
  91. if err != nil {
  92. return err
  93. }
  94. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  95. if err != nil {
  96. return err
  97. }
  98. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  99. return nil
  100. }
  101. var drawRight = func(rowIndex int, value string) error {
  102. right1Cell := fmt.Sprintf("J%d", rowIndex)
  103. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("L%d", rowIndex))
  104. if err != nil {
  105. return err
  106. }
  107. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  108. if err != nil {
  109. return err
  110. }
  111. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  112. return nil
  113. }
  114. // !isPrint
  115. if !b.Content.IsPrint {
  116. drawLeft = func(rowIndex int, value string) error {
  117. //左边1
  118. left1Cell := fmt.Sprintf("A%d", rowIndex)
  119. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("F%d", rowIndex))
  120. if err != nil {
  121. return err
  122. }
  123. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  124. if err != nil {
  125. return err
  126. }
  127. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  128. return nil
  129. }
  130. drawRight = func(rowIndex int, value string) error {
  131. right1Cell := fmt.Sprintf("G%d", rowIndex)
  132. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("I%d", rowIndex))
  133. if err != nil {
  134. return err
  135. }
  136. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  137. if err != nil {
  138. return err
  139. }
  140. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  141. return nil
  142. }
  143. if b.Content.IsLam {
  144. drawLeft = func(rowIndex int, value string) error {
  145. //左边1
  146. left1Cell := fmt.Sprintf("A%d", rowIndex)
  147. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("H%d", rowIndex))
  148. if err != nil {
  149. return err
  150. }
  151. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  152. if err != nil {
  153. return err
  154. }
  155. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  156. return nil
  157. }
  158. drawRight = func(rowIndex int, value string) error {
  159. right1Cell := fmt.Sprintf("I%d", rowIndex)
  160. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("K%d", rowIndex))
  161. if err != nil {
  162. return err
  163. }
  164. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  165. if err != nil {
  166. return err
  167. }
  168. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  169. return nil
  170. }
  171. }
  172. }
  173. //第一行
  174. drawLeft(b.Row, "类别:"+b.Content.Type)
  175. drawRight(b.Row, "单号:"+b.Content.SerialNumber)
  176. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  177. //第二行
  178. drawLeft(b.Row+1, "供应商名称:"+b.Content.Supplier)
  179. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  180. drawRight(b.Row+1, "下单时间:"+timeformat)
  181. b.Excel.SetRowHeight(b.SheetName, b.Row+1, 21)
  182. //第三行
  183. drawLeft(b.Row+2, "产品名称:"+b.Content.ProductName)
  184. status := ""
  185. if b.Content.Status == "complete" {
  186. status = "已完成"
  187. }
  188. if b.Content.Status == "created" {
  189. status = "进行中"
  190. }
  191. drawRight(b.Row+2, "状态:"+status)
  192. b.Excel.SetRowHeight(b.SheetName, b.Row+2, 21)
  193. return nil
  194. }
  195. func (b *ReportProduceExcel) drawTableTitle() error {
  196. b.Row += 3
  197. //A加工项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I
  198. var drawCol = func(prefix string, value string) error {
  199. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  200. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  201. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  202. if err != nil {
  203. return err
  204. }
  205. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  206. if err != nil {
  207. return err
  208. }
  209. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  210. }
  211. var drawCol2 = func(prefix string, value1 string, value2 string) error {
  212. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  213. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  214. err := b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  215. if err != nil {
  216. return err
  217. }
  218. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  219. b.Excel.SetCellValue(b.SheetName, left2Cell, value2)
  220. return nil
  221. }
  222. var drawCol3 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
  223. left1Cell := fmt.Sprintf("%s%d", prefix1, b.Row)
  224. left2Cell := fmt.Sprintf("%s%d", prefix2, b.Row)
  225. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  226. if err != nil {
  227. return err
  228. }
  229. if err != nil {
  230. fmt.Println(err)
  231. return err
  232. }
  233. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  234. if err != nil {
  235. return err
  236. }
  237. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  238. val2Cel := fmt.Sprintf("%s%d", prefix1, b.Row+1)
  239. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  240. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  241. val3Cel := fmt.Sprintf("%s%d", prefix2, b.Row+1)
  242. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  243. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  244. return nil
  245. }
  246. unit := b.Content.Produces[0].Unit
  247. if b.Content.IsPrint {
  248. drawCol("A", "加工项目")
  249. drawCol("B", "规格")
  250. drawCol("C", "纸张")
  251. drawCol("D", "来纸尺寸")
  252. drawCol("E", "印刷尺寸")
  253. drawCol("F", "下单数量")
  254. drawCol("G", "完成数量")
  255. drawCol2("H", "单价", unit)
  256. drawCol("I", "预算金额")
  257. drawCol("J", "实际金额")
  258. drawCol("K", "交货时间")
  259. drawCol("L", "备注")
  260. } else {
  261. if b.Content.IsLam {
  262. drawCol("A", "加工项目")
  263. drawCol("B", "规格")
  264. drawCol("C", "覆膜尺寸")
  265. drawCol("D", "下单数量")
  266. drawCol("E", "完成数量")
  267. unit2 := b.Content.Produces[0].Unit2
  268. drawCol3("E", "G", "单价", unit, unit2)
  269. drawCol("H", "预算金额")
  270. drawCol("I", "实际金额")
  271. drawCol("J", "交货时间")
  272. drawCol("K", "备注")
  273. } else {
  274. drawCol("A", "加工项目")
  275. drawCol("B", "规格")
  276. drawCol("C", "下单数量")
  277. drawCol("D", "完成数量")
  278. drawCol2("E", "单价", unit)
  279. drawCol("F", "预算金额")
  280. drawCol("G", "实际金额")
  281. drawCol("H", "交货时间")
  282. drawCol("I", "备注")
  283. }
  284. }
  285. return nil
  286. }
  287. func (b *ReportProduceExcel) drawTableContent() error {
  288. b.Row += 2
  289. var DrawRow = func(rowIndex int, values ...string) {
  290. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}
  291. // !isPrint
  292. if !b.Content.IsPrint {
  293. charas = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"}
  294. if b.Content.IsLam {
  295. charas = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"}
  296. }
  297. }
  298. for i, c := range charas {
  299. v := ""
  300. if i < len(values) {
  301. v = values[i]
  302. }
  303. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  304. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  305. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  306. var magN float64 = 1
  307. lenv := len([]rune(v))
  308. magN = math.Ceil(float64(lenv) / 10)
  309. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21*magN)
  310. }
  311. }
  312. produces := b.Content.Produces
  313. if len(produces) > 0 {
  314. for _, produce := range produces {
  315. realCount := ""
  316. price := produce.OrderPrice
  317. priceStr := fmt.Sprintf("%.3f", price)
  318. b.FormatToEmpty(&priceStr)
  319. // 预算金额
  320. budgetAmount := fmt.Sprintf("%.3f", produce.OrderPrice*float64(produce.OrderCount))
  321. b.FormatToEmpty(&budgetAmount)
  322. // 实际金额
  323. realPrice := ""
  324. // 实际完成数
  325. realCount = fmt.Sprintf("%d", produce.ConfirmCount)
  326. b.FormatToEmpty(&realCount)
  327. realPrice = fmt.Sprintf("%.3f", produce.OrderPrice*float64(produce.ConfirmCount))
  328. b.FormatToEmpty(&realPrice)
  329. deliveryTime := produce.DeliveryTime.Local().Format("2006-01-02")
  330. // !isPrint
  331. if !b.Content.IsPrint {
  332. if b.Content.IsLam {
  333. DrawRow(b.Row, produce.Name, produce.Norm, produce.PrintSize, fmt.Sprintf("%d", produce.OrderCount), realCount, priceStr, fmt.Sprintf("%.3f", produce.Price2), budgetAmount, realPrice, deliveryTime, produce.Remark)
  334. } else {
  335. DrawRow(b.Row, produce.Name, produce.Norm, fmt.Sprintf("%d", produce.OrderCount), realCount, priceStr, budgetAmount, realPrice, deliveryTime, produce.Remark)
  336. }
  337. } else {
  338. DrawRow(b.Row, produce.Name, produce.Norm, produce.Paper, produce.PaperSize, produce.PrintSize, fmt.Sprintf("%d", produce.OrderCount), realCount, priceStr, budgetAmount, realPrice, deliveryTime, produce.Remark)
  339. }
  340. b.Row++
  341. b.BudgetAmount += produce.OrderPrice * float64(produce.OrderCount)
  342. b.RealAmount += produce.OrderPrice * float64(produce.ConfirmCount)
  343. }
  344. }
  345. return nil
  346. }
  347. func (b *ReportProduceExcel) Draws() {
  348. b.drawTitle()
  349. b.drawSubTitles()
  350. b.drawTableTitle()
  351. b.drawTableContent()
  352. }
  353. func NewReportProduceExcel(f *excelize.File) *ReportProduceExcel {
  354. border := []excelize.Border{
  355. {Type: "top", Style: 1, Color: "000000"},
  356. {Type: "left", Style: 1, Color: "000000"},
  357. {Type: "right", Style: 1, Color: "000000"},
  358. {Type: "bottom", Style: 1, Color: "000000"},
  359. }
  360. styleLeft, _ := f.NewStyle(&excelize.Style{
  361. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  362. Border: border,
  363. })
  364. b := &ReportProduceExcel{
  365. Title: "中鱼互动加工单",
  366. SheetName: "Sheet1",
  367. Excel: f,
  368. Offset: 0,
  369. AlignCenterStyle: styleLeft,
  370. }
  371. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  372. return b
  373. }
  374. func (b *ReportProduceExcel) FormatToEmpty(str *string) {
  375. if *str == "0" || *str == "0.000" {
  376. *str = ""
  377. }
  378. }