<?php
# All we are doing here is calculating the total for one invoice, for use in listunpaidinvoices.php, for example.
# No printing is done here.

function get_total ($transaction_id, $grandtotal, $link) {

  ###############################################################################################
  #                                    labor hours and rate                                     #
  ###############################################################################################

  # choose from these:

  $fields           = 'transaction_id, type_id, labor_hours, labor_rate, tax_rate';


  $result           = mysql_query("SELECT $fields FROM transaction WHERE transaction_id = $transaction_id");
  $row              = mysql_fetch_array($result, MYSQL_ASSOC);

  # here we have to map a few scalars from the array $row
  $labor_hours      = $row['labor_hours'];
  $labor_rate       = $row['labor_rate'];
  $tax_rate         = $row['tax_rate'];
  $type_id          = $row['type_id'];


  ###############################################################################################
  #                                      materials used                                         #
  ###############################################################################################

  # initialize $subtotal
  $materialtotal    = 0;

  # now we do our query and loop

  # here we set up the fields we want to grab
  $fields           = 'material_id, transaction_id, quantity, unitprice';

  $result           = mysql_query("SELECT $fields FROM material WHERE transaction_id = $transaction_id");
  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    # handle the results of the select
    $extended       = $row["quantity"] * $row["unitprice"];
    $materialtotal  = $materialtotal + $extended;
  }

  $labortotal       = $labor_hours * $labor_rate;
  $subtotal         = $materialtotal + $labortotal;

  # determine the sales taxes
  $taxonmaterial    = ($materialtotal * $tax_rate) / 100;
  if ($is_labor_taxed == 1) {
    $taxonlabor     = ($labortotal * $tax_rate) / 100;
  } else {
    $taxonlabor     = 0;
  }

  $salestax         = $taxonmaterial + $taxonlabor;
  $grandtotal       = $subtotal + $salestax;

  #print number_format($grandtotal,2);
  return $grandtotal;
}

?>
