You are here

function _invoice_get_invoice_totals in Invoice 6

Same name and namespace in other branches
  1. 7 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

5 calls to _invoice_get_invoice_totals()
invoice_delete_item in ./invoice_ajax.inc
Delete an invoice item
invoice_form in ./invoice_form.inc
Implementatin of node_form()
invoice_invoices in ./invoice.module
Overview of all invoices
invoice_load in ./invoice.module
Implementation of hook_load()
invoice_save_item in ./invoice_ajax.inc
Add an invoice item

File

./invoice_helpers.inc, line 139
Invoice module

Code

function _invoice_get_invoice_totals($invoice_number, $user_id = 0) {
  $a_totals = array();
  $sql_user_addition = '';
  if ($user_id > 0) {
    $sql_user_addition = ' AND uid="' . intval($user_id) . '"';
  }
  $result = db_query("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=%d" . $sql_user_addition, $invoice_number);
  while ($row = db_fetch_object($result)) {
    $a_totals['extotal'] += _invoice_round($row->extotal, 2);
    $a_totals['inctotal'] += $row->extotal * _invoice_vat_percent_to_decimal($row->vat);
    $a_totals['vattotal'] += $row->vattotal;
  }
  $a_totals['extotal'] = _invoice_round($a_totals['extotal'], 2);
  $a_totals['inctotal'] = _invoice_round($a_totals['inctotal'], 2);
  $a_totals['vattotal'] = _invoice_round($a_totals['vattotal'], 2);
  return $a_totals;
}