public override void IsValidPeriod(PXCache sender, object Row, object NewValue)
		{
			if (NewValue != null && _ValidatePeriod != PeriodValidation.Nothing)
			{
				FinPeriod period = (FinPeriod)PXSelect<FinPeriod, Where<FinPeriod.finPeriodID, Equal<Required<FinPeriod.finPeriodID>>>>.Select(sender.Graph, (string)NewValue);

				if (period == null)
				{
					throw new PXSetPropertyException(Messages.NoPeriodsDefined);
				}

				FASetup setup = PXSetupOptional<FASetup>.Select(sender.Graph);
				if (setup == null || setup.UpdateGL == true)
				{
					if (period.Active != true)
					{
						sender.RaiseExceptionHandling(_FieldName, Row, null, new FiscalPeriodInactiveException(period.FinPeriodID, PXErrorLevel.Warning));
						return;
					}

					if (period.FAClosed == true)
					{
						if (PostClosedPeriods(sender.Graph))
						{
							sender.RaiseExceptionHandling(_FieldName, Row, null, new FiscalPeriodClosedException(period.FinPeriodID, PXErrorLevel.Warning));
							return;
						}
						else
						{
							throw new FiscalPeriodClosedException(period.FinPeriodID);
						}
					}
				}
			}
		}
        protected override void ValidateFinPeriodsStatus(PXCache sender, object row, string finPeriodID, ICalendarOrganizationIDProvider calendarOrganizationIDProvider)
        {
            FASetup setup = PXSetupOptional <FASetup> .Select(sender.Graph);

            if (setup == null || setup.UpdateGL == true)
            {
                base.ValidateFinPeriodsStatus(sender, row, finPeriodID, calendarOrganizationIDProvider);
            }
        }
        public static void Update(PXGraph graph, int?vendorID, int?vendorLocationID, string curyID, int?inventoryID, int?subItemID, string uom, decimal curyCost)
        {
            if (curyCost <= 0 || string.IsNullOrEmpty(uom) ||
                vendorID == null ||
                vendorLocationID == null)
            {
                return;
            }

            PXCache cache = graph.Caches[typeof(POVendorInventoryPriceUpdate)];

            foreach (PXResult <InventoryItem, Vendor, Company> r in
                     PXSelectJoin <InventoryItem,
                                   LeftJoinSingleTable <Vendor,
                                                        On <Vendor.bAccountID, Equal <Required <Vendor.bAccountID> > >,
                                                        CrossJoin <Company> >,
                                   Where <InventoryItem.inventoryID, Equal <Required <InventoryItem.inventoryID> > > > .
                     Select(graph, vendorID, inventoryID))
            {
                InventoryItem item    = r;
                Vendor        vendor  = r;
                Company       company = r;
                if (item.InventoryID == null || vendor.BAccountID == null ||
                    (item.StkItem == true && subItemID == null))
                {
                    continue;
                }
                INSetup setup = PXSetupOptional <INSetup> .Select(graph);

                int?savedSubItemID = item.StkItem == true ? subItemID : null;

                POVendorInventoryPriceUpdate vendorPrice = (POVendorInventoryPriceUpdate)cache.CreateInstance();
                vendorPrice.InventoryID      = inventoryID;
                vendorPrice.SubItemID        = savedSubItemID;
                vendorPrice.VendorID         = vendorID;
                vendorPrice.VendorLocationID = vendorLocationID;
                vendorPrice.PurchaseUnit     = uom;
                vendorPrice = (POVendorInventoryPriceUpdate)cache.Insert(vendorPrice);
                if (item.StkItem != true)
                {
                    vendorPrice.SubItemID = savedSubItemID;
                }
                vendorPrice.CuryID = curyID;
                cache.Normalize();
                vendorPrice.Active    = true;
                vendorPrice.LastPrice = curyCost;
            }
        }
        public static void ConvertToAccount(Contact contact, AccountsFilter param)
        {
            BusinessAccountMaint accountMaint = CreateInstance <BusinessAccountMaint>();
            object cd = param.BAccountID;

            accountMaint.BAccount.Cache.RaiseFieldUpdating <BAccount.acctCD>(null, ref cd);
            BAccount account = new BAccount
            {
                AcctCD           = (string)cd,
                AcctName         = param.AccountName,
                Type             = BAccountType.ProspectType,
                ClassID          = param.AccountClass,
                ParentBAccountID = contact.ParentBAccountID
            };

            #region Set Contact and Address fields
            CRContactClass cls = PXSelect <CRContactClass, Where <CRContactClass.classID, Equal <Current <Contact.classID> > > > .SelectSingleBound(accountMaint, new object[] { contact });

            if (cls != null && cls.OwnerToBAccount == true)
            {
                account.WorkgroupID = contact.WorkgroupID;
                account.OwnerID     = contact.OwnerID;
            }

            try
            {
                object newValue = account.OwnerID;
                accountMaint.BAccount.Cache.RaiseFieldVerifying <BAccount.ownerID>(account, ref newValue);
            }
            catch (PXSetPropertyException)
            {
                account.OwnerID = null;
            }

            account = accountMaint.BAccount.Insert(account);

            accountMaint.Answers.CopyAllAttributes(account, contact);

            Contact defContact = PXCache <Contact> .CreateCopy(PXSelect <Contact, Where <Contact.contactID, Equal <Current <BAccount.defContactID> > > > .SelectSingleBound(accountMaint, new object[] { account }));

            var defContactNoteID = defContact.NoteID;
            PXCache <Contact> .RestoreCopy(defContact, contact);

            defContact.ContactType     = ContactTypesAttribute.BAccountProperty;
            defContact.FullName        = account.AcctName;
            defContact.ContactID       = account.DefContactID;
            defContact.BAccountID      = account.BAccountID;
            defContact.DuplicateStatus = DuplicateStatusAttribute.NotValidated;
            defContact.DuplicateFound  = false;
            defContact.WorkgroupID     = null;
            defContact.OwnerID         = null;
            defContact.NoteID          = defContactNoteID;
            defContact = accountMaint.DefContact.Update(defContact);

            Address contactAddress = PXSelect <Address, Where <Address.addressID, Equal <Required <Contact.defAddressID> > > > .Select(accountMaint, contact.DefAddressID);

            if (contactAddress == null)
            {
                throw new PXException(Messages.DefAddressNotExists, contact.DisplayName);
            }
            contactAddress.BAccountID = account.BAccountID;
            accountMaint.AddressCurrent.Cache.Clear();

            defContact.DefAddressID = contactAddress.AddressID;
            defContact = accountMaint.DefContact.Update(defContact);

            contactAddress = accountMaint.AddressCurrent.Update(contactAddress);

            account.DefAddressID = contactAddress.AddressID;
            accountMaint.BAccount.Update(account);

            contact.BAccountID      = account.BAccountID;
            contact.DuplicateStatus = DuplicateStatusAttribute.NotValidated;
            contact.DuplicateFound  = false;
            if (contact.QualificationDate == null)
            {
                contact.QualificationDate = PXTimeZoneInfo.Now;
            }
            accountMaint.Contacts.Cache.SetStatus(contact, PXEntryStatus.Updated);
            CR.Location location = accountMaint.DefLocation.Select();
            location.DefAddressID = contactAddress.AddressID;
            accountMaint.DefLocation.Update(location);

            account.NoteID = PXNoteAttribute.GetNoteID <CRActivity.noteID>(accountMaint.CurrentBAccount.Cache, account);
            foreach (CRPMTimeActivity a in PXSelect <CRPMTimeActivity, Where <CRPMTimeActivity.refNoteID, Equal <Required <Contact.noteID> > > > .Select(accountMaint, contact.NoteID))
            {
                a.BAccountID = account.BAccountID;
                accountMaint.Activities.Cache.Update(a);
            }
            #endregion

            // Copy Note text and Files references
            CRSetup setup = PXSetupOptional <CRSetup> .Select(accountMaint);

            PXNoteAttribute.CopyNoteAndFiles(accountMaint.Contacts.Cache, contact, accountMaint.CurrentBAccount.Cache, account, setup);

            throw new PXRedirectRequiredException(accountMaint, "Business Account");
        }
        public virtual IEnumerable ConvertToOpportunity(PXAdapter adapter)
        {
            Save.Press();
            bool           isOpportunityAutoNumberOn = this.IsNumberingAutonumbered(Setup.Current.OpportunityNumberingID);
            List <Contact> contacts = new List <Contact>(adapter.Get().Cast <Contact>());

            foreach (Contact l in contacts)
            {
                OpportunityMaint opportunityMaint = CreateInstance <OpportunityMaint>();
                CROpportunity    opportunity      = (CROpportunity)opportunityMaint.Opportunity.Cache.Insert();

                if (!isOpportunityAutoNumberOn)
                {
                    if (OpportunityInfo.AskExt() != WebDialogResult.OK || !OpportunityInfo.VerifyRequired())
                    {
                        return(contacts);
                    }
                    CROpportunity existing = PXSelect <CROpportunity, Where <CROpportunity.opportunityID, Equal <Required <CROpportunity.opportunityID> > > > .SelectSingleBound(this, null, OpportunityInfo.Current.OpportunityID);

                    if (existing != null)
                    {
                        OpportunityInfo.Cache.RaiseExceptionHandling <OpportunityFilter.opportunityID>(OpportunityInfo.Current, OpportunityInfo.Current.OpportunityID, new PXSetPropertyException(Messages.OpportunityAlreadyExists, OpportunityInfo.Current.OpportunityID));
                        return(contacts);
                    }

                    object cd = OpportunityInfo.Current.OpportunityID;
                    opportunityMaint.Opportunity.Cache.RaiseFieldUpdating <CROpportunity.opportunityID>(null, ref cd);
                    opportunity.OpportunityID = (string)cd;
                }
                Contact lead = l;
                PXLongOperation.StartOperation(this, delegate()
                {
                    CRContactClass cls = PXSelect <CRContactClass, Where <CRContactClass.classID, Equal <Current <Contact.classID> > > > .SelectSingleBound(this, new object[] { lead });
                    if (cls != null && cls.OwnerToOpportunity == true)
                    {
                        opportunity.WorkgroupID = lead.WorkgroupID;
                        opportunity.OwnerID     = lead.OwnerID;
                    }
                    if (cls != null && cls.TargetOpportunityClassID != null)
                    {
                        opportunity.CROpportunityClassID = cls.TargetOpportunityClassID;
                    }

                    if (lead.BAccountID != null)
                    {
                        opportunity.BAccountID = lead.BAccountID;
                    }

                    if (lead.ParentBAccountID != null)
                    {
                        opportunity.ParentBAccountID = lead.ParentBAccountID;
                    }

                    if (lead.CampaignID != null)
                    {
                        opportunity.CampaignSourceID = lead.CampaignID;
                    }
                    opportunity.ContactID       = lead.ContactID;
                    opportunity.ConvertedLeadID = lead.ContactID;
                    opportunity.OpportunityName = string.IsNullOrEmpty(lead.FullName) ? lead.DisplayName : lead.FullName;
                    opportunity = (CROpportunity)opportunityMaint.Opportunity.Cache.Update(opportunity);

                    ContactMaint contactGraph = CreateInstance <ContactMaint>();
                    lead.ContactType          = ContactTypesAttribute.Person;
                    lead.QualificationDate    = PXTimeZoneInfo.Now;
                    lead.ConvertedBy          = Accessinfo.UserID;
                    lead = contactGraph.Contact.Update(lead);

                    opportunityMaint.Opportunity.Search <CROpportunity.opportunityID>(opportunity.OpportunityID);
                    lead = opportunityMaint.Leads.Update(lead);

                    // Copy Note text and Files references
                    CRSetup setup = PXSetupOptional <CRSetup> .Select(opportunityMaint);
                    PXNoteAttribute.CopyNoteAndFiles(opportunityMaint.Leads.Cache, lead, opportunityMaint.OpportunityCurrent.Cache, opportunity, setup);

                    throw new PXRedirectRequiredException(opportunityMaint, "Opportunity", true);
                });
            }
            return(contacts);
        }
        protected override BAccount CreateMaster(BusinessAccountMaint graph, AccountConversionOptions config)
        {
            var param      = AccountInfo.Current;
            var document   = Documents.Current;
            var docContact = Contacts.Current ?? Contacts.SelectSingle();
            var docAddress = Addresses.Current ?? Addresses.SelectSingle();

            object cd = param.BAccountID;

            graph.BAccount.Cache.RaiseFieldUpdating <BAccount.acctCD>(null, ref cd);

            BAccount account = graph.BAccount.Insert(new BAccount
            {
                AcctCD           = (string)cd,
                AcctName         = param.AccountName,
                Type             = BAccountType.ProspectType,
                ParentBAccountID = document.ParentBAccountID,
                CampaignSourceID = document.CampaignID,
            });

            account.ClassID = param.AccountClass;             // In case of (param.AccountClass == null) constructor fills ClassID with default value, so we have to set this directly.

            CRCustomerClass ocls = PXSelect <
                CRCustomerClass,
                Where <
                    CRCustomerClass.cRCustomerClassID, Equal <Required <CRCustomerClass.cRCustomerClassID> > > >
                                   .SelectSingleBound(graph, null, account.ClassID);

            if (ocls?.DefaultOwner == CRDefaultOwnerAttribute.Source)
            {
                account.WorkgroupID = document.WorkgroupID;
                account.OwnerID     = document.OwnerID;
            }

            account = graph.BAccount.Update(account);

            if (param.LinkContactToAccount == true)
            {
                // in case of opportunity
                Contact contact = PXSelect <Contact, Where <Contact.contactID, Equal <Required <CROpportunity.contactID> > > > .Select(graph, document.RefContactID);

                if (contact != null)
                {
                    graph.Answers.CopyAttributes(account, contact);
                    contact.BAccountID = account.BAccountID;
                    graph.Contacts.Update(contact);
                }
            }


            var defContact = graph.DefContact.SelectSingle()
                             ?? throw new InvalidOperationException("Cannot get Contact for Business Account.");    // just to ensure

            MapContact(docContact, account, ref defContact);
            MapConsentable(docContact, defContact);
            defContact = graph.DefContact.Update(defContact);

            var defAddress = graph.AddressCurrent.SelectSingle()
                             ?? throw new InvalidOperationException("Cannot get Address for Business Account.");    // just to ensure

            MapAddress(docAddress, account, ref defAddress);
            defAddress = graph.AddressCurrent.Update(defAddress);

            CR.Location location = graph.DefLocation.Select();
            location.DefAddressID = defAddress.AddressID;
            location.CTaxZoneID   = document.TaxZoneID;           // Saving tax zone before ReverseDocumentUpdate() removes it
            graph.DefLocation.Update(location);

            ReverseDocumentUpdate(graph, account);

            FillRelations(graph.Relations, account);

            FillAttributes(graph.Answers, account);

            TransferActivities(graph, account);

            // Copy Note text and Files references
            CRSetup setup = PXSetupOptional <CRSetup> .Select(graph);

            PXNoteAttribute.CopyNoteAndFiles(graph.Caches <TMain>(), GetMain(document), graph.CurrentBAccount.Cache, account, setup);

            return(account);
        }
        protected override Contact CreateMaster(ContactMaint graph, ContactConversionOptions _)
        {
            var entity           = Documents.Current;
            var param            = ContactInfo.Current;
            var docContact       = Contacts.Current ?? Contacts.SelectSingle();
            var docContactMethod = ContactMethod.Current ?? ContactMethod.SelectSingle();
            var docAddress       = Addresses.Current ?? Addresses.SelectSingle();

            Contact contact = new Contact
            {
                ContactType      = ContactTypesAttribute.Person,
                ParentBAccountID = entity.ParentBAccountID
            };

            MapContact(docContact, contact);
            MapConsentable(docContact, contact);

            contact.FirstName        = param.FirstName;
            contact.LastName         = param.LastName;
            contact.Salutation       = param.Salutation;
            contact.Phone1           = param.Phone1;
            contact.Phone1Type       = param.Phone1Type;
            contact.EMail            = param.Email;
            contact.ContactType      = ContactTypesAttribute.Person;
            contact.ParentBAccountID = entity.ParentBAccountID;
            contact.BAccountID       = entity.BAccountID;
            contact.Source           = entity.Source;

            MapContactMethod(docContactMethod, contact);

            var address = (Address)graph.AddressCurrent.Cache.CreateInstance();

            address = graph.AddressCurrent.Insert(address);

            contact = graph.Contact.Insert(contact);

            contact.ClassID = param.ContactClass;

            CRContactClass cls = PXSelect <
                CRContactClass,
                Where <
                    CRContactClass.classID, Equal <Required <CRContactClass.classID> > > >
                                 .SelectSingleBound(graph, null, contact.ClassID);

            if (cls?.DefaultOwner == CRDefaultOwnerAttribute.Source)
            {
                contact.WorkgroupID = entity.WorkgroupID;
                contact.OwnerID     = entity.OwnerID;
            }

            MapAddress(docAddress, address);

            address = (Address)graph.AddressCurrent.Cache.Update(address);

            contact.DefAddressID = address.AddressID;

            contact = graph.Contact.Update(contact);

            ReverseDocumentUpdate(graph, contact);

            FillRelations(graph.Relations, contact);

            FillAttributes(graph.Answers, contact);

            TransferActivities(graph, contact);

            // Copy Note text and Files references
            CRSetup setup = PXSetupOptional <CRSetup> .Select(graph);

            PXNoteAttribute.CopyNoteAndFiles(graph.Caches <TMain>(), Documents.Cache.GetMain(entity), graph.Contact.Cache, contact, setup);

            return(graph.Contact.Update(contact));
        }
