protected virtual void FSAppointmentTaxTran_RowPersisting(PXCache sender, PXRowPersistingEventArgs e) { FSAppointmentTaxTran row = e.Row as FSAppointmentTaxTran; if (row == null) { return; } if (e.Operation == PXDBOperation.Delete) { FSAppointmentTax tax = (FSAppointmentTax)Base.TaxLines.Cache.Locate((FSAppointmentTax)row); if (Base.TaxLines.Cache.GetStatus(tax) == PXEntryStatus.Deleted || Base.TaxLines.Cache.GetStatus(tax) == PXEntryStatus.InsertedDeleted) { e.Cancel = true; } } if (e.Operation == PXDBOperation.Update) { FSAppointmentTax tax = (FSAppointmentTax)Base.TaxLines.Cache.Locate((FSAppointmentTax)row); if (Base.TaxLines.Cache.GetStatus(tax) == PXEntryStatus.Updated) { e.Cancel = true; } } }
public void ClearTaxes(FSAppointment appointmentRow) { if (appointmentRow == null) { return; } if (IsExternalTax(appointmentRow.TaxZoneID)) { foreach (PXResult <FSAppointmentTaxTran, Tax> res in Taxes.View.SelectMultiBound(new object[] { appointmentRow })) { FSAppointmentTaxTran taxTran = (FSAppointmentTaxTran)res; Taxes.Delete(taxTran); } appointmentRow.CuryTaxTotal = 0; appointmentRow.CuryDocTotal = GetCuryDocTotal(appointmentRow.CuryBillableLineTotal, appointmentRow.CuryDiscTot, 0, 0); } }
protected void ApplyTax(FSAppointment order, GetTaxResult result) { TaxZone taxZone = null; AP.Vendor vendor = null; if (result.TaxSummary.Length > 0) { taxZone = (TaxZone)PXSetup <TaxZone, Where <TaxZone.taxZoneID, Equal <Required <FSAppointment.taxZoneID> > > > .Select(Base, order.TaxZoneID); vendor = (AP.Vendor) PXSelect <AP.Vendor, Where <AP.Vendor.bAccountID, Equal <Required <AP.Vendor.bAccountID> > > > .Select(Base, taxZone.TaxVendorID); if (vendor == null) { throw new PXException(PX.Objects.CR.Messages.ExternalTaxVendorNotFound); } } //Clear all existing Tax transactions: foreach (PXResult <FSAppointmentTaxTran, Tax> res in Base.Taxes.View.SelectMultiBound(new object[] { order })) { FSAppointmentTaxTran taxTran = (FSAppointmentTaxTran)res; Base.Taxes.Delete(taxTran); } Base.Views.Caches.Add(typeof(Tax)); for (int i = 0; i < result.TaxSummary.Length; i++) { string taxID = 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(Base, taxID); if (tx == null) { tx = new Tax(); tx.TaxID = taxID; tx.Descr = PXMessages.LocalizeFormatNoPrefixNLA(PX.Objects.TX.Messages.ExternalTaxProviderTaxId, taxID); tx.TaxType = CSTaxType.Sales; tx.TaxCalcType = CSTaxCalcType.Doc; tx.TaxCalcLevel = result.TaxSummary[i].TaxCalculationLevel.ToCSTaxCalcLevel(); 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; Base.Caches[typeof(Tax)].Insert(tx); } FSAppointmentTaxTran tax = new FSAppointmentTaxTran(); tax.EntityType = ID.PostDoc_EntityType.APPOINTMENT; tax.EntityID = order.AppointmentID; tax.TaxID = taxID; tax.CuryTaxAmt = Math.Abs(result.TaxSummary[i].TaxAmount); tax.CuryTaxableAmt = Math.Abs(result.TaxSummary[i].TaxableAmount); tax.TaxRate = Convert.ToDecimal(result.TaxSummary[i].Rate) * 100; Base.Taxes.Insert(tax); } Base.AppointmentSelected.SetValueExt <FSAppointment.curyTaxTotal>(order, Math.Abs(result.TotalTaxAmount)); decimal?СuryDocTotal = AppointmentEntry.GetCuryDocTotal(order.CuryBillableLineTotal, order.CuryDiscTot, order.CuryTaxTotal, 0); Base.AppointmentSelected.SetValueExt <FSAppointment.curyDocTotal>(order, СuryDocTotal ?? 0m); }
/*protected virtual GetTaxRequest BuildGetTaxRequestOpen(FSAppointment order) * { * Stopwatch sw = new Stopwatch(); * sw.Start(); * * if (order == null) * throw new PXArgumentException(ErrorMessages.ArgumentNullException); * * Customer cust = (Customer)Base.TaxCustomer.View.SelectSingleBound(new object[] { order }); * Location loc = (Location)Base.TaxLocation.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 = cust.AcctCD; * request.OriginAddress = AddressConverter.ConvertTaxAddress(fromAddress); * request.DestinationAddress = AddressConverter.ConvertTaxAddress(toAddress); * request.DocCode = string.Format("SO.{0}.{1}", order.SrvOrdType, order.RefNbr); * request.DocDate = order.ScheduledDateTimeBegin.GetValueOrDefault(); * request.LocationCode = GetExternalTaxProviderLocationCode(order); * * int mult = 1; * * if (!string.IsNullOrEmpty(loc.CAvalaraCustomerUsageType)) * { * request.CustomerUsageType = loc.CAvalaraCustomerUsageType; * } * if (!string.IsNullOrEmpty(loc.CAvalaraExemptionNumber)) * { * request.ExemptionNo = loc.CAvalaraExemptionNumber; * } * * FSSrvOrdType srvOrdType = (FSSrvOrdType)Base.ServiceOrderTypeSelected.View.SelectSingleBound(new object[] { order }); * * if (srvOrdType.DefaultOperation == SOOperation.Receipt) * { * request.DocType = TaxDocumentType.ReturnOrder; * mult = -1; * * 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 = Messages.ReturnReason; * request.TaxOverride.TaxDate = soLine.TranDate.Value; * request.TaxOverride.TaxOverrideType = TaxOverrideType.TaxDate; * } * * } * else * { * request.DocType = TaxDocumentType.SalesOrder; * } * request.DocType = TaxDocumentType.SalesOrder; * * * PXSelectBase<FSAppointmentDet> select = new PXSelectJoin<FSAppointmentDet, * LeftJoin<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 = order.CuryDiscTot.GetValueOrDefault(); * * foreach (PXResult<FSAppointmentDet, InventoryItem, Account> res in select.View.SelectMultiBound(new object[] { order })) * { * FSAppointmentDet tran = (FSAppointmentDet)res; * InventoryItem item = (InventoryItem)res; * Account salesAccount = (Account)res; * * if (tran.OpenAmt >= 0) * { * var line = new TaxCartItem(); * line.Index = tran.LineNbr ?? 0; * if (srvOrdType.DefaultOperation != tran.Operation) * line.Amount = -1 * mult * tran.CuryOpenAmt.GetValueOrDefault(); * else * line.Amount = mult * tran.CuryOpenAmt.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.OpenQty.GetValueOrDefault()); * line.Discounted = request.Discount > 0; * line.RevAcct = salesAccount.AccountCD; * * line.TaxCode = tran.TaxCategoryID; * * request.CartItems.Add(line); * } * } * * sw.Stop(); * Debug.Print("BuildGetTaxRequestOpen() in {0} millisec.", sw.ElapsedMilliseconds); * * return request; * } */ /*protected virtual GetTaxRequest BuildGetTaxRequestUnbilled(FSAppointment order) * { * Stopwatch sw = new Stopwatch(); * sw.Start(); * * if (order == null) * throw new PXArgumentException(ErrorMessages.ArgumentNullException); * * Customer cust = (Customer)Base.TaxCustomer.View.SelectSingleBound(new object[] { order }); * Location loc = (Location)Base.TaxLocation.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 = cust.AcctCD; * request.OriginAddress = AddressConverter.ConvertTaxAddress(fromAddress); * request.DestinationAddress = AddressConverter.ConvertTaxAddress(toAddress); * request.DocCode = string.Format("{0}.{1}.Open", order.SrvOrdType, order.RefNbr); * request.DocDate = order.ScheduledDateTimeBegin.GetValueOrDefault(); * request.LocationCode = GetExternalTaxProviderLocationCode(order); * * int mult = 1; * * if (!string.IsNullOrEmpty(order.AvalaraCustomerUsageType)) * { * request.CustomerUsageType = order.AvalaraCustomerUsageType; * } * if (!string.IsNullOrEmpty(loc.CAvalaraExemptionNumber)) * { * request.ExemptionNo = loc.CAvalaraExemptionNumber; * } * * FSSrvOrdType srvOrdType = (FSSrvOrdType)Base.ServiceOrderTypeSelected.View.SelectSingleBound(new object[] { order }); * * if (srvOrdType.DefaultOperation == SOOperation.Receipt) * { * request.DocType = TaxDocumentType.ReturnOrder; * mult = -1; * * 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 = Messages.ReturnReason; * request.TaxOverride.TaxDate = soLine.TranDate.Value; * request.TaxOverride.TaxOverrideType = TaxOverrideType.TaxDate; * } * * } * else * { * request.DocType = TaxDocumentType.SalesOrder; * } * * * PXSelectBase<FSAppointmentDet> select = new PXSelectJoin<FSAppointmentDet, * LeftJoin<InventoryItem, On<InventoryItem.inventoryID, Equal<FSAppointmentDet.inventoryID>>, * LeftJoin<Account, On<Account.accountID, Equal<FSAppointmentDet.salesAcctID>>>>, * 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 = order.CuryDiscTot.GetValueOrDefault(); * * foreach (PXResult<FSAppointmentDet, InventoryItem, Account> res in select.View.SelectMultiBound(new object[] { order })) * { * FSAppointmentDet tran = (FSAppointmentDet)res; * InventoryItem item = (InventoryItem)res; * Account salesAccount = (Account)res; * * if (tran.UnbilledAmt >= 0) * { * var line = new TaxCartItem(); * line.Index = tran.LineNbr ?? 0; * if (srvOrdType.DefaultOperation != tran.Operation) * line.Amount = -1 * mult * tran.CuryUnbilledAmt.GetValueOrDefault(); * else * line.Amount = mult * tran.CuryUnbilledAmt.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.UnbilledQty.GetValueOrDefault()); * line.Discounted = request.Discount > 0; * line.RevAcct = salesAccount.AccountCD; * * line.TaxCode = tran.TaxCategoryID; * * request.CartItems.Add(line); * } * } * * sw.Stop(); * Debug.Print("BuildGetTaxRequestUnbilled() in {0} millisec.", sw.ElapsedMilliseconds); * * return request; * } */ /*protected virtual GetTaxRequest BuildGetTaxRequestFreight(FSAppointment order) * { * Stopwatch sw = new Stopwatch(); * sw.Start(); * * if (order == null) * throw new PXArgumentException(ErrorMessages.ArgumentNullException); * * Customer cust = (Customer)Base.TaxCustomer.View.SelectSingleBound(new object[] { order }); * Location loc = (Location)Base.TaxLocation.View.SelectSingleBound(new object[] { order }); * * 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(order.TaxZoneID, order.BranchID); * request.CurrencyCode = order.CuryID; * request.CustomerCode = cust.AcctCD; * request.OriginAddress = AddressConverter.ConvertTaxAddress(fromAddress); * request.DestinationAddress = AddressConverter.ConvertTaxAddress(toAddress); * request.DocCode = $"{order.SrvOrdType}.{order.RefNbr}.Freight"; * request.DocDate = order.ScheduledDateTimeBegin.GetValueOrDefault(); * request.LocationCode = GetExternalTaxProviderLocationCode(order); * * int mult = 1; * * if (!string.IsNullOrEmpty(loc.CAvalaraCustomerUsageType)) * { * request.CustomerUsageType = loc.CAvalaraCustomerUsageType; * } * if (!string.IsNullOrEmpty(loc.CAvalaraExemptionNumber)) * { * request.ExemptionNo = loc.CAvalaraExemptionNumber; * } * * FSSrvOrdType srvOrdType = (FSSrvOrdType)Base.ServiceOrderTypeSelected.View.SelectSingleBound(new object[] { order }); * * if (srvOrdType.ARDocType == ARDocType.CreditMemo) * { * request.DocType = TaxDocumentType.ReturnOrder; * mult = -1; * } * else * { * request.DocType = TaxDocumentType.SalesOrder; * } * * if (order.CuryFreightTot > 0) * { * var line = new TaxCartItem(); * line.Index = short.MaxValue; * line.Amount = mult * 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); * } * * sw.Stop(); * Debug.Print("BuildGetTaxRequestFreight() in {0} millisec.", sw.ElapsedMilliseconds); * * return request; * } */ protected virtual void ApplyTax(FSAppointment order, GetTaxResult result, GetTaxResult resultOpen, GetTaxResult resultUnbilled, GetTaxResult resultFreight) { TaxZone taxZone = (TaxZone)Base.TaxZone.View.SelectSingleBound(new object[] { order }); if (taxZone == null) { throw new PXException(PX.Objects.SO.Messages.TaxZoneIsNotSet); } AP.Vendor vendor = PXSelect <AP.Vendor, Where <AP.Vendor.bAccountID, Equal <Required <AP.Vendor.bAccountID> > > > .Select(Base, taxZone.TaxVendorID); if (vendor == null) { throw new PXException(PX.Objects.CR.Messages.ExternalTaxVendorNotFound); } /*var sign = ((FSSrvOrdType)Base.ServiceOrderTypeSelected.View.SelectSingleBound(new object[] { order })).DefaultOperation == SOOperation.Receipt * ? Sign.Minus * : Sign.Plus;*/ var sign = Sign.Plus; if (result != null) { //Clear all existing Tax transactions: foreach (PXResult <FSAppointmentTaxTran, Tax> res in Base.Taxes.View.SelectMultiBound(new object[] { order })) { FSAppointmentTaxTran taxTran = res; Base.Taxes.Delete(taxTran); } Base.Views.Caches.Add(typeof(Tax)); decimal freightTax = 0; if (resultFreight != null) { freightTax = sign * resultFreight.TotalTaxAmount; } //bool requireControlTotal = Base.ServiceOrderTypeSelected.Current.RequireControlTotal == true; /*if (order.Hold != true) * Base.ServiceOrderTypeSelected.Current.RequireControlTotal = false;*/ var taxDetails = new List <PX.TaxProvider.TaxDetail>(); foreach (TaxProvider.TaxDetail tax in result.TaxSummary.OrderByDescending(e => e.TaxAmount)) { if (tax.TaxAmount != 0 || taxDetails.Find(e => e.TaxName == tax.TaxName) == default(TaxProvider.TaxDetail)) { taxDetails.Add(tax); } } if (resultFreight != null) { foreach (TaxProvider.TaxDetail tax in resultFreight.TaxSummary.OrderByDescending(e => e.TaxAmount)) { if (tax.TaxAmount != 0 || taxDetails.Find(e => e.TaxName == tax.TaxName) == default(TaxProvider.TaxDetail)) { taxDetails.Add(tax); } } } try { foreach (var taxDetail in taxDetails) { string taxID = taxDetail.TaxName; if (string.IsNullOrEmpty(taxID)) { taxID = taxDetail.JurisCode; } if (string.IsNullOrEmpty(taxID)) { PXTrace.WriteInformation(PX.Objects.SO.Messages.EmptyValuesFromExternalTaxProvider); continue; } CreateTax(Base, taxZone, vendor, taxDetail, taxID); FSAppointmentTaxTran tax = (FSAppointmentTaxTran)Base.Taxes.Cache.CreateInstance();; tax.TaxID = taxID; tax.CuryTaxAmt = Math.Abs(taxDetail.TaxAmount); tax.CuryTaxableAmt = Math.Abs(taxDetail.TaxableAmount); tax.TaxRate = Convert.ToDecimal(taxDetail.Rate) * 100; tax.JurisType = taxDetail.JurisType; tax.JurisName = taxDetail.JurisName; Base.Taxes.Insert(tax); } Base.AppointmentSelected.SetValueExt <FSAppointment.curyTaxTotal>(order, sign * result.TotalTaxAmount + freightTax); decimal?СuryDocTotal = AppointmentEntry.GetCuryDocTotal(order.CuryBillableLineTotal, order.CuryLogBillableTranAmountTotal, order.CuryDiscTot, order.CuryTaxTotal, 0); Base.AppointmentSelected.SetValueExt <FSAppointment.curyDocTotal>(order, СuryDocTotal ?? 0m); } finally { //Base.ServiceOrderTypeSelected.Current.RequireControlTotal = requireControlTotal; } } /*if (resultUnbilled != null) * Base.AppointmentRecords.SetValueExt<FSAppointment.curyUnbilledTaxTotal>(order, sign * resultUnbilled.TotalTaxAmount); * * if (resultOpen != null) * Base.AppointmentRecords.SetValueExt<FSAppointment.curyOpenTaxTotal>(order, sign * resultOpen.TotalTaxAmount);*/ order = (FSAppointment)Base.AppointmentSelected.Cache.CreateCopy(order); order.IsTaxValid = true; Base.AppointmentSelected.Cache.Update(order); if (Base.TimeStamp == null) { Base.SelectTimeStamp(); } SkipTaxCalcAndSave(); }