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 }
public function test_mark_invoiced() { $group = $this->create_object('org_openpsa_products_product_group_dba'); $product_attributes = array('productGroup' => $group->id, 'code' => 'TEST-' . __CLASS__ . time(), 'delivery' => org_openpsa_products_product_dba::DELIVERY_SUBSCRIPTION); $product = $this->create_object('org_openpsa_products_product_dba', $product_attributes); $salesproject = $this->create_object('org_openpsa_sales_salesproject_dba'); $deliverable_attributes = array('salesproject' => $salesproject->id, 'product' => $product->id, 'description' => 'TEST DESCRIPTION', 'plannedUnits' => 15); $deliverable = $this->create_object('org_openpsa_sales_salesproject_deliverable_dba', $deliverable_attributes); self::$_task->agreement = $deliverable->id; self::$_task->update(); $report_attributes = array('task' => self::$_task->id, 'invoiceable' => true, 'hours' => 15); $report = $this->create_object('org_openpsa_projects_hour_report_dba', $report_attributes); $invoice = $this->create_object('org_openpsa_invoices_invoice_dba'); $result = org_openpsa_projects_workflow::mark_invoiced(self::$_task, $invoice); $this->assertEquals(15, $result); $report->refresh(); $this->assertEquals($invoice->id, $report->invoice); }
/** * Returns the invoice items that should be written * * @return array */ public function get_invoice_items(org_openpsa_invoices_invoice_dba $invoice) { $this->_invoice = $invoice; $items = array(); // Mark the tasks (and hour reports) related to this agreement as invoiced $tasks = $this->_find_tasks(); foreach ($tasks as $task) { $hours_marked = org_openpsa_projects_workflow::mark_invoiced($task, $invoice); $items[] = $this->_generate_invoice_item($task->title, $hours_marked, $task); } if (sizeof($tasks) == 0) { $items[] = $this->_generate_invoice_item($this->_deliverable->title, $this->_deliverable->units); } return $items; }