<?php
# Here we are printing or displaying a transaction of some sort.

# our config file
include('config.inc');

# the function that processes POST variables
include('getpostvars.inc');

# what do I need to accept here? probably just the transaction_id, if that
$transaction_id		= getpostvars('transaction_id', 11);

#
# let's set up our database connection
include ('database.inc');

#
# the wrapper I wrote to help with database calls
include ('keyvalue.inc');


###############################################################################################
#                                       print header                                          #
###############################################################################################

$title	= 'Print Invoice';
include('header.inc');

###############################################################################################
#                               print date and invoice number                                 #
###############################################################################################

# choose from these:
$fields		= 'transaction_id, transaction_date, type_id, company_id';
$fields		.= ', purchase_order, project_reference, problem, solution';
$fields		.= ', technician_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
$transaction_date	= $row['transaction_date'];
$type_id			= $row['type_id'];
$company_id			= $row['company_id'];
$purchase_order		= $row['purchase_order'];
$project_reference	= $row['project_reference'];
$problem			= $row['problem'];
$solution			= $row['solution'];
$technician_id		= $row['technician_id'];
$labor_hours		= $row['labor_hours'];
$labor_rate			= $row['labor_rate'];
$tax_rate			= $row['tax_rate'];

# now we echo our data
echo "<table>\n";
echo "<tr>\n<td><b>Date:</b></td>\n<td>" . $row['transaction_date'] . "</td>\n</tr>\n";
# here we look up and dynamically print the type of transaction
$type_name	= fetch_name ($type_id, $type_name, 'type', $link);
echo "<tr>\n<td><b>$type_name:</b></td>\n<td>$transaction_id</td>\n</tr>\n";
echo "</table>\n<br>\n";


###############################################################################################
#                                 print company information                                   #
###############################################################################################

# let's set $company_id so we have something to work with.
#$company_id = 1;
echo "<big><b>Company Information</b></big><br>\n";
include('showcompany.inc');

###############################################################################################
#                              description of work performed                                  #
###############################################################################################
#
# let's grab $technician_name out of the database
$technician_name	= fetch_name($technician_id, $technician_name, 'technician', $link);

?>
<p><big><b>Problem Reported</big></b>
<table border=1 width="100%">
  <tr>
    <td>
      <?php echo $problem . "\n" ?>
    </td>
  </tr>
 </table>

<p><big><b>Description of work performed</big></b>
<table border=1 width="100%">
  <tr>
    <td>
      <?php echo "$solution\n" ?>
    </td>
  </tr>
 </table>
<b>Technician:</b> <?= $technician_name ?>

<?php
###############################################################################################
#                                      materials used                                         #
###############################################################################################

# initialize $subtotal
$materialtotal	= 0;

?>
<p><big><b>Materials Used</big></b>
<table border=1 width="100%">
  <tr>
    <th>Quantity</td>
    <th>Description</td>
    <th>Unit Price</td>
    <th>Extended</td>
  </tr>
  <?php
  # now we do our query and loop

  # here we set up the fields we want to grab
  # DO I WANT ALL OF THIS?????
  $fields		= 'material_id, transaction_id, quantity, item_type, item_description, 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
    echo "<tr>\n";
    echo "<td align=\"center\">" . $row["quantity"] . "</td>\n";
    echo "<td align=\"left\">" . $row["item_description"] . "</td>\n";
    echo "<td align=\"right\">" . number_format($row["unitprice"],2) . "</td>\n";
    $extended	= $row["quantity"] * $row["unitprice"];
	$materialtotal	= $materialtotal + $extended;
    echo "<td align=\"right\">" . number_format($extended,2) . "</td>\n";
    echo "</tr>\n";
  }

  $labortotal	= $labor_hours * $labor_rate;
  #$subtotal		= $materialtotal + $labortotal;
  #$salestax		= ($subtotal * $tax_rate) / 100;
  #$grandtotal	= $subtotal + $salestax;
  ?>
  <tr>
    <td> </td>
	<td> </td>
	<td> </td>
	<td> </td>
  <tr>
    <td align="center"><?= $labor_hours ?></td>
	<td>hours labor</td>
	<td align="right">$<?= $labor_rate ?> per hour</td>
	<td align="right">$<?= number_format($labortotal,2) ?></td>
  </tr>

  <?php
  # here we have to add up all the numbers and print out our totals
  include ('totals.inc');
  ?>

</table>

<?php
#number_format($parts["price"][$items],2)
###############################################################################################
#                                      print footer                                           #
###############################################################################################

include('footer.inc');

?>
