<?php # complete_transaction.php
require ('includes/config.inc.php');
$page_title = 'Complete Transaction';
include ('includes/header.html');
require (MYSQL); // Need the database connection:
// refresh_session(0);
// If no first_name session variable exists, redirect the user:
if (!isset($_SESSION['person_id'])) {
$url = BASE_URL . 'index.php'; // Define the URL.
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.
}
// header
echo "<h1>$page_title</h1><br />";
refresh_session(0); // Refresh session settinings incase of previous changes
// Check for a valid worker ID and product ID, through GET or POST:
if ( (isset($_GET['check'])) && (is_numeric($_GET['check'])) && ($_GET['check'] = 123) &&
(isset($_GET['wid'])) && (is_numeric($_GET['wid'])) &&
(isset($_SESSION['worker_id'])) && ($_SESSION['worker_id'] == $_GET['wid']) ) { // From add_deal.php
} else { // No valid ID, kill the script.
echo '<p class="error">This page has been accessed in error.</p>';
include ('includes/footer.html');
exit();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.
$trimmed = array_map('trim', $_POST); // Trim all the incoming data:
$sale_date = $payment_type = FALSE;
if (isset($trimmed['payment_type'])) {
$payment_type = mysqli_real_escape_string ($dbc, $trimmed['payment_type']);
}
// Check for date of sale:
if ($trimmed['sale_date'] == date('Y-m-d H:i',strtotime($trimmed['sale_date']))){
$sale_date = mysqli_real_escape_string ($dbc, $trimmed['sale_date']);
} else {
echo '<p class="error">Please enter a valid date or date and time!<br>
Must be in this exact format <br>
YYYY-MM-DD   or YYYY-MM-DD HH:MM
Time is in 24 hour format</p>';
}
//Date.parse("2009/06/29 13:30:10", "yyyy/MM/dd HH:mm:ss");
if ($sale_date && $payment_type ) { // If everything's OK...
// complete temp transaction then
// Transfer from temp tables to final tables
try {
$dbc->autocommit(FALSE); // i.e., start transaction
// double check there is a temp transaction
$q = "SELECT ".$_SESSION['transaction_id']." FROM temp_transaction";
$r = @mysqli_query ($dbc, $q);
if (mysqli_affected_rows($dbc) == 0) { // If it ran OK.
echo '<p class="error">It appears that there is no items yet.<br><br>';
throw new Exception($dbc->error);
trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
}
BUG(1);
//
// add to permanate the_transaction table and get an ID
//
$q = "
INSERT INTO the_transaction ( worker_id, payment_type, placed_dateTime, paid_dateTime,
shipped_dateTime, dilivered_dateTime )
VALUES ( '".$_SESSION['worker_id']."', '".$trimmed['payment_type']."', '".$trimmed['sale_date']."',
'".$trimmed['sale_date']."', '".$trimmed['sale_date']."', '".$trimmed['sale_date']."' )
";
$result = $dbc->query($q);
$the_transaction_id = $dbc->insert_id; // last auto_inc id from *this* connection
if ( !$result ) {
//$result->free();
throw new Exception($dbc->error);
trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
}
BUG(2);
//
// get all items ready to and transfer to permanate table
//
$q = "SELECT item_number, transaction_id, product_id, quantaty, negotiated_price, date_time
FROM temp_order_item
WHERE transaction_id=".$_SESSION['transaction_id'];
$r = @mysqli_query ($dbc, $q);
if (mysqli_affected_rows($dbc) == 0) { // If it ran OK.
throw new Exception($dbc->error);
trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
}
BUG(3);
/*
$row = mysqli_fetch_all($r, MYSQLI_ASSOC);
echo $row[0]['product_id'].'<br>';
echo $row[1]['product_id'].'<br>';
echo $row[2]['product_id'].'<br>';
echo 'tran id<br>';
echo $row[0]['transaction_id'].'<br>';
echo $row[1]['transaction_id'].'<br>';
echo $row[2]['transaction_id'].'<br>';
$numRows = mysqli_affected_rows($dbc);
echo '<br>'.$numRows.' rows affected!! what! <br><br>';
for ($i=0;$i<$numRows;$i++){
echo $row[$i]['product_id'].'<br>';
echo $row[$i]['transaction_id'].'<br>';
}
*/
//
// make the final transfer to the_transaction table
//
//$row = mysqli_fetch_all($r, MYSQLI_ASSOC); // this works on prtable but not live
//$row = $r->fetch_all(MYSQLI_ASSOC); // this works on prtable but not live
//print_r($row); BUG(55,1);
//$row = $r->fetch_assoc(); // this one does not like how the veriables are set up
//$numRows = mysqli_affected_rows($dbc);
//$tran_id = $row[0][1]; // was
$count = 0;
$r_array = array();
while ($row = $r->fetch_assoc()) {
$r_array[] = $row;
$count++;
}
$tran_id = $r_array[0]['transaction_id'];
for ($i=0;$i<$count;$i++){
$q = "
INSERT INTO order_item ( transaction_id, product_id, quantaty, negotiated_price, date_time )
VALUES ( $the_transaction_id, ".$r_array[$i]['product_id'].", ".$r_array[$i]['quantaty'].",
".$r_array[$i]['negotiated_price'].", '".$r_array[$i]['date_time']."' )
";
$r = @mysqli_query ($dbc, $q);
BUG(4);
if (mysqli_affected_rows($dbc) == 0) { // If it ran OK.
throw new Exception($dbc->error);
trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
}
} // End of WHILE loop.
BUG(4.5);
//
// delete temp order items
//
$q = "DELETE FROM temp_order_item WHERE transaction_id=$tran_id LIMIT $count";
$r = @mysqli_query ($dbc, $q);
if (mysqli_affected_rows($dbc) != $count) { // If it ran OK.
throw new Exception($dbc->error);
trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
}
BUG(5);
//
// delete temp_transaction ID
//
$q = "DELETE FROM temp_transaction WHERE transaction_id=$tran_id";
$r = @mysqli_query ($dbc, $q);
if (mysqli_affected_rows($dbc) != 1) { // If it ran OK.
throw new Exception($dbc->error);
trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
}
BUG(6);
//
// our SQL queries have been successful. commit them
// and go back to non-transaction mode.
//
$dbc->commit();
$dbc->autocommit(TRUE); // i.e., end transaction
$passed = TRUE;
}
catch ( Exception $email ) { // had issues, must rollback.
$dbc->rollback();
$dbc->autocommit(TRUE); // i.e., end transaction
$passed = FALSE;
BUG(7);
}
if ($passed) { // If it ran OK.
BUG(8);
// Finish the page:
echo '<h3>The transaction has been made final.</h3>';
$_SESSION['transaction_id']="FALSE";
BUG(9);
//refresh_session();
// forward to add_deal.php in 1 seconds
$url = BASE_URL . 'add_deal.php';
//header('Refresh: 1;url=' . $url . '');
header("Location: $url");
include ('includes/footer.html'); // Include the HTML footer.
exit(); // Stop the page.
} else { // If the query did not run OK.
echo '<p class="error">The changes did not take place due to a system error.<br>
We apologize for any inconvenience.</p>';
BUG(10);
// forward to add_deal.php in 2 seconds
$url = BASE_URL . 'add_deal.php';
header('Refresh: 7;url=' . $url . '');
include ('includes/footer.html'); // Include the HTML footer.
exit(); // Stop the page.
}
} else {
// forward to add_deal.php in 2 seconds
$url = BASE_URL . 'add_deal.php';
header('Refresh: 5;url=' . $url . '');
include ('includes/footer.html'); // Include the HTML footer.
exit(); // Stop the page.
}
}
include ('includes/footer.html');
?>