prepare_input($password), $db->prepare_input($username)); $manufacturer = $db->Execute($query); // If not exists, abort script if($manufacturer->RecordCount()<1) { exit('Login error'); } } /////////////////////////////////////////////////////////////////////////// // Ok, passed access validation $manufacturers_id = $manufacturer->fields['manufacturers_id']; $manufacturers_name = $manufacturer->fields['manufacturers_name']; $mailing_time = $manufacturer->fields['mailing_time']; // 13:01:59 $strHeaderDate = substr($mailing_time, 0, 5) . date(' l, jS F Y'); /////////////////////////////////////////////////////////////////////////// // Handle radio button selections if ($_POST) { if (!empty($_POST['resetTestOrder'])) { resetTestOrder(); } if (1) { // Should be first to run setNoLongerAvailable(); } if (1) { setShippingToday(); } if (1) { setWillOrderToday(); } if (1) { packingSlips(); } if (1) { updateOrdersStatus(); } } /////////////////////////////////////////////////////////////////////////// /* Set product status to Off Email the admin Email the customer */ function setNoLongerAvailable() { global $db; global $manufacturers_id; global $SQL_updateProductsStatus; global $SQL_selectDiscontinuedInfo; global $SQL_updateOrdersProductsStatus; global $SQL_updateOrdersStatusHistory; // Find product ids marked No Longer Available $productIds = array(); $ordersProductIds = array(); foreach($_POST['products_id'] as $rowIndex => $products_id) { if($_POST['productAction'.$rowIndex]=='setNoLongerAvailable') { $productIds[$products_id] = $products_id; $ordersProductIds[] = $_POST['orders_products_id'][$rowIndex]; } } if (!sizeof($productIds)) { return; } // Find order ids marked as no longer available $orderIds = array(); $ordersProductIds = array(); foreach($_POST['orders_id'] as $rowIndex => $orders_id) { if($_POST['productAction'.$rowIndex]=='setNoLongerAvailable') { $orderIds[$orders_id] = $orders_id; $ordersProductIds[] = $_POST['orders_products_id'][$rowIndex]; } } if (!sizeof($orderIds)) { return; } // Set products status to Off $status = 0; // Off $query = sprintf($SQL_updateProductsStatus, $status, implode($productIds, ','), $manufacturers_id); $db->Execute($query); debugTrace($query, 'SQL_updateProductsStatus'); // Set orders_products_status to Not available $status = 4; // Not available $query = sprintf($SQL_updateOrdersProductsStatus, $status, implode($ordersProductIds, ','), $manufacturers_id); $db->Execute($query); debugTrace($query, 'SQL_updateOrdersProductsStatus'); //Set orders_status to Part Refund Needed /*$status = 147; $query = sprintf($SQL_updateOrdersStatus, $status, implode($orderIds, ','), $manufacturers_id); $db->Execute($query); debugTrace($query, 'SQL_updateOrdersStatus');*/ // Get products info and email admin, email customer $query = sprintf($SQL_selectDiscontinuedInfo, implode($ordersProductIds, ',')); $info = $db->Execute($query); debugTrace($query, 'SQL_selectDiscontinuedInfo'); while (!$info->EOF) { debugTrace($info->fields, 'Discontinued Info'); $products_name = $info->fields['products_name']; $products_model = $info->fields['products_model']; $manufacturers_name = $info->fields['manufacturers_name']; $customers_name = $info->fields['customers_name']; $customers_email_address = $info->fields['customers_email_address']; //Update zen_orders_status_history for ZC admin /*$comment = "Update from Manufacturer($manufacturers_name): Refund Needed for $products_name [$products_model]"; $timestampInSeconds = $_SERVER['REQUEST_TIME']; $mySqlDateTime= date("Y-m-d H:i:s", $timestampInSeconds); $query = sprintf($SQL_updateOrdersStatusHistory, implode($orderIds, ','), $status, $mySqlDateTime, '1', $comment); $db->Execute($query); debugTrace($query, 'SQL_updateOrdersStatusHistory');*/ // Admin $subject = "ITEM DISCONTINUED: $products_name | $products_model | $manufacturers_name"; $message = "The following item has been discontinued:\nName: $products_name\nModel: $products_model\nManufacturer: $manufacturers_name"; send_email( $subject, $message, 'admin', 'admin@fancydressheaven.co.uk', EMAIL_FROM_NAME, EMAIL_FROM_ADDRESS ); debugTrace($message, $subject); // Customer $subject = "Item is no longer available"; $message = "Dear $customers_name,\n\nThe following item has been discontinued:\nName: $products_name\nModel: $products_model\nManufacturer: $manufacturers_name"; send_email( $subject, $message, $customers_name, $customers_email_address, EMAIL_FROM_NAME, EMAIL_FROM_ADDRESS ); debugTrace($message, $subject); $info->MoveNext(); } } /* Set orders_products_status as Shipped Email the customers Email packing slip to manufacturer */ function setShippingToday() { global $db; global $manufacturers_id; global $SQL_updateOrdersProductsStatus; global $SQL_updateOrdersStatusHistory; global $SQL_selectDiscontinuedInfo; // Find order ids marked for shipping $orderIds = array(); $ordersProductIds = array(); foreach($_POST['orders_id'] as $rowIndex => $orders_id) { if($_POST['productAction'.$rowIndex]=='setShippingToday') { $orderIds[$orders_id] = $orders_id; $ordersProductIds[] = $_POST['orders_products_id'][$rowIndex]; } } if (!sizeof($orderIds)) { return; } // Update orders_products_status in the database $status = 3; // Shipped $query = sprintf($SQL_updateOrdersProductsStatus, $status, implode($ordersProductIds, ','), $manufacturers_id); $db->Execute($query); debugTrace($query); // Get available products of an order $queryTmp = " SELECT op.products_quantity, op.products_name, o.customers_email_address, o.customers_name FROM ".TABLE_ORDERS." o, ".TABLE_PRODUCTS." p, ".TABLE_ORDERS_PRODUCTS." op WHERE o.orders_id = op.orders_id AND p.products_id = op.products_id AND p.products_status = '1' AND op.orders_products_status != '4' AND p.manufacturers_id = '%s' AND o.orders_id = '%s' AND o.date_purchased > '".CUTOFF_ORDER_DATE."' "; // For each order, email the customer a product list foreach($orderIds as $orders_id) { // Get the order's products $query = sprintf($queryTmp, $manufacturers_id, $orders_id); $orderedProducts = $db->Execute($query); debugTrace($query); // Create email message product list $message = "Dear Customer,\n\nHere are the details of your order (No. $orders_id):"; $message .= "\n\nQuantity - Item Name"; while (!$orderedProducts->EOF) { debugTrace($orderedProducts->fields); $message .= sprintf("\n%7sx %s", $orderedProducts->fields['products_quantity'], $orderedProducts->fields['products_name']); // Get email address here $customers_name = $orderedProducts->fields['customers_name']; $customers_email_address = $orderedProducts->fields['customers_email_address']; $orderedProducts->MoveNext(); } // Send email $subject = EMAIL_CUSTOMER_PRODUCTLIST_SUBJECT; send_email( $subject, $message, $customers_name, $customers_email_address, EMAIL_FROM_NAME, EMAIL_FROM_ADDRESS ); debugTrace($message); } } /* Set orders_products_status as Ordered Email the customers */ function setWillOrderToday() { global $db; global $manufacturers_id; global $SQL_updateOrdersProductsStatus; global $SQL_updateOrdersStatusHistory; global $SQL_updateProductsQuantity; global $SQL_updateOrdersStatus; // Find items marked "Will order today" $orderIds = array(); $ordersProductIds = array(); foreach($_POST['orders_id'] as $rowIndex => $orders_id) { if($_POST['productAction'.$rowIndex]=='setWillOrderToday') { $orderIds[$orders_id] = $orders_id; $ordersProductIds[] = $_POST['orders_products_id'][$rowIndex]; } } if (!sizeof($orderIds)) { return; } // Set orders_products_status to Ordered $status = 2; // Ordered $query = sprintf($SQL_updateOrdersProductsStatus, $status, implode($ordersProductIds, ','), $manufacturers_id); $db->Execute($query); debugTrace($query); // Set products_quantity to 0 $quantity = 0; $query = sprintf($SQL_updateProductsQuantity, $quantity, implode($ordersProductIds, ','), $manufacturers_id); $db->Execute($query); debugTrace($query); //Set orders_status to Part on way to supplier $status = 133; $query = sprintf($SQL_updateOrdersStatus, $status, implode($orderIds, ','), $manufacturers_id); $db->Execute($query); debugTrace($query); // Get available products of an order $queryTmp = " SELECT op.products_quantity, op.products_name, o.customers_email_address, o.customers_name FROM ".TABLE_ORDERS." o, ".TABLE_PRODUCTS." p, ".TABLE_ORDERS_PRODUCTS." op WHERE o.orders_id = op.orders_id AND p.products_id = op.products_id AND p.products_status = '1' AND op.orders_products_status = '2' AND p.manufacturers_id = '%s' AND o.orders_id = '%s' AND o.date_purchased > '".CUTOFF_ORDER_DATE."' "; // For each order, email the customer foreach($orderIds as $orders_id) { // Get order's products $query = sprintf($queryTmp, $manufacturers_id, $orders_id); $orderedProducts = $db->Execute($query); debugTrace($query); // Create email message product list $message = "Dear Customer,\n\nThe following item(s) have been ordered for you from our supplier.We will let you know when they are dispatched to you."; $message .= "\n\nQuantity - Item Name"; while (!$orderedProducts->EOF) { debugTrace($orderedProducts->fields); $message .= sprintf("\n%7sx %s", $orderedProducts->fields['products_quantity'], $orderedProducts->fields['products_name']); // Get email address here $customers_name = $orderedProducts->fields['customers_name']; $customers_email_address = $orderedProducts->fields['customers_email_address']; $orderedProducts->MoveNext(); } // Send email $subject = "Your order No. $orders_id"; send_email( $subject, $message, $customers_name, $customers_email_address, EMAIL_FROM_NAME, EMAIL_FROM_ADDRESS ); debugTrace($message); } $query2 = " SELECT op.products_quantity, op.products_name, op.products_model FROM ".TABLE_ORDERS." o, ".TABLE_PRODUCTS." p, ".TABLE_ORDERS_PRODUCTS." op WHERE o.orders_id = op.orders_id AND p.products_id = op.products_id AND p.products_status = '1' AND op.orders_products_status = '2' AND p.manufacturers_id = '%s' AND o.orders_id = '%s' "; foreach($orderIds as $orders_id) { // Get order's products $query = sprintf($query2, $manufacturers_id, $orders_id); $orderedProducts = $db->Execute($query); debugTrace($query); //Update orders status history $message2 = "The following item(s) have been ordered for you from our supplier (ID: $manufacturers_id). We will let you know when they are dispatched to you."; while (!$orderedProducts->EOF) { debugTrace($orderedProducts->fields); $message2 .= sprintf("\n%7sx %s[%s]", $orderedProducts->fields['products_quantity'], $orderedProducts->fields['products_name'], $orderedProducts->fields['products_model']); $orderedProducts->MoveNext(); } $status=133; $timestampInSeconds = $_SERVER['REQUEST_TIME']; $mySqlDateTime= date("Y-m-d H:i:s", $timestampInSeconds); $query3 = sprintf($SQL_updateOrdersStatusHistory, $orders_id, $status, $mySqlDateTime, '1', mysql_real_escape_string($message2)); $db->Execute($query3); } } /* Create and Email packing slips */ function packingSlips() { global $db; global $manufacturers_id; global $SQL_selectManufacturer2; global $SQL_selectOrderProducts2; global $SQL_selectSumProductsQuantity; $toOrderIds = array(); $toShipIds = array(); foreach($_POST['orders_products_id'] as $rowIndex => $orders_products_id) { if($_POST['productAction'.$rowIndex]=='setWillOrderToday') { $toOrderIds[] = $orders_products_id; } if($_POST['productAction'.$rowIndex]=='setShippingToday') { $toShipIds[] = $orders_products_id; } } debugTrace($toOrderIds, 'toOrderIds'); debugTrace($toShipIds, 'toShipIds'); // Get sums of product quantites (for "order today" page) $prodQtySums = false; if (sizeof($toOrderIds)) { $query = sprintf($SQL_selectSumProductsQuantity, implode($toOrderIds, ',')); $prodQtySums = $db->Execute($query); debugTrace($query, 'SQL_selectSumProductsQuantity'); } // Get the order products $ordersProductsIds = array_merge($toOrderIds, $toShipIds); debugTrace($ordersProductsIds, 'ordersProductsIds'); if (!sizeof($ordersProductsIds)) { return; } $query = sprintf($SQL_selectOrderProducts2, $manufacturers_id, implode($ordersProductsIds, ',')); $orderedProducts = $db->Execute($query); debugTrace($query, 'SQL_selectOrderProducts2'); // Create packing slip $packingSlip = null; $settings = array( 'pslip_title' => PSLIP_TITLE, 'packingslip_filename' => PACKINGSLIP_FILENAME . date('-Y-m-d'), ); debugTrace($settings, 'packingSlip settings'); createPackingSlip($orderedProducts, $packingSlip, $settings, $prodQtySums); // Get manufacturer's info $query = sprintf($SQL_selectManufacturer2, $manufacturers_id); $manufacturer = $db->Execute($query); debugTrace($query, 'SQL_selectManufacturer2'); // Prepare email message $message = PANEL_PACKINGSLIP_EMAIL_MESSAGE; debugTrace($message); // Email packing slip $subject = EMAIL_PACKINGSLIP_SUBJECT; $email_error = emailPackingSlip( $packingSlip, $subject, $message, $manufacturer->fields['manufacturers_name'], $manufacturer->fields['manufacturers_email'], EMAIL_FROM_NAME, EMAIL_FROM_ADDRESS ); debugTrace("email : $message "); debugTrace("email_error: $email_error "); } /* Set an order_status to Delivered once all products were set as Shipped or Not available */ function updateOrdersStatus() { global $db; // Check each posted order $orderIds =$_POST['orders_id']; $orderIds = array_unique($orderIds); debugTrace($orderIds); foreach($orderIds as $orders_id) { // Count order's number of products $query = "SELECT COUNT(*) as theCount FROM ".TABLE_ORDERS_PRODUCTS." WHERE orders_id='$orders_id' "; $row = $db->Execute($query); debugTrace($query); $count1 = $row->fields['theCount']; // Count how many are shipped (or disontinued) $query = "SELECT COUNT(*) as theCount FROM ".TABLE_ORDERS_PRODUCTS." WHERE orders_id='$orders_id' AND orders_products_status in (3,5)"; $row = $db->Execute($query); debugTrace($query); $count2 = $row->fields['theCount']; debugTrace("$count1 - $count2"); // If counts match order is completed if ($count1 && $count1==$count2) { $query = "UPDATE ".TABLE_ORDERS." SET orders_status = '3' WHERE orders_id='$orders_id' "; debugTrace($query); $db->Execute($query); } } } /* Display the manufacturer's products (of pending orders) */ function showOrderedProducts() { global $db; global $manufacturers_id; global $SQL_selectOrderComments; $queryTmp = " SELECT op.products_quantity, op.products_name, op.products_model, op.products_id, op.orders_products_id, op.orders_products_status, o.date_purchased, o.orders_id, o.shipping_method FROM ".TABLE_ORDERS." o, ".TABLE_PRODUCTS." p, ".TABLE_ORDERS_PRODUCTS." op WHERE o.orders_id = op.orders_id AND p.products_id = op.products_id AND (o.orders_status = '2' OR o.orders_status = '129' OR o.orders_status = '133') AND op.orders_products_status != '4' /* exclude not available */ AND op.orders_products_status != '3' /* exclude shipped */ AND p.manufacturers_id = '%s' AND o.date_purchased > '".CUTOFF_ORDER_DATE."' ORDER BY o.date_purchased DESC, op.products_model, op.orders_id "; $rowIndex = 0; $query = sprintf($queryTmp, $db->prepare_input($manufacturers_id)); $orders = $db->Execute($query); debugTrace($query); while (!$orders->EOF) { if (DEBUG_MAX_PRODUCTS && $rowIndex >= DEBUG_MAX_PRODUCTS) { break; } debugTrace($orders->fields); $products_name = htmlentities($orders->fields['products_name']); $products_model = htmlentities($orders->fields['products_model']); $products_quantity = $orders->fields['products_quantity']; $products_id = $orders->fields['products_id']; $orders_id = $orders->fields['orders_id']; $orders_products_id = $orders->fields['orders_products_id']; $shipping_method = htmlentities($orders->fields['shipping_method']); $query = sprintf($SQL_selectOrderComments, $orders_id, 1); $comments = $db->Execute($query); $comment = htmlentities($comments->fields['comments']); $productAttributes = getProductAttributes($orders_products_id); $wasOrdered = ($orders->fields['orders_products_status']==2); if ($wasOrdered) { $radioNoLongerAvailable = ""; $productAttributes .= '
ORDERED - AWAITING ARRIVAL
'; } else { $radioNoLongerAvailable = ""; } $radioWillOrderToday = ""; if ($wasOrdered) { $radioWillOrderToday = ""; // $radioWillOrderToday .= '