private static void CreateMemo(ARInvoiceEntry graph, ARRegister doc, RUTROT rutrot, string docType, bool OnRelease = false) { DuplicateFilter filter = PXCache <DuplicateFilter> .CreateCopy(graph.duplicatefilter.Current); foreach (PXResult <ARInvoice, CurrencyInfo, Terms, Customer> res in ARInvoice_CurrencyInfo_Terms_Customer.Select(graph, (object)doc.DocType, doc.RefNbr, doc.CustomerID)) { CurrencyInfo info = PXCache <CurrencyInfo> .CreateCopy((CurrencyInfo)res); info.CuryInfoID = null; info.IsReadOnly = false; info = PXCache <CurrencyInfo> .CreateCopy(graph.currencyinfo.Insert(info)); ARInvoice invoice = (ARInvoice)graph.Document.Cache.CreateInstance(); if (docType == ARDocType.CreditMemo) { invoice.DueDate = null; invoice.DiscDate = null; invoice.CustomerID = doc.CustomerID; invoice.ARAccountID = doc.ARAccountID; invoice.ARSubID = doc.ARSubID; } if (docType == ARInvoiceType.DebitMemo) { invoice.DueDate = ((ARInvoice)res).DueDate; invoice.DiscDate = ((ARInvoice)res).DiscDate; BranchRUTROT branchRUTROT = GetBranchRUTROT(graph); invoice.CustomerID = branchRUTROT.TaxAgencyAccountID; invoice.ARAccountID = null; invoice.ARSubID = null; } ARInvoiceRUTROT invoiceRUTROT = RUTROTHelper.GetExtensionNullable <ARInvoice, ARInvoiceRUTROT>(invoice); invoiceRUTROT.IsRUTROTDeductible = false; invoice.CuryInfoID = info.CuryInfoID; invoice.DocType = docType; invoice.OrigModule = GL.BatchModule.AR; invoice.RefNbr = null; invoice.OrigModule = GL.BatchModule.AR; invoice.DocDesc = PXLocalizer.LocalizeFormat(RUTROTMessages.MemoDescription, doc.RefNbr); invoice.OpenDoc = true; invoice.Released = false; invoice.Hold = false; invoice.Printed = false; invoice.Emailed = false; invoice.BatchNbr = null; invoice.ScheduleID = null; invoice.Scheduled = false; invoice.NoteID = null; invoice.RefNoteID = null; invoice.TermsID = null; invoice.InstallmentCntr = null; invoice.InstallmentNbr = null; invoice.CuryOrigDiscAmt = 0m; invoice.FinPeriodID = doc.FinPeriodID; invoice.OrigDocDate = invoice.DocDate; invoice.CuryLineTotal = 0m; invoice.IsTaxPosted = false; invoice.IsTaxValid = false; invoice.CuryVatTaxableTotal = 0m; invoice.CuryVatExemptTotal = 0m; invoice.StatementDate = null; invoice.PendingPPD = false; invoice.CustomerLocationID = null; if (!string.IsNullOrEmpty(invoice.PaymentMethodID)) { CA.PaymentMethod pm = null; if (invoice.CashAccountID.HasValue) { CA.PaymentMethodAccount pmAccount = null; PXResult <CA.PaymentMethod, CA.PaymentMethodAccount> pmResult = (PXResult <CA.PaymentMethod, CA.PaymentMethodAccount>) PXSelectJoin <CA.PaymentMethod, LeftJoin < CA.PaymentMethodAccount, On <CA.PaymentMethod.paymentMethodID, Equal <CA.PaymentMethodAccount.paymentMethodID> > >, Where < CA.PaymentMethod.paymentMethodID, Equal <Required <CA.PaymentMethod.paymentMethodID> >, And <CA.PaymentMethodAccount.cashAccountID, Equal <Required <CA.PaymentMethodAccount.cashAccountID> > > > > . Select(graph, invoice.PaymentMethodID, invoice.CashAccountID); pm = pmResult; pmAccount = pmResult; if (pm == null || pm.UseForAR == false || pm.IsActive == false) { invoice.PaymentMethodID = null; invoice.CashAccountID = null; } else if (pmAccount == null || pmAccount.CashAccountID == null || pmAccount.UseForAR != true) { invoice.CashAccountID = null; } } else { pm = PXSelect <CA.PaymentMethod, Where <CA.PaymentMethod.paymentMethodID, Equal <Required <CA.PaymentMethod.paymentMethodID> > > > .Select(graph, invoice.PaymentMethodID); if (pm == null || pm.UseForAR == false || pm.IsActive == false) { invoice.PaymentMethodID = null; invoice.CashAccountID = null; invoice.PMInstanceID = null; } } if (invoice.PMInstanceID.HasValue) { CustomerPaymentMethod cpm = PXSelect <CustomerPaymentMethod, Where <CustomerPaymentMethod.pMInstanceID, Equal <Required <CustomerPaymentMethod.pMInstanceID> > > > . Select(graph, invoice.PMInstanceID); if (string.IsNullOrEmpty(invoice.PaymentMethodID) || cpm == null || cpm.IsActive == false || cpm.PaymentMethodID != invoice.PaymentMethodID) { invoice.PMInstanceID = null; } } } else { invoice.CashAccountID = null; invoice.PMInstanceID = null; } SalesPerson sp = (SalesPerson)PXSelectorAttribute.Select <ARInvoice.salesPersonID>(graph.Document.Cache, invoice); if (sp == null || sp.IsActive == false) { invoice.SalesPersonID = null; } invoice = graph.Document.Insert(invoice); } TX.TaxAttribute.SetTaxCalc <ARTran.taxCategoryID>(graph.Transactions.Cache, null, TX.TaxCalc.ManualCalc); graph.FieldDefaulting.AddHandler <ARTran.salesPersonID>((sender, e) => { e.NewValue = null; e.Cancel = true; }); decimal roundedTotalDistributedLinesAmt = 0m; foreach (ARTran srcTran in PXSelect <ARTran, Where <ARTran.tranType, Equal <Required <ARTran.tranType> >, And <ARTran.refNbr, Equal <Required <ARTran.refNbr> > > > > .Select(graph, doc.DocType, doc.RefNbr)) { ARTran tran = PXCache <ARTran> .CreateCopy(srcTran); ARTranRUTROT tranRR = RUTROTHelper.GetExtensionNullable <ARTran, ARTranRUTROT>(tran); if (tranRR.IsRUTROTDeductible != true) { continue; } tran.TranType = graph.Document.Current.DocType; tran.RefNbr = graph.Document.Current.RefNbr; string origDrCr = tran.DrCr; tran.DrCr = null; tran.Released = null; tran.CuryInfoID = null; tran.SOOrderNbr = null; tran.SOShipmentNbr = null; tran.OrigInvoiceDate = tran.TranDate; tran.NoteID = null; tran.ManualPrice = true; tran.CuryTranAmt = Math.Floor(tranRR.CuryRUTROTAvailableAmt ?? 0m); roundedTotalDistributedLinesAmt += tran.CuryTranAmt ?? 0m; tranRR.IsRUTROTDeductible = false; if (!string.IsNullOrEmpty(tran.DeferredCode)) { DRSchedule schedule = PXSelect <DRSchedule, Where <DRSchedule.module, Equal <BQLConstants.moduleAR>, And <DRSchedule.docType, Equal <Required <DRSchedule.docType> >, And <DRSchedule.refNbr, Equal <Required <DRSchedule.refNbr> >, And <DRSchedule.lineNbr, Equal <Required <DRSchedule.lineNbr> > > > > > > . Select(graph, doc.DocType, doc.RefNbr, tran.LineNbr); if (schedule != null) { tran.DefScheduleID = schedule.ScheduleID; } } SalesPerson sp = (SalesPerson)PXSelectorAttribute.Select <ARTran.salesPersonID>(graph.Transactions.Cache, tran); if (sp == null || sp.IsActive == false) { tran.SalesPersonID = null; } ARTran insertedTran = graph.Transactions.Insert(tran); PXNoteAttribute.CopyNoteAndFiles(graph.Transactions.Cache, srcTran, graph.Transactions.Cache, insertedTran); insertedTran.ManualDisc = true; insertedTran.TaxCategoryID = null; graph.Transactions.Update(insertedTran); } decimal distributedFee = (rutrot.CuryDistributedAmt ?? 0m) - roundedTotalDistributedLinesAmt; if (distributedFee != 0m) { foreach (ARTran artran in graph.Transactions.Cache.Inserted) { if (Math.Round(distributedFee) == 0m) { break; } if (artran.CuryTranAmt != 0m) { artran.CuryTranAmt += Math.Sign(distributedFee); distributedFee -= Math.Sign(distributedFee); } graph.Transactions.Update(artran); } } graph.Document.Current.CuryOrigDocAmt = graph.Document.Current.CuryDocBal; graph.Document.Cache.Update(graph.Document.Current); graph.RowInserting.AddHandler <ARSalesPerTran>((sender, e) => { e.Cancel = true; }); foreach (ARSalesPerTran salespertran in PXSelect <ARSalesPerTran, Where <ARSalesPerTran.docType, Equal <Required <ARSalesPerTran.docType> >, And <ARSalesPerTran.refNbr, Equal <Required <ARSalesPerTran.refNbr> > > > > .Select(graph, doc.DocType, doc.RefNbr)) { ARSalesPerTran newtran = PXCache <ARSalesPerTran> .CreateCopy(salespertran); newtran.DocType = graph.Document.Current.DocType; newtran.RefNbr = graph.Document.Current.RefNbr; newtran.Released = false; newtran.CuryInfoID = null; newtran.CuryCommnblAmt *= -1m; newtran.CuryCommnAmt *= -1m; SalesPerson sp = (SalesPerson)PXSelectorAttribute.Select <ARSalesPerTran.salespersonID>(graph.salesPerTrans.Cache, newtran); if (!(sp == null || sp.IsActive == false)) { graph.salesPerTrans.Update(newtran); } } var discountDetailsSet = PXSelect <ARInvoiceDiscountDetail, Where <ARInvoiceDiscountDetail.docType, Equal <Required <ARInvoice.docType> >, And <ARInvoiceDiscountDetail.refNbr, Equal <Required <ARInvoice.refNbr> > > >, OrderBy <Asc <ARInvoiceDiscountDetail.docType, Asc <ARInvoiceDiscountDetail.refNbr> > > > .Select(graph, doc.DocType, doc.RefNbr); foreach (ARInvoiceDiscountDetail discountDetail in discountDetailsSet) { ARInvoiceDiscountDetail newDiscountDetail = PXCache <ARInvoiceDiscountDetail> .CreateCopy(discountDetail); newDiscountDetail.DocType = graph.Document.Current.DocType; newDiscountDetail.RefNbr = graph.Document.Current.RefNbr; newDiscountDetail.IsManual = true; DiscountEngineProvider.GetEngineFor <ARTran, ARInvoiceDiscountDetail>().UpdateDiscountDetail(graph.ARDiscountDetails.Cache, graph.ARDiscountDetails, newDiscountDetail); } graph.Save.Press(); if (docType == ARDocType.CreditMemo && !OnRelease) { CreateAdjustment(graph, doc, graph.Document.Current); } }
public virtual ARInvoice ReleaseRetainageProc(ARInvoice doc, RetainageOptions retainageOpts, bool isAutoRelease = false) { Base.Clear(PXClearOption.PreserveTimeStamp); if (retainageOpts.CuryRetainageAmt <= 0 || retainageOpts.CuryRetainageAmt > doc.CuryRetainageUnreleasedAmt) { throw new PXException(AP.Messages.IncorrectRetainageAmount); } // Magic. We need to prevent rewriting of CurrencyInfo.IsReadOnly // by true in CurrencyInfoView // Base.CurrentDocument.Cache.AllowUpdate = true; PXResult <ARInvoice, CurrencyInfo, Terms, Customer> resultDoc = ARInvoice_CurrencyInfo_Terms_Customer .SelectSingleBound(Base, null, doc.DocType, doc.RefNbr, doc.CustomerID) .Cast <PXResult <ARInvoice, CurrencyInfo, Terms, Customer> >() .First(); CurrencyInfo info = resultDoc; ARInvoice origInvoice = resultDoc; Customer customer = 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)); ARInvoice invoice = PXCache <ARInvoice> .CreateCopy(origInvoice); invoice.CuryInfoID = new_info.CuryInfoID; invoice.DocType = ARDocType.Invoice; invoice.RefNbr = null; invoice.LineCntr = null; invoice.InvoiceNbr = origInvoice.InvoiceNbr; // Must be set for _RowSelected event handler // invoice.OpenDoc = true; invoice.Released = false; Base.Document.Cache.SetDefaultExt <ARInvoice.isMigratedRecord>(invoice); Base.Document.Cache.SetDefaultExt <ARInvoice.hold>(invoice); invoice.BatchNbr = null; 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.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.ARSetup.Current.HoldEntry == true; 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 <ARInvoice.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 <ARTran> details = PXSelectGroupBy <ARTran, Where <ARTran.tranType, Equal <Required <ARTran.tranType> >, And <ARTran.refNbr, Equal <Required <ARTran.refNbr> >, And <ARTran.curyRetainageAmt, NotEqual <decimal0> > > >, Aggregate < GroupBy <ARTran.taxCategoryID, Sum <ARTran.curyRetainageAmt> > > > .Select(Base, doc.DocType, doc.RefNbr); ARTran tranNew = null; decimal prevCuryTotal = 0m; TaxCalc oldTaxCalc = TaxBaseAttribute.GetTaxCalc <ARTran.taxCategoryID>(Base.Transactions.Cache, null); TaxBaseAttribute.SetTaxCalc <ARTran.taxCategoryID>(Base.Transactions.Cache, null, TaxCalc.ManualCalc); foreach (ARTran detail in details) { // Create ARTran record for chosen retainage amount, // clear all required fields to prevent tax calculation, // discount calculation and retainage calculation. // CuryUnitPrice = 0m and CuryExtPrice = 0m here to prevent their // FieldDefaulting events, because in our case default value // should be equal to zero. // tranNew = Base.Transactions.Insert(new ARTran { CuryUnitPrice = 0m, CuryExtPrice = 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.ManualDisc = true; tranNew.DiscPct = 0m; tranNew.CuryDiscAmt = 0m; tranNew.RetainagePct = 0m; tranNew.CuryRetainageAmt = 0m; tranNew.CuryTaxableAmt = 0m; tranNew.CuryTaxAmt = 0; tranNew.GroupDiscountRate = 1m; tranNew.DocumentDiscountRate = 1m; using (new PXLocaleScope(customer.LocaleName)) { tranNew.TranDesc = PXMessages.LocalizeFormatNoPrefix( AP.Messages.RetainageForTransactionDescription, ARDocTypeDict[origInvoice.DocType], origInvoice.RefNbr); } prevCuryTotal = (retainageOpts.CuryRetainageAmt ?? 0m) - (invoice.CuryDocBal ?? 0m); tranNew.CuryExtPrice = 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 (ARTaxTran artaxtran in PXSelect <ARTaxTran, Where <ARTaxTran.module, Equal <BatchModule.moduleAR>, And <ARTaxTran.tranType, Equal <Required <ARTaxTran.tranType> >, And <ARTaxTran.refNbr, Equal <Required <ARTaxTran.refNbr> > > > > > .Select(Base, origInvoice.DocType, origInvoice.RefNbr) .RowCast <ARTaxTran>() .Where(row => row.CuryRetainedTaxAmt != 0m)) { ARTaxTran new_artaxtran = Base.Taxes.Insert(new ARTaxTran { TaxID = artaxtran.TaxID }); if (new_artaxtran != null) { new_artaxtran = PXCache <ARTaxTran> .CreateCopy(new_artaxtran); new_artaxtran.TaxRate = artaxtran.TaxRate; new_artaxtran = Base.Taxes.Update(new_artaxtran); } } TaxBaseAttribute.SetTaxCalc <ARTran.taxCategoryID>(Base.Transactions.Cache, null, oldTaxCalc); decimal diff = (retainageOpts.CuryRetainageAmt ?? 0m) - (invoice.CuryDocBal ?? 0m); if (tranNew != null && diff != 0m) { HashSet <string> taxList = PXSelectJoin <ARTax, InnerJoin <Tax, On <Tax.taxID, Equal <ARTax.taxID> > >, Where <ARTax.tranType, Equal <Required <ARTax.tranType> >, And <ARTax.refNbr, Equal <Required <ARTax.refNbr> >, And <ARTax.lineNbr, Equal <Required <ARTax.lineNbr> >, And <Tax.taxType, NotEqual <CSTaxType.use> > > > > > .Select(Base, tranNew.TranType, tranNew.RefNbr, tranNew.LineNbr) .RowCast <ARTax>() .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 curyExtPrice = calcClass.CalcTaxableFromTotalAmount( Base.Transactions.Cache, tranNew, taxList, invoice.DocDate.Value, prevCuryTotal); tranNew.CuryExtPrice = curyExtPrice; tranNew = Base.Transactions.Update(tranNew); } return(invoice); }