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 updateReports($reports) { foreach ($reports as $id) { $report = new WPLA_AmazonReport($id); $report->loadFromAmazon(); } }