bill.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 Bill(r *GinRouter) {
  16. // 创建单据
  17. r.POSTJWT("/bill/purchase/create", CreateBill)
  18. // 获取单据详情
  19. r.GET("/bill/purchase/detail/:id", GetBill)
  20. // 获取单据列表
  21. r.GET("/bill/purchase/list", GetBills)
  22. // 获取单据列表
  23. r.GET("/bill/purchase/download", DownLoadBills)
  24. // 更新单据
  25. r.POST("/bill/purchase/update", UpdateBill)
  26. // 删除单据
  27. r.POST("/bill/purchase/delete/:id", DelBill)
  28. // 审核单据
  29. r.POSTJWT("/bill/purchase/review/:id", PurchaseReview)
  30. }
  31. // 审核单据
  32. func PurchaseReview(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.PurchaseBill{}
  51. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  52. CollectName: repo.CollectionBillPurchase,
  53. Query: repo.Map{"_id": id, "reviewed": 1},
  54. }, &bill)
  55. signs := make([]primitive.ObjectID, 0)
  56. if found && 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. purchase := model.PurchaseBill{
  68. Reviewed: 1,
  69. UpdateTime: time.Now(),
  70. SignUsers: signs,
  71. }
  72. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, _id, &purchase)
  73. }
  74. type MatBillReq struct {
  75. Bill *model.PurchaseBill
  76. CompIndex *int
  77. MatIndex *int
  78. //MatKey string //components.0.mats.0.billId
  79. }
  80. // 创建单据
  81. func CreateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  82. req := &model.PurchaseBill{}
  83. err := c.ShouldBindJSON(req)
  84. if err != nil {
  85. fmt.Println(err)
  86. return nil, errors.New("参数错误")
  87. }
  88. ctx := apictx.CreateRepoCtx()
  89. bill := req
  90. if bill.PackId.Hex() == "" {
  91. return nil, errors.New("包装产品id为空")
  92. }
  93. if bill.PlanId.Hex() == "" {
  94. return nil, errors.New("生产计划id为空")
  95. }
  96. if bill.Type == "" {
  97. return nil, errors.New("类型为空")
  98. }
  99. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  100. if err != nil {
  101. return nil, err
  102. }
  103. bill.Status = "created"
  104. if bill.Reviewed == 0 {
  105. bill.Reviewed = -1
  106. }
  107. bill.CreateTime = time.Now()
  108. bill.UpdateTime = time.Now()
  109. notAck := false
  110. bill.IsAck = &notAck
  111. // 制单人数据
  112. userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  113. fmt.Println("userId:", apictx.User.Parent)
  114. if !userId.IsZero() {
  115. user, err := getUserById(apictx, userId)
  116. if err == nil {
  117. bill.UserName = user.Name
  118. bill.UserId = userId
  119. }
  120. }
  121. return repo.RepoAddDoc(ctx, repo.CollectionBillPurchase, &bill)
  122. }
  123. // 获取单据信息
  124. func GetBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  125. billId := c.Param("id")
  126. id, err := primitive.ObjectIDFromHex(billId)
  127. if err != nil {
  128. return nil, errors.New("非法id")
  129. }
  130. var bill model.PurchaseBill
  131. option := &repo.DocSearchOptions{
  132. CollectName: repo.CollectionBillPurchase,
  133. Query: repo.Map{"_id": id},
  134. }
  135. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  136. if !found || err != nil {
  137. log.Info(err)
  138. return nil, errors.New("数据未找到")
  139. }
  140. return bill, nil
  141. }
  142. // 获取单据列表
  143. func GetBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  144. page, size, query := UtilQueryPageSize(c)
  145. if query["packId"] != nil {
  146. query["packId"], _ = primitive.ObjectIDFromHex(query["packId"].(string))
  147. }
  148. if query["planId"] != nil {
  149. query["planId"], _ = primitive.ObjectIDFromHex(query["planId"].(string))
  150. }
  151. // 时间范围查询
  152. // createTime 选中的当天时间
  153. st, ok1 := query["startTime"]
  154. delete(query, "startTime")
  155. et, ok2 := query["endTime"]
  156. delete(query, "endTime")
  157. if ok1 && ok2 {
  158. startTime := st.(string)
  159. endTime := et.(string)
  160. start, end := getTimeRange(startTime, endTime)
  161. query["createTime"] = bson.M{"$gte": start, "$lte": end}
  162. }
  163. if productName, ok := query["productName"]; ok {
  164. delete(query, "productName")
  165. query["productName"] = bson.M{"$regex": productName.(string)}
  166. }
  167. option := &repo.PageSearchOptions{
  168. CollectName: repo.CollectionBillPurchase,
  169. Query: query,
  170. Page: page,
  171. Size: size,
  172. Sort: bson.M{"createTime": -1},
  173. }
  174. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  175. }
  176. func DownLoadBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  177. billId := c.Query("id")
  178. isPdf := c.Query("isPdf")
  179. if len(billId) < 1 {
  180. return nil, fmt.Errorf("id不能为空")
  181. }
  182. id, err := primitive.ObjectIDFromHex(billId)
  183. if err != nil {
  184. return nil, errors.New("非法id")
  185. }
  186. var bill model.PurchaseBill
  187. option := &repo.DocSearchOptions{
  188. CollectName: repo.CollectionBillPurchase,
  189. Query: repo.Map{"_id": id},
  190. }
  191. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  192. if !found || err != nil {
  193. log.Info(err)
  194. return nil, errors.New("数据未找到")
  195. }
  196. f := excelize.NewFile()
  197. index := f.NewSheet("Sheet1")
  198. f.SetActiveSheet(index)
  199. f.SetDefaultFont("宋体")
  200. var billExcel *PurchaseBillExcel
  201. if len(bill.Paper) > 0 {
  202. billExcel = NewPurchaseBill(f)
  203. }
  204. if billExcel == nil {
  205. return nil, errors.New("数据未找到")
  206. }
  207. // 获取已审核的签名数据
  208. if bill.Reviewed == 1 {
  209. if len(bill.SignUsers) > 0 {
  210. signs := []*model.Signature{}
  211. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  212. CollectName: repo.CollectionSignature,
  213. Query: repo.Map{"_id": bson.M{"$in": bill.SignUsers}},
  214. Sort: bson.M{"sort": 1}, // 升序
  215. }, &signs)
  216. billExcel.Signatures = signs
  217. }
  218. }
  219. billExcel.Content = &bill
  220. billExcel.IsPdf = isPdf
  221. companyName := getCompanyName(apictx)
  222. billExcel.Title = fmt.Sprintf("%s原材料采购单", companyName)
  223. //设置对应的数据
  224. billExcel.Draws()
  225. // 下载为pdf
  226. if isPdf == "true" {
  227. buf, _ := f.WriteToBuffer()
  228. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  229. if err != nil {
  230. fmt.Println(err)
  231. return nil, errors.New("转化pdf失败")
  232. }
  233. defer res.Body.Close()
  234. c.Header("Content-Type", "application/octet-stream")
  235. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  236. c.Header("Content-Transfer-Encoding", "binary")
  237. err = res.Write(c.Writer)
  238. if err != nil {
  239. return nil, err
  240. }
  241. return nil, nil
  242. }
  243. // 下载为execl
  244. c.Header("Content-Type", "application/octet-stream")
  245. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  246. c.Header("Content-Transfer-Encoding", "binary")
  247. err = f.Write(c.Writer)
  248. if err != nil {
  249. return nil, err
  250. }
  251. return nil, nil
  252. }
  253. // 更新单据
  254. func UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  255. var bill model.PurchaseBill
  256. err := c.ShouldBindJSON(&bill)
  257. if err != nil {
  258. fmt.Println(err)
  259. return nil, errors.New("参数错误")
  260. }
  261. if bill.Id.Hex() == "" {
  262. return nil, errors.New("id的为空")
  263. }
  264. // 如果更改类型
  265. if len(bill.Type) > 0 {
  266. billType, err := searchBillTypeById(apictx, repo.CollectionBillPurchase, bill.Id)
  267. if err != nil {
  268. return nil, err
  269. }
  270. if billType != bill.Type {
  271. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  272. if err != nil {
  273. return nil, err
  274. }
  275. }
  276. }
  277. if bill.Status == "complete" {
  278. bill.CompleteTime = time.Now()
  279. }
  280. if bill.Remark == "" {
  281. bill.Remark = " "
  282. }
  283. if bill.SupplierRemark == "" {
  284. bill.SupplierRemark = " "
  285. }
  286. // 更新供应商确定数量与plan中stage项的同步
  287. if len(bill.Paper) > 0 {
  288. idCounts := map[string]int{}
  289. for _, paper := range bill.Paper {
  290. if len(paper.Id) == 0 {
  291. continue
  292. }
  293. idCounts[paper.Id] = paper.ConfirmCount
  294. }
  295. fmt.Println(idCounts)
  296. result, err := updateStageCount(bill.Id, bill.PlanId, idCounts, apictx)
  297. if err != nil {
  298. fmt.Println(err)
  299. log.Error(err)
  300. }
  301. fmt.Println(result)
  302. }
  303. bill.UpdateTime = time.Now()
  304. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, bill.Id.Hex(), &bill)
  305. }
  306. // 删除单据
  307. func DelBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  308. billId := c.Param("id")
  309. if billId == "" {
  310. return nil, errors.New("id为空")
  311. }
  312. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, billId)
  313. }