add product ingredients
This commit is contained in:
@@ -166,3 +166,110 @@ func ModelToContractAccountResponse(resp *models.AccountResponse) *contract.Acco
|
||||
|
||||
return contractResp
|
||||
}
|
||||
|
||||
// Order Ingredient Transaction mappers
|
||||
func ContractToModelCreateOrderIngredientTransactionRequest(req *contract.CreateOrderIngredientTransactionRequest) *models.CreateOrderIngredientTransactionRequest {
|
||||
return &models.CreateOrderIngredientTransactionRequest{
|
||||
OrderID: req.OrderID,
|
||||
OrderItemID: req.OrderItemID,
|
||||
ProductID: req.ProductID,
|
||||
ProductVariantID: req.ProductVariantID,
|
||||
IngredientID: req.IngredientID,
|
||||
GrossQty: req.GrossQty,
|
||||
NetQty: req.NetQty,
|
||||
WasteQty: req.WasteQty,
|
||||
Unit: req.Unit,
|
||||
TransactionDate: req.TransactionDate,
|
||||
}
|
||||
}
|
||||
|
||||
func ContractToModelUpdateOrderIngredientTransactionRequest(req *contract.UpdateOrderIngredientTransactionRequest) *models.UpdateOrderIngredientTransactionRequest {
|
||||
return &models.UpdateOrderIngredientTransactionRequest{
|
||||
GrossQty: req.GrossQty,
|
||||
NetQty: req.NetQty,
|
||||
WasteQty: req.WasteQty,
|
||||
Unit: req.Unit,
|
||||
TransactionDate: req.TransactionDate,
|
||||
}
|
||||
}
|
||||
|
||||
func ModelToContractOrderIngredientTransactionResponse(resp *models.OrderIngredientTransactionResponse) *contract.OrderIngredientTransactionResponse {
|
||||
return &contract.OrderIngredientTransactionResponse{
|
||||
ID: resp.ID,
|
||||
OrganizationID: resp.OrganizationID,
|
||||
OutletID: resp.OutletID,
|
||||
OrderID: resp.OrderID,
|
||||
OrderItemID: resp.OrderItemID,
|
||||
ProductID: resp.ProductID,
|
||||
ProductVariantID: resp.ProductVariantID,
|
||||
IngredientID: resp.IngredientID,
|
||||
GrossQty: resp.GrossQty,
|
||||
NetQty: resp.NetQty,
|
||||
WasteQty: resp.WasteQty,
|
||||
Unit: resp.Unit,
|
||||
TransactionDate: resp.TransactionDate,
|
||||
CreatedBy: resp.CreatedBy,
|
||||
CreatedAt: resp.CreatedAt,
|
||||
UpdatedAt: resp.UpdatedAt,
|
||||
Organization: resp.Organization,
|
||||
Outlet: resp.Outlet,
|
||||
Order: resp.Order,
|
||||
OrderItem: resp.OrderItem,
|
||||
Product: resp.Product,
|
||||
ProductVariant: resp.ProductVariant,
|
||||
Ingredient: resp.Ingredient,
|
||||
CreatedByUser: resp.CreatedByUser,
|
||||
}
|
||||
}
|
||||
|
||||
func ModelToContractOrderIngredientTransactionResponses(responses []*models.OrderIngredientTransactionResponse) []*contract.OrderIngredientTransactionResponse {
|
||||
if responses == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
contractResponses := make([]*contract.OrderIngredientTransactionResponse, len(responses))
|
||||
for i, resp := range responses {
|
||||
contractResponses[i] = ModelToContractOrderIngredientTransactionResponse(resp)
|
||||
}
|
||||
|
||||
return contractResponses
|
||||
}
|
||||
|
||||
func ContractToModelListOrderIngredientTransactionsRequest(req *contract.ListOrderIngredientTransactionsRequest) *models.ListOrderIngredientTransactionsRequest {
|
||||
return &models.ListOrderIngredientTransactionsRequest{
|
||||
OrderID: req.OrderID,
|
||||
OrderItemID: req.OrderItemID,
|
||||
ProductID: req.ProductID,
|
||||
ProductVariantID: req.ProductVariantID,
|
||||
IngredientID: req.IngredientID,
|
||||
StartDate: req.StartDate,
|
||||
EndDate: req.EndDate,
|
||||
Page: req.Page,
|
||||
Limit: req.Limit,
|
||||
}
|
||||
}
|
||||
|
||||
func ModelToContractOrderIngredientTransactionSummary(resp *models.OrderIngredientTransactionSummary) *contract.OrderIngredientTransactionSummary {
|
||||
return &contract.OrderIngredientTransactionSummary{
|
||||
IngredientID: resp.IngredientID,
|
||||
IngredientName: resp.IngredientName,
|
||||
TotalGrossQty: resp.TotalGrossQty,
|
||||
TotalNetQty: resp.TotalNetQty,
|
||||
TotalWasteQty: resp.TotalWasteQty,
|
||||
WastePercentage: resp.WastePercentage,
|
||||
Unit: resp.Unit,
|
||||
}
|
||||
}
|
||||
|
||||
func ModelToContractOrderIngredientTransactionSummaries(responses []*models.OrderIngredientTransactionSummary) []*contract.OrderIngredientTransactionSummary {
|
||||
if responses == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
contractResponses := make([]*contract.OrderIngredientTransactionSummary, len(responses))
|
||||
for i, resp := range responses {
|
||||
contractResponses[i] = ModelToContractOrderIngredientTransactionSummary(resp)
|
||||
}
|
||||
|
||||
return contractResponses
|
||||
}
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
package mappers
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/entities"
|
||||
"apskel-pos-be/internal/models"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func MapOrderIngredientTransactionEntityToModel(entity *entities.OrderIngredientTransaction) *models.OrderIngredientTransaction {
|
||||
if entity == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &models.OrderIngredientTransaction{
|
||||
ID: entity.ID,
|
||||
OrganizationID: entity.OrganizationID,
|
||||
OutletID: entity.OutletID,
|
||||
OrderID: entity.OrderID,
|
||||
OrderItemID: entity.OrderItemID,
|
||||
ProductID: entity.ProductID,
|
||||
ProductVariantID: entity.ProductVariantID,
|
||||
IngredientID: entity.IngredientID,
|
||||
GrossQty: entity.GrossQty,
|
||||
NetQty: entity.NetQty,
|
||||
WasteQty: entity.WasteQty,
|
||||
Unit: entity.Unit,
|
||||
TransactionDate: entity.TransactionDate,
|
||||
CreatedBy: entity.CreatedBy,
|
||||
CreatedAt: entity.CreatedAt,
|
||||
UpdatedAt: entity.UpdatedAt,
|
||||
Organization: nil,
|
||||
Outlet: nil,
|
||||
Order: nil,
|
||||
OrderItem: nil,
|
||||
Product: nil,
|
||||
ProductVariant: nil,
|
||||
Ingredient: nil,
|
||||
CreatedByUser: nil,
|
||||
}
|
||||
}
|
||||
|
||||
func MapOrderIngredientTransactionModelToEntity(model *models.OrderIngredientTransaction) *entities.OrderIngredientTransaction {
|
||||
if model == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &entities.OrderIngredientTransaction{
|
||||
ID: model.ID,
|
||||
OrganizationID: model.OrganizationID,
|
||||
OutletID: model.OutletID,
|
||||
OrderID: model.OrderID,
|
||||
OrderItemID: model.OrderItemID,
|
||||
ProductID: model.ProductID,
|
||||
ProductVariantID: model.ProductVariantID,
|
||||
IngredientID: model.IngredientID,
|
||||
GrossQty: model.GrossQty,
|
||||
NetQty: model.NetQty,
|
||||
WasteQty: model.WasteQty,
|
||||
Unit: model.Unit,
|
||||
TransactionDate: model.TransactionDate,
|
||||
CreatedBy: model.CreatedBy,
|
||||
CreatedAt: model.CreatedAt,
|
||||
UpdatedAt: model.UpdatedAt,
|
||||
Organization: entities.Organization{},
|
||||
Outlet: nil,
|
||||
Order: entities.Order{},
|
||||
OrderItem: nil,
|
||||
Product: entities.Product{},
|
||||
ProductVariant: nil,
|
||||
Ingredient: entities.Ingredient{},
|
||||
CreatedByUser: entities.User{},
|
||||
}
|
||||
}
|
||||
|
||||
func MapOrderIngredientTransactionEntitiesToModels(entities []*entities.OrderIngredientTransaction) []*models.OrderIngredientTransaction {
|
||||
if entities == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
models := make([]*models.OrderIngredientTransaction, len(entities))
|
||||
for i, entity := range entities {
|
||||
models[i] = MapOrderIngredientTransactionEntityToModel(entity)
|
||||
}
|
||||
|
||||
return models
|
||||
}
|
||||
|
||||
func MapOrderIngredientTransactionModelsToEntities(models []*models.OrderIngredientTransaction) []*entities.OrderIngredientTransaction {
|
||||
if models == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
entities := make([]*entities.OrderIngredientTransaction, len(models))
|
||||
for i, model := range models {
|
||||
entities[i] = MapOrderIngredientTransactionModelToEntity(model)
|
||||
}
|
||||
|
||||
return entities
|
||||
}
|
||||
|
||||
func MapOrderIngredientTransactionEntityToResponse(entity *entities.OrderIngredientTransaction) *models.OrderIngredientTransactionResponse {
|
||||
if entity == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &models.OrderIngredientTransactionResponse{
|
||||
ID: entity.ID,
|
||||
OrganizationID: entity.OrganizationID,
|
||||
OutletID: entity.OutletID,
|
||||
OrderID: entity.OrderID,
|
||||
OrderItemID: entity.OrderItemID,
|
||||
ProductID: entity.ProductID,
|
||||
ProductVariantID: entity.ProductVariantID,
|
||||
IngredientID: entity.IngredientID,
|
||||
GrossQty: entity.GrossQty,
|
||||
NetQty: entity.NetQty,
|
||||
WasteQty: entity.WasteQty,
|
||||
Unit: entity.Unit,
|
||||
TransactionDate: entity.TransactionDate,
|
||||
CreatedBy: entity.CreatedBy,
|
||||
CreatedAt: entity.CreatedAt,
|
||||
UpdatedAt: entity.UpdatedAt,
|
||||
Organization: nil,
|
||||
Outlet: nil,
|
||||
Order: nil,
|
||||
OrderItem: nil,
|
||||
Product: nil,
|
||||
ProductVariant: nil,
|
||||
Ingredient: nil,
|
||||
CreatedByUser: nil,
|
||||
}
|
||||
}
|
||||
|
||||
func MapOrderIngredientTransactionEntitiesToResponses(entities []*entities.OrderIngredientTransaction) []*models.OrderIngredientTransactionResponse {
|
||||
if entities == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
responses := make([]*models.OrderIngredientTransactionResponse, len(entities))
|
||||
for i, entity := range entities {
|
||||
responses[i] = MapOrderIngredientTransactionEntityToResponse(entity)
|
||||
}
|
||||
|
||||
return responses
|
||||
}
|
||||
|
||||
func MapOrderIngredientTransactionSummary(transactions []*entities.OrderIngredientTransaction) []*models.OrderIngredientTransactionSummary {
|
||||
if transactions == nil || len(transactions) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Group by ingredient ID
|
||||
ingredientMap := make(map[uuid.UUID]*models.OrderIngredientTransactionSummary)
|
||||
|
||||
for _, transaction := range transactions {
|
||||
ingredientID := transaction.IngredientID
|
||||
|
||||
if summary, exists := ingredientMap[ingredientID]; exists {
|
||||
summary.TotalGrossQty += transaction.GrossQty
|
||||
summary.TotalNetQty += transaction.NetQty
|
||||
summary.TotalWasteQty += transaction.WasteQty
|
||||
} else {
|
||||
ingredientMap[ingredientID] = &models.OrderIngredientTransactionSummary{
|
||||
IngredientID: ingredientID,
|
||||
IngredientName: transaction.Ingredient.Name,
|
||||
TotalGrossQty: transaction.GrossQty,
|
||||
TotalNetQty: transaction.NetQty,
|
||||
TotalWasteQty: transaction.WasteQty,
|
||||
Unit: transaction.Unit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert map to slice and calculate waste percentage
|
||||
var summaries []*models.OrderIngredientTransactionSummary
|
||||
for _, summary := range ingredientMap {
|
||||
if summary.TotalGrossQty > 0 {
|
||||
summary.WastePercentage = (summary.TotalWasteQty / summary.TotalGrossQty) * 100
|
||||
}
|
||||
summaries = append(summaries, summary)
|
||||
}
|
||||
|
||||
return summaries
|
||||
}
|
||||
@@ -11,16 +11,17 @@ func MapProductIngredientEntityToModel(entity *entities.ProductIngredient) *mode
|
||||
}
|
||||
|
||||
return &models.ProductIngredient{
|
||||
ID: entity.ID,
|
||||
OrganizationID: entity.OrganizationID,
|
||||
OutletID: entity.OutletID,
|
||||
ProductID: entity.ProductID,
|
||||
IngredientID: entity.IngredientID,
|
||||
Quantity: entity.Quantity,
|
||||
CreatedAt: entity.CreatedAt,
|
||||
UpdatedAt: entity.UpdatedAt,
|
||||
Product: ProductEntityToModel(entity.Product),
|
||||
Ingredient: MapIngredientEntityToModel(entity.Ingredient),
|
||||
ID: entity.ID,
|
||||
OrganizationID: entity.OrganizationID,
|
||||
OutletID: entity.OutletID,
|
||||
ProductID: entity.ProductID,
|
||||
IngredientID: entity.IngredientID,
|
||||
Quantity: entity.Quantity,
|
||||
WastePercentage: entity.WastePercentage,
|
||||
CreatedAt: entity.CreatedAt,
|
||||
UpdatedAt: entity.UpdatedAt,
|
||||
Product: ProductEntityToModel(entity.Product),
|
||||
Ingredient: MapIngredientEntityToModel(entity.Ingredient),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,16 +31,17 @@ func MapProductIngredientModelToEntity(model *models.ProductIngredient) *entitie
|
||||
}
|
||||
|
||||
return &entities.ProductIngredient{
|
||||
ID: model.ID,
|
||||
OrganizationID: model.OrganizationID,
|
||||
OutletID: model.OutletID,
|
||||
ProductID: model.ProductID,
|
||||
IngredientID: model.IngredientID,
|
||||
Quantity: model.Quantity,
|
||||
CreatedAt: model.CreatedAt,
|
||||
UpdatedAt: model.UpdatedAt,
|
||||
Product: ProductModelToEntity(model.Product),
|
||||
Ingredient: MapIngredientModelToEntity(model.Ingredient),
|
||||
ID: model.ID,
|
||||
OrganizationID: model.OrganizationID,
|
||||
OutletID: model.OutletID,
|
||||
ProductID: model.ProductID,
|
||||
IngredientID: model.IngredientID,
|
||||
Quantity: model.Quantity,
|
||||
WastePercentage: model.WastePercentage,
|
||||
CreatedAt: model.CreatedAt,
|
||||
UpdatedAt: model.UpdatedAt,
|
||||
Product: ProductModelToEntity(model.Product),
|
||||
Ingredient: MapIngredientModelToEntity(model.Ingredient),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user