bill-product.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. // 获取当前订单提交数
  204. // 对比提交数量是否变化,变化了就同步计划中的提交数
  205. currProduct := &model.ProductBill{}
  206. currConfirmCountMap := map[string]int{}
  207. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  208. CollectName: repo.CollectionBillProduct,
  209. Query: repo.Map{"_id": bill.Id},
  210. Project: []string{"products"},
  211. }, currProduct)
  212. if len(currProduct.Products) > 0 {
  213. for _, cp := range currProduct.Products {
  214. currConfirmCountMap[cp.Id] = cp.ConfirmCount
  215. }
  216. }
  217. isSyncConfirm := false
  218. // 更新供应商确定数量与plan中stage项的同步
  219. if len(bill.Products) > 0 {
  220. idCounts := map[string]int{}
  221. for _, product := range bill.Products {
  222. if len(product.Id) == 0 {
  223. continue
  224. }
  225. // 对比提交数量不一致时
  226. if v, ok := currConfirmCountMap[product.Id]; ok {
  227. if v != product.ConfirmCount {
  228. isSyncConfirm = true
  229. idCounts[product.Id] = product.ConfirmCount
  230. }
  231. }
  232. }
  233. fmt.Println("单据变化的提交数量:", idCounts)
  234. if isSyncConfirm {
  235. result, err := updateStageCount(c, bill.PlanId, idCounts, apictx)
  236. if err != nil {
  237. fmt.Println(err)
  238. log.Error(err)
  239. }
  240. fmt.Println(result)
  241. }
  242. }
  243. // 更新供应商确定数量与plan中stage项的同步
  244. // if len(bill.Products) > 0 {
  245. // idCounts := map[string]int{}
  246. // for _, product := range bill.Products {
  247. // if len(product.Id) == 0 {
  248. // continue
  249. // }
  250. // idCounts[product.Id] = product.ConfirmCount
  251. // }
  252. // fmt.Println(idCounts)
  253. // result, err := updateStageCount(c, bill.PlanId, idCounts, apictx)
  254. // if err != nil {
  255. // fmt.Println(err)
  256. // log.Error(err)
  257. // }
  258. // fmt.Println(result)
  259. // }
  260. bill.UpdateTime = time.Now()
  261. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, bill.Id.Hex(), &bill)
  262. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduct, bill.Id.Hex(), &bill, &repo.RecordLogReq{
  263. Path: c.Request.URL.Path,
  264. UserInfo: user,
  265. TargetId: bill.Id.Hex(),
  266. Type: logType,
  267. })
  268. }
  269. // 删除单据
  270. func DelProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  271. billId := c.Param("id")
  272. if billId == "" {
  273. return nil, errors.New("id为空")
  274. }
  275. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, billId)
  276. }
  277. func DownProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  278. billId := c.Query("id")
  279. isPdf := c.Query("isPdf")
  280. if len(billId) < 1 {
  281. return nil, fmt.Errorf("id不能为空")
  282. }
  283. id, err := primitive.ObjectIDFromHex(billId)
  284. if err != nil {
  285. return nil, errors.New("非法id")
  286. }
  287. var bill model.ProductBill
  288. option := &repo.DocSearchOptions{
  289. CollectName: repo.CollectionBillProduct,
  290. Query: repo.Map{"_id": id},
  291. }
  292. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  293. if !found || err != nil {
  294. log.Info(err)
  295. return nil, errors.New("数据未找到")
  296. }
  297. f := excelize.NewFile()
  298. index := f.NewSheet("Sheet1")
  299. f.SetActiveSheet(index)
  300. f.SetDefaultFont("宋体")
  301. billExcel := NewProductBill(f)
  302. // 获取已审核的签名数据
  303. if bill.Reviewed == 1 {
  304. if len(bill.SignUsers) > 0 {
  305. signs := []*model.Signature{}
  306. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  307. CollectName: repo.CollectionSignature,
  308. Query: repo.Map{"_id": bson.M{"$in": bill.SignUsers}},
  309. Sort: bson.M{"sort": 1}, // 升序
  310. }, &signs)
  311. billExcel.Signatures = signs
  312. }
  313. }
  314. billExcel.Content = &bill
  315. billExcel.IsPdf = isPdf
  316. billExcel.Title = getCompanyName(apictx)
  317. //设置对应的数据
  318. billExcel.Draws()
  319. // 下载为pdf
  320. if isPdf == "true" {
  321. buf, _ := f.WriteToBuffer()
  322. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  323. if err != nil {
  324. return nil, errors.New("转化pdf失败")
  325. }
  326. defer res.Body.Close()
  327. c.Header("Content-Type", "application/octet-stream")
  328. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  329. c.Header("Content-Transfer-Encoding", "binary")
  330. err = res.Write(c.Writer)
  331. if err != nil {
  332. return nil, err
  333. }
  334. return nil, nil
  335. }
  336. c.Header("Content-Type", "application/octet-stream")
  337. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  338. c.Header("Content-Transfer-Encoding", "binary")
  339. err = f.Write(c.Writer)
  340. if err != nil {
  341. return nil, err
  342. }
  343. return nil, nil
  344. }