invoice_api_controller.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package controllers
  2. import (
  3. "Xcx_New/enums"
  4. "Xcx_New/models"
  5. "Xcx_New/service"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/astaxie/beego"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. type InvoiceApiController struct {
  15. BaseServeAPIController
  16. }
  17. func InvoiceApiRegistRouters() {
  18. beego.Router("/api/invoice/applies", &InvoiceApiController{}, "Get:GetApplies")
  19. beego.Router("/api/invoice/apply", &InvoiceApiController{}, "Post:CreateInvoice")
  20. beego.Router("/api/invoice/noinvorders", &InvoiceApiController{}, "Get:GetNoInvOrders")
  21. beego.Router("/api/invoices", &InvoiceApiController{}, "Get:GetInvoices")
  22. }
  23. func (c *InvoiceApiController) GetApplies() {
  24. orders := c.GetString("orders")
  25. if len(orders) == 0 {
  26. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  27. return
  28. }
  29. if match, _ := regexp.MatchString("^([0-9]+[,])*([0-9]+)$", orders); !match {
  30. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  31. return
  32. }
  33. adminUserInfo := c.GetAdminUserInfo()
  34. ordersIDSlipe := make([]int64, 0)
  35. ordersSlipe := strings.Split(orders, ",")
  36. for index := range ordersSlipe {
  37. orderid, _ := strconv.ParseInt(ordersSlipe[index], 10, 64)
  38. if orderid > 0 {
  39. ordersIDSlipe = append(ordersIDSlipe, orderid)
  40. }
  41. }
  42. if len(ordersIDSlipe) == 0 {
  43. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  44. return
  45. }
  46. has, nos, err := service.FindServeOrdersByIDs(adminUserInfo.CurrentOrgId, ordersIDSlipe)
  47. if err != nil {
  48. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  49. return
  50. }
  51. c.ServeSuccessJSON(map[string]interface{}{
  52. "has": has,
  53. "nos": nos,
  54. })
  55. }
  56. func (c *InvoiceApiController) CreateInvoice() {
  57. orders := c.GetString("orders")
  58. if len(orders) == 0 {
  59. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  60. return
  61. }
  62. if match, _ := regexp.MatchString("^([0-9]+[,])*([0-9]+)$", orders); !match {
  63. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  64. return
  65. }
  66. adminUserInfo := c.GetAdminUserInfo()
  67. ordersIDSlipe := make([]int64, 0)
  68. ordersSlipe := strings.Split(orders, ",")
  69. for index := range ordersSlipe {
  70. orderid, _ := strconv.ParseInt(ordersSlipe[index], 10, 64)
  71. if orderid > 0 {
  72. ordersIDSlipe = append(ordersIDSlipe, orderid)
  73. }
  74. }
  75. if len(ordersIDSlipe) == 0 {
  76. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  77. return
  78. }
  79. _, nos, err := service.FindServeOrdersByIDs(adminUserInfo.CurrentOrgId, ordersIDSlipe)
  80. if err != nil {
  81. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  82. return
  83. }
  84. if len(nos) == 0 {
  85. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeServeNotExist)
  86. return
  87. }
  88. var invoiceData models.ServeInvoice
  89. err = json.Unmarshal(c.Ctx.Input.RequestBody, &invoiceData)
  90. if err != nil {
  91. fmt.Println(err)
  92. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  93. return
  94. }
  95. inds := make([]string, 0)
  96. var inc float64
  97. for index := range nos {
  98. inds = append(inds, strconv.FormatInt(nos[index].ID, 10))
  99. inc += nos[index].PayableAmount
  100. }
  101. invoiceData.OrgId = adminUserInfo.CurrentOrgId
  102. invoiceData.Status = 1
  103. invoiceData.CreatedTime = time.Now().Unix()
  104. invoiceData.UpdatedTime = time.Now().Unix()
  105. invoiceData.InvoiceAmountGross = inc
  106. orders = "," + strings.Join(inds, ",") + ","
  107. invoiceData.Orders = orders
  108. invoiceData.InvoiceStatus = 1
  109. err = service.CreateInvoice(&invoiceData)
  110. if err != nil {
  111. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeApplyInvoiceFail)
  112. return
  113. }
  114. c.ServeSuccessJSON(map[string]interface{}{
  115. "invoice": invoiceData,
  116. })
  117. }
  118. func (c *InvoiceApiController) GetNoInvOrders() {
  119. adminUserInfo := c.GetAdminUserInfo()
  120. orders, err := service.FindUnInvoiceOrders(adminUserInfo.CurrentOrgId)
  121. if err != nil {
  122. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  123. return
  124. }
  125. c.ServeSuccessJSON(map[string]interface{}{
  126. "orders": orders,
  127. })
  128. }
  129. func (c *InvoiceApiController) GetInvoices() {
  130. adminUserInfo := c.GetAdminUserInfo()
  131. invoices, err := service.FindInvoices(adminUserInfo.CurrentOrgId)
  132. if err != nil {
  133. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  134. return
  135. }
  136. c.ServeSuccessJSON(map[string]interface{}{
  137. "invoices": invoices,
  138. })
  139. }