<?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);

# Ok, this is utterly horrible.
# There must be a way to refactor this. I've just typed these field names in thrice.
# Step through an array?
# What about putting this in a class, once I figure out how to do OOP?

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

# transaction
#$fields		= 'transaction_id, type_id, date, company_id';
$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		= "'NULL, $type_id, $transaction_date', '$company_id'";
$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);
#echo "<br>INSERT INTO $table ($fields) VALUES ($values)<br>\n";

# need to determine $transaction_id that was just assigned,
# so we can insert it into table material
$transaction_id	= mysql_insert_id();
#echo "<h2>transaction_id = $transaction_id</h2>\n";

# material
# here we need to loop this to get all values from the array

for ($line=1; $line <= $material_lines; $line++) {

  # all of these have been posted above, so we can just use the data
  $quantity						= $materials[$line]["quantity"];
  # $item_type hasn't been integrated into the form yet...
  #$item_type						= $materials[$line]["item_type"];
  #$item_type						= '';	# do this, or just leave it undefined?
  $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

	# Here we excape the SQL before running the query
	$quantity						= escapesqldata($quantity);
	# $item_type					= escapesqldata($item_type); 	# we haven't placed this into the form yet
	$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);
    #echo "<br>INSERT INTO $table ($fields) VALUES ($values)<br>\n";
  }

}

# echo results to the user

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

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


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

?>
