 --todo: separate script/sproc to add period end (levy and payment scripts can add add as appropriate)
 -- make period end/dates align for payment gen
--- number of payments in month
 -- transfers
 
 -- DELETE THE TEMP STORED PROCEDURES IF THEY EXIST

BEGIN  
BEGIN TRANSACTION

--todo: use numberOfPayments

    DECLARE @periodEndDate DATETIME = DATEADD(month, -1, @createDate)

	--SELECT dbo.CalendarPeriodYear(@periodEndDate)

    @ukprn BIGINT,
    @courseName NVARCHAR(MAX),
	@periodEndDate DATETIME,
    @totalAmount DECIMAL(18,5),
	@numberOfPayments INT
)  
AS  
BEGIN  	

    DECLARE @paymentAmount DECIMAL(18,5) = @totalAmount / @numberOfPayments

	declare @apprentices table (row int, name varchar(100), uln bigint, id bigint)
	insert @apprentices values
	(1, 'Connie Lingus', 1234567890, 1111),
	(2, 'Martin du Bois', 2345678901, 2222),
	(3, 'Annaleigh Probin', 3456789012, 3333),
	(4, 'Jack Gough', 4567890123, 4444),
	(5, 'Juan Kerr', 5678901234, 5555)

	declare @name varchar(100)
	declare @uln bigint
	declare @id bigint

	while (@numberOfPayments > 0)
	BEGIN

	  SELECT @name = name, @uln = uln, @id = id
      FROM @apprentices
	  WHERE row = @numberOfPayments

      EXEC #createPayment @accountId, @providerName, @courseName, 1, @name, @ukprn, @uln, @id, 1, @paymentAmount, @periodEndDate

	  set @numberOfPayments = @numberOfPayments - 1
	END
END
GO

