bill.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "fmt"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "github.com/xuri/excelize/v2"
  11. "go.mongodb.org/mongo-driver/bson"
  12. "go.mongodb.org/mongo-driver/bson/primitive"
  13. )
  14. // 单据管理
  15. func Bill(r *GinRouter) {
  16. // 创建单据
  17. r.POST("/bill/purchase/create", CreateBill)
  18. // 获取单据详情
  19. r.GET("/bill/purchase/detail/:id", GetBill)
  20. // 获取单据列表
  21. r.GET("/bill/purchase/list", GetBills)
  22. // 获取单据列表
  23. r.GET("/bill/purchase/download", DownLoadBills)
  24. // 更新单据
  25. r.POST("/bill/purchase/update", UpdateBill)
  26. // 删除单据
  27. r.POST("/bill/purchase/delete/:id", DelBill)
  28. }
  29. type MatBillReq struct {
  30. Bill *model.PurchaseBill
  31. CompIndex *int
  32. MatIndex *int
  33. //MatKey string //components.0.mats.0.billId
  34. }
  35. // 创建单据
  36. func CreateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  37. req := &model.PurchaseBill{}
  38. err := c.ShouldBindJSON(req)
  39. if err != nil {
  40. fmt.Println(err)
  41. return nil, errors.New("参数错误")
  42. }
  43. ctx := apictx.CreateRepoCtx()
  44. bill := req
  45. if bill.PackId.Hex() == "" {
  46. return nil, errors.New("包装产品id为空")
  47. }
  48. if bill.PlanId.Hex() == "" {
  49. return nil, errors.New("生产计划id为空")
  50. }
  51. if bill.Type == "" {
  52. return nil, errors.New("类型为空")
  53. }
  54. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  55. if err != nil {
  56. return nil, err
  57. }
  58. bill.Status = "created"
  59. bill.CreateTime = time.Now()
  60. bill.UpdateTime = time.Now()
  61. return repo.RepoAddDoc(ctx, repo.CollectionBillPurchase, &bill)
  62. }
  63. // 获取单据信息
  64. func GetBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  65. billId := c.Param("id")
  66. id, err := primitive.ObjectIDFromHex(billId)
  67. if err != nil {
  68. return nil, errors.New("非法id")
  69. }
  70. var bill model.PurchaseBill
  71. option := &repo.DocSearchOptions{
  72. CollectName: repo.CollectionBillPurchase,
  73. Query: repo.Map{"_id": id},
  74. }
  75. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  76. if !found || err != nil {
  77. log.Info(err)
  78. return nil, errors.New("数据未找到")
  79. }
  80. return bill, nil
  81. }
  82. // 获取单据列表
  83. func GetBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  84. page, size, query := UtilQueryPageSize(c)
  85. if query["packId"] != nil {
  86. query["packId"], _ = primitive.ObjectIDFromHex(query["packId"].(string))
  87. }
  88. if query["planId"] != nil {
  89. query["planId"], _ = primitive.ObjectIDFromHex(query["planId"].(string))
  90. }
  91. option := &repo.PageSearchOptions{
  92. CollectName: repo.CollectionBillPurchase,
  93. Query: query,
  94. Page: page,
  95. Size: size,
  96. Sort: bson.M{"createTime": -1},
  97. }
  98. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  99. }
  100. func DownLoadBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  101. billId := c.Query("id")
  102. isPdf := c.Query("isPdf")
  103. if len(billId) < 1 {
  104. return nil, fmt.Errorf("id不能为空")
  105. }
  106. id, err := primitive.ObjectIDFromHex(billId)
  107. if err != nil {
  108. return nil, errors.New("非法id")
  109. }
  110. var bill model.PurchaseBill
  111. option := &repo.DocSearchOptions{
  112. CollectName: repo.CollectionBillPurchase,
  113. Query: repo.Map{"_id": id},
  114. }
  115. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  116. if !found || err != nil {
  117. log.Info(err)
  118. return nil, errors.New("数据未找到")
  119. }
  120. f := excelize.NewFile()
  121. // Create a new sheet.
  122. index := f.NewSheet("Sheet1")
  123. f.SetActiveSheet(index)
  124. f.SetDefaultFont("宋体")
  125. billExcel := NewPurchaseBill(f)
  126. billExcel.Content = &bill
  127. info := model.Setting{}
  128. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  129. CollectName: "infos",
  130. }, &info)
  131. billExcel.Title = fmt.Sprintf("%s原材料采购单", info.CompanyName)
  132. //设置对应的数据
  133. billExcel.Draws()
  134. // 下载为pdf
  135. if isPdf == "true" {
  136. buf, _ := f.WriteToBuffer()
  137. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  138. if err != nil {
  139. return nil, errors.New("转化pdf失败")
  140. }
  141. c.Header("Content-Type", "application/octet-stream")
  142. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  143. c.Header("Content-Transfer-Encoding", "binary")
  144. err = res.Write(c.Writer)
  145. if err != nil {
  146. return nil, err
  147. }
  148. return nil, nil
  149. }
  150. // 下载为execl
  151. c.Header("Content-Type", "application/octet-stream")
  152. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  153. c.Header("Content-Transfer-Encoding", "binary")
  154. err = f.Write(c.Writer)
  155. if err != nil {
  156. return nil, err
  157. }
  158. return nil, nil
  159. }
  160. // 更新单据
  161. func UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  162. var bill model.PurchaseBill
  163. err := c.ShouldBindJSON(&bill)
  164. if err != nil {
  165. return nil, errors.New("参数错误")
  166. }
  167. if bill.Id.Hex() == "" {
  168. return nil, errors.New("id的为空")
  169. }
  170. billType, err := searchBillTypeById(apictx, repo.CollectionBillPurchase, bill.Id)
  171. if err != nil {
  172. return nil, err
  173. }
  174. // 如果更改类型
  175. if billType != bill.Type {
  176. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  177. if err != nil {
  178. return nil, err
  179. }
  180. }
  181. bill.UpdateTime = time.Now()
  182. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, bill.Id.Hex(), &bill)
  183. }
  184. // 删除单据
  185. func DelBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  186. billId := c.Param("id")
  187. if billId == "" {
  188. return nil, errors.New("id为空")
  189. }
  190. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, billId)
  191. }