1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

CSV Exports

This commit is contained in:
David Bomba 2022-04-27 16:29:38 +10:00
parent ad3516aa26
commit 519e427cda
9 changed files with 215 additions and 30 deletions

View File

@ -109,6 +109,7 @@ class ClientExport extends BaseExport
$this->csv->insertOne($this->buildHeader());
$query = Client::query()->with('contacts')
->withTrashed()
->where('company_id', $this->company->id)
->where('is_deleted',0);

View File

@ -98,6 +98,7 @@ class CreditExport extends BaseExport
$this->csv->insertOne($this->buildHeader());
$query = Credit::query()
->withTrashed()
->with('client')->where('company_id', $this->company->id)
->where('is_deleted',0);

View File

@ -97,6 +97,7 @@ class ExpenseExport extends BaseExport
$query = Expense::query()
->with('client')
->withTrashed()
->where('company_id', $this->company->id)
->where('is_deleted',0);

View File

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

View File

@ -20,14 +20,16 @@ use App\Utils\Ninja;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class InvoiceItemExport
class InvoiceItemExport extends BaseExport
{
private $company;
private Company $company;
private $report_keys;
protected array $input;
private $invoice_transformer;
protected string $date_key = 'date';
private array $entity_keys = [
'amount' => 'amount',
'balance' => 'balance',
@ -88,10 +90,10 @@ class InvoiceItemExport
'currency',
];
public function __construct(Company $company, array $report_keys)
public function __construct(Company $company, array $input)
{
$this->company = $company;
$this->report_keys = $report_keys;
$this->input = $input;
$this->invoice_transformer = new InvoiceTransformer();
}
@ -110,14 +112,18 @@ class InvoiceItemExport
//insert the header
$this->csv->insertOne($this->buildHeader());
Invoice::with('client')->where('company_id', $this->company->id)
->where('is_deleted',0)
->cursor()
->each(function ($invoice){
$query = Invoice::query()
->with('client')->where('company_id', $this->company->id)
->where('is_deleted',0);
$this->iterateItems($invoice);
$query = $this->addDateRange($query);
});
$query->cursor()
->each(function ($invoice){
$this->iterateItems($invoice);
});
return $this->csv->toString();
@ -128,7 +134,7 @@ class InvoiceItemExport
$header = [];
foreach(array_keys($this->report_keys) as $key)
foreach(array_keys($this->input['report_keys']) as $key)
$header[] = ctrans("texts.{$key}");
return $header;
@ -144,7 +150,7 @@ class InvoiceItemExport
{
$item_array = [];
foreach(array_values($this->report_keys) as $key){
foreach(array_values($this->input['report_keys']) as $key){
if(str_contains($key, "item.")){
@ -160,7 +166,7 @@ class InvoiceItemExport
$transformed_items = $this->decorateAdvancedFields($invoice, $transformed_items);
foreach(array_values($this->report_keys) as $key)
foreach(array_values($this->input['report_keys']) as $key)
{
$key = str_replace("item.", "", $key);
$entity[$key] = $transformed_items[$key];
@ -179,7 +185,7 @@ class InvoiceItemExport
$entity = [];
foreach(array_values($this->report_keys) as $key){
foreach(array_values($this->input['report_keys']) as $key){
if(!str_contains($key, "item."))
$entity[$key] = $transformed_invoice[$key];

View File

@ -13,7 +13,7 @@ namespace App\Http\Controllers\Reports;
use App\Export\CSV\CreditExport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\ClientContactReportRequest;
use App\Http\Requests\Report\GenericReportRequest;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Response;
@ -60,7 +60,7 @@ class CreditReportController extends BaseController
* ),
* )
*/
public function __invoke(ClientContactReportRequest $request)
public function __invoke(GenericReportRequest $request)
{
// expect a list of visible fields, or use the default

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\InvoiceItemExport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\GenericReportRequest;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Response;
class InvoiceItemReportController extends BaseController
{
use MakesHash;
private string $filename = 'invoice_items.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 InvoiceItemExport(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

@ -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\InvoiceExport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\GenericReportRequest;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Response;
class InvoiceReportController extends BaseController
{
use MakesHash;
private string $filename = 'invoices.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 InvoiceExport(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

@ -159,6 +159,8 @@ Route::group(['middleware' => ['throttle:300,1', 'api_db', 'token_auth', 'locale
Route::post('reports/credits', 'Reports\CreditReportController');
Route::post('reports/documents', 'Reports\DocumentReportController');
Route::post('reports/expenses', 'Reports\ExpenseReportController');
Route::post('reports/invoices', 'Reports\InvoiceReportController');
Route::post('reports/invoice_items', 'Reports\InvoiceItemReportController');
Route::get('scheduler', 'SchedulerController@index');