I take that back, it's only processing the first record in the .csv file, the .csv file looks like this:
This invoice has two separate fees one for $1 and one for $2 that need their fee payments made individually then the invoice Balance updated.
This is my current code. Any help is appreciated.
CREATE TABLE #tempTable1
(
PERMIT_INS VARCHAR(50),
AMOUNT_PD DECIMAL(10,2),
DATE_PAID DATETIME,
NAME VARCHAR(150)
)
BULK INSERT #tempTable1
FROM 'D:\Frederick County Fee Import Test\FrederickCounty.csv'
WITH
(
FirstRow = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
);
--c1 = Invoices Joined with the temp table.
DECLARE @InvoiceId uniqueidentifier;
DECLARE @BillToPartyId uniqueidentifier;
DECLARE @InvoiceNumber varchar(50);
DECLARE @RecordId uniqueidentifier;
DECLARE @InvoiceBalance DECIMAL(10,2);
DECLARE @apl _PAID date;
DECLARE @AMOUNT_PD money;
DECLARE @DeezNutz varchar(20);
DECLARE c1 CURSOR FOR
SELECT INV.InvoiceId, INV.BillToPartyId, INV.InvoiceNumber, INV.RecordId, temp.DATE_PAID, temp.AMOUNT_PD, temp.NAME
FROM Invoices AS INV
INNER JOIN #tempTable1 AS temp ON temp.PERMIT_INS = INV.InvoiceNumber
OPEN c1
FETCH NEXT FROM c1 INTO @InvoiceId, @BillToPartyId, @InvoiceNumber, @RecordId, @apl _PAID, @AMOUNT_PD, @DeezNutz ;
WHILE @@Fetch _STATUS = 0
BEGIN
-- Invoice Payments ..
Declare @NewId uniqueidentifier;
Set @NewId = NewId();
INSERT INTO InvoicePayments(PaymentId, InvoiceId, PaymentUserId, PaymentDate, PaymentAmount, PaymentType)
VALUES(@NewId, @InvoiceId, @BillToPartyId, @apl _PAID, @AMOUNT_PD, 'Credit Card');
Declare @FeeId uniqueidentifier;
DECLARE @FeeAmt money;
DECLARE @PaymentAmt money;
Declare @PaymentDate date;
--c1 = Fees on the invoice.
DECLARE c2 CURSOR FOR
Select FeeId, FeeAmt, PaymentAmt, PaymentDate FROM FEES Where InvoiceId = @InvoiceId Order By FeeDate
OPEN c2
FETCH NEXT FROM c2 INTO @FeeId, @FeeAmt, @PaymentAmt, @PaymentDate;
WHILE @@Fetch _STATUS = 0
BEGIN
Set @PaymentDate = GetDate();
Declare @RemainingAmt DECIMAL(10,2)
Declare @InvoiceAmt DECIMAL(10,2)
Set @InvoiceAmt = @AMOUNT_PD
Set @RemainingAmt = @InvoiceAmt - @FeeAmt
-- Insert FeePayments ...
Insert Into FeePayments (FeeId, PaymentDate, PaymentAmt, PaymentType, PaymentUserId, InvoicePaymentId)
Values (@FeeId, @PaymentDate, @Amount_PD, 'Credit Card', @BillToPartyId, @NewId);
-- Update Fees ...
Update Fees Set PaymentDate = @apl _PAID, PaymentAmt = @FeeAmt, PaymentUserId = @BillToPartyId, DateUpdated = @PaymentDate Where FeeId = @FeeId;
IF(@RemainingAmt=0)
BEGIN
Update Invoices Set InvoiceBalance = @AMOUNT_PD, DateUpdated = @PaymentDate Where InvoiceId = @InvoiceId;
END
FETCH NEXT FROM c2 INTO @FeeId, @FeeAmt, @PaymentAmt, @PaymentDate;
END
CLOSE c2;
DEALLOCATE c2;
FETCH NEXT FROM c1 INTO @InvoiceId, @BillToPartyId, @InvoiceNumber, @RecordId, @apl _PAID, @AMOUNT_PD, @DeezNutz ;
END
CLOSE c1;
DEALLOCATE c1;
drop table #tempTable1
The resulting query posts this:
(2 rows affected)
(1 row affected)
(1 row affected)
(1 row affected)
(1 row affected)
(1 row affected)