protected virtual GetTaxRequest BuildGetTaxRequestUnbilled(POOrder order)
        {
            if (order == null)
            {
                throw new PXArgumentException(nameof(order));
            }

            Vendor   vend = (Vendor)Base.vendor.View.SelectSingleBound(new object[] { order });
            Location loc  = (Location)Base.location.View.SelectSingleBound(new object[] { order });

            IAddressBase fromAddress = GetFromAddress(order);
            IAddressBase toAddress   = GetToAddress(order);

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

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

            GetTaxRequest request = new GetTaxRequest();

            request.CompanyCode        = CompanyCodeFromBranch(order.TaxZoneID, order.BranchID);
            request.CurrencyCode       = order.CuryID;
            request.CustomerCode       = vend.AcctCD;
            request.OriginAddress      = AddressConverter.ConvertTaxAddress(fromAddress);
            request.DestinationAddress = AddressConverter.ConvertTaxAddress(toAddress);
            request.DocCode            = string.Format("PO.{0}.{1}", order.OrderType, order.OrderNbr);
            request.DocDate            = order.OrderDate.GetValueOrDefault();
            request.LocationCode       = GetExternalTaxProviderLocationCode(order);

            int mult = 1;

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


            request.DocType = TaxDocumentType.PurchaseOrder;

            PXSelectBase <POLine> select = new PXSelectJoin <POLine,
                                                             LeftJoin <InventoryItem, On <POLine.FK.InventoryItem>,
                                                                       LeftJoin <Account, On <Account.accountID, Equal <InventoryItem.salesAcctID> > > >,
                                                             Where <POLine.orderType, Equal <Current <POOrder.orderType> >, And <POLine.orderNbr, Equal <Current <POOrder.orderNbr> > > >,
                                                             OrderBy <Asc <POLine.lineNbr> > >(Base);


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

                if (tran.UnbilledAmt > 0)
                {
                    var line = new TaxCartItem();
                    line.Index              = tran.LineNbr ?? 0;
                    line.Amount             = mult * tran.CuryUnbilledAmt.GetValueOrDefault();
                    line.Description        = tran.TranDesc;
                    line.OriginAddress      = AddressConverter.ConvertTaxAddress(GetFromAddress(order, tran));
                    line.DestinationAddress = AddressConverter.ConvertTaxAddress(GetToAddress(order, tran));
                    line.ItemCode           = item.InventoryCD;
                    line.Quantity           = tran.BaseUnbilledQty.GetValueOrDefault();
                    line.Discounted         = request.Discount > 0;
                    line.RevAcct            = salesAccount.AccountCD;

                    line.TaxCode = tran.TaxCategoryID;

                    request.CartItems.Add(line);
                }
            }

            return(request);
        }
