SlanginBeef.com – add_deal.php


<?php # add_deal.php
// This script will be the base page to set up the user's profile
require ('includes/config.inc.php'); 
$page_title = 'Add a Deal';
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.
}

refresh_session(0); // Refresh session settinings incase of previous changes

// header
echo "<h1>$page_title</h1><br />";
//$_SESSION['transaction_id']= "NULL";



if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.
    $trimmed = array_map('trim', $_POST); // Trim all the incoming data:
    
    // Assume invalid values:
    $box = $sale_price = $sale_price_pass = FALSE;
    
    // Check for item number:
    if (isset($trimmed['item']) && is_numeric($trimmed['item'])) {
        $product_id = $trimmed['item'];
    }
    
    // Check for box:
    if (preg_match ('/^([0-9]{1,2})$/', $trimmed['boxes'])) {
        $box = mysqli_real_escape_string ($dbc, $trimmed['boxes']);
    } else {
        echo '<p class="error">Please enter a valid box count!<br>
            Must be between 0 and 99 <br>
            Numbers Only.</p>';
    }
    
    // Check for sale price:
    //  ^([+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2})$  // Currency amount cents mandatory, Optional (+-) (thousands separators)
    //  ^([+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2})$  // Currency amount cents mandatory, Optional (thousands separators)
    //  ^([+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?)$  // Currency amount, Optional (+-) (cents) (thousands separators)
    //  ^()$|^([0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?)$   // Currency amount, Optional (blank) (cents) (thousands separators)
    //  ^([0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?)$   // Currency amount, Optional (cents) (thousands separators)
    //  ^([0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2})$   // Currency amount up to 9999.99, Optional (cents) (thousands separators)
    if (!isset($trimmed['how_much'])) {$trimmed['how_much'] = 0.00;
    } else {if ($trimmed['how_much'] == 0) {$trimmed['how_much'] = 0.00;
        }
    }
    if (preg_match ('/^([0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?)$/', $trimmed['how_much']) &&
            $trimmed['how_much'] < 2500.01) {
        $sale_price = mysqli_real_escape_string ($dbc, $trimmed['how_much']);
        $sale_price_pass = TRUE;
    } else {
        echo '<p class="error">Please enter a valid Case Price!<br>
            Cents Optional. No letters or symboles<br>
            Must be between 0.00 and 2500.00</p>';
    }


    if ($box && $sale_price_pass ) { // If everything's OK...
        
        // Add the line item to temp_transaction table in the database:
        try {
            $dbc->autocommit(FALSE); // i.e., start transaction
            
            //
            // Add product to product table
            //
            
            // Make sure the temp_trans_id is already in the table:
            $q = "SELECT worker_id, transaction_id FROM temp_transaction WHERE worker_id=".$_SESSION['worker_id'];
            $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />
                    MySQL Error: " . mysqli_error($dbc));
            if (mysqli_num_rows($r) == 0) { // temp_trans_id needs to be added to the table
                $q = "
                    INSERT INTO temp_transaction ( worker_id )
                    VALUES ( ".$_SESSION['worker_id']." )
                    ";
                $result = $dbc->query($q);
                $transaction_id = $dbc->insert_id; // last auto_inc id from *this* connection
                $_SESSION['transaction_id'] = "$transaction_id";
                if ( !$result ) {
                    //$result->free();
                    throw new Exception($dbc->error);
                    trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
                }
            } else { // already has transaction_id
                $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
                $transaction_id = $row['transaction_id'];
                $_SESSION['transaction_id'] = "$transaction_id";
            }
            
            // insert new line item into temp_order_item table
            $q = "
                INSERT INTO temp_order_item ( transaction_id, product_id, quantaty, negotiated_price )
                VALUES ( ".$_SESSION['transaction_id'].", $product_id, $box, $sale_price )
                ";
            $result = $dbc->query($q);
            if ( !$result ) {
                //$result->free();
                throw new Exception($dbc->error);
                trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
            } 
            
        
            
            // 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;
            $trimmed = array(); // Destroy the variables.
            BUG(98);
        }
        catch ( Exception $email ) {
            $dbc->rollback(); 
            $dbc->autocommit(TRUE); // i.e., end transaction   
            $passed = FALSE;
            BUG(99);
        }
        if ($passed) { // If it ran OK.
            // Finish the page:
            echo '<h3>The item was added.</h3>';
            
            
        }
        
    } else { // If one of the data tests failed.
        echo '<p class="error">Please try again.</p>';
    }
    
    
}// End of the main Submit conditional.

