/** * Manually trigger a subscription cycle run. */ private function _run_cycle() { if (empty($_POST['at_entry'])) { throw new midcom_error('No AT entry specified'); } $entry = new midcom_services_at_entry_dba($_POST['at_entry']); $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($entry->arguments['deliverable']); $scheduler = new org_openpsa_invoices_scheduler($deliverable); if (!$scheduler->run_cycle($entry->arguments['cycle'])) { throw new midcom_error('Failed to run cycle, see debug log for details'); } if (!$entry->delete()) { throw new midcom_error('Could not delete AT entry: ' . midcom_connection::get_error_string()); } }
/** * @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(); }
function order() { if ($this->state >= org_openpsa_sales_salesproject_deliverable_dba::STATUS_ORDERED) { return false; } // Cache the original cost values intended and reset the fields $this->plannedUnits = $this->units; $this->plannedCost = $this->cost; if ($this->invoiceByActualUnits) { $this->cost = 0; $this->units = 0; } // Check what kind of order this is $product = org_openpsa_products_product_dba::get_cached($this->product); $scheduler = new org_openpsa_invoices_scheduler($this); if ($product->delivery == org_openpsa_products_product_dba::DELIVERY_SUBSCRIPTION) { // This is a new subscription, initiate the cycle but don't send invoice if (!$scheduler->run_cycle(1, false)) { return false; } } else { // Check if we need to create task or ship goods switch ($product->orgOpenpsaObtype) { case org_openpsa_products_product_dba::TYPE_SERVICE: $scheduler->create_task($this->start, $this->end, $this->title); break; case org_openpsa_products_product_dba::TYPE_GOODS: // TODO: Warehouse management: create new order // TODO: Warehouse management: create new order default: break; } } $this->state = org_openpsa_sales_salesproject_deliverable_dba::STATUS_ORDERED; if ($this->update()) { // Update sales project and mark as won $salesproject = new org_openpsa_sales_salesproject_dba($this->salesproject); if ($salesproject->status != org_openpsa_sales_salesproject_dba::STATUS_WON) { $salesproject->status = org_openpsa_sales_salesproject_dba::STATUS_WON; $salesproject->update(); } return true; } return false; }
/** * AT handler for handling subscription cycles. * * @param array $args handler arguments * @param object &$handler reference to the cron_handler object calling this method. * @return boolean indicating success/failure */ function new_subscription_cycle($args, &$handler) { if (!isset($args['deliverable']) || !isset($args['cycle'])) { $msg = 'deliverable GUID or cycle number not set, aborting'; $handler->print_error($msg); debug_add($msg, MIDCOM_LOG_ERROR); return false; } try { $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args['deliverable']); } catch (midcom_error $e) { $msg = "Deliverable {$args['deliverable']} not found, error " . midcom_connection::get_error_string(); $handler->print_error($msg); debug_add($msg, MIDCOM_LOG_ERROR); return false; } $scheduler = new org_openpsa_invoices_scheduler($deliverable); return $scheduler->run_cycle($args['cycle']); }