Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class EmployeeActivitiesEntry_Extension : PXGraphExtension<EmployeeActivitiesEntry>
{
public PXAction<EmployeeActivitiesEntry.PMTimeActivityFilter> GenerateTimeCards;
[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Generate Time Cards")]
protected virtual IEnumerable generateTimeCards(PXAdapter adapter)
{
EmployeeActivitiesEntry.PMTimeActivityFilter filter = Base.Filter.Current;
if (filter.OwnerID == null) return adapter.Get();
Base.Actions.PressSave();
Guid owner = filter.OwnerID.Value;
EPEmployee employee = PXSelect<EPEmployee, Where<EPEmployee.userID, Equal<Required<EPEmployee.userID>>>>.Select(Base, owner);
if (employee == null) throw new PXException(Neural.Messages.NoEmployee);
Int32 employeeid = employee.BAccountID.Value;
PXLongOperation.StartOperation(Base, () =>
{
DoGenerateTimeCards(owner, employeeid);
});
return adapter.Get();
}
public static void DoGenerateTimeCards(Guid owner, Int32 employeeID)
{
TimeCardMaint graph = PXGraph.CreateInstance<TimeCardMaint>();
EPTimeCard firstTimeCard = PXSelectReadonly<EPTimeCard,
Where<EPTimeCard.employeeID, Equal<Required<EPTimeCard.employeeID>>>, OrderBy<Asc<EPTimeCard.weekId>>>.SelectWindowed(graph, 0, 1, employeeID);
EPTimeCard lastTimeCard = PXSelectReadonly<EPTimeCard,
Where<EPTimeCard.employeeID, Equal<Required<EPTimeCard.employeeID>>>, OrderBy<Desc<EPTimeCard.weekId>>>.SelectWindowed(graph, 0, 1, employeeID);
EPActivityApprove firstActivity = PXSelectReadonly<EPActivityApprove,
Where<EPActivityApprove.ownerID, Equal<Required<EPActivityApprove.ownerID>>,
And<EPActivityApprove.released, NotEqual<True>,
And<EPActivityApprove.timeCardCD, IsNull>>>,
OrderBy<Asc<EPActivityApprove.date>>>.SelectWindowed(graph, 0, 1, owner);
if (firstActivity == null) return; // nothing to process
EPActivityApprove lastActivity = PXSelectReadonly<EPActivityApprove,
Where<EPActivityApprove.ownerID, Equal<Required<EPActivityApprove.ownerID>>,
And<EPActivityApprove.released, NotEqual<True>,
And<EPActivityApprove.timeCardCD, IsNull>>>,
OrderBy<Desc<EPActivityApprove.date>>>.SelectWindowed(graph, 0, 1, owner);
//Corrrections
DateTime startDate = firstActivity.Date.Value;
if(firstTimeCard != null && firstTimeCard.WeekStartDate > startDate) startDate = firstTimeCard.WeekStartDate.Value;
Int32 weekID = PXWeekSelector2Attribute.GetWeekID(graph, startDate);
do
{
Boolean newCard = false, anyChange = false;
EPTimeCard card = PXSelectReadonly<EPTimeCard,
Where<EPTimeCard.employeeID, Equal<Required<EPTimeCard.employeeID>>,
And<EPTimeCard.weekId, Equal<Required<EPTimeCard.weekId>>>>>.SelectWindowed(graph, 0, 1, employeeID, weekID);
if (card == null)
{
card = (EPTimeCard)graph.Document.Cache.CreateInstance();
card.WeekID = weekID;
card.EmployeeID = employeeID;
card = graph.Document.Insert(card);
newCard = true;
}
if (!newCard)
{
Boolean anyActivity = PXSelectReadonly<EPActivityApprove,
Where<EPActivityApprove.ownerID, Equal<Required<EPActivityApprove.ownerID>>,
And<EPActivityApprove.timeCardCD, IsNull,
And<EPActivityApprove.released, NotEqual<True>,
And<EPActivityApprove.date, GreaterEqual<Required<EPActivityApprove.date>>,
And<EPActivityApprove.date, LessEqual<Required<EPActivityApprove.date>>>>>>>>.Select(graph, owner,
PXWeekSelector2Attribute.GetWeekStartDate(graph, weekID), PXWeekSelector2Attribute.GetWeekEndDate(graph, weekID)).Count > 0;
if (anyActivity)
{
graph.Document.Current = card;
if(card.IsReleased ?? false) card = ExecAction<EPTimeCard>(graph.Document.Cache, graph.correct, card);
if (card.IsApproved ?? false) card = ExecAction<EPTimeCard>(graph.Document.Cache, graph.edit, card);
anyChange = true;
}
}
if (newCard || anyChange)
{
graph.Save.Press();
if (card.IsHold ?? false) card = ExecAction<EPTimeCard>(graph.Document.Cache, graph.submit, card);
//if (card.IsApproved ?? false) card = ExecAction<EPTimeCard>(graph.Document.Cache, graph.release, card);
}
weekID = PXWeekSelector2Attribute.GetNextWeekID(graph, weekID);
}
while (PXWeekSelector2Attribute.GetWeekStartDate(weekID) < lastActivity.Date);
}
public static T ExecAction<T>(PXCache cache, PXAction action, T row)
where T : class
{
PXSelectDelegate handler = new PXSelectDelegate(delegate() { return new object[] { row }; });
PXAdapter adapter = new PXAdapter(new PXView(cache.Graph, true, BqlCommand.CreateInstance(typeof(Select<>), cache.GetItemType()), handler));
foreach (T item in action.Press(adapter))
{
return item;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.