Exemplo n.º 1
0
 public function render_link()
 {
     $url = '';
     $link = nl2br($this->description);
     $siteconfig = org_openpsa_core_siteconfig::get_instance();
     $projects_url = $siteconfig->get_node_full_url('org.openpsa.projects');
     $sales_url = $siteconfig->get_node_full_url('org.openpsa.sales');
     if ($projects_url) {
         try {
             $task = org_openpsa_projects_task_dba::get_cached($this->task);
             $url = $projects_url . 'task/' . $task->guid . '/';
         } catch (midcom_error $e) {
         }
     }
     if ($url == '' && $sales_url) {
         try {
             $deliverable = org_openpsa_sales_salesproject_deliverable_dba::get_cached($this->deliverable);
             $url = $sales_url . 'deliverable/' . $deliverable->guid . '/';
         } catch (midcom_error $e) {
         }
     }
     if ($url != '') {
         $link = '<a href="' . $url . '">' . $link . '</a>';
     }
     return $link;
 }
Exemplo n.º 2
0
 public function get_row(midcom_core_dbaobject $at_entry)
 {
     $invoice = array('time' => strftime('%Y-%m-%d %H:%M:%S', $at_entry->start));
     try {
         $deliverable = org_openpsa_sales_salesproject_deliverable_dba::get_cached($at_entry->arguments['deliverable']);
         $salesproject = org_openpsa_sales_salesproject_dba::get_cached($deliverable->salesproject);
     } catch (midcom_error $e) {
         $e->log();
         return $invoice;
     }
     if ($deliverable->invoiceByActualUnits) {
         $type = midcom::get('i18n')->get_l10n('org.openpsa.expenses')->get('invoiceable reports');
         $invoice_sum = $deliverable->units * $deliverable->pricePerUnit;
     } else {
         $invoice_sum = $deliverable->price;
         $type = midcom::get('i18n')->get_l10n('org.openpsa.reports')->get('fixed price');
     }
     $invoice['sum'] = $invoice_sum;
     $invoice['deliverable'] = $deliverable->title;
     $invoice['index_deliverable'] = $deliverable->title;
     $invoice['type'] = $type;
     $invoice = $this->_render_contact_field($salesproject->customer, 'customer', $invoice, 'org_openpsa_contacts_group_dba');
     $invoice = $this->_render_contact_field($salesproject->customerContact, 'customerContact', $invoice);
     $invoice = $this->_render_contact_field($salesproject->owner, 'owner', $invoice);
     if (!empty($this->_sales_url)) {
         $invoice['deliverable'] = '<a href="' . $this->_sales_url . 'deliverable/' . $deliverable->guid . '/">' . $invoice['deliverable'] . '</a>';
     }
     return $invoice;
 }
Exemplo n.º 3
0
 private function _generate_invoice()
 {
     $invoice = new org_openpsa_invoices_invoice_dba();
     $invoice->customer = (int) $_POST['org_openpsa_invoices_invoice_customer'];
     $invoice->number = $invoice->generate_invoice_number();
     $invoice->owner = midcom_connection::get_user();
     $invoice->vat = $invoice->get_default('vat');
     $invoice->description = $invoice->get_default('remarks');
     if (!$invoice->create()) {
         midcom::get('uimessages')->add($this->_l10n->get('org.openpsa.invoices'), $this->_l10n->get('failed to create invoice, reason ') . midcom_connection::get_error_string(), 'error');
         return false;
     }
     // create invoice_items
     foreach ($_POST['org_openpsa_invoices_invoice_tasks'] as $task_id => $invoiceable) {
         if (!$invoiceable) {
             continue;
         }
         $task = $this->_tasks[$task_id];
         //instance the invoice_items
         $item = new org_openpsa_invoices_invoice_item_dba();
         $item->task = $task_id;
         try {
             $deliverable = org_openpsa_sales_salesproject_deliverable_dba::get_cached($task->agreement);
             $item->deliverable = $deliverable->id;
         } catch (midcom_error $e) {
             $e->log();
         }
         $item->invoice = $invoice->id;
         $item->description = $task->title;
         $item->pricePerUnit = (double) $_POST['org_openpsa_invoices_invoice_tasks_price'][$task_id];
         $item->units = (double) $_POST['org_openpsa_invoices_invoice_tasks_units'][$task_id];
         $item->create();
         // Connect invoice to the tasks involved
         org_openpsa_projects_workflow::mark_invoiced($task, $invoice);
     }
     // Generate "Send invoice" task
     $invoice_sender_guid = $this->_config->get('invoice_sender');
     if (!empty($invoice_sender_guid)) {
         $invoice->generate_invoicing_task($invoice_sender_guid);
     }
     midcom::get('uimessages')->add($this->_l10n->get('org.openpsa.invoices'), sprintf($this->_l10n->get('invoice %s created'), $invoice->get_label()), 'ok');
     midcom::get()->relocate("invoice/edit/{$invoice->guid}/");
     // This will exit
 }
