137 lines
3.9 KiB
Go
137 lines
3.9 KiB
Go
package transformer
|
|
|
|
import (
|
|
"apskel-pos-be/internal/contract"
|
|
"apskel-pos-be/internal/models"
|
|
"apskel-pos-be/internal/util"
|
|
"time"
|
|
)
|
|
|
|
func StandardHPPContractToModel(req *contract.StandardHPPRequest) *models.StandardHPPRequest {
|
|
return &models.StandardHPPRequest{
|
|
OrganizationID: req.OrganizationID,
|
|
ProductID: req.ProductID,
|
|
CategoryID: req.CategoryID,
|
|
}
|
|
}
|
|
|
|
func StandardHPPModelToContract(resp *models.StandardHPPResponse) *contract.StandardHPPResponse {
|
|
if resp == nil {
|
|
return nil
|
|
}
|
|
|
|
products := make([]contract.StandardHPPProductData, len(resp.Products))
|
|
for i, p := range resp.Products {
|
|
products[i] = contract.StandardHPPProductData{
|
|
ProductID: p.ProductID,
|
|
ProductName: p.ProductName,
|
|
ProductSku: p.ProductSku,
|
|
CategoryID: p.CategoryID,
|
|
CategoryName: p.CategoryName,
|
|
SellingPrice: p.SellingPrice,
|
|
ProductCost: p.ProductCost,
|
|
StandardCost: p.StandardCost,
|
|
StandardHPPPercentage: p.StandardHPPPercentage,
|
|
HasRecipe: p.HasRecipe,
|
|
}
|
|
}
|
|
|
|
ingredients := make([]contract.StandardHPPIngredientData, len(resp.Ingredients))
|
|
for i, ing := range resp.Ingredients {
|
|
ingredients[i] = contract.StandardHPPIngredientData{
|
|
ProductID: ing.ProductID,
|
|
IngredientID: ing.IngredientID,
|
|
IngredientName: ing.IngredientName,
|
|
Quantity: ing.Quantity,
|
|
UnitName: ing.UnitName,
|
|
CostPerUnit: ing.CostPerUnit,
|
|
WastePercentage: ing.WastePercentage,
|
|
TotalCost: ing.TotalCost,
|
|
}
|
|
}
|
|
|
|
return &contract.StandardHPPResponse{
|
|
OrganizationID: resp.OrganizationID,
|
|
Summary: hppSummaryModelToContract(resp.Summary),
|
|
Products: products,
|
|
Ingredients: ingredients,
|
|
}
|
|
}
|
|
|
|
func RealHPPContractToModel(req *contract.RealHPPRequest) *models.RealHPPRequest {
|
|
var dateFrom, dateTo time.Time
|
|
|
|
if fromTime, toTime, err := util.ParseDateRangeToJakartaTime(req.DateFrom, req.DateTo); err == nil {
|
|
if fromTime != nil {
|
|
dateFrom = *fromTime
|
|
}
|
|
if toTime != nil {
|
|
dateTo = *toTime
|
|
}
|
|
}
|
|
|
|
return &models.RealHPPRequest{
|
|
OrganizationID: req.OrganizationID,
|
|
OutletID: req.OutletID,
|
|
ProductID: req.ProductID,
|
|
CategoryID: req.CategoryID,
|
|
DateFrom: dateFrom,
|
|
DateTo: dateTo,
|
|
GroupBy: req.GroupBy,
|
|
}
|
|
}
|
|
|
|
func RealHPPModelToContract(resp *models.RealHPPResponse) *contract.RealHPPResponse {
|
|
if resp == nil {
|
|
return nil
|
|
}
|
|
|
|
products := make([]contract.RealHPPProductData, len(resp.Products))
|
|
for i, p := range resp.Products {
|
|
products[i] = contract.RealHPPProductData{
|
|
ProductID: p.ProductID,
|
|
ProductName: p.ProductName,
|
|
ProductSku: p.ProductSku,
|
|
CategoryID: p.CategoryID,
|
|
CategoryName: p.CategoryName,
|
|
SellingPrice: p.SellingPrice,
|
|
RealTotalCost: p.RealTotalCost,
|
|
RealTotalRevenue: p.RealTotalRevenue,
|
|
RealHPPPercentage: p.RealHPPPercentage,
|
|
TotalQuantitySold: p.TotalQuantitySold,
|
|
TotalOrders: p.TotalOrders,
|
|
}
|
|
}
|
|
|
|
timeSeries := make([]contract.RealHPPTimeSeriesData, len(resp.TimeSeries))
|
|
for i, ts := range resp.TimeSeries {
|
|
timeSeries[i] = contract.RealHPPTimeSeriesData{
|
|
Date: ts.Date,
|
|
RealTotalCost: ts.RealTotalCost,
|
|
RealTotalRevenue: ts.RealTotalRevenue,
|
|
RealHPPPercentage: ts.RealHPPPercentage,
|
|
TotalOrders: ts.TotalOrders,
|
|
}
|
|
}
|
|
|
|
return &contract.RealHPPResponse{
|
|
OrganizationID: resp.OrganizationID,
|
|
OutletID: resp.OutletID,
|
|
DateFrom: resp.DateFrom,
|
|
DateTo: resp.DateTo,
|
|
GroupBy: resp.GroupBy,
|
|
Summary: hppSummaryModelToContract(resp.Summary),
|
|
Products: products,
|
|
TimeSeries: timeSeries,
|
|
}
|
|
}
|
|
|
|
func hppSummaryModelToContract(s models.HPPSummary) contract.HPPSummary {
|
|
return contract.HPPSummary{
|
|
AverageStandardHPP: s.AverageStandardHPP,
|
|
AverageRealHPP: s.AverageRealHPP,
|
|
HPPVariance: s.HPPVariance,
|
|
TotalProducts: s.TotalProducts,
|
|
}
|
|
}
|