//
// add to the current transaction
//
echo '
<form action="add_deal.php" method="post" id=2>
    <fieldset class="select_item">
    <legend>What item was sold?</legend>
        <select id="item" name="item">
            ';
            // Define the query:
            $q = "
                SELECT pro.product_id, pro.item_number, pro.description, wpi.case_price, 
                    wpi.box_price, wpi.list_price, wpi.inventory_box_count
                FROM product AS pro
                    INNER JOIN worker_price_inventory AS wpi
                    ON pro.product_id = wpi.product_id
                    AND wpi.worker_id = ".$_SESSION['worker_id']."
                    AND pro.office_id = ".$_SESSION['office_id']."
            ";
            $r = @mysqli_query ($dbc, $q); // Run the query.
            
            while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
                $product_id=$row["product_id"];
                $item=$row["item_number"];
                $disc=$row["description"];
                $case=$row["case_price"];
                $msrp=$row["list_price"];
                 echo "<option value=$product_id>
                            $item, -----> $case / $msrp
                        </option>";
            }
?>
            </select>
            <br>
            <p>How many boxes? <input type="text" name="boxes" size="2" maxlength="2" 
                value="<?php echo (isset($trimmed['boxes']) ? $trimmed['boxes'] : '6' ); ?>" /></p>
            
            <p>How Much? <input type="text" name="how_much" size="7" maxlength="7" 
                value="<?php echo (isset($trimmed['how_much']) ? $trimmed['how_much'] : '' ); ?>" /></p>
            
            <div>
                &emsp; &emsp;<input name="add" type="submit" value="Add to Tansaction" />
            </div>
    </fieldset>
</form>
<br>
<?php



//
// The current transaction
//

echo '
<form action="complete_transaction.php?check=123&wid='.$_SESSION['worker_id'].'" method="post">
    <fieldset class="transaction">
    <legend>The Current Transaction</legend>
            ';
            
            $q = "
                SELECT pro.item_number, toi.quantaty, toi.negotiated_price, toi.item_number AS item_id
                FROM temp_order_item AS toi
                    INNER JOIN product AS pro
                    ON toi.product_id=pro.product_id
                WHERE transaction_id = ".$_SESSION['transaction_id']."
                ";
            $r = @mysqli_query ($dbc, $q); // Run the query.
            
            // Table header:
            echo '<table align="center" cellspacing="0" cellpadding="5" width="75%">
            <tr>
                <td align="left"><b>Item #</a></b></td>
                <td align="left"><b>Boxes</b></td>
                <td align="left"><b>Amount</a></b></td>
                <td align="left"><b>Delete</b></td>
            </tr>
            ';
            
            // Fetch and print all the records....
            $bg = '#eeeeee'; 
            while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
                $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
                    echo '<tr bgcolor="' . $bg . '">
                    <td align="left">' . $row['item_number'] . '</td>
                    <td align="left">' . $row['quantaty'] . '</td>
                    <td align="left">' . $row['negotiated_price'] . '</td>
                    <td align="left"><a href="del_pre_trans_item.php?toi_id='.$row['item_id'].'&wid='.$_SESSION['worker_id'].'">delete</a></td>
                </tr>
                ';
            } // End of WHILE loop.
            echo '</table>';
?>
        <div align="center">
            <br>
            <p>Cash:<input type="radio" name="payment_type" value="Cash" checked="checked" /> &emsp;  
                Credit:<input type="radio" name="payment_type" value="Credit" />&emsp;  
                Check:<input type="radio" name="payment_type" value="Check" />&emsp;  
                EBT:<input type="radio" name="payment_type" value="EBT" /></span></pre>
