ALTER PROCEDURE [dbo].[coc_ConfirmUploadRepayments]
-- Add the parameters for the stored procedure here
(@username nvarchar(500),
@computername nvarchar(50),
@compdomain nvarchar(50),
@ipadress nvarchar(50),
@Response nvarchar(50) OUTPUT)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
DECLARE @AmortizationId int;
DECLARE @MemberId nvarchar(50);
DECLARE @MemberNo nvarchar(50);
DECLARE @PaymentModeId nvarchar(50);
DECLARE @product nvarchar(500);
DECLARE @repaymentDate date;
DECLARE @PrincipalPortion decimal(18,2);
DECLARE @interestPortion decimal(18,2);
DECLARE @appliedInterest decimal(18,2);
DECLARE @LoanNumber nvarchar(50);
DECLARE @LoanId nvarchar(50);
DECLARE @PayableAmount decimal(18,2);
DECLARE @amountpaid decimal(18,2);
DECLARE @Penalties decimal(18,2);
DECLARE @AppliedPenalties decimal(18,2);
DECLARE @Date date;
DECLARE @ID int;
DECLARE @RefNo nvarchar(50);
DECLARE @ReceivedFrom nvarchar(50);
--DECLARE @TransactionCode nvarchar(50) = (SELECT [dbo].GetTransactionCode())
DECLARE uploadrepayments CURSOR
FOR
SELECT LoanRepaymentUploadId,MemberNo,LoanNo,Date,Amount,ReceivedBy,PaymentMethod FROM ACC0416 WHERE Confirmed=0
Open uploadrepayments
FETCH NEXT FROM uploadrepayments INTO @ID,@MemberNo,@LoanNumber,@repaymentDate,@amountpaid,@ReceivedFrom,@PaymentModeId
WHILE @@FETCH_STATUS<>-1
BEGIN
SELECT @LoanId = ((SELECT MAX(LoanId) FROM ACC0110)+1);
SELECT @MemberId =(SELECT MemberId FROM ACC0010 WHERE MemberNo=@MemberNo);
DECLARE @CountMemberProductId int=(SELECT COUNT(productid) FROM ACC0110 WHERE MemberId=@MemberId)
DECLARE @ReceiptNumber nvarchar(500) = (SELECT [dbo].GetReceiptNo())
DECLARE @TransactionCode nvarchar(50) = (SELECT [dbo].GetTransactionCode())
DECLARE @COUNTMemberID int=(SELECT COUNT(MemberId) FROM ACC0010 WHERE MemberNo=@MemberNo)
IF (@COUNTMemberID=1 OR @CountMemberProductId=1 )
BEGIN
SELECT @product =(SELECT productid FROM ACC0110 WHERE MemberId=@MemberId );
SELECT @AmortizationId=(SELECT AmortizationId FROM ACC0121 WHERE LoanNumber=@LoanNumber);
SELECT @PrincipalPortion=(SELECT PrincipalPortion FROM ACC0121 WHERE LoanNumber=@LoanNumber);
SELECT @interestPortion=(SELECT interestAmount FROM ACC0121 WHERE LoanNumber=@LoanNumber );
SELECT @appliedInterest=(SELECT appliedInterest FROM ACC0121 WHERE LoanNumber=@LoanNumber );
SELECT @PayableAmount=(SELECT TotalAMountPayable FROM ACC0121 WHERE LoanNumber=@LoanNumber );
SELECT @Penalties=(SELECT Penalties FROM ACC0121 WHERE LoanNumber=@LoanNumber );
SELECT @AppliedPenalties=(SELECT AppliedPenalties FROM ACC0121 WHERE LoanNumber=@LoanNumber );
INSERT INTO ACC0122 (PaymentModeId,AmortizationId,MemberId,Product,LoanNumber,PrincipalPortion,Interestportion,AppliedInterest,Penalty,Appliedpenalty,PayableAmount,PaidAmount,DatePaid,ReceiptNumber,ReferenceNumber,ReceivedFrom)
VALUES (@PaymentModeId,@AmortizationId,@MemberId,@product,@LoanNumber,@PrincipalPortion,@interestPortion,@appliedInterest,@Penalties,@AppliedPenalties,@PayableAmount,@amountpaid,@Date,@ReceiptNumber,@RefNo,@ReceivedFrom)
INSERT INTO ACC0229 (LoanNumber,MemberId,PrincipalPortion,Interestportion,Penalties,PrincipalDebitCOAID,PrincipalCreditCOAID,InterestDebitCOAID,InterestCreditCOAID,PenaltiesDebitCOAID,PenaltiesCreditCOAID,DateRepaid,Posted)
VALUES(@LoanNumber,@MemberId,@PrincipalPortion,@interestPortion,@Penalties,99,57,99,4,99,7,@Date,0)
UPDATE ACC0121 SET AmountPaid=@amountpaid,AppliedPenalties=@AppliedPenalties,AppliedInterest=@appliedInterest WHERE AmortizationId=@AmortizationId
SET @Response='Successfully posted'
--New Loan Balance sql
--declare @bal decimal(18,3)=(select sum(paidamount) as PaidAmount1 from acc0122 where loannumber=@LoanNumber)-(select sum(paidamount) as PaidAmount1 from acc0122 where loannumber=@LoanNumber)
--update ACC0121 set LoanBalance=@bal where LoanNumber=@LoanNumber and AmortizationId=@AmortizationId
END
ELSE
BEGIN
SET @Response='Some members not registered or do not have respective accounts'
END
FETCH NEXT FROM uploadrepayments INTO @ID,@MemberNo,@LoanNumber,@repaymentDate,@amountpaid,@ReceivedFrom,@PaymentModeId,@product
END
CLOSE uploadsavings
DEALLOCATE uploadrepayments
EXECUTE Coc_TrackUser_400 'CONFIRMED LOAN REPAYMENTS',@TransactionCode,@username,@computername,@compdomain,@ipadress
SET @Response='Successfully confirmed'
END
这是我的存储过程。它使用CURSOR uploadrepayments从另一个表中加载大部分信息。然后遍历数据并将数据插入表ACC0122,ACC0229和ACC0121。