Exemple #1
0
        private Model.Invoice createInvoiceFromEngagement(Model.BillingType?billingType = null)
        {
            Regex rgx  = new Regex("[^a-zA-Z0-9 -]");
            var   name = this._engagement.Name;

            name = rgx.Replace(name, "");
            name = Regex.Replace(name, @"[\d-]", "");
            name = Regex.Replace(name, @"\s+", "");
            name = name.Substring(0, 3).ToUpper();
            name = name + this._engagement.ClientId.ToString().Substring(0, 3).ToUpper();
            name = name + this._generator.BillingDate.ToString("yy") + this._generator.BillingDate.ToString("MM");

            var invoice = new Model.Invoice();

            invoice.Name     = name;
            invoice.BilledOn = this._generator.BillingDate.AddHours(12);

            if (this._engagement.HasParentEngagement == true)
            {
                invoice.StartDate = DateTime.SpecifyKind(this._engagement.GetInvoicePeriodStartDate(this._generator.BillingDate, this._isNewEngagement, billingType, "Quarterly"), DateTimeKind.Utc).AddHours(12);
                invoice.EndDate   = DateTime.SpecifyKind(this._engagement.GetInvoicePeriodEndDate(this._generator.BillingDate, this._isNewEngagement, billingType), DateTimeKind.Utc).AddHours(12);
            }
            else
            {
                invoice.StartDate = DateTime.SpecifyKind(this._engagement.GetInvoicePeriodStartDate(this._generator.BillingDate, this._isNewEngagement, billingType), DateTimeKind.Utc).AddHours(12);
                invoice.EndDate   = DateTime.SpecifyKind(this._engagement.GetInvoicePeriodEndDate(this._generator.BillingDate, this._isNewEngagement, billingType), DateTimeKind.Utc).AddHours(12);
            }

            invoice.EarnedOn  = invoice.EndDate;
            invoice.DaysToPay = this._engagement.GetDaysToPay();
            invoice.GeneralLedgerAccountId = this._engagement.GetGeneralLedgerAccountId();
            invoice.EngagementId           = this._engagement.Id;
            invoice.ClientId = this._engagement.ClientId;
            return(invoice);
        }
Exemple #2
0
        private Model.Invoice modifyForInArrearsTermination(Model.Invoice invoice)
        {
            var terminationDate = this._engagementTerminationDate.Date;

            if (this._engagementProductType.BillingFrequency == "Quarterly")
            {
                var beginQuarterMonths = new int[] { 1, 4, 7, 10 };
                var endQuarterMonths   = new int[] { 3, 6, 9, 12 };
                var beginingOfMonth    = terminationDate.Day == 1;
                var endOfMonth         = terminationDate.Day == DateTime.DaysInMonth(terminationDate.Year, terminationDate.Month);

                if (beginingOfMonth && beginQuarterMonths.Contains(terminationDate.Month) == true)
                {
                }
                else if (endOfMonth && endQuarterMonths.Contains(terminationDate.Month) == true)
                {
                }
                else
                {
                    //terminationDate = terminationDate.AddDays(-1);
                }
            }
            else if (this._engagementProductType.BillingFrequency == "Monthly")
            {
                var beginingOfMonth = terminationDate.Day == 1;
                var endOfMonth      = terminationDate.Day == DateTime.DaysInMonth(terminationDate.Year, terminationDate.Month);

                if (beginingOfMonth == false && endOfMonth == false)
                {
                    //terminationDate = terminationDate.AddDays(-1);
                }
            }
            else
            {
                if (terminationDate.Day == 1 && terminationDate.Month == 1)
                {
                }
                else if (terminationDate.Day == 31 && terminationDate.Month == 12)
                {
                }
                else
                {
                    //terminationDate = terminationDate.AddDays(-1);
                }
            }

            var typicalInvoiceFee = Calculator.CalculateInvoiceFee(this._annualFee, this._engagementProductType, this._generator.BillingDate, this._engagementEffectiveDate, false, this._isNewEngagement, this._engagementEffectiveDate);
            var invoiceCredit     = Calculator.CalculateInvoiceCredit(this._isTerminatedEngagement, typicalInvoiceFee, terminationDate, this._engagementProductType);

            invoice.InvoiceFee    = typicalInvoiceFee - invoiceCredit;
            invoice.InvoiceCredit = 0;
            return(invoice);
        }
