protected virtual ARInvoice InsertDocument( ARInvoiceEntry invoiceEntry, ScheduleDet occurrence, Customer customer, ARInvoice scheduledInvoice, CurrencyInfo scheduledInvoiceCurrencyInfo) { if (scheduledInvoice.Released == true) { throw new PXException(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); ARInvoice newInvoice = PXCache <ARInvoice> .CreateCopy(scheduledInvoice); newInvoice.CuryInfoID = newCurrencyInfo.CuryInfoID; newInvoice.DocDate = occurrence.ScheduledDate; FinPeriod finPeriod = FinPeriodRepository.GetFinPeriodByMasterPeriodID(PXAccess.GetParentOrganizationID(newInvoice.BranchID), occurrence.ScheduledPeriod) .GetValueOrRaiseError(); newInvoice.FinPeriodID = finPeriod.FinPeriodID; newInvoice.TranPeriodID = null; newInvoice.DueDate = null; newInvoice.DiscDate = 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.IsTaxValid = false; newInvoice.IsTaxPosted = false; newInvoice.IsTaxSaved = false; newInvoice.OrigDocType = scheduledInvoice.DocType; newInvoice.OrigRefNbr = scheduledInvoice.RefNbr; invoiceEntry.Document.Cache.SetDefaultExt <ARInvoice.printed>(newInvoice); invoiceEntry.Document.Cache.SetDefaultExt <ARInvoice.emailed>(newInvoice); bool forceClear = false; bool clearPaymentMethod = false; if (newInvoice.PMInstanceID.HasValue) { PXResult <CustomerPaymentMethod, PaymentMethod> paymentMethodResult = (PXResult <CustomerPaymentMethod, PaymentMethod>) PXSelectJoin < CustomerPaymentMethod, InnerJoin <PaymentMethod, On <PaymentMethod.paymentMethodID, Equal <CustomerPaymentMethod.paymentMethodID> > >, Where < CustomerPaymentMethod.pMInstanceID, Equal <Required <CustomerPaymentMethod.pMInstanceID> > > > .Select(invoiceEntry, newInvoice.PMInstanceID); if (paymentMethodResult != null) { CustomerPaymentMethod customerPaymentMethod = paymentMethodResult; PaymentMethod paymentMethod = paymentMethodResult; if (customerPaymentMethod == null || customerPaymentMethod.IsActive != true || paymentMethod.IsActive != true || paymentMethod.UseForAR != true) { clearPaymentMethod = true; forceClear = true; } } else { clearPaymentMethod = true; forceClear = true; } } else { if (!string.IsNullOrEmpty(newInvoice.PaymentMethodID)) { PaymentMethod paymentMethod = PXSelect < PaymentMethod, Where <PaymentMethod.paymentMethodID, Equal <Required <PaymentMethod.paymentMethodID> > > > .Select(invoiceEntry, newInvoice.PaymentMethodID); if (paymentMethod == null || paymentMethod.IsActive != true || paymentMethod.UseForAR != true) { clearPaymentMethod = true; forceClear = true; } } } if (clearPaymentMethod) { newInvoice.PMInstanceID = null; newInvoice.PaymentMethodID = null; newInvoice.CashAccountID = null; } invoiceEntry.ClearRetainageSummary(newInvoice); newInvoice = invoiceEntry.Document.Insert(newInvoice); 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); // Force credit rule back // - invoiceEntry.customer.Current = customer; if (forceClear == true) { ARInvoice copy = PXCache <ARInvoice> .CreateCopy(newInvoice); copy.PMInstanceID = null; copy.PaymentMethodID = null; copy.CashAccountID = null; newInvoice = invoiceEntry.Document.Update(copy); } AddressAttribute.CopyRecord <ARInvoice.billAddressID>(invoiceEntry.Document.Cache, newInvoice, scheduledInvoice, false); ContactAttribute.CopyRecord <ARInvoice.billContactID>(invoiceEntry.Document.Cache, newInvoice, scheduledInvoice, false); CurrencyInfoAttribute.SetEffectiveDate <ARInvoice.docDate>( invoiceEntry.Document.Cache, new PXFieldUpdatedEventArgs(newInvoice, null, false)); PXNoteAttribute.CopyNoteAndFiles(Caches[typeof(ARInvoice)], scheduledInvoice, invoiceEntry.Document.Cache, newInvoice); return(newInvoice); }
private static void GenerateRequisition(RQRequestSelection filter, List <RQRequestLineOwned> lines) { RQRequisitionEntry graph = PXGraph.CreateInstance <RQRequisitionEntry>(); RQRequisition requisition = (RQRequisition)graph.Document.Cache.CreateInstance(); graph.Document.Insert(requisition); requisition.ShipDestType = null; bool isCustomerSet = true; bool isVendorSet = true; bool isShipToSet = true; int? shipContactID = null; int? shipAddressID = null; var vendors = new HashSet <VendorRef>(); foreach (RQRequestLine line in lines) { PXResult <RQRequest, RQRequestClass> e = (PXResult <RQRequest, RQRequestClass>) PXSelectJoin <RQRequest, InnerJoin <RQRequestClass, On <RQRequestClass.reqClassID, Equal <RQRequest.reqClassID> > >, Where <RQRequest.orderNbr, Equal <Required <RQRequest.orderNbr> > > > .Select(graph, line.OrderNbr); RQRequest req = e; RQRequestClass reqclass = e; requisition = PXCache <RQRequisition> .CreateCopy(graph.Document.Current); if (reqclass.CustomerRequest == true && isCustomerSet) { if (requisition.CustomerID == null) { requisition.CustomerID = req.EmployeeID; requisition.CustomerLocationID = req.LocationID; } else if (requisition.CustomerID != req.EmployeeID || requisition.CustomerLocationID != req.LocationID) { isCustomerSet = false; } } else { isCustomerSet = false; } if (isShipToSet) { if (shipContactID == null && shipAddressID == null) { requisition.ShipDestType = req.ShipDestType; requisition.ShipToBAccountID = req.ShipToBAccountID; requisition.ShipToLocationID = req.ShipToLocationID; shipContactID = req.ShipContactID; shipAddressID = req.ShipAddressID; } else if (requisition.ShipDestType != req.ShipDestType || requisition.ShipToBAccountID != req.ShipToBAccountID || requisition.ShipToLocationID != req.ShipToLocationID) { isShipToSet = false; } } if (line.VendorID != null && line.VendorLocationID != null) { VendorRef vendor = new VendorRef() { VendorID = line.VendorID.Value, LocationID = line.VendorLocationID.Value }; vendors.Add(vendor); if (isVendorSet) { if (requisition.VendorID == null) { requisition.VendorID = line.VendorID; requisition.VendorLocationID = line.VendorLocationID; } else if (requisition.VendorID != line.VendorID || requisition.VendorLocationID != line.VendorLocationID) { isVendorSet = false; } } } else { isVendorSet = false; } if (!isCustomerSet) { requisition.CustomerID = null; requisition.CustomerLocationID = null; } if (!isVendorSet) { requisition.VendorID = null; requisition.VendorLocationID = null; requisition.RemitAddressID = null; requisition.RemitContactID = null; } else if (requisition.VendorID == req.VendorID && requisition.VendorLocationID == req.VendorLocationID) { requisition.RemitAddressID = req.RemitAddressID; requisition.RemitContactID = req.RemitContactID; } if (!isShipToSet) { requisition.ShipDestType = PX.Objects.PO.POShippingDestination.CompanyLocation; graph.Document.Cache.SetDefaultExt <RQRequisition.shipToBAccountID>(requisition); } graph.Document.Update(requisition); if (line.OpenQty > 0) { if (!graph.Lines.Cache.IsDirty && req.CuryID != requisition.CuryID) { requisition = PXCache <RQRequisition> .CreateCopy(graph.Document.Current); requisition.CuryID = req.CuryID; graph.Document.Update(requisition); } graph.InsertRequestLine(line, line.OpenQty.GetValueOrDefault(), filter.AddExists == true); } } if (isShipToSet) { foreach (PXResult <POAddress, POContact> res in PXSelectJoin <POAddress, CrossJoin <POContact>, Where <POAddress.addressID, Equal <Required <RQRequisition.shipAddressID> >, And <POContact.contactID, Equal <Required <RQRequisition.shipContactID> > > > > .Select(graph, shipAddressID, shipContactID)) { AddressAttribute.CopyRecord <RQRequisition.shipAddressID>(graph.Document.Cache, graph.Document.Current, (POAddress)res, true); AddressAttribute.CopyRecord <RQRequisition.shipContactID>(graph.Document.Cache, graph.Document.Current, (POContact)res, true); } } if (requisition.VendorID == null && vendors.Count > 0) { foreach (var vendor in vendors) { RQBiddingVendor bid = PXCache <RQBiddingVendor> .CreateCopy(graph.Vendors.Insert()); bid.VendorID = vendor.VendorID; bid.VendorLocationID = vendor.LocationID; graph.Vendors.Update(bid); } } if (graph.Lines.Cache.IsDirty) { graph.Save.Press(); throw new PXRedirectRequiredException(graph, string.Format(Messages.RequisitionCreated, graph.Document.Current.ReqNbr)); } for (int i = 0; i < lines.Count; i++) { PXProcessing <RQRequestLine> .SetInfo(i, PXMessages.LocalizeFormatNoPrefixNLA(Messages.RequisitionCreated, graph.Document.Current.ReqNbr)); } }
public virtual void GenerateProc(Schedule s, short Times, DateTime runDate) { List <ScheduleDet> sd = GL.ScheduleProcess.MakeSchedule(this, s, Times, runDate); ARInvoiceEntry docgraph = CreateGraph(); foreach (ScheduleDet sdet in sd) { foreach (PXResult <ARInvoice, Customer, CurrencyInfo> res in PXSelectJoin <ARInvoice, InnerJoin <Customer, On <Customer.bAccountID, Equal <ARInvoice.customerID> >, InnerJoin <CurrencyInfo, On <CurrencyInfo.curyInfoID, Equal <ARInvoice.curyInfoID> > > >, Where <ARInvoice.scheduleID, Equal <Required <ARInvoice.scheduleID> >, And <ARInvoice.scheduled, Equal <boolTrue> > > > .Select(this, s.ScheduleID)) { docgraph.Clear(); docgraph.customer.Current = (Customer)res; ARInvoice apdoc = (ARInvoice)res; CurrencyInfo info = (CurrencyInfo)res; CurrencyInfo new_info = PXCache <CurrencyInfo> .CreateCopy(info); new_info.CuryInfoID = null; new_info = docgraph.currencyinfo.Insert(new_info); ARInvoice new_ardoc = PXCache <ARInvoice> .CreateCopy(apdoc); new_ardoc.CuryInfoID = new_info.CuryInfoID; new_ardoc.DocDate = sdet.ScheduledDate; new_ardoc.FinPeriodID = sdet.ScheduledPeriod; new_ardoc.TranPeriodID = null; new_ardoc.DueDate = null; new_ardoc.DiscDate = null; new_ardoc.CuryOrigDiscAmt = null; new_ardoc.OrigDiscAmt = null; new_ardoc.RefNbr = null; new_ardoc.Scheduled = false; new_ardoc.CuryLineTotal = 0m; new_ardoc.CuryVatTaxableTotal = 0m; new_ardoc.CuryVatExemptTotal = 0m; new_ardoc.NoteID = null; bool forceClear = false; bool clearPM = false; if (new_ardoc.PMInstanceID.HasValue) { PXResult <CustomerPaymentMethod, CA.PaymentMethod> pmiResult = (PXResult <CustomerPaymentMethod, CA.PaymentMethod>) PXSelectJoin <CustomerPaymentMethod, InnerJoin <CA.PaymentMethod, On <CA.PaymentMethod.paymentMethodID, Equal <CustomerPaymentMethod.paymentMethodID> > >, Where <CustomerPaymentMethod.pMInstanceID, Equal <Required <CustomerPaymentMethod.pMInstanceID> > > > .Select(docgraph, new_ardoc.PMInstanceID); if (pmiResult != null) { CustomerPaymentMethod pmInstance = pmiResult; CA.PaymentMethod paymentMethod = pmiResult; if (pmInstance == null || pmInstance.IsActive != true || paymentMethod.IsActive != true || paymentMethod.UseForAR != true) { clearPM = true; forceClear = true; } } else { clearPM = true; forceClear = true; } } else { if (string.IsNullOrEmpty(new_ardoc.PaymentMethodID) == false) { CA.PaymentMethod pm = PXSelect <CA.PaymentMethod, Where <CA.PaymentMethod.paymentMethodID, Equal <Required <CA.PaymentMethod.paymentMethodID> > > > .Select(docgraph, new_ardoc.PaymentMethodID); if (pm == null || pm.IsActive != true || pm.UseForAR != true) { clearPM = true; forceClear = true; } } } if (clearPM) { new_ardoc.PMInstanceID = null; new_ardoc.PaymentMethodID = null; new_ardoc.CashAccountID = null; } new_ardoc = docgraph.Document.Insert(new_ardoc); //force creditrule back docgraph.customer.Current = (Customer)res; if (forceClear == true) { ARInvoice copy = PXCache <ARInvoice> .CreateCopy(new_ardoc); copy.PMInstanceID = null; copy.PaymentMethodID = null; copy.CashAccountID = null; new_ardoc = docgraph.Document.Update(copy); } AddressAttribute.CopyRecord <ARInvoice.billAddressID>(docgraph.Document.Cache, new_ardoc, apdoc, false); ContactAttribute.CopyRecord <ARInvoice.billContactID>(docgraph.Document.Cache, new_ardoc, apdoc, false); TaxAttribute.SetTaxCalc <ARTran.taxCategoryID, ARTaxAttribute>(docgraph.Transactions.Cache, null, TaxCalc.ManualCalc); PXNoteAttribute.SetNote(docgraph.Document.Cache, new_ardoc, PXNoteAttribute.GetNote(Caches[typeof(ARInvoice)], apdoc)); PXNoteAttribute.SetFileNotes(docgraph.Document.Cache, new_ardoc, PXNoteAttribute.GetFileNotes(Caches[typeof(ARInvoice)], apdoc)); foreach (ARTran aptran in PXSelect <ARTran, Where <ARTran.tranType, Equal <Required <ARTran.tranType> >, And <ARTran.refNbr, Equal <Required <ARTran.refNbr> > > > > .Select(docgraph, apdoc.DocType, apdoc.RefNbr)) { ARTran new_aptran = PXCache <ARTran> .CreateCopy(aptran); new_aptran.RefNbr = null; new_aptran.CuryInfoID = null; docgraph.Transactions.Insert(new_aptran); } foreach (ARTaxTran tax in PXSelect <ARTaxTran, Where <ARTaxTran.tranType, Equal <Required <ARTaxTran.tranType> >, And <ARTaxTran.refNbr, Equal <Required <ARTaxTran.refNbr> > > > > .Select(docgraph, apdoc.DocType, apdoc.RefNbr)) { ARTaxTran new_artax = new ARTaxTran(); new_artax.TaxID = tax.TaxID; new_artax = docgraph.Taxes.Insert(new_artax); if (new_artax != null) { new_artax = PXCache <ARTaxTran> .CreateCopy(new_artax); new_artax.TaxRate = tax.TaxRate; new_artax.CuryTaxableAmt = tax.CuryTaxableAmt; new_artax.CuryTaxAmt = tax.CuryTaxAmt; new_artax = docgraph.Taxes.Update(new_artax); } } 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); }