Quote CSV exports

This commit is contained in:
David Bomba 2022-04-27 16:33:30 +10:00
parent 519e427cda
commit bd5428ccee
4 changed files with 108 additions and 17 deletions

View File

@ -20,14 +20,16 @@ use App\Utils\Ninja;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use League\Csv\Writer; use League\Csv\Writer;
class QuoteExport class QuoteExport extends BaseExport
{ {
private $company; private Company $company;
private $report_keys; protected array $input;
private $quote_transformer; private $quote_transformer;
protected string $date_key = 'date';
private array $entity_keys = [ private array $entity_keys = [
'amount' => 'amount', 'amount' => 'amount',
'balance' => 'balance', 'balance' => 'balance',
@ -71,10 +73,10 @@ class QuoteExport
'invoice' 'invoice'
]; ];
public function __construct(Company $company, array $report_keys) public function __construct(Company $company, array $input)
{ {
$this->company = $company; $this->company = $company;
$this->report_keys = $report_keys; $this->input = $input;
$this->quote_transformer = new QuoteTransformer(); $this->quote_transformer = new QuoteTransformer();
} }
@ -93,14 +95,18 @@ class QuoteExport
//insert the header //insert the header
$this->csv->insertOne($this->buildHeader()); $this->csv->insertOne($this->buildHeader());
Quote::with('client')->where('company_id', $this->company->id) $query = Quote::query()
->where('is_deleted',0) ->with('client')->where('company_id', $this->company->id)
->cursor() ->where('is_deleted',0);
->each(function ($quote){
$this->csv->insertOne($this->buildRow($quote)); $query = $this->addDateRange($query);
}); $query->cursor()
->each(function ($quote){
$this->csv->insertOne($this->buildRow($quote));
});
return $this->csv->toString(); return $this->csv->toString();
@ -112,7 +118,7 @@ class QuoteExport
$header = []; $header = [];
foreach(array_keys($this->report_keys) as $key) foreach(array_keys($this->input['report_keys']) as $key)
$header[] = ctrans("texts.{$key}"); $header[] = ctrans("texts.{$key}");
return $header; return $header;
@ -125,7 +131,7 @@ class QuoteExport
$entity = []; $entity = [];
foreach(array_values($this->report_keys) as $key){ foreach(array_values($this->input['report_keys']) as $key){
$entity[$key] = $transformed_quote[$key]; $entity[$key] = $transformed_quote[$key];
} }

View File

@ -30,11 +30,11 @@ class InvoiceItemReportController extends BaseController
/** /**
* @OA\Post( * @OA\Post(
* path="/api/v1/reports/invoices", * path="/api/v1/reports/invoice_items",
* operationId="getInvoiceReport", * operationId="getInvoiceItemReport",
* tags={"reports"}, * tags={"reports"},
* summary="Invoice reports", * summary="Invoice item reports",
* description="Export invoice reports", * description="Export invoice item reports",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\RequestBody( * @OA\RequestBody(

View File

@ -0,0 +1,84 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers\Reports;
use App\Export\CSV\QuoteExport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\GenericReportRequest;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Response;
class QuoteReportController extends BaseController
{
use MakesHash;
private string $filename = 'quotes.csv';
public function __construct()
{
parent::__construct();
}
/**
* @OA\Post(
* path="/api/v1/reports/invoices",
* operationId="getInvoiceReport",
* tags={"reports"},
* summary="Invoice reports",
* description="Export invoice reports",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(ref="#/components/schemas/GenericReportSchema")
* ),
* @OA\Response(
* response=200,
* description="success",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function __invoke(GenericReportRequest $request)
{
// expect a list of visible fields, or use the default
$export = new QuoteExport(auth()->user()->company(), $request->all());
$csv = $export->run();
$headers = array(
'Content-Disposition' => 'attachment',
'Content-Type' => 'text/csv',
);
return response()->streamDownload(function () use ($csv) {
echo $csv;
}, $this->filename, $headers);
}
}

View File

@ -161,6 +161,7 @@ Route::group(['middleware' => ['throttle:300,1', 'api_db', 'token_auth', 'locale
Route::post('reports/expenses', 'Reports\ExpenseReportController'); Route::post('reports/expenses', 'Reports\ExpenseReportController');
Route::post('reports/invoices', 'Reports\InvoiceReportController'); Route::post('reports/invoices', 'Reports\InvoiceReportController');
Route::post('reports/invoice_items', 'Reports\InvoiceItemReportController'); Route::post('reports/invoice_items', 'Reports\InvoiceItemReportController');
Route::post('reports/quotes', 'Reports\QuoteReportController');
Route::get('scheduler', 'SchedulerController@index'); Route::get('scheduler', 'SchedulerController@index');