Exemplo n.º 8
0
        protected static void ApplyAvalaraTax(OpportunityMaint rg, CROpportunity order, GetTaxResult result)
        {
            var avalaraSetup = (TXAvalaraSetup)PXSetupOptional <TXAvalaraSetup> .Select(rg);

            TaxZone taxZone = (TaxZone)PXSetup <TaxZone, Where <TaxZone.taxZoneID, Equal <Required <CROpportunity.taxZoneID> > > > .Select(rg, order.TaxZoneID);

            AP.Vendor vendor = PXSelect <AP.Vendor, Where <AP.Vendor.bAccountID, Equal <Required <AP.Vendor.bAccountID> > > > .Select(rg, taxZone.TaxVendorID);

            if (vendor == null)
            {
                throw new PXException(Messages.ExternalTaxVendorNotFound);
            }

            //Clear all existing Tax transactions:
            foreach (PXResult <CRTaxTran, Tax> res in rg.Taxes.View.SelectMultiBound(new object[] { order }))
            {
                CRTaxTran taxTran = (CRTaxTran)res;
                rg.Taxes.Delete(taxTran);
            }

            rg.Views.Caches.Add(typeof(Tax));

            for (int i = 0; i < result.TaxSummary.Count; i++)
            {
                string taxID = AvalaraMaint.GetTaxID(result.TaxSummary[i]);

                //Insert Tax if not exists - just for the selectors sake
                Tax tx = PXSelect <Tax, Where <Tax.taxID, Equal <Required <Tax.taxID> > > > .Select(rg, taxID);

                if (tx == null)
                {
                    tx       = new Tax();
                    tx.TaxID = taxID;
                    //tx.Descr = string.Format("Avalara {0} {1}%", taxID, Convert.ToDecimal(result.TaxSummary[i].Rate)*100);
                    tx.Descr             = PXMessages.LocalizeFormatNoPrefixNLA(TX.Messages.AvalaraTaxId, taxID);
                    tx.TaxType           = CSTaxType.Sales;
                    tx.TaxCalcType       = CSTaxCalcType.Doc;
                    tx.TaxCalcLevel      = avalaraSetup.IsInclusiveTax == true ? CSTaxCalcLevel.Inclusive : CSTaxCalcLevel.CalcOnItemAmt;
                    tx.TaxApplyTermsDisc = CSTaxTermsDiscount.ToTaxableAmount;
                    tx.SalesTaxAcctID    = vendor.SalesTaxAcctID;
                    tx.SalesTaxSubID     = vendor.SalesTaxSubID;
                    tx.ExpenseAccountID  = vendor.TaxExpenseAcctID;
                    tx.ExpenseSubID      = vendor.TaxExpenseSubID;
                    tx.TaxVendorID       = taxZone.TaxVendorID;
                    tx.IsExternal        = true;

                    rg.Caches[typeof(Tax)].Insert(tx);
                }

                CRTaxTran tax = new CRTaxTran();
                tax.OpportunityID  = order.OpportunityID;
                tax.TaxID          = taxID;
                tax.CuryTaxAmt     = Math.Abs(result.TaxSummary[i].Tax);
                tax.CuryTaxableAmt = Math.Abs(result.TaxSummary[i].Taxable);
                tax.TaxRate        = Convert.ToDecimal(result.TaxSummary[i].Rate) * 100;

                rg.Taxes.Insert(tax);
            }

            rg.Opportunity.SetValueExt <CROpportunity.curyTaxTotal>(order, Math.Abs(result.TotalTax));

            try
            {
                rg.SkipAvalaraTaxProcessing = true;
                rg.Save.Press();
            }
            finally
            {
                rg.SkipAvalaraTaxProcessing = false;
            }
        }
