Anousone Worlakoumman commited on
Commit
b164630
·
1 Parent(s): 71d6ca6

fix sell revenue

Browse files
internal/dashboard/dashboard.go CHANGED
@@ -3,6 +3,8 @@ package dashboard
3
  import (
4
  "log/slog"
5
  "net/http"
 
 
6
 
7
  "github.com/pocketbase/dbx"
8
  "github.com/pocketbase/pocketbase"
@@ -153,29 +155,70 @@ func (s *DashboardService) getSellAmounts() (SellObj, error) {
153
 
154
  func (s *DashboardService) SellRevenue(e *core.RequestEvent) error {
155
  type revenueResult struct {
156
- Year int `db:"year"`
157
- Month int `db:"month"`
158
  AmountLAK *float64 `db:"amount_lak"`
159
  AmountUSD *float64 `db:"amount_usd"`
160
  AmountTHB *float64 `db:"amount_thb"`
161
  }
162
 
163
- var results []revenueResult
164
- err := s.app.DB().
 
 
 
 
 
 
 
 
 
 
 
 
165
  Select(
166
- "strftime('%Y', oi.created) as year",
167
- "strftime('%m', oi.created) as month",
168
  "SUM(oi.quantity * oi.price_lak) as amount_lak",
169
  "SUM(oi.quantity * oi.price_usd) as amount_usd",
170
  "SUM(oi.quantity * oi.price_thb) as amount_thb",
171
  ).
172
  From("order_items oi").
173
  InnerJoin("orders o", dbx.NewExp("o.id = oi.order_id")).
174
- // Where(dbx.NewExp("o.status = {:status}", dbx.Params{"status": StatusCompleted})).
175
- Where(dbx.In("o.status", StatusPurchased, StatusDelivering, StatusCompleted)).
176
- GroupBy("strftime('%Y', oi.created)", "strftime('%m', oi.created)").
177
- OrderBy("year DESC", "month DESC").
178
- All(&results)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  if err != nil {
180
  slog.Error("db", "err", err)
181
  return e.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to get revenue data"})
@@ -184,8 +227,7 @@ func (s *DashboardService) SellRevenue(e *core.RequestEvent) error {
184
  sellRevenue := make([]SellRevenueObj, 0)
185
  for _, result := range results {
186
  obj := SellRevenueObj{
187
- Year: result.Year,
188
- Month: result.Month,
189
  }
190
 
191
  if result.AmountLAK != nil {
 
3
  import (
4
  "log/slog"
5
  "net/http"
6
+ "strconv"
7
+ "time"
8
 
9
  "github.com/pocketbase/dbx"
10
  "github.com/pocketbase/pocketbase"
 
155
 
156
  func (s *DashboardService) SellRevenue(e *core.RequestEvent) error {
157
  type revenueResult struct {
158
+ Period string `db:"period"`
 
159
  AmountLAK *float64 `db:"amount_lak"`
160
  AmountUSD *float64 `db:"amount_usd"`
161
  AmountTHB *float64 `db:"amount_thb"`
162
  }
163
 
164
+ isYearParam := e.Request.URL.Query().Get("isYear")
165
+ startDateParam := e.Request.URL.Query().Get("startDate")
166
+ endDateParam := e.Request.URL.Query().Get("endDate")
167
+
168
+ var isYear bool
169
+ if isYearParam != "" {
170
+ var err error
171
+ isYear, err = strconv.ParseBool(isYearParam)
172
+ if err != nil {
173
+ return e.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid isYear parameter"})
174
+ }
175
+ }
176
+
177
+ query := s.app.DB().
178
  Select(
179
+ "oi.created as period",
 
180
  "SUM(oi.quantity * oi.price_lak) as amount_lak",
181
  "SUM(oi.quantity * oi.price_usd) as amount_usd",
182
  "SUM(oi.quantity * oi.price_thb) as amount_thb",
183
  ).
184
  From("order_items oi").
185
  InnerJoin("orders o", dbx.NewExp("o.id = oi.order_id")).
186
+ Where(dbx.In("o.status", StatusPurchased, StatusDelivering, StatusCompleted))
187
+
188
+ currentTime := time.Now()
189
+
190
+ if startDateParam != "" && endDateParam != "" {
191
+ startDate, err := time.Parse("2006-01-02", startDateParam)
192
+ if err != nil {
193
+ return e.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid startDate format, use YYYY-MM-DD"})
194
+ }
195
+ endDate, err := time.Parse("2006-01-02", endDateParam)
196
+ if err != nil {
197
+ return e.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid endDate format, use YYYY-MM-DD"})
198
+ }
199
+
200
+ query = query.
201
+ Where(dbx.NewExp("DATE(oi.created) >= {:start_date}", dbx.Params{"start_date": startDate.Format("2006-01-02")})).
202
+ Where(dbx.NewExp("DATE(oi.created) <= {:end_date}", dbx.Params{"end_date": endDate.Format("2006-01-02")})).
203
+ GroupBy("DATE(oi.created)").
204
+ OrderBy("DATE(oi.created) DESC")
205
+ } else if isYear {
206
+ startYear := currentTime.Year() - 5
207
+ query = query.
208
+ Where(dbx.NewExp("strftime('%Y', oi.created) >= {:start_year}", dbx.Params{"start_year": strconv.Itoa(startYear)})).
209
+ GroupBy("strftime('%Y', oi.created)").
210
+ OrderBy("strftime('%Y', oi.created) DESC")
211
+ } else {
212
+ currentYear := currentTime.Year()
213
+ query = query.
214
+ Where(dbx.NewExp("strftime('%Y', oi.created) = {:current_year}", dbx.Params{"current_year": strconv.Itoa(currentYear)})).
215
+ Where(dbx.NewExp("strftime('%m', oi.created) <= {:current_month}", dbx.Params{"current_month": strconv.Itoa(int(currentTime.Month()))})).
216
+ GroupBy("strftime('%Y', oi.created)", "strftime('%m', oi.created)").
217
+ OrderBy("strftime('%Y', oi.created) DESC", "strftime('%m', oi.created) DESC")
218
+ }
219
+
220
+ var results []revenueResult
221
+ err := query.All(&results)
222
  if err != nil {
223
  slog.Error("db", "err", err)
224
  return e.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to get revenue data"})
 
227
  sellRevenue := make([]SellRevenueObj, 0)
228
  for _, result := range results {
229
  obj := SellRevenueObj{
230
+ Period: result.Period,
 
231
  }
232
 
233
  if result.AmountLAK != nil {
internal/dashboard/dto.go CHANGED
@@ -9,8 +9,7 @@ type Count struct {
9
  }
10
 
11
  type SellRevenueObj struct {
12
- Year int `json:"year"`
13
- Month int `json:"month"`
14
  AmountLAK decimal.Decimal `json:"amountLAK"`
15
  AmountTHB decimal.Decimal `json:"amountTHB"`
16
  AmountUSD decimal.Decimal `json:"amountUSD"`
 
9
  }
10
 
11
  type SellRevenueObj struct {
12
+ Period string `json:"period"`
 
13
  AmountLAK decimal.Decimal `json:"amountLAK"`
14
  AmountTHB decimal.Decimal `json:"amountTHB"`
15
  AmountUSD decimal.Decimal `json:"amountUSD"`