bill-product.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. <<<<<<< HEAD
  15. // 成品采购单据管理
  16. =======
  17. // 单据管理
  18. >>>>>>> bb4e90e71ab5ef881ab438960e3aa9d606f4899c
  19. func BillProduct(r *GinRouter) {
  20. // 创建单据
  21. r.POSTJWT("/bill/product/create", CreateProductBill)
  22. // 获取单据详情
  23. r.GET("/bill/product/detail/:id", GetProductBill)
  24. // 获取单据列表
  25. r.GET("/bill/product/list", GetProductBills)
  26. // 更新单据
  27. r.POST("/bill/product/update", UpdateProductBill)
  28. // 删除单据
  29. r.POST("/bill/product/delete/:id", DelProductBill)
  30. //下载单据
  31. r.GET("/bill/product/download", DownProductBill)
  32. // 审核单据
  33. r.POSTJWT("/bill/product/review/:id", ProductReview)
  34. }
  35. // 审核单据
  36. func ProductReview(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  37. _id := c.Param("id")
  38. id, err := primitive.ObjectIDFromHex(_id)
  39. if err != nil {
  40. return nil, errors.New("id错误")
  41. }
  42. userId, err := primitive.ObjectIDFromHex(apictx.User.Parent)
  43. if err != nil {
  44. return nil, errors.New("用户异常")
  45. }
  46. user, err := getUserById(apictx, userId)
  47. if err != nil {
  48. return nil, errors.New("查找用户失败")
  49. }
  50. if !isManager(user.Roles) {
  51. return nil, errors.New("该用户没有权限")
  52. }
  53. // 查询单据获取已有的签字
  54. <<<<<<< HEAD
  55. bill := model.ProduceBill{}
  56. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  57. CollectName: repo.CollectionBillProduce,
  58. =======
  59. bill := model.ProductBill{}
  60. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  61. CollectName: repo.CollectionBillProduct,
  62. >>>>>>> bb4e90e71ab5ef881ab438960e3aa9d606f4899c
  63. Query: repo.Map{"_id": id, "reviewed": 1},
  64. }, &bill)
  65. signs := make([]primitive.ObjectID, 0)
  66. if len(bill.SignUsers) > 0 {
  67. // 如果自己已存在该集合中了
  68. for _, signUser := range bill.SignUsers {
  69. if signUser == userId {
  70. return nil, errors.New("该单据您已审核过了")
  71. }
  72. }
  73. signs = bill.SignUsers
  74. }
  75. // 更改状态为已审核 并签字
  76. signs = append(signs, userId)
  77. <<<<<<< HEAD
  78. produce := model.ProduceBill{
  79. =======
  80. product := model.ProductBill{
  81. >>>>>>> bb4e90e71ab5ef881ab438960e3aa9d606f4899c
  82. Reviewed: 1,
  83. UpdateTime: time.Now(),
  84. SignUsers: signs,
  85. }
  86. <<<<<<< HEAD
  87. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, _id, &produce)
  88. }
  89. // 创建成品采购单据
  90. =======
  91. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, _id, &product)
  92. }
  93. // 创建生产加工单据
  94. >>>>>>> bb4e90e71ab5ef881ab438960e3aa9d606f4899c
  95. func CreateProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  96. bill := &model.ProductBill{}
  97. err := c.ShouldBindJSON(bill)
  98. if err != nil {
  99. fmt.Println(err)
  100. return nil, errors.New("参数错误!")
  101. }
  102. ctx := apictx.CreateRepoCtx()
  103. if bill.PackId.Hex() == "" {
  104. return nil, errors.New("包装产品id为空")
  105. }
  106. if bill.PlanId.Hex() == "" {
  107. return nil, errors.New("生产计划id为空")
  108. }
  109. if bill.Type == "" {
  110. return nil, errors.New("类型为空")
  111. }
  112. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  113. if err != nil {
  114. return nil, err
  115. }
  116. bill.Status = "created"
  117. if bill.Reviewed == 0 {
  118. bill.Reviewed = -1
  119. }
  120. bill.CreateTime = time.Now()
  121. bill.UpdateTime = time.Now()
  122. // 制单人数据
  123. userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  124. fmt.Println("userId:", apictx.User.Parent)
  125. if !userId.IsZero() {
  126. user, err := getUserById(apictx, userId)
  127. if err == nil {
  128. bill.UserName = user.Name
  129. bill.UserId = userId
  130. }
  131. }
  132. result, err := repo.RepoAddDoc(ctx, repo.CollectionBillProduct, &bill)
  133. return result, err
  134. }
  135. // 获取单据信息
  136. func GetProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  137. billId := c.Param("id")
  138. id, err := primitive.ObjectIDFromHex(billId)
  139. if err != nil {
  140. return nil, errors.New("非法id")
  141. }
  142. var bill model.ProductBill
  143. option := &repo.DocSearchOptions{
  144. <<<<<<< HEAD
  145. CollectName: repo.CollectionBillProduce,
  146. =======
  147. CollectName: repo.CollectionBillProduct,
  148. >>>>>>> bb4e90e71ab5ef881ab438960e3aa9d606f4899c
  149. Query: repo.Map{"_id": id},
  150. }
  151. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  152. if !found || err != nil {
  153. log.Info(err)
  154. return nil, errors.New("数据未找到")
  155. }
  156. return bill, nil
  157. }
  158. // 获取单据列表
  159. func GetProductBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  160. page, size, query := UtilQueryPageSize(c)
  161. if query["packId"] != nil {
  162. query["packId"], _ = primitive.ObjectIDFromHex(query["packId"].(string))
  163. }
  164. if query["planId"] != nil {
  165. query["planId"], _ = primitive.ObjectIDFromHex(query["planId"].(string))
  166. }
  167. // 时间范围查询
  168. // createTime 选中的当天时间
  169. st, ok1 := query["startTime"]
  170. delete(query, "startTime")
  171. et, ok2 := query["endTime"]
  172. delete(query, "endTime")
  173. if ok1 && ok2 {
  174. startTime := st.(string)
  175. endTime := et.(string)
  176. start, end := getTimeRange(startTime, endTime)
  177. query["createTime"] = bson.M{"$gte": start, "$lte": end}
  178. }
  179. option := &repo.PageSearchOptions{
  180. <<<<<<< HEAD
  181. CollectName: repo.CollectionBillProduce,
  182. =======
  183. CollectName: repo.CollectionBillProduct,
  184. >>>>>>> bb4e90e71ab5ef881ab438960e3aa9d606f4899c
  185. Query: query,
  186. Page: page,
  187. Size: size,
  188. Sort: bson.M{"createTime": -1},
  189. }
  190. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  191. }
  192. // 更新单据
  193. func UpdateProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  194. <<<<<<< HEAD
  195. var bill model.ProduceBill
  196. =======
  197. var bill model.ProductBill
  198. >>>>>>> bb4e90e71ab5ef881ab438960e3aa9d606f4899c
  199. err := c.ShouldBindJSON(&bill)
  200. if err != nil {
  201. fmt.Println(err)
  202. return nil, errors.New("参数错误")
  203. }
  204. if bill.Id.Hex() == "" {
  205. return nil, errors.New("id的为空")
  206. }
  207. billType, err := searchBillTypeById(apictx, repo.CollectionBillProduct, bill.Id)
  208. if err != nil {
  209. return nil, err
  210. }
  211. // 如果更改类型
  212. if billType != bill.Type {
  213. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  214. if err != nil {
  215. return nil, err
  216. }
  217. }
  218. // 计算结算价格
  219. if bill.Status == "complete" {
  220. bill.CompleteTime = time.Now()
  221. }
  222. bill.UpdateTime = time.Now()
  223. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, bill.Id.Hex(), &bill)
  224. }
  225. // 删除单据
  226. func DelProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  227. billId := c.Param("id")
  228. if billId == "" {
  229. return nil, errors.New("id为空")
  230. }
  231. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, billId)
  232. }
  233. func DownProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  234. billId := c.Query("id")
  235. isPdf := c.Query("isPdf")
  236. if len(billId) < 1 {
  237. return nil, fmt.Errorf("id不能为空")
  238. }
  239. id, err := primitive.ObjectIDFromHex(billId)
  240. if err != nil {
  241. return nil, errors.New("非法id")
  242. }
  243. var bill model.ProductBill
  244. option := &repo.DocSearchOptions{
  245. CollectName: repo.CollectionBillProduct,
  246. Query: repo.Map{"_id": id},
  247. }
  248. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  249. if !found || err != nil {
  250. log.Info(err)
  251. return nil, errors.New("数据未找到")
  252. }
  253. f := excelize.NewFile()
  254. // Create a new sheet.
  255. index := f.NewSheet("Sheet1")
  256. f.SetActiveSheet(index)
  257. f.SetDefaultFont("宋体")
  258. <<<<<<< HEAD
  259. billExcel := NewProduceBill(f)
  260. =======
  261. billExcel := NewProductBill(f)
  262. >>>>>>> bb4e90e71ab5ef881ab438960e3aa9d606f4899c
  263. // 获取已审核的签名数据
  264. if bill.Reviewed == 1 {
  265. if len(bill.SignUsers) > 0 {
  266. signs := []*model.Signature{}
  267. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  268. CollectName: repo.CollectionSignature,
  269. Query: repo.Map{"_id": bson.M{"$in": bill.SignUsers}},
  270. Sort: bson.M{"sort": 1}, // 升序
  271. }, &signs)
  272. billExcel.Signatures = signs
  273. }
  274. }
  275. <<<<<<< HEAD
  276. // billExcel.Content = &bill
  277. companyName := getCompanyName(apictx)
  278. billExcel.Title = fmt.Sprintf("%s加工单", companyName)
  279. =======
  280. billExcel.Content = &bill
  281. companyName := getCompanyName(apictx)
  282. billExcel.Title = fmt.Sprintf("%s成品采购单", companyName)
  283. >>>>>>> bb4e90e71ab5ef881ab438960e3aa9d606f4899c
  284. //设置对应的数据
  285. billExcel.Draws()
  286. // 下载为pdf
  287. if isPdf == "true" {
  288. buf, _ := f.WriteToBuffer()
  289. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  290. if err != nil {
  291. return nil, errors.New("转化pdf失败")
  292. }
  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. }