<?php
# display include for invoice app

# troubleshooting
#print_r($parts);
#echo $parts["quantity"][1];

# grab today's date if requested
if ($today == "true")
  {
    $month = date("m");
    $month = $month * 1;    # removes leading zero
    $day = date("j");
    $year =date("Y");
  };
?>

<table width="100%" COLS=2>
  <tr>
    <td><b>Date:</b> <?php echo "$month/$day/$year"; ?><br><b>Invoice:</b> <?php echo $invoicenumber; ?>  </td>
    <td align=right><big><b><?php echo $transaction; ?></b></big>  </td>
  </tr>
</table>


<p><big><b>Company Information</b></big><br>

<table BORDER WIDTH="100%" NOSAVE >
  <tr>
    <td>
      <b>Company:</b> <?php echo $company; ?>
      <br><b>Address:</b> <?php echo $street; ?>
      <br><b>Building Number:</b> <?php echo $building; ?> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Suite:</b> <?php echo $suite; ?>
      <br><b>Attention:</b> <?php echo $attention; ?>
      <br><b>Phone:</b> <?php echo "($phonearea) $phonex-$phonedigits &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <b>Fax:</b> ($faxarea) $faxex-$faxdigits"; ?>
    </td>
    <td VALIGN=top>
      <b>Job Number / Purchase Order:</b> <?php echo $jobnumber; ?>
      <br><b>Location / Store Number:</b> <?php echo $location; ?>
    </td>
  </tr>
</table>

<br>&nbsp;<br><big><b>Problem Reported</b></big>
<table BORDER COLS=1 WIDTH="100%" NOSAVE >
  <tr>
    <td>
      <?php echo stripslashes($problem); ?>
    </td>
  </tr>
</table>

<br>&nbsp;<br><big><b>Description of work performed</b></big>
<table BORDER COLS=1 WIDTH="100%" NOSAVE >
  <tr>
  <td>
    <?php
    echo stripslashes($work);
    if ($transaction == Invoice) {
    echo "<p><b>Technician:</b> $tech";
    };
    ?>
  </td>
  </tr>
</table>

<br>&nbsp;<br><big><b>Materials used</b></big>
<table BORDER WIDTH="100%" NOSAVE >
  <tr>
    <th>
      Quantity
    </td>
    <th>
      Item
    </td>
    <th>
      Unit Price
    </td>
    <th>
      Extended Price
    </td>
  </tr>

<?php
  # this needs to be an array inside a loop
  /*$extended1 = $price1 * $quantity1;
  $extended2 = $price2 * $quantity2;
  $extended3 = $price3 * $quantity3;
  $extended4 = $price4 * $quantity4;
  $partstotal += $extended1;
  $partstotal += $extended2;
  $partstotal += $extended3;
  $partstotal += $extended4;*/

/* pugsly's way
foreach ($parts as $items){
  // The above breaks out the first array into its own array
  foreach ($items as $singleentry){
    //this one actually gets the values
    echo "
      <tr>
      <td><div align=right>$items " . $parts["quantity"][$items] . "</div>  </td>
      <td><div align=right>" . $parts["description"][$items] . "</div>  </td>
      <td><div align=right>\$" . number_format($parts["price"][$items],2) . "</div>  </td>
      <td><div align=right>\$" . number_format($parts["extended"][$items],2) . "</div>  </td>
      </tr>";
    }
}   */
?>


  <?php
  #foreach ($parts as $key => $item) # this never did work right
  for ($item=1; $item <= 20; $item++) {
    #if ( ($parts["quantity"][$item] > 0) ) { #) {  #and ( (isset($parts["description"][$item])) ) {
    if ( ($parts["quantity"][$item] > 0) and (($parts["description"][$item]) != "") ) {
      $parts["extended"][$item] = $parts["price"][$item] * $parts["quantity"][$item];
      $partstotal += $parts["extended"][$item];
      echo "
      <tr>
        <td><div align=center>" . $parts["quantity"][$item] . "</div>  </td>
        <td><div align=left>" . $parts["description"][$item] . "</div>  </td>
        <td><div align=right>\$" . number_format($parts["price"][$item],2) . "</div>  </td>
        <td><div align=right>\$" . number_format($parts["extended"][$item],2) . "</div>  </td>
      </tr>";
    }
  }
  ?>

<?php
/*  echo "
  <tr>
    <td><div align=right>$quantity2</div>  </td>
    <td><div align=right>$description2</div>  </td>
    <td><div align=right>\$" . number_format($price2,2) . "</div>  </td>
    <td><div align=right>\$" . number_format($extended2,2) . "</div>  </td>
  </tr>

  <tr>
    <td><div align=right>$quantity3</div>  </td>
    <td><div align=right>$description3</div>  </td>
    <td><div align=right>\$" . number_format($price3,2) . "</div>  </td>
    <td><div align=right>\$" . number_format($extended3,2) . "</div>  </td>
  </tr>

  <tr>
    <td><div align=right>$quantity4</div>  </td>
    <td><div align=right>$description4</div>  </td>
    <td><div align=right>\$" . number_format($price4,2) . "</div>  </td>
    <td><div align=right>\$" . number_format($extended4,2) . "</div>  </td>
  </tr>";  */
?>

  <?php
  # $tierchecked is for previous $tier radio selection
  switch($tier) {
    case 1:
      $rate = 60;
      $tier1checked = 'checked';
      break;
    case 2:
      $rate = 90;
      $tier2checked = 'checked';
      break;
    case 3:
      $rate = 150;
      $tier3checked = 'checked';
      break;
  };
  $labor = $hours * $rate;
  ?>

  <tr>
    <td>
      <div align=center><?php echo $hours; ?></div>
    </td>
    <td>
      <div align=left>labor @ $<?php echo $rate; ?> per hour</div>
    </td>
    <td>
      <div align=right>$<?php echo number_format($rate,2); ?></div>
    </td>
    <td>
      <div align=right>$<?php echo number_format($labor,2); ?></div>
    </td>
  </tr>

  <?php
  $subtotal = $labor + $partstotal;
  ?>

  <tr>
    <td>  </td>
    <td>  </td>
    <td><div align=right>&nbsp;<br><b>Subtotal:</b></div>  </td>
    <td><div align=right>&nbsp;<br>$<?php echo number_format($subtotal,2); ?></div>  </td>
  </tr>

  <?php
  $salestax = $subtotal * $taxrate / 100;
  ?>

  <tr>
    <td></td>
    <td></td>
    <td>
      <div align=right><b>Sales Tax: </b>(<?php echo $taxrate; ?>%)</div>
    </td>
    <td>
      <div align=right>$<?php echo number_format($salestax,2); ?></div>
    </td>
  </tr>

  <?php
  $grandtotal = $subtotal + $salestax;
  ?>

  <tr>
    <td></td>
    <td></td>
    <td>
      <div align=right><b><big>TOTAL:</big></b></div>
    </td>
    <td>
      <div align=right><b><big>$<?php echo number_format($grandtotal,2); ?></big></b></div>
    </td>
  </tr>
</table>

<?php
# that's all for now
?>
