X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/timetracker.git/blobdiff_plain/6072ff00f2f58e59e2e1138771038b42bc9f32aa..0092d0f0e70b66795f5ab0fcff4bfcef89842f3d:/WEB-INF/lib/ttReportHelper.class.php diff --git a/WEB-INF/lib/ttReportHelper.class.php b/WEB-INF/lib/ttReportHelper.class.php index 74ddd582..612180a9 100644 --- a/WEB-INF/lib/ttReportHelper.class.php +++ b/WEB-INF/lib/ttReportHelper.class.php @@ -37,8 +37,7 @@ require_once(dirname(__FILE__).'/../../plugins/CustomFields.class.php'); 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. @@ -57,71 +56,13 @@ class ttReportHelper { 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'; - $user_list_part = null; 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; - - // 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; - } - - // 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->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()) - $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']) @@ -138,13 +79,13 @@ class ttReportHelper { } // getExpenseWhere prepares WHERE clause for expenses query in a report. - static function getExpenseWhere($bean, $options) { + static function getExpenseWhere($options) { global $user; // Prepare dropdown parts. $dropdown_parts = ''; if ($options['client_id']) - $dropdown_parts .= ' and l.client_id = '.$options['client_id']; + $dropdown_parts .= ' and ei.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']; @@ -155,60 +96,11 @@ class ttReportHelper { // Prepare sql query part for user list. $userlist = $options['users'] ? $options['users'] : '-1'; - $user_list_part = null; 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; - - // 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) { - global $user; - - // Prepare dropdown parts. - $dropdown_parts = ''; - if ($options['client_id']) - $dropdown_parts .= ' and ei.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 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()) - $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']) @@ -235,8 +127,8 @@ class ttReportHelper { $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. @@ -246,16 +138,16 @@ class ttReportHelper { 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']; @@ -266,28 +158,28 @@ class ttReportHelper { } } // Add start time. - if ($bean->getAttribute('chstart')) { + if ($options['show_start']) { array_push($fields, "l.start as unformatted_start"); array_push($fields, "TIME_FORMAT(l.start, '%k:%i') as start"); } // Add finish time. - if ($bean->getAttribute('chfinish')) + if ($options['show_end']) array_push($fields, "TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), '%k:%i') as finish"); // Add duration. - if ($bean->getAttribute('chduration')) + if ($options['show_duration']) array_push($fields, "TIME_FORMAT(l.duration, '%k:%i') as duration"); // Add work units. - if ($bean->getAttribute('chunits')) { + if ($options['show_work_units']) { if ($user->unit_totals_only) array_push($fields, "null as units"); else - array_push($fields, "if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit)) as units"); + array_push($fields, "if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit)) as units"); } // Add note. - if ($bean->getAttribute('chnote')) + if ($options['show_note']) array_push($fields, 'l.comment as note'); // Handle cost. - $includeCost = $bean->getAttribute('chcost'); + $includeCost = $options['show_cost']; if ($includeCost) { if (MODE_TIME == $user->tracking_mode) array_push($fields, "cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2)) as cost"); // Use default user rate. @@ -296,31 +188,30 @@ class ttReportHelper { array_push($fields, "null as expense"); } // Add paid status. - if ($canViewReports && $bean->getAttribute('chpaid')) + if ($canViewReports && $options['show_paid']) array_push($fields, 'l.paid as paid'); // Add IP address. - if ($canViewReports && $bean->getAttribute('chip')) { + if ($canViewReports && $options['show_ip']) { array_push($fields, 'l.created as created'); array_push($fields, 'l.created_ip as created_ip'); array_push($fields, 'l.modified as modified'); array_push($fields, 'l.modified_ip as modified_ip'); } - // Add invoice name if it is selected. - if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice')) + if (($canViewReports || $isClient) && $options['show_invoice']) array_push($fields, 'i.name as invoice'); // Prepare sql query part for left joins. $left_joins = null; - if ($bean->getAttribute('chclient') || 'client' == $group_by_option) + if ($options['show_client'] || 'client' == $group_by_option) $left_joins .= " left join tt_clients c on (c.id = l.client_id)"; - if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice')) + if (($canViewReports || $isClient) && $options['show_invoice']) $left_joins .= " left join tt_invoices i on (i.id = l.invoice_id and i.status = 1)"; if ($canViewReports || $isClient || $user->isPluginEnabled('ex')) $left_joins .= " left join tt_users u on (u.id = l.user_id)"; - if ($bean->getAttribute('chproject') || 'project' == $group_by_option) + if ($options['show_project'] || 'project' == $group_by_option) $left_joins .= " left join tt_projects p on (p.id = l.project_id)"; - if ($bean->getAttribute('chtask') || 'task' == $group_by_option) + if ($options['show_task'] || 'task' == $group_by_option) $left_joins .= " left join tt_tasks t on (t.id = l.task_id)"; if ($include_cf_1) { if ($cf_1_type == CustomFields::TYPE_TEXT) @@ -333,13 +224,15 @@ class ttReportHelper { 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 we don't have expense items (such as when the Expenses plugin is desabled), the above is all sql we need, // with an exception of sorting part, that is added in the end. +// TODO: refactoring in progress down from here... The above is identical to getFavItems and is ready to merge. + // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table. if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled @@ -401,7 +294,7 @@ class ttReportHelper { 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"; @@ -558,7 +451,6 @@ class ttReportHelper { else array_push($fields, "if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit)) as units"); } - // Add note. if ($options['show_note']) array_push($fields, 'l.comment as note'); @@ -608,7 +500,7 @@ class ttReportHelper { 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"; @@ -674,7 +566,7 @@ class ttReportHelper { 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"; @@ -784,7 +676,7 @@ class ttReportHelper { 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') @@ -851,7 +743,7 @@ class ttReportHelper { 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"; @@ -935,7 +827,7 @@ class ttReportHelper { break; } - $where = ttReportHelper::getFavWhere($options); + $where = ttReportHelper::getWhere($options); if ($options['show_cost']) { if (MODE_TIME == $user->tracking_mode) { if ($group_by_option != 'user') @@ -1003,7 +895,7 @@ class ttReportHelper { 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"; @@ -1049,7 +941,7 @@ class ttReportHelper { $mdb2 = getConnection(); - $where = ttReportHelper::getWhere($bean, $options); + $where = ttReportHelper::getWhere($options); // Prepare parts. $time_part = "sum(time_to_sec(l.duration)) as time"; @@ -1076,7 +968,7 @@ class ttReportHelper { // 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"; @@ -1131,7 +1023,7 @@ class ttReportHelper { $mdb2 = getConnection(); - $where = ttReportHelper::getFavWhere($options); + $where = ttReportHelper::getWhere($options); // Prepare parts. $time_part = "sum(time_to_sec(l.duration)) as time"; @@ -1158,7 +1050,7 @@ class ttReportHelper { // 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"; @@ -1912,28 +1804,51 @@ class ttReportHelper { $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'); + $options['show_invoice'] = $bean->getAttribute('chinvoice'); + $options['show_paid'] = $bean->getAttribute('chpaid'); + $options['show_ip'] = $bean->getAttribute('chip'); + $options['show_project'] = $bean->getAttribute('chproject'); + $options['show_start'] = $bean->getAttribute('chstart'); + $options['show_duration'] = $bean->getAttribute('chduration'); + $options['show_cost'] = $bean->getAttribute('chcost'); + $options['show_task'] = $bean->getAttribute('chtask'); + $options['show_end'] = $bean->getAttribute('chfinish'); + $options['show_note'] = $bean->getAttribute('chnote'); + $options['show_custom_field_1'] = $bean->getAttribute('chcf_1'); + $options['show_work_units'] = $bean->getAttribute('chunits'); /* - * TODO: remaining fields to fill in... - `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 - `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 - `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 - `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; + } }