public function test_generate_invoice() { midcom::get('auth')->request_sudo('org.openpsa.invoices'); $customer = $this->create_object('org_openpsa_contacts_group_dba'); $salesproject = $this->create_object('org_openpsa_sales_salesproject_dba'); $deliverable_attributes = array('salesproject' => $salesproject->id, 'price' => 100, 'state' => org_openpsa_sales_salesproject_deliverable_dba::STATUS_DELIVERED, 'invoiceByActualUnits' => false); $deliverable = $this->create_object('org_openpsa_sales_salesproject_deliverable_dba', $deliverable_attributes); $task_attributes = array('project' => $salesproject->id, 'agreement' => $deliverable->id, 'status' => org_openpsa_projects_task_status_dba::COMPLETED, 'invoiceableHours' => true); $task = $this->create_object('org_openpsa_projects_task_dba', $task_attributes); $_POST = array('org_openpsa_invoices_invoice' => true, 'org_openpsa_invoices_invoice_tasks' => array($task->id => true), 'org_openpsa_invoices_invoice_tasks_price' => array($task->id => 10), 'org_openpsa_invoices_invoice_tasks_units' => array($task->id => 10), 'org_openpsa_invoices_invoice_customer' => $customer->id); $url = $this->run_relocate_handler('org.openpsa.invoices', array('projects')); $qb = org_openpsa_invoices_invoice_item_dba::new_query_builder(); $qb->add_constraint('task', '=', $task->id); $results = $qb->execute(); $this->assertEquals(1, sizeof($results)); $item = $results[0]; $invoice = new org_openpsa_invoices_invoice_dba($item->invoice); $task->refresh(); $this->assertEquals('invoice/edit/' . $invoice->guid . '/', $url); $this->assertEquals(100, $invoice->sum); $this->assertEquals($deliverable->id, $item->deliverable); $deliverable->refresh(); $this->assertEquals(org_openpsa_sales_salesproject_deliverable_dba::STATUS_INVOICED, $deliverable->state); midcom::get('auth')->drop_sudo(); }
if (!$found) { echo "Could not identify invoice item for deliverable " . $deliverable->title . " on invoice " . $invoice->get_label() . ", creating empty item\n"; flush(); $item = new org_openpsa_invoices_invoice_item_dba(); $item->invoice = $invoice->id; $item->deliverable = $deliverable->id; $item->pricePerUnit = 0; $item->units = 1; $item->description = $deliverable->title . ' (auto-generated)'; $item->create(); } } $relatedto->delete(); } } $qb_items = org_openpsa_invoices_invoice_item_dba::new_query_builder(); $qb_items->add_constraint('task', '>', 0); $qb_items->add_constraint('deliverable', '=', 0); $items = $qb_items->execute(); foreach ($items as $item) { try { $task = new org_openpsa_projects_task_dba($item->task); } catch (midcom_error $e) { echo 'Failed to load task #' . $item->task . ': ' . $e->getMessage() . ", skipping\n"; flush(); continue; } try { $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($task->agreement); } catch (midcom_error $e) { echo 'Failed to load deliverable #' . $task->agreement . ': ' . $e->getMessage() . ", skipping\n";
/** * Helper function to get invoice_item for the passed task id, if there is no item * it will return a new created one */ private function _probe_invoice_item_for_task($task_id) { //check if there is already an invoice_item for this task $qb_invoice_item = org_openpsa_invoices_invoice_item_dba::new_query_builder(); $qb_invoice_item->add_constraint('invoice', '=', $this->id); $qb_invoice_item->add_constraint('task', '=', $task_id); $invoice_items = $qb_invoice_item->execute(); if (count($invoice_items) == 1) { $invoice_item = $invoice_items[0]; } else { if (count($invoice_items) > 1) { debug_add('More than one item found for task #' . $task_id . ', only returning the first', MIDCOM_LOG_INFO); $invoice_item = $invoice_items[0]; } else { $invoice_item = new org_openpsa_invoices_invoice_item_dba(); $invoice_item->task = $task_id; $invoice_item->invoice = $this->id; $invoice_item->create(); } } return $invoice_item; }
private function _count_invoice_items($invoice_id) { $qb = org_openpsa_invoices_invoice_item_dba::new_query_builder(); $qb->add_constraint('invoice', '=', $invoice_id); $items = $qb->execute(); $this->register_objects($items); return count($items); }
/** * @depends testRun_cycle */ public function testRun_cycle_multiple() { midcom::get('auth')->request_sudo('org.openpsa.invoices'); $deliverable_attributes = array('salesproject' => $this->_salesproject->id, 'product' => $this->_product->id, 'description' => 'TEST DESCRIPTION 2', 'pricePerUnit' => 10, 'plannedUnits' => 15, 'units' => 10, 'invoiceByActualUnits' => true, 'state' => org_openpsa_sales_salesproject_deliverable_dba::STATUS_STARTED, 'start' => strtotime('2010-02-02 00:00:00')); $deliverable2 = $this->create_object('org_openpsa_sales_salesproject_deliverable_dba', $deliverable_attributes); $task_attributes = array('project' => $this->_project->id, 'agreement' => $deliverable2->id, 'title' => 'TEST TITLE 2', 'reportedHours' => 10); $task2 = $this->create_object('org_openpsa_projects_task_dba', $task_attributes); $this->_product->delivery = org_openpsa_products_product_dba::DELIVERY_SUBSCRIPTION; $this->_product->update(); $this->_deliverable->start = strtotime('2010-02-02 00:00:00'); $this->_deliverable->continuous = true; $this->_deliverable->invoiceByActualUnits = false; $this->_deliverable->pricePerUnit = 10; $this->_deliverable->plannedUnits = 10; $this->_deliverable->state = org_openpsa_sales_salesproject_deliverable_dba::STATUS_STARTED; $this->_deliverable->update(); $scheduler = new org_openpsa_invoices_scheduler($this->_deliverable); $stat = $scheduler->run_cycle(1, true); $this->assertTrue($stat); $scheduler = new org_openpsa_invoices_scheduler($deliverable2); $stat = $scheduler->run_cycle(1, true); $this->assertTrue($stat); $qb = org_openpsa_invoices_invoice_item_dba::new_query_builder(); $qb->add_constraint('deliverable', '=', $this->_deliverable->id); $results = $qb->execute(); $this->assertEquals(1, sizeof($results)); $item1 = $results[0]; $this->register_object($item1); $qb = org_openpsa_invoices_invoice_item_dba::new_query_builder(); $qb->add_constraint('deliverable', '=', $deliverable2->id); $results = $qb->execute(); $this->assertEquals(1, sizeof($results)); $item2 = $results[0]; $this->register_object($item2); $this->assertEquals($item1->invoice, $item2->invoice); $this->assertEquals($this->_deliverable->id, $item1->deliverable); $this->assertEquals($deliverable2->id, $item2->deliverable); $invoice = new org_openpsa_invoices_invoice_dba($item2->invoice); $this->register_object($invoice); $this->assertEquals(200, $invoice->sum); $this->assertEquals(100, $deliverable2->invoiced); midcom::get('auth')->drop_sudo(); }