bill.go 6.6 KB

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