mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Expose export csv routes
This commit is contained in:
parent
01ecc22d5f
commit
ad3516aa26
@ -20,13 +20,15 @@ use App\Utils\Ninja;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use League\Csv\Writer;
|
||||
|
||||
class CreditExport
|
||||
class CreditExport extends BaseExport
|
||||
{
|
||||
private $company;
|
||||
private Company $company;
|
||||
|
||||
private $report_keys;
|
||||
protected array $input;
|
||||
|
||||
private $credit_transformer;
|
||||
private CreditTransformer $credit_transformer;
|
||||
|
||||
protected string $date_key = 'created_at';
|
||||
|
||||
private array $entity_keys = [
|
||||
'amount' => 'amount',
|
||||
@ -73,10 +75,10 @@ class CreditExport
|
||||
'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->credit_transformer = new CreditTransformer();
|
||||
}
|
||||
|
||||
@ -95,15 +97,18 @@ class CreditExport
|
||||
//insert the header
|
||||
$this->csv->insertOne($this->buildHeader());
|
||||
|
||||
Credit::with('client')->where('company_id', $this->company->id)
|
||||
->where('is_deleted',0)
|
||||
->cursor()
|
||||
->each(function ($credit){
|
||||
$query = Credit::query()
|
||||
->with('client')->where('company_id', $this->company->id)
|
||||
->where('is_deleted',0);
|
||||
|
||||
$this->csv->insertOne($this->buildRow($credit));
|
||||
$query = $this->addDateRange($query);
|
||||
|
||||
});
|
||||
$query->cursor()
|
||||
->each(function ($credit){
|
||||
|
||||
$this->csv->insertOne($this->buildRow($credit));
|
||||
|
||||
});
|
||||
|
||||
return $this->csv->toString();
|
||||
|
||||
@ -114,7 +119,7 @@ class CreditExport
|
||||
|
||||
$header = [];
|
||||
|
||||
foreach(array_keys($this->report_keys) as $key)
|
||||
foreach(array_keys($this->input['report_keys']) as $key)
|
||||
$header[] = ctrans("texts.{$key}");
|
||||
|
||||
return $header;
|
||||
@ -127,7 +132,7 @@ class CreditExport
|
||||
|
||||
$entity = [];
|
||||
|
||||
foreach(array_values($this->report_keys) as $key){
|
||||
foreach(array_values($this->input['report_keys']) as $key){
|
||||
|
||||
$entity[$key] = $transformed_credit[$key];
|
||||
}
|
||||
|
@ -21,14 +21,16 @@ use App\Utils\Ninja;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use League\Csv\Writer;
|
||||
|
||||
class DocumentExport
|
||||
class DocumentExport extends BaseExport
|
||||
{
|
||||
private $company;
|
||||
private Company $company;
|
||||
|
||||
private $report_keys;
|
||||
protected array $input;
|
||||
|
||||
private $entity_transformer;
|
||||
|
||||
protected $date_key = 'created_at';
|
||||
|
||||
private array $entity_keys = [
|
||||
'record_type' => 'record_type',
|
||||
'record_name' => 'record_name',
|
||||
@ -41,10 +43,10 @@ class DocumentExport
|
||||
|
||||
];
|
||||
|
||||
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->entity_transformer = new DocumentTransformer();
|
||||
}
|
||||
|
||||
@ -63,14 +65,16 @@ class DocumentExport
|
||||
//insert the header
|
||||
$this->csv->insertOne($this->buildHeader());
|
||||
|
||||
Document::where('company_id', $this->company->id)
|
||||
->cursor()
|
||||
->each(function ($entity){
|
||||
$query = Document::query()->where('company_id', $this->company->id);
|
||||
|
||||
$this->csv->insertOne($this->buildRow($entity));
|
||||
$query = $this->addDateRange($query);
|
||||
|
||||
});
|
||||
$query->cursor()
|
||||
->each(function ($entity){
|
||||
|
||||
$this->csv->insertOne($this->buildRow($entity));
|
||||
|
||||
});
|
||||
|
||||
return $this->csv->toString();
|
||||
|
||||
@ -81,7 +85,7 @@ class DocumentExport
|
||||
|
||||
$header = [];
|
||||
|
||||
foreach(array_keys($this->report_keys) as $key)
|
||||
foreach(array_keys($this->input['report_keys']) as $key)
|
||||
$header[] = ctrans("texts.{$key}");
|
||||
|
||||
return $header;
|
||||
@ -94,7 +98,7 @@ class DocumentExport
|
||||
|
||||
$entity = [];
|
||||
|
||||
foreach(array_values($this->report_keys) as $key){
|
||||
foreach(array_values($this->input['report_keys']) as $key){
|
||||
|
||||
$entity[$key] = $transformed_entity[$key];
|
||||
|
||||
|
@ -20,14 +20,16 @@ use App\Utils\Ninja;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use League\Csv\Writer;
|
||||
|
||||
class ExpenseExport
|
||||
class ExpenseExport extends BaseExport
|
||||
{
|
||||
private $company;
|
||||
private Company $company;
|
||||
|
||||
private $report_keys;
|
||||
protected array $input;
|
||||
|
||||
private $expense_transformer;
|
||||
|
||||
protected $date_key = 'date';
|
||||
|
||||
private array $entity_keys = [
|
||||
'amount' => 'amount',
|
||||
'category' => 'category_id',
|
||||
@ -71,10 +73,10 @@ class ExpenseExport
|
||||
'payment_type_id',
|
||||
];
|
||||
|
||||
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->expense_transformer = new ExpenseTransformer();
|
||||
}
|
||||
|
||||
@ -93,14 +95,19 @@ class ExpenseExport
|
||||
//insert the header
|
||||
$this->csv->insertOne($this->buildHeader());
|
||||
|
||||
Expense::with('client')->where('company_id', $this->company->id)
|
||||
->where('is_deleted',0)
|
||||
->cursor()
|
||||
->each(function ($expense){
|
||||
$query = Expense::query()
|
||||
->with('client')
|
||||
->where('company_id', $this->company->id)
|
||||
->where('is_deleted',0);
|
||||
|
||||
$this->csv->insertOne($this->buildRow($expense));
|
||||
$query = $this->addDateRange($query);
|
||||
|
||||
});
|
||||
$query->cursor()
|
||||
->each(function ($expense){
|
||||
|
||||
$this->csv->insertOne($this->buildRow($expense));
|
||||
|
||||
});
|
||||
|
||||
|
||||
return $this->csv->toString();
|
||||
@ -112,7 +119,7 @@ class ExpenseExport
|
||||
|
||||
$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 +132,7 @@ class ExpenseExport
|
||||
|
||||
$entity = [];
|
||||
|
||||
foreach(array_values($this->report_keys) as $key){
|
||||
foreach(array_values($this->input['report_keys']) as $key){
|
||||
|
||||
$entity[$key] = $transformed_expense[$key];
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* @OA\Schema(
|
||||
* schema="ClientReportSchema",
|
||||
* schema="GenericReportSchema",
|
||||
* type="object",
|
||||
* @OA\Property(property="date_range", type="string", example="last7", description="The string representation of the date range of data to be returned"),
|
||||
* @OA\Property(property="date_key", type="string", example="created_at", description="The date column to search between."),
|
@ -13,7 +13,7 @@ namespace App\Http\Controllers\Reports;
|
||||
|
||||
use App\Export\CSV\ContactExport;
|
||||
use App\Http\Controllers\BaseController;
|
||||
use App\Http\Requests\Report\ClientContactReportRequest;
|
||||
use App\Http\Requests\Report\GenericReportSchema;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
@ -30,16 +30,16 @@ class ClientContactReportController extends BaseController
|
||||
|
||||
/**
|
||||
* @OA\Post(
|
||||
* path="/api/v1/reports/clients",
|
||||
* operationId="getClientReport",
|
||||
* path="/api/v1/reports/contacts",
|
||||
* operationId="getContactReport",
|
||||
* tags={"reports"},
|
||||
* summary="Client reports",
|
||||
* description="Export client reports",
|
||||
* summary="Contact reports",
|
||||
* description="Export contact 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/ClientReportSchema")
|
||||
* @OA\JsonContent(ref="#/components/schemas/GenericReportSchema")
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
@ -60,7 +60,7 @@ class ClientContactReportController extends BaseController
|
||||
* ),
|
||||
* )
|
||||
*/
|
||||
public function __invoke(ClientContactReportRequest $request)
|
||||
public function __invoke(GenericReportSchema $request)
|
||||
{
|
||||
// expect a list of visible fields, or use the default
|
||||
|
||||
|
@ -13,7 +13,7 @@ namespace App\Http\Controllers\Reports;
|
||||
|
||||
use App\Export\CSV\ClientExport;
|
||||
use App\Http\Controllers\BaseController;
|
||||
use App\Http\Requests\Report\ClientReportRequest;
|
||||
use App\Http\Requests\Report\GenericReportRequest;
|
||||
use App\Models\Client;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\Response;
|
||||
@ -22,6 +22,8 @@ class ClientReportController extends BaseController
|
||||
{
|
||||
use MakesHash;
|
||||
|
||||
private string $filename = 'clients.csv';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
@ -59,7 +61,7 @@ class ClientReportController extends BaseController
|
||||
* ),
|
||||
* )
|
||||
*/
|
||||
public function __invoke(ClientReportRequest $request)
|
||||
public function __invoke(GenericReportRequest $request)
|
||||
{
|
||||
// expect a list of visible fields, or use the default
|
||||
|
||||
@ -74,7 +76,7 @@ class ClientReportController extends BaseController
|
||||
|
||||
return response()->streamDownload(function () use ($csv) {
|
||||
echo $csv;
|
||||
}, 'clients.csv', $headers);
|
||||
}, $this->filename, $headers);
|
||||
|
||||
}
|
||||
|
||||
|
84
app/Http/Controllers/Reports/CreditReportController.php
Normal file
84
app/Http/Controllers/Reports/CreditReportController.php
Normal 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\CreditExport;
|
||||
use App\Http\Controllers\BaseController;
|
||||
use App\Http\Requests\Report\ClientContactReportRequest;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class CreditReportController extends BaseController
|
||||
{
|
||||
use MakesHash;
|
||||
|
||||
private string $filename = 'credits.csv';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Post(
|
||||
* path="/api/v1/reports/clients",
|
||||
* operationId="getClientReport",
|
||||
* tags={"reports"},
|
||||
* summary="Client reports",
|
||||
* description="Export client 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(ClientContactReportRequest $request)
|
||||
{
|
||||
// expect a list of visible fields, or use the default
|
||||
|
||||
$export = new CreditExport(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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
84
app/Http/Controllers/Reports/DocumentReportController.php
Normal file
84
app/Http/Controllers/Reports/DocumentReportController.php
Normal 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\DocumentExport;
|
||||
use App\Http\Controllers\BaseController;
|
||||
use App\Http\Requests\Report\ClientContactReportRequest;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class DocumentReportController extends BaseController
|
||||
{
|
||||
use MakesHash;
|
||||
|
||||
private string $filename = 'documents.csv';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Post(
|
||||
* path="/api/v1/reports/documents",
|
||||
* operationId="getDocumentReport",
|
||||
* tags={"reports"},
|
||||
* summary="Document reports",
|
||||
* description="Export document 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(ClientContactReportRequest $request)
|
||||
{
|
||||
// expect a list of visible fields, or use the default
|
||||
|
||||
$export = new DocumentExport(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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
85
app/Http/Controllers/Reports/ExpenseReportController.php
Normal file
85
app/Http/Controllers/Reports/ExpenseReportController.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?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\ExpenseExport;
|
||||
use App\Http\Controllers\BaseController;
|
||||
use App\Http\Requests\Report\GenericReportRequest;
|
||||
use App\Models\Client;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class ExpenseReportController extends BaseController
|
||||
{
|
||||
use MakesHash;
|
||||
|
||||
private string $filename = 'expense.csv';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Post(
|
||||
* path="/api/v1/reports/expense",
|
||||
* operationId="getExpenseReport",
|
||||
* tags={"reports"},
|
||||
* summary="Expense reports",
|
||||
* description="Export expense 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 ExpenseExport(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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
<?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\Requests\Report;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
|
||||
class ClientContactReportRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize() : bool
|
||||
{
|
||||
return auth()->user()->isAdmin();
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'start_date' => 'string|date',
|
||||
'end_date' => 'string|date',
|
||||
'date_key' => 'string',
|
||||
'date_range' => 'string',
|
||||
'report_keys' => 'sometimes|array'
|
||||
];
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@ namespace App\Http\Requests\Report;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
|
||||
class ClientReportRequest extends Request
|
||||
class GenericReportRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
@ -156,6 +156,9 @@ Route::group(['middleware' => ['throttle:300,1', 'api_db', 'token_auth', 'locale
|
||||
|
||||
Route::post('reports/clients', 'Reports\ClientReportController');
|
||||
Route::post('reports/contacts', 'Reports\ClientContactReportController');
|
||||
Route::post('reports/credits', 'Reports\CreditReportController');
|
||||
Route::post('reports/documents', 'Reports\DocumentReportController');
|
||||
Route::post('reports/expenses', 'Reports\ExpenseReportController');
|
||||
|
||||
|
||||
Route::get('scheduler', 'SchedulerController@index');
|
||||
|
Loading…
x
Reference in New Issue
Block a user