bill-produce.go 8.0 KB

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