bill-produce.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 BillProduce(r *GinRouter) {
  16. // 创建单据
  17. r.POSTJWT("/bill/produce/create", CreateProduceBill)
  18. // 获取单据详情
  19. r.GETJWT("/bill/produce/detail/:id", GetProduceBill)
  20. // 获取单据列表
  21. r.GETJWT("/bill/produce/list", GetProduceBills)
  22. // 更新单据
  23. r.POSTJWT("/bill/produce/update", UpdateProduceBill)
  24. // 删除单据
  25. r.POSTJWT("/bill/produce/delete/:id", DelProduceBill)
  26. //下载单据
  27. r.GETJWT("/bill/produce/download", DownProduceBill)
  28. // 审核单据
  29. r.POSTJWT("/bill/produce/review/:id", ProduceReview)
  30. }
  31. // 审核单据
  32. func ProduceReview(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.ProduceBill{}
  51. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  52. CollectName: repo.CollectionBillProduce,
  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. produce := model.ProduceBill{
  68. Reviewed: 1,
  69. UpdateTime: time.Now(),
  70. SignUsers: signs,
  71. }
  72. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, _id, &produce)
  73. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduce, _id, &produce, &repo.RecordLogReq{
  74. Path: c.Request.URL.Path,
  75. UserInfo: user,
  76. TargetId: _id,
  77. Type: "reviewed",
  78. })
  79. }
  80. // 创建生产加工单据
  81. func CreateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  82. bill := &model.ProduceBill{}
  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.CollectionBillProduce, &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 GetProduceBill(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.ProduceBill
  138. option := &repo.DocSearchOptions{
  139. CollectName: repo.CollectionBillProduce,
  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 GetProduceBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  151. page, size, query := UtilQueryPageSize(c)
  152. option := &repo.PageSearchOptions{
  153. CollectName: repo.CollectionBillProduce,
  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 UpdateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  163. var bill model.ProduceBill
  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.CollectionBillProduce, 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. userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
  186. user, _ := getUserById(apictx, userId)
  187. logType := "update"
  188. // 计算结算价格
  189. if bill.Status == "complete" {
  190. bill.CompleteTime = time.Now()
  191. logType = "complete"
  192. }
  193. if bill.Status == "deprecated" {
  194. logType = "deprecated"
  195. }
  196. if bill.Remark == "" {
  197. bill.Remark = " "
  198. }
  199. if bill.SupplierRemark == "" {
  200. bill.SupplierRemark = " "
  201. }
  202. // 获取当前订单提交数
  203. // 对比提交数量是否变化,变化了就同步计划中的提交数
  204. currProduce := &model.ProduceBill{}
  205. currConfirmCountMap := map[string]int{}
  206. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  207. CollectName: repo.CollectionBillProduce,
  208. Query: repo.Map{"_id": bill.Id},
  209. Project: []string{"produces"},
  210. }, currProduce)
  211. if len(currProduce.Produces) > 0 {
  212. for _, cp := range currProduce.Produces {
  213. currConfirmCountMap[cp.Id] = cp.ConfirmCount
  214. }
  215. }
  216. isSyncConfirm := false
  217. // 更新供应商确定数量与plan中stage项的同步
  218. if len(bill.Produces) > 0 {
  219. idCounts := map[string]int{}
  220. for _, produce := range bill.Produces {
  221. if len(produce.Id) == 0 {
  222. continue
  223. }
  224. // 对比提交数量不一致时
  225. if v, ok := currConfirmCountMap[produce.Id]; ok {
  226. if v != produce.ConfirmCount {
  227. isSyncConfirm = true
  228. idCounts[produce.Id] = produce.ConfirmCount
  229. }
  230. }
  231. }
  232. fmt.Println("单据变化的提交数量:", idCounts)
  233. if isSyncConfirm {
  234. result, err := updateStageCount(c, bill.PlanId, idCounts, apictx)
  235. if err != nil {
  236. fmt.Println(err)
  237. log.Error(err)
  238. }
  239. fmt.Println(result)
  240. }
  241. }
  242. bill.UpdateTime = time.Now()
  243. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill)
  244. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill, &repo.RecordLogReq{
  245. Path: c.Request.URL.Path,
  246. UserInfo: user,
  247. TargetId: bill.Id.Hex(),
  248. Type: logType,
  249. })
  250. }
  251. // 删除单据
  252. func DelProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  253. billId := c.Param("id")
  254. if billId == "" {
  255. return nil, errors.New("id为空")
  256. }
  257. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId)
  258. }
  259. func DownProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  260. billId := c.Query("id")
  261. isPdf := c.Query("isPdf")
  262. if len(billId) < 1 {
  263. return nil, fmt.Errorf("id不能为空")
  264. }
  265. id, err := primitive.ObjectIDFromHex(billId)
  266. if err != nil {
  267. return nil, errors.New("非法id")
  268. }
  269. var bill model.ProduceBill
  270. option := &repo.DocSearchOptions{
  271. CollectName: repo.CollectionBillProduce,
  272. Query: repo.Map{"_id": id},
  273. }
  274. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  275. if !found || err != nil {
  276. log.Info(err)
  277. return nil, errors.New("数据未找到")
  278. }
  279. f := excelize.NewFile()
  280. // Create a new sheet.
  281. index := f.NewSheet("Sheet1")
  282. f.SetActiveSheet(index)
  283. f.SetDefaultFont("宋体")
  284. billExcel := NewProduceBill(f)
  285. // 获取已审核的签名数据
  286. if bill.Reviewed == 1 {
  287. if len(bill.SignUsers) > 0 {
  288. signs := []*model.Signature{}
  289. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  290. CollectName: repo.CollectionSignature,
  291. Query: repo.Map{"_id": bson.M{"$in": bill.SignUsers}},
  292. Sort: bson.M{"sort": 1}, // 升序
  293. }, &signs)
  294. billExcel.Signatures = signs
  295. }
  296. }
  297. // 覆膜、打印与其他有来纸尺寸类型互斥
  298. // 如果是这两种类型,不管isPaper的值,都需要有自己的表格
  299. if bill.IsLam || bill.IsPrint {
  300. bill.IsPaper = false
  301. }
  302. billExcel.Content = &bill
  303. billExcel.IsPdf = isPdf
  304. companyName := getCompanyName(apictx)
  305. billExcel.Title = fmt.Sprintf("%s加工单", companyName)
  306. //设置对应的数据
  307. billExcel.Draws()
  308. // 下载为pdf
  309. if isPdf == "true" {
  310. buf, _ := f.WriteToBuffer()
  311. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  312. if err != nil {
  313. return nil, errors.New("转化pdf失败")
  314. }
  315. defer res.Body.Close()
  316. c.Header("Content-Type", "application/octet-stream")
  317. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  318. c.Header("Content-Transfer-Encoding", "binary")
  319. err = res.Write(c.Writer)
  320. if err != nil {
  321. return nil, err
  322. }
  323. return nil, nil
  324. }
  325. c.Header("Content-Type", "application/octet-stream")
  326. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  327. c.Header("Content-Transfer-Encoding", "binary")
  328. err = f.Write(c.Writer)
  329. if err != nil {
  330. return nil, err
  331. }
  332. return nil, nil
  333. }