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 '
' . $this->l('List of orders recognized') . '
';
echo '';
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 '
' . $this->l('Email send to') . ': ' . $customer->email;
else
echo '
' . $this->l('Email faild to') . ': ' . $customer->email;
echo '- ' . $this->l('Order number') . ': ' . $id_order;
echo '
' . $this->l('Shipping number') . ': ' . $shipping_number;
echo " \n";
}
}
echo '
';
echo '
';
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 '' . $this->l('Please upload a CSV file.') . '
';
}
else
{
if (!$this->_importNumber())
return ''.$this->l('Column').' '.$this->columnName.' '.$this->l('is required').'.'.'
';
}
}
}
private function displayOrdersTable()
{
global $cookie;
$order_state = new OrderState(Configuration::get('EXPEDITOR_STATE_EXP'));
$html = '';
$html.= ''.$this->l('All orders which have the state').' "'.$order_state->name[$cookie->id_lang].'"';
foreach(explode(',',Configuration::get('EXPEDITOR_CARRIER')) as $val ) {
$carrier = new Carrier($val);
if ($carrier->id) $html.= ' '.$this->l('or the carrier').' "'.$carrier->name.'"';
}
$html.= '.
' . $this->l('Change configuration') . '
';
$orders = ExpeditorModule::getOrders();
if (empty($orders))
{
$html.= '' . $this->l('No orders with this state.') . '
';
}
else
{
$html.= '';
}
return $html;
}
public function displayImportForm()
{
$_html = '';
return $_html;
}
public function display()
{
$html = '';
if (!empty($_POST))
$html.= $this->_postProcess();
if ($this->displayInfos)
{
$html.= $this->displayOrdersTable();
$html.= '
';
$html.= $this->displayImportForm();
}
echo $html;
}
}
?>