Exemplo n.º 9
0
        protected static GetTaxRequest BuildGetTaxRequest(OpportunityMaint rg, CROpportunity order)
        {
            if (order == null)
            {
                throw new PXArgumentException(ErrorMessages.ArgumentNullException);
            }

            BAccount cust = (BAccount)PXSelect <BAccount,
                                                Where <BAccount.bAccountID, Equal <Required <BAccount.bAccountID> > > > .
                            Select(rg, order.BAccountID);

            Location loc = (Location)PXSelect <Location,
                                               Where <Location.bAccountID, Equal <Required <Location.bAccountID> >, And <Location.locationID, Equal <Required <Location.locationID> > > > > .
                           Select(rg, order.BAccountID, order.LocationID);

            IAddressBase addressFrom = GetFromAddress(rg);
            IAddressBase addressTo   = GetToAddress(rg, order);

            if (addressFrom == null)
            {
                throw new PXException(Messages.FailedGetFromAddressSO);
            }

            if (addressTo == null)
            {
                throw new PXException(Messages.FailedGetToAddressSO);
            }

            var avalaraSetup = (TXAvalaraSetup)PXSetupOptional <TXAvalaraSetup> .Select(rg);

            GetTaxRequest request = new GetTaxRequest();

            request.CompanyCode        = AvalaraMaint.CompanyCodeFromBranch(rg, rg.Accessinfo.BranchID);
            request.CurrencyCode       = order.CuryID;
            request.CustomerCode       = cust.AcctCD;
            request.OriginAddress      = AvalaraMaint.FromAddress(addressFrom);
            request.DestinationAddress = AvalaraMaint.FromAddress(addressTo);
            request.DetailLevel        = DetailLevel.Summary;
            request.DocCode            = string.Format("CR.{0}", order.OpportunityID);
            request.DocDate            = order.CloseDate.GetValueOrDefault();

            int mult = 1;

            if (!string.IsNullOrEmpty(loc.CAvalaraCustomerUsageType))
            {
                request.CustomerUsageType = loc.CAvalaraCustomerUsageType;
            }
            if (!string.IsNullOrEmpty(loc.CAvalaraExemptionNumber))
            {
                request.ExemptionNo = loc.CAvalaraExemptionNumber;
            }

            request.DocType = DocumentType.SalesOrder;

            PXSelectBase <CROpportunityProducts> select = new PXSelectJoin <CROpportunityProducts,
                                                                            LeftJoin <InventoryItem, On <InventoryItem.inventoryID, Equal <CROpportunityProducts.inventoryID> >,
                                                                                      LeftJoin <Account, On <Account.accountID, Equal <InventoryItem.salesAcctID> > > >,
                                                                            Where <CROpportunityProducts.cROpportunityID, Equal <Current <CROpportunity.opportunityID> > >,
                                                                            OrderBy <Asc <CROpportunityProducts.cROpportunityProductID> > >(rg);

            foreach (PXResult <CROpportunityProducts, InventoryItem, Account> res in select.View.SelectMultiBound(new object[] { order }))
            {
                CROpportunityProducts tran = (CROpportunityProducts)res;
                InventoryItem         item = (InventoryItem)res;
                Account salesAccount       = (Account)res;

                Line line = new Line();
                line.No                 = Convert.ToString(tran.CROpportunityProductID);
                line.Amount             = mult * tran.CuryAmount.GetValueOrDefault();
                line.Description        = tran.TransactionDescription;
                line.DestinationAddress = request.DestinationAddress;
                line.OriginAddress      = request.OriginAddress;
                line.ItemCode           = item.InventoryCD;
                line.Qty                = Math.Abs(Convert.ToDouble(tran.Qty.GetValueOrDefault()));
                line.Discounted         = request.Discount > 0;
                line.TaxIncluded        = avalaraSetup.IsInclusiveTax == true;

                if (avalaraSetup != null && avalaraSetup.SendRevenueAccount == true)
                {
                    line.RevAcct = salesAccount.AccountCD;
                }

                line.TaxCode = tran.TaxCategoryID;

                request.Lines.Add(line);
            }

            return(request);
        }
