bill.go 9.0 KB

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