Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.29 KB | None | 0 0
  1. CREATE PROCEDURE [dbo].SP_Corrige_Parcelas @idPagamentoVenda    INT,
  2.                                            @qtParcelas          INT,
  3.                                            @prTaxaAdministracao NUMERIC(9, 2)
  4. AS
  5.      IF(
  6.      (
  7.          SELECT COUNT(*) AS 'Linhas'
  8.          FROM card.tbParcela
  9.          WHERE idPagamentoVenda = @idPagamentoVenda
  10.      ) <> @qtParcelas)
  11.          BEGIN
  12.              DECLARE @iterator INT= 0;
  13.              DECLARE @cntCorrente INT=
  14.              (
  15.                  SELECT TOP 1 *
  16.                  FROM card.tbParcela
  17.                  WHERE idPagamentoVenda = @idPagamentoVenda
  18.              );
  19.              DELETE FROM card.tbParcela
  20.              WHERE idPagamentoVenda = @idPagamentoVenda;
  21.              WHILE @qtParcelas > @iterator
  22.                  BEGIN
  23.                      INSERT INTO card.tbParcela
  24.                      (idPagamentoVenda,
  25.                       nrParcela,
  26.                       idEmpresa,
  27.                       dtEmissao,
  28.                       dtVencimento,
  29.                       vlParcela,
  30.                       vlTaxaAdministracao,
  31.                       idContaCorrente,
  32.                       dtPagamento,
  33.                       vlPago,
  34.                       idStatusParcela,
  35.                       idMovimentoBanco
  36.                      )
  37.                      VALUES
  38.                      (
  39.                      (
  40.                          SELECT t1.idPagamentoVenda,
  41.                                 @iterator + 1,
  42.                                 t1.idEmpresa,
  43.                                 t1.dtEmissao,
  44.                                 CONVERT(VARCHAR, DATEADD(DAY, 30, CONVERT(DATE, t1.dtEmissao, 103)), 23),
  45.                                 t1.vlPagamento / @qtParcelas,
  46.                                 @prTaxaAdministracao,
  47.                                 @cntCorrente,
  48.                                 NULL,
  49.                                 NULL,
  50.                                 1,
  51.                                 NULL
  52.                          FROM card.tbPagamentoVenda t1
  53.                               INNER JOIN card.tbContaCorrente t2 ON t1.idEmpresa = t2.idEmpresa
  54.                          WHERE t1.idPagamentoVenda = @idPagamentoVenda
  55.                      )
  56.                      );
  57.      END;
  58.      END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement