You are here

function _invoice_get_invoice_totals in Invoice 7

Same name and namespace in other branches
  1. 6 invoice_helpers.inc \_invoice_get_invoice_totals()

Helper function to calculate invoice totals

The ROUND() function of MySQL is not very reliable, especially in MySQL 4.x, that's why this function came into play

Parameters

integer $invoice_number:

integer $user_id:

Return value

array $a_totals

6 calls to _invoice_get_invoice_totals()
invoice_delete_item in ./invoice_ajax.inc
Delete an invoice item
invoice_form in ./invoice_form.inc
Implements hook_form()
invoice_invoices in ./invoice.module
Overview of all invoices
invoice_load in ./invoice.module
Implements hook_load()
invoice_save_item in ./invoice_ajax.inc
Add an invoice item

... See full list

File

./invoice_helpers.inc, line 191
Invoice module

Code

function _invoice_get_invoice_totals($invoice_number, $user_id = 0) {
  $totals = array(
    'extotal' => null,
    'inctotal' => null,
    'vattotal' => null,
  );
  $sql = "SELECT vat,quantity*unitcost as extotal,\n    (quantity*unitcost)*((vat / 100) +1) as inctotal,\n    (quantity*unitcost)*((vat / 100) +1) * (1 - (1 / ((vat / 100) +1))) as vattotal\n    FROM {invoice_items}\n    WHERE invoice_id = :invoice_id";
  $params = array(
    ':invoice_id' => $invoice_number,
  );
  if ($user_id > 0) {
    $sql .= " AND uid = :uid";
    $params[':uid'] = (int) $user_id;
  }
  $result = db_query($sql, $params);
  foreach ($result as $row) {
    $totals['extotal'] += _invoice_round($row->extotal, 2);
    $totals['inctotal'] += $row->extotal * _invoice_vat_percent_to_decimal($row->vat);
    $totals['vattotal'] += $row->vattotal;
  }
  $totals['extotal'] = _invoice_round($totals['extotal'], 2);
  $totals['inctotal'] = _invoice_round($totals['inctotal'], 2);
  $totals['vattotal'] = _invoice_round($totals['vattotal'], 2);
  return $totals;
}