<?php
/**
 * NOTICE OF LICENSE.
 *
 * This source file is subject to a commercial license from Common-Services Co., Ltd.
 * Use, copy, modification or distribution of this source file without written
 * license agreement from the SARL SMC is strictly forbidden.
 * In order to obtain a license, please contact us: support.mondialrelay@common-services.com
 * ...........................................................................
 * INFORMATION SUR LA LICENCE D'UTILISATION
 *
 * L'utilisation de ce fichier source est soumise a une licence commerciale
 * concedee par la societe Common-Services Co., Ltd.
 * Toute utilisation, reproduction, modification ou distribution du present
 * fichier source sans contrat de licence ecrit de la part de la Common-Services Co. Ltd. est
 * expressement interdite.
 * Pour obtenir une licence, veuillez contacter Common-Services Co., Ltd. a l'adresse:
 *            support.mondialrelay@common-services.com
 * ...........................................................................
 *
 * @author    Alexandre D.
 * @copyright Copyright (c) 2011-2015 Common Services Co Ltd - 90/25 Sukhumvit 81 - 10260 Bangkok - Thailand
 * @license   Commercial license
 * Support by mail  :  support.mondialrelay@common-services.com
 */

ob_start();

if (isset($_SERVER['DropBox']) && $_SERVER['DropBox']) {
    require_once readlink(dirname($_SERVER['SCRIPT_FILENAME']).'/../../../config/config.inc.php');
} else {
    require_once dirname(__FILE__).'/../../../config/config.inc.php';
}
if (version_compare(_PS_VERSION_, '1.5', '<')) {
    require_once _PS_MODULE_DIR_.'mondialrelayadvanced/backward_compatibility/backward.php';
}
require_once _PS_MODULE_DIR_.'mondialrelayadvanced/classes/MondialRelayDelivery.php';
require_once _PS_MODULE_DIR_.'mondialrelayadvanced/mondialrelayadvanced.php';

$mondial_relay = new MondialRelayAdvanced();
$file_name = basename(__FILE__, '.php');

if (!Tools::getValue('token') || Tools::getValue('token') != Configuration::get('MRA_TOKEN')) {
    die($mondial_relay->l('No token or wrong one.', $file_name));
}

if (!Tools::getValue('orders')) {
    die($mondial_relay->l('No ID orders received.', $file_name));
}

$employee_id = new Cookie('mra_current_employee');
$token = Tools::getAdminToken(
    'AdminOrders'.(int) Tab::getIdFromClassName('AdminOrders').(int) $employee_id->variable_name
);
$result = array();

foreach (Tools::getValue('orders') as $id_order) {
    $tmp_order = new Order((int) $id_order);
    $tmp_customer = new Customer((int) $tmp_order->id_customer);
    $tmp_address = new Address((int) $tmp_order->id_address_delivery);
    $tmp_label = new MondialRelayDelivery(null, $tmp_order->id_cart, $tmp_order->id_customer);

    $result[] = array(
        'id' => $id_order,
        'mr_selected' => $tmp_label->id_mr_selected,
        'customer' => Tools::ucfirst($tmp_customer->firstname).' '.Tools::ucfirst($tmp_customer->lastname),
        'address' => $tmp_address->address1.', '.$tmp_address->postcode.' '.
            $tmp_address->city.', '.$tmp_address->country,
        'date_add' => $tmp_order->date_add,
        'weight' => (float)$tmp_label->mr_poids ? $tmp_label->mr_poids : $tmp_order->getTotalWeight(),
        'nbcolis' => $tmp_label->mr_nbcolis ?: 1,
        'label_url' => $tmp_label->url_etiquette,
        'tracking_url' => $tmp_label->url_suivi,
        'exp_number' => $tmp_label->exp_number,
        'url' => '?controller=AdminOrders&id_order='.$id_order.'&vieworder&token='.$token,
    );
}

$callback = Tools::getValue('callback');
$output = ob_get_clean();

die($callback.'('.Tools::jsonEncode(array('output' => $output, 'result' => $result)).')');
