Add exclusive-summary
This commit is contained in:
@@ -559,3 +559,229 @@ func profitLossSummaryRowModelToContract(row models.ProfitLossSummaryRow) contra
|
||||
SubItems: subItems,
|
||||
}
|
||||
}
|
||||
|
||||
func ExclusiveSummaryPeriodContractToModel(req *contract.ExclusiveSummaryPeriodRequest) (*models.ExclusiveSummaryPeriodRequest, error) {
|
||||
if req == nil {
|
||||
return nil, fmt.Errorf("request cannot be nil")
|
||||
}
|
||||
|
||||
dateFrom, dateTo, err := parseFlexibleDateRangeToJakartaTime(req.DateFrom, req.DateTo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid date range: %w", err)
|
||||
}
|
||||
|
||||
if dateFrom == nil {
|
||||
return nil, fmt.Errorf("date_from is required")
|
||||
}
|
||||
|
||||
if dateTo == nil {
|
||||
return nil, fmt.Errorf("date_to is required")
|
||||
}
|
||||
|
||||
return &models.ExclusiveSummaryPeriodRequest{
|
||||
OrganizationID: req.OrganizationID,
|
||||
OutletID: parseOutletID(req.OutletID),
|
||||
DateFrom: *dateFrom,
|
||||
DateTo: *dateTo,
|
||||
ExcludeGajiStaffFromReimburse: req.ExcludeGajiStaffFromReimburse,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ExclusiveSummaryMonthlyContractToModel(req *contract.ExclusiveSummaryMonthlyRequest) (*models.ExclusiveSummaryMonthlyRequest, error) {
|
||||
if req == nil {
|
||||
return nil, fmt.Errorf("request cannot be nil")
|
||||
}
|
||||
|
||||
month, err := parseMonthToJakartaTime(req.Month)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid month: %w", err)
|
||||
}
|
||||
|
||||
return &models.ExclusiveSummaryMonthlyRequest{
|
||||
OrganizationID: req.OrganizationID,
|
||||
OutletID: parseOutletID(req.OutletID),
|
||||
Month: month,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ExclusiveSummaryPeriodModelToContract(resp *models.ExclusiveSummaryPeriodResponse) *contract.ExclusiveSummaryPeriodResponse {
|
||||
if resp == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
hppBreakdown := make([]contract.ExclusiveSummaryCategoryBreakdown, len(resp.HPPBreakdown))
|
||||
for i, item := range resp.HPPBreakdown {
|
||||
hppBreakdown[i] = exclusiveSummaryCategoryBreakdownModelToContract(item)
|
||||
}
|
||||
|
||||
operationalBreakdown := make([]contract.ExclusiveSummaryCategoryBreakdown, len(resp.OperationalExpenseBreakdown))
|
||||
for i, item := range resp.OperationalExpenseBreakdown {
|
||||
operationalBreakdown[i] = exclusiveSummaryCategoryBreakdownModelToContract(item)
|
||||
}
|
||||
|
||||
dailySummary := make([]contract.ExclusiveSummaryDailySummary, len(resp.DailySummary))
|
||||
for i, item := range resp.DailySummary {
|
||||
dailySummary[i] = contract.ExclusiveSummaryDailySummary{
|
||||
Date: item.Date,
|
||||
TransactionCount: item.TransactionCount,
|
||||
TotalCost: item.TotalCost,
|
||||
}
|
||||
}
|
||||
|
||||
dailyTransactions := make([]contract.ExclusiveSummaryDailyTransaction, len(resp.DailyTransactions))
|
||||
for i, item := range resp.DailyTransactions {
|
||||
dailyTransactions[i] = contract.ExclusiveSummaryDailyTransaction{
|
||||
Date: item.Date,
|
||||
CategoryCode: item.CategoryCode,
|
||||
CategoryName: item.CategoryName,
|
||||
Description: item.Description,
|
||||
Amount: item.Amount,
|
||||
Source: item.Source,
|
||||
}
|
||||
}
|
||||
|
||||
return &contract.ExclusiveSummaryPeriodResponse{
|
||||
OrganizationID: resp.OrganizationID,
|
||||
OutletID: resp.OutletID,
|
||||
Period: contract.ExclusiveSummaryPeriodRange{
|
||||
DateFrom: resp.Period.DateFrom,
|
||||
DateTo: resp.Period.DateTo,
|
||||
},
|
||||
Summary: contract.ExclusiveSummaryPeriodSummary{
|
||||
Sales: resp.Summary.Sales,
|
||||
HPP: resp.Summary.HPP,
|
||||
GrossProfit: resp.Summary.GrossProfit,
|
||||
SalaryTotal: resp.Summary.SalaryTotal,
|
||||
SalaryDW: resp.Summary.SalaryDW,
|
||||
SalaryStaff: resp.Summary.SalaryStaff,
|
||||
SalaryOther: resp.Summary.SalaryOther,
|
||||
OtherOperationalExpenses: resp.Summary.OtherOperationalExpenses,
|
||||
OperationalExpensesTotal: resp.Summary.OperationalExpensesTotal,
|
||||
TotalCost: resp.Summary.TotalCost,
|
||||
NetProfit: resp.Summary.NetProfit,
|
||||
},
|
||||
Reimburse: contract.ExclusiveSummaryReimburse{
|
||||
TotalCost: resp.Reimburse.TotalCost,
|
||||
ExcludedSalaryStaff: resp.Reimburse.ExcludedSalaryStaff,
|
||||
TotalReimburse: resp.Reimburse.TotalReimburse,
|
||||
},
|
||||
HPPBreakdown: hppBreakdown,
|
||||
OperationalExpenseBreakdown: operationalBreakdown,
|
||||
DailySummary: dailySummary,
|
||||
DailyTransactions: dailyTransactions,
|
||||
}
|
||||
}
|
||||
|
||||
func ExclusiveSummaryMonthlyModelToContract(resp *models.ExclusiveSummaryMonthlyResponse) *contract.ExclusiveSummaryMonthlyResponse {
|
||||
if resp == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
periods := make([]contract.ExclusiveSummaryMonthlyPeriod, len(resp.Periods))
|
||||
for i, item := range resp.Periods {
|
||||
periods[i] = contract.ExclusiveSummaryMonthlyPeriod{
|
||||
Label: item.Label,
|
||||
DateFrom: item.DateFrom,
|
||||
DateTo: item.DateTo,
|
||||
Sales: item.Sales,
|
||||
HPP: item.HPP,
|
||||
GrossProfit: item.GrossProfit,
|
||||
GrossMargin: item.GrossMargin,
|
||||
}
|
||||
}
|
||||
|
||||
bankBalance := make([]contract.ExclusiveSummaryBankBalance, len(resp.BankBalance))
|
||||
for i, item := range resp.BankBalance {
|
||||
bankBalance[i] = contract.ExclusiveSummaryBankBalance{
|
||||
Bank: item.Bank,
|
||||
OpeningBalance: item.OpeningBalance,
|
||||
IncomingMutation: item.IncomingMutation,
|
||||
OutgoingMutation: item.OutgoingMutation,
|
||||
ClosingBalance: item.ClosingBalance,
|
||||
Notes: item.Notes,
|
||||
}
|
||||
}
|
||||
|
||||
return &contract.ExclusiveSummaryMonthlyResponse{
|
||||
OrganizationID: resp.OrganizationID,
|
||||
OutletID: resp.OutletID,
|
||||
Month: resp.Month,
|
||||
Summary: contract.ExclusiveSummaryMonthlySummary{
|
||||
TotalSales: resp.Summary.TotalSales,
|
||||
HPP: resp.Summary.HPP,
|
||||
GrossProfit: resp.Summary.GrossProfit,
|
||||
OperationalExpensesTotal: resp.Summary.OperationalExpensesTotal,
|
||||
TotalCost: resp.Summary.TotalCost,
|
||||
NetProfit: resp.Summary.NetProfit,
|
||||
NetProfitMargin: resp.Summary.NetProfitMargin,
|
||||
},
|
||||
Periods: periods,
|
||||
BankBalance: bankBalance,
|
||||
}
|
||||
}
|
||||
|
||||
func exclusiveSummaryCategoryBreakdownModelToContract(item models.ExclusiveSummaryCategoryBreakdown) contract.ExclusiveSummaryCategoryBreakdown {
|
||||
return contract.ExclusiveSummaryCategoryBreakdown{
|
||||
CategoryCode: item.CategoryCode,
|
||||
CategoryName: item.CategoryName,
|
||||
Amount: item.Amount,
|
||||
Percentage: item.Percentage,
|
||||
}
|
||||
}
|
||||
|
||||
func parseFlexibleDateRangeToJakartaTime(dateFrom, dateTo string) (*time.Time, *time.Time, error) {
|
||||
fromTime, toTime, err := util.ParseDateRangeToJakartaTime(dateFrom, dateTo)
|
||||
if err == nil {
|
||||
return fromTime, toTime, nil
|
||||
}
|
||||
|
||||
fromTime, err = parseISODateToJakartaTime(dateFrom, false)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
toTime, err = parseISODateToJakartaTime(dateTo, true)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return fromTime, toTime, nil
|
||||
}
|
||||
|
||||
func parseISODateToJakartaTime(dateStr string, endOfDay bool) (*time.Time, error) {
|
||||
if dateStr == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
date, err := time.Parse("2006-01-02", dateStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
location, err := time.LoadLocation("Asia/Jakarta")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if endOfDay {
|
||||
result := time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 999999999, location)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
result := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, location)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func parseMonthToJakartaTime(month string) (time.Time, error) {
|
||||
location, err := time.LoadLocation("Asia/Jakarta")
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
|
||||
parsed, err := time.ParseInLocation("2006-01", month, location)
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
|
||||
return time.Date(parsed.Year(), parsed.Month(), 1, 0, 0, 0, 0, location), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user