public function ajax_view_report_details()
 {
     // show report details - if not logged in, check signature
     if ($this->requestAction() == 'wpla_report_details') {
         $report = WPLA_AmazonReport::getReportByRequestId($_REQUEST['rrid']);
         if (!$report) {
             die('unknown report');
         }
         $signature = md5($report->ReportRequestId . get_option('wpla_instance'));
         if ($_REQUEST['sig'] != $signature) {
             die('invalid signature');
         }
         $this->showReportDetails($report->id);
         exit;
     }
 }
 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);
 }