<?php
# We're including this code in creatinvoice.php if $verified is not set to 'true'


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

# Now we can do our insert

# This needs to have referential integrity, and I can't do that kind of stuff yet

# here's my algorithm:
# do the transaction insert
# get the transaction_id
# do the materials inserts, in a loop, checking each line along the way for validity


# need to construct $transaction_date
$transaction_date	= $year . "-" . $month . "-" . $day;

# we need to escape our SQL data before doing the query using escapesqldata()
$transaction_date   = escapesqldata($transaction_date);
$type_id            = escapesqldata($type_id);
$company_id         = escapesqldata($company_id);
$purchase_order     = escapesqldata($purchase_order);
$project_reference  = escapesqldata($project_reference);
$problem            = escapesqldata($problem);
$technician_id      = escapesqldata($technician_id);
$labor_hours        = escapesqldata($labor_hours);
$labor_rate         = escapesqldata($labor_rate);
$tax_rate           = escapesqldata($tax_rate);


# let's grab $type_id from the database
$type_id            = fetch_id($type_id, $type_name, 'type', $link);



##### transaction #####

$fields             = 'transaction_date, type_id, company_id';
$fields             .= ', purchase_order, project_reference, problem, solution';
$fields             .= ', technician_id, labor_hours, labor_rate, tax_rate, paid';


$values             = "'$transaction_date', '$type_id', '$company_id'";
$values             .= ", '$purchase_order', '$project_reference', '$problem', '$solution'";
$values             .= ", '$technician_id', '$labor_hours', '$labor_rate', '$tax_rate', '$paid'";

$table              = 'transaction';
@mysql_query ("INSERT INTO $table ($fields) VALUES ($values)", $link);

# need to determine $transaction_id that was just assigned,
# so we can insert it into table material
$transaction_id	= mysql_insert_id();



##### material #####

# here we need to loop this to get all values from the array
for ($line=1; $line <= $material_lines; $line++) {

  # here we $_POST the materials variables
  $materials[$line]["quantity"]           = $_POST["materials"][$line]["quantity"];
  $materials[$line]["item_description"]   = $_POST["materials"][$line]["item_description"];
  $materials[$line]["unitprice"]          = $_POST["materials"][$line]["unitprice"];

  # all of these have been posted above, so we can just use the data
  $quantity           = $materials[$line]["quantity"];
  $item_description   = $materials[$line]["item_description"];
  $unitprice          = $materials[$line]["unitprice"];

  # here we need to test each line for actual data before doing the insert
  if ( ($quantity != '') and ($item_description != '') and ($unitprice != '') ) {
    # since we apparently have some sort of data, let's do an insert on table material
    # ----FIXME--- this isn't good enough here, I've had typos eat my data here

    # Here we excape the SQL before running the query
    $quantity         = escapesqldata($quantity);
    $item_description = escapesqldata($item_description);
    $unitprice        = escapesqldata($unitprice);

    $fields           = 'transaction_id, quantity, item_type, item_description, unitprice';
    $values           = "'$transaction_id', '$quantity', '$item_type', '$item_description', '$unitprice'";

    $table            = 'material';
    @mysql_query ("INSERT INTO $table ($fields) VALUES ($values)", $link);
  }

}

# print results to the user

# query for $type_name
$type_name          = fetch_name ($type_id, $type_name, 'type', $link);

print "<big><b>Thanks. The $type_name was added to the database.</b></big>\n";
#include('dberrors_insert.inc');


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

?>
