fix:(fot) ajuste bug no gasto de captaçao

This commit is contained in:
NANDO9322 2026-02-12 11:32:14 -03:00
parent 8703f9c8a8
commit 421ca81ee4
5 changed files with 31 additions and 22 deletions

View file

@ -598,16 +598,15 @@ const updateCadastroFotGastos = `-- name: UpdateCadastroFotGastos :exec
UPDATE cadastro_fot SET UPDATE cadastro_fot SET
gastos_captacao = $2, gastos_captacao = $2,
updated_at = CURRENT_TIMESTAMP updated_at = CURRENT_TIMESTAMP
WHERE id = $1 AND regiao = $3 WHERE id = $1
` `
type UpdateCadastroFotGastosParams struct { type UpdateCadastroFotGastosParams struct {
ID pgtype.UUID `json:"id"` ID pgtype.UUID `json:"id"`
GastosCaptacao pgtype.Numeric `json:"gastos_captacao"` GastosCaptacao pgtype.Numeric `json:"gastos_captacao"`
Regiao pgtype.Text `json:"regiao"`
} }
func (q *Queries) UpdateCadastroFotGastos(ctx context.Context, arg UpdateCadastroFotGastosParams) error { func (q *Queries) UpdateCadastroFotGastos(ctx context.Context, arg UpdateCadastroFotGastosParams) error {
_, err := q.db.Exec(ctx, updateCadastroFotGastos, arg.ID, arg.GastosCaptacao, arg.Regiao) _, err := q.db.Exec(ctx, updateCadastroFotGastos, arg.ID, arg.GastosCaptacao)
return err return err
} }

View file

@ -653,16 +653,11 @@ func (q *Queries) ListTransactionsPaginatedFiltered(ctx context.Context, arg Lis
const sumTotalByFot = `-- name: SumTotalByFot :one const sumTotalByFot = `-- name: SumTotalByFot :one
SELECT COALESCE(SUM(total_pagar), 0)::NUMERIC SELECT COALESCE(SUM(total_pagar), 0)::NUMERIC
FROM financial_transactions FROM financial_transactions
WHERE fot_id = $1 AND regiao = $2 WHERE fot_id = $1
` `
type SumTotalByFotParams struct { func (q *Queries) SumTotalByFot(ctx context.Context, fotID pgtype.UUID) (pgtype.Numeric, error) {
FotID pgtype.UUID `json:"fot_id"` row := q.db.QueryRow(ctx, sumTotalByFot, fotID)
Regiao pgtype.Text `json:"regiao"`
}
func (q *Queries) SumTotalByFot(ctx context.Context, arg SumTotalByFotParams) (pgtype.Numeric, error) {
row := q.db.QueryRow(ctx, sumTotalByFot, arg.FotID, arg.Regiao)
var column_1 pgtype.Numeric var column_1 pgtype.Numeric
err := row.Scan(&column_1) err := row.Scan(&column_1)
return column_1, err return column_1, err

View file

@ -94,7 +94,7 @@ WHERE c.fot = $1 AND c.regiao = @regiao;
UPDATE cadastro_fot SET UPDATE cadastro_fot SET
gastos_captacao = $2, gastos_captacao = $2,
updated_at = CURRENT_TIMESTAMP updated_at = CURRENT_TIMESTAMP
WHERE id = $1 AND regiao = @regiao; WHERE id = $1;
-- name: SearchFot :many -- name: SearchFot :many
SELECT SELECT

View file

@ -22,7 +22,7 @@ ORDER BY t.data_cobranca DESC NULLS LAST;
-- name: SumTotalByFot :one -- name: SumTotalByFot :one
SELECT COALESCE(SUM(total_pagar), 0)::NUMERIC SELECT COALESCE(SUM(total_pagar), 0)::NUMERIC
FROM financial_transactions FROM financial_transactions
WHERE fot_id = $1 AND regiao = @regiao; WHERE fot_id = $1;
-- name: UpdateTransaction :one -- name: UpdateTransaction :one
UPDATE financial_transactions SET UPDATE financial_transactions SET

View file

@ -29,7 +29,7 @@ func (s *Service) Create(ctx context.Context, params generated.CreateTransaction
} }
if params.FotID.Valid { if params.FotID.Valid {
_ = s.updateFotExpenses(ctx, params.FotID, regiao) _ = s.updateFotExpenses(ctx, params.FotID)
} }
return txn, nil return txn, nil
@ -44,7 +44,7 @@ func (s *Service) Update(ctx context.Context, params generated.UpdateTransaction
// Recalculate for the new FOT (if changed, we should technically recalc old one too, but simpler for now) // Recalculate for the new FOT (if changed, we should technically recalc old one too, but simpler for now)
if params.FotID.Valid { if params.FotID.Valid {
_ = s.updateFotExpenses(ctx, params.FotID, regiao) _ = s.updateFotExpenses(ctx, params.FotID)
} }
return txn, nil return txn, nil
} }
@ -63,15 +63,33 @@ func (s *Service) BulkUpdate(ctx context.Context, ids []string, valorExtra float
return nil return nil
} }
// 1. Identify affected FOTs to recalculate expenses later
affectedFotIDs := make(map[string]pgtype.UUID)
for _, id := range pgIDs {
txn, err := s.queries.GetTransaction(ctx, generated.GetTransactionParams{
ID: id,
Regiao: pgtype.Text{String: regiao, Valid: true},
})
if err == nil && txn.FotID.Valid {
affectedFotIDs[fmt.Sprintf("%x", txn.FotID.Bytes)] = txn.FotID
}
}
// 2. Perform Bulk Update
err := s.queries.BulkUpdateExtras(ctx, generated.BulkUpdateExtrasParams{ err := s.queries.BulkUpdateExtras(ctx, generated.BulkUpdateExtrasParams{
ValorExtra: toNumeric(valorExtra), ValorExtra: toNumeric(valorExtra),
DescricaoExtra: pgtype.Text{String: descricaoExtra, Valid: true}, // Allow empty description? Yes. DescricaoExtra: pgtype.Text{String: descricaoExtra, Valid: true},
Ids: pgIDs, Ids: pgIDs,
}) })
if err != nil { if err != nil {
return err return err
} }
// 3. Recalculate Expenses for affected FOTs
for _, fotID := range affectedFotIDs {
_ = s.updateFotExpenses(ctx, fotID)
}
return nil return nil
} }
@ -94,7 +112,7 @@ func (s *Service) Delete(ctx context.Context, id pgtype.UUID, regiao string) err
} }
if txn.FotID.Valid { if txn.FotID.Valid {
_ = s.updateFotExpenses(ctx, txn.FotID, regiao) _ = s.updateFotExpenses(ctx, txn.FotID)
} }
return nil return nil
} }
@ -235,11 +253,8 @@ func (s *Service) SearchFot(ctx context.Context, query string, regiao string) ([
}) })
} }
func (s *Service) updateFotExpenses(ctx context.Context, fotID pgtype.UUID, regiao string) error { func (s *Service) updateFotExpenses(ctx context.Context, fotID pgtype.UUID) error {
total, err := s.queries.SumTotalByFot(ctx, generated.SumTotalByFotParams{ total, err := s.queries.SumTotalByFot(ctx, fotID)
FotID: fotID,
Regiao: pgtype.Text{String: regiao, Valid: true},
})
if err != nil { if err != nil {
return err return err
} }