Exemplo n.º 4
0
 private function _prepare_grid_data()
 {
     $this->_request_data['grid'] = new org_openpsa_widgets_grid('invoice_items', 'local');
     $entries = array();
     $invoice_sum = 0;
     foreach ($this->_request_data['invoice_items'] as $item) {
         $entry = array();
         $entry['id'] = $item->id;
         try {
             $deliverable = org_openpsa_sales_salesproject_deliverable_dba::get_cached($item->deliverable);
             $entry['deliverable'] = $deliverable->title;
         } catch (midcom_error $e) {
             $entry['deliverable'] = '';
         }
         try {
             $task = org_openpsa_projects_task_dba::get_cached($item->task);
             $entry['task'] = $task->title;
         } catch (midcom_error $e) {
             $entry['task'] = '';
         }
         $entry['description'] = $item->description;
         $entry['price'] = $item->pricePerUnit;
         $entry['quantity'] = $item->units;
         $entry['position'] = $item->position;
         $item_sum = $item->units * $item->pricePerUnit;
         $invoice_sum += $item_sum;
         $entry['sum'] = $item_sum;
         $entry['action'] = '';
         $entries[] = $entry;
     }
     $this->_request_data['entries'] = $entries;
     $this->_request_data['grid']->set_footer_data(array('sum' => $invoice_sum));
 }
