<?php
# Here we are trying to list all of the invoices that have not been paid

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

# here we send our POST variables off to be processed
$transaction_id		= getpostvars('transaction_id', 11);


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

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


###############################################################################################
#                                   database insert                                           #
###############################################################################################

# I don't really want to do the database stuff before the table,
# but the user needs to see what transaction he just marked paid.

if ( ($transaction_id > 0) ) {
  $table		= 'transaction';
  @mysql_query ("UPDATE $table SET paid = 1 WHERE transaction_id = $transaction_id", $link);

  print "<p>&nbsp;</p>";
  print "<big><b>Thanks. Invoice number $transaction_id has been marked paid.</b></big><br>\n";

  print "<form action=\"printinvoice.php\" method=\"POST\" enctype=\"text\">\n";
  print "<input type=hidden name=\"transaction_id\" value=\"$transaction_id\">\n";
  print "<input type=submit value=\"Print\">\n";
  print "</form>\n</td>\n";

  print "<p>&nbsp;</p>";
  print "<p>&nbsp;</p>";
}

# now we reset the variables and print clean forms in case the page is refreshed
$transaction_id		= 0;



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

print "<big><b>Unpaid Invoices by Company</b></big><br>";

print "<table border=1 cellpadding=2>\n<tr>\n";
print "<th>Company</th>\n";
print "<th>Date</th>\n";
print "<th>Invoice</th>\n";
print "<th>Total</th>\n";
print "<th>Problem</th>\n";
print "<th colspan=2>Actions</th>\n";
print "</tr>\n";


# Here we set up the fields we want to grab
$fields		= 'transaction_id, transaction_date, type_id, company_id, problem, paid';
$type_id	= 'IN';

$query		= "SELECT $fields FROM transaction WHERE type_id = 'IN' and paid = 0 order by company_id";
$result		= mysql_query($query, $link);

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

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

# all of the below is taken from "listbycompany.php", adjust to taste
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  print "<tr>\n";
  # convert company_id to English
  $company_name       = fetch_name($row[company_id], $company_name, 'company', $link);
  print "<td>$company_name</td>\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";
  $grandtotal = get_total($transaction_id, $grandtotal, $link);
  print "<td>\$" . number_format($grandtotal,2) . "</td>\n";
  print "<td>$row[problem]</td>\n";
  print "<td>\n";
  print "<form action=\"printinvoice.php\" method=\"POST\" enctype=\"text\" target=\"_new\">\n";
  print "<input type=hidden name=\"transaction_id\" value=\"$row[transaction_id]\">\n";
  print "<input type=submit value=\"Print\">\n";
  print "</form>\n</td>\n";
  print "<td>\n";
  print "<form action=\"listunpaidinvoices.php\" method=\"POST\" enctype=\"text\">\n";
  print "<input type=hidden name=\"transaction_id\" value=\"$row[transaction_id]\">\n";
  print "<input type=submit value=\"Mark Paid\">\n";
  print "</form> </td></tr>\n";
  print "</tr>\n";
  # Tally the balance.
  $runningtotal	= $runningtotal + $grandtotal;

}


print "</table>\n";

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

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

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

?>

