bill-produce.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. // 更新供应商确定数量与plan中stage项的同步
  203. if len(bill.Produces) > 0 {
  204. idCounts := map[string]int{}
  205. for _, produce := range bill.Produces {
  206. if len(produce.Id) == 0 {
  207. continue
  208. }
  209. idCounts[produce.Id] = produce.ConfirmCount
  210. }
  211. fmt.Println(idCounts)
  212. result, err := updateStageCount(c, bill.PlanId, idCounts, apictx)
  213. if err != nil {
  214. fmt.Println(err)
  215. log.Error(err)
  216. }
  217. fmt.Println(result)
  218. }
  219. bill.UpdateTime = time.Now()
  220. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill)
  221. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill, &repo.RecordLogReq{
  222. Path: c.Request.URL.Path,
  223. UserInfo: user,
  224. TargetId: bill.Id.Hex(),
  225. Type: logType,
  226. })
  227. }
  228. // 删除单据
  229. func DelProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  230. billId := c.Param("id")
  231. if billId == "" {
  232. return nil, errors.New("id为空")
  233. }
  234. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId)
  235. }
  236. func DownProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  237. billId := c.Query("id")
  238. isPdf := c.Query("isPdf")
  239. if len(billId) < 1 {
  240. return nil, fmt.Errorf("id不能为空")
  241. }
  242. id, err := primitive.ObjectIDFromHex(billId)
  243. if err != nil {
  244. return nil, errors.New("非法id")
  245. }
  246. var bill model.ProduceBill
  247. option := &repo.DocSearchOptions{
  248. CollectName: repo.CollectionBillProduce,
  249. Query: repo.Map{"_id": id},
  250. }
  251. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  252. if !found || err != nil {
  253. log.Info(err)
  254. return nil, errors.New("数据未找到")
  255. }
  256. f := excelize.NewFile()
  257. // Create a new sheet.
  258. index := f.NewSheet("Sheet1")
  259. f.SetActiveSheet(index)
  260. f.SetDefaultFont("宋体")
  261. billExcel := NewProduceBill(f)
  262. // 获取已审核的签名数据
  263. if bill.Reviewed == 1 {
  264. if len(bill.SignUsers) > 0 {
  265. signs := []*model.Signature{}
  266. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  267. CollectName: repo.CollectionSignature,
  268. Query: repo.Map{"_id": bson.M{"$in": bill.SignUsers}},
  269. Sort: bson.M{"sort": 1}, // 升序
  270. }, &signs)
  271. billExcel.Signatures = signs
  272. }
  273. }
  274. // 覆膜、打印与其他有来纸尺寸类型互斥
  275. // 如果是这两种类型,不管isPaper的值,都需要有自己的表格
  276. if bill.IsLam || bill.IsPrint {
  277. bill.IsPaper = false
  278. }
  279. billExcel.Content = &bill
  280. billExcel.IsPdf = isPdf
  281. companyName := getCompanyName(apictx)
  282. billExcel.Title = fmt.Sprintf("%s加工单", companyName)
  283. //设置对应的数据
  284. billExcel.Draws()
  285. // 下载为pdf
  286. if isPdf == "true" {
  287. buf, _ := f.WriteToBuffer()
  288. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  289. if err != nil {
  290. return nil, errors.New("转化pdf失败")
  291. }
  292. defer res.Body.Close()
  293. c.Header("Content-Type", "application/octet-stream")
  294. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  295. c.Header("Content-Transfer-Encoding", "binary")
  296. err = res.Write(c.Writer)
  297. if err != nil {
  298. return nil, err
  299. }
  300. return nil, nil
  301. }
  302. c.Header("Content-Type", "application/octet-stream")
  303. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  304. c.Header("Content-Transfer-Encoding", "binary")
  305. err = f.Write(c.Writer)
  306. if err != nil {
  307. return nil, err
  308. }
  309. return nil, nil
  310. }