Proc Subway Invoice UpdateCollectedAmount

Purpose
This procedure inserts a new record or updates an existing record in the table WeeklySummary. It was made into its own stored procedure so it could be called when creating Invoices, Cash Receipts, or Beginning Balances

Method Signature
[dbo].[proc_Subway_Invoice_UpdateCollectedAmount] @EntityType AS INT ,	@EntityId AS INT ,	@BalanceId AS INT ,	@AssociatedBalanceId AS INT ,	@Amount AS DECIMAL(17,2)

Procedure Flow

 * 1) Determine the Adjustment Type for the Entity and Balance Type. The Adjustment Type will determine the sort order for how Invoices are paid off. It is possible that an Entity and Balance Type
 * 2) If the Adjustment Type is Balance, Fixed or Manual sort the Invoices by Posting Date from earliest to latest and apply the cash in that order.
 * 3) If the Adjustment Type is Current only look at the Invoices for the current Week Ending date and sort by Posting Date from earliest to latest and apply the cash in that order.
 * 4) If the Adjustment Type is Royalties only look at the Invoices for the Item Type is Weekly Charge and sort by Posting Date from earliest to latest and apply the cash in that order.
 * 5) If the Adjustment Type is Interest only look at the Invoices for the Item Type is Note Interest and sort by Posting Date from earliest to latest and apply the cash in that order.
 * 6) If the Adjustment Type is Interest Plus sort the Invoices by Note Interest and then all other invoice types and then by Posting Date from earliest to latest and apply the cash in that order.
 * 7) The individual records are updated in the order they were set until the amount applied is equal to the amount passed in OR there are no more invoices to apply the amount to.  The amount applied to each record can be up to but not greater than the Item Amount.