/**
 * Get Report List Action Sample
 * returns a list of reports; by default the most recent ten reports,
 * regardless of their acknowledgement status
 *
 * @param MarketplaceWebService_Interface $service instance of MarketplaceWebService_Interface
 * @param mixed $request MarketplaceWebService_Model_GetReportList or array of parameters
 */
function invokeGetReportRequestList(MarketplaceWebService_Interface $service, $request)
{
    try {
        $response = $service->getReportRequestList($request);
        echo "Service Response\n";
        echo "=============================================================================\n";
        echo "        GetReportRequestListResponse\n";
        if ($response->isSetGetReportRequestListResult()) {
            echo "            GetReportRequestListResult\n";
            $getReportRequestListResult = $response->getGetReportRequestListResult();
            if ($getReportRequestListResult->isSetNextToken()) {
                echo "                NextToken\n";
                echo "                    " . $getReportRequestListResult->getNextToken() . "\n";
            }
            if ($getReportRequestListResult->isSetHasNext()) {
                echo "                HasNext\n";
                echo "                    " . $getReportRequestListResult->getHasNext() . "\n";
            }
            $reportRequestInfoList = $getReportRequestListResult->getReportRequestInfoList();
            foreach ($reportRequestInfoList as $reportRequestInfo) {
                echo "                ReportRequestInfo\n";
                if ($reportRequestInfo->isSetReportRequestId()) {
                    echo "                    ReportRequestId\n";
                    echo "                        " . $reportRequestInfo->getReportRequestId() . "\n";
                }
                if ($reportRequestInfo->isSetReportType()) {
                    echo "                    ReportType\n";
                    echo "                        " . $reportRequestInfo->getReportType() . "\n";
                }
                if ($reportRequestInfo->isSetStartDate()) {
                    echo "                    StartDate\n";
                    echo "                        " . $reportRequestInfo->getStartDate()->format(DATE_FORMAT) . "\n";
                }
                if ($reportRequestInfo->isSetEndDate()) {
                    echo "                    EndDate\n";
                    echo "                        " . $reportRequestInfo->getEndDate()->format(DATE_FORMAT) . "\n";
                }
                // add start
                if ($reportRequestInfo->isSetScheduled()) {
                    echo "                    Scheduled\n";
                    echo "                        " . $reportRequestInfo->getScheduled() . "\n";
                }
                // add end
                if ($reportRequestInfo->isSetSubmittedDate()) {
                    echo "                    SubmittedDate\n";
                    echo "                        " . $reportRequestInfo->getSubmittedDate()->format(DATE_FORMAT) . "\n";
                }
                if ($reportRequestInfo->isSetReportProcessingStatus()) {
                    echo "                    ReportProcessingStatus\n";
                    echo "                        " . $reportRequestInfo->getReportProcessingStatus() . "\n";
                }
                // add start
                if ($reportRequestInfo->isSetGeneratedReportId()) {
                    echo "                    GeneratedReportId\n";
                    echo "                        " . $reportRequestInfo->getGeneratedReportId() . "\n";
                }
                if ($reportRequestInfo->isSetStartedProcessingDate()) {
                    echo "                    StartedProcessingDate\n";
                    echo "                        " . $reportRequestInfo->getStartedProcessingDate()->format(DATE_FORMAT) . "\n";
                }
                if ($reportRequestInfo->isSetCompletedDate()) {
                    echo "                    CompletedDate\n";
                    echo "                        " . $reportRequestInfo->getCompletedDate()->format(DATE_FORMAT) . "\n";
                }
                // add end
            }
        }
        if ($response->isSetResponseMetadata()) {
            echo "            ResponseMetadata\n";
            $responseMetadata = $response->getResponseMetadata();
            if ($responseMetadata->isSetRequestId()) {
                echo "                RequestId\n";
                echo "                    " . $responseMetadata->getRequestId() . "\n";
            }
        }
        echo "            ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n";
    } catch (MarketplaceWebService_Exception $ex) {
        echo "Caught Exception: " . $ex->getMessage() . "\n";
        echo "Response Status Code: " . $ex->getStatusCode() . "\n";
        echo "Error Code: " . $ex->getErrorCode() . "\n";
        echo "Error Type: " . $ex->getErrorType() . "\n";
        echo "Request ID: " . $ex->getRequestId() . "\n";
        echo "XML: " . $ex->getXML() . "\n";
        echo "ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n";
    }
}
 function invokeGetReportRequestList(MarketplaceWebService_Interface $service, $request)
 {
     global $wpdb, $woocommerce;
     try {
         $response = $service->getReportRequestList($request);
         if ($response->isSetGetReportRequestListResult()) {
             $getReportRequestListResult = $response->getGetReportRequestListResult();
             $reportRequestInfoList = $getReportRequestListResult->getReportRequestInfoList();
             print_r($reportRequestInfoList);
             foreach ($reportRequestInfoList as $reportRequestInfo) {
                 if ($reportRequestInfo->isSetReportType() && $reportRequestInfo->getReportType() == '_GET_ORDERS_DATA_' && ($reportRequestInfo->isSetReportProcessingStatus() && $reportRequestInfo->getReportProcessingStatus() == '_DONE_')) {
                     if ($reportRequestInfo->isSetReportRequestId()) {
                         $ReportRequestId = $reportRequestInfo->getReportRequestId();
                     }
                     if ($reportRequestInfo->isSetGeneratedReportId()) {
                         $GeneratedReportId = $reportRequestInfo->getGeneratedReportId();
                         if ($GeneratedReportId == '' && $ReportRequestId != '') {
                             $GeneratedReportId = $this->get_report_list_api($ReportRequestId);
                             $data = $this->get_report_api($GeneratedReportId);
                         } else {
                             $data = $this->get_report_api($GeneratedReportId);
                         }
                         $xml = simplexml_load_string($data);
                         // Check and dump MWS Report API Response
                         $pwacheckkout = new Pwacheckout();
                         if ($pwacheckkout->get_option('mws_report_dump') == 'yes') {
                             $dir = $pwacheckkout->get_option('mws_report_dump_url');
                             if (!file_exists($dir) && !is_dir($dir)) {
                                 mkdir($dir, 0777);
                             }
                             $filename = $dir . $GeneratedReportId . '_mws_report';
                             $myfile = fopen($filename, "w");
                             fwrite($myfile, $data);
                             fclose($myfile);
                         }
                         foreach ($xml->Message as $orderdetail) {
                             $AmazonOrderID = (string) $orderdetail->OrderReport->AmazonOrderID;
                             $order_postmeta = $wpdb->get_results("select post_id from {$wpdb->postmeta} where meta_key = '_pwa_order_id' and meta_value = '{$AmazonOrderID}' ");
                             if (empty($order_postmeta)) {
                                 $order = wc_create_order();
                                 add_post_meta($order->id, '_pwa_order_id', $AmazonOrderID);
                                 $this->update_order_detail($order->id, $orderdetail);
                             } else {
                                 $order_id = $order_postmeta[0]->post_id;
                                 $this->update_order_detail($order_id, $orderdetail);
                             }
                         }
                     }
                 }
             }
             $dateTime = new DateTime('now', new DateTimeZone('UTC'));
             $time = $dateTime->format(DATE_ISO8601);
             $wpdb->insert($wpdb->prefix . 'pwa_mws_report_cron', array('created_before' => $time));
         }
     } catch (MarketplaceWebService_Exception $ex) {
         $message = 'MWS Report API : Caught Exception : ' . $ex->getMessage() . "\n";
         $message .= "Response Status Code: " . $ex->getStatusCode() . "\n";
         $message .= "Error Code: " . $ex->getErrorCode() . "\n";
         $message .= "Error Type: " . $ex->getErrorType() . "\n";
         $param['message'] = $message;
         $this->generate_log($param);
     }
 }
 /**
  * Get Report List Action Sample
  * returns a list of reports; by default the most recent ten reports,
  * regardless of their acknowledgement status
  *   
  * @param MarketplaceWebService_Interface $service instance of MarketplaceWebService_Interface
  * @param mixed $request MarketplaceWebService_Model_GetReportList or array of parameters
  */
 function invokeGetReportRequestList(MarketplaceWebService_Interface $service, $request)
 {
     $reports = array();
     try {
         $response = $service->getReportRequestList($request);
         if ($response->isSetGetReportRequestListResult()) {
             $getReportRequestListResult = $response->getGetReportRequestListResult();
             // if ($getReportRequestListResult->isSetNextToken())
             // if ($getReportRequestListResult->isSetHasNext())
             $reportRequestInfoList = $getReportRequestListResult->getReportRequestInfoList();
             foreach ($reportRequestInfoList as $reportRequestInfo) {
                 $report = new stdClass();
                 $report->ReportRequestId = $reportRequestInfo->getReportRequestId();
                 $report->ReportType = $reportRequestInfo->getReportType();
                 $report->ReportProcessingStatus = $reportRequestInfo->getReportProcessingStatus();
                 if ($reportRequestInfo->isSetStartDate()) {
                     $report->StartDate = $reportRequestInfo->getStartDate()->format(DATE_FORMAT);
                 }
                 if ($reportRequestInfo->isSetEndDate()) {
                     $report->EndDate = $reportRequestInfo->getEndDate()->format(DATE_FORMAT);
                 }
                 if ($reportRequestInfo->isSetSubmittedDate()) {
                     $report->SubmittedDate = $reportRequestInfo->getSubmittedDate()->format(DATE_FORMAT);
                 }
                 if ($reportRequestInfo->isSetCompletedDate()) {
                     $report->CompletedDate = $reportRequestInfo->getCompletedDate()->format(DATE_FORMAT);
                 }
                 if ($reportRequestInfo->isSetStartedProcessingDate()) {
                     $report->StartedProcessingDate = $reportRequestInfo->getStartedProcessingDate()->format(DATE_FORMAT);
                 }
                 if ($reportRequestInfo->isSetGeneratedReportId()) {
                     $report->GeneratedReportId = $reportRequestInfo->getGeneratedReportId();
                 }
                 $reports[] = $report;
             }
         }
     } catch (MarketplaceWebService_Exception $ex) {
         // echo("Caught Exception: " . $ex->getMessage() . "\n");
         // echo("Response Status Code: " . $ex->getStatusCode() . "\n");
         // echo("Error Code: " . $ex->getErrorCode() . "\n");
         // echo("Error Type: " . $ex->getErrorType() . "\n");
         // echo("Request ID: " . $ex->getRequestId() . "\n");
         // echo("XML: " . $ex->getXML() . "\n");
         // echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
         $error = new stdClass();
         $error->ErrorMessage = $ex->getMessage();
         $error->ErrorCode = $ex->getErrorCode();
         $error->StatusCode = $ex->getStatusCode();
         return $error;
     }
     // log to db - parsed request
     $this->dblogger->updateLog(array('result' => json_encode($reports), 'success' => 'Success'));
     return $reports;
 }
 function invokeGetReportRequestList(MarketplaceWebService_Interface $service, $request)
 {
     $prefix = _DB_PREFIX_;
     try {
         $response = $service->getReportRequestList($request);
         if ($response->isSetGetReportRequestListResult()) {
             $getReportRequestListResult = $response->getGetReportRequestListResult();
             $reportRequestInfoList = $getReportRequestListResult->getReportRequestInfoList();
             foreach ($reportRequestInfoList as $reportRequestInfo) {
                 if ($reportRequestInfo->isSetReportType() && $reportRequestInfo->getReportType() == '_GET_ORDERS_DATA_' && ($reportRequestInfo->isSetReportProcessingStatus() && $reportRequestInfo->getReportProcessingStatus() == '_DONE_')) {
                     if ($reportRequestInfo->isSetReportRequestId()) {
                         $ReportRequestId = $reportRequestInfo->getReportRequestId();
                     }
                     if ($reportRequestInfo->isSetGeneratedReportId()) {
                         $GeneratedReportId = $reportRequestInfo->getGeneratedReportId();
                         if ($GeneratedReportId == '' && $ReportRequestId != '') {
                             $GeneratedReportId = $this->get_report_list_api($ReportRequestId);
                             $data = $this->get_report_api($GeneratedReportId);
                         } else {
                             $data = $this->get_report_api($GeneratedReportId);
                         }
                         $xml = simplexml_load_string($data);
                         // Check and dump MWS Report API Response
                         if (Configuration::get('PWAPRESTA_PWAPRESTA_MWS_REPORT_DUMP') == '1') {
                             $dir = Configuration::get('PWAPRESTA_PWAPRESTA_MWS_REPORT_DUMP_URL');
                             if (!file_exists($dir) && !is_dir($dir)) {
                                 mkdir($dir, 0777);
                             }
                             $filename = $dir . $GeneratedReportId . '_mws_report';
                             $myfile = fopen($filename, "w");
                             fwrite($myfile, $data);
                             fclose($myfile);
                         }
                         foreach ($xml->Message as $orderdetail) {
                             $AmazonOrderID = (string) $orderdetail->OrderReport->AmazonOrderID;
                             $sql = 'select * from `' . $prefix . 'pwa_orders` where amazon_order_id = "' . $AmazonOrderID . '" ';
                             $results = Db::getInstance()->ExecuteS($sql);
                             if (empty($results)) {
                                 $tablename = $prefix . 'orders';
                                 $date = date('Y-m-d H:i:s');
                                 $sql = 'INSERT into `' . $tablename . '`  (`current_state` , `payment` , `module` , `date_add` ) VALUES( 99, "Pay with Amazon", "pwapresta", "' . $date . '" )';
                                 Db::getInstance()->Execute($sql);
                                 $order_id = Db::getInstance()->Insert_ID();
                                 $tablename = $prefix . 'pwa_orders';
                                 $sql = 'INSERT into `' . $tablename . '`  (`prestashop_order_id` , `amazon_order_id` ) VALUES( "' . $order_id . '", "' . $AmazonOrderID . '" )';
                                 Db::getInstance()->Execute($sql);
                                 $this->update_order_detail($order_id, $orderdetail);
                             } else {
                                 $order_id = $results[0]['prestashop_order_id'];
                                 $this->update_order_detail($order_id, $orderdetail);
                             }
                         }
                     }
                 }
             }
             $dateTime = new DateTime('now', new DateTimeZone('UTC'));
             $time = $dateTime->format(DATE_ISO8601);
             $sql = 'INSERT into `' . $prefix . 'mws_report_cron` (`created_before`) VALUES("' . $time . '") ';
             Db::getInstance()->Execute($sql);
         }
     } catch (MarketplaceWebService_Exception $ex) {
         $message = 'MWS Report API : Caught Exception : ' . $ex->getMessage() . "\n";
         $message .= "Response Status Code: " . $ex->getStatusCode() . "\n";
         $message .= "Error Code: " . $ex->getErrorCode() . "\n";
         $message .= "Error Type: " . $ex->getErrorType() . "\n";
         $param['message'] = $message;
         $obj = new Pwapresta();
         $obj->generate_log($param);
     }
 }