reinstate profit loss overview
This commit is contained in:
@@ -17,7 +17,7 @@ type AnalyticsRepository interface {
|
||||
GetProductAnalytics(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, dateFrom, dateTo time.Time, limit int) ([]*entities.ProductAnalytics, error)
|
||||
GetProductAnalyticsPerCategory(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, dateFrom, dateTo time.Time) ([]*entities.ProductAnalyticsPerCategory, error)
|
||||
GetDashboardOverview(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, dateFrom, dateTo time.Time) (*entities.DashboardOverview, error)
|
||||
GetProfitLossAnalytics(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, dateFrom, dateTo time.Time) (*entities.ProfitLossAnalytics, error)
|
||||
GetProfitLossAnalytics(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, dateFrom, dateTo time.Time, groupBy string) (*entities.ProfitLossAnalytics, error)
|
||||
}
|
||||
|
||||
type AnalyticsRepositoryImpl struct {
|
||||
@@ -432,11 +432,138 @@ func (r *AnalyticsRepositoryImpl) GetDashboardOverview(ctx context.Context, orga
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (r *AnalyticsRepositoryImpl) GetProfitLossAnalytics(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, dateFrom, dateTo time.Time) (*entities.ProfitLossAnalytics, error) {
|
||||
func (r *AnalyticsRepositoryImpl) GetProfitLossAnalytics(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, dateFrom, dateTo time.Time, groupBy string) (*entities.ProfitLossAnalytics, error) {
|
||||
mtdStart := time.Date(dateTo.Year(), dateTo.Month(), 1, 0, 0, 0, 0, dateTo.Location())
|
||||
todayStart := time.Date(dateTo.Year(), dateTo.Month(), dateTo.Day(), 0, 0, 0, 0, dateTo.Location())
|
||||
todayEnd := todayStart.Add(24 * time.Hour).Add(-time.Nanosecond)
|
||||
|
||||
var summary entities.ProfitLossSummary
|
||||
summaryQuery := r.db.WithContext(ctx).
|
||||
Table("orders o").
|
||||
Select(`
|
||||
COALESCE(SUM(o.total_amount), 0) as total_revenue,
|
||||
COALESCE(SUM(o.total_cost), 0) as total_cost,
|
||||
COALESCE(SUM(o.total_amount - o.total_cost), 0) as gross_profit,
|
||||
CASE
|
||||
WHEN SUM(o.total_amount) > 0
|
||||
THEN (SUM(o.total_amount - o.total_cost) / SUM(o.total_amount)) * 100
|
||||
ELSE 0
|
||||
END as gross_profit_margin,
|
||||
COALESCE(SUM(o.tax_amount), 0) as total_tax,
|
||||
COALESCE(SUM(o.discount_amount), 0) as total_discount,
|
||||
COALESCE(SUM(o.total_amount - o.total_cost - o.discount_amount), 0) as net_profit,
|
||||
CASE
|
||||
WHEN SUM(o.total_amount) > 0
|
||||
THEN (SUM(o.total_amount - o.total_cost - o.discount_amount) / SUM(o.total_amount)) * 100
|
||||
ELSE 0
|
||||
END as net_profit_margin,
|
||||
COUNT(o.id) as total_orders,
|
||||
CASE
|
||||
WHEN COUNT(o.id) > 0
|
||||
THEN SUM(o.total_amount - o.total_cost - o.discount_amount) / COUNT(o.id)
|
||||
ELSE 0
|
||||
END as average_profit,
|
||||
CASE
|
||||
WHEN SUM(o.total_cost) > 0
|
||||
THEN (SUM(o.total_amount - o.total_cost) / SUM(o.total_cost)) * 100
|
||||
ELSE 0
|
||||
END as profitability_ratio
|
||||
`).
|
||||
Where("o.organization_id = ?", organizationID).
|
||||
Where("o.status = ?", entities.OrderStatusCompleted).
|
||||
Where("o.payment_status = ?", entities.PaymentStatusCompleted).
|
||||
Where("o.is_void = false AND o.is_refund = false").
|
||||
Where("o.created_at >= ? AND o.created_at <= ?", dateFrom, dateTo)
|
||||
summaryQuery = r.resolveOutletID(summaryQuery, outletID, "o.outlet_id")
|
||||
if err := summaryQuery.Scan(&summary).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var timeFormat string
|
||||
switch groupBy {
|
||||
case "hour":
|
||||
timeFormat = "DATE_TRUNC('hour', o.created_at)"
|
||||
case "week":
|
||||
timeFormat = "DATE_TRUNC('week', o.created_at)"
|
||||
case "month":
|
||||
timeFormat = "DATE_TRUNC('month', o.created_at)"
|
||||
default:
|
||||
timeFormat = "DATE_TRUNC('day', o.created_at)"
|
||||
}
|
||||
|
||||
var data []entities.ProfitLossData
|
||||
dataQuery := r.db.WithContext(ctx).
|
||||
Table("orders o").
|
||||
Select(`
|
||||
`+timeFormat+` as date,
|
||||
COALESCE(SUM(o.total_amount), 0) as revenue,
|
||||
COALESCE(SUM(o.total_cost), 0) as cost,
|
||||
COALESCE(SUM(o.total_amount - o.total_cost), 0) as gross_profit,
|
||||
CASE
|
||||
WHEN SUM(o.total_amount) > 0
|
||||
THEN (SUM(o.total_amount - o.total_cost) / SUM(o.total_amount)) * 100
|
||||
ELSE 0
|
||||
END as gross_profit_margin,
|
||||
COALESCE(SUM(o.tax_amount), 0) as tax,
|
||||
COALESCE(SUM(o.discount_amount), 0) as discount,
|
||||
COALESCE(SUM(o.total_amount - o.total_cost - o.discount_amount), 0) as net_profit,
|
||||
CASE
|
||||
WHEN SUM(o.total_amount) > 0
|
||||
THEN (SUM(o.total_amount - o.total_cost - o.discount_amount) / SUM(o.total_amount)) * 100
|
||||
ELSE 0
|
||||
END as net_profit_margin,
|
||||
COUNT(o.id) as orders
|
||||
`).
|
||||
Where("o.organization_id = ?", organizationID).
|
||||
Where("o.status = ?", entities.OrderStatusCompleted).
|
||||
Where("o.payment_status = ?", entities.PaymentStatusCompleted).
|
||||
Where("o.is_void = false AND o.is_refund = false").
|
||||
Where("o.created_at >= ? AND o.created_at <= ?", dateFrom, dateTo).
|
||||
Group(timeFormat).
|
||||
Order(timeFormat)
|
||||
dataQuery = r.resolveOutletID(dataQuery, outletID, "o.outlet_id")
|
||||
if err := dataQuery.Scan(&data).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var productData []entities.ProductProfitData
|
||||
productQuery := r.db.WithContext(ctx).
|
||||
Table("order_items oi").
|
||||
Select(`
|
||||
p.id as product_id,
|
||||
p.name as product_name,
|
||||
c.id as category_id,
|
||||
c.name as category_name,
|
||||
SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.quantity - COALESCE(oi.refund_quantity, 0) ELSE 0 END) as quantity_sold,
|
||||
SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.total_price - COALESCE(oi.refund_amount, 0) ELSE 0 END) as revenue,
|
||||
SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.total_cost * ((oi.quantity - COALESCE(oi.refund_quantity, 0))::float / NULLIF(oi.quantity, 0)) ELSE 0 END) as cost,
|
||||
SUM(CASE WHEN oi.is_fully_refunded = false THEN (oi.total_price - COALESCE(oi.refund_amount, 0)) - (oi.total_cost * ((oi.quantity - COALESCE(oi.refund_quantity, 0))::float / NULLIF(oi.quantity, 0))) ELSE 0 END) as gross_profit,
|
||||
CASE
|
||||
WHEN SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.total_price - COALESCE(oi.refund_amount, 0) ELSE 0 END) > 0
|
||||
THEN (SUM(CASE WHEN oi.is_fully_refunded = false THEN (oi.total_price - COALESCE(oi.refund_amount, 0)) - (oi.total_cost * ((oi.quantity - COALESCE(oi.refund_quantity, 0))::float / NULLIF(oi.quantity, 0))) ELSE 0 END) / SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.total_price - COALESCE(oi.refund_amount, 0) ELSE 0 END)) * 100
|
||||
ELSE 0
|
||||
END as gross_profit_margin,
|
||||
AVG(CASE WHEN oi.is_fully_refunded = false THEN oi.unit_price ELSE NULL END) as average_price,
|
||||
AVG(CASE WHEN oi.is_fully_refunded = false THEN oi.unit_cost ELSE NULL END) as average_cost,
|
||||
AVG(CASE WHEN oi.is_fully_refunded = false THEN oi.unit_price - oi.unit_cost ELSE NULL END) as profit_per_unit
|
||||
`).
|
||||
Joins("JOIN orders o ON oi.order_id = o.id").
|
||||
Joins("JOIN products p ON oi.product_id = p.id").
|
||||
Joins("JOIN categories c ON p.category_id = c.id").
|
||||
Where("o.organization_id = ?", organizationID).
|
||||
Where("o.status = ?", entities.OrderStatusCompleted).
|
||||
Where("o.payment_status = ?", entities.PaymentStatusCompleted).
|
||||
Where("o.is_void = false AND o.is_refund = false").
|
||||
Where("oi.status != ?", entities.OrderItemStatusCancelled).
|
||||
Where("o.created_at >= ? AND o.created_at <= ?", dateFrom, dateTo).
|
||||
Group("p.id, p.name, c.id, c.name").
|
||||
Order("p.name ASC").
|
||||
Limit(1000)
|
||||
productQuery = r.resolveOutletID(productQuery, outletID, "o.outlet_id")
|
||||
if err := productQuery.Scan(&productData).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
type revenueCostResult struct {
|
||||
Revenue float64
|
||||
Cost float64
|
||||
@@ -492,6 +619,9 @@ func (r *AnalyticsRepositoryImpl) GetProfitLossAnalytics(ctx context.Context, or
|
||||
}
|
||||
|
||||
return &entities.ProfitLossAnalytics{
|
||||
Summary: summary,
|
||||
Data: data,
|
||||
ProductData: productData,
|
||||
TodayRevenue: todayRC.Revenue,
|
||||
TodayCost: todayRC.Cost,
|
||||
MtdRevenue: mtdRC.Revenue,
|
||||
|
||||
Reference in New Issue
Block a user