bill-product.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. _isSend := false
  109. bill.IsSend = &_isSend
  110. notAck := false
  111. bill.IsAck = &notAck
  112. // 制单人数据
  113. userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  114. fmt.Println("userId:", apictx.User.Parent)
  115. userInfo := &model.UserSmaple{}
  116. if !userId.IsZero() {
  117. user, err := getUserById(apictx, userId)
  118. userInfo = user
  119. if err == nil {
  120. bill.UserName = user.Name
  121. bill.UserId = userId
  122. }
  123. }
  124. result, err := repo.RepoAddDoc1(ctx, repo.CollectionBillProduct, &bill, &repo.RecordLogReq{
  125. Path: c.Request.URL.Path,
  126. UserInfo: userInfo,
  127. TargetId: "",
  128. Type: "created",
  129. })
  130. return result, err
  131. }
  132. // 获取单据信息
  133. func GetProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  134. billId := c.Param("id")
  135. id, err := primitive.ObjectIDFromHex(billId)
  136. if err != nil {
  137. return nil, errors.New("非法id")
  138. }
  139. var bill model.ProductBill
  140. option := &repo.DocSearchOptions{
  141. CollectName: repo.CollectionBillProduct,
  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. return bill, nil
  150. }
  151. // 获取单据列表
  152. func GetProductBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  153. page, size, query := UtilQueryPageSize(c)
  154. option := &repo.PageSearchOptions{
  155. CollectName: repo.CollectionBillProduct,
  156. Query: makeBillQuery(query),
  157. Page: page,
  158. Size: size,
  159. Sort: bson.M{"createTime": -1},
  160. }
  161. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  162. }
  163. // 更新单据
  164. func UpdateProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  165. var bill model.ProductBill
  166. err := c.ShouldBindJSON(&bill)
  167. if err != nil {
  168. fmt.Println(err)
  169. return nil, errors.New("参数错误")
  170. }
  171. if bill.Id.Hex() == "" {
  172. return nil, errors.New("id的为空")
  173. }
  174. // 如果更改类型
  175. if len(bill.Type) > 0 {
  176. billType, err := searchBillTypeById(apictx, repo.CollectionBillProduct, bill.Id)
  177. if err != nil {
  178. return nil, err
  179. }
  180. if billType != bill.Type {
  181. bill.SerialNumber, err = generateSerial(c, apictx, bill.Type)
  182. if err != nil {
  183. return nil, err
  184. }
  185. }
  186. }
  187. // 计算结算价格
  188. userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
  189. user, _ := getUserById(apictx, userId)
  190. logType := "update"
  191. // 更改状态
  192. ok, err := isCompareStatus(apictx, repo.CollectionBillProduct, bill.Id, bill.Status)
  193. if err != nil {
  194. return nil, err
  195. }
  196. if bill.Status == "complete" {
  197. if !ok {
  198. bill.CompleteTime = time.Now()
  199. logType = "complete"
  200. }
  201. }
  202. if bill.Status == "deprecated" {
  203. if !ok {
  204. logType = "deprecated"
  205. }
  206. }
  207. if bill.Remark == "" {
  208. bill.Remark = " "
  209. }
  210. if bill.SupplierRemark == "" {
  211. bill.SupplierRemark = " "
  212. }
  213. // 修改单据信息需要同步plan中stage项
  214. if len(bill.Products) > 0 {
  215. // 获取当前订单供应商id
  216. // 对比供应商是否变化,变化了就同步计划中的供应商
  217. currProduct := &model.ProductBill{}
  218. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  219. CollectName: repo.CollectionBillProduct,
  220. Query: repo.Map{"_id": bill.Id},
  221. Project: []string{"supplierId"},
  222. }, currProduct)
  223. var supplierInfo *model.Supplier
  224. if currProduct.SupplierId != bill.SupplierId {
  225. // 查询更改后的supplierInfo
  226. supplierInfo = &model.Supplier{}
  227. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  228. CollectName: repo.CollectionSupplier,
  229. Query: repo.Map{"_id": bill.SupplierId},
  230. }, supplierInfo)
  231. }
  232. idStatges := make(map[string]*UpdateBilltoStageReq)
  233. for _, product := range bill.Products {
  234. if len(product.Id) == 0 {
  235. continue
  236. }
  237. idStatges[product.Id] = &UpdateBilltoStageReq{
  238. BillType: "product",
  239. SupplierInfo: supplierInfo,
  240. Norm: product.Norm,
  241. Size: product.Size,
  242. OrderCount: product.OrderCount,
  243. OrderPrice: product.OrderPrice,
  244. ConfirmCount: product.ConfirmCount,
  245. Remark: product.Remark,
  246. DeliveryTime: product.DeliveryTime,
  247. }
  248. }
  249. _, err := updateBilltoStage(c, bill.PlanId, idStatges, apictx)
  250. if err != nil {
  251. return nil, errors.New("该单据改动同步到产品失败")
  252. }
  253. fmt.Println("单据同步到产品,planId:", bill.PlanId)
  254. }
  255. bill.UpdateTime = time.Now()
  256. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, bill.Id.Hex(), &bill)
  257. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduct, bill.Id.Hex(), &bill, &repo.RecordLogReq{
  258. Path: c.Request.URL.Path,
  259. UserInfo: user,
  260. TargetId: bill.Id.Hex(),
  261. Type: logType,
  262. })
  263. }
  264. // 删除单据
  265. func DelProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  266. billId := c.Param("id")
  267. if billId == "" {
  268. return nil, errors.New("id为空")
  269. }
  270. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, billId)
  271. }
  272. func DownProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  273. billId := c.Query("id")
  274. isPdf := c.Query("isPdf")
  275. if len(billId) < 1 {
  276. return nil, fmt.Errorf("id不能为空")
  277. }
  278. id, err := primitive.ObjectIDFromHex(billId)
  279. if err != nil {
  280. return nil, errors.New("非法id")
  281. }
  282. var bill model.ProductBill
  283. option := &repo.DocSearchOptions{
  284. CollectName: repo.CollectionBillProduct,
  285. Query: repo.Map{"_id": id},
  286. }
  287. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  288. if !found || err != nil {
  289. log.Info(err)
  290. return nil, errors.New("数据未找到")
  291. }
  292. f := excelize.NewFile()
  293. index := f.NewSheet("Sheet1")
  294. f.SetActiveSheet(index)
  295. f.SetDefaultFont("宋体")
  296. billExcel := NewProductBill(f)
  297. // 获取已审核的签名数据
  298. if bill.Reviewed == 1 {
  299. if len(bill.SignUsers) > 0 {
  300. signs := []*model.Signature{}
  301. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  302. CollectName: repo.CollectionSignature,
  303. Query: repo.Map{"_id": bson.M{"$in": bill.SignUsers}},
  304. Sort: bson.M{"sort": 1}, // 升序
  305. }, &signs)
  306. billExcel.Signatures = signs
  307. }
  308. }
  309. billExcel.Content = &bill
  310. billExcel.IsPdf = isPdf
  311. billExcel.Title = getCompanyName(apictx)
  312. //设置对应的数据
  313. billExcel.Draws()
  314. // 下载为pdf
  315. if isPdf == "true" {
  316. buf, _ := f.WriteToBuffer()
  317. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  318. if err != nil {
  319. return nil, errors.New("转化pdf失败")
  320. }
  321. defer res.Body.Close()
  322. c.Header("Content-Type", "application/octet-stream")
  323. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  324. c.Header("Content-Transfer-Encoding", "binary")
  325. err = res.Write(c.Writer)
  326. if err != nil {
  327. return nil, err
  328. }
  329. return nil, nil
  330. }
  331. c.Header("Content-Type", "application/octet-stream")
  332. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  333. c.Header("Content-Transfer-Encoding", "binary")
  334. err = f.Write(c.Writer)
  335. if err != nil {
  336. return nil, err
  337. }
  338. return nil, nil
  339. }