Commands - timesheet

- Migrated handleReportTimesheet to CmdTimesheet.
This commit is contained in:
Paul Beckingham 2011-05-30 13:46:08 -04:00
parent 2fb743992f
commit 75a24f5113
9 changed files with 230 additions and 146 deletions

View file

@ -51,143 +51,6 @@ static void countTasks (const std::vector <Task>&, const std::string&, const std
extern Context context;
////////////////////////////////////////////////////////////////////////////////
int handleReportTimesheet (std::string& outs)
{
int rc = 0;
// Scan the pending tasks.
std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking"));
handleRecurrence ();
context.tdb.load (tasks, context.filter);
context.tdb.commit ();
context.tdb.unlock ();
// Just do this once.
int width = context.getWidth ();
// What day of the week does the user consider the first?
int weekStart = Date::dayOfWeek (context.config.get ("weekstart"));
if (weekStart != 0 && weekStart != 1)
throw std::string ("The 'weekstart' configuration variable may "
"only contain 'Sunday' or 'Monday'.");
// Determine the date of the first day of the most recent report.
Date today;
Date start;
start -= (((today.dayOfWeek () - weekStart) + 7) % 7) * 86400;
// Roll back to midnight.
start = Date (start.month (), start.day (), start.year ());
Date end = start + (7 * 86400);
// Determine how many reports to run.
int quantity = 1;
if (context.sequence.size () == 1)
quantity = context.sequence[0];
std::stringstream out;
for (int week = 0; week < quantity; ++week)
{
Date endString (end);
endString -= 86400;
std::string title = start.toString (context.config.get ("dateformat"))
+ " - "
+ endString.toString (context.config.get ("dateformat"));
Color bold (Color::nocolor, Color::nocolor, false, true, false);
out << "\n"
<< (context.color () ? bold.colorize (title) : title)
<< "\n";
// Render the completed table.
ViewText completed;
completed.width (width);
completed.add (Column::factory ("string", " "));
completed.add (Column::factory ("string", "Project"));
completed.add (Column::factory ("string.right", "Due"));
completed.add (Column::factory ("string", "Description"));
std::vector <Task>::iterator task;
for (task = tasks.begin (); task != tasks.end (); ++task)
{
// If task completed within range.
if (task->getStatus () == Task::completed)
{
Date compDate (atoi (task->get ("end").c_str ()));
if (compDate >= start && compDate < end)
{
Color c (task->get ("fg") + " " + task->get ("bg"));
if (context.color ())
autoColorize (*task, c);
int row = completed.addRow ();
std::string format = context.config.get ("dateformat.report");
if (format == "")
format = context.config.get ("dateformat");
completed.set (row, 1, task->get ("project"), c);
completed.set (row, 2, getDueDate (*task, format), c);
completed.set (row, 3, getFullDescription (*task, "timesheet"), c);
}
}
}
out << " Completed (" << completed.rows () << " tasks)\n";
if (completed.rows ())
out << completed.render ()
<< "\n";
// Now render the started table.
ViewText started;
started.width (width);
started.add (Column::factory ("string", " "));
started.add (Column::factory ("string", "Project"));
started.add (Column::factory ("string.right", "Due"));
started.add (Column::factory ("string", "Description"));
for (task = tasks.begin (); task != tasks.end (); ++task)
{
// If task started within range, but not completed withing range.
if (task->getStatus () == Task::pending &&
task->has ("start"))
{
Date startDate (atoi (task->get ("start").c_str ()));
if (startDate >= start && startDate < end)
{
Color c (task->get ("fg") + " " + task->get ("bg"));
if (context.color ())
autoColorize (*task, c);
int row = started.addRow ();
std::string format = context.config.get ("dateformat.report");
if (format == "")
format = context.config.get ("dateformat");
started.set (row, 1, task->get ("project"), c);
started.set (row, 2, getDueDate (*task, format), c);
started.set (row, 3, getFullDescription (*task, "timesheet"), c);
}
}
}
out << " Started (" << started.rows () << " tasks)\n";
if (started.rows ())
out << started.render ()
<< "\n\n";
// Prior week.
start -= 7 * 86400;
end -= 7 * 86400;
}
outs = out.str ();
return rc;
}
///////////////////////////////////////////////////////////////////////////////
std::string getFullDescription (Task& task, const std::string& report)
{