bill-produce.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. bill.IsAck = &notAck
  112. // 制单人数据
  113. userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  114. fmt.Println("userId:", apictx.User.Parent)
  115. userInfo := &model.UserSmaple{}
  116. if !userId.IsZero() {
  117. user, err := getUserById(apictx, userId)
  118. userInfo = user
  119. if err == nil {
  120. bill.UserName = user.Name
  121. bill.UserId = userId
  122. }
  123. }
  124. result, err := repo.RepoAddDoc1(ctx, repo.CollectionBillProduce, &bill, &repo.RecordLogReq{
  125. Path: c.Request.URL.Path,
  126. UserInfo: userInfo,
  127. TargetId: "",
  128. Type: "created",
  129. })
  130. return result, err
  131. }
  132. // 获取单据信息
  133. func GetProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  134. billId := c.Param("id")
  135. id, err := primitive.ObjectIDFromHex(billId)
  136. if err != nil {
  137. return nil, errors.New("非法id")
  138. }
  139. var bill model.ProduceBill
  140. option := &repo.DocSearchOptions{
  141. CollectName: repo.CollectionBillProduce,
  142. Query: repo.Map{"_id": id},
  143. }
  144. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  145. if !found || err != nil {
  146. log.Info(err)
  147. return nil, errors.New("数据未找到")
  148. }
  149. return bill, nil
  150. }
  151. // 获取单据列表
  152. func GetProduceBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  153. page, size, query := UtilQueryPageSize(c)
  154. option := &repo.PageSearchOptions{
  155. CollectName: repo.CollectionBillProduce,
  156. Query: makeBillQuery(query),
  157. Page: page,
  158. Size: size,
  159. Sort: bson.M{"createTime": -1},
  160. }
  161. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  162. }
  163. // 更新单据
  164. func UpdateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  165. var bill model.ProduceBill
  166. err := c.ShouldBindJSON(&bill)
  167. if err != nil {
  168. fmt.Println(err)
  169. return nil, errors.New("参数错误")
  170. }
  171. if bill.Id.Hex() == "" {
  172. return nil, errors.New("id的为空")
  173. }
  174. // 如果更改类型 重新生成订单号
  175. if len(bill.Type) > 0 {
  176. billType, err := searchBillTypeById(apictx, repo.CollectionBillProduce, bill.Id)
  177. if err != nil {
  178. return nil, err
  179. }
  180. if billType != bill.Type {
  181. bill.SerialNumber, err = generateSerial(c, apictx, bill.Type)
  182. if err != nil {
  183. return nil, err
  184. }
  185. }
  186. }
  187. userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
  188. user, _ := getUserById(apictx, userId)
  189. logType := "update"
  190. // 计算结算价格
  191. if bill.Status == "complete" {
  192. bill.CompleteTime = time.Now()
  193. logType = "complete"
  194. }
  195. if bill.Status == "deprecated" {
  196. logType = "deprecated"
  197. }
  198. if bill.Remark == "" {
  199. bill.Remark = " "
  200. }
  201. if bill.SupplierRemark == "" {
  202. bill.SupplierRemark = " "
  203. }
  204. // 修改单据信息需要同步plan中stage项
  205. if len(bill.Produces) > 0 {
  206. // 获取当前订单供应商id
  207. // 对比供应商是否变化,变化了就同步计划中的供应商
  208. currProduce := &model.ProduceBill{}
  209. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  210. CollectName: repo.CollectionBillProduce,
  211. Query: repo.Map{"_id": bill.Id},
  212. Project: []string{"supplierId"},
  213. }, currProduce)
  214. var supplierInfo *model.Supplier
  215. if currProduce.SupplierId != bill.SupplierId {
  216. // 查询更改后的supplierInfo
  217. supplierInfo = &model.Supplier{}
  218. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  219. CollectName: repo.CollectionSupplier,
  220. }, supplierInfo)
  221. }
  222. idStatges := make(map[string]*UpdateBilltoStageReq)
  223. for _, produce := range bill.Produces {
  224. if len(produce.Id) == 0 {
  225. continue
  226. }
  227. ps := strings.Split(produce.PrintSize, "*")
  228. width := 0
  229. height := 0
  230. if len(ps) == 2 {
  231. height, _ = strconv.Atoi(ps[0])
  232. width, _ = strconv.Atoi(ps[1])
  233. }
  234. idStatges[produce.Id] = &UpdateBilltoStageReq{
  235. BillType: "produce",
  236. SupplierInfo: supplierInfo,
  237. Norm: produce.Norm,
  238. Price2: produce.Price2,
  239. OrderCount: produce.OrderCount,
  240. OrderPrice: produce.OrderPrice,
  241. ConfirmCount: produce.ConfirmCount,
  242. Remark: produce.Remark,
  243. Width: width,
  244. Height: height,
  245. DeliveryTime: produce.DeliveryTime,
  246. }
  247. }
  248. _, err := updateBilltoStage(c, bill.PlanId, idStatges, apictx)
  249. if err != nil {
  250. return nil, errors.New("该单据改动同步到产品失败")
  251. }
  252. fmt.Println("单据同步到产品,planId:", bill.PlanId)
  253. }
  254. bill.UpdateTime = time.Now()
  255. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill)
  256. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill, &repo.RecordLogReq{
  257. Path: c.Request.URL.Path,
  258. UserInfo: user,
  259. TargetId: bill.Id.Hex(),
  260. Type: logType,
  261. })
  262. }
  263. // 删除单据
  264. func DelProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  265. billId := c.Param("id")
  266. if billId == "" {
  267. return nil, errors.New("id为空")
  268. }
  269. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId)
  270. }
  271. func DownProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  272. billId := c.Query("id")
  273. isPdf := c.Query("isPdf")
  274. if len(billId) < 1 {
  275. return nil, fmt.Errorf("id不能为空")
  276. }
  277. id, err := primitive.ObjectIDFromHex(billId)
  278. if err != nil {
  279. return nil, errors.New("非法id")
  280. }
  281. var bill model.ProduceBill
  282. option := &repo.DocSearchOptions{
  283. CollectName: repo.CollectionBillProduce,
  284. Query: repo.Map{"_id": id},
  285. }
  286. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  287. if !found || err != nil {
  288. log.Info(err)
  289. return nil, errors.New("数据未找到")
  290. }
  291. f := excelize.NewFile()
  292. // Create a new sheet.
  293. index := f.NewSheet("Sheet1")
  294. f.SetActiveSheet(index)
  295. f.SetDefaultFont("宋体")
  296. billExcel := NewProduceBill(f)
  297. // 获取已审核的签名数据
  298. if bill.Reviewed == 1 {
  299. if len(bill.SignUsers) > 0 {
  300. signs := []*model.Signature{}
  301. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  302. CollectName: repo.CollectionSignature,
  303. Query: repo.Map{"_id": bson.M{"$in": bill.SignUsers}},
  304. Sort: bson.M{"sort": 1}, // 升序
  305. }, &signs)
  306. billExcel.Signatures = signs
  307. }
  308. }
  309. // 覆膜、打印与其他有来纸尺寸类型互斥
  310. // 如果是这两种类型,不管isPaper的值,都需要有自己的表格
  311. if bill.IsLam || bill.IsPrint {
  312. bill.IsPaper = false
  313. }
  314. billExcel.Content = &bill
  315. billExcel.IsPdf = isPdf
  316. companyName := getCompanyName(apictx)
  317. billExcel.Title = fmt.Sprintf("%s加工单", companyName)
  318. //设置对应的数据
  319. billExcel.Draws()
  320. // 下载为pdf
  321. if isPdf == "true" {
  322. buf, _ := f.WriteToBuffer()
  323. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  324. if err != nil {
  325. return nil, errors.New("转化pdf失败")
  326. }
  327. defer res.Body.Close()
  328. c.Header("Content-Type", "application/octet-stream")
  329. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  330. c.Header("Content-Transfer-Encoding", "binary")
  331. err = res.Write(c.Writer)
  332. if err != nil {
  333. return nil, err
  334. }
  335. return nil, nil
  336. }
  337. c.Header("Content-Type", "application/octet-stream")
  338. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  339. c.Header("Content-Transfer-Encoding", "binary")
  340. err = f.Write(c.Writer)
  341. if err != nil {
  342. return nil, err
  343. }
  344. return nil, nil
  345. }