public static function ajax_processReportPage($job, $task, $single_sku_mode = false)
 {
     // init
     $report = new WPLA_AmazonReport($task['id']);
     // $account = WPLA_AmazonAccount::getAccount( $report->account_id );
     // $api     = new WPLA_AmazonAPI( $account->id );
     // get CSV data
     $rows = $report->get_data_rows();
     if ($single_sku_mode) {
         // slice single row with matching SKU
         $selected_rows = array();
         foreach ($rows as $row) {
             if ($row['seller-sku'] == $task['sku']) {
                 $selected_rows[] = $row;
             }
         }
         $rows = $selected_rows;
     } else {
         // slice rows array according to limits
         $from_row = $task['from_row'];
         $to_row = $task['to_row'];
         $rows = array_slice($rows, $from_row - 1, $to_row - $from_row + 1, true);
     }
     // _GET_AFN_INVENTORY_DATA_
     if ($report->ReportType == '_GET_AFN_INVENTORY_DATA_') {
         return self::processFBAReportPage($report, $rows, $job, $task);
         die;
     }
     // _GET_MERCHANT_LISTINGS_DEFECT_DATA_
     if ($report->ReportType == '_GET_MERCHANT_LISTINGS_DEFECT_DATA_') {
         return self::processQualityReportPage($report, $rows, $job, $task);
         die;
     }
     // _GET_MERCHANT_LISTINGS_DATA_
     if ($report->ReportType == '_GET_MERCHANT_LISTINGS_DATA_') {
         return self::processInventoryReportPage($report, $rows, $job, $task);
         die;
     }
     echo "Unknown report type: " . $report->ReportType;
     die;
 }
 public function showReportDetails($id)
 {
     // get amazon_report record
     $report = new WPLA_AmazonReport($id);
     // get WooCommerce report
     // $wc_report_notes = $amazon_report['post_id'] ? $this->get_report_notes( $amazon_report['post_id'] ) : false;
     // check for query paramater
     $query = isset($_REQUEST['query']) ? sanitize_text_field($_REQUEST['query']) : '';
     $rows = $report->get_data_rows($query);
     unset($report->data);
     unset($report->results);
     // limit to 1000 rows per page
     $limit = 1000;
     $offset = 0;
     $total_rows = sizeof($rows);
     if ($total_rows > $limit) {
         $rows = array_splice($rows, $offset, $limit);
     }
     $aData = array('report' => $report, 'rows' => $rows, 'total_rows' => $total_rows, 'query' => $query);
     $this->display('report_details', $aData);
 }
 public function jobs_load_tasks()
 {
     // quit if no job name provided
     if (!isset($_REQUEST['job'])) {
         return false;
     }
     $jobname = $_REQUEST['job'];
     // check if an array of listing IDs was provided
     $lm = new WPLA_ListingsModel();
     $listing_ids = isset($_REQUEST['item_ids']) && is_array($_REQUEST['item_ids']) ? $_REQUEST['item_ids'] : false;
     if ($listing_ids) {
         $items = $lm->getItemsByIdArray($listing_ids);
     }
     // register shutdown handler
     global $wpla_shutdown_handler_enabled;
     $wpla_shutdown_handler_enabled = true;
     register_shutdown_function(array($this, 'shutdown_handler'));
     // handle job name
     switch ($jobname) {
         case 'updateProductsWithoutASIN':
             // get prepared items
             $sm = new WPLA_ListingsModel();
             $items = $sm->getAllOnlineWithoutASIN();
             // create job from items and send response
             $response = $this->_create_bulk_listing_job('updateProduct', $items, $jobname);
             $this->returnJSON($response);
             exit;
         case 'createAllImportedProducts':
             // get prepared items
             $sm = new WPLA_ListingsModel();
             $items = $sm->getAllImported();
             // DEV: limit to 10 tasks at a time ***
             // $items = array_slice($items, 0, 10, true);
             // create job from items and send response
             $response = $this->_create_bulk_listing_job('createProduct', $items, $jobname);
             $this->returnJSON($response);
             exit;
         case 'processAmazonReport':
             // get report
             $id = $_REQUEST['item_id'];
             $report = new WPLA_AmazonReport($id);
             $rows = $report->get_data_rows();
             $rows_count = sizeof($rows);
             $page_size = 500;
             $number_of_pages = intval($rows_count / $page_size) + 1;
             $items = array();
             if ($number_of_pages > 0) {
                 for ($page = 0; $page < $number_of_pages; $page++) {
                     $from_row = $page * $page_size + 1;
                     $to_row = ($page + 1) * $page_size;
                     if ($to_row > $rows_count) {
                         $to_row = $rows_count;
                     }
                     $items[] = array('id' => $id, 'page' => $page, 'from_row' => $from_row, 'to_row' => $to_row, 'title' => 'Processing rows ' . $from_row . ' to ' . $to_row);
                 }
             }
             // create job from items and send response
             $response = $this->_create_bulk_listing_job('processReportPage', $items, $jobname);
             $this->returnJSON($response);
             exit;
         case 'processRowsFromAmazonReport':
             $id = $_REQUEST['report_id'];
             $skus = $_REQUEST['sku_list'];
             foreach ($skus as $sku) {
                 $items[] = array('id' => $id, 'sku' => $sku, 'title' => 'Processing SKU ' . $sku);
             }
             // create job from items and send response
             $response = $this->_create_bulk_listing_job('processSingleSkuFromReport', $items, $jobname);
             $this->returnJSON($response);
             exit;
         case 'fetchProductDescription':
             // create job from items and send response
             $response = $this->_create_bulk_listing_job('fetchFullProductDescription', $items, $jobname);
             $this->returnJSON($response);
             exit;
         default:
             // echo "unknown job";
             // break;
     }
     // exit();
 }