<?php
# Here we are trying to list all of the invoices that have not bee 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);

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

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

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

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



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

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

echo "<table border=1 cellpadding=2>\n<tr>\n";
echo "<th>Company</th>\n";
echo "<th>Date</th>\n";
echo "<th>Transaction</th>\n";
echo "<th>Problem</th>\n";
echo "<th colspan=2>Actions</th>\n";
echo "</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);


# all of the below is taken from "listbycompany.php", adjust to taste
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  echo "<tr>\n";
  # convert company_id to English
  $company_name       = fetch_name($row[company_id], $company_name, 'company', $link);
  echo "<td>$company_name</td>\n";
  echo "<td>$row[transaction_date]</td>\n";
  echo "<td>$row[transaction_id]</td>\n";
  echo "<td>$row[problem]</td>\n";
  echo "<td>\n";
  echo "<form action=\"printinvoice.php\" method=\"POST\" enctype=\"text\" target=\"_new\">\n";
  echo "<input type=hidden name=\"transaction_id\" value=\"$row[transaction_id]\">\n";
  echo "<input type=submit value=\"Print\">\n";
  echo "</form>\n</td>\n";
  echo "<td>\n";
  echo "<form action=\"listunpaidinvoices.php\" method=\"POST\" enctype=\"text\">\n";
  echo "<input type=hidden name=\"transaction_id\" value=\"$row[transaction_id]\">\n";
  echo "<input type=submit value=\"Mark Paid\">\n";
  echo "</form> </td></tr>\n";
  echo "</tr>\n";
}


echo "</table>\n";

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

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

?>

