bill-produce.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "fmt"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/gin-gonic/gin"
  12. "github.com/xuri/excelize/v2"
  13. "go.mongodb.org/mongo-driver/bson"
  14. "go.mongodb.org/mongo-driver/bson/primitive"
  15. )
  16. // 单据管理
  17. func BillProduce(r *GinRouter) {
  18. // 创建单据
  19. r.POSTJWT("/bill/produce/create", CreateProduceBill)
  20. // 获取单据详情
  21. r.GETJWT("/bill/produce/detail/:id", GetProduceBill)
  22. // 获取单据列表
  23. r.GETJWT("/bill/produce/list", GetProduceBills)
  24. // 更新单据
  25. r.POSTJWT("/bill/produce/update", UpdateProduceBill)
  26. // 删除单据
  27. r.POSTJWT("/bill/produce/delete/:id", DelProduceBill)
  28. //下载单据
  29. r.GETJWT("/bill/produce/download", DownProduceBill)
  30. // 审核单据
  31. r.POSTJWT("/bill/produce/review/:id", ProduceReview)
  32. }
  33. // 审核单据
  34. func ProduceReview(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  35. _id := c.Param("id")
  36. id, err := primitive.ObjectIDFromHex(_id)
  37. if err != nil {
  38. return nil, errors.New("id错误")
  39. }
  40. userId, err := primitive.ObjectIDFromHex(apictx.User.Parent)
  41. if err != nil {
  42. return nil, errors.New("用户异常")
  43. }
  44. user, err := getUserById(apictx, userId)
  45. if err != nil {
  46. return nil, errors.New("查找用户失败")
  47. }
  48. if !isManager(user.Roles) {
  49. return nil, errors.New("该用户没有权限")
  50. }
  51. // 查询单据获取已有的签字
  52. bill := model.ProduceBill{}
  53. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  54. CollectName: repo.CollectionBillProduce,
  55. Query: repo.Map{"_id": id, "reviewed": 1},
  56. }, &bill)
  57. signs := make([]primitive.ObjectID, 0)
  58. if len(bill.SignUsers) > 0 {
  59. // 如果自己已存在该集合中了
  60. for _, signUser := range bill.SignUsers {
  61. if signUser == userId {
  62. return nil, errors.New("该单据您已审核过了")
  63. }
  64. }
  65. signs = bill.SignUsers
  66. }
  67. // 更改状态为已审核 并签字
  68. signs = append(signs, userId)
  69. produce := model.ProduceBill{
  70. Reviewed: 1,
  71. UpdateTime: time.Now(),
  72. SignUsers: signs,
  73. }
  74. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, _id, &produce)
  75. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduce, _id, &produce, &repo.RecordLogReq{
  76. Path: c.Request.URL.Path,
  77. UserInfo: user,
  78. TargetId: _id,
  79. Type: "reviewed",
  80. })
  81. }
  82. // 创建生产加工单据
  83. func CreateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  84. bill := &model.ProduceBill{}
  85. err := c.ShouldBindJSON(bill)
  86. if err != nil {
  87. fmt.Println(err)
  88. return nil, errors.New("参数错误!")
  89. }
  90. ctx := apictx.CreateRepoCtx()
  91. if bill.PackId.Hex() == "" {
  92. return nil, errors.New("包装产品id为空")
  93. }
  94. if bill.PlanId.Hex() == "" {
  95. return nil, errors.New("生产计划id为空")
  96. }
  97. if bill.Type == "" {
  98. return nil, errors.New("类型为空")
  99. }
  100. bill.SerialNumber, err = generateSerial(c, apictx, bill.Type)
  101. if err != nil {
  102. return nil, err
  103. }
  104. bill.Status = "created"
  105. if bill.Reviewed == 0 {
  106. bill.Reviewed = -1
  107. }
  108. bill.CreateTime = time.Now()
  109. bill.UpdateTime = time.Now()
  110. notAck := false
  111. _isSend := false
  112. bill.IsSend = &_isSend
  113. bill.IsAck = &notAck
  114. // 制单人数据
  115. userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  116. fmt.Println("userId:", apictx.User.Parent)
  117. userInfo := &model.UserSmaple{}
  118. if !userId.IsZero() {
  119. user, err := getUserById(apictx, userId)
  120. userInfo = user
  121. if err == nil {
  122. bill.UserName = user.Name
  123. bill.UserId = userId
  124. }
  125. }
  126. result, err := repo.RepoAddDoc1(ctx, repo.CollectionBillProduce, &bill, &repo.RecordLogReq{
  127. Path: c.Request.URL.Path,
  128. UserInfo: userInfo,
  129. TargetId: "",
  130. Type: "created",
  131. })
  132. return result, err
  133. }
  134. // 获取单据信息
  135. func GetProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  136. billId := c.Param("id")
  137. id, err := primitive.ObjectIDFromHex(billId)
  138. if err != nil {
  139. return nil, errors.New("非法id")
  140. }
  141. var bill model.ProduceBill
  142. option := &repo.DocSearchOptions{
  143. CollectName: repo.CollectionBillProduce,
  144. Query: repo.Map{"_id": id},
  145. }
  146. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  147. if !found || err != nil {
  148. log.Info(err)
  149. return nil, errors.New("数据未找到")
  150. }
  151. return bill, nil
  152. }
  153. // 获取单据列表
  154. func GetProduceBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  155. page, size, query := UtilQueryPageSize(c)
  156. option := &repo.PageSearchOptions{
  157. CollectName: repo.CollectionBillProduce,
  158. Query: makeBillQuery(query),
  159. Page: page,
  160. Size: size,
  161. Sort: bson.M{"createTime": -1},
  162. }
  163. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  164. }
  165. // 更新单据
  166. func UpdateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  167. var bill model.ProduceBill
  168. err := c.ShouldBindJSON(&bill)
  169. if err != nil {
  170. fmt.Println(err)
  171. return nil, errors.New("参数错误")
  172. }
  173. if bill.Id.Hex() == "" {
  174. return nil, errors.New("id的为空")
  175. }
  176. // 如果更改类型 重新生成订单号
  177. if len(bill.Type) > 0 {
  178. billType, err := searchBillTypeById(apictx, repo.CollectionBillProduce, bill.Id)
  179. if err != nil {
  180. return nil, err
  181. }
  182. if billType != bill.Type {
  183. bill.SerialNumber, err = generateSerial(c, apictx, bill.Type)
  184. if err != nil {
  185. return nil, err
  186. }
  187. }
  188. }
  189. userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
  190. user, _ := getUserById(apictx, userId)
  191. logType := "update"
  192. // 更改状态
  193. ok, err := isCompareStatus(apictx, repo.CollectionBillProduce, bill.Id, bill.Status)
  194. if err != nil {
  195. return nil, err
  196. }
  197. if bill.Status == "complete" {
  198. if !ok {
  199. bill.CompleteTime = time.Now()
  200. logType = "complete"
  201. }
  202. }
  203. if bill.Status == "deprecated" {
  204. if !ok {
  205. logType = "deprecated"
  206. }
  207. }
  208. if bill.Remark == "" {
  209. bill.Remark = " "
  210. }
  211. if bill.SupplierRemark == "" {
  212. bill.SupplierRemark = " "
  213. }
  214. // 修改单据信息需要同步plan中stage项
  215. if len(bill.Produces) > 0 {
  216. // 获取当前订单供应商id
  217. // 对比供应商是否变化,变化了就同步计划中的供应商
  218. currProduce := &model.ProduceBill{}
  219. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  220. CollectName: repo.CollectionBillProduce,
  221. Query: repo.Map{"_id": bill.Id},
  222. Project: []string{"supplierId"},
  223. }, currProduce)
  224. var supplierInfo *model.Supplier
  225. if currProduce.SupplierId != bill.SupplierId {
  226. // 查询更改后的supplierInfo
  227. supplierInfo = &model.Supplier{}
  228. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  229. CollectName: repo.CollectionSupplier,
  230. Query: repo.Map{"_id": bill.SupplierId},
  231. }, supplierInfo)
  232. }
  233. idStatges := make(map[string]*UpdateBilltoStageReq)
  234. for _, produce := range bill.Produces {
  235. if len(produce.Id) == 0 {
  236. continue
  237. }
  238. ps := strings.Split(produce.PrintSize, "*")
  239. width := 0
  240. height := 0
  241. if len(ps) == 2 {
  242. height, _ = strconv.Atoi(ps[0])
  243. width, _ = strconv.Atoi(ps[1])
  244. }
  245. idStatges[produce.Id] = &UpdateBilltoStageReq{
  246. BillType: "produce",
  247. IsChangePrice2: bill.IsLam,
  248. SupplierInfo: supplierInfo,
  249. Norm: produce.Norm,
  250. Price2: produce.Price2,
  251. OrderCount: produce.OrderCount,
  252. OrderPrice: produce.OrderPrice,
  253. ConfirmCount: produce.ConfirmCount,
  254. Remark: produce.Remark,
  255. Width: width,
  256. Height: height,
  257. DeliveryTime: produce.DeliveryTime,
  258. }
  259. }
  260. _, err := updateBilltoStage(c, bill.PlanId, idStatges, apictx)
  261. if err != nil {
  262. return nil, errors.New("该单据改动同步到产品失败")
  263. }
  264. fmt.Println("单据同步到产品,planId:", bill.PlanId)
  265. }
  266. bill.UpdateTime = time.Now()
  267. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill)
  268. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill, &repo.RecordLogReq{
  269. Path: c.Request.URL.Path,
  270. UserInfo: user,
  271. TargetId: bill.Id.Hex(),
  272. Type: logType,
  273. })
  274. }
  275. // 删除单据
  276. func DelProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  277. billId := c.Param("id")
  278. if billId == "" {
  279. return nil, errors.New("id为空")
  280. }
  281. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId)
  282. }
  283. func DownProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  284. billId := c.Query("id")
  285. isPdf := c.Query("isPdf")
  286. if len(billId) < 1 {
  287. return nil, fmt.Errorf("id不能为空")
  288. }
  289. id, err := primitive.ObjectIDFromHex(billId)
  290. if err != nil {
  291. return nil, errors.New("非法id")
  292. }
  293. var bill model.ProduceBill
  294. option := &repo.DocSearchOptions{
  295. CollectName: repo.CollectionBillProduce,
  296. Query: repo.Map{"_id": id},
  297. }
  298. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  299. if !found || err != nil {
  300. log.Info(err)
  301. return nil, errors.New("数据未找到")
  302. }
  303. f := excelize.NewFile()
  304. // Create a new sheet.
  305. index := f.NewSheet("Sheet1")
  306. f.SetActiveSheet(index)
  307. f.SetDefaultFont("宋体")
  308. billExcel := NewProduceBill(f)
  309. // 获取已审核的签名数据
  310. if bill.Reviewed == 1 {
  311. if len(bill.SignUsers) > 0 {
  312. signs := []*model.Signature{}
  313. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  314. CollectName: repo.CollectionSignature,
  315. Query: repo.Map{"_id": bson.M{"$in": bill.SignUsers}},
  316. Sort: bson.M{"sort": 1}, // 升序
  317. }, &signs)
  318. billExcel.Signatures = signs
  319. }
  320. }
  321. // 覆膜、打印与其他有来纸尺寸类型互斥
  322. // 如果是这两种类型,不管isPaper的值,都需要有自己的表格
  323. if bill.IsLam || bill.IsPrint {
  324. bill.IsPaper = false
  325. }
  326. billExcel.Content = &bill
  327. billExcel.IsPdf = isPdf
  328. companyName := getCompanyName(apictx)
  329. billExcel.Title = fmt.Sprintf("%s加工单", companyName)
  330. //设置对应的数据
  331. billExcel.Draws()
  332. // 下载为pdf
  333. if isPdf == "true" {
  334. buf, _ := f.WriteToBuffer()
  335. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  336. if err != nil {
  337. return nil, errors.New("转化pdf失败")
  338. }
  339. defer res.Body.Close()
  340. c.Header("Content-Type", "application/octet-stream")
  341. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  342. c.Header("Content-Transfer-Encoding", "binary")
  343. err = res.Write(c.Writer)
  344. if err != nil {
  345. return nil, err
  346. }
  347. return nil, nil
  348. }
  349. c.Header("Content-Type", "application/octet-stream")
  350. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  351. c.Header("Content-Transfer-Encoding", "binary")
  352. err = f.Write(c.Writer)
  353. if err != nil {
  354. return nil, err
  355. }
  356. return nil, nil
  357. }