bill-product.go 10 KB

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