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) {
- 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 ($bean->getAttribute('option')) $dropdown_parts .= ' and l.id in(select log_id from tt_custom_field_log where status = 1 and option_id = '.$bean->getAttribute('option').')';
- if ($bean->getAttribute('project')) $dropdown_parts .= ' and l.project_id = '.$bean->getAttribute('project');
- if ($bean->getAttribute('task')) $dropdown_parts .= ' and l.task_id = '.$bean->getAttribute('task');
- if ($bean->getAttribute('include_records')=='1') $dropdown_parts .= ' and l.billable = 1';
- if ($bean->getAttribute('include_records')=='2') $dropdown_parts .= ' and l.billable = 0';
- if ($bean->getAttribute('invoice')=='1') $dropdown_parts .= ' and l.invoice_id is not NULL';
- if ($bean->getAttribute('invoice')=='2') $dropdown_parts .= ' and l.invoice_id is NULL';
- if ($bean->getAttribute('paid_status')=='1') $dropdown_parts .= ' and l.paid = 1';
- if ($bean->getAttribute('paid_status')=='2') $dropdown_parts .= ' and l.paid = 0';
-
- // Prepare user list part.
- $userlist = -1;
- if (($user->can('view_reports') || $user->isClient()) && is_array($bean->getAttribute('users')))
- $userlist = join(',', $bean->getAttribute('users'));
- // Prepare sql query part for user list.
- $user_list_part = null;
- if ($user->can('view_reports') || $user->isClient())
- $user_list_part = " and l.user_id in ($userlist)";
- else
- $user_list_part = " and l.user_id = ".$user->id;
-
- // Prepare sql query part for where.
- if ($bean->getAttribute('period'))
- $period = new Period($bean->getAttribute('period'), new DateAndTime($user->date_format));
- else {
- $period = new Period();
- $period->setPeriod(
- new DateAndTime($user->date_format, $bean->getAttribute('start_date')),
- new DateAndTime($user->date_format, $bean->getAttribute('end_date')));
- }
- $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;
- }
-
- // getFavWhere prepares a WHERE clause for a favorite report query.
- static function getFavWhere($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';
- // 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.
- $user_list_part = null;
- if ($user->can('view_reports') || $user->isClient())
+ $userlist = $options['users'] ? $options['users'] : '-1';
+ 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'])
}
// 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 ($bean->getAttribute('project')) $dropdown_parts .= ' and ei.project_id = '.$bean->getAttribute('project');
- if ($bean->getAttribute('invoice')=='1') $dropdown_parts .= ' and ei.invoice_id is not NULL';
- if ($bean->getAttribute('invoice')=='2') $dropdown_parts .= ' and ei.invoice_id is NULL';
- if ($bean->getAttribute('paid_status')=='1') $dropdown_parts .= ' and ei.paid = 1';
- if ($bean->getAttribute('paid_status')=='2') $dropdown_parts .= ' and ei.paid = 0';
-
- // Prepare user list part.
- $userlist = -1;
- if (($user->can('view_reports') || $user->isClient()) && is_array($bean->getAttribute('users')))
- $userlist = join(',', $bean->getAttribute('users'));
- // Prepare sql query part for user list.
- $user_list_part = null;
- if ($user->can('view_reports') || $user->isClient())
- $user_list_part = " and ei.user_id in ($userlist)";
- else
- $user_list_part = " and ei.user_id = ".$user->id;
-
- // Prepare sql query part for where.
- if ($bean->getAttribute('period'))
- $period = new Period($bean->getAttribute('period'), new DateAndTime($user->date_format));
- else {
- $period = new Period();
- $period->setPeriod(
- new DateAndTime($user->date_format, $bean->getAttribute('start_date')),
- new DateAndTime($user->date_format, $bean->getAttribute('end_date')));
- }
- $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.
- $user_list_part = null;
- if ($user->can('view_reports') || $user->isClient())
+ $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'])
$canViewReports = $user->can('view_reports');
$isClient = $user->isClient();
- $group_by_option = $bean->getAttribute('group_by');
- $convertTo12Hour = ('%I:%M %p' == $user->time_format) && ($bean->getAttribute('chstart') || $bean->getAttribute('chfinish'));
+ $group_by_option = $options['group_by'];
+ $convertTo12Hour = ('%I:%M %p' == $user->time_format) && ($options['show_start'] || $options['show_end']);
// Prepare a query for time items in tt_log table.
$fields = array(); // An array of fields for database query.
if($canViewReports || $isClient)
array_push($fields, 'u.name as user');
// Add client name if it is selected.
- if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
+ if ($options['show_client'] || 'client' == $group_by_option)
array_push($fields, 'c.name as client');
// Add project name if it is selected.
- if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
+ if ($options['show_project'] || 'project' == $group_by_option)
array_push($fields, 'p.name as project');
// Add task name if it is selected.
- if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
+ if ($options['show_task'] || 'task' == $group_by_option)
array_push($fields, 't.name as task');
// Add custom field.
- $include_cf_1 = $bean->getAttribute('chcf_1') || 'cf_1' == $group_by_option;
+ $include_cf_1 = $options['show_custom_field_1'] || 'cf_1' == $group_by_option;
if ($include_cf_1) {
$custom_fields = new CustomFields($user->group_id);
$cf_1_type = $custom_fields->fields[0]['type'];
array_push($fields, 'cfo.value as cf_1');
}
}
+
+// TODO: refactoring in progress down from here... The above is identical to getFavItems and is ready to merge.
// Add start time.
if ($bean->getAttribute('chstart')) {
array_push($fields, "l.start as unformatted_start");
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";
$options['name'] = null; // No name required.
$options['user_id'] = $user->id; // Not sure if we need user_id here. Fav reports use it to recycle $user object in cron.php.
$options['client_id'] = $bean->getAttribute('client');
+ $options['cf_1_option_id'] = $bean->getAttribute('option');
+ $options['project_id'] = $bean->getAttribute('project');
+ $options['task_id'] = $bean->getAttribute('task');
+ $options['billable'] = $bean->getAttribute('include_records');
+ $options['invoice'] = $bean->getAttribute('invoice');
+ $options['paid_status'] = $bean->getAttribute('paid_status');
+ if (is_array($bean->getAttribute('users'))) $options['users'] = join(',', $bean->getAttribute('users'));
+ $options['period'] = $bean->getAttribute('period');
+ $options['period_start'] = $bean->getAttribute('start_date');
+ $options['period_end'] = $bean->getAttribute('end_date');
+ $options['show_client'] = $bean->getAttribute('chclient');
/*
* TODO: remaining fields to fill in...
- `client_id` int(11) default NULL, # client id (if selected)
- `cf_1_option_id` int(11) default NULL, # custom field 1 option id (if selected)
- `project_id` int(11) default NULL, # project id (if selected)
- `task_id` int(11) default NULL, # task id (if selected)
- `billable` tinyint(4) default NULL, # whether to include billable, not billable, or all records
- `invoice` tinyint(4) default NULL, # whether to include invoiced, not invoiced, or all records
- `paid_status` tinyint(4) default NULL, # whether to include paid, not paid, or all records
- `users` text default NULL, # Comma-separated list of user ids. Nothing here means "all" users.
- `period` tinyint(4) default NULL, # selected period type for report
- `period_start` date default NULL, # period start
- `period_end` date default NULL, # period end
- `show_client` tinyint(4) NOT NULL default 0, # whether to show client column
`show_invoice` tinyint(4) NOT NULL default 0, # whether to show invoice column
`show_paid` tinyint(4) NOT NULL default 0, # whether to show paid column
`show_ip` tinyint(4) NOT NULL default 0, # whether to show ip column
- `show_project` tinyint(4) NOT NULL default 0, # whether to show project column
- `show_start` tinyint(4) NOT NULL default 0, # whether to show start field
+ */
+ $options['show_project'] = $bean->getAttribute('chproject');
+ $options['show_start'] = $bean->getAttribute('chstart');
+/*
`show_duration` tinyint(4) NOT NULL default 0, # whether to show duration field
`show_cost` tinyint(4) NOT NULL default 0, # whether to show cost field
- `show_task` tinyint(4) NOT NULL default 0, # whether to show task column
- `show_end` tinyint(4) NOT NULL default 0, # whether to show end field
+ */
+ $options['show_task'] = $bean->getAttribute('chtask');
+ $options['show_end'] = $bean->getAttribute('chfinish');
+/*
`show_note` tinyint(4) NOT NULL default 0, # whether to show note column
`show_custom_field_1` tinyint(4) NOT NULL default 0, # whether to show custom field 1
+ */
+ $options['show_custom_field_1'] = $bean->getAttribute('chcf_1');
+ /*
`show_work_units` tinyint(4) NOT NULL default 0, # whether to show work units
`show_totals_only` tinyint(4) NOT NULL default 0, # whether to show totals only
- `group_by` varchar(20) default NULL, # group by field
+ **/
+ $options['group_by'] = $bean->getAttribute('group_by');
+/*
+ * TODO: remaining fields to fill in...
`status` tinyint(4) default 1, # favorite report status
PRIMARY KEY (`id`)
);
*/
return $options;
}
+
+ // verifyBean is a security function to make sure data in bean makes sense for a group.
+ static function verifyBean($bean) {
+ global $user;
+
+ // Check users.
+ $users_in_bean = $bean->getAttribute('users');
+ if (is_array($users_in_bean)) {
+ $users_in_group = ttTeamHelper::getUsers();
+ foreach ($users_in_group as $user_in_group) {
+ $valid_ids[] = $user_in_group['id'];
+ }
+ foreach ($users_in_bean as $user_in_bean) {
+ if (!in_array($user_in_bean, $valid_ids)) {
+ return false;
+ }
+ }
+ }
+
+ // TODO: add additional checks here. Perhaps do it before saving the bean for consistency.
+ return true;
+ }
}