Exemplo n.º 2
0
        public virtual CommitTaxRequest BuildCommitTaxRequestWithFrt(ARInvoice invoice)
        {
            if (invoice == null)
            {
                throw new PXArgumentException(nameof(invoice), ErrorMessages.ArgumentNullException);
            }

            Customer cust = (Customer)Base.customer.View.SelectSingleBound(new object[] { invoice });

            CR.Location loc = (CR.Location)Base.location.View.SelectSingleBound(new object[] { invoice });

            CommitTaxRequest request = new CommitTaxRequest();

            request.CompanyCode       = Base1.CompanyCodeFromBranch(invoice.TaxZoneID, invoice.BranchID);
            request.CurrencyCode      = invoice.CuryID;
            request.CustomerCode      = cust.AcctCD;
            request.TaxRegistrationID = loc?.TaxRegistrationID;
            IAddressBase fromAddress = Base1.GetFromAddress(invoice);
            IAddressBase toAddress   = Base1.GetToAddress(invoice);

            if (fromAddress == null)
            {
                throw new PXException(Messages.FailedGetFrom);
            }

            if (toAddress == null)
            {
                throw new PXException(Messages.FailedGetTo);
            }

            request.OriginAddress      = AddressConverter.ConvertTaxAddress(fromAddress);
            request.DestinationAddress = AddressConverter.ConvertTaxAddress(toAddress);
            request.DocCode            = $"AR.{invoice.DocType}.{invoice.RefNbr}";
            request.DocDate            = invoice.DocDate.GetValueOrDefault();
            request.LocationCode       = GetExternalTaxProviderLocationCode <ARTran, ARTran.FK.Invoice.SameAsCurrent, ARTran.siteID>(invoice);
            request.CustomerUsageType  = invoice.AvalaraCustomerUsageType;

            if (!string.IsNullOrEmpty(invoice.ExternalTaxExemptionNumber))
            {
                request.ExemptionNo = invoice.ExternalTaxExemptionNumber;
            }

            request.DocType = Base1.GetTaxDocumentType(invoice);
            Sign sign = Base1.GetDocumentSign(invoice);

            PXSelectBase <ARTran> select = new PXSelectJoin <ARTran, LeftJoin <InventoryItem, On <InventoryItem.inventoryID, Equal <ARTran.inventoryID> >,
                                                                               LeftJoin <Account, On <Account.accountID, Equal <ARTran.accountID> > > >,
                                                             Where <ARTran.tranType, Equal <Current <ARInvoice.docType> >,
                                                                    And <ARTran.refNbr, Equal <Current <ARInvoice.refNbr> >,
                                                                         And <Where <ARTran.lineType, NotEqual <SOLineType.discount>, Or <ARTran.lineType, IsNull> > > > >,
                                                             OrderBy <Asc <ARTran.tranType, Asc <ARTran.refNbr, Asc <ARTran.lineNbr> > > > >(Base);

            request.Discount = Base.Document.Current.CuryDiscTot.GetValueOrDefault();
            DateTime?taxDate = invoice.OrigDocDate;

            bool applyRetainage = Base.ARSetup.Current?.RetainTaxes != true && invoice.IsOriginalRetainageDocument();

            /// <summary>
            /// Add the following condition and logic per Jira [IP-23]
            /// </summary>>
            string taxCategory = (Base as SOInvoiceEntry).FreightDetails.Current?.TaxCategoryID;

            if (invoice.CuryFreightTot > 0 && GL.Branch.PK.Find(Base, Base.Accessinfo.BranchID).CountryID == Country_US && invoice.TaxZoneID == TaxCloud && !string.IsNullOrEmpty(taxCategory))
            {
                var line = new TaxCartItem();
                line.Index              = short.MinValue;
                line.Quantity           = 1;
                line.UOM                = "EA";
                line.Amount             = sign * invoice.CuryFreightTot.GetValueOrDefault();
                line.Description        = PXMessages.LocalizeNoPrefix(SO.Messages.FreightDesc);
                line.DestinationAddress = request.DestinationAddress;
                line.OriginAddress      = request.OriginAddress;
                line.ItemCode           = "N/A";
                line.Discounted         = false;
                line.TaxCode            = taxCategory;

                request.CartItems.Add(line);
            }

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

                var line = new TaxCartItem();
                line.Index              = tran.LineNbr ?? 0;
                line.Amount             = sign * (tran.CuryTranAmt.GetValueOrDefault() + (applyRetainage ? tran.CuryRetainageAmt.GetValueOrDefault() : 0m));
                line.Description        = tran.TranDesc;
                line.DestinationAddress = AddressConverter.ConvertTaxAddress(Base1.GetToAddress(invoice, tran));
                line.OriginAddress      = AddressConverter.ConvertTaxAddress(Base1.GetFromAddress(invoice, tran));
                line.ItemCode           = item.InventoryCD;
                line.Quantity           = Math.Abs(tran.Qty.GetValueOrDefault());
                line.UOM        = tran.UOM;
                line.Discounted = tran.LineType != SOLineType.Freight && request.Discount > 0;
                line.RevAcct    = salesAccount.AccountCD;

                line.TaxCode           = tran.TaxCategoryID;
                line.CustomerUsageType = tran.AvalaraCustomerUsageType;

                if (tran.OrigInvoiceDate != null)
                {
                    taxDate = tran.OrigInvoiceDate;
                }

                request.CartItems.Add(line);
            }

            if (applyRetainage)
            {
                var line = new TaxCartItem();
                line.Index              = invoice.LineCntr.GetValueOrDefault() + 1;
                line.Amount             = Sign.Minus * sign * invoice.CuryLineRetainageTotal.GetValueOrDefault();
                line.Description        = PXMessages.LocalizeFormatNoPrefix(AP.Messages.RetainageForTransactionDescription, GetLabel.For <ARDocType>(invoice.DocType), invoice.RefNbr);
                line.DestinationAddress = request.DestinationAddress;
                line.OriginAddress      = request.OriginAddress;
                line.ItemCode           = "Retainage";
                line.Discounted         = false;
                line.NonTaxable         = true;

                request.CartItems.Add(line);
            }

            if ((invoice.DocType == ARDocType.CreditMemo || invoice.DocType == ARDocType.CashReturn) && invoice.OrigDocDate != null)
            {
                request.TaxOverride.Reason          = Messages.ReturnReason;
                request.TaxOverride.TaxDate         = taxDate.Value;
                request.TaxOverride.TaxOverrideType = TaxOverrideType.TaxDate;
                sign = Sign.Minus;
            }

            return(request);
        }
        protected virtual GetTaxRequest BuildGetTaxRequest(CROpportunity order)
        {
            if (order == null)
            {
                throw new PXArgumentException(nameof(order));
            }

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

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

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

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

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

            GetTaxRequest request = new GetTaxRequest();

            request.CompanyCode        = CompanyCodeFromBranch(order.TaxZoneID, Base.Accessinfo.BranchID);
            request.CurrencyCode       = order.CuryID;
            request.CustomerCode       = cust?.AcctCD;
            request.OriginAddress      = AddressConverter.ConvertTaxAddress(addressFrom);
            request.DestinationAddress = AddressConverter.ConvertTaxAddress(addressTo);
            request.DocCode            = $"CR.{order.OpportunityID}";
            request.DocDate            = order.CloseDate.GetValueOrDefault();
            request.Discount           = order.CuryLineDocDiscountTotal.GetValueOrDefault();

            int mult = 1;

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

            request.DocType = TaxDocumentType.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.quoteID, Equal <Current <CROpportunity.quoteNoteID> > >,
                                                                            OrderBy <Asc <CROpportunityProducts.lineNbr> > >(Base);

            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;

                var line = new TaxCartItem();
                line.Index              = tran.LineNbr ?? 0;
                line.Amount             = mult * tran.CuryAmount.GetValueOrDefault();
                line.Description        = tran.Descr;
                line.DestinationAddress = request.DestinationAddress;
                line.OriginAddress      = request.OriginAddress;
                line.ItemCode           = item.InventoryCD;
                line.Quantity           = Math.Abs(tran.Qty.GetValueOrDefault());
                line.Discounted         = request.Discount > 0;
                line.RevAcct            = salesAccount.AccountCD;
                line.TaxCode            = tran.TaxCategoryID;

                request.CartItems.Add(line);
            }

            return(request);
        }
