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)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func (s *orderSvc) ExecuteOrderInquiry(ctx mycontext.Context,
|
||||
token string, paymentMethod, paymentProvider, inprogressOrderID string) (*entity.OrderResponse, error) {
|
||||
token string, paymentMethod, paymentProvider string, inprogressOrderID int64) (*entity.OrderResponse, error) {
|
||||
inquiry, err := s.validateInquiry(ctx, token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -12,6 +12,24 @@ type Repository interface {
|
||||
CreateInquiry(ctx mycontext.Context, inquiry *entity.OrderInquiry) (*entity.OrderInquiry, error)
|
||||
FindInquiryByID(ctx mycontext.Context, id string) (*entity.OrderInquiry, error)
|
||||
UpdateInquiryStatus(ctx mycontext.Context, id string, status string) error
|
||||
GetOrderHistoryByPartnerID(ctx mycontext.Context, partnerID int64, req entity.SearchRequest) ([]*entity.Order, int64, error)
|
||||
GetOrderPaymentMethodBreakdown(
|
||||
ctx mycontext.Context,
|
||||
partnerID int64,
|
||||
req entity.SearchRequest,
|
||||
) ([]entity.PaymentMethodBreakdown, error)
|
||||
GetRevenueOverview(
|
||||
ctx mycontext.Context,
|
||||
req entity.RevenueOverviewRequest,
|
||||
) ([]entity.RevenueOverviewItem, error)
|
||||
GetSalesByCategory(
|
||||
ctx mycontext.Context,
|
||||
req entity.SalesByCategoryRequest,
|
||||
) ([]entity.SalesByCategoryItem, error)
|
||||
GetPopularProducts(
|
||||
ctx mycontext.Context,
|
||||
req entity.PopularProductsRequest,
|
||||
) ([]entity.PopularProductItem, error)
|
||||
}
|
||||
|
||||
type ProductService interface {
|
||||
@@ -42,21 +60,65 @@ type Service interface {
|
||||
CreateOrderInquiry(ctx mycontext.Context,
|
||||
req *entity.OrderRequest) (*entity.OrderInquiryResponse, error)
|
||||
ExecuteOrderInquiry(ctx mycontext.Context,
|
||||
token string, paymentMethod, paymentProvider, inProgressOrderID string) (*entity.OrderResponse, error)
|
||||
token string, paymentMethod, paymentProvider string, inProgressOrderID int64) (*entity.OrderResponse, error)
|
||||
GetOrderHistory(ctx mycontext.Context, partnerID int64, request entity.SearchRequest) ([]*entity.Order, int64, error)
|
||||
CalculateOrderTotals(
|
||||
ctx mycontext.Context,
|
||||
items []entity.OrderItemRequest,
|
||||
productDetails *entity.ProductDetails,
|
||||
source string,
|
||||
) (*entity.OrderCalculation, error)
|
||||
ValidateOrderItems(ctx mycontext.Context, items []entity.OrderItemRequest) ([]int64, []entity.OrderItemRequest, error)
|
||||
GetOrderPaymentAnalysis(
|
||||
ctx mycontext.Context,
|
||||
partnerID int64,
|
||||
req entity.SearchRequest,
|
||||
) (*entity.OrderPaymentAnalysis, error)
|
||||
GetRevenueOverview(
|
||||
ctx mycontext.Context,
|
||||
partnerID int64,
|
||||
year int,
|
||||
granularity string,
|
||||
status string,
|
||||
) ([]entity.RevenueOverviewItem, error)
|
||||
GetSalesByCategory(
|
||||
ctx mycontext.Context,
|
||||
partnerID int64,
|
||||
period string,
|
||||
status string,
|
||||
) ([]entity.SalesByCategoryItem, error)
|
||||
GetPopularProducts(
|
||||
ctx mycontext.Context,
|
||||
partnerID int64,
|
||||
period string,
|
||||
status string,
|
||||
limit int,
|
||||
sortBy string,
|
||||
) ([]entity.PopularProductItem, error)
|
||||
}
|
||||
|
||||
type Config interface {
|
||||
GetOrderFee(source string) float64
|
||||
}
|
||||
|
||||
type PartnerSettings interface {
|
||||
GetSettings(ctx mycontext.Context, partnerID int64) (*entity.PartnerSettings, error)
|
||||
}
|
||||
|
||||
type InProgressOrderRepository interface {
|
||||
GetListByPartnerID(ctx mycontext.Context, partnerID int64, limit, offset int) ([]*entity.InProgressOrder, error)
|
||||
}
|
||||
|
||||
type orderSvc struct {
|
||||
repo Repository
|
||||
product ProductService
|
||||
customer CustomerService
|
||||
transaction TransactionService
|
||||
crypt CryptService
|
||||
cfg Config
|
||||
notification NotificationService
|
||||
repo Repository
|
||||
product ProductService
|
||||
customer CustomerService
|
||||
transaction TransactionService
|
||||
crypt CryptService
|
||||
cfg Config
|
||||
notification NotificationService
|
||||
partnerSetting PartnerSettings
|
||||
inprogressOrder InProgressOrderRepository
|
||||
}
|
||||
|
||||
func New(
|
||||
@@ -67,14 +129,16 @@ func New(
|
||||
crypt CryptService,
|
||||
cfg Config,
|
||||
notification NotificationService,
|
||||
partnerSetting PartnerSettings,
|
||||
) Service {
|
||||
return &orderSvc{
|
||||
repo: repo,
|
||||
product: product,
|
||||
customer: customer,
|
||||
transaction: transaction,
|
||||
crypt: crypt,
|
||||
cfg: cfg,
|
||||
notification: notification,
|
||||
repo: repo,
|
||||
product: product,
|
||||
customer: customer,
|
||||
transaction: transaction,
|
||||
crypt: crypt,
|
||||
cfg: cfg,
|
||||
notification: notification,
|
||||
partnerSetting: partnerSetting,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package order
|
||||
|
||||
import (
|
||||
"enaklo-pos-be/internal/common/mycontext"
|
||||
"enaklo-pos-be/internal/entity"
|
||||
)
|
||||
|
||||
func (s *orderSvc) GetOrderHistory(ctx mycontext.Context, partnerID int64, request entity.SearchRequest) ([]*entity.Order, int64, error) {
|
||||
return s.repo.GetOrderHistoryByPartnerID(ctx, partnerID, request)
|
||||
}
|
||||
Reference in New Issue
Block a user