Exemple #1
0
 /// <summary>
 /// Creates a new record if ID is empty and updates existing records.
 /// </summary>
 /// <param name="invoice"></param>
 public static void Save(Model.Invoice invoice, bool isNew = false)
 {
     try
     {
         if (invoice.Id == Guid.Empty || isNew == true)
         {
             invoice.Id = Guid.NewGuid();
             Globals.CrmServiceBroker.Service.Create(invoice.GetDynamicEntity());
         }
         else
         {
             Globals.CrmServiceBroker.Service.Update(invoice.GetDynamicEntity());
         }
     }
     catch (SoapException soapEx)
     {
         Console.WriteLine("SoapException: " + soapEx.Detail);
         Console.WriteLine("SoapException: " + soapEx.Message);
     }
 }
        public static List <Model.InvoiceLineItem> Retrieve(Model.Invoice invoice)
        {
            var criteria = new FilterExpression();

            criteria.AddCondition("new_customerinvoiceid", ConditionOperator.Equal, invoice.Id);

            var request = Globals.GetRetrieveMultipleRequest(_tableName, criteria);
            var retrieveMultipleResponse = Globals.CrmServiceBroker.ExecuteRetrieveMultipleRequest(request);
            var businessEntityCollection = retrieveMultipleResponse.BusinessEntityCollection;

            var result = new List <Model.InvoiceLineItem>();

            foreach (var businessEntity in businessEntityCollection.BusinessEntities)
            {
                var dynamicEntity   = (DynamicEntity)businessEntity;
                var invoiceLineItem = new CRM.Model.InvoiceLineItem(dynamicEntity);
                result.Add(invoiceLineItem);
            }

            return(result.FindAll(x => x.CustomerInvoiceId == invoice.Id));
        }
        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);
            }
        }
        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 _createGlaInvoiceLineItemTeamSplitRecordsInCrm(CRM.Model.Invoice crmInvoice, CRM.Model.InvoiceLineItem crmInvoiceLineItem, Model.InvoiceLineItem invoiceLineItem)
        {
            var engagement = this.Engagements.Find(x => x.Id == crmInvoice.EngagementId);

            if (engagement.IsOngoingEngagement() && invoiceLineItem.LineItemType == Model.LineItemType.Fee)
            {
                var split = new CRM.Model.GlaInvoiceTeamSplit();
                split.InvoiceLineItemId = crmInvoiceLineItem.Id;
                split.StartDate         = (DateTime)crmInvoiceLineItem.StartDate;
                split.EndDate           = (DateTime)crmInvoiceLineItem.EndDate;
                split.TotalSplit        = 100.00m;
                split.ProductType       = engagement.ProductType;

                if (invoiceLineItem.IsWorkplace && invoiceLineItem.TeamId == new Guid("9FB7C526-7EAF-E311-B765-D8D385C29900"))                            // Workplace && RetireAdvisers
                {
                    if (invoiceLineItem.ApprovedOn == null || ((DateTime)invoiceLineItem.ApprovedOn).Date.AddMonths(12) > invoiceLineItem.StartDate.Date) // IsNew
                    {
                        split.GeneralLedgerAccountId = this.GeneralLedgerAccounts.Find(x => x.QuickBooksAccountNumber.Equals("4042")).Id;
                    }
                    else // IsLegacy
                    {
                        split.GeneralLedgerAccountId = this.GeneralLedgerAccounts.Find(x => x.QuickBooksAccountNumber.Equals("4046")).Id;
                    }
                }
                else
                {
                    switch (invoiceLineItem.TeamId.ToString().ToUpper())
                    {
                    case "7671B7ED-E4BA-DB11-9697-001143F10531":     //ERISA
                        split.GeneralLedgerAccountId = this.GeneralLedgerAccounts.Find(x => x.QuickBooksAccountNumber.Equals("4020")).Id;
                        break;

                    case "7287DE04-E5BA-DB11-9697-001143F10531":     //Investment
                        split.GeneralLedgerAccountId = this.GeneralLedgerAccounts.Find(x => x.QuickBooksAccountNumber.Equals("4030")).Id;
                        break;

                    case "9FB7C526-7EAF-E311-B765-D8D385C29900":     //RetireAdvisers
                        split.GeneralLedgerAccountId = this.GeneralLedgerAccounts.Find(x => x.QuickBooksAccountNumber.Equals("4040")).Id;
                        break;

                    case "F8ECF41B-E5BA-DB11-9697-001143F10531":     //Vendor
                        split.GeneralLedgerAccountId = this.GeneralLedgerAccounts.Find(x => x.QuickBooksAccountNumber.Equals("4050")).Id;
                        break;
                    }
                }

                var splitsInvoice = (CRM.Model.GlaInvoiceTeamSplit) this.GlaInvoiceTeamSplitsInvoiceLineItems
                                    .FindAll(x => x.InvoiceLineItemId == crmInvoiceLineItem.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;
                }

                CRM.Data.GlaInvoiceTeamSplit.Save(split, isNew);
            }
            else
            {
                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 = (CRM.Model.GlaInvoiceTeamSplit) this.GlaInvoiceTeamSplitsInvoiceLineItems
                                        .FindAll(x => x.InvoiceLineItemId == crmInvoiceLineItem.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 startDate = split.StartDate;
                    if (split.StartDate.Date <= ((DateTime)crmInvoice.StartDate).Date)
                    {
                        split.StartDate = (DateTime)crmInvoice.StartDate;
                    }
                    else
                    {
                        var coveredDays = (decimal)(((DateTime)crmInvoice.EndDate).Date - split.StartDate.Date).TotalDays;
                        var totalDays   = (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 = (decimal)(split.EndDate.Date - ((DateTime)crmInvoice.StartDate).Date).TotalDays;
                        var totalDays   = (decimal)(((DateTime)crmInvoice.EndDate).Date - ((DateTime)crmInvoice.StartDate).Date).TotalDays;
                        split.TotalSplit = (coveredDays / totalDays) * 100.00m;
                    }

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

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

                    split.AccountId = accountId;
                    split.InvoiceId = invoiceId;
                    split.StartDate = startDate;
                    split.EndDate   = endDate;
                }
            }
        }