Update profit-loss
This commit is contained in:
@@ -306,20 +306,8 @@ func (s *AnalyticsServiceImpl) validateProfitLossAnalyticsRequest(req *models.Pr
|
||||
return fmt.Errorf("organization_id is required")
|
||||
}
|
||||
|
||||
if req.DateFrom.IsZero() {
|
||||
return fmt.Errorf("date_from is required")
|
||||
}
|
||||
|
||||
if req.DateTo.IsZero() {
|
||||
return fmt.Errorf("date_to is required")
|
||||
}
|
||||
|
||||
if req.DateFrom.After(req.DateTo) {
|
||||
return fmt.Errorf("date_from cannot be after date_to")
|
||||
}
|
||||
|
||||
if req.GroupBy != "" && req.GroupBy != "hour" && req.GroupBy != "day" && req.GroupBy != "week" && req.GroupBy != "month" {
|
||||
return fmt.Errorf("invalid group_by value, must be one of: hour, day, week, month")
|
||||
if req.Date.IsZero() {
|
||||
return fmt.Errorf("date is required")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -113,7 +113,8 @@ func (s *ReportServiceImpl) GenerateDailyTransactionPDF(ctx context.Context, org
|
||||
end := day.Add(24*time.Hour - time.Nanosecond)
|
||||
|
||||
salesReq := &models.SalesAnalyticsRequest{OrganizationID: orgID, OutletID: &outID, DateFrom: start, DateTo: end, GroupBy: "day"}
|
||||
plReq := &models.ProfitLossAnalyticsRequest{OrganizationID: orgID, OutletID: &outID, DateFrom: start, DateTo: end, GroupBy: "day"}
|
||||
plReq := &models.ProfitLossAnalyticsRequest{OrganizationID: orgID, OutletID: &outID, Date: day}
|
||||
productReq := &models.ProductAnalyticsRequest{OrganizationID: orgID, OutletID: &outID, DateFrom: start, DateTo: end, Limit: 1000}
|
||||
|
||||
sales, err := s.analyticsService.GetSalesAnalytics(ctx, salesReq)
|
||||
if err != nil {
|
||||
@@ -123,6 +124,15 @@ func (s *ReportServiceImpl) GenerateDailyTransactionPDF(ctx context.Context, org
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("get profit/loss analytics: %w", err)
|
||||
}
|
||||
products, err := s.analyticsService.GetProductAnalytics(ctx, productReq)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("get product analytics: %w", err)
|
||||
}
|
||||
|
||||
totalOmset := getPLNominalByID(pl.MainSummary, "total_omset")
|
||||
hpp := getPLNominalByID(pl.MainSummary, "hpp")
|
||||
labaKotor := getPLNominalByID(pl.MainSummary, "laba_kotor")
|
||||
labaKotorPct := getPLPctByID(pl.MainSummary, "laba_kotor")
|
||||
|
||||
data := reportTemplateData{
|
||||
OrganizationName: org.Name,
|
||||
@@ -133,28 +143,28 @@ func (s *ReportServiceImpl) GenerateDailyTransactionPDF(ctx context.Context, org
|
||||
GeneratedBy: generatedBy,
|
||||
PrintTime: time.Now().Format("02/01/2006 15:04:05"),
|
||||
Summary: reportSummary{
|
||||
TotalTransactions: pl.Summary.TotalOrders,
|
||||
TotalTransactions: sales.Summary.TotalOrders,
|
||||
TotalItems: sales.Summary.TotalItems,
|
||||
GrossSales: formatCurrency(pl.Summary.TotalRevenue),
|
||||
Discount: formatCurrency(pl.Summary.TotalDiscount),
|
||||
Tax: formatCurrency(pl.Summary.TotalTax),
|
||||
GrossSales: formatCurrency(totalOmset),
|
||||
Discount: formatCurrency(sales.Summary.TotalDiscount),
|
||||
Tax: formatCurrency(sales.Summary.TotalTax),
|
||||
NetSales: formatCurrency(sales.Summary.NetSales),
|
||||
COGS: formatCurrency(pl.Summary.TotalCost),
|
||||
GrossProfit: formatCurrency(pl.Summary.GrossProfit),
|
||||
GrossMarginPercent: fmt.Sprintf("%.2f", pl.Summary.GrossProfitMargin),
|
||||
COGS: formatCurrency(hpp),
|
||||
GrossProfit: formatCurrency(labaKotor),
|
||||
GrossMarginPercent: fmt.Sprintf("%.2f", labaKotorPct),
|
||||
},
|
||||
}
|
||||
|
||||
items := make([]reportItem, 0, len(pl.ProductData))
|
||||
for _, p := range pl.ProductData {
|
||||
items := make([]reportItem, 0, len(products.Data))
|
||||
for _, p := range products.Data {
|
||||
items = append(items, reportItem{
|
||||
Name: p.ProductName,
|
||||
Quantity: p.QuantitySold,
|
||||
GrossSales: formatCurrency(p.Revenue),
|
||||
Discount: formatCurrency(0),
|
||||
NetSales: formatCurrency(p.Revenue),
|
||||
COGS: formatCurrency(p.Cost),
|
||||
GrossProfit: formatCurrency(p.GrossProfit),
|
||||
COGS: formatCurrency(p.StandardHppTotal),
|
||||
GrossProfit: formatCurrency(p.Revenue - p.StandardHppTotal),
|
||||
})
|
||||
}
|
||||
data.Items = items
|
||||
@@ -190,3 +200,21 @@ func (s *ReportServiceImpl) GenerateDailyTransactionPDF(ctx context.Context, org
|
||||
|
||||
return publicURL, fileName, nil
|
||||
}
|
||||
|
||||
func getPLNominalByID(rows []models.ProfitLossSummaryRow, id string) float64 {
|
||||
for _, row := range rows {
|
||||
if row.ID == id {
|
||||
return row.TodayNominal
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func getPLPctByID(rows []models.ProfitLossSummaryRow, id string) float64 {
|
||||
for _, row := range rows {
|
||||
if row.ID == id {
|
||||
return row.TodayPct
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user