Exemplo n.º 5
0
 /**
  *
  * @param mixed $handler_id The ID of the handler.
  * @param array &$data The local request data.
  */
 public function _show_generator($handler_id, array &$data)
 {
     midcom_show_style('sales_report-deliverable-start');
     // Quick workaround to Bergies lazy determination of whether this is user's or everyone's report...
     if ($this->_request_data['query_data']['resource'] == 'user:'******'auth')->user->guid) {
         // My report
         $data['handler_id'] = 'deliverable_report';
     } else {
         // Generic report
         $data['handler_id'] = 'sales_report';
     }
     /*** Copied from sales/handler/deliverable/report.php ***/
     midcom_show_style('sales_report-deliverable-header');
     $invoices_node = midcom_helper_misc::find_node_by_component('org.openpsa.invoices');
     $sums_per_person = array();
     $sums_all = array('price' => 0, 'cost' => 0, 'profit' => 0);
     $odd = true;
     foreach ($data['invoices'] as $deliverable_guid => $invoices) {
         if (count($invoices) == 0) {
             // No invoices sent in this project, skip
             continue;
         }
         try {
             $deliverable = org_openpsa_sales_salesproject_deliverable_dba::get_cached($deliverable_guid);
             $product = org_openpsa_products_product_dba::get_cached($deliverable->product);
             $salesproject = org_openpsa_sales_salesproject_dba::get_cached($deliverable->salesproject);
             $customer = midcom_db_group::get_cached($salesproject->customer);
         } catch (midcom_error $e) {
             continue;
         }
         if (!array_key_exists($salesproject->owner, $sums_per_person)) {
             $sums_per_person[$salesproject->owner] = array('price' => 0, 'cost' => 0, 'profit' => 0);
         }
         // Calculate the price and cost from invoices
         $invoice_price = 0;
         $data['invoice_string'] = '';
         $invoice_cycle_numbers = array();
         foreach ($invoices as $invoice) {
             $invoice_price += $invoice->sum;
             $invoice_class = $invoice->get_status();
             if ($invoices_node) {
                 $invoice_label = "<a class=\"{$invoice_class}\" href=\"{$invoices_node[MIDCOM_NAV_FULLURL]}invoice/{$invoice->guid}/\">" . $invoice->get_label() . "</a>";
             } else {
                 $invoice_label = $invoice->get_label();
             }
             if ($product->delivery == org_openpsa_products_product_dba::DELIVERY_SUBSCRIPTION) {
                 $invoice_cycle_numbers[] = (int) $invoice->parameter('org.openpsa.sales', 'cycle_number');
             }
             $data['invoice_string'] .= "<li class=\"{$invoice_class}\">{$invoice_label}</li>\n";
         }
         if ($product->delivery == org_openpsa_products_product_dba::DELIVERY_SUBSCRIPTION) {
             // This is a subscription, it should be shown only if it is the first invoice
             if (!in_array(1, $invoice_cycle_numbers)) {
                 continue;
                 // This will skip to next deliverable
             }
             $scheduler = new org_openpsa_invoices_scheduler($deliverable);
             if ($deliverable->end == 0) {
                 // Subscription doesn't have an end date, use specified amount of months for calculation
                 $cycles = $scheduler->calculate_cycles($this->_config->get('subscription_profit_months'));
                 $data['calculation_basis'] = sprintf($data['l10n']->get('%s cycles in %s months'), $cycles, $this->_config->get('subscription_profit_months'));
             } else {
                 $cycles = $scheduler->calculate_cycles();
                 $data['calculation_basis'] = sprintf($data['l10n']->get('%s cycles, %s - %s'), $cycles, strftime('%x', $deliverable->start), strftime('%x', $deliverable->end));
             }
             $price = $deliverable->price * $cycles;
             $cost = $deliverable->cost * $cycles;
         } else {
             // This is a single delivery, calculate cost as percentage as it may be invoiced in pieces
             if ($deliverable->price) {
                 $cost_percentage = 100 / $deliverable->price * $invoice_price;
                 $cost = $deliverable->cost / 100 * $cost_percentage;
             } else {
                 $cost_percentage = 100;
                 $cost = $deliverable->cost;
             }
             $price = $invoice_price;
             $data['calculation_basis'] = sprintf($data['l10n']->get('%s%% of %s'), round($cost_percentage), $deliverable->price);
         }
         // And now just count the profit
         $profit = $price - $cost;
         $data['customer'] = $customer;
         $data['salesproject'] = $salesproject;
         $data['deliverable'] = $deliverable;
         $data['price'] = $price;
         $sums_per_person[$salesproject->owner]['price'] += $price;
         $sums_all['price'] += $price;
         $data['cost'] = $cost;
         $sums_per_person[$salesproject->owner]['cost'] += $cost;
         $sums_all['cost'] += $cost;
         $data['profit'] = $profit;
         $sums_per_person[$salesproject->owner]['profit'] += $profit;
         $sums_all['profit'] += $profit;
         if ($odd) {
             $data['row_class'] = '';
             $odd = false;
         } else {
             $data['row_class'] = ' class="even"';
             $odd = true;
         }
         midcom_show_style('sales_report-deliverable-item');
     }
     $data['sums_per_person'] = $sums_per_person;
     $data['sums_all'] = $sums_all;
     midcom_show_style('sales_report-deliverable-footer');
     /*** /Copied from sales/handler/deliverable/report.php ***/
     midcom_show_style('sales_report-deliverable-end');
 }
