$qry = $db->query("SELECT `submission_id` FROM `" . DB_PREFIX . "order_amazon_report` WHERE `status` = 'processing'");
$submission_ids = array();
foreach ($qry->rows as $row) {
$submission_ids[] = $row['submission_id'];
}
for ($i = 0; $i < count($submission_ids); $i += 50) {
$ids = array_slice($submission_ids, $i, 50);
$args = $this->getCommonParameters();
$args['Merchant'] = $this->getMerchantId();
$args['Action'] = 'GetReportList';
$args['Version'] = '2009-01-01';
$args['Acknowledged'] = 'false';
$count = 1;
foreach ($ids as $id) {
$args['ReportRequestIdList.Id.' . $count++] = $id;
}
$response = $this->getMwsResponse('POST', '/', array(), $args);
$xml = simplexml_load_string($response);
if (isset($xml->Error->Code) && (string)$xml->Error->Code == 'RequestThrottled') {
return;
}
if (isset($xml->GetReportListResult) || isset($xml->GetReportListByNextTokenResult)) {
if (isset($xml->GetReportListResult)) {
$list = $xml->GetReportListResult->ReportInfo;
$next_token = ((string)$xml->GetReportListResult->HasNext == 'true') ? (string)$xml->GetReportListResult->NextToken : false;
} else {
$list = $xml->GetReportListByNextTokenResult->ReportInfo;
$next_token = ((string)$xml->GetReportListByNextTokenResult->HasNext == 'true') ? (string)$xml->GetReportListByNextTokenResult->NextToken : false;
}
$report_ids = array();
foreach ($list as $list_item) {
$args = $this->getCommonParameters();
$args['Merchant'] = $this->getMerchantId();
$args['Action'] = 'GetReport';
$args['Version'] = '2009-01-01';
$args['ReportId'] = (string)$list_item->ReportId;
$report = $this->getMwsResponse('POST', '/', array(), $args);
$lines = explode("\n", $report);
$errors = array();
foreach ($lines as $line) {
$values = explode("\t", $line);
if (isset($values[5]) && ($values[4] == 'Error' || $values[4] == 'Fatal')) {
$errors[] = 'Order ID: ' . $values[1] . '<br /> Order Item ID: ' . $values[2] . '<br /> Error Message: ' . trim($values[5]);
}
}
if (empty($errors)) {
$status = 'success';
} else {
$status = 'error';
}
$error_message = implode('<br />', $errors);
$submission_id = (string)$list_item->ReportRequestId;
$db->query("UPDATE `" . DB_PREFIX . "order_amazon_report` SET `status` = '" . $db->escape($status) . "', text = '" . $db->escape($error_message) . "' WHERE `submission_id` = '" . $db->escape($submission_id) . "'");
$report_ids[] = (string)$list_item->ReportId;
}
$args = $this->getCommonParameters();
$args['Merchant'] = $this->getMerchantId();
$args['Action'] = 'UpdateReportAcknowledgements';
$args['Version'] = '2009-01-01';
$args['Acknowledged'] = 'true';
for ($i = 1; $i <= count($report_ids); $i++) {
$args['ReportIdList.Id.' . $i] = $report_ids[$i - 1];
}
$this->getMwsResponse('POST', '/', array(), $args);
}
}