public function displayMainImportPage()
 {
     $recent_reports = WPLA_AmazonReport::getRecentInventoryReports();
     $reports_in_progress = $this->checkReportsInProgress();
     $default_account = WPLA_AmazonAccount::getAccount(get_option('wpla_default_account_id'));
     $aData = array('plugin_url' => self::$PLUGIN_URL, 'message' => $this->message, 'recent_reports' => $recent_reports, 'reports_in_progress' => $reports_in_progress, 'reports_update_woo_stock' => get_option('wpla_reports_update_woo_stock', 1), 'reports_update_woo_price' => get_option('wpla_reports_update_woo_price', 1), 'reports_update_woo_condition' => get_option('wpla_reports_update_woo_condition', 1), 'import_creates_all_variations' => get_option('wpla_import_creates_all_variations', 0), 'import_variations_as_simple' => get_option('wpla_import_variations_as_simple', 0), 'default_account_title' => $default_account ? $default_account->title : 'invalid!', 'form_action' => 'admin.php?page=' . self::ParentMenuId . '-import');
     $this->display('import/import_page', $aData);
 }
 public function handleActions()
 {
     // trigger orders update
     if ($this->requestAction() == 'update_orders') {
         do_action('wpla_update_orders');
     }
     // load order items
     if ($this->requestAction() == 'load_order_items') {
         $lm = new WPLA_OrdersModel();
         $order = $lm->getItem($_REQUEST['amazon_order']);
         if (!$order) {
             return;
         }
         $account = WPLA_AmazonAccount::getAccount($order['account_id']);
         if (!$account) {
             return;
         }
         $api = new WPLA_AmazonAPI($account->id);
         // get report requests
         $items = $api->getOrderLineItems($order['order_id']);
         // echo "<pre>";print_r($items);echo"</pre>";die();
         if (is_array($items)) {
             // run the import
             $this->importOrderItems($items, $order['order_id']);
             $this->showMessage(sprintf(__('%s item(s) were processed for account %s.', 'wpla'), sizeof($items), $account->title));
         } elseif ($items->Error->Message) {
             $this->showMessage(sprintf(__('There was a problem downloading items for account %s.', 'wpla'), $account->title) . '<br>Error: ' . $items->Error->Message, 1);
         } else {
             $this->showMessage(sprintf(__('There was a problem downloading items for account %s.', 'wpla'), $account->title), 1);
         }
     }
     // handle update from Amazon action
     if ($this->requestAction() == 'update') {
         $this->updateOrdersfromAmazon($_REQUEST['amazon_order']);
         // $this->showMessage( __('Not implemented yet.','wpla') );
     }
     // handle delete action
     if ($this->requestAction() == 'delete') {
         $this->deleteOrders($_REQUEST['amazon_order']);
         $this->showMessage(__('Selected items were removed.', 'wpla'));
     }
 }
 public function displayAccountsPage()
 {
     // handle actions and show notes
     $this->handleActions();
     if ($this->requestAction() == 'save_account') {
         $this->saveAccount();
     }
     if ($this->requestAction() == 'edit_account') {
         return $this->displayEditAccountsPage();
     }
     if ($default_account_id = get_option('wpla_default_account_id')) {
         $default_account = WPLA_AmazonAccount::getAccount($default_account_id);
         if (!$default_account) {
             $this->showMessage(__('Your default account does not exist anymore. Please select a new default account.', 'wpla'), 1);
         }
     }
     // create table and fetch items to show
     $this->accountsTable = new WPLA_AccountsTable();
     $this->accountsTable->prepare_items();
     $active_tab = 'accounts';
     $aData = array('plugin_url' => self::$PLUGIN_URL, 'message' => $this->message, 'accountsTable' => $this->accountsTable, 'amazon_markets' => WPLA_AmazonMarket::getAll(), 'amazon_accounts' => WPLA_AmazonAccount::getAll(true), 'default_account' => get_option('wpla_default_account_id'), 'settings_url' => 'admin.php?page=' . self::ParentMenuId . '-settings', 'form_action' => 'admin.php?page=' . self::ParentMenuId . '-settings' . '&tab=' . $active_tab);
     $this->display('settings_accounts', $aData);
 }
