Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once '../startsession.php';
- // Make sure the user is logged in before going any further.
- if (!isset($_SESSION['user_id']))
- {
- $login_url = 'http://www.alloygatorspain.es/login.php';
- header('Location: ' . $login_url);
- exit();
- }
- // Set the timezone
- ini_set("date.timezone", "Europe/Madrid");
- date_default_timezone_set('Europe/Madrid');
- // Define the € symbol as a constant for use in the invoices
- define('EURO', chr(128));
- // PHP for the form
- if (isset($_POST["submit"]))
- {
- $fullname = $_SESSION['fullname'];
- $address = $_SESSION['address'];
- $email = $_SESSION['email'];
- $quantity = $_POST['quantity'];
- $nie = $_SESSION['nie'];
- $postcode = $_SESSION['postcode'];
- // Connect to the database
- include '../connectvars.php';
- $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
- // Set the MySQL timezone
- $query0 = "SET time_zone = 'Europe/Madrid'";
- $query00 = "SET time_zone = '+01:00'";
- $query000 = "SET @@session.time_zone = '+01:00'";
- mysqli_query($dbc, $query0) or die("The time zone could not be set: " . mysqli_error($dbc));
- mysqli_query($dbc, $query00) or die("The time zone could not be set: " . mysqli_error($dbc));
- mysqli_query($dbc, $query000) or die("The time zone could not be set: " . mysqli_error($dbc));
- // If each quantity field has been filled, add this order to the database, send the email and then clear the form
- if (isset($_POST['quantity']))
- {
- /**************************************************************************************************************/
- // QUERY 2
- // Add the order to the orders database (order_id, ordered_by, status, ordered_at)
- $query2 = "INSERT INTO orders (ordered_by, status, ordered_at) VALUES ('$fullname', 'To pay', NOW())";
- mysqli_query($dbc, $query2) or die("The order could not be added to the orders database");
- $insertedOrderID = mysqli_insert_id($dbc);
- /**************************************************************************************************************/
- // QUERY 3
- // Add the order to the order_details database (order_id, product_id, quantity)
- // Create an array to store the keys and values from the form
- $orderDetailsArray = array();
- // Loop through each quantity from the POST, adding it to the orderDetailsArray
- foreach ($quantity as $key => $value)
- {
- $orderDetailsArray[] = "('$insertedOrderID', '$key', '$value')";
- }
- $orderDetailsArray_str = implode(',', $orderDetailsArray);
- // Perform the MySQL query
- $query3 = "INSERT INTO order_details (order_id, product_id, quantity) VALUES $orderDetailsArray_str";
- mysqli_query($dbc, $query3) or die("There was an error: " . mysqli_error($dbc));
- /**************************************************************************************************************/
- // EMAIL 1: Email AGS the invoice
- // Produce the PDF using FPDF
- require '../fpdf/fpdf.php';
- class PDF extends FPDF
- {
- //Page header
- function Header()
- {
- // Define the variables to be global so they can be accessed
- global $insertedOrderID, $nie, $fullname, $address, $postcode;
- // Logo
- $this->Image('../assets/images/agslogo.jpg', 10, 10, 0, 30);
- // Invoice Number
- $this->Cell(130);
- $this->SetTextColor(132, 189, 0);
- $this->SetFont('Arial', 'B', 14);
- $this->Cell(40, 10, 'FACTURA ' . $insertedOrderID . '');
- $this->Ln(2);
- // Add our details
- $this->Cell(130);
- $this->SetFont('Arial', 'I', 10);
- $this->Cell(40, 30, 'AlloyGatorSpain');
- $this->Ln(4);
- $this->Cell(130);
- $this->Cell(40, 30, 'EC23 Calle El Castillo');
- $this->Ln(4);
- $this->Cell(130);
- $this->Cell(40, 30, 'Urb La Heredia');
- $this->Ln(4);
- $this->Cell(130);
- $this->Cell(40, 30, 'Benahavis');
- $this->Ln(4);
- $this->Cell(130);
- $this->Cell(40, 30, '29679');
- $this->Ln(4);
- $this->Cell(130);
- $this->Cell(40, 30, 'CIF HERE');
- $this->Ln(10);
- // Add partner's details
- // Add NIE/CIF
- $this->Cell(40, 30, 'NIE/CIF: ' . $nie);
- $this->Ln(4);
- // Add name
- $this->Cell(40, 30, 'Nombre: ' . $fullname);
- $this->Ln(4);
- // Add address + postcode
- $this->Cell(40, 30, 'Dirección: ' . $address);
- $this->Ln(4);
- $this->Cell(40, 30, 'Código Postal: ' . $postcode);
- $this->Ln(12);
- // Add date
- $this->SetFont('Arial', 'I', 10);
- $date = date("F j, Y");
- $this->Cell(40, 30, 'Fecha: ' . $date);
- $this->SetDrawColor(0, 0, 0);
- }
- // Page footer
- function Footer()
- {
- //Position at 1.5 cm from bottom
- $this->SetY(-15);
- //Arial italic 8
- $this->SetFont('Arial','I',8);
- //Page number
- $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
- }
- }
- $pdf = new PDF();
- $pdf->AddPage();
- $pdf->AliasNbPages();
- $pdf->SetAutoPageBreak(false);
- // Table header
- $pdf->SetFillColor(132, 189, 0);
- $pdf->setFont("Arial", "B", "12");
- $pdf->SetTextColor(91, 103, 112);
- $pdf->setXY(10, 90);
- $pdf->Cell(35, 10, "Product ID", 1, 0, "L", 1);
- $pdf->Cell(35, 10, "Quantity", 1, 0, "L", 1);
- $pdf->Cell(35, 10, "Price (exc VAT)", 1, 0, "L", 1);
- $pdf->Cell(35, 10, "VAT", 1, 0, "L", 1);
- $pdf->Cell(35, 10, "Total", 1, 0, "L", 1);
- // Table body
- $y = 100;
- $x = 10;
- $pdf->setXY($x, $y);
- $pdf->setFont("Arial", "", "12");
- $query4 = "SELECT * FROM order_details INNER JOIN products ON order_details.product_id = products.product_id WHERE order_id = '$insertedOrderID'";
- $query4result = mysqli_query($dbc, $query4) or die(mysqli_error($dbc));
- $quantityCount = 0;
- $priceCount = 0;
- $itemsToReorderArray = array();
- while ($row = mysqli_fetch_assoc($query4result))
- {
- $stock = 0;
- $pdf->Cell(35, 8, $row['product_name'], 1);
- $pdf->Cell(35, 8, $row['quantity'], 1);
- $pdf->Cell(35, 8, EURO . number_format($row['price'] * 0.8 * $row['quantity'], 2), 1);
- $pdf->Cell(35, 8, EURO . number_format($row['price'] * 0.2 * $row['quantity'], 2), 1);
- $pdf->Cell(35, 8, EURO . number_format($row['price'] * $row['quantity'], 2), 1);
- // Set the product ID and new stock level, then insert into the database
- $productId = $row['product_id'];
- $stock = $row['stock'] - $row['quantity'];
- $query5 = "UPDATE products SET stock = '$stock' WHERE product_id = '$productId'";
- mysqli_query($dbc, $query5) or die (mysqli_error($dbc));
- // If the calculated stock is less than or equal to the reorder level, add the product to the
- // $itemsToReorderArray
- if ($stock <= $row['reorder_level'])
- {
- array_push($itemsToReorderArray, array($row["product_id"], $row["product_name"], $stock, $row["reorder_quantity"]));
- }
- // Move to the next row down, setting the quantityCount and priceCount
- $y += 8;
- $quantityCount += $row['quantity'];
- $priceCount += ($row['price'] * $row['quantity']);
- // Insert a page break if needed
- if ($y > 260)
- {
- $pdf->AddPage();
- $y = 40;
- }
- $pdf->setXY($x, $y);
- }
- // For each item in the $itemsToReorderArray, email sales@alloygator.com ordering more stock
- include "../PHPMailer-master/PHPMailerAutoload.php";
- if (!empty($itemsToReorderArray))
- {
- $mail3 = new PHPMailer();
- // Enable SMTP debugging
- $mail3->SMTPDebug = 0;
- // Set debugging output to be HTML (friendly)
- $mail3->Debugoutput = "html";
- // Set PHPMailer to use SMTP
- $mail3->isSMTP();
- // Set SMTP host name
- $mail3->Host = "sub5.mail.dreamhost.com";
- // Set this to true if SMTP host requires authentication to send email
- $mail3->SMTPAuth = true;
- // Provide username and password
- $mail3->Username = "no-reply@alloygatorspain.es";
- $mail3->Password = "Lukeonaldo07!";
- // If SMTP requires TLS encryption then set it
- $mail3->SMTPSecure = "tls";
- // Set TCP port to connect to
- $mail3->Port = 587;
- // Set headers and content
- $mail3->From = "no-reply@alloygatorspain.es";
- $mail3->FromName = "AlloyGatorSpain";
- $mail3->addAddress("web@alloygatorspain.es", "Web - AlloyGatorSpain"); // TO DO Change to sales@alloygator.com
- $mail3->isHTML(true);
- $mail3->Subject = "Request for stock";
- // Get each product and its associated reorder quantity
- $mail3->Body = "Dear Sales, <br /><br />";
- $mail3->Body .= "Please could we order the following products:<br /><br />";
- $mail3->Body .= '<table style="border: 3px #000000; text-align: center;">';
- $mail3->Body .= "<thead>";
- $mail3->Body .= "<tr>";
- $mail3->Body .= "<th>Item</th>";
- $mail3->Body .= "<th>Required Quantity</th>";
- $mail3->Body .= "</tr>";
- $mail3->Body .= "</thead>";
- $mail3->Body .= "<tbody>";
- foreach ($itemsToReorderArray as $v)
- {
- $mail3->Body .= "<tr>";
- $mail3->Body .= "<td>{$v[1]}</td>";
- $mail3->Body .= "<td>{$v[3]}</td>";
- $mail3->Body .= "</tr>";
- // TO DO
- // For each item in the $itemsToReorderArray, add the reorder_quantity to the stock
- $quantityToInsert = $v[2] + $v[3];
- $query6 = "UPDATE products SET stock = '$quantityToInsert' WHERE product_id = '$v[0]'";
- $q6result = mysqli_query($dbc, $query6) or die(mysqli_error($dbc));
- }
- $mail3->Body .= "</tbody>";
- $mail3->Body .= "</table><br /><br />";
- $mail3->Body .= "Thanks,<br />";
- $mail3->Body .= "AlloyGatorSpain";
- // Send the email
- $mail3->send();
- }
- // See how big the order is and set postage accordingly
- // TO DO: Adjust postage for Balearics
- if ($quantityCount >= 10)
- {
- $postage = 14;
- }
- else
- {
- $postage = 9;
- }
- // Add postage
- $pdf->Cell(35, 8, "Postage", 1);
- $pdf->Cell(35, 8, "1", 1);
- $pdf->Cell(35, 8, EURO . number_format($postage * 0.8, 2), 1);
- $pdf->Cell(35, 8, EURO . number_format($postage * 0.2, 2), 1);
- $pdf->Cell(35, 8, EURO . number_format($postage, 2), 1);
- $y += 8;
- if ($y > 260)
- {
- $pdf->AddPage();
- $y = 40;
- }
- $pdf->setXY($x, $y);
- // Blank row
- $pdf->Cell(175, 8, "", 1);
- $y += 8;
- if ($y > 260)
- {
- $pdf->AddPage();
- $y = 40;
- }
- $pdf->setXY($x, $y);
- // Total row
- $pdf->Cell(70, 8, "TOTAL", 1);
- $pdf->Cell(35, 8, EURO . number_format($priceCount * 0.8, 2), 1);
- $pdf->Cell(35, 8, EURO . number_format($priceCount * 0.2, 2), 1);
- $pdf->Cell(35, 8, EURO . number_format($priceCount, 2), 1);
- $y += 16;
- if ($y > 260)
- {
- $pdf->AddPage();
- $y = 40;
- }
- $pdf->setXY($x, $y);
- // Insert the rest of the body (our payment details, NIE/CIF)
- $pdf->SetFont('Arial', 'B', 14);
- $pdf->Cell(0, 15, 'Our payment details:', 0, 0, 'L', false);
- $y += 8;
- if ($y > 260)
- {
- $pdf->AddPage();
- $y = 40;
- }
- $pdf->setXY($x, $y);
- $pdf->SetFont('Arial', '', 12);
- $pdf->Cell(0, 15, 'Sort code: 11-06-41', 0, 0, 'L', false);
- $y += 8;
- if ($y > 260)
- {
- $pdf->AddPage();
- $y = 40;
- }
- $pdf->setXY($x, $y);
- $pdf->SetFont('Arial', '', 12);
- $pdf->Cell(0, 15, 'Account number: 12345678', 0, 0, 'L', false);
- $y += 8;
- if ($y > 260)
- {
- $pdf->AddPage();
- $y = 40;
- }
- $pdf->setXY($x, $y);
- $pdf->SetFont('Arial', '', 12);
- $pdf->Cell(0, 15, 'PayPal: lukecolli98@gmail.com', 0, 0, 'L', false);
- $y += 8;
- if ($y > 260)
- {
- $pdf->AddPage();
- $y = 40;
- }
- $pdf->setXY($x, $y);
- $doc = $pdf->Output('S');
- // DEAL WITH THE EMAIL
- $mail = new PHPMailer();
- // Enable SMTP debugging
- $mail->SMTPDebug = 0;
- // Set debugging output to be HTML (friendly)
- $mail->Debugoutput = "html";
- // Set PHPMailer to use SMTP
- $mail->isSMTP();
- // Set SMTP host name
- $mail->Host = "sub5.mail.dreamhost.com";
- // Set this to true if SMTP host requires authentication to send email
- $mail->SMTPAuth = true;
- // Provide username and password
- $mail->Username = "no-reply@alloygatorspain.es";
- $mail->Password = "Lukeonaldo07!";
- // If SMTP requires TLS encryption then set it
- $mail->SMTPSecure = "tls";
- // Set TCP port to connect to
- $mail->Port = 587;
- // Set headers and content
- $mail->From = "no-reply@alloygatorspain.es";
- $mail->FromName = "AlloyGatorSpain";
- $mail->addAddress("web@alloygatorspain.es", "Web - AlloyGatorSpain"); // Change to Ben and David
- $mail->isHTML(true);
- $mail->Subject = "Invoice " . $insertedOrderID;
- $mail->Body = "A partner has ordered some more stock. The details are below. Once you receive payment, please mark the order as paid and then process the order. You will <strong>not</strong> receive a notification when the partner pays. A copy of the invoice is attached.";
- $mail->AltBody = "A partner has ordered some more stock. The details are below. Once you receive payment, please mark the order as paid and then process the order. You will *not* receive a notification when the partner pays. A copy of the invoice is attached.";
- // Add the attachment
- $mail->AddStringAttachment($doc, 'invoice.pdf', 'base64', 'application/pdf');
- // Send the email
- if(!$mail->send())
- {
- $result = '<div class="alert alert-danger">' . $lang['ERROR'] . '</div>';
- }
- else
- {
- $result = '<div class="alert alert-success">' . $lang['PAY_FOR_ORDER'] . '</div>';
- }
- /**************************************************************************************************************/
- // EMAIL 2: Email the partner the invoice
- $mail2 = new PHPMailer();
- // Enable SMTP debugging
- $mail2->SMTPDebug = 0;
- // Set debugging output to be HTML (friendly)
- $mail2->Debugoutput = "html";
- // Set PHPMailer to use SMTP
- $mail2->isSMTP();
- // Set SMTP host name
- $mail2->Host = "sub5.mail.dreamhost.com";
- // Set this to true if SMTP host requires authentication to send email
- $mail2->SMTPAuth = true;
- // Provide username and password
- $mail2->Username = "no-reply@alloygatorspain.es";
- $mail2->Password = "Lukeonaldo07!";
- // If SMTP requires TLS encryption then set it
- $mail2->SMTPSecure = "tls";
- // Set TCP port to connect to
- $mail2->Port = 587;
- // Set headers and content
- $mail2->From = "no-reply@alloygatorspain.es";
- $mail2->FromName = "AlloyGatorSpain";
- $mail2->addAddress("web@alloygatorspain.es", "Web - AlloyGatorSpain");
- $mail2->isHTML(true);
- $mail2->Subject = "Invoice " . $insertedOrderID;
- $mail2->Body = $lang['EMAIL_BODY'];
- // Add the attachment
- $mail2->AddStringAttachment($doc, 'invoice.pdf', 'base64', 'application/pdf');
- // Send the email
- $mail2->send();
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <?php
- // Set page title
- $page_title = "Purchase Products - AlloyGatorSpain";
- // Everything inside HTML <head> tags
- include '../includes/header.php';
- ?>
- </head>
- <body>
- <?php
- // Page header (above navbar)
- include '../includes/page_header.php';
- // Navbar
- include '../includes/navbar.php';
- // Carousel
- include '../includes/carousels/homeCarousel.php';
- ?>
- <!-- ===== SPACER ===== -->
- <br />
- <!-- ===== PAGE CONTENT ===== -->
- <div class="container text-center">
- <!-- First row -->
- <div class="row">
- <div class="col-sm-12">
- <h1><?php echo $lang['PURCHASE_TITLE']; ?></h1>
- <?php echo $lang['PURCHASE_1']; ?>
- </div>
- </div>
- <br />
- <!-- Second row -->
- <div class="row">
- <!-- Order form -->
- <div class="col-sm-12">
- <form class="form-horizontal" role="form" method="post" action="">
- <div class="form-group">
- <div class="col-sm-12">
- <?php
- echo $result;
- ?>
- </div>
- </div>
- <?php
- // Connect to the database
- include '../connectvars.php';
- $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
- // Retrieve the products from MySQL
- $query = "SELECT * FROM products WHERE active = 1 ORDER BY product_id ASC";
- $data = mysqli_query($dbc, $query);
- // Loop through the array of products, formatting it as a HTML table
- echo '<table class="table table-hover purchaseProductsTable">
- <tr>
- <th>' . $lang['PRODUCT_NAME'] . '</th>
- <th>' . $lang['PRICE'] . '</th>
- <th>' . $lang['DESCRIPTION'] . '</th>
- <th>' . $lang['QUANTITY'] . '</th>
- <th>' . $lang['COST'] . '</th>
- </tr>
- ';
- while ($row = mysqli_fetch_array($data))
- {
- // Display the data
- echo '<tr>';
- echo '<td id="product_name">' . $row['product_name'] . '</td>';
- echo '<td id="price-holder">€<span id="price">' . $row['price'] . '</span></td>';
- echo '<td id="description">' . $row['description'] . '</td>';
- echo '<td id="quantity1" oninput="calculate()"><input type="number" min="0" class="form-control" id="quantity" name="quantity['.$row['product_id'].']" placeholder="' . $lang['CHOOSE_QUAN'] . '"></td>';
- echo '<td id="cost">';
- ?>
- <script>
- function calculate()
- {
- var price = parseFloat(document.getElementById('price').innerHTML);
- var quantity = parseFloat(document.getElementById('quantity').value);
- document.getElementById('cost').innerHTML = "€" + (price * quantity);
- }
- </script>
- <?php
- echo '</td>';
- echo '</tr>';
- }
- echo '<tr>';
- echo '<td></td>';
- echo '<td></td>';
- echo '<td></td>';
- echo '<td><strong>Total:</strong></td>';
- echo '<td id="total_cost">TOTAL</td>';
- echo '</tr>';
- echo '</table>';
- ?>
- <!-- SUBMIT -->
- <div class="form-group">
- <div class="col-sm-12">
- <input id="submit" name="submit" type="submit" value="<?php echo $lang['SUBMIT_ORDER']; ?>" class="btn btn-primary">
- </div>
- </div>
- </form>
- </div>
- </div>
- </div>
- <!-- ===== SPACER ===== -->
- <br />
- <?php
- // Footer
- include '../includes/footer.php';
- ?>
- </body>
- </html>
Add Comment
Please, Sign In to add comment