bill-product.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 BillProduct(r *GinRouter) {
  16. // 创建单据
  17. r.POSTJWT("/bill/product/create", CreateProductBill)
  18. // 获取单据详情
  19. r.GETJWT("/bill/product/detail/:id", GetProductBill)
  20. // 获取单据列表
  21. r.GETJWT("/bill/product/list", GetProductBills)
  22. // 更新单据
  23. r.POSTJWT("/bill/product/update", UpdateProductBill)
  24. // 删除单据
  25. r.POSTJWT("/bill/product/delete/:id", DelProductBill)
  26. //下载单据
  27. r.GETJWT("/bill/product/download", DownProductBill)
  28. // 审核单据
  29. r.POSTJWT("/bill/product/review/:id", ProductReview)
  30. }
  31. // 审核单据
  32. func ProductReview(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.ProductBill{}
  51. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  52. CollectName: repo.CollectionBillProduct,
  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. product := model.ProductBill{
  68. Reviewed: 1,
  69. UpdateTime: time.Now(),
  70. SignUsers: signs,
  71. }
  72. desc := fmt.Sprintf("【%s】审核了订单", user.Name)
  73. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, _id, &product)
  74. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduct, _id, &product, &repo.RecordLogReq{
  75. Path: c.Request.URL.Path,
  76. UserId: apictx.User.ID,
  77. TargetId: _id,
  78. Desc: desc,
  79. })
  80. }
  81. // 创建生产加工单据
  82. func CreateProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  83. bill := &model.ProductBill{}
  84. err := c.ShouldBindJSON(bill)
  85. if err != nil {
  86. fmt.Println(err)
  87. return nil, errors.New("参数错误!")
  88. }
  89. ctx := apictx.CreateRepoCtx()
  90. if bill.PackId.Hex() == "" {
  91. return nil, errors.New("包装产品id为空")
  92. }
  93. if bill.PlanId.Hex() == "" {
  94. return nil, errors.New("生产计划id为空")
  95. }
  96. if bill.Type == "" {
  97. return nil, errors.New("类型为空")
  98. }
  99. bill.SerialNumber, err = generateSerial(c, apictx, bill.Type)
  100. if err != nil {
  101. return nil, err
  102. }
  103. bill.Status = "created"
  104. if bill.Reviewed == 0 {
  105. bill.Reviewed = -1
  106. }
  107. bill.CreateTime = time.Now()
  108. bill.UpdateTime = time.Now()
  109. notAck := false
  110. bill.IsAck = &notAck
  111. // 制单人数据
  112. userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  113. fmt.Println("userId:", apictx.User.Parent)
  114. if !userId.IsZero() {
  115. user, err := getUserById(apictx, userId)
  116. if err == nil {
  117. bill.UserName = user.Name
  118. bill.UserId = userId
  119. }
  120. }
  121. result, err := repo.RepoAddDoc(ctx, repo.CollectionBillProduct, &bill)
  122. return result, err
  123. }
  124. // 获取单据信息
  125. func GetProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  126. billId := c.Param("id")
  127. id, err := primitive.ObjectIDFromHex(billId)
  128. if err != nil {
  129. return nil, errors.New("非法id")
  130. }
  131. var bill model.ProductBill
  132. option := &repo.DocSearchOptions{
  133. CollectName: repo.CollectionBillProduct,
  134. Query: repo.Map{"_id": id},
  135. }
  136. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  137. if !found || err != nil {
  138. log.Info(err)
  139. return nil, errors.New("数据未找到")
  140. }
  141. return bill, nil
  142. }
  143. // 获取单据列表
  144. func GetProductBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  145. page, size, query := UtilQueryPageSize(c)
  146. option := &repo.PageSearchOptions{
  147. CollectName: repo.CollectionBillProduct,
  148. Query: makeBillQuery(query),
  149. Page: page,
  150. Size: size,
  151. Sort: bson.M{"createTime": -1},
  152. }
  153. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  154. }
  155. // 更新单据
  156. func UpdateProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  157. var bill model.ProductBill
  158. err := c.ShouldBindJSON(&bill)
  159. if err != nil {
  160. fmt.Println(err)
  161. return nil, errors.New("参数错误")
  162. }
  163. if bill.Id.Hex() == "" {
  164. return nil, errors.New("id的为空")
  165. }
  166. // 如果更改类型
  167. if len(bill.Type) > 0 {
  168. billType, err := searchBillTypeById(apictx, repo.CollectionBillProduct, bill.Id)
  169. if err != nil {
  170. return nil, err
  171. }
  172. if billType != bill.Type {
  173. bill.SerialNumber, err = generateSerial(c, apictx, bill.Type)
  174. if err != nil {
  175. return nil, err
  176. }
  177. }
  178. }
  179. // 计算结算价格
  180. userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
  181. user, _ := getUserById(apictx, userId)
  182. desc := fmt.Sprintf("【%s】更新了订单", user.Name)
  183. if bill.Status == "complete" {
  184. bill.CompleteTime = time.Now()
  185. desc = fmt.Sprintf("【%s】完成了订单", user.Name)
  186. }
  187. if bill.Remark == "" {
  188. bill.Remark = " "
  189. }
  190. if bill.SupplierRemark == "" {
  191. bill.SupplierRemark = " "
  192. }
  193. // 更新供应商确定数量与plan中stage项的同步
  194. if len(bill.Products) > 0 {
  195. idCounts := map[string]int{}
  196. for _, product := range bill.Products {
  197. if len(product.Id) == 0 {
  198. continue
  199. }
  200. idCounts[product.Id] = product.ConfirmCount
  201. }
  202. fmt.Println(idCounts)
  203. result, err := updateStageCount(c, bill.PlanId, idCounts, apictx)
  204. if err != nil {
  205. fmt.Println(err)
  206. log.Error(err)
  207. }
  208. fmt.Println(result)
  209. }
  210. bill.UpdateTime = time.Now()
  211. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, bill.Id.Hex(), &bill)
  212. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduct, bill.Id.Hex(), &bill, &repo.RecordLogReq{
  213. Path: c.Request.URL.Path,
  214. UserId: apictx.User.ID,
  215. TargetId: bill.Id.Hex(),
  216. Desc: desc,
  217. })
  218. }
  219. // 删除单据
  220. func DelProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  221. billId := c.Param("id")
  222. if billId == "" {
  223. return nil, errors.New("id为空")
  224. }
  225. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, billId)
  226. }
  227. func DownProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  228. billId := c.Query("id")
  229. isPdf := c.Query("isPdf")
  230. if len(billId) < 1 {
  231. return nil, fmt.Errorf("id不能为空")
  232. }
  233. id, err := primitive.ObjectIDFromHex(billId)
  234. if err != nil {
  235. return nil, errors.New("非法id")
  236. }
  237. var bill model.ProductBill
  238. option := &repo.DocSearchOptions{
  239. CollectName: repo.CollectionBillProduct,
  240. Query: repo.Map{"_id": id},
  241. }
  242. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  243. if !found || err != nil {
  244. log.Info(err)
  245. return nil, errors.New("数据未找到")
  246. }
  247. f := excelize.NewFile()
  248. index := f.NewSheet("Sheet1")
  249. f.SetActiveSheet(index)
  250. f.SetDefaultFont("宋体")
  251. billExcel := NewProductBill(f)
  252. // 获取已审核的签名数据
  253. if bill.Reviewed == 1 {
  254. if len(bill.SignUsers) > 0 {
  255. signs := []*model.Signature{}
  256. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  257. CollectName: repo.CollectionSignature,
  258. Query: repo.Map{"_id": bson.M{"$in": bill.SignUsers}},
  259. Sort: bson.M{"sort": 1}, // 升序
  260. }, &signs)
  261. billExcel.Signatures = signs
  262. }
  263. }
  264. billExcel.Content = &bill
  265. billExcel.IsPdf = isPdf
  266. billExcel.Title = getCompanyName(apictx)
  267. //设置对应的数据
  268. billExcel.Draws()
  269. // 下载为pdf
  270. if isPdf == "true" {
  271. buf, _ := f.WriteToBuffer()
  272. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  273. if err != nil {
  274. return nil, errors.New("转化pdf失败")
  275. }
  276. defer res.Body.Close()
  277. c.Header("Content-Type", "application/octet-stream")
  278. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  279. c.Header("Content-Transfer-Encoding", "binary")
  280. err = res.Write(c.Writer)
  281. if err != nil {
  282. return nil, err
  283. }
  284. return nil, nil
  285. }
  286. c.Header("Content-Type", "application/octet-stream")
  287. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  288. c.Header("Content-Transfer-Encoding", "binary")
  289. err = f.Write(c.Writer)
  290. if err != nil {
  291. return nil, err
  292. }
  293. return nil, nil
  294. }