272 lines
9.3 KiB
PHP
272 lines
9.3 KiB
PHP
|
<?php
|
||
|
|
||
|
include_once(_PS_ROOT_DIR_.'/classes/ExpeditorModule.php');
|
||
|
|
||
|
class AdminExpeditor extends AdminTab
|
||
|
{
|
||
|
private $columnName = 'ReferenceExpedition';
|
||
|
private $prefix = 'EXP';
|
||
|
private $displayInfos = true;
|
||
|
|
||
|
public function __construct()
|
||
|
{
|
||
|
$this->table = 'expeditor';
|
||
|
$this->className = 'ExpeditorModule';
|
||
|
|
||
|
parent::__construct();
|
||
|
}
|
||
|
|
||
|
private function _treatCsvFile($file)
|
||
|
{
|
||
|
global $cookie;
|
||
|
|
||
|
/*
|
||
|
* get all informations of CSV file
|
||
|
*/
|
||
|
$row = 0;
|
||
|
$infos = array();
|
||
|
$handle = fopen($file, "r");
|
||
|
while (($data = fgetcsv($handle, 1000, ';', '"')) !== FALSE)
|
||
|
{
|
||
|
for ($c=0; $c < sizeof($data); $c++)
|
||
|
{
|
||
|
$infos[$row][$c] = $data[$c];
|
||
|
}
|
||
|
$row++;
|
||
|
}
|
||
|
/*
|
||
|
* Extract all needings informations
|
||
|
*/
|
||
|
|
||
|
|
||
|
if (($keyId = array_search($this->columnName, $infos[0])) === false)
|
||
|
return false;
|
||
|
$keyShippingNumber = array_search('NumeroColis', $infos[0]);
|
||
|
|
||
|
echo '<p>' . $this->l('List of orders recognized') . '</p>';
|
||
|
echo '<ul>';
|
||
|
unset($infos[0]); // delete csv header
|
||
|
foreach ($infos as $info)
|
||
|
{
|
||
|
$id_order = eregi_replace($this->prefix, '', $info[$keyId]);
|
||
|
$shipping_number = $info[$keyShippingNumber];
|
||
|
|
||
|
$id_expeditor = ExpeditorModule::getByIdOrder($id_order);
|
||
|
|
||
|
if (!is_numeric($id_expeditor)) continue;
|
||
|
$expeditor = new ExpeditorModule($id_expeditor);
|
||
|
$expeditor->is_send = 1;
|
||
|
$expeditor->save();
|
||
|
|
||
|
$order = new Order($id_order);
|
||
|
// Avoid to make multiple submissions
|
||
|
if($order->shipping_number == '')
|
||
|
{
|
||
|
$order->shipping_number = $shipping_number;
|
||
|
$order->update();
|
||
|
|
||
|
$customer = new Customer($order->id_customer);
|
||
|
$carrier = new Carrier(intval($order->id_carrier), intval($order->id_lang));
|
||
|
$templateVars = array('{followup}' => str_replace('@', $shipping_number, $carrier->url));
|
||
|
|
||
|
$history = new OrderHistory();
|
||
|
$history->id_order = $id_order;
|
||
|
$history->changeIdOrderState(Configuration::get('EXPEDITOR_STATE_IMP'), $id_order); // Shipping
|
||
|
$history->id_employee = intval($cookie->id_employee);
|
||
|
$history->addWithemail(true, $templateVars);
|
||
|
|
||
|
global $_LANGMAIL;
|
||
|
$templateVars = array(
|
||
|
'{followup}' => str_replace('@', $order->shipping_number, $carrier->url),
|
||
|
'{firstname}' => $customer->firstname,
|
||
|
'{lastname}' => $customer->lastname,
|
||
|
'{id_order}' => intval($order->id)
|
||
|
);
|
||
|
//send email with traking number
|
||
|
$subject = 'Package in transit';
|
||
|
if (Mail::Send(intval($order->id_lang), 'in_transit', ((is_array($_LANGMAIL) AND key_exists($subject, $_LANGMAIL)) ? $_LANGMAIL[$subject] : $subject), $templateVars, $customer->email, $customer->firstname.' '.$customer->lastname))
|
||
|
echo '<br/>' . $this->l('Email send to') . ': ' . $customer->email;
|
||
|
else
|
||
|
echo '<br/>' . $this->l('Email faild to') . ': ' . $customer->email;
|
||
|
echo '<li>' . $this->l('Order number') . ': ' . $id_order;
|
||
|
echo '<br/>' . $this->l('Shipping number') . ': ' . $shipping_number;
|
||
|
echo "</li>\n";
|
||
|
}
|
||
|
}
|
||
|
echo '</ul>';
|
||
|
echo '
|
||
|
<form action="index.php" method="get">
|
||
|
<p>
|
||
|
<input type="hidden" name="tab" value="AdminExpeditor" />
|
||
|
<input type="hidden" name="token" value="'.Tools::getValue('token').'" />
|
||
|
<input type="submit" value="'.$this->l(' Back ').'" class="button" />
|
||
|
</p>
|
||
|
</form>';
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
private function _importNumber()
|
||
|
{
|
||
|
$file = $_FILES['file']['tmp_name'];
|
||
|
if ($html = $this->_treatCsvFile($file))
|
||
|
{
|
||
|
$this->displayInfos = false;
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
private function _generateCsvFile()
|
||
|
{
|
||
|
foreach($_POST['order'] as $field)
|
||
|
{
|
||
|
if (is_numeric($field['weight']) AND $field['weight'] > 0)
|
||
|
{
|
||
|
$id_expeditor = ExpeditorModule::getByIdOrder($field['id']);
|
||
|
$expeditor = new ExpeditorModule($id_expeditor);
|
||
|
$expeditor->id_order = $field['id'];
|
||
|
$expeditor->weight = $field['weight'];
|
||
|
$expeditor->standard_size = 0;
|
||
|
if ($field['standard_size'] == '0')
|
||
|
$expeditor->standard_size = 1;
|
||
|
$expeditor->save();
|
||
|
}
|
||
|
}
|
||
|
header('Location: http://'. htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8') . __PS_BASE_URI__ .'modules/expeditor/getCsv.php');
|
||
|
}
|
||
|
|
||
|
private function _postProcess()
|
||
|
{
|
||
|
if (isset($_POST['generate']))
|
||
|
{
|
||
|
$this->_generateCsvFile();
|
||
|
}
|
||
|
else if (isset($_POST['import']))
|
||
|
{
|
||
|
if (empty($_FILES['file']['tmp_name']) OR !isset($_FILES['file']))
|
||
|
{
|
||
|
return '<p class="alert">' . $this->l('Please upload a CSV file.') . '</p>';
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (!$this->_importNumber())
|
||
|
return '<p class="alert">'.$this->l('Column').' '.$this->columnName.' '.$this->l('is required').'.'.'</p>';
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function displayOrdersTable()
|
||
|
{
|
||
|
global $cookie;
|
||
|
|
||
|
$order_state = new OrderState(Configuration::get('EXPEDITOR_STATE_EXP'));
|
||
|
|
||
|
$html = '';
|
||
|
$html.= '<p>'.$this->l('All orders which have the state').' "<b>'.$order_state->name[$cookie->id_lang].'</b>"';
|
||
|
|
||
|
foreach(explode(',',Configuration::get('EXPEDITOR_CARRIER')) as $val ) {
|
||
|
$carrier = new Carrier($val);
|
||
|
if ($carrier->id) $html.= ' '.$this->l('or the carrier').' "<b>'.$carrier->name.'</b>"';
|
||
|
}
|
||
|
$html.= '. <p><a href="index.php?tab=AdminModules&configure=expeditor&token='.Tools::getAdminToken('AdminModules'.intval(Tab::getIdFromClassName('AdminModules')).intval($cookie->id_employee)).'" class="green">' . $this->l('Change configuration') . '</a></p>';
|
||
|
|
||
|
$orders = ExpeditorModule::getOrders();
|
||
|
if (empty($orders))
|
||
|
{
|
||
|
$html.= '<h3>' . $this->l('No orders with this state.') . '</h3>';
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$html.= '<form method="post" action="'.$_SERVER['REQUEST_URI'].'">';
|
||
|
$html.= "\n<table class=\"table\" cellpadding=\"0\" cellspacing=\"0\">";
|
||
|
$html.= '<tr>';
|
||
|
$html.= '<th>' . $this->l('Order ID') . '</th>';
|
||
|
$html.= '<th>' . $this->l('Customer') . '</th>';
|
||
|
$html.= '<th>' . $this->l('Total price') . '</th>';
|
||
|
$html.= '<th>' . $this->l('Total shipping') . '</th>';
|
||
|
$html.= '<th>' . $this->l('Date') . '</th>';
|
||
|
$html.= '<th>' . $this->l('Weight (grams)') . '</th>';
|
||
|
$html.= '<th>' . $this->l('Non-standard size') . '</th>';
|
||
|
$html.= '<th>' . $this->l('Detail') . '</th>';
|
||
|
$html.= '</tr>';
|
||
|
foreach ($orders as $order)
|
||
|
{
|
||
|
$oOrder = new Order($order['id_order']);
|
||
|
|
||
|
$products = $oOrder->getProducts();
|
||
|
$total_weight = 0;
|
||
|
/* Antadis */
|
||
|
$in_data = Db::getInstance()->ExecuteS('SELECT weight FROM `'._DB_PREFIX_.'expeditor` WHERE id_order = '.$order['id_order']);
|
||
|
if(count($in_data) > 0) {
|
||
|
$total_weight = $in_data[0]['weight'];
|
||
|
} else {
|
||
|
foreach($products AS $product)
|
||
|
$total_weight += $product['product_weight'] * $product['product_quantity'];
|
||
|
}
|
||
|
|
||
|
$html.= "\n<tr>";
|
||
|
$html.= '<td>' . $order['id_order'] . '<input type="hidden" name="order[' . $order['id_order'] . '][id]" id="id_order_' . $order['id_order'] . '" value="' . $order['id_order'] . '" /></td>';
|
||
|
$html.= '<td>' . $order['customer'] . '</td>';
|
||
|
$html.= '<td>' . Tools::displayPrice($order['total'], new Currency($order['id_currency'])) . '</td>';
|
||
|
$html.= '<td>' . Tools::displayPrice($order['shipping'], new Currency($order['id_currency'])) . '</td>';
|
||
|
$html.= '<td>' . Tools::displayDate($order['date'], $order['id_lang']) . '</td>';
|
||
|
$html.= '<td><input type="text" name="order[' . $order['id_order'] . '][weight]" id="weight_' . $order['id_order'] . '" size="8" value="';
|
||
|
if (($total_weight * Configuration::get('EXPEDITOR_MULTIPLY')) == 0)
|
||
|
$html.= '1';
|
||
|
else {
|
||
|
if(count($in_data) > 0) {
|
||
|
$html.= $total_weight;
|
||
|
} else {
|
||
|
$html.= ($total_weight * Configuration::get('EXPEDITOR_MULTIPLY'));
|
||
|
}
|
||
|
}
|
||
|
$html.= '" /></td>';
|
||
|
$html.= '<td><input type="checkbox" name="order[' . $order['id_order'] . '][standard_size]" id="standard_size_' . $order['id_order'] . '" value="0" /><label for="standard_size_' . $order['id_order'] . '" class="t"> ' . $this->l('tick this') . '</label></td>';
|
||
|
$html.= '<td class="center">
|
||
|
<a href="index.php?tab=AdminOrders&id_order='.$order['id_order'].'&vieworder&token='.Tools::getAdminToken('AdminOrders'.intval(Tab::getIdFromClassName('AdminOrders')).intval($cookie->id_employee)).'">
|
||
|
<img border="0" title="'.$this->l('View').'" alt="'.$this->l('View').'" src="'._PS_IMG_.'admin/details.gif"/></a>
|
||
|
</td>';
|
||
|
$html.= '</tr>';
|
||
|
}
|
||
|
$html.= '</table><br>';
|
||
|
$html.= '<input type="submit" name="generate" id="generate" value="' . $this->l('Generate') . '" class="button" />';
|
||
|
$html.= '</form>';
|
||
|
}
|
||
|
return $html;
|
||
|
}
|
||
|
|
||
|
public function displayImportForm()
|
||
|
{
|
||
|
$_html = '<form action="'.$_SERVER['REQUEST_URI'].'" method="post" enctype="multipart/form-data">';
|
||
|
$_html.= '<fieldset>';
|
||
|
$_html.= '<legend>' . $this->l('Import Shipping Number') . '</legend>';
|
||
|
$_html.= '<p><label for="file" class="t">' . $this->l('Select a CSV file generated by Expeditor') . '</label></p>';
|
||
|
$_html.= '<input type="hidden" name="MAX_FILE_SIZE" value="500000" />';
|
||
|
$_html.= '<input type="file" name="file" id="file" class="button" size="60" />';
|
||
|
$_html.= '<p><input type="submit" name="import" id="import" value="' . $this->l('Import file') . '" class="button" /></p>';
|
||
|
$_html.= '</fieldset>';
|
||
|
$_html.= '</form>';
|
||
|
return $_html;
|
||
|
}
|
||
|
|
||
|
public function display()
|
||
|
{
|
||
|
$html = '';
|
||
|
|
||
|
if (!empty($_POST))
|
||
|
$html.= $this->_postProcess();
|
||
|
|
||
|
if ($this->displayInfos)
|
||
|
{
|
||
|
$html.= $this->displayOrdersTable();
|
||
|
$html.= '<br/><br/>';
|
||
|
$html.= $this->displayImportForm();
|
||
|
}
|
||
|
echo $html;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
?>
|