Example #1
0
 public function testCRUD()
 {
     midcom::get('auth')->request_sudo('org.openpsa.invoices');
     $data = new org_openpsa_invoices_billing_data_dba();
     $data->linkGuid = self::$_contact->guid;
     $data->useContactAddress = true;
     $stat = $data->create();
     $this->assertTrue($stat, midcom_connection::get_error_string());
     $this->register_object($data);
     $parent = $data->get_parent();
     $this->assertEquals($parent->guid, self::$_contact->guid);
     self::$_contact->refresh();
     $this->assertEquals(self::$_contact->street, $data->street);
     $data->vat = 12;
     $data->due = 12;
     $stat = $data->update();
     $this->assertTrue($stat);
     self::$_contact->refresh();
     $invoice = new org_openpsa_invoices_invoice_dba();
     $invoice->customerContact = self::$_contact->id;
     $invoice_data = $invoice->get_billing_data();
     $this->assertEquals($data->guid, $invoice_data->guid);
     $this->assertEquals($data->vat, $invoice->get_default('vat'));
     $this->assertEquals($data->due, $invoice->get_default('due'));
     $stat = $data->delete();
     $this->assertTrue($stat);
     midcom::get('auth')->drop_sudo();
 }
Example #2
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
 }
Example #3
0
 public function load_schemadb()
 {
     $schemadb = midcom_helper_datamanager2_schema::load_database($this->_config->get('schemadb_billing_data'));
     $fields =& $schemadb[$this->get_schema_name()]->fields;
     // Fill VAT select
     $vat_array = explode(',', $this->_config->get('vat_percentages'));
     if (is_array($vat_array) && count($vat_array) > 0) {
         $vat_values = array();
         foreach ($vat_array as $vat) {
             $vat_values[$vat] = "{$vat}%";
         }
         $fields['vat']['type_config']['options'] = $vat_values;
     }
     $dummy_invoice = new org_openpsa_invoices_invoice_dba();
     //set the defaults for vat & due to the schema
     $fields['due']['default'] = $dummy_invoice->get_default('due');
     $fields['vat']['default'] = $dummy_invoice->get_default('vat');
     unset($dummy_invoice);
     return $schemadb;
 }
Example #4
0
 private function _get_invoices_for_subscription($deliverable, $at_entry)
 {
     if ($deliverable->invoiceByActualUnits && $at_entry->arguments['cycle'] > 1) {
         $invoice_sum = $deliverable->invoiced / ($at_entry->arguments['cycle'] - 1);
         if ($invoice_sum == 0) {
             return array();
         }
         $calculation_base = sprintf($this->_l10n->get('average of %s runs'), $at_entry->arguments['cycle'] - 1);
     } else {
         $invoice_sum = $deliverable->price;
         $calculation_base = $this->_l10n->get('fixed price');
     }
     $salesproject = org_openpsa_sales_salesproject_dba::get_cached($deliverable->salesproject);
     $scheduler = new org_openpsa_invoices_scheduler($deliverable);
     $invoices = array();
     $time = $at_entry->start;
     while ($time < $this->_request_data['end'] && ($time < $deliverable->end || $deliverable->continuous)) {
         $invoice = new org_openpsa_invoices_invoice_dba();
         $invoice->customer = $salesproject->customer;
         $invoice->customerContact = $salesproject->customerContact;
         $invoice->owner = $salesproject->owner;
         $invoice->sum = $invoice_sum;
         $invoice->sent = $time;
         $invoice->due = $invoice->get_default('due') * 3600 * 24 + $time;
         $invoice->vat = $invoice->get_default('vat');
         $invoice->description = $deliverable->title . ' (' . $calculation_base . ')';
         if ($this->_sales_url) {
             $invoice->description = '<a href="' . $this->_sales_url . 'deliverable/' . $deliverable->guid . '/">' . $invoice->description . '</a>';
         }
         $invoice->paid = $invoice->due;
         $invoices[] = $invoice;
         if (!($time = $scheduler->calculate_cycle_next($time))) {
             debug_add('Failed to calculate timestamp for next cycle, exiting', MIDCOM_LOG_WARN);
             break;
         }
     }
     return $invoices;
 }
Example #5
0
 private function _create_invoice($cycle_number = null)
 {
     $salesproject = new org_openpsa_sales_salesproject_dba($this->_deliverable->salesproject);
     $invoice = new org_openpsa_invoices_invoice_dba();
     $invoice->customer = $salesproject->customer;
     $invoice->customerContact = $salesproject->customerContact;
     $invoice->number = $invoice->generate_invoice_number();
     $invoice->owner = $salesproject->owner;
     $invoice->vat = $invoice->get_default('vat');
     $invoice->description = $invoice->get_default('remarks');
     if ($invoice->create()) {
         // Register the cycle number for reporting purposes
         if (!is_null($cycle_number)) {
             $invoice->parameter('org.openpsa.sales', 'cycle_number', $cycle_number);
         }
         return $invoice;
     } else {
         throw new midcom_error('Failed to create invoice, ' . midcom_connection::get_error_string());
     }
 }
Example #6
0
 function _load_defaults()
 {
     $this->_defaults['date'] = time();
     $this->_defaults['deliverydate'] = time();
     // Set default due date and copy customer remarks to invoice description
     if (array_key_exists('customer', $this->_request_data)) {
         $dummy = new org_openpsa_invoices_invoice_dba();
         $dummy->customer = $this->_request_data['customer']->id;
         $this->_defaults['vat'] = $dummy->get_default('vat');
         if (is_a($this->_request_data['customer'], 'org_openpsa_contacts_person_dba')) {
             $this->_defaults['customerContact'] = $this->_request_data['customer']->id;
         }
         // we got a customer, set description default
         $this->_defaults['description'] = $dummy->get_default('remarks');
         unset($dummy);
     } else {
         $due_date = $this->_config->get('default_due_days') * 3600 * 24 + time();
         $this->_defaults['due'] = $due_date;
     }
     // Generate invoice number
     $client_class = midcom_baseclasses_components_configuration::get('org.openpsa.sales', 'config')->get('calculator');
     $calculator = new $client_class();
     $this->_defaults['number'] = $calculator->generate_invoice_number();
     $this->_defaults['owner'] = midcom_connection::get_user();
 }