bill-produce.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 BillProduce(r *GinRouter) {
  16. // 创建单据
  17. r.POSTJWT("/bill/produce/create", CreateProduceBill)
  18. // 获取单据详情
  19. r.GET("/bill/produce/detail/:id", GetProduceBill)
  20. // 获取单据列表
  21. r.GET("/bill/produce/list", GetProduceBills)
  22. // 更新单据
  23. r.POST("/bill/produce/update", UpdateProduceBill)
  24. // 删除单据
  25. r.POST("/bill/produce/delete/:id", DelProduceBill)
  26. //下载单据
  27. r.GET("/bill/produce/download", DownProduceBill)
  28. // 审核单据
  29. r.POSTJWT("/bill/produce/review/:id", ProduceReview)
  30. }
  31. // 审核单据
  32. func ProduceReview(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  33. _id := c.Param("id")
  34. id, err := primitive.ObjectIDFromHex(_id)
  35. if err != nil {
  36. return nil, errors.New("id错误")
  37. }
  38. userId, err := primitive.ObjectIDFromHex(apictx.User.Parent)
  39. if err != nil {
  40. return nil, errors.New("用户异常")
  41. }
  42. user, err := getUserById(apictx, userId)
  43. if err != nil {
  44. return nil, errors.New("查找用户失败")
  45. }
  46. if !isManager(user.Roles) {
  47. return nil, errors.New("该用户没有权限")
  48. }
  49. // 查询单据获取已有的签字
  50. bill := model.ProduceBill{}
  51. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  52. CollectName: repo.CollectionBillProduce,
  53. Query: repo.Map{"_id": id, "reviewed": 1},
  54. }, &bill)
  55. signs := make([]primitive.ObjectID, 0)
  56. if len(bill.SignUsers) > 0 {
  57. // 如果自己已存在该集合中了
  58. for _, signUser := range bill.SignUsers {
  59. if signUser == userId {
  60. return nil, errors.New("该单据您已审核过了")
  61. }
  62. }
  63. signs = bill.SignUsers
  64. }
  65. // 更改状态为已审核 并签字
  66. signs = append(signs, userId)
  67. produce := model.ProduceBill{
  68. Reviewed: 1,
  69. UpdateTime: time.Now(),
  70. SignUsers: signs,
  71. }
  72. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, _id, &produce)
  73. }
  74. // 创建生产加工单据
  75. func CreateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  76. bill := &model.ProduceBill{}
  77. err := c.ShouldBindJSON(bill)
  78. if err != nil {
  79. fmt.Println(err)
  80. return nil, errors.New("参数错误!")
  81. }
  82. ctx := apictx.CreateRepoCtx()
  83. if bill.PackId.Hex() == "" {
  84. return nil, errors.New("包装产品id为空")
  85. }
  86. if bill.PlanId.Hex() == "" {
  87. return nil, errors.New("生产计划id为空")
  88. }
  89. if bill.Type == "" {
  90. return nil, errors.New("类型为空")
  91. }
  92. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  93. if err != nil {
  94. return nil, err
  95. }
  96. bill.Status = "created"
  97. if bill.Reviewed == 0 {
  98. bill.Reviewed = -1
  99. }
  100. bill.CreateTime = time.Now()
  101. bill.UpdateTime = time.Now()
  102. // 制单人数据
  103. userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  104. fmt.Println("userId:", apictx.User.Parent)
  105. if !userId.IsZero() {
  106. user, err := getUserById(apictx, userId)
  107. if err == nil {
  108. bill.UserName = user.Name
  109. bill.UserId = userId
  110. }
  111. }
  112. result, err := repo.RepoAddDoc(ctx, repo.CollectionBillProduce, &bill)
  113. return result, err
  114. }
  115. // 获取单据信息
  116. func GetProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  117. billId := c.Param("id")
  118. id, err := primitive.ObjectIDFromHex(billId)
  119. if err != nil {
  120. return nil, errors.New("非法id")
  121. }
  122. var bill model.ProduceBill
  123. option := &repo.DocSearchOptions{
  124. CollectName: repo.CollectionBillProduce,
  125. Query: repo.Map{"_id": id},
  126. }
  127. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  128. if !found || err != nil {
  129. log.Info(err)
  130. return nil, errors.New("数据未找到")
  131. }
  132. return bill, nil
  133. }
  134. // 获取单据列表
  135. func GetProduceBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  136. page, size, query := UtilQueryPageSize(c)
  137. if query["packId"] != nil {
  138. query["packId"], _ = primitive.ObjectIDFromHex(query["packId"].(string))
  139. }
  140. if query["planId"] != nil {
  141. query["planId"], _ = primitive.ObjectIDFromHex(query["planId"].(string))
  142. }
  143. // 时间范围查询
  144. // createTime 选中的当天时间
  145. st, ok1 := query["startTime"]
  146. delete(query, "startTime")
  147. et, ok2 := query["endTime"]
  148. delete(query, "endTime")
  149. if ok1 && ok2 {
  150. startTime := st.(string)
  151. endTime := et.(string)
  152. start, end := getTimeRange(startTime, endTime)
  153. query["createTime"] = bson.M{"$gte": start, "$lte": end}
  154. }
  155. option := &repo.PageSearchOptions{
  156. CollectName: repo.CollectionBillProduce,
  157. Query: query,
  158. Page: page,
  159. Size: size,
  160. Sort: bson.M{"createTime": -1},
  161. }
  162. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  163. }
  164. // 更新单据
  165. func UpdateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  166. var bill model.ProduceBill
  167. err := c.ShouldBindJSON(&bill)
  168. if err != nil {
  169. fmt.Println(err)
  170. return nil, errors.New("参数错误")
  171. }
  172. if bill.Id.Hex() == "" {
  173. return nil, errors.New("id的为空")
  174. }
  175. billType, err := searchBillTypeById(apictx, repo.CollectionBillProduce, bill.Id)
  176. if err != nil {
  177. return nil, err
  178. }
  179. // 如果更改类型
  180. if billType != bill.Type {
  181. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  182. if err != nil {
  183. return nil, err
  184. }
  185. }
  186. // 计算结算价格
  187. if bill.Status == "complete" {
  188. bill.CompleteTime = time.Now()
  189. }
  190. bill.UpdateTime = time.Now()
  191. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill)
  192. }
  193. // 删除单据
  194. func DelProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  195. billId := c.Param("id")
  196. if billId == "" {
  197. return nil, errors.New("id为空")
  198. }
  199. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId)
  200. }
  201. func DownProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  202. billId := c.Query("id")
  203. isPdf := c.Query("isPdf")
  204. if len(billId) < 1 {
  205. return nil, fmt.Errorf("id不能为空")
  206. }
  207. id, err := primitive.ObjectIDFromHex(billId)
  208. if err != nil {
  209. return nil, errors.New("非法id")
  210. }
  211. var bill model.ProduceBill
  212. option := &repo.DocSearchOptions{
  213. CollectName: repo.CollectionBillProduce,
  214. Query: repo.Map{"_id": id},
  215. }
  216. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  217. if !found || err != nil {
  218. log.Info(err)
  219. return nil, errors.New("数据未找到")
  220. }
  221. f := excelize.NewFile()
  222. // Create a new sheet.
  223. index := f.NewSheet("Sheet1")
  224. f.SetActiveSheet(index)
  225. f.SetDefaultFont("宋体")
  226. billExcel := NewProduceBill(f)
  227. // 获取已审核的签名数据
  228. if bill.Reviewed == 1 {
  229. if len(bill.SignUsers) > 0 {
  230. signs := []*model.Signature{}
  231. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  232. CollectName: repo.CollectionSignature,
  233. Query: repo.Map{"_id": bson.M{"$in": bill.SignUsers}},
  234. Sort: bson.M{"sort": 1}, // 升序
  235. }, &signs)
  236. billExcel.Signatures = signs
  237. }
  238. }
  239. billExcel.Content = &bill
  240. companyName := getCompanyName(apictx)
  241. billExcel.Title = fmt.Sprintf("%s加工单", companyName)
  242. //设置对应的数据
  243. billExcel.Draws()
  244. // 下载为pdf
  245. if isPdf == "true" {
  246. buf, _ := f.WriteToBuffer()
  247. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  248. if err != nil {
  249. return nil, errors.New("转化pdf失败")
  250. }
  251. c.Header("Content-Type", "application/octet-stream")
  252. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  253. c.Header("Content-Transfer-Encoding", "binary")
  254. err = res.Write(c.Writer)
  255. if err != nil {
  256. return nil, err
  257. }
  258. return nil, nil
  259. }
  260. c.Header("Content-Type", "application/octet-stream")
  261. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  262. c.Header("Content-Transfer-Encoding", "binary")
  263. err = f.Write(c.Writer)
  264. if err != nil {
  265. return nil, err
  266. }
  267. return nil, nil
  268. }