Exemplo n.º 10
0
        protected static void ApplyAvalaraTax(OpportunityMaint rg, CROpportunity order, GetTaxResult result)
        {
            var avalaraSetup = (TXAvalaraSetup)PXSetupOptional <TXAvalaraSetup> .Select(rg);

            TaxZone taxZone = (TaxZone)PXSetup <TaxZone, Where <TaxZone.taxZoneID, Equal <Required <CROpportunity.taxZoneID> > > > .Select(rg, order.TaxZoneID);

            AP.Vendor vendor = PXSelect <AP.Vendor, Where <AP.Vendor.bAccountID, Equal <Required <AP.Vendor.bAccountID> > > > .Select(rg, taxZone.TaxVendorID);

            if (vendor == null)
            {
                throw new PXException("Tax Vendor is required but not found for the External TaxZone.");
            }

            Dictionary <string, CRTaxTran> existingRows = new Dictionary <string, CRTaxTran>();

            foreach (PXResult <CRTaxTran, Tax> res in rg.Taxes.View.SelectMultiBound(new object[] { order }))
            {
                CRTaxTran taxTran = (CRTaxTran)res;
                existingRows.Add(taxTran.TaxID.Trim().ToUpperInvariant(), taxTran);
            }

            rg.Views.Caches.Add(typeof(Tax));

            for (int i = 0; i < result.TaxSummary.Count; i++)
            {
                string taxID = result.TaxSummary[i].TaxName.ToUpperInvariant();

                //Insert Tax if not exists - just for the selectors sake
                Tax tx = PXSelect <Tax, Where <Tax.taxID, Equal <Required <Tax.taxID> > > > .Select(rg, taxID);

                if (tx == null)
                {
                    tx       = new Tax();
                    tx.TaxID = taxID;
                    //tx.Descr = string.Format("Avalara {0} {1}%", taxID, Convert.ToDecimal(result.TaxSummary[i].Rate)*100);
                    tx.Descr             = string.Format("Avalara {0}", taxID);
                    tx.TaxType           = CSTaxType.Sales;
                    tx.TaxCalcType       = CSTaxCalcType.Doc;
                    tx.TaxCalcLevel      = avalaraSetup.IsInclusiveTax == true ? CSTaxCalcLevel.Inclusive : CSTaxCalcLevel.CalcOnItemAmt;
                    tx.TaxApplyTermsDisc = CSTaxTermsDiscount.ToTaxableAmount;
                    tx.SalesTaxAcctID    = vendor.SalesTaxAcctID;
                    tx.SalesTaxSubID     = vendor.SalesTaxSubID;
                    tx.ExpenseAccountID  = vendor.TaxExpenseAcctID;
                    tx.ExpenseSubID      = vendor.TaxExpenseSubID;
                    tx.TaxVendorID       = taxZone.TaxVendorID;

                    rg.Caches[typeof(Tax)].Insert(tx);
                }

                CRTaxTran existing = null;
                existingRows.TryGetValue(taxID, out existing);

                if (existing != null)
                {
                    existing.TaxAmt         = Math.Abs(result.TaxSummary[i].Tax);
                    existing.CuryTaxAmt     = Math.Abs(result.TaxSummary[i].Tax);
                    existing.TaxableAmt     = Math.Abs(result.TaxSummary[i].Taxable);
                    existing.CuryTaxableAmt = Math.Abs(result.TaxSummary[i].Taxable);
                    existing.TaxRate        = Convert.ToDecimal(result.TaxSummary[i].Rate);

                    rg.Taxes.Update(existing);
                    existingRows.Remove(existing.TaxID.Trim().ToUpperInvariant());
                }
                else
                {
                    CRTaxTran tax = new CRTaxTran();
                    tax.OpportunityID  = order.OpportunityID;
                    tax.TaxID          = taxID;
                    tax.TaxAmt         = Math.Abs(result.TaxSummary[i].Tax);
                    tax.CuryTaxAmt     = Math.Abs(result.TaxSummary[i].Tax);
                    tax.TaxableAmt     = Math.Abs(result.TaxSummary[i].Taxable);
                    tax.CuryTaxableAmt = Math.Abs(result.TaxSummary[i].Taxable);
                    tax.TaxRate        = Convert.ToDecimal(result.TaxSummary[i].Rate);

                    rg.Taxes.Insert(tax);
                }
            }

            foreach (CRTaxTran taxTran in existingRows.Values)
            {
                rg.Taxes.Delete(taxTran);
            }

            rg.Opportunity.SetValueExt <CROpportunity.curyTaxTotal>(order, Math.Abs(result.TotalTax));

            try
            {
                rg.SkipAvalaraTaxProcessing = true;
                rg.Save.Press();
            }
            finally
            {
                rg.SkipAvalaraTaxProcessing = false;
            }
        }