Esempio n. 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.GlaInvoiceTeamSplit split, bool isNew = false)
 {
     try
     {
         if (split.Id == Guid.Empty || isNew == true)
         {
             split.Id = Guid.NewGuid();
             Globals.CrmServiceBroker.Service.Create(split.GetDynamicEntity());
         }
         else
         {
             Globals.CrmServiceBroker.Service.Update(split.GetDynamicEntity());
         }
     }
     catch (SoapException soapEx)
     {
         Console.WriteLine("SoapException: " + soapEx.Detail);
         Console.WriteLine("SoapException: " + soapEx.Message);
     }
 }
        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;
                }
            }
        }