/// <summary>
		/// Takes the ammount to allocate then provides a recommendation of where to allocate the
		/// charges to.  Returns a DataTable with the following Collumns:
		///		Provider, Ammount
		/// 
		/// The idea being that the calling method will have the payment in their hand and will
		/// use the recommendation to generate the appropriate PROVIDER paysplit info.  So the calling
		/// method will have the Date of Payment, Paytable source etc.
		/// </summary>
		/// <param name="AmountToAllocate">Should be -ve because it is a payment.</param>
		/// <returns></returns>
		public DataTable NextPaySplitRecommendation(decimal AmountToAllocate)//(uint uGuarantor, decimal AmountToAllocate)
		{
			DataTable rVal = new DataTable();
			if (!Ledger.IS_FILLED)
				Ledger.Fill(false);
			if (!Ledger.IS_EQUALIZED)
				Ledger.EqualizePaymentsV2();
			if (!Ledger.IS_FILLED || !Ledger.IS_EQUALIZED)
			{
				//dt.Columns.Add("ERROR");
				//dt.Rows.Add(dt.NewRow()[0] = "Error Getting table filled");
				return null;
			}
			List<ProviderPayment> psiList = new List<ProviderPayment>();
			ProviderPayment curPayItem = new ProviderPayment();
			curPayItem.Provider = -1;
			

			decimal remainingAmmount = AmountToAllocate;
			for (int i= 0; i < Ledger.Charges.Count; i++)
			{
				if (Ledger.Charges[i].AmtUnallocated > 0)
				{
					// if first time thru create curPayItem
					if (curPayItem.Provider == -1)
					{

						curPayItem = new ProviderPayment();
						curPayItem.Provider = Ledger.Charges[i].PROVNUM;
					}
					// if 2+ thru and ProvNums don't match create a new curPayItem
					if (curPayItem.Provider != Ledger.Charges[i].PROVNUM)
					{
						psiList.Add(curPayItem);
						curPayItem = new ProviderPayment();
						curPayItem.Provider = Ledger.Charges[i].PROVNUM;
					}
					// Determin what ammount to add
					if (Ledger.Charges[i].AmtUnallocated <= remainingAmmount)
					{
						curPayItem.Ammount += Ledger.Charges[i].AmtUnallocated;
						remainingAmmount -= Ledger.Charges[i].AmtUnallocated;
					}
					else
					{
						curPayItem.Ammount += remainingAmmount;
						remainingAmmount =0;
					}
					if (remainingAmmount == 0) i = Ledger.Charges.Count; // end loop
				}

			}

			/// Scenarios:  1:  Partial Ammount was allocated.  But curPayItem not recorded
			///				2:	No Ammount was allocated so a curPayItem needs recorded
			///				for 1:  Need to recored  curPayItem and Generate a new CurPayItem with no provider for the remaining
			if (remainingAmmount != AmountToAllocate)
			{
				// allocation occurred but not saved
				psiList.Add(curPayItem);
				// any remaining ammount?
				if (remainingAmmount != 0)
				{
					curPayItem = new ProviderPayment();
					curPayItem.Provider = -1;
					curPayItem.Ammount = remainingAmmount;
					remainingAmmount = 0;
					psiList.Add(curPayItem);
				}
			}
			else // No allocation occurred
			{
				curPayItem = new ProviderPayment();
				curPayItem.Provider = -1;
				curPayItem.Ammount = remainingAmmount;
				remainingAmmount = 0;
				psiList.Add(curPayItem);
			}
			// last item created was not added.

			/// Generate the returning table.
			DataColumn dc1 = new DataColumn("Provider", typeof(uint));
			DataColumn dc2 = new DataColumn("Ammount", typeof(decimal));
			rVal.Columns.Add(dc1);
			rVal.Columns.Add(dc2);

			foreach (ProviderPayment pi in psiList)
			{
				DataRow dr = rVal.NewRow();
				if (pi.Provider < 0)
					dr[0] = 0;
				else
					dr[0] = pi.Provider;
				dr[1] = pi.Ammount;
				rVal.Rows.Add(dr);
			}
			return rVal;

		}
