$log = new Log('cba_cron.log');
$log->write('Started cron job');
$args = $this->getCommonParameters();
$args['Merchant'] = $this->getMerchantId();
$args['Action'] = 'GetReportList';
$args['Version'] = '2009-01-01';
$args['ReportTypeList.Type.1'] = '_GET_ORDERS_DATA_';
$args['Acknowledged'] = 'false';
$response = $this->getMwsResponse('POST', '/', array(), $args);
$xml = simplexml_load_string($response);
while ($xml && (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) {
// retrieve report
$args = $this->getCommonParameters();
$args['Merchant'] = $this->getMerchantId();
$args['Action'] = 'GetReport';
$args['Version'] = '2009-01-01';
$args['ReportId'] = (string)$list_item->ReportId;
$report_response = $this->getMwsResponse('POST', '/', array(), $args);
$report_xml = simplexml_load_string($report_response);
foreach ($report_xml->Message as $message) {
$amazon_order_id = (string)$message->OrderReport->AmazonOrderID;
$billing_email = (string)$message->OrderReport->BillingData->BuyerEmailAddress;
$billing_name = (string)$message->OrderReport->BillingData->BuyerName;
$billing_phone_number = (string)$message->OrderReport->BillingData->BuyerPhoneNumber;
$shipping_name = (string)$message->OrderReport->FulfillmentData->Address->Name;
$shipping_address1 = (string)$message->OrderReport->FulfillmentData->Address->AddressFieldOne;
$shipping_address2 = (string)$message->OrderReport->FulfillmentData->Address->AddressFieldTwo;
$shipping_city = (string)$message->OrderReport->FulfillmentData->Address->City;
$shipping_zone = (string)$message->OrderReport->FulfillmentData->Address->StateOrRegion;
$shipping_post_code = (string)$message->OrderReport->FulfillmentData->Address->PostalCode;
$shipping_country_code = (string)$message->OrderReport->FulfillmentData->Address->CountryCode;
$result = $db->query("SELECT `order_id` FROM `" . DB_PREFIX . "order_amazon` WHERE `amazon_order_id` = '" . $db->escape($amazon_order_id) . "'")->row;
if (!isset($result['order_id']) || empty($result['order_id'])) {
$log->write("Order " . $amazon_order_id . " was not found");
continue;
}
$order_id = $result['order_id'];
$db->query("UPDATE `" . DB_PREFIX . "order` AS `o`, `" . DB_PREFIX . "order_amazon` `oa` SET `o`.`payment_firstname` = '" . $db->escape($billing_name) . "', `o`.`firstname` = '" . $db->escape($billing_name) . "', `o`.`email` = '" . $db->escape($billing_email) . "', `o`.`telephone` = '" . $db->escape($billing_phone_number) . "', `o`.`shipping_firstname` = '" . $db->escape($shipping_name) . "', `o`.`shipping_address_1` = '" . $db->escape($shipping_address1) . "', `o`.`shipping_address_2` = '" . $db->escape($shipping_address2) . "', `o`.`shipping_city` = '" . $db->escape($shipping_city) . "', `o`.`shipping_zone` = '" . $db->escape($shipping_zone) . "', `o`.`shipping_country` = '" . $db->escape($shipping_country_code) . "', `o`.`shipping_postcode` = '" . $db->escape($shipping_post_code) . "', `o`.`order_status_id` = " . (int)$settings->get('amazon_checkout_order_ready_status') . " WHERE `o`.`order_id` = " . (int)$order_id);
$db->query("INSERT INTO `" . DB_PREFIX . "order_history` (`order_id`, `order_status_id`, `comment`, `date_added`) VALUES (" . (int)$order_id . ", " . (int)$settings->get('amazon_checkout_order_ready_status') . ", '', NOW())");
foreach ($message->OrderReport->Item as $item) {
$amazon_order_item_code = (string)$item->AmazonOrderItemCode;
$order_product_id = (string)$item->SKU;
$db->query("REPLACE INTO `" . DB_PREFIX . "order_amazon_product` (`order_product_id`, `amazon_order_item_code`) SELECT `op`.`order_product_id`, '" . $db->escape($amazon_order_item_code) . "' FROM `" . DB_PREFIX . "order_product` `op` WHERE `op`.`order_product_id` = '" . $db->escape($order_product_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);
if ($next_token) {
$args = $this->getCommonParameters();
$args['Merchant'] = $this->getMerchantId();
$args['Action'] = 'GetReportListByNextToken';
$args['Version'] = '2009-01-01';
$args['NextToken'] = $next_token;
$response = $this->getMwsResponse('POST', '/', array(), $args);
$xml = simplexml_load_string($response);
if (isset($xml->Error->Code) && (string)$xml->Error->Code == 'RequestThrottled') {
$xml = false;
}
} else {
$xml = false;
}
}
$log->write('Finished cron job');