Exemplo n.º 6
0
 /**
  * Connect the task hour reports to an invoice
  *
  * @param org_openpsa_projects_task_dba &$task The task we're working on
  * @param org_openpsa_invoices_invoice_dba &$invoice The invoice we're working on
  */
 static function mark_invoiced(&$task, &$invoice)
 {
     debug_add("task->mark_invoiced() called with user #" . midcom_connection::get_user());
     try {
         $deliverable = org_openpsa_sales_salesproject_deliverable_dba::get_cached($task->agreement);
     } catch (midcom_error $e) {
         $e->log();
     }
     // Mark the hour reports invoiced
     $hours_marked = 0;
     $qb = org_openpsa_projects_hour_report_dba::new_query_builder();
     $qb->add_constraint('task', '=', $task->id);
     $qb->add_constraint('invoice', '=', 0);
     $qb->add_constraint('invoiceable', '=', true);
     // Check how the agreement deals with hour reports
     if ($deliverable && $deliverable->invoiceApprovedOnly) {
         // The agreement allows invoicing only approved hours, therefore don't mark unapproved
         $qb->add_constraint('metadata.isapproved', '=', true);
     }
     $reports = $qb->execute();
     foreach ($reports as $report) {
         $report->invoice = $invoice->id;
         $report->_skip_parent_refresh = true;
         if ($report->update()) {
             $hours_marked += $report->hours;
         }
     }
     // Update hour caches to agreement
     if (!$task->update_cache()) {
         debug_add('Failed to update task hour caches, last Midgard error: ' . midcom_connection::get_error_string(), MIDCOM_LOG_WARN);
     }
     // Notify user
     midcom::get('uimessages')->add(midcom::get('i18n')->get_string('org.openpsa.projects', 'org.openpsa.projects'), sprintf(midcom::get('i18n')->get_string('marked %s hours as invoiced in task "%s"', 'org.openpsa.projects'), $hours_marked, $task->title), 'ok');
     return $hours_marked;
 }
Exemplo n.º 7
0
 private function _prepare_save()
 {
     //Make sure we have start
     if (!$this->start) {
         $this->start = time();
     }
     //Make sure we have end
     if (!$this->end || $this->end == -1) {
         $this->end = time();
     }
     //Reset start and end to start/end of day
     $this->start = mktime(0, 0, 0, date('n', $this->start), date('j', $this->start), date('Y', $this->start));
     $this->end = mktime(23, 59, 59, date('n', $this->end), date('j', $this->end), date('Y', $this->end));
     if ($this->start > $this->end) {
         debug_add("start ({$this->start}) is greater than end ({$this->end}), aborting", MIDCOM_LOG_ERROR);
         return false;
     }
     if ($this->orgOpenpsaWgtype == ORG_OPENPSA_OBTYPE_TASK) {
         $this->orgOpenpsaWgtype = ORG_OPENPSA_WGTYPE_NONE;
     }
     if ($this->agreement) {
         // Get customer company into cache from agreement's sales project
         try {
             $agreement = org_openpsa_sales_salesproject_deliverable_dba::get_cached($this->agreement);
             $this->hoursInvoiceableDefault = true;
             if (!$this->customer) {
                 $salesproject = org_openpsa_sales_salesproject_dba::get_cached($agreement->salesproject);
                 $this->customer = $salesproject->customer;
             }
         } catch (midcom_error $e) {
         }
     } else {
         // No agreement, we can't be invoiceable
         $this->hoursInvoiceableDefault = false;
     }
     // Update hour caches
     $this->update_cache(false);
     return true;
 }
