shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

fba.php (44623B)


      1 <?php
      2 class ControllerExtensionOpenbayFba extends Controller {
      3     public function install() {
      4         $this->load->model('extension/openbay/fba');
      5         $this->load->model('setting/setting');
      6         $this->load->model('setting/extension');
      7         $this->load->model('user/user_group');
      8 
      9         $this->model_user_user_group->addPermission($this->user->getGroupId(), 'access', 'extension/openbay/fba');
     10         $this->model_user_user_group->addPermission($this->user->getGroupId(), 'modify', 'extension/openbay/fba');
     11 
     12         $this->model_extension_openbay_fba->install();
     13     }
     14 
     15     public function uninstall() {
     16         $this->load->model('extension/openbay/fba');
     17         $this->load->model('setting/setting');
     18         $this->load->model('setting/extension');
     19 
     20         $this->model_extension_openbay_fba->uninstall();
     21         $this->model_setting_extension->uninstall('openbay', $this->request->get['extension']);
     22         $this->model_setting_setting->deleteSetting($this->request->get['extension']);
     23     }
     24 
     25     public function index() {
     26         $this->load->model('setting/setting');
     27         $this->load->model('localisation/order_status');
     28         $this->load->model('extension/openbay/fba');
     29 
     30         $data = $this->load->language('extension/openbay/fba');
     31 
     32         $this->document->setTitle($this->language->get('text_dashboard'));
     33         $this->document->addScript('view/javascript/openbay/js/faq.js');
     34 
     35         $data['breadcrumbs'] = array();
     36         $data['breadcrumbs'][] = array(
     37             'href'      => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true),
     38             'text'      => $this->language->get('text_home'),
     39         );
     40         $data['breadcrumbs'][] = array(
     41             'href'      => $this->url->link('marketplace/openbay', 'user_token=' . $this->session->data['user_token'], true),
     42             'text'      => $this->language->get('text_openbay'),
     43         );
     44         $data['breadcrumbs'][] = array(
     45             'href'      => $this->url->link('extension/openbay/fba', 'user_token=' . $this->session->data['user_token'], true),
     46             'text'      => $this->language->get('text_dashboard'),
     47         );
     48 
     49         $data['success'] = '';
     50         if (isset($this->session->data['success'])) {
     51             $data['success'] = $this->session->data['success'];
     52             unset($this->session->data['success']);
     53         } else {
     54             $data['success'] = '';
     55         }
     56 
     57         $data['validation'] = $this->openbay->fba->validate();
     58         $data['link_settings'] = $this->url->link('extension/openbay/fba/settings', 'user_token=' . $this->session->data['user_token'], true);
     59         $data['link_account'] = 'https://account.openbaypro.com/fba/index/';
     60         $data['link_fulfillments'] = $this->url->link('extension/openbay/fba/fulfillmentlist', 'user_token=' . $this->session->data['user_token'], true);
     61         $data['link_orders'] = $this->url->link('extension/openbay/fba/orderlist', 'user_token=' . $this->session->data['user_token'], true);
     62 		$data['link_signup'] = 'https://account.openbaypro.com/fba/apiRegister/?endpoint=2&utm_source=opencart_install&utm_medium=dashboard&utm_campaign=fba';
     63 
     64         $data['header'] = $this->load->controller('common/header');
     65         $data['column_left'] = $this->load->controller('common/column_left');
     66         $data['footer'] = $this->load->controller('common/footer');
     67 
     68         $this->response->setOutput($this->load->view('extension/openbay/fba', $data));
     69     }
     70 
     71     public function settings() {
     72         $data = $this->load->language('extension/openbay/fba_settings');
     73 
     74         $this->document->setTitle($this->language->get('heading_title'));
     75         $this->document->addScript('view/javascript/openbay/js/faq.js');
     76 
     77         $this->load->model('setting/setting');
     78         $this->load->model('extension/openbay/fba');
     79         $this->load->model('localisation/order_status');
     80 
     81         if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
     82             $this->model_setting_setting->editSetting('openbay_fba', $this->request->post);
     83             $this->session->data['success'] = $this->language->get('text_success');
     84             $this->response->redirect($this->url->link('extension/openbay/fba', 'user_token=' . $this->session->data['user_token'], true));
     85         }
     86 
     87         $data['breadcrumbs'] = array();
     88 
     89         $data['breadcrumbs'][] = array(
     90             'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true),
     91             'text' => $this->language->get('text_home'),
     92         );
     93 
     94         $data['breadcrumbs'][] = array(
     95             'href' => $this->url->link('marketplace/openbay', 'user_token=' . $this->session->data['user_token'], true),
     96             'text' => $this->language->get('text_openbay'),
     97         );
     98 
     99         $data['breadcrumbs'][] = array(
    100             'href' => $this->url->link('extension/openbay/fba', 'user_token=' . $this->session->data['user_token'], true),
    101             'text' => $this->language->get('text_fba'),
    102         );
    103 
    104         $data['breadcrumbs'][] = array(
    105             'href' => $this->url->link('extension/openbay/fba/settings', 'user_token=' . $this->session->data['user_token'], true),
    106             'text' => $this->language->get('heading_title'),
    107         );
    108 
    109         $data['action'] = $this->url->link('extension/openbay/fba/settings', 'user_token=' . $this->session->data['user_token'], true);
    110         $data['cancel'] = $this->url->link('extension/openbay/fba', 'user_token=' . $this->session->data['user_token'], true);
    111         $data['link_signup'] = 'https://account.openbaypro.com/fba/apiRegister/?endpoint=2&utm_source=opencart_install&utm_medium=settings&utm_campaign=fba';
    112 
    113         $data['user_token'] = $this->session->data['user_token'];
    114 
    115         if (isset($this->error['warning'])) {
    116             $data['error_warning'] = $this->error['warning'];
    117         } else {
    118             $data['error_warning'] = '';
    119         }
    120 
    121         if (isset($this->request->post['openbay_fba_status'])) {
    122             $data['openbay_fba_status'] = $this->request->post['openbay_fba_status'];
    123         } else {
    124             $data['openbay_fba_status'] = $this->config->get('openbay_fba_status');
    125         }
    126 
    127         if (isset($this->request->post['openbay_fba_api_key'])) {
    128             $data['openbay_fba_api_key'] = trim($this->request->post['openbay_fba_api_key']);
    129         } else {
    130             $data['openbay_fba_api_key'] = trim($this->config->get('openbay_fba_api_key'));
    131         }
    132 
    133         if (isset($this->request->post['openbay_fba_encryption_key'])) {
    134             $data['openbay_fba_encryption_key'] = trim($this->request->post['openbay_fba_encryption_key']);
    135         } else {
    136             $data['openbay_fba_encryption_key'] = trim($this->config->get('openbay_fba_encryption_key'));
    137         }
    138 
    139         if (isset($this->request->post['openbay_fba_encryption_iv'])) {
    140             $data['openbay_fba_encryption_iv'] = trim($this->request->post['openbay_fba_encryption_iv']);
    141         } else {
    142             $data['openbay_fba_encryption_iv'] = trim($this->config->get('openbay_fba_encryption_iv'));
    143         }
    144 
    145         if (isset($this->request->post['openbay_fba_api_account_id'])) {
    146             $data['openbay_fba_api_account_id'] = trim($this->request->post['openbay_fba_api_account_id']);
    147         } else {
    148             $data['openbay_fba_api_account_id'] = trim($this->config->get('openbay_fba_api_account_id'));
    149         }
    150 
    151         if (isset($this->request->post['openbay_fba_send_orders'])) {
    152             $data['openbay_fba_send_orders'] = $this->request->post['openbay_fba_send_orders'];
    153         } else {
    154             $data['openbay_fba_send_orders'] = $this->config->get('openbay_fba_send_orders');
    155         }
    156 
    157         if (isset($this->request->post['openbay_fba_debug_log'])) {
    158             $data['openbay_fba_debug_log'] = $this->request->post['openbay_fba_debug_log'];
    159         } else {
    160             $data['openbay_fba_debug_log'] = $this->config->get('openbay_fba_debug_log');
    161         }
    162 
    163         $order_total = $this->model_extension_openbay_fba->countFbaOrders();
    164 
    165         if ($order_total > 0) {
    166             $data['prefix_can_edit'] = false;
    167         } else {
    168             $data['prefix_can_edit'] = true;
    169         }
    170 
    171         if (isset($this->request->post['openbay_fba_order_prefix'])) {
    172             $data['openbay_fba_order_prefix'] = $this->request->post['openbay_fba_order_prefix'];
    173         } else {
    174             $data['openbay_fba_order_prefix'] = $this->config->get('openbay_fba_order_prefix');
    175         }
    176 
    177         if (isset($this->request->post['openbay_fba_order_trigger_status'])) {
    178             $data['openbay_fba_order_trigger_status'] = $this->request->post['openbay_fba_order_trigger_status'];
    179         } else {
    180             $data['openbay_fba_order_trigger_status'] = $this->config->get('openbay_fba_order_trigger_status');
    181         }
    182 
    183         if (isset($this->request->post['openbay_fba_only_fill_complete'])) {
    184             $data['openbay_fba_only_fill_complete'] = $this->request->post['openbay_fba_only_fill_complete'];
    185         } else {
    186             $data['openbay_fba_only_fill_complete'] = $this->config->get('openbay_fba_only_fill_complete');
    187         }
    188 
    189         $data['fulfillment_policy'] = array(
    190             'FillOrKill' => $this->language->get('text_fillorkill'),
    191             'FillAll' => $this->language->get('text_fillall'),
    192             'FillAllAvailable' => $this->language->get('text_fillallavailable'),
    193         );
    194 
    195         if (isset($this->request->post['openbay_fba_fulfill_policy'])) {
    196             $data['openbay_fba_fulfill_policy'] = $this->request->post['openbay_fba_fulfill_policy'];
    197         } else {
    198             $data['openbay_fba_fulfill_policy'] = $this->config->get('openbay_fba_fulfill_policy');
    199         }
    200 
    201         $data['shipping_speed'] = array(
    202             'Standard' => $this->language->get('text_standard'),
    203             'Expedited' => $this->language->get('text_expedited'),
    204             'Priority' => $this->language->get('text_priority'),
    205         );
    206 
    207         if (isset($this->request->post['openbay_fba_shipping_speed'])) {
    208             $data['openbay_fba_shipping_speed'] = $this->request->post['openbay_fba_shipping_speed'];
    209         } else {
    210             $data['openbay_fba_shipping_speed'] = $this->config->get('openbay_fba_shipping_speed');
    211         }
    212 
    213         $data['api_server'] = $this->openbay->fba->getServerUrl();
    214         $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
    215 
    216         $data['header'] = $this->load->controller('common/header');
    217         $data['column_left'] = $this->load->controller('common/column_left');
    218         $data['footer'] = $this->load->controller('common/footer');
    219 
    220         $this->response->setOutput($this->load->view('extension/openbay/fba_settings', $data));
    221     }
    222 
    223     public function verifyCredentials() {
    224         $this->load->language('extension/openbay/fba_settings');
    225 
    226         $errors = array();
    227 
    228         if (!isset($this->request->post['openbay_fba_api_key']) || empty($this->request->post['openbay_fba_api_key'])) {
    229             $errors[] = array('message' => $this->language->get('error_api_key'));
    230         }
    231 
    232         if (!isset($this->request->post['openbay_fba_api_account_id']) || empty($this->request->post['openbay_fba_api_account_id'])) {
    233             $errors[] = array('message' => $this->language->get('error_api_account_id'));
    234         }
    235 
    236         if (!isset($this->request->post['openbay_fba_encryption_key']) || empty($this->request->post['openbay_fba_encryption_key'])) {
    237             $errors[] = array('message' => $this->language->get('error_encryption_key'));
    238         }
    239 
    240         if (!isset($this->request->post['openbay_fba_encryption_iv']) || empty($this->request->post['openbay_fba_encryption_iv'])) {
    241             $errors[] = array('message' => $this->language->get('error_encryption_iv'));
    242         }
    243 
    244         if (!$errors) {
    245             $this->openbay->fba->setApiKey($this->request->post['openbay_fba_api_key']);
    246             $this->openbay->fba->setAccountId($this->request->post['openbay_fba_api_account_id']);
    247             $this->openbay->fba->setEncryptionKey($this->request->post['openbay_fba_encryption_key']);
    248             $this->openbay->fba->setEncryptionIv($this->request->post['openbay_fba_encryption_iv']);
    249 
    250             $response = $this->openbay->fba->call("v1/fba/status/", array(), 'GET');
    251         } else {
    252             $response = array(
    253                 "result" => null,
    254                 "error" => true,
    255                 "error_messages" => $errors,
    256             );
    257         }
    258 
    259         $this->response->addHeader('Content-Type: application/json');
    260         $this->response->setOutput(json_encode($response));
    261     }
    262 
    263     protected function validate() {
    264         if (!$this->user->hasPermission('modify', 'extension/openbay/fba')) {
    265             $this->error['warning'] = $this->language->get('error_permission');
    266         }
    267 
    268         if (!$this->request->post['openbay_fba_api_key']) {
    269             $this->error['api_key'] = $this->language->get('error_api_key');
    270         }
    271 
    272         if (!$this->request->post['openbay_fba_api_account_id']) {
    273             $this->error['api_account_id'] = $this->language->get('error_api_account_id');
    274         }
    275 
    276         return !$this->error;
    277     }
    278 
    279     public function fulfillment() {
    280         $data = $this->load->language('extension/openbay/fba_fulfillment');
    281 
    282         $this->document->setTitle($this->language->get('heading_title'));
    283         $this->document->addScript('view/javascript/openbay/js/faq.js');
    284 
    285         if (!isset($this->request->get['fulfillment_id'])) {
    286             $this->response->redirect($this->url->link('extension/openbay/fba/fulfillmentlist', 'user_token=' . $this->session->data['user_token'] . (!empty($this->request->get['filter_date']) ? '&filter_date=' . $this->request->get['filter_date'] : ''), true));
    287         }
    288 
    289         $data['breadcrumbs'] = array();
    290 
    291         $data['breadcrumbs'][] = array(
    292             'href'      => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true),
    293             'text'      => $this->language->get('text_home'),
    294         );
    295 
    296         $data['breadcrumbs'][] = array(
    297             'href' => $this->url->link('marketplace/openbay', 'user_token=' . $this->session->data['user_token'], true),
    298             'text' => $this->language->get('text_openbay'),
    299         );
    300 
    301         $data['breadcrumbs'][] = array(
    302             'href' => $this->url->link('extension/openbay/fba', 'user_token=' . $this->session->data['user_token'], true),
    303             'text' => $data['text_fba'],
    304         );
    305 
    306         $data['breadcrumbs'][] = array(
    307             'href' => $this->url->link('extension/openbay/fba/fulfillmentlist', 'user_token=' . $this->session->data['user_token'] . (!empty($this->request->get['filter_date']) ? '&filter_date=' . $this->request->get['filter_date'] : ''), true),
    308             'text' => $data['heading_title'],
    309         );
    310 
    311         $response = $this->openbay->fba->call("v1/fba/fulfillments/" . $this->request->get['fulfillment_id'] . "/", array());
    312         $data['response'] = $response['body'];
    313 
    314         if ($response['error'] == true || $response['response_http'] != 200) {
    315             $this->session->data['error'] = $this->language->get('error_loading_fulfillment');
    316 
    317             $this->response->redirect($this->url->link('extension/openbay/fba/fulfillmentlist', 'user_token=' . $this->session->data['user_token'], true));
    318         }
    319 
    320         $data['user_token'] = $this->session->data['user_token'];
    321 
    322         if (isset($this->session->data['error'])) {
    323             $data['error_warning'] = $this->session->data['error'];
    324             unset($this->session->data['error']);
    325         } else {
    326             $data['error_warning'] = '';
    327         }
    328 
    329         if (isset($this->session->data['success'])) {
    330             $data['success'] = $this->session->data['success'];
    331 
    332             unset($this->session->data['success']);
    333         } else {
    334             $data['success'] = '';
    335         }
    336 
    337         $data['header'] = $this->load->controller('common/header');
    338         $data['column_left'] = $this->load->controller('common/column_left');
    339         $data['footer'] = $this->load->controller('common/footer');
    340 
    341         $this->response->setOutput($this->load->view('extension/openbay/fba_fulfillment_form', $data));
    342     }
    343 
    344     public function fulfillmentList() {
    345         $data = $this->load->language('extension/openbay/fba_fulfillment_list');
    346 
    347         $this->document->setTitle($this->language->get('heading_title'));
    348         $this->document->addScript('view/javascript/openbay/js/faq.js');
    349 
    350         $data['breadcrumbs'] = array();
    351 
    352         $data['breadcrumbs'][] = array(
    353             'href'      => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true),
    354             'text'      => $this->language->get('text_home'),
    355         );
    356 
    357         $data['breadcrumbs'][] = array(
    358             'href' => $this->url->link('marketplace/openbay', 'user_token=' . $this->session->data['user_token'], true),
    359             'text' => $this->language->get('text_openbay'),
    360         );
    361 
    362         $data['breadcrumbs'][] = array(
    363             'href' => $this->url->link('extension/openbay/fba', 'user_token=' . $this->session->data['user_token'], true),
    364             'text' => $data['text_fba'],
    365         );
    366 
    367         $data['breadcrumbs'][] = array(
    368             'href' => $this->url->link('extension/openbay/fba', 'user_token=' . $this->session->data['user_token'], true),
    369             'text' => $data['heading_title'],
    370         );
    371 
    372         if (isset($this->request->get['filter_date'])) {
    373             $data['filter_date'] = $this->request->get['filter_date'];
    374             $request_url = "?query_start_date_time=".urlencode($this->request->get['filter_date'] . "T00:00:00Z");
    375         } else {
    376             $data['filter_date'] = '';
    377             $request_url = "";
    378         }
    379 
    380         $data['fulfillments'] = array();
    381 
    382         $response = $this->openbay->fba->call("v1/fba/fulfillments/".$request_url, array(), 'GET');
    383 
    384         if (isset($response['body']) && is_array($response['body'])) {
    385             foreach ($response['body'] as $fulfillment_order) {
    386                 $data['fulfillments'][] = array(
    387                     'seller_fulfillment_order_id' => $fulfillment_order['seller_fulfillment_order_id'],
    388                     'displayable_order_id' => $fulfillment_order['displayable_order_id'],
    389                     'displayable_order_date_time' => $fulfillment_order['displayable_order_date_time'],
    390                     'shipping_speed_category' => $fulfillment_order['shipping_speed_category'],
    391                     'fulfillment_order_status' => $fulfillment_order['fulfillment_order_status'],
    392                     'edit' => $this->url->link('extension/openbay/fba/fulfillment', 'user_token=' . $this->session->data['user_token'] . '&fulfillment_id=' . $fulfillment_order['seller_fulfillment_order_id'] . (!empty($data['filter_date']) ? '&filter_date=' . $data['filter_date'] : ''), true),
    393                 );
    394             }
    395         }
    396 
    397         $data['cancel'] = $this->url->link('extension/openbay/fba/index', 'user_token=' . $this->session->data['user_token'], true);
    398         $data['user_token'] = $this->session->data['user_token'];
    399 
    400         if (isset($this->session->data['error'])) {
    401             $data['error_warning'] = $this->session->data['error'];
    402             unset($this->session->data['error']);
    403         } else {
    404             $data['error_warning'] = '';
    405         }
    406 
    407         if (isset($this->session->data['success'])) {
    408             $data['success'] = $this->session->data['success'];
    409 
    410             unset($this->session->data['success']);
    411         } else {
    412             $data['success'] = '';
    413         }
    414 
    415         $data['header'] = $this->load->controller('common/header');
    416         $data['column_left'] = $this->load->controller('common/column_left');
    417         $data['footer'] = $this->load->controller('common/footer');
    418 
    419         $this->response->setOutput($this->load->view('extension/openbay/fba_fulfillment_list', $data));
    420     }
    421 
    422     public function shipFulfillment() {
    423         $this->load->language('extension/openbay/fba_fulfillment');
    424 
    425         $errors = array();
    426 
    427         if (empty($this->request->get['order_id']) || empty($this->request->get['fba_order_fulfillment_id'])) {
    428             $this->session->data['error'] = $this->language->get('error_missing_id');
    429 
    430             $this->response->redirect($this->url->link('extension/openbay/fba/orderlist', 'user_token=' . $this->session->data['user_token'], true));
    431         } else {
    432             $order_id = (int)$this->request->get['order_id'];
    433             $fba_order_fulfillment_id = (int)$this->request->get['fba_order_fulfillment_id'];
    434 
    435             $this->openbay->fba->log('shipFulfillment request for order ID: ' . $order_id . ', Fulfillment ID: ' . $fba_order_fulfillment_id);
    436 
    437             $fba_fulfillment_id = $this->openbay->fba->createFBAFulfillmentID($order_id, 1);
    438 
    439             $response = $this->openbay->fba->call("v1/fba/fulfillments/" . $this->config->get('openbay_fba_order_prefix') . $order_id . '-' . $fba_order_fulfillment_id . "/ship/", array(), 'GET');
    440 
    441 
    442 
    443             if (!isset($response['response_http']) || $response['response_http'] != 200) {
    444                 /**
    445                  * @todo notify the admin about any errors
    446                  */
    447                 $errors[] = $this->language->get('error_amazon_request');
    448 
    449                 //$this->openbay->fba->updateFBAOrderStatus($order_id, 1);
    450             } else {
    451                 $this->openbay->fba->populateFBAFulfillment(json_encode(array()), json_encode($response), $response['response_http'], $fba_fulfillment_id);
    452 
    453                 $this->openbay->fba->updateFBAOrderStatus($order_id, 3);
    454 
    455                 $this->session->data['success'] = $this->language->get('text_fulfillment_shipped');
    456             }
    457         }
    458 
    459         if ($errors) {
    460             $this->session->data['error'] = $errors;
    461         }
    462 
    463         $this->response->redirect($this->url->link('extension/openbay/fba/order', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order_id, true));
    464     }
    465 
    466     public function cancelFulfillment() {
    467         $this->load->language('extension/openbay/fba_fulfillment');
    468 
    469         $errors = array();
    470 
    471         if (empty($this->request->get['order_id']) || empty($this->request->get['fba_order_fulfillment_id'])) {
    472             $this->session->data['error'] = $this->language->get('error_missing_id');
    473 
    474             $this->response->redirect($this->url->link('extension/openbay/fba/orderlist', 'user_token=' . $this->session->data['user_token'], true));
    475         } else {
    476             $order_id = (int)$this->request->get['order_id'];
    477             $fba_order_fulfillment_id = (int)$this->request->get['fba_order_fulfillment_id'];
    478 
    479             $this->openbay->fba->log('cancelFulfillment request for order ID: ' . $order_id . ', Fulfillment ID: ' . $fba_order_fulfillment_id);
    480 
    481             $fba_fulfillment_id = $this->openbay->fba->createFBAFulfillmentID($order_id, 2);
    482 
    483             $response = $this->openbay->fba->call("v1/fba/fulfillments/" . $this->config->get('openbay_fba_order_prefix') . $order_id . '-' . $fba_order_fulfillment_id . "/cancel/", array(), 'POST');
    484 
    485             if (!isset($response['response_http']) || $response['response_http'] != 200) {
    486                 /**
    487                  * @todo notify the admin about any errors
    488                  */
    489                 $errors[] = $this->language->get('error_amazon_request');
    490             } else {
    491                 $this->openbay->fba->populateFBAFulfillment(json_encode(array()), json_encode($response), $response['response_http'], $fba_fulfillment_id);
    492 
    493                 $this->openbay->fba->updateFBAOrderStatus($order_id, 4);
    494 
    495                 $this->session->data['success'] = $this->language->get('text_fulfillment_cancelled');
    496             }
    497         }
    498 
    499         if ($errors) {
    500             $this->session->data['error'] = $errors;
    501         }
    502 
    503         $this->response->redirect($this->url->link('extension/openbay/fba/order', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order_id, true));
    504     }
    505 
    506     public function resendFulfillment() {
    507         $this->load->language('extension/openbay/fba_fulfillment');
    508 
    509         $errors = array();
    510 
    511         if (empty($this->request->get['order_id'])) {
    512             $this->session->data['error'] = $this->language->get('error_missing_id');
    513 
    514             $this->response->redirect($this->url->link('extension/openbay/fba/orderlist', 'user_token=' . $this->session->data['user_token'], true));
    515         } else {
    516             $order_id = (int)$this->request->get['order_id'];
    517 
    518             $this->openbay->fba->log('resendFulfillment request for order ID: ' . $order_id);
    519 
    520             $this->load->model('sale/order');
    521             $this->load->model('catalog/product');
    522 
    523             $order = $this->model_sale_order->getOrder($order_id);
    524 
    525             if ($order['shipping_method']) {
    526                 if ($this->config->get('openbay_fba_order_trigger_status') == $order['order_status_id']) {
    527                     $fba_fulfillment_id = $this->openbay->fba->createFBAFulfillmentID($order_id, 1);
    528 
    529                     $order_products = $this->model_sale_order->getOrderProducts($order_id);
    530 
    531                     $fulfillment_items = array();
    532 
    533                     foreach ($order_products as $order_product) {
    534                         $product = $this->model_catalog_product->getProduct($order_product['product_id']);
    535 
    536                         if ($product['location'] == 'FBA') {
    537                             $fulfillment_items[] = array(
    538                                 'seller_sku' => $product['sku'],
    539                                 'quantity' => $order_product['quantity'],
    540                                 'seller_fulfillment_order_item_id' => $this->config->get('openbay_fba_order_prefix') . $fba_fulfillment_id . '-' . $order_product['order_product_id'],
    541                                 'per_unit_declared_value' => array(
    542                                     'currency_code' => $order['currency_code'],
    543                                     'value' => number_format($order_product['price'], 2)
    544                                 ),
    545                             );
    546                         }
    547                     }
    548 
    549                     $total_fulfillment_items = count($fulfillment_items);
    550 
    551                     if (!empty($fulfillment_items)) {
    552                         $request = array();
    553 
    554                         $datetime = new DateTime($order['date_added']);
    555                         $request['displayable_order_datetime'] = $datetime->format(DateTime::ISO8601);
    556 
    557                         $request['seller_fulfillment_order_id'] = $this->config->get('openbay_fba_order_prefix') . $order_id . '-' . $fba_fulfillment_id;
    558                         $request['displayable_order_id'] = $order_id;
    559                         $request['displayable_order_comment'] = 'none';
    560                         $request['shipping_speed_category'] = $this->config->get('openbay_fba_shipping_speed');
    561                         $request['fulfillment_action'] = ($this->config->get('openbay_fba_send_orders') == 1 ? 'Ship' : 'Hold');
    562                         $request['fulfillment_policy'] = $this->config->get('openbay_fba_fulfill_policy');
    563 
    564                         $request['destination_address'] = array(
    565                             'name' => $order['shipping_firstname'] . ' ' . $order['shipping_lastname'],
    566                             'line_1' => (!empty($order['shipping_company']) ? $order['shipping_company'] : $order['shipping_address_1']),
    567                             'line_2' => (!empty($order['shipping_company']) ? $order['shipping_address_1'] : $order['shipping_address_2']),
    568                             'line_3' => (!empty($order['shipping_company']) ? $order['shipping_address_2'] : ''),
    569                             'state_or_province_code' => $order['shipping_zone'],
    570                             'city' => $order['shipping_city'],
    571                             'country_code' => $order['shipping_iso_code_2'],
    572                             'postal_code' => $order['shipping_postcode'],
    573                         );
    574 
    575                         $request['items'] = $fulfillment_items;
    576 
    577                         $response = $this->openbay->fba->call("v1/fba/fulfillments/", $request, 'POST');
    578 
    579                         if ($response['response_http'] != 201) {
    580                             /**
    581                              * @todo notify the admin about any errors
    582                              */
    583                             $errors[] = $this->language->get('error_amazon_request');
    584 
    585                             $this->openbay->fba->updateFBAOrderStatus($order_id, 1);
    586                         } else {
    587                             if ($this->config->get('openbay_fba_send_orders') == 1) {
    588                                 $this->openbay->fba->updateFBAOrderStatus($order_id, 3);
    589                             } else {
    590                                 $this->openbay->fba->updateFBAOrderStatus($order_id, 2);
    591                             }
    592 
    593                             $this->openbay->fba->updateFBAOrderRef($order_id, $this->config->get('openbay_fba_order_prefix') . $order_id . '-' . $fba_fulfillment_id);
    594 
    595                             $this->session->data['success'] = $this->language->get('text_fulfillment_sent');
    596                         }
    597 
    598                         $this->openbay->fba->populateFBAFulfillment(json_encode($request), json_encode($response), $response['response_http'], $fba_fulfillment_id);
    599                         $this->openbay->fba->updateFBAOrderFulfillmentID($order_id, $fba_fulfillment_id);
    600                     } else {
    601                         $errors[] = $this->language->get('error_no_items');
    602                     }
    603                 }
    604             } else {
    605                 $errors[] = $this->language->get('error_no_shipping');
    606             }
    607         }
    608 
    609         if ($errors) {
    610             $this->session->data['error'] = $errors;
    611         }
    612 
    613         $this->response->redirect($this->url->link('extension/openbay/fba/order', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order_id, true));
    614     }
    615 
    616     public function orderList() {
    617         $data = $this->load->language('extension/openbay/fba_order');
    618 
    619         $this->document->setTitle($this->language->get('heading_title'));
    620         $this->document->addScript('view/javascript/openbay/js/faq.js');
    621 
    622         $data['breadcrumbs'] = array();
    623 
    624         $data['breadcrumbs'][] = array(
    625             'href'      => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true),
    626             'text'      => $this->language->get('text_home'),
    627         );
    628 
    629         $data['breadcrumbs'][] = array(
    630             'href' => $this->url->link('marketplace/openbay', 'user_token=' . $this->session->data['user_token'], true),
    631             'text' => $this->language->get('text_openbay'),
    632         );
    633 
    634         $data['breadcrumbs'][] = array(
    635             'href' => $this->url->link('extension/openbay/fba', 'user_token=' . $this->session->data['user_token'], true),
    636             'text' => $data['text_fba'],
    637         );
    638 
    639         $data['breadcrumbs'][] = array(
    640             'href' => $this->url->link('extension/openbay/fba', 'user_token=' . $this->session->data['user_token'], true),
    641             'text' => $data['heading_title'],
    642         );
    643 
    644         $filters = array();
    645 
    646         $url = '';
    647 
    648         if (isset($this->request->get['filter_start'])) {
    649             $filters['filter_start'] = $this->request->get['filter_start'];
    650             $data['filter_start'] = $this->request->get['filter_start'];
    651             $url .= "&filter_start=".urlencode($this->request->get['filter_start']);
    652         } else {
    653             $filters['filter_start'] = null;
    654             $data['filter_start'] = null;
    655         }
    656 
    657         if (isset($this->request->get['filter_end'])) {
    658             $filters['filter_end'] = $this->request->get['filter_end'];
    659             $data['filter_end'] = $this->request->get['filter_end'];
    660             $url .= "&filter_end=".urlencode($this->request->get['filter_end']);
    661         } else {
    662             $filters['filter_end'] = null;
    663             $data['filter_end'] = null;
    664         }
    665 
    666         if (isset($this->request->get['filter_status'])) {
    667             $filters['filter_status'] = $this->request->get['filter_status'];
    668             $data['filter_status'] = $this->request->get['filter_status'];
    669             $url .= "&filter_status=".urlencode($this->request->get['filter_status']);
    670         } else {
    671             $filters['filter_status'] = null;
    672             $data['filter_status'] = null;
    673         }
    674 
    675         $data['orders'] = array();
    676 
    677         $orders = $this->openbay->fba->getFBAOrders($filters);
    678 
    679         if (!empty($orders)) {
    680             foreach ($orders as $order) {
    681                 $data['orders'][] = array(
    682                     'order_id' => $order['order_id'],
    683                     'order_link' => $this->url->link('sale/order/info', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order['order_id'] . $url, true),
    684                     'status' => $order['status'],
    685                     'created' => $order['created'],
    686                     'fba_item_count' => $this->openbay->fba->hasOrderFBAItems($order['order_id']),
    687                     'view' => $this->url->link('extension/openbay/fba/order', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order['order_id'] . $url, true)
    688                 );
    689             }
    690         }
    691 
    692         $data['cancel'] = $this->url->link('extension/openbay/fba/index', 'user_token=' . $this->session->data['user_token'], true);
    693         $data['user_token'] = $this->session->data['user_token'];
    694 
    695         $data['status_options'] = array(
    696             0 => $this->language->get('text_option_new'),
    697             1 => $this->language->get('text_option_error'),
    698             2 => $this->language->get('text_option_held'),
    699             3 => $this->language->get('text_option_shipped'),
    700         );
    701 
    702         if (isset($this->session->data['error'])) {
    703             $data['error_warning'] = $this->session->data['error'];
    704             unset($this->session->data['error']);
    705         } else {
    706             $data['error_warning'] = '';
    707         }
    708 
    709         if (isset($this->session->data['success'])) {
    710             $data['success'] = $this->session->data['success'];
    711 
    712             unset($this->session->data['success']);
    713         } else {
    714             $data['success'] = '';
    715         }
    716 		
    717         $data['header'] = $this->load->controller('common/header');
    718         $data['column_left'] = $this->load->controller('common/column_left');
    719         $data['footer'] = $this->load->controller('common/footer');
    720 
    721         $this->response->setOutput($this->load->view('extension/openbay/fba_order_list', $data));
    722     }
    723 
    724     public function order() {
    725         $data = $this->load->language('extension/openbay/fba_order');
    726 
    727         $this->document->setTitle($this->language->get('heading_title'));
    728         $this->document->addScript('view/javascript/openbay/js/faq.js');
    729 
    730         $this->load->model('sale/order');
    731         $this->load->model('catalog/product');
    732 
    733         if (!isset($this->request->get['order_id'])) {
    734             $this->response->redirect($this->url->link('extension/openbay/fba/orderList', 'user_token=' . $this->session->data['user_token'], true));
    735         }
    736 
    737         $order_id = (int)$this->request->get['order_id'];
    738         $order_fba = $this->openbay->fba->getFBAOrder($order_id);
    739         $order_info = $this->model_sale_order->getOrder($order_id);
    740 
    741         if ($order_fba['status'] == 2 || $order_fba['status'] == 3 || $order_fba['status'] == 4) {
    742             $data['fulfillment_id'] = $order_fba['fba_order_fulfillment_ref'];
    743             $data['fulfillment_link'] = $this->url->link('extension/openbay/fba/fulfillment', 'user_token=' . $this->session->data['user_token'] . '&fulfillment_id=' . $data['fulfillment_id'], true);
    744         } else {
    745             $data['fulfillment_id'] = '';
    746             $data['fulfillment_link'] = '';
    747         }
    748 
    749         $data['fba_order_status'] = $order_fba['status'];
    750         $data['order_id'] = $order_id;
    751         $data['order_link'] = $this->url->link('sale/order/info', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order_id, true);
    752         $data['resend_link'] = $this->url->link('extension/openbay/fba/resendfulfillment', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order_id, true);
    753         $data['ship_link'] = $this->url->link('extension/openbay/fba/shipfulfillment', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order_id . '&fba_order_fulfillment_id=' . $order_fba['fba_order_fulfillment_id'], true);
    754         $data['cancel_link'] = $this->url->link('extension/openbay/fba/cancelfulfillment', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order_id . '&fba_order_fulfillment_id=' . $order_fba['fba_order_fulfillment_id'], true);
    755 
    756         $data['cancel'] = $this->url->link('extension/openbay/fba/orderlist', 'user_token=' . $this->session->data['user_token'], true);
    757 
    758         $data['breadcrumbs'] = array();
    759 
    760         $data['breadcrumbs'][] = array(
    761             'href'      => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true),
    762             'text'      => $this->language->get('text_home'),
    763         );
    764 
    765         $data['breadcrumbs'][] = array(
    766             'href' => $this->url->link('marketplace/openbay', 'user_token=' . $this->session->data['user_token'], true),
    767             'text' => $this->language->get('text_openbay'),
    768         );
    769 
    770         $data['breadcrumbs'][] = array(
    771             'href' => $this->url->link('extension/openbay/fba', 'user_token=' . $this->session->data['user_token'], true),
    772             'text' => $data['text_fba'],
    773         );
    774 
    775         $data['breadcrumbs'][] = array(
    776             'href' => $this->url->link('extension/openbay/fba/orderlist', 'user_token=' . $this->session->data['user_token'], true),
    777             'text' => $data['heading_title'],
    778         );
    779 
    780         $data['fulfillments'] = array();
    781 
    782         if (is_array($order_fba['fulfillments'])) {
    783             foreach ($order_fba['fulfillments'] as $fulfillment) {
    784                 $response_body = json_decode($fulfillment['response_body'], true);
    785 
    786                 $fulfillment_errors = array();
    787 
    788                 if (isset($response_body['error']) && $response_body['error'] == true) {
    789                     if (is_array($response_body['error_messages']) && !empty($response_body['error_messages'])) {
    790                         $fulfillment_errors = $response_body['error_messages'];
    791                     }
    792                 }
    793 
    794                 $data['fulfillments'][] = array(
    795                     'fba_order_fulfillment_id' => $fulfillment['fba_order_fulfillment_id'],
    796                     'created' => $fulfillment['created'],
    797                     'request_body' => json_decode($fulfillment['request_body']),
    798                     'request_body_output' => print_r(json_decode($fulfillment['request_body']), true),
    799                     'response_body' => json_decode($fulfillment['response_body']),
    800                     'response_body_output' => print_r(json_decode($fulfillment['response_body']), true),
    801                     'response_header_code' => $fulfillment['response_header_code'],
    802                     'errors' => $fulfillment_errors,
    803                     'type' => $fulfillment['type'],
    804                 );
    805             }
    806         }
    807 
    808         // Shipping Address
    809         if ($order_info['shipping_address_format']) {
    810             $format = $order_info['shipping_address_format'];
    811         } else {
    812             $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
    813         }
    814 
    815         $find = array(
    816             '{firstname}',
    817             '{lastname}',
    818             '{company}',
    819             '{address_1}',
    820             '{address_2}',
    821             '{city}',
    822             '{postcode}',
    823             '{zone}',
    824             '{zone_code}',
    825             '{country}'
    826         );
    827 
    828         $replace = array(
    829             'firstname' => $order_info['shipping_firstname'],
    830             'lastname'  => $order_info['shipping_lastname'],
    831             'company'   => $order_info['shipping_company'],
    832             'address_1' => $order_info['shipping_address_1'],
    833             'address_2' => $order_info['shipping_address_2'],
    834             'city'      => $order_info['shipping_city'],
    835             'postcode'  => $order_info['shipping_postcode'],
    836             'zone'      => $order_info['shipping_zone'],
    837             'zone_code' => $order_info['shipping_zone_code'],
    838             'country'   => $order_info['shipping_country']
    839         );
    840 
    841         $data['shipping_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));
    842 
    843         $data['status_options'] = array(
    844             0 => $this->language->get('text_option_new'),
    845             1 => $this->language->get('text_option_error'),
    846             2 => $this->language->get('text_option_held'),
    847             3 => $this->language->get('text_option_shipped'),
    848             4 => $this->language->get('text_option_cancelled'),
    849         );
    850 
    851         $data['type_options'] = array(
    852             0 => $this->language->get('text_type_new'),
    853             1 => $this->language->get('text_type_ship'),
    854             2 => $this->language->get('text_type_cancel'),
    855         );
    856 
    857         $data['products'] = array();
    858 
    859         $products = $this->model_sale_order->getOrderProducts($this->request->get['order_id']);
    860 
    861         foreach ($products as $product) {
    862             $option_data = array();
    863 
    864             $product_info = $this->model_catalog_product->getProduct($product['product_id']);
    865 
    866             $options = $this->model_sale_order->getOrderOptions($this->request->get['order_id'], $product['order_product_id']);
    867 
    868             foreach ($options as $option) {
    869                 if ($option['type'] != 'file') {
    870                     $option_data[] = array(
    871                         'name'  => $option['name'],
    872                         'value' => $option['value'],
    873                         'type'  => $option['type']
    874                     );
    875                 }
    876             }
    877 
    878             $data['products'][] = array(
    879                 'order_product_id' => $product['order_product_id'],
    880                 'product_id'       => $product['product_id'],
    881                 'name'    	 	   => $product['name'],
    882                 'sku'    		   => $product_info['sku'],
    883                 'option'   		   => $option_data,
    884                 'quantity'		   => $product['quantity'],
    885                 'fba'		       => ($product_info['location'] == 'FBA' ? 1 : 0),
    886                 'href'     		   => $this->url->link('catalog/product/edit', 'user_token=' . $this->session->data['user_token'] . '&product_id=' . $product['product_id'], true),
    887             );
    888         }
    889 
    890         if (isset($this->session->data['error'])) {
    891             $data['error_warning'] = $this->session->data['error'];
    892             unset($this->session->data['error']);
    893         } else {
    894             $data['error_warning'] = '';
    895         }
    896 
    897         if (isset($this->session->data['success'])) {
    898             $data['success'] = $this->session->data['success'];
    899             unset($this->session->data['success']);
    900         } else {
    901             $data['success'] = '';
    902         }
    903 
    904 
    905         $data['header'] = $this->load->controller('common/header');
    906         $data['column_left'] = $this->load->controller('common/column_left');
    907         $data['footer'] = $this->load->controller('common/footer');
    908 
    909         $this->response->setOutput($this->load->view('extension/openbay/fba_order_info', $data));
    910     }
    911 }