Exemplo n.º 4
0
        protected GetTaxRequest BuildGetTaxRequestWithFRTCate <TLineAmt, TLineQty, TDocDiscount>(SOOrder order, string docCode, string debugMethodName)
            where TLineAmt : IBqlField
            where TLineQty : IBqlField
            where TDocDiscount : IBqlField
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            Debug.Indent();

            if (order == null)
            {
                throw new PXArgumentException(nameof(order));
            }

            Customer cust = (Customer)Base.customer.View.SelectSingleBound(new object[] { order });
            Location loc  = (Location)Base.location.View.SelectSingleBound(new object[] { order });

            IAddressBase fromAddress = GetFromAddress(order);
            IAddressBase toAddress   = GetToAddress(order);

            Debug.Print($"{DateTime.Now.TimeOfDay} Select Customer, Location, Addresses in {sw.ElapsedMilliseconds} millisec");

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

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

            GetTaxRequest request = new GetTaxRequest();

            request.CompanyCode        = Base1.CompanyCodeFromBranch(order.TaxZoneID, order.BranchID);
            request.CurrencyCode       = order.CuryID;
            request.CustomerCode       = cust.AcctCD;
            request.TaxRegistrationID  = loc?.TaxRegistrationID;
            request.OriginAddress      = AddressConverter.ConvertTaxAddress(fromAddress);
            request.DestinationAddress = AddressConverter.ConvertTaxAddress(toAddress);
            request.DocCode            = docCode;
            request.DocDate            = order.OrderDate.GetValueOrDefault();
            request.LocationCode       = GetExternalTaxProviderLocationCode <SOLine, SOLine.FK.Order.SameAsCurrent, SOLine.siteID>(order);

            Sign docSign = Sign.Plus;

            request.CustomerUsageType = order.AvalaraCustomerUsageType;

            if (!string.IsNullOrEmpty(order.ExternalTaxExemptionNumber))
            {
                request.ExemptionNo = order.ExternalTaxExemptionNumber;
            }

            SOOrderType orderType = (SOOrderType)Base.soordertype.View.SelectSingleBound(new object[] { order });

            if (orderType.DefaultOperation == SOOperation.Receipt)
            {
                request.DocType = TaxDocumentType.ReturnOrder;
                docSign         = Sign.Minus;
            }
            else
            {
                request.DocType = TaxDocumentType.SalesOrder;
            }

            PXSelectBase <SOLine> select = new PXSelectJoin <SOLine, LeftJoin <InventoryItem, On <SOLine.FK.InventoryItem>,
                                                                               LeftJoin <Account, On <Account.accountID, Equal <SOLine.salesAcctID> > > >,
                                                             Where <SOLine.orderType, Equal <Current <SOOrder.orderType> >,
                                                                    And <SOLine.orderNbr, Equal <Current <SOOrder.orderNbr> > > >,
                                                             OrderBy <Asc <SOLine.orderType,
                                                                           Asc <SOLine.orderNbr,
                                                                                Asc <SOLine.lineNbr> > > > >(Base);

            PXCache documentCache = Base.Caches[typeof(SOOrder)];

            request.Discount = (documentCache.GetValue <TDocDiscount>(order) as decimal?) ?? 0m;

            Stopwatch sw2 = new Stopwatch();

            sw2.Start();

            // Add additional condition to skip the calculation including the freight amount. Per YJ's request.
            if (order.CuryFreightTot > 0 && !string.IsNullOrEmpty(order.FreightTaxCategoryID))
            {
                var line = new TaxCartItem();
                line.Index              = short.MinValue;
                line.Quantity           = 1;
                line.UOM                = "EA";
                line.Amount             = docSign * order.CuryFreightTot.GetValueOrDefault();
                line.Description        = PXMessages.LocalizeNoPrefix(Messages.FreightDesc);
                line.DestinationAddress = request.DestinationAddress;
                line.OriginAddress      = request.OriginAddress;
                line.ItemCode           = "N/A";
                line.Discounted         = false;
                line.TaxCode            = order.FreightTaxCategoryID;

                request.CartItems.Add(line);
            }

            PXCache lineCache = Base.Caches[typeof(SOLine)];

            foreach (PXResult <SOLine, InventoryItem, Account> res in select.View.SelectMultiBound(new object[] { order }))
            {
                SOLine        tran             = (SOLine)res;
                InventoryItem item             = (InventoryItem)res;
                Account       salesAccount     = (Account)res;
                bool          lineIsDiscounted = request.Discount > 0m &&
                                                 ((tran.DocumentDiscountRate ?? 1m) != 1m || (tran.GroupDiscountRate ?? 1m) != 1m);

                var line = new TaxCartItem();
                line.Index = tran.LineNbr ?? 0;

                decimal lineAmount = (lineCache.GetValue <TLineAmt>(tran) as decimal?) ?? 0m;
                decimal lineQty    = (lineCache.GetValue <TLineQty>(tran) as decimal?) ?? 0m;

                line.Amount = orderType.DefaultOperation != tran.Operation
                                        ? Sign.Minus * docSign * lineAmount
                                        : docSign * lineAmount;

                line.Description        = tran.TranDesc;
                line.DestinationAddress = AddressConverter.ConvertTaxAddress(Base1.GetToAddress(order, tran));
                line.OriginAddress      = AddressConverter.ConvertTaxAddress(Base1.GetFromAddress(order, tran));
                line.ItemCode           = item.InventoryCD;
                line.Quantity           = Math.Abs(lineQty);
                line.UOM        = tran.UOM;
                line.Discounted = lineIsDiscounted;
                line.RevAcct    = salesAccount.AccountCD;

                line.TaxCode           = tran.TaxCategoryID;
                line.CustomerUsageType = tran.AvalaraCustomerUsageType;

                if (tran.Operation == SOOperation.Receipt && tran.InvoiceDate != null)
                {
                    line.TaxOverride.Reason          = Messages.ReturnReason;
                    line.TaxOverride.TaxDate         = tran.InvoiceDate.Value;
                    line.TaxOverride.TaxOverrideType = TaxOverrideType.TaxDate;
                }

                request.CartItems.Add(line);
            }

            sw2.Stop();
            Debug.Print($"{DateTime.Now.TimeOfDay} Select detail lines in {sw2.ElapsedMilliseconds} millisec.");

            Debug.Unindent();
            sw.Stop();
            Debug.Print($"{DateTime.Now.TimeOfDay} {debugMethodName}() in {sw.ElapsedMilliseconds} millisec.");

            return(request);
        }
        protected virtual GetTaxRequest BuildGetTaxRequest(ARCashSale invoice)
        {
            if (invoice == null)
            {
                throw new PXArgumentException(nameof(invoice), ErrorMessages.ArgumentNullException);
            }

            Customer cust = (Customer)Base.customer.View.SelectSingleBound(new object[] { invoice });
            Location loc  = (Location)Base.location.View.SelectSingleBound(new object[] { invoice });

            GetTaxRequest request = new GetTaxRequest();

            request.CompanyCode  = CompanyCodeFromBranch(invoice.TaxZoneID, invoice.BranchID);
            request.CurrencyCode = invoice.CuryID;
            request.CustomerCode = cust.AcctCD;
            IAddressBase fromAddress = GetFromAddress(invoice);
            IAddressBase toAddress   = GetToAddress(invoice);

            if (fromAddress == null)
            {
                throw new PXException(Messages.FailedGetFrom);
            }

            if (toAddress == null)
            {
                throw new PXException(Messages.FailedGetTo);
            }

            request.OriginAddress      = AddressConverter.ConvertTaxAddress(fromAddress);
            request.DestinationAddress = AddressConverter.ConvertTaxAddress(toAddress);
            request.DocCode            = $"AR.{invoice.DocType}.{invoice.RefNbr}";
            request.DocDate            = invoice.DocDate.GetValueOrDefault();
            request.LocationCode       = GetExternalTaxProviderLocationCode(invoice);
            request.CustomerUsageType  = invoice.AvalaraCustomerUsageType;
            if (!string.IsNullOrEmpty(loc.CAvalaraExemptionNumber))
            {
                request.ExemptionNo = loc.CAvalaraExemptionNumber;
            }

            request.DocType = GetTaxDocumentType(invoice);
            Sign sign = GetDocumentSign(invoice);

            PXSelectBase <ARTran> select = new PXSelectJoin <ARTran,
                                                             LeftJoin <InventoryItem, On <InventoryItem.inventoryID, Equal <ARTran.inventoryID> >,
                                                                       LeftJoin <Account, On <Account.accountID, Equal <ARTran.accountID> > > >,
                                                             Where <ARTran.tranType, Equal <Current <ARCashSale.docType> >,
                                                                    And <ARTran.refNbr, Equal <Current <ARCashSale.refNbr> >,
                                                                         And <Where <ARTran.lineType, NotEqual <SOLineType.discount>, Or <ARTran.lineType, IsNull> > > > >,
                                                             OrderBy <Asc <ARTran.tranType, Asc <ARTran.refNbr, Asc <ARTran.lineNbr> > > > >(Base);

            request.Discount = GetDocDiscount().GetValueOrDefault();
            DateTime?taxDate = invoice.OrigDocDate;

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

                var line = new TaxCartItem();
                line.Index              = tran.LineNbr ?? 0;
                line.Amount             = sign * tran.CuryTranAmt.GetValueOrDefault();
                line.Description        = tran.TranDesc;
                line.DestinationAddress = request.DestinationAddress;
                line.OriginAddress      = request.OriginAddress;
                line.ItemCode           = item.InventoryCD;
                line.Quantity           = Math.Abs(tran.Qty.GetValueOrDefault());
                line.Discounted         = request.Discount > 0;
                line.RevAcct            = salesAccount.AccountCD;
                line.TaxCode            = tran.TaxCategoryID;

                if (tran.OrigInvoiceDate != null)
                {
                    taxDate = tran.OrigInvoiceDate;
                }

                request.CartItems.Add(line);
            }

            if (invoice.DocType == ARDocType.CashReturn && invoice.OrigDocDate != null)
            {
                request.TaxOverride.Reason          = Messages.ReturnReason;
                request.TaxOverride.TaxDate         = taxDate.Value;
                request.TaxOverride.TaxOverrideType = TaxOverrideType.TaxDate;
            }

            return(request);
        }
        protected virtual GetTaxRequest BuildGetTaxRequest(CAAdj invoice)
        {
            if (invoice == null)
            {
                throw new PXArgumentException(nameof(invoice), ErrorMessages.ArgumentNullException);
            }

            GetTaxRequest request = new GetTaxRequest();

            request.CompanyCode  = CompanyCodeFromBranch(invoice.TaxZoneID, invoice.BranchID);
            request.CurrencyCode = invoice.CuryID;
            request.CustomerCode = "N/A";
            IAddressBase fromAddress = GetToAddress(invoice);
            IAddressBase toAddress   = fromAddress;

            if (fromAddress == null)
            {
                throw new PXException(Messages.FailedGetFrom);
            }

            if (toAddress == null)
            {
                throw new PXException(Messages.FailedGetTo);
            }

            request.OriginAddress      = AddressConverter.ConvertTaxAddress(fromAddress);
            request.DestinationAddress = AddressConverter.ConvertTaxAddress(toAddress);
            request.DocCode            = $"CA.{invoice.AdjRefNbr}";
            request.DocDate            = invoice.TranDate.GetValueOrDefault();
            request.LocationCode       = GetExternalTaxProviderLocationCode(invoice);
            Location branchLoc = GetBranchLocation(invoice);

            if (branchLoc != null)
            {
                request.CustomerUsageType = branchLoc.CAvalaraCustomerUsageType;
                request.ExemptionNo       = branchLoc.CAvalaraExemptionNumber;
            }

            request.DocType = TaxDocumentType.PurchaseInvoice;
            int mult = 1;

            if (invoice.DrCr == CADrCr.CADebit)
            {
                request.DocType = TaxDocumentType.SalesInvoice;
            }
            else
            {
                request.DocType = TaxDocumentType.PurchaseInvoice;
            }

            PXSelectBase <CASplit> select = new PXSelectJoin <CASplit,
                                                              LeftJoin <InventoryItem, On <InventoryItem.inventoryID, Equal <CASplit.inventoryID> > >,
                                                              Where <CASplit.adjRefNbr, Equal <Current <CAAdj.adjRefNbr> > >,
                                                              OrderBy <Asc <CASplit.adjRefNbr, Asc <CASplit.lineNbr> > > >(Base);

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

                var line = new TaxCartItem
                {
                    Index              = tran.LineNbr ?? 0,
                    Amount             = mult * tran.CuryTranAmt.GetValueOrDefault(),
                    Description        = tran.TranDesc,
                    DestinationAddress = request.DestinationAddress,
                    OriginAddress      = request.OriginAddress,
                    ItemCode           = item.InventoryCD,
                    Quantity           = Math.Abs(tran.Qty.GetValueOrDefault()),
                    Discounted         = request.Discount > 0,
                    TaxCode            = tran.TaxCategoryID,
                };

                request.CartItems.Add(line);
            }

            return(request);
        }
        protected GetTaxRequest BuildGetTaxRequest(FSServiceOrder order)
        {
            if (order == null)
            {
                throw new PXArgumentException(ErrorMessages.ArgumentNullException);
            }

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

            Location loc = (Location)PXSelect <Location,
                                               Where <Location.bAccountID, Equal <Required <Location.bAccountID> >, And <Location.locationID, Equal <Required <Location.locationID> > > > > .
                           Select(Base, order.BillCustomerID, order.BillLocationID);

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

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

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

            GetTaxRequest request = new GetTaxRequest();

            request.CompanyCode        = CompanyCodeFromBranch(order.TaxZoneID, Base.Accessinfo.BranchID);
            request.CurrencyCode       = order.CuryID;
            request.CustomerCode       = cust.AcctCD;
            request.OriginAddress      = AddressConverter.ConvertTaxAddress(addressFrom);
            request.DestinationAddress = AddressConverter.ConvertTaxAddress(addressTo);
            request.DocCode            = $"CR.{order.SOID}";
            request.DocDate            = order.OrderDate.GetValueOrDefault();

            int mult = 1;

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

            request.DocType = TaxDocumentType.SalesOrder;

            PXSelectBase <FSSODet> select = new PXSelectJoin <FSSODet,
                                                              LeftJoin <InventoryItem, On <InventoryItem.inventoryID, Equal <FSSODet.inventoryID> >,
                                                                        LeftJoin <Account, On <Account.accountID, Equal <InventoryItem.salesAcctID> > > >,
                                                              Where <FSSODet.sOID, Equal <Current <FSServiceOrder.sOID> > >,
                                                              OrderBy <Asc <FSSODet.lineNbr> > >(Base);

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

                var line = new TaxCartItem();
                line.Index              = tran.LineNbr ?? 0;
                line.Amount             = mult * tran.CuryBillableTranAmt.GetValueOrDefault();
                line.Description        = tran.TranDesc;
                line.DestinationAddress = request.DestinationAddress;
                line.OriginAddress      = request.OriginAddress;
                line.ItemCode           = item.InventoryCD;
                line.Quantity           = Math.Abs(tran.BillableQty.GetValueOrDefault());
                line.Discounted         = request.Discount > 0;
                line.RevAcct            = salesAccount.AccountCD;
                line.TaxCode            = tran.TaxCategoryID;

                request.CartItems.Add(line);
            }

            return(request);
        }
        protected GetTaxRequest BuildGetTaxRequest(PMQuote quote)
        {
            if (quote == null)
            {
                throw new PXArgumentException(ErrorMessages.ArgumentNullException);
            }

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

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

            IAddressBase addressFrom = GetFromAddress(quote);
            IAddressBase addressTo   = GetToAddress(quote);

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

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

            GetTaxRequest request = new GetTaxRequest();

            request.CompanyCode        = CompanyCodeFromBranch(quote.TaxZoneID, Base.Accessinfo.BranchID);
            request.CurrencyCode       = quote.CuryID;
            request.CustomerCode       = cust.AcctCD;
            request.OriginAddress      = AddressConverter.ConvertTaxAddress(addressFrom);
            request.DestinationAddress = AddressConverter.ConvertTaxAddress(addressTo);
            request.DocCode            = string.Format("CR.{0}", quote.OpportunityID);
            request.DocDate            = quote.DocumentDate.GetValueOrDefault();
            request.Discount           = quote.CuryLineDocDiscountTotal.GetValueOrDefault();

            int mult = 1;

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

            request.DocType = TaxDocumentType.SalesOrder;

            var select = PXSelectJoin <CROpportunityProducts,
                                       LeftJoin <InventoryItem, On <InventoryItem.inventoryID, Equal <CROpportunityProducts.inventoryID> >,
                                                 LeftJoin <Account, On <Account.accountID, Equal <InventoryItem.salesAcctID> > > >,
                                       Where <CROpportunityProducts.quoteID, Equal <Required <PMQuote.quoteID> > >,
                                       OrderBy <Asc <CROpportunityProducts.lineNbr> > > .Select(Base, quote.QuoteID);

            foreach (PXResult <CROpportunityProducts, InventoryItem, Account> res in select)
            {
                CROpportunityProducts tran = (CROpportunityProducts)res;
                InventoryItem         item = (InventoryItem)res;
                Account salesAccount       = (Account)res;

                var line = new TaxCartItem();
                line.Index              = tran.LineNbr ?? 0;
                line.Amount             = mult * tran.CuryAmount.GetValueOrDefault();
                line.Description        = tran.Descr;
                line.DestinationAddress = request.DestinationAddress;
                line.OriginAddress      = request.OriginAddress;
                line.ItemCode           = item.InventoryCD;
                line.Quantity           = tran.Qty.GetValueOrDefault();
                line.Discounted         = request.Discount > 0;
                line.RevAcct            = salesAccount.AccountCD;
                line.TaxCode            = tran.TaxCategoryID;

                request.CartItems.Add(line);
            }

            return(request);
        }
