Timeclock events now record their file position

This commit is contained in:
John Wiegley 2009-11-11 02:01:34 -05:00
parent a4b1e7c5ab
commit ed9209cc27
3 changed files with 64 additions and 56 deletions

View file

@ -422,9 +422,19 @@ void instance_t::clock_in_directive(char * line,
else
end = NULL;
timelog.clock_in(parse_datetime(datetime, current_year),
account_stack.front()->find_account(p),
n ? n : "", end ? end : "");
position_t position;
position.pathname = pathname;
position.beg_pos = line_beg_pos;
position.beg_line = linenum;
position.end_pos = curr_pos;
position.end_line = linenum;
time_xact_t event(position, parse_datetime(datetime, current_year),
p ? account_stack.front()->find_account(p) : NULL,
n ? n : "",
end ? end : "");
timelog.clock_in(event);
}
void instance_t::clock_out_directive(char * line,
@ -441,9 +451,19 @@ void instance_t::clock_out_directive(char * line,
else
end = NULL;
timelog.clock_out(parse_datetime(datetime, current_year),
position_t position;
position.pathname = pathname;
position.beg_pos = line_beg_pos;
position.beg_line = linenum;
position.end_pos = curr_pos;
position.end_line = linenum;
time_xact_t event(position, parse_datetime(datetime, current_year),
p ? account_stack.front()->find_account(p) : NULL,
n ? n : "", end ? end : "");
n ? n : "",
end ? end : "");
timelog.clock_out(event);
count++;
}

View file

@ -41,11 +41,8 @@ namespace ledger {
namespace {
void clock_out_from_timelog(std::list<time_xact_t>& time_xacts,
const datetime_t& when,
account_t * account,
const char * desc,
const char * note,
journal_t& journal)
time_xact_t out_event,
journal_t& journal)
{
time_xact_t event;
@ -56,7 +53,7 @@ namespace {
else if (time_xacts.empty()) {
throw parse_error(_("Timelog check-out event without a check-in"));
}
else if (! account) {
else if (! out_event.account) {
throw parse_error
(_("When multiple check-ins are active, checking out requires an account"));
}
@ -66,7 +63,7 @@ namespace {
for (std::list<time_xact_t>::iterator i = time_xacts.begin();
i != time_xacts.end();
i++)
if (account == (*i).account) {
if (out_event.account == (*i).account) {
event = *i;
found = true;
time_xacts.erase(i);
@ -78,36 +75,37 @@ namespace {
(_("Timelog check-out event does not match any current check-ins"));
}
if (desc && event.desc.empty()) {
event.desc = desc;
desc = NULL;
if (out_event.checkin < event.checkin)
throw parse_error
(_("Timelog check-out date less than corresponding check-in"));
if (! out_event.desc.empty() && event.desc.empty()) {
event.desc = out_event.desc;
out_event.desc = empty_string;
}
if (note && event.note.empty()) {
event.note = note;
note = NULL;
}
if (! out_event.note.empty() && event.note.empty())
event.note = out_event.note;
std::auto_ptr<xact_t> curr(new xact_t);
curr->_date = when.date();
curr->code = desc ? desc : "";
curr->_date = out_event.checkin.date();
curr->code = out_event.desc; // if it wasn't used above
curr->payee = event.desc;
curr->pos = event.position;
if (! event.note.empty())
curr->append_note(event.note.c_str());
if (when < event.checkin)
throw parse_error
(_("Timelog check-out date less than corresponding check-in"));
char buf[32];
std::sprintf(buf, "%lds", long((when - event.checkin).total_seconds()));
std::sprintf(buf, "%lds", long((out_event.checkin - event.checkin)
.total_seconds()));
amount_t amt;
amt.parse(buf);
VERIFY(amt.valid());
post_t * post = new post_t(event.account, amt, POST_VIRTUAL);
post->set_state(item_t::CLEARED);
post->pos = event.position;
curr->add_post(post);
if (! journal.add_xact(curr.get()))
@ -128,20 +126,16 @@ time_log_t::~time_log_t()
accounts.push_back(time_xact.account);
foreach (account_t * account, accounts)
clock_out_from_timelog(time_xacts, CURRENT_TIME(), account,
NULL, NULL, journal);
clock_out_from_timelog(time_xacts,
time_xact_t(none, CURRENT_TIME(), account),
journal);
assert(time_xacts.empty());
}
}
void time_log_t::clock_in(const datetime_t& checkin,
account_t * account,
const string& desc,
const string& note)
void time_log_t::clock_in(time_xact_t event)
{
time_xact_t event(checkin, account, desc, note);
if (! time_xacts.empty()) {
foreach (time_xact_t& time_xact, time_xacts) {
if (event.account == time_xact.account)
@ -152,16 +146,12 @@ void time_log_t::clock_in(const datetime_t& checkin,
time_xacts.push_back(event);
}
void time_log_t::clock_out(const datetime_t& checkin,
account_t * account,
const string& desc,
const string& note)
void time_log_t::clock_out(time_xact_t event)
{
if (time_xacts.empty())
throw std::logic_error(_("Timelog check-out event without a check-in"));
clock_out_from_timelog(time_xacts, checkin, account, desc.c_str(),
note.c_str(), journal);
clock_out_from_timelog(time_xacts, event, journal);
}
} // namespace ledger

View file

@ -44,6 +44,7 @@
#include "utils.h"
#include "times.h"
#include "item.h"
namespace ledger {
@ -57,20 +58,24 @@ public:
account_t * account;
string desc;
string note;
position_t position;
time_xact_t() : account(NULL) {
TRACE_CTOR(time_xact_t, "");
}
time_xact_t(const datetime_t& _checkin,
account_t * _account = NULL,
const string& _desc = "",
const string& _note = "")
: checkin(_checkin), account(_account), desc(_desc), note(_note) {
TRACE_CTOR(time_xact_t, "const datetime_t&, account_t *, string, string");
time_xact_t(const optional<position_t>& _position,
const datetime_t& _checkin,
account_t * _account = NULL,
const string& _desc = "",
const string& _note = "")
: checkin(_checkin), account(_account), desc(_desc), note(_note),
position(_position ? *_position : position_t()) {
TRACE_CTOR(time_xact_t,
"position_t, datetime_t, account_t *, string, string");
}
time_xact_t(const time_xact_t& xact)
: checkin(xact.checkin), account(xact.account),
desc(xact.desc), note(xact.note) {
desc(xact.desc), note(xact.note), position(xact.position) {
TRACE_CTOR(time_xact_t, "copy");
}
~time_xact_t() throw() {
@ -89,15 +94,8 @@ public:
}
~time_log_t();
void clock_in(const datetime_t& checkin,
account_t * account = NULL,
const string& desc = "",
const string& note = "");
void clock_out(const datetime_t& checkin,
account_t * account = NULL,
const string& desc = "",
const string& note = "");
void clock_in(time_xact_t event);
void clock_out(time_xact_t event);
};
} // namespace ledger