bill-product.go 8.6 KB

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