bill.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. r.POSTJWT("/bill/purchase/review/:id", PurchaseReview)
  30. }
  31. // 审核单据
  32. func PurchaseReview(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  33. _id := c.Param("id")
  34. userId, err := primitive.ObjectIDFromHex(apictx.User.Parent)
  35. if err != nil {
  36. return nil, errors.New("用户异常")
  37. }
  38. user, err := getUserById(apictx, userId)
  39. if err != nil {
  40. return nil, errors.New("查找用户失败")
  41. }
  42. if !isManager(user.Roles) {
  43. return nil, errors.New("该用户没有权限")
  44. }
  45. // 更改状态为已审核
  46. purchase := model.PurchaseBill{
  47. Reviewed: 1,
  48. UpdateTime: time.Now(),
  49. }
  50. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, _id, &purchase)
  51. }
  52. type MatBillReq struct {
  53. Bill *model.PurchaseBill
  54. CompIndex *int
  55. MatIndex *int
  56. //MatKey string //components.0.mats.0.billId
  57. }
  58. // 创建单据
  59. func CreateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  60. req := &model.PurchaseBill{}
  61. err := c.ShouldBindJSON(req)
  62. if err != nil {
  63. fmt.Println(err)
  64. return nil, errors.New("参数错误")
  65. }
  66. ctx := apictx.CreateRepoCtx()
  67. bill := req
  68. if bill.PackId.Hex() == "" {
  69. return nil, errors.New("包装产品id为空")
  70. }
  71. if bill.PlanId.Hex() == "" {
  72. return nil, errors.New("生产计划id为空")
  73. }
  74. if bill.Type == "" {
  75. return nil, errors.New("类型为空")
  76. }
  77. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  78. if err != nil {
  79. return nil, err
  80. }
  81. bill.Status = "created"
  82. bill.CreateTime = time.Now()
  83. bill.UpdateTime = time.Now()
  84. return repo.RepoAddDoc(ctx, repo.CollectionBillPurchase, &bill)
  85. }
  86. // 获取单据信息
  87. func GetBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  88. billId := c.Param("id")
  89. id, err := primitive.ObjectIDFromHex(billId)
  90. if err != nil {
  91. return nil, errors.New("非法id")
  92. }
  93. var bill model.PurchaseBill
  94. option := &repo.DocSearchOptions{
  95. CollectName: repo.CollectionBillPurchase,
  96. Query: repo.Map{"_id": id},
  97. }
  98. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  99. if !found || err != nil {
  100. log.Info(err)
  101. return nil, errors.New("数据未找到")
  102. }
  103. return bill, nil
  104. }
  105. // 获取单据列表
  106. func GetBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  107. page, size, query := UtilQueryPageSize(c)
  108. if query["packId"] != nil {
  109. query["packId"], _ = primitive.ObjectIDFromHex(query["packId"].(string))
  110. }
  111. if query["planId"] != nil {
  112. query["planId"], _ = primitive.ObjectIDFromHex(query["planId"].(string))
  113. }
  114. option := &repo.PageSearchOptions{
  115. CollectName: repo.CollectionBillPurchase,
  116. Query: query,
  117. Page: page,
  118. Size: size,
  119. Sort: bson.M{"createTime": -1},
  120. }
  121. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  122. }
  123. func DownLoadBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  124. billId := c.Query("id")
  125. isPdf := c.Query("isPdf")
  126. if len(billId) < 1 {
  127. return nil, fmt.Errorf("id不能为空")
  128. }
  129. id, err := primitive.ObjectIDFromHex(billId)
  130. if err != nil {
  131. return nil, errors.New("非法id")
  132. }
  133. var bill model.PurchaseBill
  134. option := &repo.DocSearchOptions{
  135. CollectName: repo.CollectionBillPurchase,
  136. Query: repo.Map{"_id": id},
  137. }
  138. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  139. if !found || err != nil {
  140. log.Info(err)
  141. return nil, errors.New("数据未找到")
  142. }
  143. f := excelize.NewFile()
  144. // Create a new sheet.
  145. index := f.NewSheet("Sheet1")
  146. f.SetActiveSheet(index)
  147. f.SetDefaultFont("宋体")
  148. billExcel := NewPurchaseBill(f)
  149. // 获取已审核的签名数据
  150. if bill.Reviewed == 1 {
  151. if len(bill.SignUsers) > 0 {
  152. signs := []*model.Signature{}
  153. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  154. CollectName: repo.CollectionSignature,
  155. Query: repo.Map{"userId": bson.M{"$in": bill.SignUsers}},
  156. Sort: bson.M{"sort": 1}, // 升序
  157. }, &signs)
  158. billExcel.Signatures = signs
  159. }
  160. }
  161. billExcel.Content = &bill
  162. info := model.Setting{}
  163. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  164. CollectName: "infos",
  165. }, &info)
  166. billExcel.Title = fmt.Sprintf("%s原材料采购单", info.CompanyName)
  167. //设置对应的数据
  168. billExcel.Draws()
  169. // 下载为pdf
  170. if isPdf == "true" {
  171. buf, _ := f.WriteToBuffer()
  172. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  173. if err != nil {
  174. fmt.Println(err)
  175. return nil, errors.New("转化pdf失败")
  176. }
  177. c.Header("Content-Type", "application/octet-stream")
  178. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  179. c.Header("Content-Transfer-Encoding", "binary")
  180. err = res.Write(c.Writer)
  181. if err != nil {
  182. return nil, err
  183. }
  184. return nil, nil
  185. }
  186. // 下载为execl
  187. c.Header("Content-Type", "application/octet-stream")
  188. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  189. c.Header("Content-Transfer-Encoding", "binary")
  190. err = f.Write(c.Writer)
  191. if err != nil {
  192. return nil, err
  193. }
  194. return nil, nil
  195. }
  196. // 更新单据
  197. func UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  198. var bill model.PurchaseBill
  199. err := c.ShouldBindJSON(&bill)
  200. if err != nil {
  201. return nil, errors.New("参数错误")
  202. }
  203. if bill.Id.Hex() == "" {
  204. return nil, errors.New("id的为空")
  205. }
  206. billType, err := searchBillTypeById(apictx, repo.CollectionBillPurchase, bill.Id)
  207. if err != nil {
  208. return nil, err
  209. }
  210. // 如果更改类型
  211. if billType != bill.Type {
  212. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  213. if err != nil {
  214. return nil, err
  215. }
  216. }
  217. bill.UpdateTime = time.Now()
  218. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, bill.Id.Hex(), &bill)
  219. }
  220. // 删除单据
  221. func DelBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  222. billId := c.Param("id")
  223. if billId == "" {
  224. return nil, errors.New("id为空")
  225. }
  226. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, billId)
  227. }