public static void Run(PMAllocator graph, PMProject item, DateTime?date, DateTime?fromDate, DateTime?toDate) { PXSelectBase <PMTask> select = new PXSelect <PMTask, Where <PMTask.projectID, Equal <Required <PMTask.projectID> >, And <PMTask.allocationID, IsNotNull, And <PMTask.isActive, Equal <True> > > > >(graph); List <PMTask> tasks = new List <PMTask>(); foreach (PMTask pmTask in select.Select(item.ContractID)) { tasks.Add(pmTask); } graph.Clear(); graph.FilterStartDate = fromDate; graph.FilterEndDate = toDate; graph.PostingDate = date; graph.Execute(tasks); if (graph.Document.Current != null) { graph.Actions.PressSave(); PMSetup setup = PXSelect <PMSetup> .Select(graph); PMRegister doc = graph.Caches[typeof(PMRegister)].Current as PMRegister; if (doc != null && setup.AutoReleaseAllocation == true) { RegisterRelease.Release(doc); } } else { throw new PXSetPropertyException(Warnings.NothingToAllocate, PXErrorLevel.RowWarning); } }
protected virtual void AutoAllocateTasks(List <PMTask> tasks) { PMSetup setup = PXSelect <PMSetup> .Select(Base); bool autoreleaseAllocation = setup.AutoReleaseAllocation == true; PMAllocator allocator = PXGraph.CreateInstance <PMAllocator>(); allocator.Clear(); allocator.TimeStamp = Base.TimeStamp; allocator.Execute(tasks); allocator.Actions.PressSave(); if (allocator.Document.Current != null && autoreleaseAllocation) { List <PMRegister> list = new List <PMRegister>(); list.Add(allocator.Document.Current); List <ProcessInfo <Batch> > batchList; bool releaseSuccess = RegisterRelease.ReleaseWithoutPost(list, false, out batchList); if (!releaseSuccess) { throw new PXException(PM.Messages.AutoReleaseFailed); } foreach (var item in batchList) { Base.created.AddRange(item.Batches); } } }
public static void Run(PMAllocator graph, PMTask item, DateTime?date, DateTime?fromDate, DateTime?toDate) { graph.Clear(); graph.FilterStartDate = fromDate; graph.FilterEndDate = toDate; graph.Execute(item); if (graph.Document.Current != null) { graph.Actions.PressSave(); PMSetup setup = PXSelect <PMSetup> .Select(graph); PMRegister doc = graph.Caches[typeof(PMRegister)].Current as PMRegister; if (doc != null && setup.AutoReleaseAllocation == true) { RegisterRelease.Release(doc); } } else { throw new PXSetPropertyException(Warnings.NothingToAllocate, PXErrorLevel.RowWarning); } }
public static bool ReleaseWithoutPost(List <PMRegister> list, bool isMassProcess, out List <ProcessInfo <Batch> > infoList) { bool failed = false; infoList = new List <ProcessInfo <Batch> >(); if (!list.Any()) { return(!failed); } RegisterReleaseProcess rg = PXGraph.CreateInstance <RegisterReleaseProcess>(); JournalEntry je = PXGraph.CreateInstance <JournalEntry>(); PMAllocator allocator = PXGraph.CreateInstance <PMAllocator>(); //Task may be IsActive=False - it may be completed. User cannot create transactions with this //TaskID. But the system has to process the given task - hence override the FieldVerification in the Selector. je.FieldVerifying.AddHandler <GLTran.projectID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; }); je.FieldVerifying.AddHandler <GLTran.taskID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; }); for (int i = 0; i < list.Count; i++) { ProcessInfo <Batch> info = new ProcessInfo <Batch>(i); infoList.Add(info); PMRegister doc = list[i]; try { List <PMTask> allocTasks; info.Batches.AddRange(rg.Release(je, doc, out allocTasks)); allocator.Clear(); allocator.TimeStamp = je.TimeStamp; if (allocTasks.Count > 0) { allocator.Execute(allocTasks); allocator.Actions.PressSave(); } if (allocator.Document.Current != null && rg.AutoReleaseAllocation) { List <PMTask> allocTasks2; info.Batches.AddRange(rg.Release(je, allocator.Document.Current, out allocTasks2)); } if (isMassProcess) { PXProcessing <PMRegister> .SetInfo(i, ActionsMessages.RecordProcessed); } } catch (Exception e) { if (isMassProcess) { PXProcessing <PMRegister> .SetError(i, e is PXOuterException?e.Message + "\r\n" + String.Join("\r\n", ((PXOuterException)e).InnerMessages) : e.Message); failed = true; } else { throw new PXMassProcessException(i, e); } } } return(!failed); }
public virtual List <Balance> Run(PMTask task) { if (task.BillingID == null) { throw new PXException(Messages.BillingIDIsNotDefined); } Dictionary <string, Balance> balances = new Dictionary <string, Balance>(); DateTime? lastDate; List <PMTran> expenseTrans = CreateExpenseTransactions(task, out lastDate); List <long> expenseTranIds = new List <long>(); Debug.Print("Created Expense Transactions:"); Debug.Indent(); foreach (PMTran tran in expenseTrans) { expenseTranIds.Add(tran.TranID.Value); Debug.Print("TranID:{0} AccountGroup:{1}, InventoryID={2}, Qty={3}, Amt={4}, Allocated={5}, Released={6}, Billed={7}, Date={8}", tran.TranID, AccountGroupFromID(tran.AccountGroupID), InventoryFromID(tran.InventoryID), tran.Qty, tran.Amount, tran.Allocated, tran.Released, tran.Billed, tran.Date); } Debug.Unindent(); if (expenseTrans.Count == 0) { PXTrace.WriteError(Messages.FailedToEmulateExpenses, task.TaskCD); return(new List <Balance>()); } PMAllocator ae = PXGraph.CreateInstance <PMAllocator>(); ae.SourceTransactions = expenseTrans; foreach (PMTran tran in expenseTrans) { ae.Transactions.Insert(tran); } ae.OverrideAllocationDate = lastDate; ae.Execute(task); Debug.Print("After Allocation:"); Debug.Indent(); foreach (PMTran tran in ae.Transactions.Cache.Inserted) { tran.Released = true; Transactions.Cache.Update(tran); if (expenseTranIds.Contains(tran.TranID.Value)) { continue; } int inventoryID = tran.InventoryID ?? PMInventorySelectorAttribute.EmptyInventoryID; string key = string.Format("{0}.{1}", tran.AccountGroupID, inventoryID); if (!balances.ContainsKey(key)) { Balance b = new Balance(); b.AccountGroupID = tran.AccountGroupID.Value; b.InventoryID = inventoryID; balances.Add(key, b); } Debug.Print("TranID:{0} AccountGroup:{1}, InventoryID={2}, Qty={3}, Amt={4}, Allocated={5}, Released={6}, Billed={7}, Date={8}", tran.TranID, AccountGroupFromID(tran.AccountGroupID), InventoryFromID(tran.InventoryID), tran.Qty, tran.Amount, tran.Allocated, tran.Released, tran.Billed, tran.Date); } Debug.Unindent(); DateTime billingDate = lastDate.Value.AddDays(1); //Get ARTrans for Bill: Debug.Print("Bill using the following Billing date={0}", billingDate); PMBillEngine engine = PXGraph.CreateInstance <PMBillEngine>(); engine.IsEmulation = true; engine.FieldVerifying.AddHandler <PMTran.projectID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; }); engine.FieldVerifying.AddHandler <PMTran.taskID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; }); Debug.Print("Transactions passed to BillTask:"); Debug.Indent(); foreach (PMTran tran in Transactions.Cache.Cached) { if (expenseTranIds.Contains(tran.TranID.Value)) { continue; } engine.Transactions.Insert(tran); Debug.Print("TranID:{0} AccountGroup:{1}, InventoryID={2}, Qty={3}, Amt={4}, Allocated={5}, Released={6}, Billed={7}, Date={8}", tran.TranID, AccountGroupFromID(tran.AccountGroupID), InventoryFromID(tran.InventoryID), tran.Qty, tran.Amount, tran.Allocated, tran.Released, tran.Billed, tran.Date); } Debug.Unindent(); List <PMBillEngine.BillingData> arTrans = engine.BillTask(task, billingDate); //Create ARInvoice to DEFAULT Accounts and SubAccounts. ARInvoiceEntry invoiceEntry = PXGraph.CreateInstance <ARInvoiceEntry>(); //Project and Task is in Planning State - thus suppress verification: invoiceEntry.FieldVerifying.AddHandler <ARInvoice.projectID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; }); invoiceEntry.FieldVerifying.AddHandler <ARTran.projectID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; }); invoiceEntry.FieldVerifying.AddHandler <ARTran.taskID>((PXCache sender, PXFieldVerifyingEventArgs e) => { e.Cancel = true; }); ARInvoice invoice = new ARInvoice(); invoice.DocType = ARDocType.Invoice; invoice = PXCache <ARInvoice> .CreateCopy(invoiceEntry.Document.Insert(invoice)); invoice.DocDate = billingDate; invoice.CustomerID = task.CustomerID; invoice.CustomerLocationID = task.LocationID; invoice.ProjectID = task.ProjectID; invoice = (ARInvoice)invoiceEntry.Document.Update(invoice); CurrencyInfo curyinfo = (CurrencyInfo)invoiceEntry.Caches[typeof(CurrencyInfo)].Current; foreach (PMBillEngine.BillingData data in arTrans) { decimal curyamount; PXDBCurrencyAttribute.CuryConvCury(invoiceEntry.Caches[typeof(CurrencyInfo)], curyinfo, (data.Tran.UnitPrice ?? 0), out curyamount); data.Tran.CuryUnitPrice = curyamount; PXDBCurrencyAttribute.CuryConvCury(invoiceEntry.Caches[typeof(CurrencyInfo)], curyinfo, (data.Tran.ExtPrice ?? 0), out curyamount); data.Tran.CuryExtPrice = curyamount; data.Tran.CuryTranAmt = data.Tran.CuryExtPrice; data.Tran.FreezeManualDisc = true; ARTran newTran = (ARTran)invoiceEntry.Caches[typeof(ARTran)].Insert(data.Tran); } ARInvoice oldInvoice = (ARInvoice)invoiceEntry.Caches[typeof(ARInvoice)].CreateCopy(invoice); invoice.CuryOrigDocAmt = invoice.CuryDocBal; invoiceEntry.Caches[typeof(ARInvoice)].RaiseRowUpdated(invoice, oldInvoice); Debug.Print("AR Trans:"); Debug.Indent(); foreach (ARTran tran in invoiceEntry.Transactions.Select()) { if (tran.TaskID == task.TaskID) { Debug.Print("InventoryID={0}, Qty={1}, Amt={2}", InventoryFromID(tran.InventoryID), tran.Qty, tran.TranAmt); Account acct = PXSelect <Account, Where <Account.accountID, Equal <Required <Account.accountID> > > > .Select(invoiceEntry, tran.AccountID); if (acct.AccountGroupID == null) { throw new PXException("Failed to emulate billing. The Sales Account in the Invoice is not mapped to any Account Group."); } string key = string.Format("{0}.{1}", acct.AccountGroupID, tran.InventoryID ?? PMInventorySelectorAttribute.EmptyInventoryID); if (balances.ContainsKey(key)) { balances[key].Amount += tran.TranAmt ?? 0; balances[key].Quantity += tran.Qty ?? 0; } else { Balance b = new Balance(); b.AccountGroupID = acct.AccountGroupID.Value; b.InventoryID = tran.InventoryID ?? PMInventorySelectorAttribute.EmptyInventoryID; b.Amount = tran.TranAmt ?? 0; b.Quantity = tran.Qty ?? 0; balances.Add(key, b); } } } return(new List <Balance>(balances.Values)); }