protected virtual void InsertDetails(ARInvoiceEntry invoiceEntry, ARInvoice scheduledInvoice, ARInvoice newInvoice) { foreach (ARTran originalLine in PXSelect < ARTran, Where < ARTran.tranType, Equal <Required <ARTran.tranType> >, And <ARTran.refNbr, Equal <Required <ARTran.refNbr> >, And <Where < ARTran.lineType, IsNull, Or <ARTran.lineType, NotEqual <SOLineType.discount> > > > > > > .Select(invoiceEntry, scheduledInvoice.DocType, scheduledInvoice.RefNbr)) { ARTran newLine = PXCache <ARTran> .CreateCopy(originalLine); newLine.FinPeriodID = null; newLine.TranPeriodID = null; newLine.RefNbr = null; newLine.CuryInfoID = null; newLine.ManualPrice = true; newLine.ManualDisc = true; newLine.NoteID = null; newLine = invoiceEntry.Transactions.Insert(newLine); PXNoteAttribute.CopyNoteAndFiles(Caches[typeof(ARTran)], originalLine, invoiceEntry.Transactions.Cache, newLine); } foreach (ARInvoiceDiscountDetail originalDiscountDetail in PXSelect < ARInvoiceDiscountDetail, Where < ARInvoiceDiscountDetail.docType, Equal <Required <ARInvoiceDiscountDetail.docType> >, And <ARInvoiceDiscountDetail.refNbr, Equal <Required <ARInvoiceDiscountDetail.refNbr> > > > > .Select(invoiceEntry, scheduledInvoice.DocType, scheduledInvoice.RefNbr)) { ARInvoiceDiscountDetail newDiscountDetail = PXCache <ARInvoiceDiscountDetail> .CreateCopy(originalDiscountDetail); newDiscountDetail.RefNbr = null; newDiscountDetail.CuryInfoID = null; newDiscountDetail.IsManual = true; DiscountEngineProvider.GetEngineFor <ARTran, ARInvoiceDiscountDetail>().InsertDiscountDetail(invoiceEntry.ARDiscountDetails.Cache, invoiceEntry.ARDiscountDetails, newDiscountDetail); } }
public virtual void GenerateProc(Schedule schedule, short times, DateTime runDate) { IEnumerable <ScheduleDet> occurrences = new Scheduler(this).MakeSchedule(schedule, times, runDate); APInvoiceEntry invoiceEntry = CreateGraph(); using (PXTransactionScope transactionScope = new PXTransactionScope()) { foreach (ScheduleDet occurrence in occurrences) { foreach (PXResult <APInvoice, Vendor, CurrencyInfo> scheduledInvoiceResult 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, schedule.ScheduleID)) { invoiceEntry.Clear(); invoiceEntry.vendor.Current = (Vendor)scheduledInvoiceResult; APInvoice scheduledInvoice = (APInvoice)scheduledInvoiceResult; CurrencyInfo scheduledInvoiceCurrencyInfo = (CurrencyInfo)scheduledInvoiceResult; if (scheduledInvoice.Released == true) { throw new PXException(AR.Messages.ScheduledDocumentAlreadyReleased); } // Cloning currency info is required because we want to preserve // (and not default) the currency rate type of the template document. // - CurrencyInfo newCurrencyInfo = PXCache <CurrencyInfo> .CreateCopy(scheduledInvoiceCurrencyInfo); newCurrencyInfo.CuryInfoID = null; newCurrencyInfo.CuryRate = null; newCurrencyInfo.CuryEffDate = occurrence.ScheduledDate; newCurrencyInfo = invoiceEntry.currencyinfo.Insert(newCurrencyInfo); APInvoice newInvoice = PXCache <APInvoice> .CreateCopy(scheduledInvoice); newInvoice.CuryInfoID = newCurrencyInfo.CuryInfoID; newInvoice.DocDate = occurrence.ScheduledDate; newInvoice.FinPeriodID = occurrence.ScheduledPeriod; newInvoice.TranPeriodID = null; newInvoice.DueDate = null; newInvoice.DiscDate = null; newInvoice.PayDate = null; newInvoice.CuryOrigDiscAmt = null; newInvoice.OrigDiscAmt = null; newInvoice.RefNbr = null; newInvoice.Scheduled = false; newInvoice.CuryLineTotal = 0m; newInvoice.CuryVatTaxableTotal = 0m; newInvoice.CuryVatExemptTotal = 0m; newInvoice.NoteID = null; newInvoice.PaySel = false; newInvoice.IsTaxValid = false; newInvoice.IsTaxPosted = false; newInvoice.IsTaxSaved = false; newInvoice.OrigDocType = scheduledInvoice.DocType; newInvoice.OrigRefNbr = scheduledInvoice.RefNbr; newInvoice = invoiceEntry.Document.Insert(newInvoice); //we insert an item here because we need DontApprove field to be set in RowSelected if (newInvoice.DontApprove != true) { // We always generate documents on hold // if approval process is enabled in AP. // - newInvoice.Hold = true; } newInvoice = invoiceEntry.Document.Update(newInvoice); CurrencyInfoAttribute.SetEffectiveDate <APInvoice.docDate>( invoiceEntry.Document.Cache, new PXFieldUpdatedEventArgs(newInvoice, null, false)); PXNoteAttribute.CopyNoteAndFiles(Caches[typeof(APInvoice)], scheduledInvoice, invoiceEntry.Document.Cache, newInvoice); foreach (APTran originalLine in PXSelect < APTran, Where < APTran.tranType, Equal <Required <APTran.tranType> >, And <APTran.refNbr, Equal <Required <APTran.refNbr> >, And <Where < APTran.lineType, IsNull, Or <APTran.lineType, NotEqual <SOLineType.discount> > > > > > > .Select(invoiceEntry, scheduledInvoice.DocType, scheduledInvoice.RefNbr)) { APTran newLine = PXCache <APTran> .CreateCopy(originalLine); newLine.RefNbr = null; newLine.CuryInfoID = null; newLine.ManualPrice = true; newLine.ManualDisc = true; newLine.NoteID = null; newLine = invoiceEntry.Transactions.Insert(newLine); PXNoteAttribute.CopyNoteAndFiles(Caches[typeof(APTran)], originalLine, invoiceEntry.Transactions.Cache, newLine); } foreach (APInvoiceDiscountDetail originalDiscountDetail in PXSelect < APInvoiceDiscountDetail, Where < APInvoiceDiscountDetail.docType, Equal <Required <APInvoiceDiscountDetail.docType> >, And <APInvoiceDiscountDetail.refNbr, Equal <Required <APInvoiceDiscountDetail.refNbr> > > > > .Select(invoiceEntry, scheduledInvoice.DocType, scheduledInvoice.RefNbr)) { APInvoiceDiscountDetail newDiscountDetail = PXCache <APInvoiceDiscountDetail> .CreateCopy(originalDiscountDetail); newDiscountDetail.RefNbr = null; newDiscountDetail.CuryInfoID = null; newDiscountDetail.IsManual = true; DiscountEngineProvider.GetEngineFor <APTran, APInvoiceDiscountDetail>().InsertDiscountDetail(invoiceEntry.DiscountDetails.Cache, invoiceEntry.DiscountDetails, newDiscountDetail); } BalanceCalculation.ForceDocumentControlTotals(invoiceEntry, newInvoice); try { invoiceEntry.Save.Press(); } catch { if (invoiceEntry.Document.Cache.IsInsertedUpdatedDeleted) { throw; } } } schedule.LastRunDate = occurrence.ScheduledDate; Running_Schedule.Cache.Update(schedule); } transactionScope.Complete(this); } using (PXTransactionScope ts = new PXTransactionScope()) { Running_Schedule.Cache.Persist(PXDBOperation.Update); ts.Complete(this); } Running_Schedule.Cache.Persisted(false); }
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); } }