* @copyright 2016-2017 GFI Informatique, 2016-2017 TNT * @license https://opensource.org/licenses/MIT MIT License */ 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/helper/TNTOfficiel_ShipmentHelper.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Logger.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/pdf/TNTOfficiel_PDFMerger.php'; class TNTOfficiel_OrderHelper { /** @var TNTOfficiel_OrderHelper */ private static $_instance = null; /** @var TNTOfficiel_Logger */ private $logger; /** * Constructor. */ public function __construct() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $this->logger = new TNTOfficiel_Logger(); } /** * Creates a singleton. * * @param void * * @return TNTOfficiel_OrderHelper */ public static function getInstance() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); if (is_null(TNTOfficiel_OrderHelper::$_instance)) { TNTOfficiel_OrderHelper::$_instance = new TNTOfficiel_OrderHelper(); } return TNTOfficiel_OrderHelper::$_instance; } /** * Gets the tnt product code for an order. * * @param $orderId * * @return string * * @throws Exception */ public function getOrderData($orderId, $stopOnException = true) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $sql = 'SELECT * FROM '._DB_PREFIX_.'tntofficiel_order WHERE id_order = '.(int)$orderId; $result = Db::getInstance()->getRow($sql, false); if (!$result && $stopOnException) { throw new Exception('No tnt product was found for the order '.$orderId); } return $result; } /** * Revert the order state to its previous state. * * @param $orderId * * @throws Exception */ public function revertState($orderId) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $arrTNTOrder = $this->getOrderData($orderId); $orderState = new OrderState($arrTNTOrder['previous_state']); $history = new OrderHistory(); $history->id_order = (int)$orderId; $history->changeIdOrderState((int)$orderState, (int)$orderId); $history->save(); } /** * Change the order state to the prestashop default shipping state. * * @param $orderId */ public function validateShippedState($orderId) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $history = new OrderHistory(); $history->id_order = (int)$orderId; $history->changeIdOrderState((int)(Configuration::get('PS_OS_SHIPPING')), (int)$orderId); $history->save(); } /** * Get all the tnt orders. * * @return array * * @throws PrestaShopDatabaseException */ public function getTntOrders() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $sql = 'SELECT * FROM '._DB_PREFIX_.'orders o '; $sql .= 'JOIN '._DB_PREFIX_.'tntofficiel_order t ON o.id_order = t.id_order'; $orders = Db::getInstance()->executeS($sql); return $orders; } /** * Concatenate all the bt for the given orders. * * @param $orderList * * @return string * * @throws Exception */ public function getBt($orderList) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $pdf = new TNTOfficiel_PDFMerger(); $btCounter = 0; foreach ($orderList as $orderId) { try { $arrTNTOrder = $this->getOrderData($orderId, false); $strBTFilename = $arrTNTOrder['bt_filename']; if ($strBTFilename && filesize(_PS_MODULE_DIR_.'tnt_media/media/bt/'.$strBTFilename) > 0) { ++$btCounter; $pdf->addPDF(_PS_MODULE_DIR_.'tnt_media/media/bt/'.$strBTFilename); } } catch (Exception $objException) { $objFileLogger = new FileLogger(); $objFileLogger->setFilename(_PS_ROOT_DIR_.'/log/'.date('Ymd').'_tnt_exception.log'); $objFileLogger->logError($objException->getMessage()); } } if ($btCounter > 0) { return $pdf->merge('download', 'bt_list.pdf'); } } /** * Create a new address and assign it to the order as the deliveray address. * * @param $arrProduct * @param $orderId */ public static function createNewAddress($arrProduct, $orderId) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $strRepoType = (array_key_exists('xett', $arrProduct) && isset($arrProduct['xett'])) ? 'xett' : 'pex'; $order = new Order($orderId); $oldAddress = new Address($order->id_address_delivery); $address = new Address(); $address->id_country = (int)Context::getContext()->country->id; $address->id_customer = 0; $address->id_manufacturer = 0; $address->id_supplier = 0; $address->id_warehouse = 0; $address->alias = TNTOfficiel::MODULE_NAME; $address->lastname = $oldAddress->lastname; //lastname cannot be empty $address->firstname = $oldAddress->firstname; //firstname cannot be empty $address->company = sprintf('%s - %s', $arrProduct[ $strRepoType ], $arrProduct['name']); $address->address1 = ($strRepoType === 'xett') ? $arrProduct['address'] : $arrProduct['address1']; $address->address2 = ($strRepoType === 'xett') ? '' : $arrProduct['address2']; $address->city = $arrProduct['city']; $address->postcode = $arrProduct['postcode']; $address->save(); //assign the new delivery address to the order $order->id_address_delivery = (int)$address->id; $order->save(); //Db::getInstance()->update('orders', array('id_address_delivery' => $address->id), 'id_order = '.(int) $orderId); } }