controller = 'AdminModules';
$this->module_name = 'ant_dropshippingtracking';
$this->config_tab = (bool)$config_tab;
if ($config_tab) {
$this->controller = 'AdminAntDropshippingtracking';
}
}
public function _postProcess() {
if(Tools::isSubmit('submitOrderTracking') && Tools::getValue('id_sale')){
$errors = 0;
$id_sale = (int)Tools::getValue('id_sale');
$sale_names = array();
foreach(Db::getInstance()->ExecuteS('
SELECT c.`name`, c.`id_lang`
FROM `'._DB_PREFIX_.'privatesale` p
LEFT JOIN `'._DB_PREFIX_.'category_lang` c ON (c.`id_category` = p.`id_category`)
WHERE p.`id_sale` = '.(int)$id_sale.'
') as $key => $row) {
$sale_names[(int)$row['id_lang']] = $row['name'];
}
$sale_products = array();
foreach(Db::getInstance()->ExecuteS('
SELECT DISTINCT `id_product`
FROM `'._DB_PREFIX_.'product_ps_cache`
WHERE `id_sale` = '.(int) $id_sale.'
') as $key => $row) {
$sale_products[(int)$row['id_product']] = (int)$row['id_product'];
}
if(isset($_FILES['csvfile']) && $_FILES['csvfile']['name'] != '') {
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
fgetcsv($f, 0, ';');
$i = 1;
$orders = array();
while($line = fgetcsv($f, 0, ';')) {
$i++;
$orders[(int) $line[0]] = array(
'id_order' => (int) $line[0],
'tracking_number' => trim($line[1]),
'carrier' => $line[2],
'link' => trim($line[3]),
);
}
if(!empty($orders)) {
foreach ($orders as $id_order => $o) {
if($id_order != 0){
$order = new Order((int)$id_order);
if(Validate::isLoadedObject($order)) {
$order_details = array();
foreach(Db::getInstance()->ExecuteS('
SELECT DISTINCT `id_order_detail`, `product_name`,`product_id`, `product_attribute_id`, `product_quantity` - GREATEST(`product_quantity_return`, `product_quantity_refunded`) AS `quantity`
FROM `'._DB_PREFIX_.'order_detail`
WHERE `id_order` = '.(int) $id_order.'
') as $key => $row) {
$order_details[(int)$row['id_order_detail']] = $row;
}
$fully_sent = false;
$products_sent = array();
foreach(Db::getInstance()->ExecuteS('
SELECT d.`id_order_detail`, IF(
(d.`product_quantity` - IF(
d.`product_quantity_return` > 0, d.`product_quantity_return`, d.`product_quantity_refunded`
) - IFNULL(SUM(s.`quantity`), 0)) > 0, 1, 0
) AS `remain`
FROM `'._DB_PREFIX_.'order_detail` d
LEFT OUTER JOIN `'._DB_PREFIX_.'lapostews` s
ON s.`id_order_detail` = d.`id_order_detail`
WHERE d.`id_order` = '.(int) $order->id.'
GROUP BY d.`id_order_detail`
') as $quantity_remain) {
if((int) $quantity_remain['remain'] == 0) {
$products_sent[] = (int) $quantity_remain['id_order_detail'];
}
}
foreach(Db::getInstance()->ExecuteS('
SELECT d.`id_order_detail`, IF(
(d.`product_quantity` - IF(
d.`product_quantity_return` > 0, d.`product_quantity_return`, d.`product_quantity_refunded`
) - IFNULL(SUM(s.`quantity`), 0)) > 0, 1, 0
) AS `remain`
FROM `'._DB_PREFIX_.'order_detail` d
LEFT OUTER JOIN `'._DB_PREFIX_.'exapaqws` s
ON s.`id_order_detail` = d.`id_order_detail`
WHERE d.`id_order` = '.(int) $order->id.'
GROUP BY d.`id_order_detail`
') as $quantity_remain) {
if((int) $quantity_remain['remain'] == 0) {
$products_sent[] = (int) $quantity_remain['id_order_detail'];
}
}
foreach(Db::getInstance()->ExecuteS('
SELECT d.`id_order_detail`, IF(
(d.`product_quantity` - IF(
d.`product_quantity_return` > 0, d.`product_quantity_return`, d.`product_quantity_refunded`
) - IFNULL(SUM(s.`quantity`), 0)) > 0, 1, 0
) AS `remain`
FROM `'._DB_PREFIX_.'order_detail` d
LEFT OUTER JOIN `'._DB_PREFIX_.'mondialrelay_parcel` s
ON s.`id_order_detail` = d.`id_order_detail`
WHERE d.`id_order` = '.(int) $order->id.'
GROUP BY d.`id_order_detail`
') as $quantity_remain) {
if((int) $quantity_remain['remain'] == 0) {
$products_sent[] = (int) $quantity_remain['id_order_detail'];
}
}
foreach(Db::getInstance()->ExecuteS('
SELECT d.`id_order_detail`, IF(
(d.`product_quantity` - IF(
d.`product_quantity_return` > 0, d.`product_quantity_return`, d.`product_quantity_refunded`
) - IFNULL(SUM(s.`quantity`), 0)) > 0, 1, 0
) AS `remain`
FROM `'._DB_PREFIX_.'order_detail` d
LEFT OUTER JOIN `'._DB_PREFIX_.'philea_parcel` s
ON s.`id_order_detail` = d.`id_order_detail`
WHERE d.`id_order` = '.(int) $order->id.'
GROUP BY d.`id_order_detail`
') as $quantity_remain) {
if((int) $quantity_remain['remain'] == 0) {
$products_sent[] = (int) $quantity_remain['id_order_detail'];
}
}
foreach(Db::getInstance()->ExecuteS('
SELECT d.`id_order_detail`, IF(
(d.`product_quantity` - IF(
d.`product_quantity_return` > 0, d.`product_quantity_return`, d.`product_quantity_refunded`
) - IFNULL(SUM(s.`quantity`), 0)) > 0, 1, 0
) AS `remain`
FROM `'._DB_PREFIX_.'order_detail` d
LEFT OUTER JOIN `'._DB_PREFIX_.'ant_dropshipping_parcel` s
ON s.`id_order_detail` = d.`id_order_detail`
WHERE d.`id_order` = '.(int) $order->id.'
GROUP BY d.`id_order_detail`
') as $quantity_remain) {
if((int) $quantity_remain['remain'] == 0) {
$products_sent[] = (int) $quantity_remain['id_order_detail'];
}
}
$remaining = array();
$html_products_sent = '';
foreach($order_details as $id_order_detail => $d) {
if(!in_array($id_order_detail, $products_sent) && !in_array($d['product_id'],$sale_products)) {
$remaining[] = (int)$id_order_detail;
} elseif(!in_array($id_order_detail, $products_sent) && in_array((int)$d['product_id'],$sale_products)) {
$html_products_sent .= '
'."\r\n".$d['quantity'] . 'x' . ' ' . $p['product_name'];
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'ant_dropshipping_parcel`
VALUES (
'.(int) $id_order_detail.',
'.(int) $d['quantity'].',
"'.pSQL($o['tracking_number']).'",
"'.pSQL($o['carrier']).'",
"'.pSQL($o['link']).'",
NOW()
)
');
}
}
if(count($remaining) == 0) {
$fully_sent = true;
} else {
$fully_sent = false;
}
$customer = new Customer((int) $order->id_customer);
$templateVars = array(
'{followup}' => $o['link'],
'{carrier}' => $o['carrier'],
'{tracking_number}' => $o['tracking_number'],
'{sale}' => $sale_names[(int)$order->id_lang],
'{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname,
'{id_order}' => (int) $order->id,
// '{product_list}' => !empty($html_products_sent)? $content_html.$html_products_sent: '',
// '{product_list_txt}' => !empty($html_products_sent)? $content_txt.strip_tags($html_products_sent): '',
);
$history = new OrderHistory();
$history->id_order = (int) $order->id;
$history->changeIdOrderState(($fully_sent? Configuration::get('PS_OS_SHIPPING'): 17), (int) $order->id);
$history->id_employee = 0;
$history->add();
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'shipping_history`
VALUES (
'.(int) $order->id.',
"'.pSQL($o['shipping_number']).'",
NOW(),
0,
'.(int)$id_sale.'
)
');
global $_LANGMAIL;
$subject = 'Package in transit';
if(!Mail::Send(intval($order->id_lang), 'in_transit_dropshipping', ((is_array($_LANGMAIL) && key_exists($subject, $_LANGMAIL)) ? $_LANGMAIL[$subject] : $subject), $templateVars, $customer->email, $customer->firstname.' '.$customer->lastname)){
$errors++;
}
}
}
}
if(!isset($_COOKIE['logistics_sales'])){
setcookie('logistics_sales', $id_sale, 0, __PS_BASE_URI__);
} else {
$sales = explode('-', $_COOKIE['logistics_sales']);
if(!in_array($id_sale,$sales)){
$sales[] = $id_sale;
}
setcookie('logistics_sales', implode('-', $sales), 0, __PS_BASE_URI__);
}
if($errors==0){
$this->_html .= HelperFormBootstrap::displaySuccess($this->l('Fichier importé avec succès !'));
} else {
$this->_html .= HelperFormBootstrap::displayErrors($this->l($errors.' mails n\'ont pas pu être envoyés'));
}
} else {
$this->_html .= HelperFormBootstrap::displayErrors($this->l('Aucune commande trouvée, vérifier votre fichier'));
}
} else {
$this->_html .= HelperFormBootstrap::displayErrors($this->l('Fichier manquant !'));
}
}
}
public function display() {
global $cookie, $currentIndex;
$base_link = $currentIndex . '&token='.Tools::getAdminTokenLite('AdminAntDropshippingtracking');
$this->_html = '';
$this->_postProcess();
$id_category_options = array();
foreach(Db::getInstance()->ExecuteS('
SELECT p.`id_sale`, c.`name`, c.`id_category`
FROM `'._DB_PREFIX_.'privatesale` p
LEFT JOIN `'._DB_PREFIX_.'category_lang` c ON (c.`id_category` = p.`id_category`)
LEFT JOIN `'._DB_PREFIX_.'privatesale_shipping_sale` s ON (s.`id_sale` = p.`id_sale`)
WHERE c.`id_lang` = '.$cookie->id_lang.'
AND s.id_shipping = 2
-- AND p.`date_start` > "2017-01-01 00:00:00"
ORDER BY p.`id_sale` DESC
LIMIT 1200
') as $row) {
$extrafields = Category::getSalesInfos(array((int) $row['id_category']));
$id_category_options[] = array(
'label' => (int) $row['id_sale'].' (#'.(int) $row['id_category'].') - '.$row['name'].(empty($extrafields[(int) $row['id_category']]['sales'][1])?'':' - '.$extrafields[(int) $row['id_category']]['sales'][1]) ,
'value' => (int) $row['id_sale']
);
}
$helperForm = new HelperFormBootstrap();
$helperForm->_select2 = true;
$this->_html .= $helperForm->renderStyle();
$this->_html .= '