Пример #2
0
        /// <summary>
        /// Takes the ammount to allocate then provides a recommendation of where to allocate the
        /// charges to.  Returns a DataTable with the following Collumns:
        ///		Provider, Ammount
        ///
        /// The idea being that the calling method will have the payment in their hand and will
        /// use the recommendation to generate the appropriate PROVIDER paysplit info.  So the calling
        /// method will have the Date of Payment, Paytable source etc.
        /// </summary>
        /// <param name="AmountToAllocate">Should be -ve because it is a payment.</param>
        /// <returns></returns>
        public DataTable NextPaySplitRecommendation(decimal AmountToAllocate)        //(uint uGuarantor, decimal AmountToAllocate)
        {
            DataTable rVal = new DataTable();

            if (!Ledger.IS_FILLED)
            {
                Ledger.Fill(false);
            }
            if (!Ledger.IS_EQUALIZED)
            {
                Ledger.EqualizePaymentsV2();
            }
            if (!Ledger.IS_FILLED || !Ledger.IS_EQUALIZED)
            {
                //dt.Columns.Add("ERROR");
                //dt.Rows.Add(dt.NewRow()[0] = "Error Getting table filled");
                return(null);
            }
            List <ProviderPayment> psiList    = new List <ProviderPayment>();
            ProviderPayment        curPayItem = new ProviderPayment();

            curPayItem.Provider = -1;


            decimal remainingAmmount = AmountToAllocate;

            for (int i = 0; i < Ledger.Charges.Count; i++)
            {
                if (Ledger.Charges[i].AmtUnallocated > 0)
                {
                    // if first time thru create curPayItem
                    if (curPayItem.Provider == -1)
                    {
                        curPayItem          = new ProviderPayment();
                        curPayItem.Provider = Ledger.Charges[i].PROVNUM;
                    }
                    // if 2+ thru and ProvNums don't match create a new curPayItem
                    if (curPayItem.Provider != Ledger.Charges[i].PROVNUM)
                    {
                        psiList.Add(curPayItem);
                        curPayItem          = new ProviderPayment();
                        curPayItem.Provider = Ledger.Charges[i].PROVNUM;
                    }
                    // Determin what ammount to add
                    if (Ledger.Charges[i].AmtUnallocated <= remainingAmmount)
                    {
                        curPayItem.Ammount += Ledger.Charges[i].AmtUnallocated;
                        remainingAmmount   -= Ledger.Charges[i].AmtUnallocated;
                    }
                    else
                    {
                        curPayItem.Ammount += remainingAmmount;
                        remainingAmmount    = 0;
                    }
                    if (remainingAmmount == 0)
                    {
                        i = Ledger.Charges.Count;                                            // end loop
                    }
                }
            }

            /// Scenarios:  1:  Partial Ammount was allocated.  But curPayItem not recorded
            ///				2:	No Ammount was allocated so a curPayItem needs recorded
            ///				for 1:  Need to recored  curPayItem and Generate a new CurPayItem with no provider for the remaining
            if (remainingAmmount != AmountToAllocate)
            {
                // allocation occurred but not saved
                psiList.Add(curPayItem);
                // any remaining ammount?
                if (remainingAmmount != 0)
                {
                    curPayItem          = new ProviderPayment();
                    curPayItem.Provider = -1;
                    curPayItem.Ammount  = remainingAmmount;
                    remainingAmmount    = 0;
                    psiList.Add(curPayItem);
                }
            }
            else             // No allocation occurred
            {
                curPayItem          = new ProviderPayment();
                curPayItem.Provider = -1;
                curPayItem.Ammount  = remainingAmmount;
                remainingAmmount    = 0;
                psiList.Add(curPayItem);
            }
            // last item created was not added.

            /// Generate the returning table.
            DataColumn dc1 = new DataColumn("Provider", typeof(uint));
            DataColumn dc2 = new DataColumn("Ammount", typeof(decimal));

            rVal.Columns.Add(dc1);
            rVal.Columns.Add(dc2);

            foreach (ProviderPayment pi in psiList)
            {
                DataRow dr = rVal.NewRow();
                if (pi.Provider < 0)
                {
                    dr[0] = 0;
                }
                else
                {
                    dr[0] = pi.Provider;
                }
                dr[1] = pi.Ammount;
                rVal.Rows.Add(dr);
            }
            return(rVal);
        }