public static bool CalculateDiscountedTaxes(PXCache cache, APTaxTran aptaxTran, decimal cashDiscPercent) { bool? result = null; object value = null; IBqlCreator whereTaxable = (IBqlCreator)Activator.CreateInstance(typeof(WhereAPPPDTaxable <Required <APTaxTran.taxID> >)); whereTaxable.Verify(cache, aptaxTran, new List <object> { aptaxTran.TaxID }, ref result, ref value); aptaxTran.CuryDiscountedTaxableAmt = cashDiscPercent == 0m ? aptaxTran.CuryTaxableAmt : PXDBCurrencyAttribute.RoundCury(cache, aptaxTran, (decimal)(aptaxTran.CuryTaxableAmt * (1m - cashDiscPercent))); aptaxTran.CuryDiscountedPrice = cashDiscPercent == 0m ? aptaxTran.CuryTaxAmt : PXDBCurrencyAttribute.RoundCury(cache, aptaxTran, (decimal)(aptaxTran.TaxRate / 100m * aptaxTran.CuryDiscountedTaxableAmt)); return(result == true); }
protected virtual void ApplyTax(APInvoice invoice, GetTaxResult result) { TaxZone taxZone = null; AP.Vendor vendor = null; if (result.TaxSummary.Length > 0) { taxZone = (TaxZone)Base.taxzone.View.SelectSingleBound(new object[] { invoice }); vendor = PXSelect <AP.Vendor, Where <AP.Vendor.bAccountID, Equal <Required <AP.Vendor.bAccountID> > > > .Select(Base, taxZone.TaxVendorID); if (vendor == null) { throw new PXException(TX.Messages.ExternalTaxVendorNotFound); } if (vendor.SalesTaxAcctID == null) { throw new PXSetPropertyException(TX.Messages.TaxPayableAccountNotSpecified, vendor.AcctCD); } if (vendor.SalesTaxSubID == null) { throw new PXException(TX.Messages.TaxPayableSubNotSpecified, vendor.AcctCD); } } //Clear all existing Tax transactions: foreach (PXResult <APTaxTran, Tax> res in Base.Taxes.View.SelectMultiBound(new object[] { invoice })) { APTaxTran taxTran = (APTaxTran)res; Base.Taxes.Delete(taxTran); } Base.Views.Caches.Add(typeof(Tax)); for (int i = 0; i < result.TaxSummary.Length; i++) { string taxID = result.TaxSummary[i].TaxName; if (string.IsNullOrEmpty(taxID)) { taxID = result.TaxSummary[i].JurisCode; } if (string.IsNullOrEmpty(taxID)) { PXTrace.WriteInformation(Messages.EmptyValuesFromExternalTaxProvider); continue; } CreateTax(Base, taxZone, vendor, result.TaxSummary[i], taxID); APTaxTran tax = new APTaxTran(); tax.Module = BatchModule.AP; tax.TranType = invoice.DocType; tax.RefNbr = invoice.RefNbr; 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; tax.TaxType = "S"; tax.TaxBucketID = 0; tax.AccountID = vendor.SalesTaxAcctID; tax.SubID = vendor.SalesTaxSubID; tax.JurisType = result.TaxSummary[i].JurisType; tax.JurisName = result.TaxSummary[i].JurisName; Base.Taxes.Insert(tax); } bool requireControlTotal = Base.APSetup.Current.RequireControlTotal == true; if (invoice.Hold != true) { Base.APSetup.Current.RequireControlTotal = false; } try { invoice.CuryTaxTotal = Math.Abs(result.TotalTaxAmount); Base.Document.Cache.SetValueExt <APInvoice.isTaxSaved>(invoice, true); } finally { Base.APSetup.Current.RequireControlTotal = requireControlTotal; } }
public virtual APInvoice ReleaseRetainageProc(APInvoice doc, RetainageOptions retainageOpts, bool isAutoRelease = false) { Base.Clear(PXClearOption.PreserveTimeStamp); if (retainageOpts.CuryRetainageAmt <= 0 || retainageOpts.CuryRetainageAmt > doc.CuryRetainageUnreleasedAmt) { throw new PXException(Messages.IncorrectRetainageAmount); } // Magic. We need to prevent rewriting of CurrencyInfo.IsReadOnly // by true in CurrencyInfoView // Base.CurrentDocument.Cache.AllowUpdate = true; PXResult <APInvoice, CurrencyInfo, Terms, Vendor> resultDoc = APInvoice_CurrencyInfo_Terms_Vendor .SelectSingleBound(Base, null, doc.DocType, doc.RefNbr, doc.VendorID) .Cast <PXResult <APInvoice, CurrencyInfo, Terms, Vendor> >() .First(); CurrencyInfo info = resultDoc; APInvoice origInvoice = resultDoc; Vendor vendor = resultDoc; CurrencyInfo new_info = PXCache <CurrencyInfo> .CreateCopy(info); new_info.CuryInfoID = null; new_info.IsReadOnly = false; new_info = PXCache <CurrencyInfo> .CreateCopy(Base.currencyinfo.Insert(new_info)); APInvoice invoice = PXCache <APInvoice> .CreateCopy(origInvoice); invoice.CuryInfoID = new_info.CuryInfoID; invoice.DocType = APDocType.Invoice; invoice.RefNbr = null; invoice.LineCntr = null; invoice.InvoiceNbr = retainageOpts.InvoiceNbr; // Must be set for _RowSelected event handler // invoice.OpenDoc = true; invoice.Released = false; Base.Document.Cache.SetDefaultExt <APInvoice.isMigratedRecord>(invoice); invoice.BatchNbr = null; invoice.PrebookBatchNbr = null; invoice.Prebooked = false; invoice.ScheduleID = null; invoice.Scheduled = false; invoice.NoteID = null; invoice.DueDate = null; invoice.DiscDate = null; invoice.CuryOrigDiscAmt = 0m; invoice.OrigDocType = origInvoice.DocType; invoice.OrigRefNbr = origInvoice.RefNbr; invoice.OrigDocDate = origInvoice.DocDate; invoice.PaySel = false; invoice.CuryLineTotal = 0m; invoice.IsTaxPosted = false; invoice.IsTaxValid = false; invoice.CuryVatTaxableTotal = 0m; invoice.CuryVatExemptTotal = 0m; invoice.CuryDocBal = 0m; invoice.CuryOrigDocAmt = retainageOpts.CuryRetainageAmt; invoice.Hold = !isAutoRelease && Base.apsetup.Current.HoldEntry == true || Base.IsApprovalRequired(invoice, Base.Document.Cache); invoice.DocDate = retainageOpts.DocDate; invoice.FinPeriodID = retainageOpts.FinPeriodID; Base.ClearRetainageSummary(invoice); invoice.RetainageApply = false; invoice.IsRetainageDocument = true; invoice = Base.Document.Insert(invoice); if (new_info != null) { CurrencyInfo b_info = (CurrencyInfo)PXSelect <CurrencyInfo, Where <CurrencyInfo.curyInfoID, Equal <Current <APInvoice.curyInfoID> > > > .Select(Base); b_info.CuryID = new_info.CuryID; b_info.CuryEffDate = new_info.CuryEffDate; b_info.CuryRateTypeID = new_info.CuryRateTypeID; b_info.CuryRate = new_info.CuryRate; b_info.RecipRate = new_info.RecipRate; b_info.CuryMultDiv = new_info.CuryMultDiv; Base.currencyinfo.Update(b_info); } decimal retainagePercent = (decimal)(retainageOpts.CuryRetainageAmt / doc.CuryRetainageTotal); PXResultset <APTran> details = PXSelectGroupBy <APTran, Where <APTran.tranType, Equal <Required <APTran.tranType> >, And <APTran.refNbr, Equal <Required <APTran.refNbr> >, And <APTran.curyRetainageAmt, NotEqual <decimal0> > > >, Aggregate < GroupBy <APTran.taxCategoryID, Sum <APTran.curyRetainageAmt> > > > .Select(Base, doc.DocType, doc.RefNbr); APTran tranNew = null; decimal prevCuryTotal = 0m; TaxCalc oldTaxCalc = TaxBaseAttribute.GetTaxCalc <APTran.taxCategoryID>(Base.Transactions.Cache, null); TaxBaseAttribute.SetTaxCalc <APTran.taxCategoryID>(Base.Transactions.Cache, null, TaxCalc.ManualCalc); foreach (APTran detail in details) { // Create APTran record for chosen retainage amount, // clear all required fields to prevent tax calculation, // discount calculation and retainage calculation. // CuryUnitCost = 0m and CuryLineAmt = 0m here to prevent their // FieldDefaulting events, because in our case default value // should be equal to zero. // tranNew = Base.Transactions.Insert(new APTran { CuryUnitCost = 0m, CuryLineAmt = 0m }); tranNew.BranchID = origInvoice.BranchID; tranNew.TaxCategoryID = detail.TaxCategoryID; tranNew.AccountID = origInvoice.RetainageAcctID; tranNew.SubID = origInvoice.RetainageSubID; tranNew.ProjectID = ProjectDefaultAttribute.NonProject(); tranNew.Qty = 0m; tranNew.CuryUnitCost = 0m; tranNew.ManualDisc = true; tranNew.DiscPct = 0m; tranNew.CuryDiscAmt = 0m; tranNew.RetainagePct = 0m; tranNew.CuryRetainageAmt = 0m; tranNew.CuryTaxableAmt = 0m; tranNew.CuryTaxAmt = 0; tranNew.CuryExpenseAmt = 0m; tranNew.GroupDiscountRate = 1m; tranNew.DocumentDiscountRate = 1m; using (new PXLocaleScope(vendor.LocaleName)) { tranNew.TranDesc = PXMessages.LocalizeFormatNoPrefix( Messages.RetainageForTransactionDescription, APDocTypeDict[origInvoice.DocType], origInvoice.RefNbr); } prevCuryTotal = (retainageOpts.CuryRetainageAmt ?? 0m) - (invoice.CuryDocBal ?? 0m); tranNew.CuryLineAmt = PXCurrencyAttribute.RoundCury(Base.Transactions.Cache, tranNew, (detail.CuryRetainageAmt ?? 0m) * retainagePercent); tranNew = Base.Transactions.Update(tranNew); } ClearCurrentDocumentDiscountDetails(); // We should copy all taxes from the original document // because it is possible to add or delete them. // foreach (APTaxTran aptaxtran in PXSelect <APTaxTran, Where <APTaxTran.module, Equal <BatchModule.moduleAP>, And <APTaxTran.tranType, Equal <Required <APTaxTran.tranType> >, And <APTaxTran.refNbr, Equal <Required <APTaxTran.refNbr> > > > > > .Select(Base, origInvoice.DocType, origInvoice.RefNbr) .RowCast <APTaxTran>() .Where(row => row.CuryRetainedTaxAmt != 0m)) { APTaxTran new_aptaxtran = Base.Taxes.Insert(new APTaxTran { TaxID = aptaxtran.TaxID }); if (new_aptaxtran != null) { new_aptaxtran = PXCache <APTaxTran> .CreateCopy(new_aptaxtran); new_aptaxtran.TaxRate = aptaxtran.TaxRate; new_aptaxtran = Base.Taxes.Update(new_aptaxtran); } } TaxBaseAttribute.SetTaxCalc <APTran.taxCategoryID>(Base.Transactions.Cache, null, oldTaxCalc); decimal diff = (retainageOpts.CuryRetainageAmt ?? 0m) - (invoice.CuryDocBal ?? 0m); if (tranNew != null && diff != 0m) { HashSet <string> taxList = PXSelectJoin <APTax, InnerJoin <Tax, On <Tax.taxID, Equal <APTax.taxID> > >, Where <APTax.tranType, Equal <Required <APTax.tranType> >, And <APTax.refNbr, Equal <Required <APTax.refNbr> >, And <APTax.lineNbr, Equal <Required <APTax.lineNbr> >, And <Tax.taxType, NotEqual <CSTaxType.use> > > > > > .Select(Base, tranNew.TranType, tranNew.RefNbr, tranNew.LineNbr) .RowCast <APTax>() .Select(row => row.TaxID) .ToHashSet(); // To guarantee correct document total amount // we should calculate last line total, // including its taxes. // TaxAttribute.CalcTaxable calcClass = new TaxAttribute.CalcTaxable(false, TaxAttribute.TaxCalcLevelEnforcing.None); decimal curyLineAmt = calcClass.CalcTaxableFromTotalAmount( Base.Transactions.Cache, tranNew, taxList, invoice.DocDate.Value, prevCuryTotal); tranNew.CuryLineAmt = curyLineAmt; tranNew = Base.Transactions.Update(tranNew); } APVendorRefNbrAttribute aPVendorRefNbrAttribute = Base.Document.Cache.GetAttributesReadonly <APInvoice.invoiceNbr>() .OfType <APVendorRefNbrAttribute>().FirstOrDefault(); if (aPVendorRefNbrAttribute != null) { var args = new PXFieldVerifyingEventArgs(invoice, invoice.InvoiceNbr, true); aPVendorRefNbrAttribute.FieldVerifying(Base.Document.Cache, args); } return(invoice); }
public virtual void GenerateProc(Schedule s, short Times, DateTime runDate) { List <ScheduleDet> sd = GL.ScheduleProcess.MakeSchedule(this, s, Times, runDate); APInvoiceEntry docgraph = CreateGraph(); foreach (ScheduleDet sdet in sd) { foreach (PXResult <APInvoice, Vendor, CurrencyInfo> res in PXSelectJoin <APInvoice, InnerJoin <Vendor, On <Vendor.bAccountID, Equal <APInvoice.vendorID> >, InnerJoin <CurrencyInfo, On <CurrencyInfo.curyInfoID, Equal <APInvoice.curyInfoID> > > >, Where <APInvoice.scheduleID, Equal <Required <APInvoice.scheduleID> >, And <APInvoice.scheduled, Equal <boolTrue> > > > .Select(this, s.ScheduleID)) { docgraph.Clear(); docgraph.vendor.Current = (Vendor)res; APInvoice apdoc = (APInvoice)res; CurrencyInfo info = (CurrencyInfo)res; CurrencyInfo new_info = PXCache <CurrencyInfo> .CreateCopy(info); new_info.CuryInfoID = null; new_info = docgraph.currencyinfo.Insert(new_info); APInvoice new_apdoc = PXCache <APInvoice> .CreateCopy(apdoc); new_apdoc.CuryInfoID = new_info.CuryInfoID; new_apdoc.DocDate = sdet.ScheduledDate; new_apdoc.FinPeriodID = sdet.ScheduledPeriod; new_apdoc.TranPeriodID = null; new_apdoc.DueDate = null; new_apdoc.DiscDate = null; new_apdoc.PayDate = null; new_apdoc.CuryOrigDiscAmt = null; new_apdoc.OrigDiscAmt = null; new_apdoc.RefNbr = null; new_apdoc.Scheduled = false; new_apdoc.CuryLineTotal = 0m; new_apdoc.CuryVatTaxableTotal = 0m; new_apdoc.CuryVatExemptTotal = 0m; new_apdoc.NoteID = null; new_apdoc = docgraph.Document.Insert(new_apdoc); TaxAttribute.SetTaxCalc <APTran.taxCategoryID, APTaxAttribute>(docgraph.Transactions.Cache, null, TaxCalc.ManualCalc); PXNoteAttribute.SetNote(docgraph.Document.Cache, new_apdoc, PXNoteAttribute.GetNote(Caches[typeof(APInvoice)], apdoc)); PXNoteAttribute.SetFileNotes(docgraph.Document.Cache, new_apdoc, PXNoteAttribute.GetFileNotes(Caches[typeof(APInvoice)], apdoc)); foreach (APTran aptran in PXSelect <APTran, Where <APTran.tranType, Equal <Required <APTran.tranType> >, And <APTran.refNbr, Equal <Required <APTran.refNbr> > > > > .Select(docgraph, apdoc.DocType, apdoc.RefNbr)) { APTran new_aptran = PXCache <APTran> .CreateCopy(aptran); new_aptran.RefNbr = null; new_aptran.CuryInfoID = null; docgraph.Transactions.Insert(new_aptran); } foreach (APTaxTran tax in PXSelect <APTaxTran, Where <APTaxTran.tranType, Equal <Required <APTaxTran.tranType> >, And <APTaxTran.refNbr, Equal <Required <APTaxTran.refNbr> > > > > .Select(docgraph, apdoc.DocType, apdoc.RefNbr)) { APTaxTran new_aptax = new APTaxTran(); new_aptax.TaxID = tax.TaxID; new_aptax = docgraph.Taxes.Insert(new_aptax); if (new_aptax != null) { new_aptax = PXCache <APTaxTran> .CreateCopy(new_aptax); new_aptax.TaxRate = tax.TaxRate; new_aptax.CuryTaxableAmt = tax.CuryTaxableAmt; new_aptax.CuryTaxAmt = tax.CuryTaxAmt; new_aptax = docgraph.Taxes.Update(new_aptax); } } docgraph.Save.Press(); } s.LastRunDate = sdet.ScheduledDate; Running_Schedule.Cache.Update(s); } using (PXTransactionScope ts = new PXTransactionScope()) { Running_Schedule.Cache.Persist(PXDBOperation.Update); ts.Complete(this); } Running_Schedule.Cache.Persisted(false); }