Exemplo n.º 8
0
 /**
  * List all tasks, optionally filtered by status
  */
 private function _handler_list_all($args)
 {
     // Default to open tasks list if none specified
     if (!isset($args[1]) || empty($args[1])) {
         $this->_request_data['view_identifier'] = 'open';
         $args[1] = 'open';
     }
     switch ($args[1]) {
         case 'agreement':
             if (!$args[2]) {
                 throw new midcom_error('Invalid arguments for agreement filter');
             }
             $agreement_id = (int) $args[2];
             $this->_request_data['agreement'] = $agreement_id;
             $deliverable = org_openpsa_sales_salesproject_deliverable_dba::get_cached($agreement_id);
             $title = sprintf($this->_l10n->get('tasks for agreement %s'), $deliverable->title);
             midcom::get('head')->set_pagetitle($title);
             $this->add_breadcrumb("", $title);
             $this->_qb->add_constraint('agreement', '=', $deliverable->id);
             $this->_provider->add_order('end', 'DESC');
             break;
         case 'all':
         case 'both':
             $args[1] = 'all';
             $this->_provider->add_order('end', 'DESC');
             break;
         case 'open':
             $this->set_active_leaf($this->_topic->id . ':tasks_open');
             $this->_qb->add_constraint('status', '<', org_openpsa_projects_task_status_dba::CLOSED);
             $this->_provider->add_order('end');
             break;
         case 'closed':
             $this->set_active_leaf($this->_topic->id . ':tasks_closed');
             $this->_qb->add_constraint('status', '=', org_openpsa_projects_task_status_dba::CLOSED);
             $this->_provider->add_order('end', 'DESC');
             break;
         case 'current':
             $this->_qb->add_constraint('status', 'IN', array(org_openpsa_projects_task_status_dba::ACCEPTED, org_openpsa_projects_task_status_dba::STARTED, org_openpsa_projects_task_status_dba::REJECTED, org_openpsa_projects_task_status_dba::REOPENED));
             $this->_provider->add_order('end');
             break;
         case 'invoiceable':
             $this->set_active_leaf($this->_topic->id . ':tasks_invoiceable');
             $this->_qb->add_constraint('invoiceableHours', '>', 0);
             $this->_provider->add_order('end');
             break;
         case 'invoiced':
             $this->set_active_leaf($this->_topic->id . ':tasks_invoiced');
             $this->_qb->add_constraint('invoicedHours', '>', 0);
             $this->_provider->add_order('end', 'DESC');
             break;
         default:
             throw new midcom_error("Filter {$args[1]} not recognized");
     }
     $qf = new org_openpsa_core_queryfilter('org_openpsa_task_list_' . $args[1]);
     $qf->add_filter(new org_openpsa_core_filter('priority', '<=', $this->_request_data['priority_array']));
     $date_filter = new org_openpsa_core_filter('timeframe');
     $date_filter->set('mode', 'timeframe');
     $date_filter->set('helptext', $this->_l10n->get("timeframe"));
     $date_filter->set('fieldname', array('start' => 'start', 'end' => 'end'));
     $qf->add_filter($date_filter);
     $qf->apply_filters($this->_qb);
     $this->_request_data["qf"] = $qf;
     $this->_request_data['table-heading'] = $args[1] . ' tasks';
     $this->_request_data['view'] = 'grid';
 }
Exemplo n.º 9
0
 private function _get_scheduled_invoices()
 {
     $invoices = array();
     $at_qb = midcom_services_at_entry_dba::new_query_builder();
     $at_qb->add_constraint('method', '=', 'new_subscription_cycle');
     $at_qb->add_constraint('component', '=', 'org.openpsa.sales');
     $at_entries = $at_qb->execute();
     foreach ($at_entries as $at_entry) {
         try {
             $deliverable = org_openpsa_sales_salesproject_deliverable_dba::get_cached($at_entry->arguments['deliverable']);
             if ($deliverable->continuous || $deliverable->start < $this->_request_data['end'] && $deliverable->end > $this->_request_data['start']) {
                 $invoices = array_merge($invoices, $this->_get_invoices_for_subscription($deliverable, $at_entry));
             }
         } catch (midcom_error $e) {
         }
     }
     $invoices = array_filter($invoices, array($this, '_filter_by_date'));
     usort($invoices, array($this, '_sort_by_date'));
     return $invoices;
 }
Exemplo n.º 10
0
<?php

$view_task =& $data['object_view'];
$task =& $data['object'];
$task->get_members();
$prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
$siteconfig = org_openpsa_core_siteconfig::get_instance();
$sales_url = $siteconfig->get_node_full_url('org.openpsa.sales');
$expenses_url = $siteconfig->get_node_relative_url('org.openpsa.expenses');
?>
<div class="org_openpsa_projects_task">
    <div class="sidebar">
        <?php 
if ($task->agreement) {
    try {
        $agreement = org_openpsa_sales_salesproject_deliverable_dba::get_cached($task->agreement);
        $output = $agreement->deliverable_html;
        if ($sales_url) {
            $salesproject = org_openpsa_sales_salesproject_dba::get_cached($agreement->salesproject);
            $output = "<a href=\"{$sales_url}salesproject/{$salesproject->guid}/#{$agreement->guid}/\">{$output}</a>\n";
        }
        echo "<h2>" . $data['l10n']->get('agreement') . "</h2>\n";
        echo $output;
    } catch (midcom_error $e) {
    }
}
if ($task->manager) {
    echo "<h2>" . $data['l10n']->get('manager') . "</h2>\n";
    $contact = org_openpsa_widgets_contact::get($task->manager);
    echo $contact->show_inline();
}