mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Merge branch 'development' of https://gitlab.com/mbugroup/lti-api into FIX/BE/Closing_keuangan
This commit is contained in:
@@ -219,25 +219,53 @@ func (r *HppRepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKandang
|
|||||||
return totals.TotalPieces, totals.TotalWeightKg, nil
|
return totals.TotalPieces, totals.TotalWeightKg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *HppRepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time) (float64, float64, error) {
|
func (r *HppRepositoryImpl) GetEggTerjualPiecesAndWeightKgByProjectFlockKandangIds(
|
||||||
|
ctx context.Context,
|
||||||
|
projectFlockKandangIDs []uint,
|
||||||
|
date *time.Time,
|
||||||
|
) (float64, float64, error) {
|
||||||
|
|
||||||
if date == nil {
|
if date == nil {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
date = &now
|
date = &now
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type subResult struct {
|
||||||
|
UsableID uint
|
||||||
|
MdpUsageQty float64
|
||||||
|
MdpWeight float64
|
||||||
|
}
|
||||||
|
|
||||||
|
subQuery := r.db.WithContext(ctx).
|
||||||
|
Table("recordings AS r").
|
||||||
|
Select(`
|
||||||
|
DISTINCT sa.usable_id,
|
||||||
|
mdp.usage_qty AS mdp_usage_qty,
|
||||||
|
mdp.total_weight AS mdp_weight
|
||||||
|
`).
|
||||||
|
Joins("JOIN recording_eggs re ON re.recording_id = r.id").
|
||||||
|
Joins(
|
||||||
|
"JOIN stock_allocations sa ON sa.stockable_type = ? AND sa.stockable_id = re.id AND sa.usable_type = ?",
|
||||||
|
fifo.StockableKeyRecordingEgg.String(),
|
||||||
|
fifo.UsableKeyMarketingDelivery.String(),
|
||||||
|
).
|
||||||
|
Joins("JOIN marketing_delivery_products mdp ON mdp.id = sa.usable_id").
|
||||||
|
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
|
||||||
|
Where("r.record_datetime <= ?", *date)
|
||||||
|
|
||||||
var totals struct {
|
var totals struct {
|
||||||
TotalPieces float64
|
TotalPieces float64
|
||||||
TotalWeight float64
|
TotalWeight float64
|
||||||
}
|
}
|
||||||
|
|
||||||
err := r.db.WithContext(ctx).
|
err := r.db.WithContext(ctx).
|
||||||
Table("recordings AS r").
|
Table("(?) AS x", subQuery).
|
||||||
Select("COALESCE(SUM(mdp.usage_qty), 0) AS total_pieces, COALESCE(SUM(mdp.total_weight), 0) AS total_weight").
|
Select(`
|
||||||
Joins("JOIN recording_eggs AS re ON re.recording_id = r.id").
|
COALESCE(SUM(x.mdp_usage_qty), 0) AS total_pieces,
|
||||||
Joins("JOIN stock_allocations AS sa ON sa.stockable_type = ? AND sa.stockable_id = re.id AND sa.usable_type = ?", fifo.StockableKeyRecordingEgg.String(), fifo.UsableKeyMarketingDelivery.String()).
|
COALESCE(SUM(x.mdp_weight), 0) AS total_weight
|
||||||
Joins("JOIN marketing_delivery_products AS mdp ON mdp.id = sa.usable_id").
|
`).
|
||||||
Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs).
|
|
||||||
Where("r.record_datetime <= ?", *date).
|
|
||||||
Scan(&totals).Error
|
Scan(&totals).Error
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE payments
|
||||||
|
ALTER COLUMN bank_id SET NOT NULL;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE payments
|
||||||
|
ALTER COLUMN bank_id DROP NOT NULL;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||||
|
commonService "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
rDashboard "gitlab.com/mbugroup/lti-api.git/internal/modules/dashboards/repositories"
|
rDashboard "gitlab.com/mbugroup/lti-api.git/internal/modules/dashboards/repositories"
|
||||||
sDashboard "gitlab.com/mbugroup/lti-api.git/internal/modules/dashboards/services"
|
sDashboard "gitlab.com/mbugroup/lti-api.git/internal/modules/dashboards/services"
|
||||||
|
|
||||||
@@ -16,11 +18,12 @@ type DashboardModule struct{}
|
|||||||
|
|
||||||
func (DashboardModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
func (DashboardModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||||
dashboardRepo := rDashboard.NewDashboardRepository(db)
|
dashboardRepo := rDashboard.NewDashboardRepository(db)
|
||||||
|
hppCostRepo := commonRepo.NewHppCostRepository(db)
|
||||||
userRepo := rUser.NewUserRepository(db)
|
userRepo := rUser.NewUserRepository(db)
|
||||||
|
|
||||||
dashboardService := sDashboard.NewDashboardService(dashboardRepo, validate)
|
hppSvc := commonService.NewHppService(hppCostRepo)
|
||||||
|
dashboardService := sDashboard.NewDashboardService(dashboardRepo, validate, hppSvc)
|
||||||
userService := sUser.NewUserService(userRepo, validate)
|
userService := sUser.NewUserService(userRepo, validate)
|
||||||
|
|
||||||
DashboardRoutes(router, userService, dashboardService)
|
DashboardRoutes(router, userService, dashboardService)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ type DashboardRepository interface {
|
|||||||
SumSellingPrice(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) (SellingPriceAggregate, error)
|
SumSellingPrice(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) (SellingPriceAggregate, error)
|
||||||
SumEggProductionWeightGrams(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) (float64, error)
|
SumEggProductionWeightGrams(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) (float64, error)
|
||||||
SumEggProductionWeightKg(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) (float64, error)
|
SumEggProductionWeightKg(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) (float64, error)
|
||||||
|
ListProjectFlockKandangIDsByEggProduction(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) ([]uint, error)
|
||||||
GetRecordingWeeklyMetrics(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) ([]RecordingWeeklyMetric, error)
|
GetRecordingWeeklyMetrics(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) ([]RecordingWeeklyMetric, error)
|
||||||
GetUniformityWeeklyMetrics(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) ([]UniformityWeeklyMetric, error)
|
GetUniformityWeeklyMetrics(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) ([]UniformityWeeklyMetric, error)
|
||||||
GetStandardWeeklyMetrics(ctx context.Context, weeks []int, filters *validation.DashboardFilter) ([]StandardWeeklyMetric, error)
|
GetStandardWeeklyMetrics(ctx context.Context, weeks []int, filters *validation.DashboardFilter) ([]StandardWeeklyMetric, error)
|
||||||
|
|||||||
@@ -309,6 +309,27 @@ func (r *DashboardRepositoryImpl) SumEggProductionWeightKg(ctx context.Context,
|
|||||||
return grams / 1000, nil
|
return grams / 1000, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *DashboardRepositoryImpl) ListProjectFlockKandangIDsByEggProduction(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) ([]uint, error) {
|
||||||
|
var ids []uint
|
||||||
|
|
||||||
|
db := r.DB().WithContext(ctx).
|
||||||
|
Table("recording_eggs AS re").
|
||||||
|
Select("DISTINCT r.project_flock_kandangs_id").
|
||||||
|
Joins("JOIN recordings AS r ON r.id = re.recording_id").
|
||||||
|
Joins("JOIN project_flock_kandangs AS pfk ON pfk.id = r.project_flock_kandangs_id").
|
||||||
|
Joins("JOIN kandangs AS k ON k.id = pfk.kandang_id").
|
||||||
|
Where("r.record_datetime >= ? AND r.record_datetime < ?", start, end).
|
||||||
|
Where("r.deleted_at IS NULL")
|
||||||
|
|
||||||
|
db = applyDashboardFilters(db, filters)
|
||||||
|
|
||||||
|
if err := db.Scan(&ids).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ids, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *DashboardRepositoryImpl) GetFeedUsageByUom(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) ([]FeedUsageByUom, error) {
|
func (r *DashboardRepositoryImpl) GetFeedUsageByUom(ctx context.Context, start, end time.Time, filters *validation.DashboardFilter) ([]FeedUsageByUom, error) {
|
||||||
var rows []FeedUsageByUom
|
var rows []FeedUsageByUom
|
||||||
|
|
||||||
@@ -553,7 +574,7 @@ func (r *DashboardRepositoryImpl) GetComparisonWeeklyMetrics(ctx context.Context
|
|||||||
var rows []ComparisonWeeklyMetric
|
var rows []ComparisonWeeklyMetric
|
||||||
db := r.DB().WithContext(ctx).
|
db := r.DB().WithContext(ctx).
|
||||||
Table("recordings AS r").
|
Table("recordings AS r").
|
||||||
Select(fmt.Sprintf(`((r.day - 1) / 7 + 1) AS week,
|
Select(fmt.Sprintf(`(CASE WHEN r.day IS NULL OR r.day <= 0 THEN 1 ELSE ((r.day - 1) / 7 + 1) END) AS week,
|
||||||
%s AS series_id,
|
%s AS series_id,
|
||||||
COALESCE(AVG(%s), 0) AS value`, seriesExpr, metricExpr)).
|
COALESCE(AVG(%s), 0) AS value`, seriesExpr, metricExpr)).
|
||||||
Joins("JOIN project_flock_kandangs AS pfk ON pfk.id = r.project_flock_kandangs_id").
|
Joins("JOIN project_flock_kandangs AS pfk ON pfk.id = r.project_flock_kandangs_id").
|
||||||
@@ -561,8 +582,7 @@ func (r *DashboardRepositoryImpl) GetComparisonWeeklyMetrics(ctx context.Context
|
|||||||
Joins("JOIN project_flocks AS pf ON pf.id = pfk.project_flock_id").
|
Joins("JOIN project_flocks AS pf ON pf.id = pfk.project_flock_id").
|
||||||
Joins("JOIN locations AS loc ON loc.id = k.location_id").
|
Joins("JOIN locations AS loc ON loc.id = k.location_id").
|
||||||
Where("r.record_datetime >= ? AND r.record_datetime < ?", start, end).
|
Where("r.record_datetime >= ? AND r.record_datetime < ?", start, end).
|
||||||
Where("r.deleted_at IS NULL").
|
Where("r.deleted_at IS NULL")
|
||||||
Where("r.day IS NOT NULL AND r.day > 0")
|
|
||||||
|
|
||||||
db = applyDashboardFilters(db, filters)
|
db = applyDashboardFilters(db, filters)
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
commonService "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/dashboards/dto"
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/dashboards/dto"
|
||||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/dashboards/repositories"
|
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/dashboards/repositories"
|
||||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/dashboards/validations"
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/dashboards/validations"
|
||||||
@@ -27,13 +28,15 @@ type dashboardService struct {
|
|||||||
Log *logrus.Logger
|
Log *logrus.Logger
|
||||||
Validate *validator.Validate
|
Validate *validator.Validate
|
||||||
Repository repository.DashboardRepository
|
Repository repository.DashboardRepository
|
||||||
|
HppSvc commonService.HppService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDashboardService(repo repository.DashboardRepository, validate *validator.Validate) DashboardService {
|
func NewDashboardService(repo repository.DashboardRepository, validate *validator.Validate, hppSvc commonService.HppService) DashboardService {
|
||||||
return &dashboardService{
|
return &dashboardService{
|
||||||
Log: utils.Log,
|
Log: utils.Log,
|
||||||
Validate: validate,
|
Validate: validate,
|
||||||
Repository: repo,
|
Repository: repo,
|
||||||
|
HppSvc: hppSvc,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -592,13 +595,13 @@ func buildAggregateComparisonPercent(weeks []int, seriesRows []repository.Compar
|
|||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
|
|
||||||
if count == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if result[week] == nil {
|
if result[week] == nil {
|
||||||
result[week] = map[uint]float64{}
|
result[week] = map[uint]float64{}
|
||||||
}
|
}
|
||||||
|
if count == 0 {
|
||||||
|
result[week][series.Id] = 0
|
||||||
|
continue
|
||||||
|
}
|
||||||
result[week][series.Id] = sum / count
|
result[week][series.Id] = sum / count
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -846,6 +849,21 @@ func percentDelta(current, last float64) float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s dashboardService) calculateHppGlobal(ctx context.Context, startDate, endExclusive, endDate time.Time, location *time.Location) (float64, float64, error) {
|
func (s dashboardService) calculateHppGlobal(ctx context.Context, startDate, endExclusive, endDate time.Time, location *time.Location) (float64, float64, error) {
|
||||||
|
if s.HppSvc != nil {
|
||||||
|
currentHpp, err := s.hppGlobalForPeriod(ctx, startDate, endExclusive)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
lastMonthStart, lastMonthEndExclusive := monthRange(endDate.AddDate(0, -1, 0), location)
|
||||||
|
lastHpp, err := s.hppGlobalForPeriod(ctx, lastMonthStart, lastMonthEndExclusive)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentHpp, lastHpp, nil
|
||||||
|
}
|
||||||
|
|
||||||
totalEggKg, err := s.Repository.SumEggProductionWeightKg(ctx, startDate, endExclusive, nil)
|
totalEggKg, err := s.Repository.SumEggProductionWeightKg(ctx, startDate, endExclusive, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
@@ -878,6 +896,37 @@ func (s dashboardService) calculateHppGlobal(ctx context.Context, startDate, end
|
|||||||
return hppCurrent, hppLast, nil
|
return hppCurrent, hppLast, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s dashboardService) hppGlobalForPeriod(ctx context.Context, startDate, endExclusive time.Time) (float64, error) {
|
||||||
|
kandangIDs, err := s.Repository.ListProjectFlockKandangIDsByEggProduction(ctx, startDate, endExclusive, nil)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if len(kandangIDs) == 0 {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
endOfPeriod := endExclusive.Add(-time.Nanosecond)
|
||||||
|
totalCost := 0.0
|
||||||
|
totalWeightKg := 0.0
|
||||||
|
for _, kandangID := range kandangIDs {
|
||||||
|
hppCost, err := s.HppSvc.CalculateHppCost(kandangID, &endOfPeriod)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if hppCost == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
totalCost += hppCost.Estimation.Total
|
||||||
|
totalWeightKg += hppCost.Estimation.Kg
|
||||||
|
}
|
||||||
|
|
||||||
|
if totalWeightKg <= 0 {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalCost / totalWeightKg, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s dashboardService) calculateSellingPrice(ctx context.Context, endDate time.Time, location *time.Location) (float64, float64, error) {
|
func (s dashboardService) calculateSellingPrice(ctx context.Context, endDate time.Time, location *time.Location) (float64, float64, error) {
|
||||||
startPrevMonth, endPrevMonthExclusive := monthRange(endDate.AddDate(0, -1, 0), location)
|
startPrevMonth, endPrevMonthExclusive := monthRange(endDate.AddDate(0, -1, 0), location)
|
||||||
currentEndExclusive := endDate.AddDate(0, 0, 1)
|
currentEndExclusive := endDate.AddDate(0, 0, 1)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ type InitialRelationDTO struct {
|
|||||||
InitialBalanceType string `json:"initial_balance_type"`
|
InitialBalanceType string `json:"initial_balance_type"`
|
||||||
InitialBalanceTypeLabel string `json:"initial_balance_type_label"`
|
InitialBalanceTypeLabel string `json:"initial_balance_type_label"`
|
||||||
Party Party `json:"party"`
|
Party Party `json:"party"`
|
||||||
Bank bankDTO.BankRelationDTO `json:"bank,omitempty"`
|
Bank *bankDTO.BankRelationDTO `json:"bank"`
|
||||||
Direction string `json:"direction"`
|
Direction string `json:"direction"`
|
||||||
Nominal float64 `json:"nominal"`
|
Nominal float64 `json:"nominal"`
|
||||||
Notes string `json:"notes"`
|
Notes string `json:"notes"`
|
||||||
@@ -128,11 +128,12 @@ func partyFromInitial(e entity.Payment) Party {
|
|||||||
return party
|
return party
|
||||||
}
|
}
|
||||||
|
|
||||||
func bankFromInitial(e entity.Payment) bankDTO.BankRelationDTO {
|
func bankFromInitial(e entity.Payment) *bankDTO.BankRelationDTO {
|
||||||
if e.BankWarehouse.Id == 0 {
|
if e.BankWarehouse.Id == 0 {
|
||||||
return bankDTO.BankRelationDTO{}
|
return nil
|
||||||
}
|
}
|
||||||
return bankDTO.ToBankRelationDTO(e.BankWarehouse)
|
bank := bankDTO.ToBankRelationDTO(e.BankWarehouse)
|
||||||
|
return &bank
|
||||||
}
|
}
|
||||||
|
|
||||||
func userFromInitial(e entity.Payment) userDTO.UserRelationDTO {
|
func userFromInitial(e entity.Payment) userDTO.UserRelationDTO {
|
||||||
@@ -161,7 +162,7 @@ func initialBalanceLabel(balanceType string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initialBalanceTypeFromPayment(e entity.Payment) string {
|
func initialBalanceTypeFromPayment(e entity.Payment) string {
|
||||||
if strings.EqualFold(e.Direction, "OUT") || e.Nominal < 0 {
|
if e.Nominal < 0 {
|
||||||
return "NEGATIVE"
|
return "NEGATIVE"
|
||||||
}
|
}
|
||||||
return "POSITIVE"
|
return "POSITIVE"
|
||||||
|
|||||||
@@ -82,6 +82,7 @@ func (s initialService) GetOne(c *fiber.Ctx, id uint) (*entity.Payment, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *initialService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Payment, error) {
|
func (s *initialService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Payment, error) {
|
||||||
|
normalizeOptionalBankId(&req.BankId)
|
||||||
if err := s.Validate.Struct(req); err != nil {
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -124,7 +125,7 @@ func (s *initialService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
|||||||
PaymentDate: time.Now(),
|
PaymentDate: time.Now(),
|
||||||
PaymentMethod: string(utils.PaymentMethodSaldo),
|
PaymentMethod: string(utils.PaymentMethodSaldo),
|
||||||
BankId: req.BankId,
|
BankId: req.BankId,
|
||||||
Direction: directionForInitialType(balanceType),
|
Direction: directionForInitialType(party, balanceType),
|
||||||
Nominal: signedNominal(balanceType, req.Nominal),
|
Nominal: signedNominal(balanceType, req.Nominal),
|
||||||
Notes: req.Note,
|
Notes: req.Note,
|
||||||
CreatedBy: actorID,
|
CreatedBy: actorID,
|
||||||
@@ -164,6 +165,7 @@ func (s *initialService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s initialService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Payment, error) {
|
func (s initialService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Payment, error) {
|
||||||
|
normalizeOptionalBankId(&req.BankId)
|
||||||
if err := s.Validate.Struct(req); err != nil {
|
if err := s.Validate.Struct(req); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -186,6 +188,8 @@ func (s initialService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
|||||||
requiresExisting := req.PartyType != nil || req.PartyId != nil || req.InitialBalanceType != nil || req.Nominal != nil
|
requiresExisting := req.PartyType != nil || req.PartyId != nil || req.InitialBalanceType != nil || req.Nominal != nil
|
||||||
requiresVerification := requiresExisting || req.ReferenceNumber != nil || req.Note != nil || req.BankId != nil
|
requiresVerification := requiresExisting || req.ReferenceNumber != nil || req.Note != nil || req.BankId != nil
|
||||||
var existing *entity.Payment
|
var existing *entity.Payment
|
||||||
|
var resolvedPartyType string
|
||||||
|
var resolvedPartyId uint
|
||||||
if requiresVerification {
|
if requiresVerification {
|
||||||
current, err := s.Repository.GetByID(c.Context(), id, nil)
|
current, err := s.Repository.GetByID(c.Context(), id, nil)
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
@@ -199,26 +203,25 @@ func (s initialService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
|||||||
return nil, fiber.NewError(fiber.StatusNotFound, "Initial not found")
|
return nil, fiber.NewError(fiber.StatusNotFound, "Initial not found")
|
||||||
}
|
}
|
||||||
existing = current
|
existing = current
|
||||||
|
resolvedPartyType = existing.PartyType
|
||||||
|
resolvedPartyId = existing.PartyId
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.PartyType != nil || req.PartyId != nil {
|
if req.PartyType != nil || req.PartyId != nil {
|
||||||
partyType := existing.PartyType
|
|
||||||
partyId := existing.PartyId
|
|
||||||
|
|
||||||
if req.PartyType != nil {
|
if req.PartyType != nil {
|
||||||
normalized, err := normalizePartyType(*req.PartyType)
|
normalized, err := normalizePartyType(*req.PartyType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
partyType = normalized
|
resolvedPartyType = normalized
|
||||||
updateBody["party_type"] = partyType
|
updateBody["party_type"] = resolvedPartyType
|
||||||
}
|
}
|
||||||
if req.PartyId != nil {
|
if req.PartyId != nil {
|
||||||
partyId = *req.PartyId
|
resolvedPartyId = *req.PartyId
|
||||||
updateBody["party_id"] = partyId
|
updateBody["party_id"] = resolvedPartyId
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.ensurePartyExists(c.Context(), partyType, partyId); err != nil {
|
if err := s.ensurePartyExists(c.Context(), resolvedPartyType, resolvedPartyId); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -238,8 +241,11 @@ func (s initialService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
|||||||
nominal = *req.Nominal
|
nominal = *req.Nominal
|
||||||
}
|
}
|
||||||
|
|
||||||
updateBody["direction"] = directionForInitialType(balanceType)
|
updateBody["direction"] = directionForInitialType(resolvedPartyType, balanceType)
|
||||||
updateBody["nominal"] = signedNominal(balanceType, nominal)
|
updateBody["nominal"] = signedNominal(balanceType, nominal)
|
||||||
|
} else if req.PartyType != nil {
|
||||||
|
balanceType := balanceTypeFromPayment(existing)
|
||||||
|
updateBody["direction"] = directionForInitialType(resolvedPartyType, balanceType)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(updateBody) == 0 {
|
if len(updateBody) == 0 {
|
||||||
@@ -262,7 +268,7 @@ func isInitialTransaction(transactionType string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func balanceTypeFromPayment(payment *entity.Payment) string {
|
func balanceTypeFromPayment(payment *entity.Payment) string {
|
||||||
if strings.EqualFold(payment.Direction, "OUT") || payment.Nominal < 0 {
|
if payment.Nominal < 0 {
|
||||||
return "NEGATIVE"
|
return "NEGATIVE"
|
||||||
}
|
}
|
||||||
return "POSITIVE"
|
return "POSITIVE"
|
||||||
@@ -286,11 +292,24 @@ func normalizeInitialBalanceType(balanceType string) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func directionForInitialType(balanceType string) string {
|
func directionForInitialType(partyType string, balanceType string) string {
|
||||||
if strings.EqualFold(balanceType, "NEGATIVE") {
|
switch utils.PaymentParty(strings.ToUpper(strings.TrimSpace(partyType))) {
|
||||||
return "OUT"
|
case utils.PaymentPartySupplier:
|
||||||
|
if strings.EqualFold(balanceType, "POSITIVE") {
|
||||||
|
return "OUT"
|
||||||
|
}
|
||||||
|
return "IN"
|
||||||
|
case utils.PaymentPartyCustomer:
|
||||||
|
if strings.EqualFold(balanceType, "NEGATIVE") {
|
||||||
|
return "OUT"
|
||||||
|
}
|
||||||
|
return "IN"
|
||||||
|
default:
|
||||||
|
if strings.EqualFold(balanceType, "NEGATIVE") {
|
||||||
|
return "OUT"
|
||||||
|
}
|
||||||
|
return "IN"
|
||||||
}
|
}
|
||||||
return "IN"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func signedNominal(balanceType string, nominal float64) float64 {
|
func signedNominal(balanceType string, nominal float64) float64 {
|
||||||
@@ -335,3 +354,12 @@ func (s initialService) ensureBankExists(ctx context.Context, bankId *uint) erro
|
|||||||
commonSvc.RelationCheck{Name: "Bank", ID: bankId, Exists: s.Repository.BankExists},
|
commonSvc.RelationCheck{Name: "Bank", ID: bankId, Exists: s.Repository.BankExists},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func normalizeOptionalBankId(bankId **uint) {
|
||||||
|
if bankId == nil || *bankId == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if **bankId == 0 {
|
||||||
|
*bankId = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package validation
|
|||||||
type Create struct {
|
type Create struct {
|
||||||
PartyType string `json:"party_type" validate:"required_strict,max=50"`
|
PartyType string `json:"party_type" validate:"required_strict,max=50"`
|
||||||
PartyId uint `json:"party_id" validate:"required_strict,number,gt=0"`
|
PartyId uint `json:"party_id" validate:"required_strict,number,gt=0"`
|
||||||
BankId *uint `json:"bank_id" validate:"required_strict,number,gt=0"`
|
BankId *uint `json:"bank_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||||
ReferenceNumber string `json:"reference_number" validate:"required_strict,max=100"`
|
ReferenceNumber string `json:"reference_number" validate:"required_strict,max=100"`
|
||||||
InitialBalanceType string `json:"initial_balance_type" validate:"required_strict,oneof=NEGATIVE POSITIVE"`
|
InitialBalanceType string `json:"initial_balance_type" validate:"required_strict,oneof=NEGATIVE POSITIVE"`
|
||||||
Nominal float64 `json:"nominal" validate:"required_strict,gt=0"`
|
Nominal float64 `json:"nominal" validate:"required_strict,gt=0"`
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ func (s *injectionService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
|
|||||||
PaymentDate: adjustmentDate,
|
PaymentDate: adjustmentDate,
|
||||||
PaymentMethod: string(utils.PaymentMethodSaldo),
|
PaymentMethod: string(utils.PaymentMethodSaldo),
|
||||||
BankId: req.BankId,
|
BankId: req.BankId,
|
||||||
Direction: "IN",
|
Direction: directionForInjectionNominal(req.Nominal),
|
||||||
Nominal: req.Nominal,
|
Nominal: req.Nominal,
|
||||||
Notes: req.Notes,
|
Notes: req.Notes,
|
||||||
CreatedBy: actorID,
|
CreatedBy: actorID,
|
||||||
@@ -186,6 +186,7 @@ func (s injectionService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
|||||||
}
|
}
|
||||||
if req.Nominal != nil {
|
if req.Nominal != nil {
|
||||||
updateBody["nominal"] = *req.Nominal
|
updateBody["nominal"] = *req.Nominal
|
||||||
|
updateBody["direction"] = directionForInjectionNominal(*req.Nominal)
|
||||||
}
|
}
|
||||||
if req.Notes != nil {
|
if req.Notes != nil {
|
||||||
updateBody["notes"] = *req.Notes
|
updateBody["notes"] = *req.Notes
|
||||||
@@ -210,6 +211,13 @@ func isInjectionTransaction(transactionType string) bool {
|
|||||||
return strings.EqualFold(transactionType, string(utils.TransactionTypeInjection))
|
return strings.EqualFold(transactionType, string(utils.TransactionTypeInjection))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func directionForInjectionNominal(nominal float64) string {
|
||||||
|
if nominal < 0 {
|
||||||
|
return "OUT"
|
||||||
|
}
|
||||||
|
return "IN"
|
||||||
|
}
|
||||||
|
|
||||||
func (s injectionService) generateInjectionCode(ctx context.Context) (string, error) {
|
func (s injectionService) generateInjectionCode(ctx context.Context) (string, error) {
|
||||||
sequence, err := s.Repository.NextPaymentSequence(ctx)
|
sequence, err := s.Repository.NextPaymentSequence(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3,14 +3,14 @@ package validation
|
|||||||
type Create struct {
|
type Create struct {
|
||||||
BankId *uint `json:"bank_id" validate:"required_strict,number,gt=0"`
|
BankId *uint `json:"bank_id" validate:"required_strict,number,gt=0"`
|
||||||
AdjustmentDate string `json:"adjustment_date" validate:"required_strict"`
|
AdjustmentDate string `json:"adjustment_date" validate:"required_strict"`
|
||||||
Nominal float64 `json:"nominal" validate:"required_strict,gt=0"`
|
Nominal float64 `json:"nominal" validate:"required_strict"`
|
||||||
Notes string `json:"notes" validate:"required_strict,max=500"`
|
Notes string `json:"notes" validate:"required_strict,max=500"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Update struct {
|
type Update struct {
|
||||||
BankId *uint `json:"bank_id,omitempty" validate:"omitempty,number,gt=0"`
|
BankId *uint `json:"bank_id,omitempty" validate:"omitempty,number,gt=0"`
|
||||||
AdjustmentDate *string `json:"adjustment_date,omitempty" validate:"omitempty"`
|
AdjustmentDate *string `json:"adjustment_date,omitempty" validate:"omitempty"`
|
||||||
Nominal *float64 `json:"nominal,omitempty" validate:"omitempty,gt=0"`
|
Nominal *float64 `json:"nominal,omitempty"`
|
||||||
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
|
Notes *string `json:"notes,omitempty" validate:"omitempty,max=500"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package controller
|
|||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/dto"
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/dto"
|
||||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/services"
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/services"
|
||||||
@@ -23,10 +24,46 @@ func NewTransactionController(transactionService service.TransactionService) *Tr
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *TransactionController) GetAll(c *fiber.Ctx) error {
|
func (u *TransactionController) GetAll(c *fiber.Ctx) error {
|
||||||
|
parseOptionalUint := func(key string) (*uint, error) {
|
||||||
|
raw := strings.TrimSpace(c.Query(key, ""))
|
||||||
|
if raw == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
parsed, err := strconv.ParseUint(raw, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid "+key)
|
||||||
|
}
|
||||||
|
if parsed == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
value := uint(parsed)
|
||||||
|
return &value, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
bankId, err := parseOptionalUint("bank_id")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
customerId, err := parseOptionalUint("customer_id")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
supplierId, err := parseOptionalUint("supplier_id")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
query := &validation.Query{
|
query := &validation.Query{
|
||||||
Page: c.QueryInt("page", 1),
|
Page: c.QueryInt("page", 1),
|
||||||
Limit: c.QueryInt("limit", 10),
|
Limit: c.QueryInt("limit", 10),
|
||||||
Search: c.Query("search", ""),
|
Search: c.Query("search", ""),
|
||||||
|
TransactionType: c.Query("transaction_type", ""),
|
||||||
|
BankId: bankId,
|
||||||
|
CustomerId: customerId,
|
||||||
|
SupplierId: supplierId,
|
||||||
|
SortDate: c.Query("sort_date", ""),
|
||||||
|
StartDate: c.Query("start_date", ""),
|
||||||
|
EndDate: c.Query("end_date", ""),
|
||||||
}
|
}
|
||||||
|
|
||||||
if query.Page < 1 || query.Limit < 1 {
|
if query.Page < 1 || query.Limit < 1 {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ type TransactionRelationDTO struct {
|
|||||||
Party Party `json:"party"`
|
Party Party `json:"party"`
|
||||||
PaymentDate time.Time `json:"payment_date"`
|
PaymentDate time.Time `json:"payment_date"`
|
||||||
PaymentMethod string `json:"payment_method"`
|
PaymentMethod string `json:"payment_method"`
|
||||||
Bank bankDTO.BankRelationDTO `json:"bank,omitempty"`
|
Bank *bankDTO.BankRelationDTO `json:"bank"`
|
||||||
ExpenseAmount float64 `json:"expense_amount"`
|
ExpenseAmount float64 `json:"expense_amount"`
|
||||||
IncomeAmount float64 `json:"income_amount"`
|
IncomeAmount float64 `json:"income_amount"`
|
||||||
Nominal float64 `json:"nominal"`
|
Nominal float64 `json:"nominal"`
|
||||||
@@ -37,7 +37,7 @@ type TransactionListDTO struct {
|
|||||||
Party Party `json:"party"`
|
Party Party `json:"party"`
|
||||||
PaymentDate time.Time `json:"payment_date"`
|
PaymentDate time.Time `json:"payment_date"`
|
||||||
PaymentMethod string `json:"payment_method"`
|
PaymentMethod string `json:"payment_method"`
|
||||||
Bank bankDTO.BankRelationDTO `json:"bank"`
|
Bank *bankDTO.BankRelationDTO `json:"bank"`
|
||||||
ExpenseAmount float64 `json:"expense_amount"`
|
ExpenseAmount float64 `json:"expense_amount"`
|
||||||
IncomeAmount float64 `json:"income_amount"`
|
IncomeAmount float64 `json:"income_amount"`
|
||||||
Nominal float64 `json:"nominal"`
|
Nominal float64 `json:"nominal"`
|
||||||
@@ -151,11 +151,12 @@ func partyFromPayment(e entity.Payment) Party {
|
|||||||
return party
|
return party
|
||||||
}
|
}
|
||||||
|
|
||||||
func bankFromPayment(e entity.Payment) bankDTO.BankRelationDTO {
|
func bankFromPayment(e entity.Payment) *bankDTO.BankRelationDTO {
|
||||||
if e.BankWarehouse.Id == 0 {
|
if e.BankWarehouse.Id == 0 {
|
||||||
return bankDTO.BankRelationDTO{}
|
return nil
|
||||||
}
|
}
|
||||||
return bankDTO.ToBankRelationDTO(e.BankWarehouse)
|
bank := bankDTO.ToBankRelationDTO(e.BankWarehouse)
|
||||||
|
return &bank
|
||||||
}
|
}
|
||||||
|
|
||||||
func userFromPayment(e entity.Payment) userDTO.UserRelationDTO {
|
func userFromPayment(e entity.Payment) userDTO.UserRelationDTO {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
@@ -61,13 +62,19 @@ func (s transactionService) GetAll(c *fiber.Ctx, params *validation.Query) ([]en
|
|||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
startDate, endDate, err := parseTransactionDateRange(params.StartDate, params.EndDate)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
offset := (params.Page - 1) * params.Limit
|
offset := (params.Page - 1) * params.Limit
|
||||||
|
|
||||||
transactions, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
transactions, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||||
db = s.withRelations(db)
|
db = s.withRelations(db)
|
||||||
|
|
||||||
if params.Search != "" {
|
if params.Search != "" {
|
||||||
like := "%" + strings.ToLower(strings.TrimSpace(params.Search)) + "%"
|
like := "%" + strings.ToLower(strings.TrimSpace(params.Search)) + "%"
|
||||||
return db.Where(
|
db = db.Where(
|
||||||
`LOWER(payment_code) LIKE ? OR
|
`LOWER(payment_code) LIKE ? OR
|
||||||
LOWER(COALESCE(reference_number, '')) LIKE ? OR
|
LOWER(COALESCE(reference_number, '')) LIKE ? OR
|
||||||
LOWER(COALESCE(transaction_type, '')) LIKE ? OR
|
LOWER(COALESCE(transaction_type, '')) LIKE ? OR
|
||||||
@@ -75,7 +82,35 @@ func (s transactionService) GetAll(c *fiber.Ctx, params *validation.Query) ([]en
|
|||||||
like, like, like, like,
|
like, like, like, like,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return db.Order("payment_date DESC").Order("created_at DESC")
|
|
||||||
|
if strings.TrimSpace(params.TransactionType) != "" {
|
||||||
|
db = db.Where("transaction_type = ?", strings.ToUpper(strings.TrimSpace(params.TransactionType)))
|
||||||
|
}
|
||||||
|
|
||||||
|
if params.BankId != nil {
|
||||||
|
db = db.Where("bank_id = ?", *params.BankId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if params.CustomerId != nil && params.SupplierId != nil {
|
||||||
|
db = db.Where(
|
||||||
|
"(party_type = ? AND party_id = ?) OR (party_type = ? AND party_id = ?)",
|
||||||
|
string(utils.PaymentPartyCustomer), *params.CustomerId,
|
||||||
|
string(utils.PaymentPartySupplier), *params.SupplierId,
|
||||||
|
)
|
||||||
|
} else if params.CustomerId != nil {
|
||||||
|
db = db.Where("party_type = ? AND party_id = ?", string(utils.PaymentPartyCustomer), *params.CustomerId)
|
||||||
|
} else if params.SupplierId != nil {
|
||||||
|
db = db.Where("party_type = ? AND party_id = ?", string(utils.PaymentPartySupplier), *params.SupplierId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if startDate != nil {
|
||||||
|
db = db.Where("payment_date >= ?", *startDate)
|
||||||
|
}
|
||||||
|
if endDate != nil {
|
||||||
|
db = db.Where("payment_date < ?", *endDate)
|
||||||
|
}
|
||||||
|
|
||||||
|
return applyTransactionSort(db, params.SortDate)
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -173,3 +208,47 @@ func (s transactionService) approvalQueryModifier() func(*gorm.DB) *gorm.DB {
|
|||||||
return db.Preload("ActionUser")
|
return db.Preload("ActionUser")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseTransactionDateRange(startDate, endDate string) (*time.Time, *time.Time, error) {
|
||||||
|
start := strings.TrimSpace(startDate)
|
||||||
|
end := strings.TrimSpace(endDate)
|
||||||
|
|
||||||
|
var startPtr *time.Time
|
||||||
|
var endPtr *time.Time
|
||||||
|
var endValue *time.Time
|
||||||
|
|
||||||
|
if start != "" {
|
||||||
|
parsed, err := utils.ParseDateString(start)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, utils.BadRequest("start_date must use format YYYY-MM-DD")
|
||||||
|
}
|
||||||
|
startPtr = &parsed
|
||||||
|
}
|
||||||
|
|
||||||
|
if end != "" {
|
||||||
|
parsed, err := utils.ParseDateString(end)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, utils.BadRequest("end_date must use format YYYY-MM-DD")
|
||||||
|
}
|
||||||
|
endValue = &parsed
|
||||||
|
nextDay := parsed.AddDate(0, 0, 1)
|
||||||
|
endPtr = &nextDay
|
||||||
|
}
|
||||||
|
|
||||||
|
if startPtr != nil && endValue != nil && startPtr.After(*endValue) {
|
||||||
|
return nil, nil, utils.BadRequest("start_date must be earlier than end_date")
|
||||||
|
}
|
||||||
|
|
||||||
|
return startPtr, endPtr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func applyTransactionSort(db *gorm.DB, sortDate string) *gorm.DB {
|
||||||
|
switch strings.ToLower(strings.TrimSpace(sortDate)) {
|
||||||
|
case "created_at":
|
||||||
|
return db.Order("created_at DESC").Order("payment_date DESC")
|
||||||
|
case "payment_date":
|
||||||
|
return db.Order("payment_date DESC").Order("created_at DESC")
|
||||||
|
default:
|
||||||
|
return db.Order("payment_date DESC").Order("created_at DESC")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,7 +9,14 @@ type Update struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
|
TransactionType string `query:"transaction_type" validate:"omitempty,max=50"`
|
||||||
|
BankId *uint `query:"bank_id" validate:"omitempty,number,gt=0"`
|
||||||
|
CustomerId *uint `query:"customer_id" validate:"omitempty,number,gt=0"`
|
||||||
|
SupplierId *uint `query:"supplier_id" validate:"omitempty,number,gt=0"`
|
||||||
|
SortDate string `query:"sort_date" validate:"omitempty,oneof=created_at payment_date"`
|
||||||
|
StartDate string `query:"start_date" validate:"omitempty,datetime=2006-01-02"`
|
||||||
|
EndDate string `query:"end_date" validate:"omitempty,datetime=2006-01-02"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -156,6 +156,22 @@ func (s customerService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint
|
|||||||
updateBody["type"] = typ
|
updateBody["type"] = typ
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if req.Address != nil {
|
||||||
|
updateBody["address"] = *req.Address
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Phone != nil {
|
||||||
|
updateBody["phone"] = *req.Phone
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Email != nil {
|
||||||
|
updateBody["email"] = *req.Email
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.AccountNumber != nil {
|
||||||
|
updateBody["account_number"] = *req.AccountNumber
|
||||||
|
}
|
||||||
|
|
||||||
if len(updateBody) == 0 {
|
if len(updateBody) == 0 {
|
||||||
return s.GetOne(c, id)
|
return s.GetOne(c, id)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ func (u *WarehouseController) GetAll(c *fiber.Ctx) error {
|
|||||||
Limit: c.QueryInt("limit", 10),
|
Limit: c.QueryInt("limit", 10),
|
||||||
Search: c.Query("search", ""),
|
Search: c.Query("search", ""),
|
||||||
AreaId: c.QueryInt("area_id", 0),
|
AreaId: c.QueryInt("area_id", 0),
|
||||||
|
LocationId: c.QueryInt("location_id", 0),
|
||||||
ActiveProjectFlockOnly: c.QueryBool("active_project_flock", false),
|
ActiveProjectFlockOnly: c.QueryBool("active_project_flock", false),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ func (s warehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
|||||||
if params.AreaId != 0 {
|
if params.AreaId != 0 {
|
||||||
db = db.Where("area_id = ?", params.AreaId)
|
db = db.Where("area_id = ?", params.AreaId)
|
||||||
}
|
}
|
||||||
|
if params.LocationId != 0 {
|
||||||
|
db = db.Where("location_id = ?", params.LocationId)
|
||||||
|
}
|
||||||
if params.ActiveProjectFlockOnly {
|
if params.ActiveProjectFlockOnly {
|
||||||
db = db.Where(`
|
db = db.Where(`
|
||||||
EXISTS (
|
EXISTS (
|
||||||
|
|||||||
@@ -21,5 +21,6 @@ type Query struct {
|
|||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
AreaId int `query:"area_id" validate:"omitempty,number,gt=0"`
|
AreaId int `query:"area_id" validate:"omitempty,number,gt=0"`
|
||||||
|
LocationId int `query:"location_id" validate:"omitempty,number,gt=0"`
|
||||||
ActiveProjectFlockOnly bool `query:"active_project_flock"`
|
ActiveProjectFlockOnly bool `query:"active_project_flock"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -287,6 +287,7 @@ func (s *recordingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
|
|||||||
}
|
}
|
||||||
|
|
||||||
mappedDepletions := recordingutil.MapDepletions(createdRecording.Id, req.Depletions)
|
mappedDepletions := recordingutil.MapDepletions(createdRecording.Id, req.Depletions)
|
||||||
|
depletionDesired := resetDepletionQuantitiesForFIFO(mappedDepletions, s.FifoSvc != nil)
|
||||||
if s.FifoSvc != nil && len(mappedDepletions) > 0 {
|
if s.FifoSvc != nil && len(mappedDepletions) > 0 {
|
||||||
sourceWarehouseID, err := s.resolvePopulationWarehouseID(ctx, req.ProjectFlockKandangId)
|
sourceWarehouseID, err := s.resolvePopulationWarehouseID(ctx, req.ProjectFlockKandangId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -301,6 +302,7 @@ func (s *recordingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if s.FifoSvc != nil {
|
if s.FifoSvc != nil {
|
||||||
|
applyDepletionDesiredQuantities(mappedDepletions, depletionDesired, true)
|
||||||
note := fmt.Sprintf("Recording-Create#%d", createdRecording.Id)
|
note := fmt.Sprintf("Recording-Create#%d", createdRecording.Id)
|
||||||
if err := s.consumeRecordingDepletions(ctx, tx, mappedDepletions, note, actorID); err != nil {
|
if err := s.consumeRecordingDepletions(ctx, tx, mappedDepletions, note, actorID); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -465,6 +467,7 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
|||||||
}
|
}
|
||||||
|
|
||||||
mappedDepletions := recordingutil.MapDepletions(recordingEntity.Id, req.Depletions)
|
mappedDepletions := recordingutil.MapDepletions(recordingEntity.Id, req.Depletions)
|
||||||
|
depletionDesired := resetDepletionQuantitiesForFIFO(mappedDepletions, s.FifoSvc != nil)
|
||||||
if s.FifoSvc != nil && len(mappedDepletions) > 0 {
|
if s.FifoSvc != nil && len(mappedDepletions) > 0 {
|
||||||
sourceWarehouseID, err := s.resolvePopulationWarehouseID(ctx, recordingEntity.ProjectFlockKandangId)
|
sourceWarehouseID, err := s.resolvePopulationWarehouseID(ctx, recordingEntity.ProjectFlockKandangId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -480,6 +483,7 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
|||||||
}
|
}
|
||||||
|
|
||||||
if s.FifoSvc != nil {
|
if s.FifoSvc != nil {
|
||||||
|
applyDepletionDesiredQuantities(mappedDepletions, depletionDesired, true)
|
||||||
note := fmt.Sprintf("Recording-Edit#%d", recordingEntity.Id)
|
note := fmt.Sprintf("Recording-Edit#%d", recordingEntity.Id)
|
||||||
if err := s.consumeRecordingDepletions(ctx, tx, mappedDepletions, note, actorID); err != nil {
|
if err := s.consumeRecordingDepletions(ctx, tx, mappedDepletions, note, actorID); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -929,6 +933,9 @@ func (s *recordingService) consumeRecordingDepletions(
|
|||||||
|
|
||||||
destDelta := depletion.Qty + depletion.PendingQty
|
destDelta := depletion.Qty + depletion.PendingQty
|
||||||
if depletion.ProductWarehouseId != 0 && destDelta > 0 && strings.TrimSpace(note) != "" && actorID != 0 {
|
if depletion.ProductWarehouseId != 0 && destDelta > 0 && strings.TrimSpace(note) != "" && actorID != 0 {
|
||||||
|
if depletion.ProductWarehouseId == sourceWarehouseID {
|
||||||
|
continue
|
||||||
|
}
|
||||||
log := &entity.StockLog{
|
log := &entity.StockLog{
|
||||||
ProductWarehouseId: depletion.ProductWarehouseId,
|
ProductWarehouseId: depletion.ProductWarehouseId,
|
||||||
CreatedBy: actorID,
|
CreatedBy: actorID,
|
||||||
@@ -1066,6 +1073,9 @@ func (s *recordingService) releaseRecordingDepletions(
|
|||||||
|
|
||||||
destDelta := depletion.Qty + depletion.PendingQty
|
destDelta := depletion.Qty + depletion.PendingQty
|
||||||
if depletion.ProductWarehouseId != 0 && destDelta > 0 && strings.TrimSpace(note) != "" && actorID != 0 {
|
if depletion.ProductWarehouseId != 0 && destDelta > 0 && strings.TrimSpace(note) != "" && actorID != 0 {
|
||||||
|
if depletion.ProductWarehouseId == sourceWarehouseID {
|
||||||
|
continue
|
||||||
|
}
|
||||||
log := &entity.StockLog{
|
log := &entity.StockLog{
|
||||||
ProductWarehouseId: depletion.ProductWarehouseId,
|
ProductWarehouseId: depletion.ProductWarehouseId,
|
||||||
CreatedBy: actorID,
|
CreatedBy: actorID,
|
||||||
@@ -1235,6 +1245,11 @@ type desiredStock struct {
|
|||||||
Pending float64
|
Pending float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type desiredDepletion struct {
|
||||||
|
Qty float64
|
||||||
|
Pending float64
|
||||||
|
}
|
||||||
|
|
||||||
func resetStockQuantitiesForFIFO(stocks []entity.RecordingStock, enabled bool) []desiredStock {
|
func resetStockQuantitiesForFIFO(stocks []entity.RecordingStock, enabled bool) []desiredStock {
|
||||||
desired := make([]desiredStock, len(stocks))
|
desired := make([]desiredStock, len(stocks))
|
||||||
for i := range stocks {
|
for i := range stocks {
|
||||||
@@ -1269,6 +1284,33 @@ func applyStockDesiredQuantities(stocks []entity.RecordingStock, desired []desir
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resetDepletionQuantitiesForFIFO(depletions []entity.RecordingDepletion, enabled bool) []desiredDepletion {
|
||||||
|
desired := make([]desiredDepletion, len(depletions))
|
||||||
|
for i := range depletions {
|
||||||
|
desired[i].Qty = depletions[i].Qty
|
||||||
|
desired[i].Pending = depletions[i].PendingQty
|
||||||
|
if !enabled {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
depletions[i].Qty = 0
|
||||||
|
depletions[i].PendingQty = 0
|
||||||
|
}
|
||||||
|
return desired
|
||||||
|
}
|
||||||
|
|
||||||
|
func applyDepletionDesiredQuantities(depletions []entity.RecordingDepletion, desired []desiredDepletion, enabled bool) {
|
||||||
|
if !enabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for i := range depletions {
|
||||||
|
if i >= len(desired) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
depletions[i].Qty = desired[i].Qty
|
||||||
|
depletions[i].PendingQty = desired[i].Pending
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *recordingService) syncRecordingStocks(
|
func (s *recordingService) syncRecordingStocks(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
tx *gorm.DB,
|
tx *gorm.DB,
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ type HppPerKandangRow struct {
|
|||||||
// RemainingChickenBirds float64
|
// RemainingChickenBirds float64
|
||||||
// RemainingChickenWeight float64
|
// RemainingChickenWeight float64
|
||||||
EggProductionWeightKgRemaining float64
|
EggProductionWeightKgRemaining float64
|
||||||
|
// AverageWeightEggPerPiece float64
|
||||||
// EggProductionPiecesRemaining float64
|
// EggProductionPiecesRemaining float64
|
||||||
// EggProductionTotalWeightKg float64
|
// EggProductionTotalWeightKg float64
|
||||||
// EggProductionTotalPieces float64
|
// EggProductionTotalPieces float64
|
||||||
@@ -229,8 +230,8 @@ func (r *hppPerKandangRepository) GetWeightRemainingByProjectFlockKandangIDs(ctx
|
|||||||
)
|
)
|
||||||
|
|
||||||
type eggRow struct {
|
type eggRow struct {
|
||||||
ProjectFlockKandangID uint
|
ProjectFlockKandangID uint
|
||||||
EggProductionWeightKgRemaining float64
|
AverageWeightEggPerPiece float64
|
||||||
// EggProductionPiecesRemaining float64
|
// EggProductionPiecesRemaining float64
|
||||||
// EggProductionTotalWeightKg float64
|
// EggProductionTotalWeightKg float64
|
||||||
// EggProductionTotalPieces float64
|
// EggProductionTotalPieces float64
|
||||||
@@ -241,7 +242,7 @@ func (r *hppPerKandangRepository) GetWeightRemainingByProjectFlockKandangIDs(ctx
|
|||||||
Table("recordings AS r").
|
Table("recordings AS r").
|
||||||
Select(`
|
Select(`
|
||||||
r.project_flock_kandangs_id AS project_flock_kandang_id,
|
r.project_flock_kandangs_id AS project_flock_kandang_id,
|
||||||
COALESCE((SUM(re.weight) / NULLIF(SUM(re.total_qty), 0)) * SUM(re.total_qty - re.total_used), 0) AS egg_production_weight_kg_remaining`).
|
COALESCE(SUM(re.weight) / NULLIF(SUM(re.total_qty), 0), 0) AS average_weight_egg_per_piece`).
|
||||||
Joins("LEFT JOIN (?) AS la ON la.approvable_id = r.id", latestApproval).
|
Joins("LEFT JOIN (?) AS la ON la.approvable_id = r.id", latestApproval).
|
||||||
Joins("LEFT JOIN recording_eggs AS re ON re.recording_id = r.id").
|
Joins("LEFT JOIN recording_eggs AS re ON re.recording_id = r.id").
|
||||||
Where("r.project_flock_kandangs_id IN ?", projectFlockKandangIDs).
|
Where("r.project_flock_kandangs_id IN ?", projectFlockKandangIDs).
|
||||||
@@ -257,8 +258,8 @@ func (r *hppPerKandangRepository) GetWeightRemainingByProjectFlockKandangIDs(ctx
|
|||||||
result := make(map[uint]HppPerKandangRow, len(eggRows))
|
result := make(map[uint]HppPerKandangRow, len(eggRows))
|
||||||
for _, row := range eggRows {
|
for _, row := range eggRows {
|
||||||
result[row.ProjectFlockKandangID] = HppPerKandangRow{
|
result[row.ProjectFlockKandangID] = HppPerKandangRow{
|
||||||
ProjectFlockKandangID: row.ProjectFlockKandangID,
|
ProjectFlockKandangID: row.ProjectFlockKandangID,
|
||||||
EggProductionWeightKgRemaining: row.EggProductionWeightKgRemaining,
|
// AverageWeightEggPerPiece: row.AverageWeightEggPerPiece,
|
||||||
// EggProductionPiecesRemaining: row.EggProductionPiecesRemaining,
|
// EggProductionPiecesRemaining: row.EggProductionPiecesRemaining,
|
||||||
// EggProductionTotalWeightKg: row.EggProductionTotalWeightKg,
|
// EggProductionTotalWeightKg: row.EggProductionTotalWeightKg,
|
||||||
// EggProductionTotalPieces: row.EggProductionTotalPieces,
|
// EggProductionTotalPieces: row.EggProductionTotalPieces,
|
||||||
|
|||||||
@@ -1516,18 +1516,16 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
eggMap, err := s.HppPerKandangRepo.GetWeightRemainingByProjectFlockKandangIDs(ctx.Context(), startOfDay, endOfDay, validPfkIDs)
|
// eggMap, err := s.HppPerKandangRepo.GetWeightRemainingByProjectFlockKandangIDs(ctx.Context(), startOfDay, endOfDay, validPfkIDs)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, nil, err
|
// return nil, nil, err
|
||||||
}
|
// }
|
||||||
for pfkID, egg := range eggMap {
|
// for pfkID, egg := range eggMap {
|
||||||
if rowIdx, ok := pfkIndex[pfkID]; ok {
|
// if rowIdx, ok := pfkIndex[pfkID]; ok {
|
||||||
repoRows[rowIdx].EggProductionWeightKgRemaining = egg.EggProductionWeightKgRemaining
|
// repoRows[rowIdx].EggProductionWeightKgRemaining = egg.EggProductionWeightKgRemaining
|
||||||
// repoRows[rowIdx].EggProductionPiecesRemaining = egg.EggProductionPiecesRemaining
|
// repoRows[rowIdx].AverageWeightEggPerPiece = egg.AverageWeightEggPerPiece
|
||||||
// repoRows[rowIdx].EggProductionTotalWeightKg = egg.EggProductionTotalWeightKg
|
// }
|
||||||
// repoRows[rowIdx].EggProductionTotalPieces = egg.EggProductionTotalPieces
|
// }
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
costMap := make(map[uint]HppCostAggregate, len(costRows))
|
costMap := make(map[uint]HppCostAggregate, len(costRows))
|
||||||
@@ -1613,9 +1611,10 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
|
|||||||
}
|
}
|
||||||
|
|
||||||
var eggPiecesFloatRemaining float64
|
var eggPiecesFloatRemaining float64
|
||||||
eggRemainingWeightFloatRemaining := row.EggProductionWeightKgRemaining
|
var eggRemainingWeightFloatRemaining float64
|
||||||
var eggTotalPiecesFloat float64
|
var eggTotalPiecesFloat float64
|
||||||
var eggWeightFloat float64
|
var eggWeightFloat float64
|
||||||
|
var avgWeight float64
|
||||||
eggHpp := 0.0
|
eggHpp := 0.0
|
||||||
if s.HppSvc != nil {
|
if s.HppSvc != nil {
|
||||||
hppCost, err := s.HppSvc.CalculateHppCost(row.ProjectFlockKandangID, &endOfDay)
|
hppCost, err := s.HppSvc.CalculateHppCost(row.ProjectFlockKandangID, &endOfDay)
|
||||||
@@ -1623,11 +1622,12 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
if hppCost != nil {
|
if hppCost != nil {
|
||||||
// eggRemainingWeightFloatRemaining = hppCost.Estimation.Kg - hppCost.Real.Kg
|
|
||||||
eggPiecesFloatRemaining = hppCost.Estimation.Butir - hppCost.Real.Butir
|
eggPiecesFloatRemaining = hppCost.Estimation.Butir - hppCost.Real.Butir
|
||||||
eggHpp = hppCost.Estimation.HargaKg
|
eggHpp = hppCost.Estimation.HargaKg
|
||||||
eggTotalPiecesFloat = hppCost.Estimation.Butir
|
eggTotalPiecesFloat = hppCost.Estimation.Butir
|
||||||
eggWeightFloat = hppCost.Estimation.Kg
|
eggWeightFloat = hppCost.Estimation.Kg
|
||||||
|
avgWeight = eggWeightFloat / eggTotalPiecesFloat
|
||||||
|
eggRemainingWeightFloatRemaining = avgWeight * eggPiecesFloatRemaining
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if math.IsNaN(eggPiecesFloatRemaining) || math.IsInf(eggPiecesFloatRemaining, 0) {
|
if math.IsNaN(eggPiecesFloatRemaining) || math.IsInf(eggPiecesFloatRemaining, 0) {
|
||||||
@@ -1643,10 +1643,6 @@ func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangRes
|
|||||||
eggWeightFloat = 0
|
eggWeightFloat = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
avgWeight := 0.0
|
|
||||||
if eggTotalPiecesFloat > 0 {
|
|
||||||
avgWeight = eggWeightFloat / eggTotalPiecesFloat
|
|
||||||
}
|
|
||||||
if params.WeightMin != nil && avgWeight < *params.WeightMin {
|
if params.WeightMin != nil && avgWeight < *params.WeightMin {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user