/** * Helper function that sets the request data for hour reports * * @param &$array The array returned by collector */ private function _add_hour_data(&$array) { static $customer_cache = array(); if (!isset($customer_cache[$array['task']])) { $customer = 0; $customer_label = $this->_l10n->get('no customer'); if ($array['task'] != 0) { $mc = new midgard_collector('org_openpsa_task', 'id', $array['task']); $mc->set_key_property('id'); $mc->add_value_property('customer'); $mc->execute(); $customer_id = $mc->get_subkey($array['task'], 'customer'); if ($customer_id) { try { $customer = new org_openpsa_contacts_group_dba($customer_id); $customer_label = $customer->official; $customer = $customer_id; } catch (midcom_error $e) { } } } $customer_cache[$array['task']] = $customer; if (!isset($this->_request_data['customers'][$customer])) { $this->_request_data['customers'][$customer] = $customer_label; } } $customer = $customer_cache[$array['task']]; $category = 'uninvoiceable'; if ($array['invoiceable']) { $category = 'invoiceable'; } if (!isset($this->_request_data['hours'][$category][$customer])) { $this->_request_data['hours'][$category][$customer] = $array['hours']; } else { $this->_request_data['hours'][$category][$customer] += $array['hours']; } $this->_request_data['hours']['total_' . $category] += $array['hours']; }
/** * Adds a passed rule for the passed class to the querybuilder * * @param array $rule contains the rule * @param string $class name of the class the rule will be added to * @param string $person_property contains the name of the property of the * passed class which links to the person */ function add_misc_rule(array $rule, $class, $person_property) { $persons = array(0 => -1); $match = $rule['match']; $constraint_match = "IN"; if ($rule['match'] == '<>' || $rule['match'] == 'NOT LIKE') { $constraint_match = "NOT IN"; switch ($rule['match']) { case '<>': $match = '='; break; case 'NOT LIKE': $match = 'LIKE'; break; default: $match = '='; break; } } $mc_misc = new midgard_collector($class, 'metadata.deleted', false); $mc_misc->set_key_property('id'); $mc_misc->add_constraint($rule['property'], $match, $rule['value']); $mc_misc->add_value_property($person_property); $mc_misc->execute(); $keys = $mc_misc->list_keys(); foreach ($keys as $key => $value) { // get user-id $persons[] = $mc_misc->get_subkey($key, $person_property); } $this->_result_mc->add_constraint('id', $constraint_match, $persons); }
/** * Helper function to create & recalculate existing invoice_items by tasks * * @param array $tasks array containing the task id's to recalculate for - if empty all tasks will be recalculated */ public function _recalculate_invoice_items($tasks = array(), $skip_invoice_update = false) { $result_items = array(); $result_tasks = array(); //get hour_reports for this invoice - mc ? $qb_hour_reports = org_openpsa_projects_hour_report_dba::new_query_builder(); $qb_hour_reports->add_constraint('invoice', '=', $this->id); if (!empty($tasks)) { $qb_hour_reports->add_constraint('task', 'IN', $tasks); //if there is a task passed it must be calculated even //if it doesn't have associated hour_reports foreach ($tasks as $task_id) { $result_tasks[$task_id] = 0; } } $hour_reports = $qb_hour_reports->execute(); // sums up the hours of hour_reports for each task foreach ($hour_reports as $hour_report) { if (!array_key_exists($hour_report->task, $result_tasks)) { $result_tasks[$hour_report->task] = 0; } //only add invoiceable hour_reports if ($hour_report->invoiceable) { $result_tasks[$hour_report->task] += $hour_report->hours; } } foreach ($result_tasks as $task_id => $hours) { $invoice_item = $this->_probe_invoice_item_for_task($task_id); //get deliverable for this task $mc_task_agreement = new midgard_collector('org_openpsa_task', 'id', $task_id); $mc_task_agreement->set_key_property('id'); $mc_task_agreement->add_value_property('title'); $mc_task_agreement->add_value_property('agreement'); $mc_task_agreement->add_constraint('agreement', '<>', 0); $mc_task_agreement->execute(); $mc_task_key = $mc_task_agreement->list_keys(); $deliverable = null; foreach ($mc_task_key as $key => $empty) { try { $deliverable = new org_openpsa_sales_salesproject_deliverable_dba((int) $mc_task_agreement->get_subkey($key, 'agreement')); $invoice_item->pricePerUnit = $deliverable->pricePerUnit; $invoice_item->deliverable = $deliverable->id; //calculate price if ($deliverable->invoiceByActualUnits || $deliverable->plannedUnits == 0) { $invoice_item->units = $hours; } else { $invoice_item->units = $deliverable->plannedUnits; } } catch (midcom_error $e) { $e->log(); $invoice_item->units = $hours; } } if ($invoice_item->description == '') { $invoice_item->description = $mc_task_agreement->get_subkey($task_id, 'title'); } $invoice_item->skip_invoice_update = $skip_invoice_update; $invoice_item->update(); $result_items[] = $invoice_item; } return $result_items; }