# 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`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.highsidelabs.co/sp-api-starter-kit/usage/feeds-and-reports.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
