<?php
# Here we are trying to list all of the invoices sent to a particular company

# Our boilerplate include file
include ('standardinclude.inc');

# here we send our POST variables off to be processed
$by_get          = getgetvars('by_get', 1);

# from reportbymonth.php, for instance
if ($by_get == 1) {
  $company_id   = getgetvars('company_id', 11);
} else {
  $company_id   = getpostvars('company_id', 11);
}

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

$title        = 'List Invoices By Company';
include("$settingspath/header.inc");

###############################################################################################
#                          retrieve company info from database                                #
###############################################################################################

print "<big><b>Company Information</b></big><br>";
include('showcompany.inc');
print "<p>&nbsp;</p>\n";

###############################################################################################
#                          pull rows from transaction database                                #
###############################################################################################

print "<table border=1 cellpadding=2>\n<tr>\n";
print "<th>Date</th>\n";
print "<th>Transaction</th>\n";
print "<th>Type</th>\n";
print "<th>Problem</th>\n";
print "<th>Technician</th>\n";
print "<th>Purchase Order</th>\n";
print "<th>Project</td>\n";
print "<th>Total</th>\n";
print "<th>Paid?</th>\n";
print "<th colspan=2>Actions</th>\n";
print "</tr>\n";


# we want to print the total due for each invoice
include('gettotal.inc');

# And also keep a running total of all balances
$runningtotal = 0;

# here we set up the fields we want to grab
$fields       = 'transaction_id, transaction_date, type_id, problem, technician_id, ';
$fields       .= 'purchase_order, project_reference, company_id, paid';

$query        = "SELECT $fields FROM transaction WHERE company_id = $company_id";
$result       = mysql_query($query, $link);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  print "<tr>\n";
  print "<td>$row[transaction_date]</td>\n";
  $transaction_id   = $row[transaction_id];       # I hate doing stuff more than once
  print "<td>$transaction_id</td>\n";
  # convert transaction type to English
  $type_name        = fetch_name($row[type_id], $type_name, 'type', $link);
  print "<td>$type_name</td>\n";
  print "<td>$row[problem]</td>\n";
  # convert technician ID to English
  $technician_name  = fetch_name($row[technician_id], $technician_name, 'technician', $link);
  print "<td>$technician_name</td>\n";
  print "<td>$row[purchase_order]&nbsp;</td>\n";
  print "<td>$row[project_reference]&nbsp;</td>\n";
  $grandtotal       = get_total($transaction_id, $grandtotal, $link);
  print "<td>\$" . number_format($grandtotal,2) . "</td>\n";
  if ($row[paid] == 1) {
    print "<td>Yes</td>\n";
  } else {
    print "<td>No</td>\n";
  }
  print "<td>\n";
  print "<form action=\"printinvoice.php\" method=\"POST\" enctype=\"text\">\n";
  print "<input type=hidden name=\"transaction_id\" value=\"$row[transaction_id]\">\n";
  print "<input type=submit value=\"Print\">\n";
  print "</form>\n";
  print "<td>\n";
  print "<form action=\"updateinvoice.php\" method=\"POST\" enctype=\"text\">\n";
  print "<input type=hidden name=\"transaction_id\" value=\"$row[transaction_id]\">\n";
  print "<input type=submit value=\"Edit\">\n";
  print "</form> </td></tr>\n";

  # Tally the running balance.
  if ($row[type_id] == 'IN') {
    $runningtotal     = $runningtotal + $grandtotal;
  }
}

print "</table>\n";

print "<p>&nbsp;</p>";
print "<big><b>Running balance: $" . number_format($runningtotal,2) . "</b></big><br>\n";

#print "<P>";
#print_r($_POST);


###############################################################################################
#                                      print footer                                           #
###############################################################################################

include("$settingspath/plainfooter.inc");

?>
