<?php
# Here we select companies from the database

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

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


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

$title        = 'Choose Company';
include ("$settingspath/header.inc");
?>

<big><b>Please choose a company from the list:</b></big>

<table border="1" cellpadding="2">
  <tr>
    <th><a href="choosecompany.php?sort_column=company_name">Company Name </a></th>
    <th>Address  </th>
    <th><a href="choosecompany.php?sort_column=city">City  </a></th>
    <th><a href="choosecompany.php?sort_column=state_id">State  </a></th>
    <th colspan=3>Actions  </th>
  </tr>

  <?php
  # Here we want to get a count of invoices by company, so we can embed the "Review Invoices" button.
  # Let's grab all of the counts stick them in a hash: $invoice_count{'$company_id'} = $count.
  $count_query  = 'select company_id, count(*) from transaction group by company_id';
  $result       = mysql_query($count_query, $link);
  while ( $invoice_count = mysql_fetch_array($result, MYSQL_ASSOC) ) {
    # Here we create an associative array (a perl "hash") containing invoice counts per company_id.
    # We could make repeated calls to the DB in the table loop, but that's really inefficient.
    $company_id               = $invoice_count["company_id"];
    $count                    = $invoice_count["count(*)"];
    $count_array[$company_id] = $count;
  }

  #
  # here we set up our company query
  $build_query       = "SELECT company_id, company_name, address1, city, state_id, url FROM company";
  #
  if ( $sort_column == '') {
    $sort_column     = 'company_name';
  }
  $build_query       .= " ORDER BY $sort_column";
  #
  $result            = mysql_query($build_query, $link);

  # need to write our query and get the variables right
  while ( $company_row = mysql_fetch_array($result, MYSQL_ASSOC) ) {
    $company_id     = $company_row["company_id"];
    $company_name   = $company_row["company_name"];
    $address1       = $company_row["address1"];
    $city           = $company_row["city"];
    $state_id       = $company_row["state_id"];
    $url            = $company_row["url"];
    # retrieve the invoice counts
    $count          = $count_array[$company_id];

    print "<tr valign=\"top\">";
    # if URL exists, print it as a link
    if ( !( $url == '' ) ) {
      print "<td><a href=\"" . $url . "\" target=\"_new\">" . $company_name . "</a> &nbsp;</td>";
    } else {
      print "<td>" . $company_name . " &nbsp;</td>";
    }
    print "<td>$address1 &nbsp;</td>";
    print "<td>$city &nbsp;</td>";
    print "<td align=\"center\">$state_id &nbsp;</td>";
    print "<td>
    <form action=\"createinvoice.php\" method=\"POST\" enctype=\"text\">
      <input type=hidden name=\"company_id\" value=\"$company_id\">
      <input type=submit value=\"Create Invoice\">
      </form> </td>\n";
    if ($count > 0) {
      print "<td>
        <form action=\"listbycompany.php\" method=\"POST\" enctype=\"text\">
        <input type=hidden name=\"company_id\" value=\"$company_id\">
        <input type=submit value=\"Review Invoices ($count)\">
        </form> </td>\n";
    } else {
      print "<td align=\"center\">no invoices found</td>\n";
    }
    print "<td>
      <form action=\"editcompany.php\" method=\"POST\" enctype=\"text\">
      <input type=hidden name=\"company_id\" value=\"$company_id\">
      <input type=hidden name=\"companytask\" value=\"edit\">
     <input type=submit value=\"Edit Company\">
     </form> </td></tr>\n";
  }

  ?>
</table>



<?php

#print_r($_POST);
#print_r($_GET);

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

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

?>

