Ejemplo n.º 1
0
 public static void AppsOnDeriveActiveCustomer(this Party party, IDerivation derivation)
 {
     foreach (CustomerRelationship customerRelationship in party.CustomerRelationshipsWhereCustomer)
     {
         if (party.AppsIsActiveCustomer(customerRelationship.InternalOrganisation, DateTime.UtcNow))
         {
             customerRelationship.InternalOrganisation.AddActiveCustomer(party);
         }
         else
         {
             customerRelationship.InternalOrganisation.RemoveActiveCustomer(party);
         }
     }
 }
Ejemplo n.º 2
0
        public static void AppsOnDerive(this Party @this, ObjectOnDerive method)
        {
            var derivation = method.Derivation;

            @this.BillingAddress        = null;
            @this.BillingInquiriesFax   = null;
            @this.BillingInquiriesPhone = null;
            @this.CellPhoneNumber       = null;
            @this.GeneralFaxNumber      = null;
            @this.GeneralPhoneNumber    = null;
            @this.HeadQuarter           = null;
            @this.HomeAddress           = null;
            @this.InternetAddress       = null;
            @this.OrderAddress          = null;
            @this.OrderInquiriesFax     = null;
            @this.OrderInquiriesPhone   = null;
            @this.PersonalEmailAddress  = null;
            @this.SalesOffice           = null;
            @this.ShippingAddress       = null;
            @this.ShippingInquiriesFax  = null;
            @this.ShippingAddress       = null;

            foreach (PartyContactMechanism partyContactMechanism in @this.PartyContactMechanisms)
            {
                if (partyContactMechanism.UseAsDefault)
                {
                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).BillingAddress))
                    {
                        @this.BillingAddress = partyContactMechanism.ContactMechanism;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).BillingInquiriesFax))
                    {
                        @this.BillingInquiriesFax = partyContactMechanism.ContactMechanism as TelecommunicationsNumber;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).BillingInquiriesPhone))
                    {
                        @this.BillingInquiriesPhone = partyContactMechanism.ContactMechanism as TelecommunicationsNumber;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).MobilePhoneNumber))
                    {
                        @this.CellPhoneNumber = partyContactMechanism.ContactMechanism as TelecommunicationsNumber;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).GeneralCorrespondence))
                    {
                        @this.GeneralCorrespondence = partyContactMechanism.ContactMechanism as PostalAddress;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).GeneralEmail))
                    {
                        @this.GeneralEmail = partyContactMechanism.ContactMechanism as EmailAddress;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).GeneralFaxNumber))
                    {
                        @this.GeneralFaxNumber = partyContactMechanism.ContactMechanism as TelecommunicationsNumber;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).GeneralPhoneNumber))
                    {
                        @this.GeneralPhoneNumber = partyContactMechanism.ContactMechanism as TelecommunicationsNumber;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).HeadQuarters))
                    {
                        @this.HeadQuarter = partyContactMechanism.ContactMechanism;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).HomeAddress))
                    {
                        @this.HomeAddress = partyContactMechanism.ContactMechanism;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).InternetAddress))
                    {
                        @this.InternetAddress = partyContactMechanism.ContactMechanism as ElectronicAddress;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).OrderAddress))
                    {
                        @this.OrderAddress = partyContactMechanism.ContactMechanism;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).OrderInquiriesFax))
                    {
                        @this.OrderInquiriesFax = partyContactMechanism.ContactMechanism as TelecommunicationsNumber;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).OrderInquiriesPhone))
                    {
                        @this.OrderInquiriesPhone = partyContactMechanism.ContactMechanism as TelecommunicationsNumber;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).PersonalEmailAddress))
                    {
                        @this.PersonalEmailAddress = partyContactMechanism.ContactMechanism as EmailAddress;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).SalesOffice))
                    {
                        @this.SalesOffice = partyContactMechanism.ContactMechanism;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).ShippingAddress))
                    {
                        @this.ShippingAddress = partyContactMechanism.ContactMechanism as PostalAddress;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).ShippingInquiriesFax))
                    {
                        @this.ShippingInquiriesFax = partyContactMechanism.ContactMechanism as TelecommunicationsNumber;
                    }

                    if (partyContactMechanism.ContactPurposes.Contains(new ContactMechanismPurposes(@this.Strategy.Session).ShippingInquiriesPhone))
                    {
                        @this.ShippingInquiriesPhone = partyContactMechanism.ContactMechanism as TelecommunicationsNumber;
                    }
                }
            }

            @this.CurrentPartyContactMechanisms = @this.PartyContactMechanisms
                                                  .Where(v => v.FromDate <= DateTime.UtcNow && (!v.ExistThroughDate || v.ThroughDate >= DateTime.UtcNow))
                                                  .ToArray();

            @this.InactivePartyContactMechanisms = @this.PartyContactMechanisms
                                                   .Except(@this.CurrentPartyContactMechanisms)
                                                   .ToArray();

            var allPartyRelationshipsWhereParty = @this.PartyRelationshipsWhereParty;

            @this.CurrentPartyRelationships = allPartyRelationshipsWhereParty
                                              .Where(v => v.FromDate <= DateTime.UtcNow && (!v.ExistThroughDate || v.ThroughDate >= DateTime.UtcNow))
                                              .ToArray();

            @this.InactivePartyRelationships = allPartyRelationshipsWhereParty
                                               .Except(@this.CurrentPartyRelationships)
                                               .ToArray();

            @this.CurrentSalesReps = @this.SalesRepRelationshipsWhereCustomer
                                     .Where(v => v.FromDate <= DateTime.UtcNow && (!v.ExistThroughDate || v.ThroughDate >= DateTime.UtcNow))
                                     .Select(v => v.SalesRepresentative)
                                     .ToArray();

            //@this.AppsOnDeriveActiveCustomer(derivation);

            foreach (CustomerRelationship customerRelationship in @this.CustomerRelationshipsWhereCustomer)
            {
                if (@this.AppsIsActiveCustomer(customerRelationship.InternalOrganisation, DateTime.UtcNow))
                {
                    customerRelationship.InternalOrganisation.AddActiveCustomer(@this);
                }
                else
                {
                    customerRelationship.InternalOrganisation.RemoveActiveCustomer(@this);
                }
            }

            //var allCustomerRelationships = @this.CustomerRelationshipsWhereCustomer;
            //var allInternalOrganisations = allCustomerRelationships
            //    .Select(v => v.InternalOrganisation)
            //    .Distinct()
            //    .ToArray();

            //foreach (InternalOrganisation internalOrganisation in allInternalOrganisations)
            //{
            //    var activeCustomers = allCustomerRelationships
            //        .Where(v => Equals(v.InternalOrganisation, internalOrganisation) && v.FromDate.Date <= DateTime.UtcNow &&
            //                    (!v.ExistThroughDate || v.ThroughDate >= DateTime.UtcNow))
            //        .Select(v => v.Customer)
            //        .ToArray();

            //    internalOrganisation.ActiveCustomers = activeCustomers;
            //}

            foreach (PartyFinancialRelationship partyFinancial in @this.PartyFinancialRelationshipsWhereParty)
            {
                partyFinancial.AmountDue     = 0;
                partyFinancial.AmountOverDue = 0;

                // Open Order Amount
                partyFinancial.OpenOrderAmount = @this.SalesOrdersWhereBillToCustomer
                                                 .Where(v =>
                                                        Equals(v.TakenBy, partyFinancial.InternalOrganisation) &&
                                                        !v.SalesOrderState.Equals(new SalesOrderStates(@this.Strategy.Session).Finished) &&
                                                        !v.SalesOrderState.Equals(new SalesOrderStates(@this.Strategy.Session).Cancelled))
                                                 .Sum(v => v.TotalIncVat);

                // Amount Due
                // Amount OverDue
                foreach (SalesInvoice salesInvoice in @this.SalesInvoicesWhereBillToCustomer.Where(v => Equals(v.BilledFrom, partyFinancial.InternalOrganisation) &&
                                                                                                   !v.SalesInvoiceState.Equals(new SalesInvoiceStates(@this.Strategy.Session).Paid)))
                {
                    if (salesInvoice.AmountPaid > 0)
                    {
                        partyFinancial.AmountDue += salesInvoice.TotalIncVat - salesInvoice.AmountPaid;
                    }
                    else
                    {
                        foreach (SalesInvoiceItem invoiceItem in salesInvoice.InvoiceItems)
                        {
                            if (!invoiceItem.SalesInvoiceItemState.Equals(
                                    new SalesInvoiceItemStates(@this.Strategy.Session).Paid))
                            {
                                if (invoiceItem.ExistTotalIncVat)
                                {
                                    partyFinancial.AmountDue += invoiceItem.TotalIncVat - invoiceItem.AmountPaid;
                                }
                            }
                        }
                    }

                    var gracePeriod = salesInvoice.Store.PaymentGracePeriod;

                    if (salesInvoice.DueDate.HasValue)
                    {
                        var dueDate = salesInvoice.DueDate.Value.AddDays(gracePeriod);

                        if (DateTime.UtcNow > dueDate)
                        {
                            partyFinancial.AmountOverDue += salesInvoice.TotalIncVat - salesInvoice.AmountPaid;
                        }
                    }
                }
            }
        }