if (!$bean->getAttribute('chunits')) $bean->setAttribute('chunits', 0);
if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0);
- if ($bean->getAttribute('users') && is_array($bean->getAttribute('users'))) {
- $users_in_bean = $bean->getAttribute('users');
-
- // If all users are selected - use a null value (which means "all users").
- $all_users_selected = true;
- if ($user->can('view_reports')) {
- $all = ttTeamHelper::getActiveUsers();
- foreach ($all as $one) {
- if (!in_array($one['id'], $users_in_bean)) {
- $all_users_selected = false;
- break;
- }
- }
- }
- if ($all_users_selected)
- $users = null;
- else
- $users = join(',', $users_in_bean);
+ $users_in_bean = $bean->getAttribute('users');
+ if ($users_in_bean && is_array($users_in_bean)) {
+ $users = join(',', $users_in_bean);
}
if ($bean->getAttribute('start_date')) {
$dt = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
$bean->setAttribute('include_records', $val['billable']);
$bean->setAttribute('invoice', $val['invoice']);
$bean->setAttribute('paid_status', $val['paid_status']);
- if ($val['users'])
- $bean->setAttribute('users', explode(',', $val['users']));
- else {
- // Null users value means "all users". Add them to the bean.
- if ($user->can('view_reports')) {
- $all = ttTeamHelper::getActiveUsers();
- foreach ($all as $one) {
- $all_user_ids[] = $one['id'];
- }
- $bean->setAttribute('users', $all_user_ids);
- }
- }
+ $bean->setAttribute('users', explode(',', $val['users']));
$bean->setAttribute('period', $val['period']);
if ($val['period_start']) {
$dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
unset($options['report_spec']); // Currently not used.
unset($options['status']);
+ // Note: special handling for NULL users field is done in cron.php
+
// $options now is a subset of db fields from tt_fav_reports table.
return $options;
}
class ttReportHelper {
// getWhere prepares a WHERE clause for a report query.
- // Note: $options is a future replacement of $bean, which is work in progress.
- static function getWhere($bean, $options) {
+ static function getWhere($options) {
global $user;
// Prepare dropdown parts.
if ($options['paid_status']=='1') $dropdown_parts .= ' and l.paid = 1';
if ($options['paid_status']=='2') $dropdown_parts .= ' and l.paid = 0';
- // Note: "Prepare sql query part for user list" is different in getFavWhere because of
- // special meaning of NULL value (all "active" users).
- //
- // If we are merging into one function, one needs to take care of this, perhaps, with redesign.
-
// Prepare sql query part for user list.
$userlist = $options['users'] ? $options['users'] : '-1';
if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
return $where;
}
- // getFavWhere prepares a WHERE clause for a favorite report query.
- static function getFavWhere($options) {
- global $user;
-
- // Prepare dropdown parts.
- $dropdown_parts = '';
- if ($options['client_id'])
- $dropdown_parts .= ' and l.client_id = '.$options['client_id'];
- elseif ($user->isClient() && $user->client_id)
- $dropdown_parts .= ' and l.client_id = '.$user->client_id;
- if ($options['cf_1_option_id']) $dropdown_parts .= ' and l.id in(select log_id from tt_custom_field_log where status = 1 and option_id = '.$options['cf_1_option_id'].')';
- if ($options['project_id']) $dropdown_parts .= ' and l.project_id = '.$options['project_id'];
- if ($options['task_id']) $dropdown_parts .= ' and l.task_id = '.$options['task_id'];
- if ($options['billable']=='1') $dropdown_parts .= ' and l.billable = 1';
- if ($options['billable']=='2') $dropdown_parts .= ' and l.billable = 0';
- if ($options['invoice']=='1') $dropdown_parts .= ' and l.invoice_id is not NULL';
- if ($options['invoice']=='2') $dropdown_parts .= ' and l.invoice_id is NULL';
- if ($options['paid_status']=='1') $dropdown_parts .= ' and l.paid = 1';
- if ($options['paid_status']=='2') $dropdown_parts .= ' and l.paid = 0';
-
- // Prepare user list part.
- $userlist = -1;
- if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
- if ($options['users'])
- $userlist = $options['users'];
- else {
- $group_users = ttTeamHelper::getUsers(); // active and inactive users
- foreach ($group_users as $single_user)
- $users[] = $single_user['id'];
- $userlist = join(',', $users);
- }
- }
- // Prepare sql query part for user list.
- if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
- $user_list_part = " and l.user_id in ($userlist)";
- else
- $user_list_part = " and l.user_id = ".$user->id;
- $user_list_part .= " and l.group_id = ".$user->getActiveGroup();
-
- // Prepare sql query part for where.
- if ($options['period'])
- $period = new Period($options['period'], new DateAndTime($user->date_format));
- else {
- $period = new Period();
- $period->setPeriod(
- new DateAndTime($user->date_format, $options['period_start']),
- new DateAndTime($user->date_format, $options['period_end']));
- }
- $where = " where l.status = 1 and l.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and l.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
- " $user_list_part $dropdown_parts";
- return $where;
- }
-
// getExpenseWhere prepares WHERE clause for expenses query in a report.
- static function getExpenseWhere($bean, $options) {
- global $user;
-
- // Prepare dropdown parts.
- $dropdown_parts = '';
- if ($options['client_id'])
- $dropdown_parts .= ' and l.client_id = '.$options['client_id'];
- elseif ($user->isClient() && $user->client_id)
- $dropdown_parts .= ' and ei.client_id = '.$user->client_id;
- if ($options['project_id']) $dropdown_parts .= ' and ei.project_id = '.$options['project_id'];
- if ($options['invoice']=='1') $dropdown_parts .= ' and ei.invoice_id is not NULL';
- if ($options['invoice']=='2') $dropdown_parts .= ' and ei.invoice_id is NULL';
- if ($options['paid_status']=='1') $dropdown_parts .= ' and ei.paid = 1';
- if ($options['paid_status']=='2') $dropdown_parts .= ' and ei.paid = 0';
-
- // Prepare sql query part for user list.
- $userlist = $options['users'] ? $options['users'] : '-1';
- if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
- $user_list_part = " and ei.user_id in ($userlist)";
- else
- $user_list_part = " and ei.user_id = ".$user->id;
- $user_list_part .= " and ei.group_id = ".$user->getActiveGroup();
-
- // Prepare sql query part for where.
- if ($options['period'])
- $period = new Period($options['period'], new DateAndTime($user->date_format));
- else {
- $period = new Period();
- $period->setPeriod(
- new DateAndTime($user->date_format, $options['period_start']),
- new DateAndTime($user->date_format, $options['period_end']));
- }
- $where = " where ei.status = 1 and ei.date >= '".$period->getStartDate(DB_DATEFORMAT)."' and ei.date <= '".$period->getEndDate(DB_DATEFORMAT)."'".
- " $user_list_part $dropdown_parts";
- return $where;
- }
-
- // getFavExpenseWhere prepares a WHERE clause for expenses query in a favorite report.
- static function getFavExpenseWhere($options) {
+ static function getExpenseWhere($options) {
global $user;
// Prepare dropdown parts.
if ($options['paid_status']=='1') $dropdown_parts .= ' and ei.paid = 1';
if ($options['paid_status']=='2') $dropdown_parts .= ' and ei.paid = 0';
- // Prepare user list part.
- $userlist = -1;
- if (($user->can('view_reports') || $user->isClient())) {
- if ($options['users'])
- $userlist = $options['users'];
- else {
- $active_users = ttTeamHelper::getActiveUsers();
- foreach ($active_users as $single_user)
- $users[] = $single_user['id'];
- $userlist = join(',', $users);
- }
- }
// Prepare sql query part for user list.
+ $userlist = $options['users'] ? $options['users'] : '-1';
if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
$user_list_part = " and ei.user_id in ($userlist)";
else
if ($includeCost && MODE_TIME != $user->tracking_mode)
$left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
- $where = ttReportHelper::getWhere($bean, $options);
+ $where = ttReportHelper::getWhere($options);
// Construct sql query for tt_log items.
$sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where";
if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
$left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
- $where = ttReportHelper::getExpenseWhere($bean, $options);
+ $where = ttReportHelper::getExpenseWhere($options);
// Construct sql query for expense items.
$sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where";
if ($includeCost && MODE_TIME != $user->tracking_mode)
$left_joins .= " left join tt_user_project_binds upb on (l.user_id = upb.user_id and l.project_id = upb.project_id)";
- $where = ttReportHelper::getFavWhere($options);
+ $where = ttReportHelper::getWhere($options);
// Construct sql query for tt_log items.
$sql = "select ".join(', ', $fields)." from tt_log l $left_joins $where";
if (($canViewReports || $isClient) && $options['show_invoice'])
$left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
- $where = ttReportHelper::getFavExpenseWhere($options);
+ $where = ttReportHelper::getExpenseWhere($options);
// Construct sql query for expense items.
$sql_for_expense_items = "select ".join(', ', $fields)." from tt_expense_items ei $left_joins $where";
break;
}
- $where = ttReportHelper::getWhere($bean, $options);
+ $where = ttReportHelper::getWhere($options);
if ($bean->getAttribute('chcost')) {
if (MODE_TIME == $user->tracking_mode) {
if ($group_by_option != 'user')
break;
}
- $where = ttReportHelper::getExpenseWhere($bean, $options);
+ $where = ttReportHelper::getExpenseWhere($options);
$sql_for_expenses = "select $group_field as group_field, null as time";
if ($bean->getAttribute('chunits')) $sql_for_expenses .= ", null as units";
$sql_for_expenses .= ", sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei $group_join $where";
break;
}
- $where = ttReportHelper::getFavWhere($options);
+ $where = ttReportHelper::getWhere($options);
if ($options['show_cost']) {
if (MODE_TIME == $user->tracking_mode) {
if ($group_by_option != 'user')
break;
}
- $where = ttReportHelper::getFavExpenseWhere($options);
+ $where = ttReportHelper::getExpenseWhere($options);
$sql_for_expenses = "select $group_field as group_field, null as time";
if ($options['show_work_units']) $sql_for_expenses .= ", null as units";
$sql_for_expenses .= ", sum(ei.cost) as cost, sum(ei.cost) as expenses from tt_expense_items ei $group_join $where";
$mdb2 = getConnection();
- $where = ttReportHelper::getWhere($bean, $options);
+ $where = ttReportHelper::getWhere($options);
// Prepare parts.
$time_part = "sum(time_to_sec(l.duration)) as time";
// If we have expenses, query becomes a bit more complex.
if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) {
- $where = ttReportHelper::getExpenseWhere($bean, $options);
+ $where = ttReportHelper::getExpenseWhere($options);
$sql_for_expenses = "select null as time";
if ($bean->getAttribute('chunits')) $sql_for_expenses .= ", null as units";
$sql_for_expenses .= ", sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";
$mdb2 = getConnection();
- $where = ttReportHelper::getFavWhere($options);
+ $where = ttReportHelper::getWhere($options);
// Prepare parts.
$time_part = "sum(time_to_sec(l.duration)) as time";
// If we have expenses, query becomes a bit more complex.
if ($options['show_cost'] && $user->isPluginEnabled('ex')) {
- $where = ttReportHelper::getFavExpenseWhere($options);
+ $where = ttReportHelper::getExpenseWhere($options);
$sql_for_expenses = "select null as time";
if ($options['show_work_units']) $sql_for_expenses .= ", null as units";
$sql_for_expenses .= ", sum(cost) as cost, sum(cost) as expenses from tt_expense_items ei $where";