public static void CreateAdjustment(ARInvoiceEntry graph, ARInvoice invoice, RUTROT rutrot) { ARPayment creditMemo = PXSelect <ARPayment, Where <ARPayment.docType, Equal <Required <RUTROT.docType> >, And <ARPayment.refNbr, Equal <Required <RUTROT.refNbr> > > > > .Select(graph, rutrot.BalancingCreditMemoDocType, rutrot.BalancingCreditMemoRefNbr); ARAdjust2 applicationToCreditMemo = new ARAdjust2 { AdjdDocType = invoice.DocType, AdjdRefNbr = invoice.RefNbr, AdjgDocType = creditMemo.DocType, AdjgRefNbr = creditMemo.RefNbr, AdjNbr = creditMemo.AdjCntr, CustomerID = creditMemo.CustomerID, AdjdCustomerID = invoice.CustomerID, AdjdBranchID = invoice.BranchID, AdjgBranchID = creditMemo.BranchID, AdjgCuryInfoID = creditMemo.CuryInfoID, AdjdOrigCuryInfoID = invoice.CuryInfoID, AdjdCuryInfoID = invoice.CuryInfoID, CuryAdjdAmt = creditMemo.CuryDocBal }; graph.Adjustments.Insert(applicationToCreditMemo); graph.Save.Press(); }
private void ValidateAdjustment(ARAdjust2 adjustment) { var documentHasExpiredCompliance = service.ValidateRelatedField <ARAdjust2, ComplianceDocument.arPaymentID, ARAdjust2.adjgRefNbr>(adjustment, ComplianceDocumentReferenceRetriever.GetComplianceDocumentReferenceId(Base, adjustment)); service.ValidateRelatedRow <ARAdjust2, ArAdjust2Ext.hasExpiredComplianceDocuments>(adjustment, documentHasExpiredCompliance); }
public virtual void ApplyTax(ARInvoice 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 PXException(TX.Messages.TaxPayableAccountNotSpecified, vendor.AcctCD); } if (vendor.SalesTaxSubID == null) { throw new PXException(TX.Messages.TaxPayableSubNotSpecified, vendor.AcctCD); } } Sign sign = invoice.DocType == ARDocType.CreditMemo ? Sign.Minus : Sign.Plus; //Clear all existing Tax transactions: foreach (PXResult <ARTaxTran, Tax> res in Base.Taxes.View.SelectMultiBound(new object[] { invoice })) { ARTaxTran taxTran = 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; } //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 { TaxID = taxID, Descr = PXMessages.LocalizeFormatNoPrefixNLA(TX.Messages.ExternalTaxProviderTaxId, taxID), TaxType = CSTaxType.Sales, TaxCalcType = CSTaxCalcType.Doc, TaxCalcLevel = result.TaxSummary[i].TaxCalculationLevel.ToCSTaxCalcLevel(), TaxApplyTermsDisc = CSTaxTermsDiscount.ToTaxableAmount, SalesTaxAcctID = vendor.SalesTaxAcctID, SalesTaxSubID = vendor.SalesTaxSubID, ExpenseAccountID = vendor.TaxExpenseAcctID, ExpenseSubID = vendor.TaxExpenseSubID, TaxVendorID = taxZone.TaxVendorID, IsExternal = true }; Base.Caches[typeof(Tax)].Insert(tx); } var tax = new ARTaxTran { Module = BatchModule.AR, TranType = invoice.DocType, RefNbr = invoice.RefNbr, TaxID = taxID, CuryTaxAmt = sign * result.TaxSummary[i].TaxAmount, CuryTaxableAmt = sign * result.TaxSummary[i].TaxableAmount, TaxRate = Convert.ToDecimal(result.TaxSummary[i].Rate) * 100, JurisType = result.TaxSummary[i].JurisType, JurisName = result.TaxSummary[i].JurisName, TaxType = CSTaxType.Sales, TaxBucketID = 0, AccountID = vendor.SalesTaxAcctID, SubID = vendor.SalesTaxSubID }; Base.Taxes.Insert(tax); } bool requireControlTotal = Base.ARSetup.Current.RequireControlTotal == true; if (invoice.Hold != true) { Base.ARSetup.Current.RequireControlTotal = false; } try { invoice.CuryTaxTotal = sign * result.TotalTaxAmount; Base.Document.Cache.SetValueExt <ARInvoice.isTaxSaved>(invoice, true); } finally { Base.ARSetup.Current.RequireControlTotal = requireControlTotal; } if (invoice.ApplyPaymentWhenTaxAvailable == true) { PXSelectBase <ARAdjust2> select = new PXSelectJoin <ARAdjust2, InnerJoin <ARPayment, On <ARAdjust2.adjgDocType, Equal <ARPayment.docType>, And <ARAdjust2.adjgRefNbr, Equal <ARPayment.refNbr> > > >, Where <ARAdjust2.adjdDocType, Equal <Required <ARInvoice.docType> >, And <ARAdjust2.adjdRefNbr, Equal <Required <ARInvoice.refNbr> > > > >(Base); decimal amountApplied = 0m; foreach (PXResult <ARAdjust2, ARPayment> res in select.Select(invoice.DocType, invoice.RefNbr)) { ARAdjust2 row = (ARAdjust2)res; ARPayment payment = (ARPayment)res; ARAdjust2 copy = PXCache <ARAdjust2> .CreateCopy(row); amountApplied += (copy.CuryAdjdAmt ?? 0m); if (amountApplied > (invoice.CuryDocBal ?? 0m)) { decimal newAdjdAmt = (copy.CuryAdjdAmt ?? 0m) - (amountApplied - (invoice.CuryDocBal ?? 0m)); copy.CuryAdjdAmt = newAdjdAmt > 0m ? newAdjdAmt : 0m; } Base.Adjustments.Update(copy); } } }