bill-produce.go 8.5 KB

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