Preprod mutu c7ca03cf57 tnt
2017-09-19 16:48:02 +02:00

500 lines
22 KiB
PHP

<?php
/**
* TNT OFFICIAL MODULE FOR PRESTASHOP.
*
* @author GFI Informatique <www.gfi.fr>
* @copyright 2016-2017 GFI Informatique, 2016-2017 TNT
* @license https://opensource.org/licenses/MIT MIT License
*/
class AdminOrdersController extends AdminOrdersControllerCore
{
private $carriers_array = array();
public function __construct()
{
// Warning : Dependencies on construct implies to do not create static method using TNTOfficiel class !
require_once _PS_MODULE_DIR_.'tntofficiel/tntofficiel.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Debug.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Carrier.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/helper/TNTOfficiel_OrderHelper.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/helper/TNTOfficiel_ShipmentHelper.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/pdf/manifest/TNTOfficiel_ManifestPDFCreator.php';
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
parent::__construct();
$this->bootstrap = true;
$this->table = 'order';
$this->className = 'Order';
$this->lang = false;
$this->addRowAction('view');
$this->explicitSelect = true;
$this->allow_export = true;
$this->deleted = false;
$this->context = Context::getContext();
$this->_select = '
a.id_currency,
a.id_order AS id_pdf,
CONCAT(LEFT(c.`firstname`, 1), \'. \', c.`lastname`) AS `customer`,
osl.`name` AS `osname`,
os.`color`,
`to`.`bt_filename` AS `BT`,
`to`.`id_order` as `tntofficiel_id_order`,
`to`.`pickup_number` as `tntofficiel_pickup_number`,
IF((`to`.`carrier_label` IS NULL OR `to`.`carrier_label` = ""),
c1.`name`,
CONCAT(c1.`name` ,\' (\', `to`.`carrier_label`,\')\')) AS `carrier`,
c1.`id_carrier` as id_carrier,
IF((SELECT so.id_order
FROM `'._DB_PREFIX_.'orders` so
WHERE so.id_customer = a.id_customer AND so.id_order < a.id_order LIMIT 1) > 0,
0,
1) as new,
country_lang.name as cname,
IF(a.valid, 1, 0) badge_success';
$this->_join = '
LEFT JOIN `'._DB_PREFIX_.'customer` c ON (c.`id_customer` = a.`id_customer`)
LEFT JOIN `'._DB_PREFIX_.'tntofficiel_order` `to` ON (a.`id_order` = `to`.`id_order`)
INNER JOIN `'._DB_PREFIX_.'carrier` c1 ON (a.`id_carrier` = c1.`id_carrier`)
INNER JOIN `'._DB_PREFIX_.'address` address ON address.id_address = a.id_address_delivery
INNER JOIN `'._DB_PREFIX_.'country` country ON address.id_country = country.id_country
INNER JOIN `'._DB_PREFIX_.'country_lang` country_lang
ON (country.`id_country` = country_lang.`id_country`
AND country_lang.`id_lang` = '.(int)$this->context->language->id.')
LEFT JOIN `'._DB_PREFIX_.'order_state` os ON (os.`id_order_state` = a.`current_state`)
LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl
ON (os.`id_order_state` = osl.`id_order_state`
AND osl.`id_lang` = '.(int)$this->context->language->id.')';
$this->_orderBy = 'id_order';
$this->_orderWay = 'DESC';
$statuses = OrderState::getOrderStates((int)$this->context->language->id);
foreach ($statuses as $status) {
$this->statuses_array[$status['id_order_state']] = $status['name'];
}
$carriers = Carrier::getCarriers(
(int)$this->context->language->id,
false,
false,
false,
null,
Carrier::ALL_CARRIERS
);
foreach ($carriers as $carrier) {
$this->carriers_array[$carrier['id_carrier']] = $carrier['name'];
}
$this->fields_list = array(
'id_order' => array(
'title' => $this->l('ID'),
'align' => 'text-center',
'class' => 'fixed-width-xs',
),
'reference' => array(
'title' => $this->l('Reference'),
),
'new' => array(
'title' => $this->l('New client'),
'align' => 'text-center',
'type' => 'bool',
'tmpTableFilter' => true,
'orderby' => false,
'callback' => 'printNewCustomer',
),
'customer' => array(
'title' => $this->l('Customer'),
'havingFilter' => true,
),
'carrier' => array(
'title' => $this->l('Carrier'),
'filter_key' => 'c1!id_carrier',
'filter_type' => 'int',
'order_key' => 'carrier',
'havingFilter' => true,
'type' => 'select',
'list' => $this->carriers_array,
),
);
if (Configuration::get('PS_B2B_ENABLE')) {
$this->fields_list = array_merge(
$this->fields_list,
array(
'company' => array(
'title' => $this->l('Company'),
'filter_key' => 'c!company',
),
)
);
}
$this->fields_list = array_merge(
$this->fields_list,
array(
'total_paid_tax_incl' => array(
'title' => $this->l('Total'),
'align' => 'text-right',
'type' => 'price',
'currency' => true,
'callback' => 'setOrderCurrency',
'badge_success' => true,
),
'payment' => array(
'title' => $this->l('Payment'),
),
'osname' => array(
'title' => $this->l('Status'),
'type' => 'select',
'color' => 'color',
'list' => $this->statuses_array,
'filter_key' => 'os!id_order_state',
'filter_type' => 'int',
'order_key' => 'osname',
),
'date_add' => array(
'title' => $this->l('Date'),
'align' => 'text-right',
'type' => 'datetime',
'filter_key' => 'a!date_add',
),
'id_pdf' => array(
'title' => $this->l('PDF'),
'align' => 'text-center',
'callback' => 'printPDFIcons',
'orderby' => false,
'search' => false,
'remove_onclick' => true,
),
'tntofficiel_id_order' => array(
'title' => $this->l('TNT'),
'align' => 'text-right',
'orderby' => false,
'search' => false,
'callback' => 'printBtIcon',
'remove_onclick' => true,
)
)
);
if (Configuration::get('TNT_CARRIER_PICKUP_NUMBER_SHOW')) {
$this->fields_list = array_merge(
$this->fields_list,
array(
'tntofficiel_pickup_number' => array(
'title' => $this->l('N° de Ramassage'),
'align' => 'text-right',
'orderby' => false,
'search' => false,
),
)
);
}
if (Country::isCurrentlyUsed('country', true)) {
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT DISTINCT c.id_country, cl.`name`
FROM `'._DB_PREFIX_.'orders` o
'.Shop::addSqlAssociation('orders', 'o').'
INNER JOIN `'._DB_PREFIX_.'address` a ON a.id_address = o.id_address_delivery
INNER JOIN `'._DB_PREFIX_.'country` c ON a.id_country = c.id_country
INNER JOIN `'._DB_PREFIX_.'country_lang` cl
ON (c.`id_country` = cl.`id_country`
AND cl.`id_lang` = '.(int)$this->context->language->id.')
ORDER BY cl.name ASC');
$country_array = array();
foreach ($result as $row) {
$country_array[$row['id_country']] = $row['name'];
}
$part1 = array_slice($this->fields_list, 0, 3);
$part2 = array_slice($this->fields_list, 3);
$part1['cname'] = array(
'title' => $this->l('Delivery'),
'type' => 'select',
'list' => $country_array,
'filter_key' => 'country!id_country',
'filter_type' => 'int',
'order_key' => 'cname',
);
$this->fields_list = array_merge($part1, $part2);
}
$this->shopLinkType = 'shop';
$this->shopShareDatas = Shop::SHARE_ORDER;
if (Tools::isSubmit('id_order')) {
$intOrderID = (int)Tools::getValue('id_order');
$objOrder = new Order($intOrderID);
$this->context->cart = new Cart($objOrder->id_cart);
$this->context->customer = new Customer($objOrder->id_customer);
}
$this->bulk_actions = array(
'updateOrderStatus' => array(
'text' => $this->l('Change Order Status'),
'icon' => 'icon-refresh',
),
'getBT' => array(
'text' => $this->l('Bon de transport de TNT'),
'icon' => 'icon-AdminTNTOfficiel',
),
'getManifest' => array(
'text' => $this->l('Manifeste TNT'),
'icon' => 'icon-file-text',
),
);
}
/**
* @param $orderId
*
* @return mixed
*/
public function printBtIcon($orderId)
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$arrTNTOrder = TNTOfficiel_OrderHelper::getInstance()->getOrderData($orderId);
$this->context->smarty->assign(array(
'bt_filename' => $arrTNTOrder['bt_filename'],
'getManifestUrl' => $this->context->link->getAdminLink('AdminTNTOfficiel').'&action=getManifest',
));
return $this->context->smarty->fetch(_PS_MODULE_DIR_.'tntofficiel/views/templates/admin/_print_bt_icon.tpl');
}
/**
* Return all the BT for the selected orders.
*/
public function processBulkGetBT()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
if (Module::isInstalled(TNTOfficiel::MODULE_NAME)) {
$arrOrderID = (array)Tools::getValue('orderBox');
$orderList = array();
foreach ($arrOrderID as $strOrderID) {
$intOrderID = (int)$strOrderID;
$objOrder = new Order($intOrderID);
// Check if it's a tnt order.
if (TNTOfficiel_Carrier::isTNTOfficielCarrierID($objOrder->id_carrier)) {
$orderList[] = $intOrderID;
}
}
TNTOfficiel_OrderHelper::getInstance()->getBt($orderList);
}
}
/**
* Return all the BT for the selected orders.
*/
public function processBulkGetManifest()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$arrOrderIDList = array();
if (!Tools::getIsset('orderBox')) {
return;
}
$arrOrderID = (array)Tools::getValue('orderBox');
foreach ($arrOrderID as $strOrderID) {
$intOrderID = (int)$strOrderID;
$arrOrderIDList[] = $intOrderID;
}
$manifest = new TNTOfficiel_ManifestPDFCreator();
$manifest->createManifest($arrOrderIDList);
}
public function processBulkUpdateOrderStatus()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$intOrderState = (int)Tools::getValue('id_order_state');
if (Tools::isSubmit('submitUpdateOrderStatus') && $intOrderState) {
if ($this->tabAccess['edit'] !== '1') {
$this->errors[] = Tools::displayError('You do not have permission to edit this.');
} else {
$objOrderState = new OrderState($intOrderState);
if (!Validate::isLoadedObject($objOrderState)) {
$this->errors[] = sprintf(Tools::displayError('Order status #%d cannot be loaded'), $intOrderState);
} else {
$arrOrderID = (array)Tools::getValue('orderBox');
foreach ($arrOrderID as $strOrderID) {
$intOrderID = (int)$strOrderID;
$objOrder = new Order($intOrderID);
if (!Validate::isLoadedObject($objOrder)) {
$this->errors[] = sprintf(Tools::displayError('Order #%d cannot be loaded'), $intOrderID);
} else {
/* Override start */
if (TNTOfficiel_Carrier::isTNTOfficielCarrierID($objOrder->id_carrier)
&& $objOrderState->id == Configuration::get('PS_OS_SHIPPING')
) {
$arrTNTOrder = TNTOfficiel_OrderHelper::getInstance()->getOrderData($objOrder->id);
if ($arrTNTOrder['shipping_date'] && !$arrTNTOrder['is_shipped']) {
$objTNTShipmentHelper = TNTOfficiel_ShipmentHelper::getInstance();
$arrMDWShippingDate = $objTNTShipmentHelper->checkSaveShipmentDate(
$objOrder->id,
$arrTNTOrder['shipping_date']
);
if (array_key_exists('error', $arrMDWShippingDate)
&& $arrMDWShippingDate['error'] == 1
) {
$arrMDWShippingDate = $objTNTShipmentHelper->getNewShippingDate($objOrder->id);
if (!$arrMDWShippingDate) {
$this->errors[] = sprintf(
Tools::displayError('Cannot change status for order #%d.'),
$intOrderID
);
continue;
}
$tempDate = new DateTime('today');
$startDate = $tempDate->format('Y-m-d');
Db::getInstance()->update(
'tntofficiel_order',
array(
'shipping_date' => pSQL($arrMDWShippingDate['shippingDate']),
'due_date' => pSQL($arrMDWShippingDate['dueDate']),
'start_date' => pSQL($startDate),
),
'id_tntofficiel_order = '.(int)$arrTNTOrder['id_tntofficiel_order']
);
}
} elseif (!$arrTNTOrder['shipping_date'] && !$arrTNTOrder['is_shipped']) {
$this->errors[] = sprintf(
Tools::displayError('Cannot change status for order #%d.'),
$intOrderID
);
continue;
}
}
/* Override end */
$current_order_state = $objOrder->getCurrentOrderState();
if ($current_order_state->id == $objOrderState->id) {
$this->errors[] = $this->displayWarning(
sprintf('Order #%d has already been assigned this status.', $intOrderID)
);
} else {
$history = new OrderHistory();
$history->id_order = $objOrder->id;
$history->id_employee = (int)$this->context->employee->id;
$use_existings_payment = !$objOrder->hasInvoice();
$history->changeIdOrderState(
(int)$objOrderState->id,
$objOrder,
$use_existings_payment
);
$carrier = new Carrier($objOrder->id_carrier, $objOrder->id_lang);
$templateVars = array();
if ($history->id_order_state == Configuration::get('PS_OS_SHIPPING')
&& $objOrder->shipping_number
) {
$templateVars = array(
'{followup}' => str_replace('@', $objOrder->shipping_number, $carrier->url)
);
}
if ($history->addWithemail(true, $templateVars)) {
if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
foreach ($objOrder->getProducts() as $product) {
if (StockAvailable::dependsOnStock($product['product_id'])) {
StockAvailable::synchronize(
$product['product_id'],
(int)$product['id_shop']
);
}
}
}
} else {
$this->errors[] = sprintf(
Tools::displayError('Cannot change status for order #%d.'),
$intOrderID
);
}
}
}
}
}
}
if (!count($this->errors)) {
Tools::redirectAdmin(self::$currentIndex.'&conf=4&token='.$this->token);
}
}
}
/* public function printNewCustomer($tr)
{
return ($tr['new'] ? $this->l('Yes') : $this->l('No'));
} */
public function postProcess()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$objContext = Context::getContext();
// See hookActionOrderStatusUpdate.
$objCookie = $objContext->cookie;
$isTntInstalled = false;
if (Module::isInstalled(TNTOfficiel::MODULE_NAME)) {
$isTntInstalled = true;
$module = Module::getInstanceByName(TNTOfficiel::MODULE_NAME);
}
if (Tools::isSubmit('id_order')) {
$intOrderID = (int)Tools::getValue('id_order');
if ($intOrderID > 0) {
$objOrder = new Order($intOrderID);
if (!Validate::isLoadedObject($objOrder)) {
$this->errors[] = Tools::displayError('The order cannot be found within your database.');
}
ShopUrl::cacheMainDomainForShop((int)$objOrder->id_shop);
if (!Tools::isSubmit('submitState') && $isTntInstalled) {
if ($module->updateShippingDate($objOrder) === false) {
$objCookie->saveShippingError = $this->l('Impossible de récupérer une date de ramassage');
}
}
}
}
if (Tools::isSubmit('submitState') && isset($objOrder) && $isTntInstalled) {
if (TNTOfficiel_Carrier::isTNTOfficielCarrierID($objOrder->id_carrier)) {
$arrTNTOrder = TNTOfficiel_OrderHelper::getInstance()->getOrderData($objOrder->id);
if ($arrTNTOrder['shipping_date'] && !$arrTNTOrder['is_shipped']) {
$objTNTShipmentHelper = TNTOfficiel_ShipmentHelper::getInstance();
$arrMDWShippingDate = $objTNTShipmentHelper->checkSaveShipmentDate(
$objOrder->id,
$arrTNTOrder['shipping_date']
);
if (array_key_exists('error', $arrMDWShippingDate)) {
if ($arrMDWShippingDate['error'] == 1) {
$objCookie->saveShippingError = $this->l('Erreur : '.$arrMDWShippingDate['message']);
return;
}
$objCookie->saveShippingError = $this->l('Attention : '.$arrMDWShippingDate['message']);
}
} elseif (!$arrTNTOrder['shipping_date'] && !$arrTNTOrder['is_shipped']) {
$objCookie->saveShippingError = $this->l('Erreur : Choisir une date de ramassage');
return;
}
}
}
parent::postProcess();
}
}