public static function processReportsRequestList($reports, $account, $is_new_request = false)
 {
     // if this is a new report request, add to reports in progress - otherwise reset it
     // TODO: count reports in progress per account
     if ($is_new_request) {
         $reports_in_progress = get_option('wpla_reports_in_progress', 0);
     } else {
         $reports_in_progress = 0;
     }
     foreach ($reports as $report) {
         // check if report exists
         $existing_record = WPLA_AmazonReport::getReportByRequestId($report->ReportRequestId);
         if ($existing_record) {
             // skip existing report if it was requested using another "account" (different marketplace using the same account)
             if ($existing_record->account_id != $account->id) {
                 WPLA()->logger->info('skipped existing report ' . $existing_record->id . ' for account ' . $existing_record->account_id);
                 continue;
             }
             $new_report = new WPLA_AmazonReport($existing_record->id);
             $new_report->ReportRequestId = $report->ReportRequestId;
             $new_report->ReportType = $report->ReportType;
             $new_report->ReportProcessingStatus = $report->ReportProcessingStatus;
             $new_report->SubmittedDate = $report->SubmittedDate;
             $new_report->StartedProcessingDate = isset($report->StartedProcessingDate) ? $report->StartedProcessingDate : '';
             $new_report->CompletedDate = isset($report->CompletedDate) ? $report->CompletedDate : '';
             $new_report->GeneratedReportId = isset($report->GeneratedReportId) ? $report->GeneratedReportId : '';
             // $new_report->account_id             = $account->id;
             $new_report->results = maybe_serialize($report);
             // save new record
             $new_report->update();
         } else {
             // add new record
             $new_report = new WPLA_AmazonReport();
             $new_report->ReportRequestId = $report->ReportRequestId;
             $new_report->ReportType = $report->ReportType;
             $new_report->ReportProcessingStatus = $report->ReportProcessingStatus;
             $new_report->SubmittedDate = $report->SubmittedDate;
             $new_report->StartedProcessingDate = isset($report->StartedProcessingDate) ? $report->StartedProcessingDate : '';
             $new_report->CompletedDate = isset($report->CompletedDate) ? $report->CompletedDate : '';
             $new_report->GeneratedReportId = isset($report->GeneratedReportId) ? $report->GeneratedReportId : '';
             $new_report->account_id = $account->id;
             $new_report->results = maybe_serialize($report);
             // save new record
             $new_report->add();
         }
         // load data for new reports automatically (not older than 24 hours)
         if (!$new_report->data && in_array($report->ReportProcessingStatus, array('_DONE_'))) {
             $report_completed_date = strtotime($new_report->CompletedDate);
             $one_day_ago = time() - 3600 * 24;
             if ($report_completed_date > $one_day_ago) {
                 $new_report->loadFromAmazon();
                 $new_report->autoProcessNewReport();
             }
             // $new_report->loadFromAmazon();
             // $new_report->processReportData();
         }
         // check if report is in progress
         if (in_array($report->ReportProcessingStatus, array('_SUBMITTED_', '_IN_PROGRESS_'))) {
             $reports_in_progress++;
         }
     }
     // update report progress status
     update_option('wpla_reports_in_progress', $reports_in_progress);
 }
 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 request_daily_inventory_report()
 {
     $report_type = '_GET_MERCHANT_LISTINGS_DATA_';
     $accounts = WPLA_AmazonAccount::getAll();
     foreach ($accounts as $account) {
         $api = new WPLA_AmazonAPI($account->id);
         // request report - returns request list as array on success
         $reports = $api->requestReport($report_type);
         if (is_array($reports)) {
             // process the result
             WPLA_AmazonReport::processReportsRequestList($reports, $account, true);
         } elseif ($reports->Error->Message) {
         } else {
         }
     }
     // foreach account
 }
 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 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();
 }
 public function requestNewInventoryReport($report_type = '_GET_MERCHANT_LISTINGS_DATA_')
 {
     $accounts = WPLA_AmazonAccount::getAll();
     foreach ($accounts as $account) {
         $api = new WPLA_AmazonAPI($account->id);
         // request report - returns request list as array on success
         $reports = $api->requestReport($report_type);
         if (is_array($reports)) {
             // process the result
             // $this->processReportsRequestList( $reports, $account );
             WPLA_AmazonReport::processReportsRequestList($reports, $account);
             $this->showMessage(sprintf(__('Report requested for account %s.', 'wpla'), $account->title));
         } elseif ($reports->Error->Message) {
             $this->showMessage(sprintf(__('There was a problem requesting the report for account %s.', 'wpla'), $account->title) . '<br>Error: ' . $reports->Error->Message, 1);
         } else {
             $this->showMessage(sprintf(__('There was a problem requesting the report for account %s.', 'wpla'), $account->title), 1);
         }
     }
 }