public static function updateMinMaxPrices($item_ids)
 {
     // echo "<pre>";print_r($item_ids);echo"</pre>";
     // TODO: sanitize values
     $min_base_price = trim($_REQUEST['min_base_price']);
     $min_price_percentage = trim($_REQUEST['min_price_percentage']);
     $min_price_amount = trim($_REQUEST['min_price_amount']);
     $max_base_price = trim($_REQUEST['max_base_price']);
     $max_price_percentage = trim($_REQUEST['max_price_percentage']);
     $max_price_amount = trim($_REQUEST['max_price_amount']);
     $min_price_amount = str_replace(',', '.', $min_price_amount);
     // convert decimal comma
     $max_price_amount = str_replace(',', '.', $max_price_amount);
     // remember last used options
     $options = array('min_base_price' => $min_base_price, 'max_base_price' => $max_base_price, 'min_price_amount' => $min_price_amount, 'max_price_amount' => $max_price_amount, 'min_price_percentage' => $min_price_percentage, 'max_price_percentage' => $max_price_percentage);
     update_option('wpla_price_wizard_options', $options);
     $lm = new WPLA_ListingsModel();
     foreach ($item_ids as $listing_id) {
         // load listing item
         $item = $lm->getItem($listing_id, OBJECT);
         if (!$item) {
             continue;
         }
         if ($item->product_type == 'variable') {
             continue;
         }
         $post_id = $item->post_id;
         // get base price (min)
         $base_price = 0;
         if ($min_base_price == 'price') {
             $base_price = WPLA_ProductWrapper::getPrice($post_id);
         }
         if ($min_base_price == 'sale_price') {
             $base_price = WPLA_ProductWrapper::getSalePrice($post_id);
         }
         if ($min_base_price == 'msrp') {
             $base_price = get_post_meta($post_id, '_msrp', true) ? get_post_meta($post_id, '_msrp', true) : get_post_meta($post_id, '_msrp_price', true);
         }
         // calculate new min price
         if ($min_price_percentage) {
             $base_price = $base_price + $base_price * floatval($min_price_percentage) / 100;
         }
         if ($min_price_amount) {
             $base_price = $base_price + floatval($min_price_amount);
         }
         if ($min_base_price == 'no_change') {
             $base_price = $item->min_price;
         }
         $new_min_price = round($base_price, 2);
         // get base price (max)
         $base_price = 0;
         if ($max_base_price == 'price') {
             $base_price = WPLA_ProductWrapper::getPrice($post_id);
         }
         if ($max_base_price == 'sale_price') {
             $base_price = WPLA_ProductWrapper::getSalePrice($post_id);
         }
         if ($max_base_price == 'msrp') {
             $base_price = get_post_meta($post_id, '_msrp', true) ? get_post_meta($post_id, '_msrp', true) : get_post_meta($post_id, '_msrp_price', true);
         }
         // calculate new max price
         if ($max_price_percentage) {
             $base_price = $base_price + $base_price * floatval($max_price_percentage) / 100;
         }
         if ($max_price_amount) {
             $base_price = $base_price + floatval($max_price_amount);
         }
         if ($max_base_price == 'no_change') {
             $base_price = $item->max_price;
         }
         $new_max_price = round($base_price, 2);
         // update listing table
         $data = array('min_price' => $new_min_price, 'max_price' => $new_max_price, 'pnq_status' => 1);
         $lm->updateWhere(array('id' => $listing_id), $data);
         // update product
         update_post_meta($item->post_id, '_amazon_minimum_price', $new_min_price);
         update_post_meta($item->post_id, '_amazon_maximum_price', $new_max_price);
     }
     // foreach item
 }
 public function getOrCreateParentVariation($result, $listing, $account)
 {
     $lm = new WPLA_ListingsModel();
     $parent_asin = $result->product->ASIN;
     WPLA()->logger->info("processing parent variation {$parent_asin}");
     // find existing parent variation listing
     if ($parent_listing = $lm->getItemByASIN($parent_asin)) {
         WPLA()->logger->info("Found existing parent for ASIN {$parent_asin}");
         $this->message = "Found existing variable parent listing for ASIN {$parent_asin}";
         $parent_id = $parent_listing->id;
     } else {
         // create parent listing
         $data = array('asin' => $parent_asin, 'sku' => $parent_asin, 'product_type' => 'variable', 'source' => $listing['source'], 'status' => 'matched', 'account_id' => $account->id);
         $parent_id = $lm->insertListingData($data);
         // update listing attributes
         $lm->updateItemAttributes($result->product->AttributeSets->ItemAttributes, $parent_id);
         $parent_listing = $lm->getItem($parent_id, OBJECT);
         // load listing object
         WPLA()->logger->info("Created new parent for ASIN {$parent_asin} with ID {$parent_id}");
         $this->message = "Created new variable product for ASIN {$parent_asin} with ID {$parent_id}";
     }
     return $parent_listing;
 }
 public function getListingURL($listing_id)
 {
     $lm = new WPLA_ListingsModel();
     $item = $lm->getItem($listing_id);
     // build listing URL
     $listing_url = 'http://www.amazon.com/dp/' . $item['asin'] . '/';
     if ($item['account_id']) {
         $account = new WPLA_AmazonAccount($item['account_id']);
         $market = new WPLA_AmazonMarket($account->market_id);
         $listing_url = 'http://www.' . $market->url . '/dp/' . $item['asin'] . '/';
     }
     return $listing_url;
 }
 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();
     }
 }
 public function displayEditPage()
 {
     // get item
     $listingsModel = new WPLA_ListingsModel();
     $item = $listingsModel->getItem($_REQUEST['listing']);
     // get other data
     $profiles = WPLA_AmazonProfile::getAll();
     $aData = array('plugin_url' => self::$PLUGIN_URL, 'message' => $this->message, 'item' => $item, 'feed_profiles' => $profiles, 'form_action' => 'admin.php?page=' . self::ParentMenuId . (isset($_REQUEST['paged']) ? '&paged=' . $_REQUEST['paged'] : ''));
     $this->display('listings_edit_page', array_merge($aData, $item));
 }