# Feeds and Reports

### Generating a report

Reports are created by submitting a request to the SP API, and then polling for the report's status until it is complete. Once the report is complete, it can be downloaded. The `ReportService` class provides a wrapper around this process, along with the `CreateReportJob`, `GetReportJob`, and `DownloadReportJob` queueable jobs. The simplest way to generate a report is to call the `Report::generate()` method:

```php
$marketplace = Marketplace::firstWhere('country_code', 'US');
$report = Report::create([
    'type' => 'GET_FLAT_FILE_OPEN_LISTINGS_DATA',
    'credentials_id' => $credentials->id,
    'data_start_date' => now()->subDays(30)->toDateString(),
    'data_end_date' => now()->toDateString(),
]);
$report->marketplaces()->attach($marketplace);
$report->generate([MyReportProcessor::class, 'processReport']);
```

This will dispatch a chain of queued jobs that create the report, poll for the report's status, and download the report. The report's results will be passed to the callback function passed to `Report::generate()`, which should take two parameters: the `Report` model, and the contents of the report as an array, `SimpleXMLElement` object, or string depending on the report type.

### Uploading a feed

Feeds are created by submitting a request to the SP API, and then polling for the feed's status until it is complete. Once the feed is complete, it can be downloaded. The `FeedService` class provides a wrapper around this process, along with the `CreateFeedJob` and `GetFeedResultJob` queueable jobs. Similarly to reports, the `Feed::submit()` method can be used to create and process a feed:

```php
$feed = Feed::create([
    'type' => 'POST_FLAT_FILE_INVLOADER_DATA',
    'credentials_id' => $credentials->id,
]);

$feedContents = '...';
$feed->setContents($feedContents)

$marketplace = Marketplace::firstWhere('country_code', 'US');
$feed->marketplaces()->attach($marketplace);

// The optional callback parameter is used to process the feed result document once it has been downloaded.
// It can be omitted if you just want to save the feed result document to the database.
$feed->submit([MyFeedProcessor::class, 'processFeed']);
```

Once the feed has finished processing, the raw feed result document is saved to `$feed->results`.
