mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
[FIX/BE-US] feat adjustment location and area
This commit is contained in:
@@ -46,12 +46,13 @@ type RepportService interface {
|
||||
GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangResponseData, *dto.HppPerKandangMetaDTO, error)
|
||||
GetProductionResult(ctx *fiber.Ctx, params *validation.ProductionResultQuery) ([]dto.ProductionResultDTO, int64, error)
|
||||
GetCustomerPayment(ctx *fiber.Ctx, params *validation.CustomerPaymentQuery) ([]dto.CustomerPaymentReportItem, int64, error)
|
||||
DB() *gorm.DB
|
||||
}
|
||||
|
||||
type repportService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
DB *gorm.DB
|
||||
db *gorm.DB
|
||||
ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
|
||||
MarketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository
|
||||
PurchaseRepo purchaseRepo.PurchaseRepository
|
||||
@@ -100,7 +101,7 @@ func NewRepportService(
|
||||
return &repportService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
DB: db,
|
||||
db: db,
|
||||
ExpenseRealizationRepo: expenseRealizationRepo,
|
||||
MarketingDeliveryRepo: marketingDeliveryRepo,
|
||||
PurchaseRepo: purchaseRepo,
|
||||
@@ -119,6 +120,9 @@ func NewRepportService(
|
||||
}
|
||||
}
|
||||
|
||||
func (s *repportService) DB() *gorm.DB {
|
||||
return s.db
|
||||
}
|
||||
|
||||
func (s *repportService) GetExpense(c *fiber.Ctx, params *validation.ExpenseQuery) ([]dto.RepportExpenseListDTO, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
@@ -407,11 +411,38 @@ func (s *repportService) GetCustomerPayment(ctx *fiber.Ctx, params *validation.C
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
locationScope, err := m.ResolveLocationScope(ctx, s.DB())
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
areaScope, err := m.ResolveAreaScope(ctx, s.DB())
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
restrictScope := locationScope.Restrict || areaScope.Restrict
|
||||
var allowedCustomerIDs []uint
|
||||
if restrictScope {
|
||||
if (locationScope.Restrict && len(locationScope.IDs) == 0) || (areaScope.Restrict && len(areaScope.IDs) == 0) {
|
||||
return []dto.CustomerPaymentReportItem{}, 0, nil
|
||||
}
|
||||
allowedCustomerIDs, err = s.getCustomerIDsByScope(ctx.Context(), locationScope.IDs, areaScope.IDs)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if len(allowedCustomerIDs) == 0 {
|
||||
return []dto.CustomerPaymentReportItem{}, 0, nil
|
||||
}
|
||||
}
|
||||
|
||||
var customerIDs []uint
|
||||
var totalCustomers int64
|
||||
|
||||
if len(params.CustomerIDs) > 0 {
|
||||
customerIDs = params.CustomerIDs
|
||||
if restrictScope {
|
||||
customerIDs = intersectUint(customerIDs, allowedCustomerIDs)
|
||||
}
|
||||
totalCustomers = int64(len(customerIDs))
|
||||
|
||||
if len(customerIDs) == 0 {
|
||||
@@ -430,7 +461,7 @@ func (s *repportService) GetCustomerPayment(ctx *fiber.Ctx, params *validation.C
|
||||
offset := (page - 1) * limit
|
||||
|
||||
var err error
|
||||
customerIDs, totalCustomers, err = s.CustomerPaymentRepo.GetCustomerIDsWithTransactions(ctx.Context(), limit, offset)
|
||||
customerIDs, totalCustomers, err = s.CustomerPaymentRepo.GetCustomerIDsWithTransactions(ctx.Context(), limit, offset, allowedCustomerIDs)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -456,6 +487,37 @@ func (s *repportService) GetCustomerPayment(ctx *fiber.Ctx, params *validation.C
|
||||
return result, totalCustomers, nil
|
||||
}
|
||||
|
||||
func (s *repportService) getCustomerIDsByScope(ctx context.Context, locationIDs, areaIDs []uint) ([]uint, error) {
|
||||
if len(locationIDs) == 0 && len(areaIDs) == 0 {
|
||||
return []uint{}, nil
|
||||
}
|
||||
|
||||
db := s.db.WithContext(ctx).
|
||||
Table("customers c").
|
||||
Select("DISTINCT c.id").
|
||||
Joins("JOIN marketings m ON m.customer_id = c.id").
|
||||
Joins("JOIN marketing_products mp ON mp.marketing_id = m.id").
|
||||
Joins("JOIN marketing_delivery_products mdp ON mdp.marketing_product_id = mp.id").
|
||||
Joins("JOIN product_warehouses pw ON pw.id = mdp.product_warehouse_id").
|
||||
Joins("JOIN warehouses w ON w.id = pw.warehouse_id").
|
||||
Where("mdp.delivery_date IS NOT NULL").
|
||||
Where("m.deleted_at IS NULL").
|
||||
Where("c.deleted_at IS NULL")
|
||||
|
||||
if len(locationIDs) > 0 {
|
||||
db = db.Where("w.location_id IN ?", locationIDs)
|
||||
}
|
||||
if len(areaIDs) > 0 {
|
||||
db = db.Where("w.area_id IN ?", areaIDs)
|
||||
}
|
||||
|
||||
var customerIDs []uint
|
||||
if err := db.Pluck("c.id", &customerIDs).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return customerIDs, nil
|
||||
}
|
||||
|
||||
func (s *repportService) processCustomerPayment(ctx context.Context, customerID uint, params *validation.CustomerPaymentQuery) (dto.CustomerPaymentReportItem, error) {
|
||||
|
||||
customer, err := s.CustomerRepo.GetByID(ctx, customerID, nil)
|
||||
@@ -803,7 +865,7 @@ func (s *repportService) getUniformityByWeek(ctx context.Context, projectFlockKa
|
||||
}
|
||||
|
||||
var rows []entity.ProjectFlockKandangUniformity
|
||||
if err := s.DB.WithContext(ctx).
|
||||
if err := s.db.WithContext(ctx).
|
||||
Model(&entity.ProjectFlockKandangUniformity{}).
|
||||
Select("week, uniformity, uniform_date, id, chart_data").
|
||||
Where("project_flock_kandang_id = ?", projectFlockKandangID).
|
||||
@@ -2007,6 +2069,23 @@ func intersectInt64(a, b []int64) []int64 {
|
||||
return out
|
||||
}
|
||||
|
||||
func intersectUint(a, b []uint) []uint {
|
||||
if len(a) == 0 || len(b) == 0 {
|
||||
return nil
|
||||
}
|
||||
set := make(map[uint]struct{}, len(b))
|
||||
for _, id := range b {
|
||||
set[id] = struct{}{}
|
||||
}
|
||||
out := make([]uint, 0, len(a))
|
||||
for _, id := range a {
|
||||
if _, ok := set[id]; ok {
|
||||
out = append(out, id)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func parseOptionalFloat64(raw string) (*float64, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
|
||||
Reference in New Issue
Block a user