This commit is contained in:
David Bomba 2022-04-27 15:17:45 +10:00
parent 6f491dfb92
commit 01ecc22d5f
11 changed files with 193 additions and 30 deletions

View File

@ -16,7 +16,7 @@ use Illuminate\Support\Carbon;
class BaseExport
{
private function addDateRange($query)
protected function addDateRange($query)
{
$date_range = $this->input['date_range'];

View File

@ -21,11 +21,11 @@ use Illuminate\Support\Facades\App;
use League\Csv\Writer;
use Illuminate\Support\Carbon;
class ClientExport
class ClientExport extends BaseExport
{
private $company;
private $input;
protected $input;
private $client_transformer;
@ -35,7 +35,7 @@ class ClientExport
private string $end_date;
private string $date_key = 'created_at';
protected string $date_key = 'created_at';
private array $entity_keys = [
'address1' => 'client.address1',

View File

@ -21,15 +21,17 @@ use App\Utils\Ninja;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class ContactExport
class ContactExport extends BaseExport
{
private $company;
private Company $company;
private $report_keys;
protected array $input;
private $client_transformer;
private ClientTransformer $client_transformer;
private $contact_transformer;
private ClientContactTransformer $contact_transformer;
protected string $date_key = 'created_at';
private array $entity_keys = [
'address1' => 'client.address1',
@ -79,10 +81,10 @@ class ContactExport
'client.industry',
];
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->client_transformer = new ClientTransformer();
$this->contact_transformer = new ClientContactTransformer();
}
@ -102,13 +104,16 @@ class ContactExport
//insert the header
$this->csv->insertOne($this->buildHeader());
ClientContact::where('company_id', $this->company->id)
->cursor()
->each(function ($contact){
$query = ClientContact::query()
->where('company_id', $this->company->id);
$this->csv->insertOne($this->buildRow($contact));
$query = $this->addDateRange($query);
});
$query->cursor()->each(function ($contact){
$this->csv->insertOne($this->buildRow($contact));
});
return $this->csv->toString();
@ -120,7 +125,7 @@ class ContactExport
$header = [];
foreach(array_keys($this->report_keys) as $key)
foreach(array_keys($this->input['report_keys']) as $key)
$header[] = ctrans("texts.{$key}");
return $header;
@ -136,7 +141,7 @@ class ContactExport
$entity = [];
foreach(array_values($this->report_keys) as $key){
foreach(array_values($this->input['report_keys']) as $key){
$parts = explode(".",$key);
$entity[$parts[1]] = "";

View File

@ -0,0 +1,21 @@
<?php
/**
* @OA\Schema(
* schema="ClientReportSchema",
* 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."),
* @OA\Property(property="start_date", type="string", example="2000-10-31", description="The start date to search between"),
* @OA\Property(property="end_date", type="string", example="2", description="The end date to search between"),
* @OA\Property(
* property="report_keys",
* type="array",
* @OA\Items(
* type="string",
* description="Array of Keys to export",
* example="['name','date']",
* ),
* ),
*
* )
*/

View File

@ -10,7 +10,6 @@
* property="contacts",
* type="array",
* @OA\Items(
*
* ref="#/components/schemas/ClientContact",
* ),
* ),

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\ContactExport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\ClientContactReportRequest;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Response;
class ClientContactReportController extends BaseController
{
use MakesHash;
private string $filename = 'contacts.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/ClientReportSchema")
* ),
* @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 ContactExport(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

@ -36,6 +36,10 @@ class ClientReportController extends BaseController
* 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/ClientReportSchema")
* ),
* @OA\Response(
* response=200,
* description="success",
@ -59,16 +63,19 @@ class ClientReportController extends BaseController
{
// expect a list of visible fields, or use the default
// return response()->json(['message' => 'Processing'], 200);
// [
// 'report_keys',
// 'date_range',
// ]
$export = new ClientExport(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;
}, 'clients.csv', $headers);
}

View File

@ -0,0 +1,38 @@
<?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'
];
}
}

View File

@ -24,4 +24,15 @@ class ClientReportRequest extends Request
{
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'
];
}
}

View File

@ -155,6 +155,7 @@ Route::group(['middleware' => ['throttle:300,1', 'api_db', 'token_auth', 'locale
Route::post('refresh', 'Auth\LoginController@refresh');
Route::post('reports/clients', 'Reports\ClientReportController');
Route::post('reports/contacts', 'Reports\ClientContactReportController');
Route::get('scheduler', 'SchedulerController@index');

View File

@ -34,8 +34,6 @@ class ExportCsvTest extends TestCase
ThrottleRequests::class
);
// $this->faker = \Faker\Factory::create();
$this->makeTestData();
$this->withoutExceptionHandling();
@ -61,7 +59,6 @@ class ExportCsvTest extends TestCase
// nlog(print_r($merged_keys, 1));
// nlog(print_r($merged_values, 1));
foreach ($merged_keys as &$key) {
$key = ctrans('texts.'.$key);
}
@ -76,7 +73,7 @@ class ExportCsvTest extends TestCase
}
}
//Storage::put(base_path('invy.csv'), $csv->getContent());
// Storage::put('invy.csv', $csv->getContent());
$this->markTestSkipped();
}