update
This commit is contained in:
@@ -7,11 +7,12 @@ import (
|
||||
"enaklo-pos-be/internal/constants"
|
||||
"enaklo-pos-be/internal/entity"
|
||||
"go.uber.org/zap"
|
||||
"math"
|
||||
)
|
||||
|
||||
func (s *orderSvc) CreateOrderInquiry(ctx mycontext.Context,
|
||||
req *entity.OrderRequest) (*entity.OrderInquiryResponse, error) {
|
||||
productIDs, filteredItems, err := s.validateOrderItems(ctx, req.OrderItems)
|
||||
productIDs, filteredItems, err := s.ValidateOrderItems(ctx, req.OrderItems)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -23,7 +24,7 @@ func (s *orderSvc) CreateOrderInquiry(ctx mycontext.Context,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
orderCalculation, err := s.calculateOrderTotals(ctx, req.OrderItems, productDetails, req.Source)
|
||||
orderCalculation, err := s.CalculateOrderTotals(ctx, req.OrderItems, productDetails, req.Source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -43,7 +44,7 @@ func (s *orderSvc) CreateOrderInquiry(ctx mycontext.Context,
|
||||
req.PartnerID,
|
||||
customerID,
|
||||
orderCalculation.Subtotal,
|
||||
orderCalculation.Fee,
|
||||
orderCalculation.Tax,
|
||||
orderCalculation.Total,
|
||||
req.PaymentMethod,
|
||||
req.Source,
|
||||
@@ -79,7 +80,7 @@ func (s *orderSvc) CreateOrderInquiry(ctx mycontext.Context,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *orderSvc) validateOrderItems(ctx mycontext.Context, items []entity.OrderItemRequest) ([]int64, []entity.OrderItemRequest, error) {
|
||||
func (s *orderSvc) ValidateOrderItems(ctx mycontext.Context, items []entity.OrderItemRequest) ([]int64, []entity.OrderItemRequest, error) {
|
||||
var productIDs []int64
|
||||
var filteredItems []entity.OrderItemRequest
|
||||
|
||||
@@ -98,7 +99,7 @@ func (s *orderSvc) validateOrderItems(ctx mycontext.Context, items []entity.Orde
|
||||
return productIDs, filteredItems, nil
|
||||
}
|
||||
|
||||
func (s *orderSvc) calculateOrderTotals(
|
||||
func (s *orderSvc) CalculateOrderTotals(
|
||||
ctx mycontext.Context,
|
||||
items []entity.OrderItemRequest,
|
||||
productDetails *entity.ProductDetails,
|
||||
@@ -114,12 +115,23 @@ func (s *orderSvc) calculateOrderTotals(
|
||||
subtotal += product.Price * float64(item.Quantity)
|
||||
}
|
||||
|
||||
fee := s.cfg.GetOrderFee(source)
|
||||
partnerID := ctx.GetPartnerID()
|
||||
setting, err := s.partnerSetting.GetSettings(ctx, *partnerID)
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.NewError(errors.ErrorInvalidRequest.ErrorType(), "failed to get partner settings")
|
||||
}
|
||||
|
||||
tax := 0.0
|
||||
if setting.TaxEnabled {
|
||||
tax = (setting.TaxPercentage / 100) * subtotal
|
||||
tax = math.Round(tax/100) * 100
|
||||
}
|
||||
|
||||
return &entity.OrderCalculation{
|
||||
Subtotal: subtotal,
|
||||
Fee: fee,
|
||||
Total: subtotal + fee,
|
||||
Tax: tax,
|
||||
Total: subtotal + tax,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -145,3 +157,79 @@ func (s *orderSvc) validateInquiry(ctx mycontext.Context, token string) (*entity
|
||||
|
||||
return inquiry, nil
|
||||
}
|
||||
|
||||
func (s *orderSvc) GetOrderPaymentAnalysis(
|
||||
ctx mycontext.Context,
|
||||
partnerID int64,
|
||||
req entity.SearchRequest,
|
||||
) (*entity.OrderPaymentAnalysis, error) {
|
||||
paymentBreakdown, err := s.repo.GetOrderPaymentMethodBreakdown(ctx, partnerID, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var totalAmount float64
|
||||
var totalTransactions int64
|
||||
|
||||
for _, breakdown := range paymentBreakdown {
|
||||
totalAmount += breakdown.TotalAmount
|
||||
totalTransactions += breakdown.TotalTransactions
|
||||
}
|
||||
|
||||
return &entity.OrderPaymentAnalysis{
|
||||
TotalAmount: totalAmount,
|
||||
TotalTransactions: totalTransactions,
|
||||
PaymentMethodBreakdown: paymentBreakdown,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *orderSvc) GetRevenueOverview(
|
||||
ctx mycontext.Context,
|
||||
partnerID int64,
|
||||
year int,
|
||||
granularity string,
|
||||
status string,
|
||||
) ([]entity.RevenueOverviewItem, error) {
|
||||
req := entity.RevenueOverviewRequest{
|
||||
PartnerID: partnerID,
|
||||
Year: year,
|
||||
Granularity: granularity,
|
||||
Status: status,
|
||||
}
|
||||
|
||||
return s.repo.GetRevenueOverview(ctx, req)
|
||||
}
|
||||
|
||||
func (s *orderSvc) GetSalesByCategory(
|
||||
ctx mycontext.Context,
|
||||
partnerID int64,
|
||||
period string,
|
||||
status string,
|
||||
) ([]entity.SalesByCategoryItem, error) {
|
||||
req := entity.SalesByCategoryRequest{
|
||||
PartnerID: partnerID,
|
||||
Period: period,
|
||||
Status: status,
|
||||
}
|
||||
|
||||
return s.repo.GetSalesByCategory(ctx, req)
|
||||
}
|
||||
|
||||
func (s *orderSvc) GetPopularProducts(
|
||||
ctx mycontext.Context,
|
||||
partnerID int64,
|
||||
period string,
|
||||
status string,
|
||||
limit int,
|
||||
sortBy string,
|
||||
) ([]entity.PopularProductItem, error) {
|
||||
req := entity.PopularProductsRequest{
|
||||
PartnerID: partnerID,
|
||||
Period: period,
|
||||
Status: status,
|
||||
Limit: limit,
|
||||
SortBy: sortBy,
|
||||
}
|
||||
|
||||
return s.repo.GetPopularProducts(ctx, req)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user