Exemple #3
0
        /// <summary>
        /// Verifies that there should be an invoice given the invoice period, and
        /// modifies vendor search invoice fee where applicable.
        /// </summary>
        /// <param name="invoice"></param>
        /// <returns></returns>
        /// <remarks>Kind of an awkward method. Needs refactoring.</remarks>
        private bool isInvalidInvoicePeriodForEngagement(Model.Invoice invoice)
        {
            var productType = this._engagement.GetProductTypeDetail();

            // Vendor Search
            if (this._engagement.ProductType == 11)
            {
                if (this._engagement.ContractTerminationDate.Date > invoice.StartDate.Date)
                {
                    if (invoice.StartDate.Date <= this._engagement.EffectiveDate.Date && invoice.EndDate.Date > this._engagement.EffectiveDate.Date)
                    {
                        invoice.InvoiceFee = invoice.AnnualFee / 2m;
                        return(false);
                    }
                    else if (invoice.StartDate.Date <= this._engagement.EffectiveDate.Date.AddMonths(5) && invoice.EndDate.Date > this._engagement.EffectiveDate.Date.AddMonths(5))
                    {
                        invoice.InvoiceFee = invoice.AnnualFee / 2m;
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                else
                {
                    return(true);
                }
            }
            else if (productType.IsTieredRate == false && productType.IsServiceOngoing == false) // other services and projects
            {
                var endOfQuarterMonths = new int[] { 3, 6, 9, 12 };
                if (this._engagement.HasParentEngagement == true && endOfQuarterMonths.Contains(invoice.BilledOn.Month) == false)
                {
                    return(true);
                }
            }

            return(false);
        }
        private void _createInvoiceRecordInCrm(Model.Invoice invoice, List <CRM.Model.Invoice> currentInvoices)
        {
            // insert invoice record into crm with invoice credit line item if applicable
            // update invoice record if it already exists, don't update if statusreason is locked
            // if invoice fee is 0 and invoice credit is 0, don't create invoice
            var crmInvoice = new CRM.Model.Invoice(invoice);

            var currentInvoice = currentInvoices
                                 .FindAll(x => x.EngagementId == invoice.EngagementId)
                                 .Find(x => x.BillingType == (int)invoice.BillingType);

            if (invoice.InvoiceFee + invoice.InvoiceCredit + invoice.LineItems.Count <= 0)
            {
                return;
            }
            else if (currentInvoice != null && currentInvoice.StatusReason == 100000000)
            {
                return;
            }
            else if (currentInvoice != null)
            {
                crmInvoice.Id = currentInvoice.Id;
            }

            var isNew = false;

            if (crmInvoice.Id == Guid.Empty)
            {
                isNew         = true;
                crmInvoice.Id = Guid.NewGuid();
            }

            CRM.Data.Invoice.Save(crmInvoice, isNew);

            this._createInvoiceLineItemRecordsInCrm(crmInvoice, invoice);
            this._createGlaInvoiceTeamSplitRecordsInCrm(crmInvoice, invoice);
        }
        private void _createGlaInvoiceTeamSplitRecordsInCrm(CRM.Model.Invoice crmInvoice, Model.Invoice invoice)
        {
            var engagement     = this.Engagements.Find(x => x.Id == crmInvoice.EngagementId);
            var splitsAccounts = this.GlaInvoiceTeamSplitsAccounts
                                 .FindAll(x => x.AccountId == engagement.ClientId)
                                 .FindAll(x => x.ProductType == engagement.ProductType)
                                 .FindAll(x => x.StartDate.Date <= ((DateTime)crmInvoice.EndDate).Date)
                                 .FindAll(x => x.EndDate.Date > ((DateTime)crmInvoice.StartDate).Date);

            foreach (var split in splitsAccounts)
            {
                var splitsInvoice = this.GlaInvoiceTeamSplitsInvoices
                                    .FindAll(x => x.InvoiceId == crmInvoice.Id)
                                    .FindAll(x => x.GeneralLedgerAccountId == split.GeneralLedgerAccountId || (x.GeneralLedgerAccountId == Guid.Empty && split.GeneralLedgerAccountId == Guid.Empty))
                                    .Find(x => x.StartDate.Date == split.StartDate.Date || x.StartDate.Date == ((DateTime)crmInvoice.StartDate).Date);

                var isNew = true;
                if (splitsInvoice != null)
                {
                    split.Id = splitsInvoice.Id;
                    isNew    = false;
                }

                var totalSplit = split.TotalSplit;
                var startDate  = split.StartDate;
                if (split.StartDate.Date <= ((DateTime)crmInvoice.StartDate).Date)
                {
                    split.StartDate = (DateTime)crmInvoice.StartDate;
                }
                else
                {
                    var coveredDays = 1 + (decimal)(((DateTime)crmInvoice.EndDate).Date - split.StartDate.Date).TotalDays;
                    var totalDays   = 1 + (decimal)(((DateTime)crmInvoice.EndDate).Date - ((DateTime)crmInvoice.StartDate).Date).TotalDays;
                    split.TotalSplit = (coveredDays / totalDays) * 100.00m;
                }

                var endDate = split.EndDate;
                if (split.EndDate.Date >= ((DateTime)crmInvoice.EndDate).Date)
                {
                    split.EndDate = (DateTime)crmInvoice.EndDate;
                }
                else
                {
                    var coveredDays = 1 + (decimal)(split.EndDate.Date - ((DateTime)crmInvoice.StartDate).Date).TotalDays;
                    var totalDays   = 1 + (decimal)(((DateTime)crmInvoice.EndDate).Date - ((DateTime)crmInvoice.StartDate).Date).TotalDays;
                    split.TotalSplit = (coveredDays / totalDays) * 100.00m;
                }

                var accountId         = split.AccountId;
                var invoiceId         = split.InvoiceId;
                var invoiceLineItemId = split.InvoiceLineItemId;

                split.InvoiceId         = crmInvoice.Id;
                split.AccountId         = Guid.Empty;
                split.InvoiceLineItemId = Guid.Empty;
                CRM.Data.GlaInvoiceTeamSplit.Save(split, isNew);

                split.AccountId         = accountId;
                split.InvoiceId         = invoiceId;
                split.InvoiceLineItemId = invoiceLineItemId;
                split.StartDate         = startDate;
                split.EndDate           = endDate;
                split.TotalSplit        = totalSplit;
            }
        }
        private void _createInvoiceLineItemRecordsInCrm(CRM.Model.Invoice crmInvoice, Model.Invoice invoice)
        {
            var crmLineItems = CRM.Data.InvoiceLineItem.Retrieve(crmInvoice);

            foreach (var lineItem in invoice.LineItems)
            {
                var crmLineItem = new CRM.Model.InvoiceLineItem(lineItem);
                crmLineItem.CustomerInvoiceId = crmInvoice.Id;

                var matchingLineItem = crmLineItems
                                       .FindAll(x => x.LineItemType == crmLineItem.LineItemType)
                                       .Find(x => x.Name == crmLineItem.Name);

                if (matchingLineItem != null)
                {
                    crmLineItem.Id = matchingLineItem.Id;
                }

                CRM.Data.InvoiceLineItem.Save(crmLineItem);
                this._createGlaInvoiceLineItemTeamSplitRecordsInCrm(crmInvoice, crmLineItem, lineItem);
            }
        }