&nbsp;
<pre><span style="font-size: 10pt;">
                
            <p>Date of the sale: <input type="text" name="sale_date" size="16" maxlength="16" 
                    value="<?php echo (isset($trimmed['sale_date']) ? $trimmed['sale_date'] : date('Y-m-d H:i') ); ?>" /></p>
            <br>
            <input type="submit" name="submit" value="Submit This Deal!" />
        </div>
    </fieldset>
</form>





<?php

//
// Show all of the deals for today
//

// Determine the sort...
// Default is by registration date.
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'rd';
$ob = (isset($_GET['ob'])) ? $_GET['ob'] : 'ASC';
$ob = ($ob=='DESC' ? 'ASC' : 'DESC');
// Determine the sorting order:
switch ($sort) {
    case 'box':
        $order_by = 'Boxes '.$ob;
        break;
    case 'pull':
        $order_by = 'Pull '.$ob;
        break;
    case 'date':
        $order_by = 'Date '.$ob;
        break;
    default:
        $order_by = 'Date '.$ob;
        $sort = 'date';
        break;
}

$worker_id = $_SESSION['worker_id'];
// Define the query:
$q = "
    SELECT t.transaction_id, pro.item_number, oi.quantaty, oi.negotiated_price, 
        oi.item_number AS item_id, t.payment_type, t.paid_dateTime, t.worker_id
    FROM order_item AS oi
        INNER JOIN product AS pro
        ON oi.product_id=pro.product_id
            INNER JOIN the_transaction AS t
            ON t.transaction_id = oi.transaction_id
            AND t.paid_dateTime AND t.paid_dateTime>CURDATE() 
    WHERE t.worker_id = $worker_id
    ";

    // AND t.paid_dateTime >= DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY)
    // DATE(date)=CURDATE()
    // AND DATE(t.paid_dateTime)=CURDATE()
    // AND t.paid_dateTime=CURDATE()
    // AND t.paid_dateTime>CURDATE()
    // CURDATE()
$r = @mysqli_query ($dbc, $q); // Run the query.
$r2 = @mysqli_query ($dbc, $q); // Run the query.

echo '
<fieldset class="todays_deals">
<legend>All Deals For Today</legend>
';
// Table header:
echo '<table align="center" cellspacing="0" cellpadding="5" width="75%">
<tr bgcolor=lightblue>
    <td align="left"><b><a href="add_deal.php?id='.$worker_id.'&sort=item&ob='.$ob.'">Tran ID</a></b></td>
    <td align="left"><b><a href="add_deal.php?id='.$worker_id.'&sort=item&ob='.$ob.'">Item Number</a></b></td>
    <td align="left"><b><a href="add_deal.php?id='.$worker_id.'&sort=box&ob='.$ob.'">Boxes</a></b></td>
    <td align="left"><b><a href="add_deal.php?id='.$worker_id.'&sort=pull&ob='.$ob.'">How Much</a></b></td>
    <td align="left"><b><a href="add_deal.php?id='.$worker_id.'&sort=pay&ob='.$ob.'">Payment</a></b></td>
    <td align="left"><b><a href="add_deal.php?id='.$worker_id.'&sort=date&ob='.$ob.'">Time</a></b></td>
</tr>
';

// Fetch and print all the records....
$row2 = mysqli_fetch_array($r2, MYSQLI_ASSOC);
$_SESSION['temp_bg1'] = $row2['transaction_id'];
$bg = '#bbbbbb';
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    if ($_SESSION['temp_bg1'] == $row['transaction_id']){
    } else {
        $bg = ($bg == '#bbbbbb' ? '#ffffff' : '#bbbbbb'); 
        $_SESSION['temp_bg1'] = $row['transaction_id'];
    }
        echo '<tr bgcolor="' . $bg . '">
        <td align="left">' . $row['transaction_id'] . '</td>
        <td align="left">' . $row['item_number'] . '</td>
        <td align="left">' . $row['quantaty'] . '</td>
        <td align="left">' . $row['negotiated_price'] . '</td>
        <td align="left">' . $row['payment_type'] . '</td>
        <td align="left">' . $row['paid_dateTime'] . '</td>
    </tr>
    ';
} // End of WHILE loop.

echo '</table>';
echo '</fieldset>';

include ('includes/footer.html');

?>