?>
" name="submit" class="button">
								<p>
									<?php 
echo __('This will find products with ASINs which do not exist on the Listings page. It will then create new listings with the status "matched".', 'wpla');
?>
								</p>
								<p>
									<?php 
echo __('If you have added ASINs to your WooCommerce products using a CSV import tool, use this to add them to WP-Lister automatically.', 'wpla');
?>
								</p>
								<p>
									<?php 
$default_account_id = get_option('wpla_default_account_id', 1);
$account = WPLA_AmazonAccount::getAccount($default_account_id);
?>
									<i>Default Account: <?php 
echo $account->title;
?>
 (<?php 
echo $account->market_code;
?>
)</i> <br>
									<i>Maximum batch size: 1000 items</i> <br>
								</p>
						</form>
						<!br style="clear:both;"/>

					</div>
				</div> <!-- postbox -->
 function column_account_id($item)
 {
     $account_id = $item['account_id'];
     if ($account = WPLA_AmazonAccount::getAccount($account_id)) {
         return $account->title;
     }
     // return $item['account_id'];
 }
 public function process_product_meta_variable($post_id)
 {
     WPLA()->logger->info('process_product_meta_variable() - ' . $post_id);
     if (!isset($_POST['variable_sku'])) {
         return;
     }
     $variable_post_id = $_POST['variable_post_id'];
     $variable_amazon_product_id = $_POST['variable_amazon_product_id'];
     $variable_amazon_id_type = $_POST['variable_amazon_id_type'];
     $variable_amazon_asin = $_POST['variable_amazon_asin'];
     $variable_sku = $_POST['variable_sku'];
     $variable_amazon_price = isset($_POST['variable_amazon_price']) ? $_POST['variable_amazon_price'] : '';
     $variable_amazon_minimum_price = isset($_POST['variable_amazon_minimum_price']) ? $_POST['variable_amazon_minimum_price'] : '';
     $variable_amazon_maximum_price = isset($_POST['variable_amazon_maximum_price']) ? $_POST['variable_amazon_maximum_price'] : '';
     $variable_amazon_condition_type = isset($_POST['variable_amazon_condition_type']) ? $_POST['variable_amazon_condition_type'] : '';
     $variable_amazon_condition_note = isset($_POST['variable_amazon_condition_note']) ? $_POST['variable_amazon_condition_note'] : '';
     $variable_amazon_is_disabled = isset($_POST['variable_amazon_is_disabled']) ? $_POST['variable_amazon_is_disabled'] : '';
     // convert decimal comma for all price fields
     $variable_amazon_price = str_replace(',', '.', $variable_amazon_price);
     $variable_amazon_minimum_price = str_replace(',', '.', $variable_amazon_minimum_price);
     $variable_amazon_maximum_price = str_replace(',', '.', $variable_amazon_maximum_price);
     $lm = new WPLA_ListingsModel();
     $all_variations_with_SKU = array();
     $all_variations_with_ASIN = array();
     $max_loop = max(array_keys($_POST['variable_post_id']));
     for ($i = 0; $i <= $max_loop; $i++) {
         if (!isset($variable_post_id[$i])) {
             continue;
         }
         $variation_id = (int) $variable_post_id[$i];
         // Update post meta
         update_post_meta($variation_id, '_amazon_product_id', trim($variable_amazon_product_id[$i]));
         update_post_meta($variation_id, '_amazon_id_type', $variable_amazon_id_type[$i]);
         update_post_meta($variation_id, '_wpla_asin', trim($variable_amazon_asin[$i]));
         update_post_meta($variation_id, '_amazon_price', isset($variable_amazon_price[$i]) ? trim($variable_amazon_price[$i]) : '');
         update_post_meta($variation_id, '_amazon_minimum_price', isset($variable_amazon_minimum_price[$i]) ? trim($variable_amazon_minimum_price[$i]) : '');
         update_post_meta($variation_id, '_amazon_maximum_price', isset($variable_amazon_maximum_price[$i]) ? trim($variable_amazon_maximum_price[$i]) : '');
         update_post_meta($variation_id, '_amazon_condition_type', isset($variable_amazon_condition_type[$i]) ? trim($variable_amazon_condition_type[$i]) : '');
         update_post_meta($variation_id, '_amazon_condition_note', isset($variable_amazon_condition_note[$i]) ? trim($variable_amazon_condition_note[$i]) : '');
         update_post_meta($variation_id, '_amazon_is_disabled', isset($variable_amazon_is_disabled[$i]) ? $variable_amazon_is_disabled[$i] : '');
         // if ( $variable_amazon_product_id[$i] !== 'parent' )
         //     update_post_meta( $variation_id, '_amazon_product_id', $variable_amazon_product_id[$i] );
         // else
         //     delete_post_meta( $variation_id, '_amazon_product_id' );
         // update min/max prices in listings table
         if (isset($_POST['variable_amazon_minimum_price'])) {
             $min_price = isset($variable_amazon_minimum_price[$i]) ? $variable_amazon_minimum_price[$i] : '';
             $max_price = isset($variable_amazon_maximum_price[$i]) ? $variable_amazon_maximum_price[$i] : '';
             $data = array();
             if ($min_price || $max_price) {
                 if ($listing = $lm->getItemByPostID($variation_id)) {
                     if ($min_price != $listing->min_price) {
                         $data['min_price'] = $min_price;
                         $data['pnq_status'] = 1;
                         // mark as changed
                     }
                     if ($max_price != $listing->max_price) {
                         $data['max_price'] = $max_price;
                         $data['pnq_status'] = 1;
                         // mark as changed
                     }
                     // update listing
                     if (!empty($data)) {
                         $lm->updateWhere(array('id' => $listing->id), $data);
                     }
                 }
             }
         }
         // collect (matched) variations with ASIN
         if ($variable_amazon_asin[$i]) {
             $all_variations_with_ASIN[$variation_id] = $variable_amazon_asin[$i];
         }
         // collect all variations with SKU
         if ($variable_sku[$i]) {
             $all_variations_with_SKU[$variation_id] = $variable_sku[$i];
         }
     }
     // each variation
     WPLA()->logger->info('Variations with ASIN: ' . print_r($all_variations_with_ASIN, 1));
     WPLA()->logger->info('Variations with SKU : ' . print_r($all_variations_with_SKU, 1));
     // process matched variations
     // check all variations with ASIN and add missing ones to listings table
     if (!empty($all_variations_with_ASIN)) {
         $lm = new WPLA_ListingsModel();
         $default_account_id = get_option('wpla_default_account_id', 1);
         if (!$default_account_id) {
             return;
         }
         // ***
         foreach ($all_variations_with_ASIN as $variation_id => $asin) {
             // check if this ASIN / ID already exist - skip if it does
             WPLA()->logger->info("searching for existing listing for #{$variation_id} / {$asin}");
             if ($lm->getItemByASIN($asin, false)) {
                 continue;
             }
             if ($lm->getItemByPostID($variation_id)) {
                 continue;
             }
             WPLA()->logger->info("no listing found for variation #{$variation_id} / {$asin}");
             // skip hidden variations
             if (get_post_meta($variation_id, '_amazon_is_disabled', true) == 'on') {
                 continue;
             }
             // insert matched listing
             $success = $lm->insertMatchedProduct($variation_id, $asin, $default_account_id);
             $error_msg = isset($lm->lastError) ? $lm->lastError : '';
             if ($success) {
                 // TODO: use persistent admin message
                 WPLA()->logger->info("Matched variation #{$variation_id} / {$asin} - {$error_msg}");
             } else {
                 echo "Failed to match variation #{$variation_id} - please report this to support: {$error_msg}";
                 WPLA()->logger->error("Failed to match variation #{$variation_id} / {$asin} - {$error_msg}");
             }
         }
         // each matched variation
     }
     // if $all_variations_with_ASIN
     // add missing variations
     // if the parent product has a listing item, then check for and add missing variation listings
     $lm = new WPLA_ListingsModel();
     $parent_listing = $lm->getItemByPostID($post_id);
     if ($parent_listing) {
         // get account from parent listing
         $account = WPLA_AmazonAccount::getAccount($parent_listing->account_id);
         if (!$account) {
             return;
         }
         foreach ($all_variations_with_SKU as $variation_id => $sku) {
             // check if this SKU / ID already exist - skip if it does
             if ($lm->getItemBySKU($sku, false)) {
                 continue;
             }
             if ($lm->getItemByPostID($variation_id)) {
                 continue;
             }
             WPLA()->logger->info("no listing found for missing variation #{$variation_id} / {$sku}");
             // check if this variation has a UPC/EAN set - skip if empty (unless brand registry is enabled)
             $_amazon_product_id = get_post_meta($variation_id, '_amazon_product_id', true);
             if (!$_amazon_product_id && !$account->is_reg_brand) {
                 continue;
             }
             // skip hidden variations
             if (get_post_meta($variation_id, '_amazon_is_disabled', true) == 'on') {
                 continue;
             }
             // insert variation listing
             $success = $lm->insertMissingVariation($variation_id, $sku, $parent_listing);
             $error_msg = isset($lm->lastError) ? $lm->lastError : '';
             if ($success) {
                 // TODO: use persistent admin message
                 WPLA()->logger->info("Matched missing variation #{$variation_id} / {$sku} - {$error_msg}");
             } else {
                 echo "Failed to match missing variation #{$variation_id} - please report this to support: {$error_msg}";
                 WPLA()->logger->error("Failed to match missing variation #{$variation_id} / {$sku} - {$error_msg}");
             }
         }
         // each variation
     }
     // if parent listing exists
 }
 static function getUrlForItemObj($item)
 {
     $item = (array) $item;
     $listing_url = 'http://www.amazon.com/dp/' . $item['asin'] . '/';
     // default to US
     if ($item['account_id']) {
         $account = WPLA_AmazonAccount::getAccount($item['account_id']);
         $market = $account ? WPLA_AmazonMarket::getMarket($account->market_id) : false;
         $listing_url = $market ? 'http://www.' . $market->url . '/dp/' . $item['asin'] . '/' : $listing_url;
     }
     return $listing_url;
 }
 public function createProductFromAmazonListing($listing)
 {
     $lm = new WPLA_ListingsModel();
     if (is_numeric($listing)) {
         $listing = $lm->getItem($listing);
     }
     if (!$listing) {
         return false;
     }
     $listing_id = $listing['id'];
     WPLA()->logger->info('--- createProductFromAmazonListing() - ID: ' . $listing['id'] . ' / ASIN ' . $listing['asin']);
     $account = WPLA_AmazonAccount::getAccount($listing['account_id']);
     if (!$account) {
         return false;
     }
     // init api
     $api = new WPLA_AmazonAPI($account->id);
     // get product details from amazon
     $result = $api->getMatchingProductForId($listing['asin'], 'ASIN');
     $this->request_count++;
     // echo "<pre>getMatchingProductForId() returned: ";print_r($result);echo"</pre>";#die();
     // handle Import Variations As Simple option
     if ($result->success && get_option('wpla_import_variations_as_simple', 0)) {
         $result->product->variation_type = '_single_';
     }
     // handle empty result error
     if ($result->success && empty($result->product->AttributeSets->ItemAttributes->Title)) {
         if (!empty($result->product->GetMatchingProductForIdResult->Error->Message)) {
             $this->lastError = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']) . '<br><code>' . $result->product->GetMatchingProductForIdResult->Error->Message . '</code>';
         } else {
             $this->lastError = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']) . ' The product data received from Amazon was empty.';
         }
         return false;
     }
     // first check if product already exists in WooCommerce...
     $post_id = WPLA_ProductBuilder::getProductIdBySKU($listing['sku']);
     if ($post_id) {
         WPLA()->logger->info('found existing product by SKU ' . $listing['sku'] . ' - post_id: ' . $post_id);
         $this->message = "Found existing product for SKU " . $listing['sku'];
         // if this SKU exists, check whether it is a variation or not
         $_product = get_product($post_id);
         // echo "<pre>";print_r($_product);echo"</pre>";#die();
         // handle child variations
         if ('variation' == $_product->product_type) {
             // set parent_id
             $data = array('post_id' => $_product->variation_id, 'parent_id' => $_product->parent->id, 'product_type' => $_product->product_type);
             $lm->updateListing($listing_id, $data);
             $this->message = "Found existing variation for SKU " . $listing['sku'];
         }
     } else {
         // SKU does not exist in WC...
         $variation_type = $result->success ? $result->product->variation_type : '_unknown_';
         $variation_type = is_string($variation_type) ? $variation_type : '_none_';
         // convert empty object to string
         WPLA()->logger->info('no WC product found for SKU ' . $listing['sku'] . ' - type: ' . $variation_type);
         // process child variation - fetch parent item instead
         if ($result->success && $result->product->variation_type == 'child') {
             // foreign imports should have their title updated first
             // $data = array();
             // $data['listing_title'] = $result->product->AttributeSets->ItemAttributes->Title;
             // $lm->updateListing( $listing_id, $data );
             // update listing attributes and title from result
             $lm->updateItemAttributes($result->product->AttributeSets->ItemAttributes, $listing_id);
             // get parent item - new request
             $parent_asin = $result->product->VariationParentASIN;
             $api = new WPLA_AmazonAPI($account->id);
             // new log record
             $parent_result = $api->getMatchingProductForId($parent_asin, 'ASIN');
             $parent_node = $parent_result->success ? $parent_result->product : false;
             $this->request_count++;
             // check for "variations without attributes"
             // if there are no variation attributes on the parent, fall back to creating a simple product instead
             if (0 == self::countVariationChildNodes($parent_node)) {
                 $msg = $listing['asin'] . " seems to be a child of parent ASIN {$parent_asin} - but that parent has no variation attributes set, so it will be imported as a simple product.";
                 WPLA()->logger->warn($msg);
                 wpla_show_message($msg, 'warn');
                 $result->product->variation_type = '_invalid_parent_';
             } else {
                 WPLA()->logger->info($listing['asin'] . " is a child of parent ASIN {$parent_asin}");
                 $result = $parent_result;
             }
         }
         // process parent variation
         if ($result->success && $result->product->variation_type == 'parent') {
             // get parent listing - or create if it doesn't exist yet
             $parent_listing = $this->getOrCreateParentVariation($result, $listing, $account);
             // all further processing should be using the parent variation - including children
             $result->product = $this->parseVariationChildNodes($result->product, $parent_listing, $account);
             // $listing      = $parent_listing; // $listing is supposed to be an array, $parent listing is an object
             $listing_id = $parent_listing->id;
             $listing = $lm->getItem($listing_id);
         }
     }
     // SKU does not exist in WC
     // import product...
     if ($result->success) {
         // update price for foreign imports (imported by ASIN)
         if ('foreign_import' == $listing['source'] && !$listing['price']) {
             $this->updateListingWithLowestPrice($listing, $account);
         }
         // update listing attributes and title from result
         $lm->updateItemAttributes($result->product->AttributeSets->ItemAttributes, $listing_id);
         $listing = $lm->getItem($listing_id);
         // create product
         $woo = new WPLA_ProductBuilder();
         $woo->importSingleProduct($listing, $result->product);
         // post-process variations - add post_id
         if ($result->product->variation_type == 'parent' && isset($result->product->variations)) {
             $this->fixNewVariationListings($result->product->variations, $parent_listing);
         }
         $success = true;
         $errors = '';
         $this->lastPostID = $woo->last_insert_id;
     } elseif (@$result->Error->Message) {
         $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']) . '<br>Error: ' . $result->Error->Message;
         $success = false;
     } else {
         $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']);
         $success = false;
     }
     $this->lastError = $errors;
     return $success;
 }
 public function jobs_run_task()
 {
     // quit if no job name provided
     if (!isset($_REQUEST['job'])) {
         return false;
     }
     if (!isset($_REQUEST['task'])) {
         return false;
     }
     $job = $_REQUEST['job'];
     $task = $_REQUEST['task'];
     // register shutdown handler
     global $wpla_shutdown_handler_enabled;
     $wpla_shutdown_handler_enabled = true;
     register_shutdown_function(array($this, 'shutdown_handler'));
     WPLA()->logger->info('running task: ' . print_r($task, 1));
     // handle job name
     switch ($task['task']) {
         // update listing from Amazon (current used for new listings without ASIN)
         case 'updateProduct':
             // init
             $lm = new WPLA_ListingsModel();
             $listing = $lm->getItem($task['id']);
             $account = WPLA_AmazonAccount::getAccount($listing['account_id']);
             $api = new WPLA_AmazonAPI($account->id);
             // get product attributes
             // $product = $api->getProduct( $listing['asin'] );
             $result = $api->getMatchingProductForId($listing['sku'], 'SellerSKU');
             // echo "<pre>";print_r($product);echo"</pre>";#die();
             // echo "<pre>";print_r($product);echo"</pre>";die();
             if ($result->success) {
                 if (!empty($result->product->ASIN)) {
                     // update listing attributes
                     $listing_id = $listing['id'];
                     // $lm->updateItemAttributes( $product, $listing_id );
                     // $listing = $lm->getItem( $listing_id ); // update values
                     $lm->updateWhere(array('id' => $listing_id), array('asin' => $result->product->ASIN));
                     WPLA()->logger->info('new ASIN for listing #' . $listing['id'] . ': ' . $result->product->ASIN);
                     // update product
                     // $woo = new WPLA_ProductBuilder();
                     // $woo->updateProducts( array( $listing ) );
                     $success = true;
                     $errors = '';
                 } else {
                     $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']);
                     $errors .= ' The product data received from Amazon was empty.';
                     $success = false;
                 }
             } elseif ($result->Error->Message) {
                 $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']) . '<br>Error: ' . $result->Error->Message;
                 $success = false;
             } else {
                 $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']);
                 $success = false;
             }
             // build response
             $response = new stdClass();
             $response->job = $job;
             $response->task = $task;
             $response->errors = empty($errors) ? array() : array(array('HtmlMessage' => $errors));
             $response->success = $success;
             $this->returnJSON($response);
             exit;
             // create new WooCommerce product from imported listing
         // create new WooCommerce product from imported listing
         case 'createProduct':
             // init
             $lm = new WPLA_ListingsModel();
             // $listing = $lm->getItem( $task['id'] );
             $listing_id = $task['id'];
             // create product
             $ProductsImporter = new WPLA_ProductsImporter();
             $success = $ProductsImporter->createProductFromAmazonListing($listing_id);
             $error = $ProductsImporter->lastError;
             $delay = $ProductsImporter->request_count * 1000;
             // ms
             // build response
             $response = new stdClass();
             $response->job = $job;
             $response->task = $task;
             $response->errors = empty($error) ? array() : array(array('HtmlMessage' => $error));
             $response->success = $success;
             $response->delay = $delay;
             $this->returnJSON($response);
             exit;
             // fetch full product description from Amazon and update WooCommerce product
         // fetch full product description from Amazon and update WooCommerce product
         case 'fetchFullProductDescription':
             $webHelper = new WPLA_AmazonWebHelper();
             $webHelper->loadListingDetails($task['id']);
             // echo "<pre>";print_r($webHelper->images);echo"</pre>";#die();
             $lm = new WPLA_ListingsModel();
             $item = $lm->getItem($task['id']);
             if (!empty($webHelper->description)) {
                 // update product
                 $post_id = $item['post_id'];
                 $post_data = array('ID' => $post_id, 'post_content' => trim($webHelper->description));
                 wp_update_post($post_data);
                 $success = true;
                 $errors = '';
             } else {
                 $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $item['asin']);
                 $errors .= ' The product description received from Amazon was empty.';
                 $success = false;
             }
             // build response
             $response = new stdClass();
             $response->job = $job;
             $response->task = $task;
             $response->errors = empty($errors) ? array() : array(array('HtmlMessage' => $errors));
             $response->success = $success;
             $this->returnJSON($response);
             exit;
             // process Merchant or FBA Report and create / update listings
         // process Merchant or FBA Report and create / update listings
         case 'processReportPage':
             // process report page - both Merchant and FBA reports
             $response = WPLA_ImportHelper::ajax_processReportPage($job, $task);
             $this->returnJSON($response);
             exit;
             // process single row (SKU) Merchant or FBA Report - and create / update listings
         // process single row (SKU) Merchant or FBA Report - and create / update listings
         case 'processSingleSkuFromReport':
             // process report page - both Merchant and FBA reports
             $response = WPLA_ImportHelper::ajax_processReportPage($job, $task, true);
             $this->returnJSON($response);
             exit;
         default:
             // echo "unknown task";
             // exit();
     }
 }
 function column_account($item)
 {
     $account = WPLA_AmazonAccount::getAccount($item['account_id']);
     if (!$account) {
         return '<span style="color:red">No Account!</span>';
     }
     return $account->title . ' (' . $account->market_code . ')';
 }
 function wpla_woocommerce_custom_shop_order_columns($column)
 {
     global $post, $woocommerce;
     switch ($column) {
         case 'wpl_order_src':
             $amazon_order_id = get_post_meta($post->ID, '_wpla_amazon_order_id', true);
             $tagged_as_fba = false;
             if ($amazon_order_id) {
                 // get order details
                 $om = new WPLA_OrdersModel();
                 $order = $om->getOrderByOrderID($amazon_order_id);
                 $account = $order ? WPLA_AmazonAccount::getAccount($order->account_id) : false;
                 $tooltip = 'This order was placed on Amazon.';
                 if ($account) {
                     $tooltip .= '<br>(' . $account->title . ')';
                 }
                 echo '<img src="' . WPLA_URL . 'img/amazon-orange-16x16.png" style="width:16px;vertical-align:middle;padding:0;" class="tips" data-tip="' . $tooltip . '" />';
                 // check for FBA
                 if ($order) {
                     $order_details = json_decode($order->details);
                     if (is_object($order_details) && $order_details->FulfillmentChannel == 'AFN') {
                         echo '&nbsp;<small style="font-size:10px;color:silver">' . 'FBA' . '</small>';
                         $tagged_as_fba = true;
                     }
                 }
             }
             // if amazon order
             // show submission status if it exists - for non-amazon orders as well
             if ($submission_status = get_post_meta($post->ID, '_wpla_submission_result', true)) {
                 if ($submission_status == 'success') {
                     echo '<br><img src="' . WPLA_URL . 'img/icon-success-32x32.png" style="width:12px;vertical-align:middle;padding:0;" class="tips" data-tip="This order was marked as shipped on Amazon" />';
                 } else {
                     $history = maybe_unserialize($submission_status);
                     $error_count = is_array($history) ? sizeof(@$history['errors']) : false;
                     if ($error_count) {
                         echo '<br><img src="' . WPLA_URL . 'img/error.gif" style="vertical-align:middle;padding:0;" class="tips" data-tip="There was a problem - this order could not be marked as shipped on Amazon!" />';
                     }
                 }
             }
             // show FBA submission status if it exists - for non-amazon orders as well
             if ($submission_status = get_post_meta($post->ID, '_wpla_fba_submission_status', true)) {
                 if (!$tagged_as_fba) {
                     echo '<small style="font-size:10px;">' . 'FBA' . '</small>&nbsp;';
                 }
                 if ($submission_status == 'success') {
                     echo '<img src="' . WPLA_URL . 'img/icon-success-32x32.png" style="width:12px;vertical-align:middle;padding:0;" class="tips" data-tip="This order was successfully submitted to be fulfilled by Amazon." />';
                 } elseif ($submission_status == 'shipped') {
                     echo '<img src="' . WPLA_URL . 'img/icon-success-32x32.png" style="width:12px;vertical-align:middle;padding:0;" class="tips" data-tip="This order has been fulfilled by Amazon." />';
                 } else {
                     $history = maybe_unserialize(get_post_meta($post->ID, '_wpla_fba_submission_result', true));
                     $error_count = is_array($history) ? sizeof(@$history['errors']) : false;
                     if ($error_count) {
                         echo '<img src="' . WPLA_URL . 'img/error.gif" style="vertical-align:middle;padding:0;" class="tips" data-tip="There was a problem submitting this order to be fulfilled by Amazon!" />';
                     }
                 }
             }
             break;
     }
     // switch ($column)
 }