Export CSVs

This commit is contained in:
David Bomba 2022-04-27 13:07:29 +10:00
parent f467b32730
commit d5f30d5776
3 changed files with 95 additions and 13 deletions

View File

@ -0,0 +1,67 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Export\CSV;
use Illuminate\Support\Carbon;
class BaseExport
{
private function addDateRange($query)
{
$date_range = $this->input['date_range'];
if(array_key_exists('date_key', $this->input))
$this->date_key = $this->input['date_key'];
try{
$custom_start_date = Carbon::parse($this->input['start_date']);
$custom_end_date = Carbon::parse($this->input['end_date']);
}
catch(\Exception $e){
$custom_start_date = now()->startOfYear();
$custom_end_date = now();
}
switch ($date_range) {
case 'all':
return $query;
case 'last7':
return $query->whereBetween($this->date_key, [now()->subDays(7), now()]);
case 'last30':
return $query->whereBetween($this->date_key, [now()->subDays(30), now()]);
case 'this_month':
return $query->whereBetween($this->date_key, [now()->startOfMonth(), now()]);
case 'last_month':
return $query->whereBetween($this->date_key, [now()->startOfMonth()->subMonth(), now()->startOfMonth()->subMonth()->endOfMonth()]);
case 'this_quarter':
return $query->whereBetween($this->date_key, [(new \Carbon\Carbon('-3 months'))->firstOfQuarter(), (new \Carbon\Carbon('-3 months'))->lastOfQuarter()]);
case 'last_quarter':
return $query->whereBetween($this->date_key, [(new \Carbon\Carbon('-6 months'))->firstOfQuarter(), (new \Carbon\Carbon('-6 months'))->lastOfQuarter()]);
case 'this_year':
return $query->whereBetween($this->date_key, [now()->startOfYear(), now()]);
case 'custom':
return $query->whereBetween($this->date_key, [$custom_start_date, $custom_end_date]);
default:
return $query->whereBetween($this->date_key, [now()->startOfYear(), now()]);
}
}
}

View File

@ -19,17 +19,24 @@ use App\Transformers\ClientTransformer;
use App\Utils\Ninja;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
use Illuminate\Support\Carbon;
class ClientExport
{
private $company;
private $report_keys;
private $input;
private $client_transformer;
private $contact_transformer;
private string $start_date;
private string $end_date;
private string $date_key = 'created_at';
private array $entity_keys = [
'address1' => 'client.address1',
'address2' => 'client.address2',
@ -78,10 +85,10 @@ class ClientExport
'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();
}
@ -101,14 +108,16 @@ class ClientExport
//insert the header
$this->csv->insertOne($this->buildHeader());
Client::with('contacts')->where('company_id', $this->company->id)
->where('is_deleted',0)
->cursor()
->each(function ($client){
$query = Client::query()->with('contacts')
->where('company_id', $this->company->id)
->where('is_deleted',0);
$query = $this->addDateRange($query);
$this->csv->insertOne($this->buildRow($client));
});
$query->cursor()
->each(function ($client){
$this->csv->insertOne($this->buildRow($client));
});
return $this->csv->toString();
@ -120,7 +129,7 @@ class ClientExport
$header = [];
foreach(array_keys($this->report_keys) as $key)
foreach(array_keys($this->input['report_keys']) as $key)
$header[] = ctrans("texts.{$key}");
return $header;
@ -139,7 +148,7 @@ class ClientExport
$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

@ -60,7 +60,13 @@ class ClientReportController extends BaseController
// expect a list of visible fields, or use the default
// return response()->json(['message' => 'Processing'], 200);
$export = new ClientExport(auth()->user()->company(), $request->input('keys'));
// [
// 'report_keys',
// 'date_range',
// ]
$export = new ClientExport(auth()->user()->company(), $request->all());
$csv = $export->run();
}