Exemplo n.º 1
0
        public static void CalculateAvalaraTax(OpportunityMaint rg, CROpportunity order)
        {
            TaxSvc service = new TaxSvc();

            AvalaraMaint.SetupService(rg, service);

            AddressSvc addressService = new AddressSvc();

            AvalaraMaint.SetupService(rg, addressService);

            GetTaxRequest getRequest       = null;
            bool          isValidByDefault = true;

            if (order.IsTaxValid != true)
            {
                getRequest = BuildGetTaxRequest(rg, order);

                if (getRequest.Lines.Count > 0)
                {
                    isValidByDefault = false;
                }
                else
                {
                    getRequest = null;
                }
            }

            if (isValidByDefault)
            {
                PXDatabase.Update <CROpportunity>(
                    new PXDataFieldAssign("IsTaxValid", true),
                    new PXDataFieldRestrict("OpportunityID", PXDbType.NVarChar, CROpportunity.OpportunityIDLength, order.OpportunityID, PXComp.EQ)
                    );
                return;
            }

            GetTaxResult result = service.GetTax(getRequest);

            if (result.ResultCode == SeverityLevel.Success)
            {
                try
                {
                    ApplyAvalaraTax(rg, order, result);
                    PXDatabase.Update <CROpportunity>(
                        new PXDataFieldAssign("IsTaxValid", true),
                        new PXDataFieldRestrict("OpportunityID", PXDbType.NVarChar, CROpportunity.OpportunityIDLength, order.OpportunityID, PXComp.EQ)
                        );
                }
                catch (Exception ex)
                {
                    throw new PXException(ex, TX.Messages.FailedToApplyTaxes);
                }
            }
            else
            {
                LogMessages(result);

                throw new PXException(TX.Messages.FailedToGetTaxes);
            }
        }
        protected override void CalcTotals(object row, bool CalcTaxes)
        {
            string taxZondeID    = CurrentDocument?.TaxZoneID;
            bool   isExternalTax = AvalaraMaint.IsExternalTax(Base, taxZondeID);

            bool doCalc = _NoSumTotals == false && CalcTaxes && !isExternalTax;

            if (doCalc && CurrentDocument != null)
            {
                ResetDiscrepancy(Base);
            }
            base.CalcTotals(row, doCalc);
        }
Exemplo n.º 3
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.º 4
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);
        }