Exemplo n.º 9
0
        public virtual GetTaxRequest BuildGetTaxRequest(PMProforma doc)
        {
            if (doc == null)
            {
                throw new PXArgumentException(ErrorMessages.ArgumentNullException);
            }

            Customer cust = (Customer)Base.Customer.View.SelectSingleBound(new object[] { doc });
            Location loc  = (Location)Base.Location.View.SelectSingleBound(new object[] { doc });

            IAddressBase fromAddress = GetFromAddress(doc);
            IAddressBase toAddress   = GetToAddress(doc);

            if (fromAddress == null)
            {
                throw new PXException(Messages.FailedGetFromAddress);
            }

            if (toAddress == null)
            {
                throw new PXException(Messages.FailedGetToAddress);
            }

            GetTaxRequest request = new GetTaxRequest();

            request.CompanyCode        = CompanyCodeFromBranch(doc.TaxZoneID, doc.BranchID);
            request.CurrencyCode       = doc.CuryID;
            request.CustomerCode       = cust.AcctCD;
            request.OriginAddress      = AddressConverter.ConvertTaxAddress(fromAddress);
            request.DestinationAddress = AddressConverter.ConvertTaxAddress(toAddress);
            request.DocCode            = $"PM.{doc.RefNbr}";
            request.DocDate            = doc.InvoiceDate.GetValueOrDefault();
            request.LocationCode       = GetExternalTaxProviderLocationCode(doc);
            request.DocType            = TaxDocumentType.SalesOrder;

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

            foreach (PMProformaProgressLine tran in Base.ProgressiveLines.Select())
            {
                InventoryItem item         = (InventoryItem)PXSelectorAttribute.Select <PMProformaProgressLine.inventoryID>(Base.ProgressiveLines.Cache, tran);
                Account       salesAccount = (Account)PXSelectorAttribute.Select <PMProformaProgressLine.accountID>(Base.ProgressiveLines.Cache, tran);

                var line = new TaxCartItem();
                line.Index = tran.LineNbr ?? 0;

                line.Amount = tran.CuryLineTotal.GetValueOrDefault();

                line.Description        = tran.Description;
                line.DestinationAddress = request.DestinationAddress;
                line.OriginAddress      = request.OriginAddress;
                line.ItemCode           = item.InventoryCD;
                line.Quantity           = tran.Qty.GetValueOrDefault();
                line.Discounted         = request.Discount > 0;
                line.RevAcct            = salesAccount.AccountCD;

                line.TaxCode = tran.TaxCategoryID;

                request.CartItems.Add(line);
            }

            foreach (PMProformaTransactLine tran in Base.TransactionLines.Select())
            {
                InventoryItem item         = (InventoryItem)PXSelectorAttribute.Select <PMProformaTransactLine.inventoryID>(Base.TransactionLines.Cache, tran);
                Account       salesAccount = (Account)PXSelectorAttribute.Select <PMProformaTransactLine.accountID>(Base.TransactionLines.Cache, tran);

                var line = new TaxCartItem();
                line.Index = tran.LineNbr ?? 0;

                line.Amount = tran.CuryLineTotal.GetValueOrDefault();

                line.Description        = tran.Description;
                line.DestinationAddress = request.DestinationAddress;
                line.OriginAddress      = request.OriginAddress;
                line.ItemCode           = item.InventoryCD;
                line.Quantity           = tran.Qty.GetValueOrDefault();
                line.Discounted         = request.Discount > 0;
                line.RevAcct            = salesAccount.AccountCD;

                line.TaxCode = tran.TaxCategoryID;

                request.CartItems.Add(line);
            }

            return(request);
        }
        protected virtual GetTaxRequest BuildGetTaxRequest(FSAppointment fsAppointment, FSServiceOrder order)
        {
            if (fsAppointment == null)
            {
                throw new PXArgumentException(nameof(fsAppointment));
            }


            Customer cust = (Customer)Base.TaxCustomer.View.SelectSingleBound(new object[] { fsAppointment });
            Location loc  = (Location)Base.TaxLocation.View.SelectSingleBound(new object[] { fsAppointment });

            IAddressBase fromAddress = GetFromAddress(order);
            IAddressBase toAddress   = GetToAddress(order);

            if (fromAddress == null)
            {
                throw new PXException(PX.Objects.CR.Messages.FailedGetFromAddressCR);
            }

            if (toAddress == null)
            {
                throw new PXException(PX.Objects.CR.Messages.FailedGetToAddressCR);
            }

            GetTaxRequest request = new GetTaxRequest();

            request.CompanyCode        = CompanyCodeFromBranch(fsAppointment.TaxZoneID, fsAppointment.BranchID);
            request.CurrencyCode       = fsAppointment.CuryID;
            request.CustomerCode       = cust.AcctCD;
            request.OriginAddress      = AddressConverter.ConvertTaxAddress(fromAddress);
            request.DestinationAddress = AddressConverter.ConvertTaxAddress(toAddress);
            request.DocCode            = string.Format("SO.{0}.{1}", fsAppointment.SrvOrdType, fsAppointment.RefNbr);
            request.DocDate            = fsAppointment.ScheduledDateTimeBegin.GetValueOrDefault();
            request.LocationCode       = GetExternalTaxProviderLocationCode(fsAppointment);

            Sign sign = Sign.Plus;

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

            FSSrvOrdType srvOrdType = (FSSrvOrdType)Base.ServiceOrderTypeSelected.View.SelectSingleBound(new object[] { fsAppointment });

            /*if (srvOrdType.DefaultOperation == SOOperation.Receipt)
             * {
             *  request.DocType = TaxDocumentType.ReturnOrder;
             *  sign = Sign.Minus;
             *
             *  PXSelectBase<FSAppointmentDet> selectLineWithInvoiceDate = new PXSelect<FSAppointmentDet,
             *  Where<FSAppointmentDet.srvOrdType, Equal<Required<FSAppointmentDet.srvOrdType>>, And<FSAppointmentDet.refNbr, Equal<Required<FSAppointmentDet.refNbr>>,
             *  And<FSAppointmentDet.invoiceDate, IsNotNull>>>>(Base);
             *
             *  FSAppointmentDet soLine = selectLineWithInvoiceDate.SelectSingle(order.SrvOrdType, order.RefNbr);
             *  if (soLine != null && soLine.TranDate != null)
             *  {
             *      request.TaxOverride.Reason = PX.Objects.SO.Messages.ReturnReason;
             *      request.TaxOverride.TaxDate = soLine.TranDate.Value;
             *      request.TaxOverride.TaxOverrideType = TaxOverrideType.TaxDate;
             *  }
             *
             * }
             * else
             * {*/
            request.DocType = TaxDocumentType.SalesOrder;
            /*}*/


            /* We need InnerJoin with InventoryItem instead of LeftJoin */
            /* because of instructions and comments lines */
            PXSelectBase <FSAppointmentDet> select = new PXSelectJoin <FSAppointmentDet,
                                                                       InnerJoin <InventoryItem, On <InventoryItem.inventoryID, Equal <FSAppointmentDet.inventoryID> >,
                                                                                  LeftJoin <Account, On <Account.accountID, Equal <FSAppointmentDet.acctID> > > >,
                                                                       Where <FSAppointmentDet.srvOrdType, Equal <Current <FSAppointment.srvOrdType> >,
                                                                              And <FSAppointmentDet.refNbr, Equal <Current <FSAppointment.refNbr> > > >,
                                                                       OrderBy <Asc <FSAppointmentDet.srvOrdType, Asc <FSAppointmentDet.refNbr, Asc <FSAppointmentDet.lineNbr> > > > >(Base);

            request.Discount = fsAppointment.CuryDiscTot.GetValueOrDefault();

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

                var line = new TaxCartItem();
                line.Index = tran.LineNbr ?? 0;

                /*if (srvOrdType.DefaultOperation != tran.Operation)
                 *  line.Amount = Sign.Minus * sign * tran.CuryLineAmt.GetValueOrDefault();
                 * else
                 *  line.Amount = sign * tran.CuryLineAmt.GetValueOrDefault();*/
                line.Amount = sign * tran.CuryBillableTranAmt.GetValueOrDefault();

                line.Description        = tran.TranDesc;
                line.DestinationAddress = AddressConverter.ConvertTaxAddress(GetToAddress(order, tran));
                line.OriginAddress      = AddressConverter.ConvertTaxAddress(GetFromAddress(order, tran));
                line.ItemCode           = item.InventoryCD;
                line.Quantity           = Math.Abs(tran.Qty.GetValueOrDefault());
                line.Discounted         = request.Discount > 0;
                line.RevAcct            = salesAccount.AccountCD;

                line.TaxCode = tran.TaxCategoryID;

                request.CartItems.Add(line);
            }

            return(request);
        }
        protected virtual GetTaxRequest BuildTaxRequest(APInvoice invoice)
        {
            if (invoice == null)
            {
                throw new PXArgumentException(nameof(invoice), ErrorMessages.ArgumentNullException);
            }

            Vendor vend = (Vendor)Base.vendor.View.SelectSingleBound(new object[] { invoice });

            var request = new PX.TaxProvider.GetTaxRequest();

            request.CompanyCode  = CompanyCodeFromBranch(invoice.TaxZoneID, invoice.BranchID);
            request.CurrencyCode = invoice.CuryID;
            request.CustomerCode = vend.AcctCD;
            IAddressBase fromAddress = GetFromAddress(invoice);
            IAddressBase toAddress   = GetToAddress(invoice);

            if (fromAddress == null)
            {
                throw new PXException(Messages.FailedGetFrom);
            }

            if (toAddress == null)
            {
                throw new PXException(Messages.FailedGetTo);
            }

            request.OriginAddress      = AddressConverter.ConvertTaxAddress(fromAddress);
            request.DestinationAddress = AddressConverter.ConvertTaxAddress(toAddress);
            request.DocCode            = $"AP.{invoice.DocType}.{invoice.RefNbr}";
            request.DocDate            = invoice.DocDate.GetValueOrDefault();
            request.LocationCode       = GetExternalTaxProviderLocationCode(invoice);

            Location branchLoc = GetBranchLocation(invoice);

            if (branchLoc != null)
            {
                request.CustomerUsageType = branchLoc.CAvalaraCustomerUsageType;
                request.ExemptionNo       = branchLoc.CAvalaraExemptionNumber;
            }

            request.DocType = TaxDocumentType.PurchaseInvoice;
            int mult = 1;

            switch (invoice.DocType)
            {
            case APDocType.Invoice:
            case APDocType.CreditAdj:
                request.DocType = TaxDocumentType.PurchaseInvoice;
                break;

            case APDocType.DebitAdj:
                if (invoice.OrigDocDate != null)
                {
                    request.TaxOverride.Reason          = Messages.DebitAdjustmentReason;
                    request.TaxOverride.TaxDate         = invoice.OrigDocDate.Value;
                    request.TaxOverride.TaxOverrideType = PX.TaxProvider.TaxOverrideType.TaxDate;
                    mult = -1;
                }
                request.DocType = TaxDocumentType.ReturnInvoice;
                break;

            default:
                throw new PXException(Messages.DocTypeNotSupported);
            }

            PXSelectBase <APTran> select = new PXSelectJoin <APTran,
                                                             LeftJoin <InventoryItem, On <InventoryItem.inventoryID, Equal <APTran.inventoryID> >,
                                                                       LeftJoin <Account, On <Account.accountID, Equal <APTran.accountID> > > >,
                                                             Where <APTran.tranType, Equal <Current <APInvoice.docType> >,
                                                                    And <APTran.refNbr, Equal <Current <APInvoice.refNbr> >,
                                                                         And <APTran.lineType, NotEqual <SOLineType.discount> > > >,
                                                             OrderBy <Asc <APTran.tranType, Asc <APTran.refNbr, Asc <APTran.lineNbr> > > > >(Base);

            request.Discount = GetDocDiscount().GetValueOrDefault();
            foreach (PXResult <APTran, InventoryItem, Account> res in select.View.SelectMultiBound(new object[] { invoice }))
            {
                APTran        tran         = (APTran)res;
                InventoryItem item         = (InventoryItem)res;
                Account       salesAccount = (Account)res;

                var line = new TaxCartItem();
                line.Index              = tran.LineNbr.GetValueOrDefault();
                line.Amount             = mult * tran.CuryTranAmt.GetValueOrDefault();
                line.Description        = tran.TranDesc;
                line.OriginAddress      = AddressConverter.ConvertTaxAddress(GetFromAddress(invoice, tran));
                line.DestinationAddress = AddressConverter.ConvertTaxAddress(GetToAddress(invoice, tran));
                line.ItemCode           = item.InventoryCD;
                line.Quantity           = Math.Abs(tran.Qty.GetValueOrDefault());
                line.Discounted         = request.Discount > 0;
                line.RevAcct            = salesAccount.AccountCD;

                line.TaxCode = tran.TaxCategoryID;

                request.CartItems.Add(line);
            }

            return(request);
        }