mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-01 03:14:47 -04:00
Export CSVs
This commit is contained in:
parent
f467b32730
commit
d5f30d5776
67
app/Export/CSV/BaseExport.php
Normal file
67
app/Export/CSV/BaseExport.php
Normal 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()]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -19,17 +19,24 @@ use App\Transformers\ClientTransformer;
|
|||||||
use App\Utils\Ninja;
|
use App\Utils\Ninja;
|
||||||
use Illuminate\Support\Facades\App;
|
use Illuminate\Support\Facades\App;
|
||||||
use League\Csv\Writer;
|
use League\Csv\Writer;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
class ClientExport
|
class ClientExport
|
||||||
{
|
{
|
||||||
private $company;
|
private $company;
|
||||||
|
|
||||||
private $report_keys;
|
private $input;
|
||||||
|
|
||||||
private $client_transformer;
|
private $client_transformer;
|
||||||
|
|
||||||
private $contact_transformer;
|
private $contact_transformer;
|
||||||
|
|
||||||
|
private string $start_date;
|
||||||
|
|
||||||
|
private string $end_date;
|
||||||
|
|
||||||
|
private string $date_key = 'created_at';
|
||||||
|
|
||||||
private array $entity_keys = [
|
private array $entity_keys = [
|
||||||
'address1' => 'client.address1',
|
'address1' => 'client.address1',
|
||||||
'address2' => 'client.address2',
|
'address2' => 'client.address2',
|
||||||
@ -78,10 +85,10 @@ class ClientExport
|
|||||||
'client.industry',
|
'client.industry',
|
||||||
];
|
];
|
||||||
|
|
||||||
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->client_transformer = new ClientTransformer();
|
$this->client_transformer = new ClientTransformer();
|
||||||
$this->contact_transformer = new ClientContactTransformer();
|
$this->contact_transformer = new ClientContactTransformer();
|
||||||
}
|
}
|
||||||
@ -101,14 +108,16 @@ class ClientExport
|
|||||||
//insert the header
|
//insert the header
|
||||||
$this->csv->insertOne($this->buildHeader());
|
$this->csv->insertOne($this->buildHeader());
|
||||||
|
|
||||||
Client::with('contacts')->where('company_id', $this->company->id)
|
$query = Client::query()->with('contacts')
|
||||||
->where('is_deleted',0)
|
->where('company_id', $this->company->id)
|
||||||
->cursor()
|
->where('is_deleted',0);
|
||||||
->each(function ($client){
|
|
||||||
|
$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();
|
return $this->csv->toString();
|
||||||
@ -120,7 +129,7 @@ class ClientExport
|
|||||||
|
|
||||||
$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;
|
||||||
@ -139,7 +148,7 @@ class ClientExport
|
|||||||
|
|
||||||
$entity = [];
|
$entity = [];
|
||||||
|
|
||||||
foreach(array_values($this->report_keys) as $key){
|
foreach(array_values($this->input['report_keys']) as $key){
|
||||||
|
|
||||||
$parts = explode(".",$key);
|
$parts = explode(".",$key);
|
||||||
$entity[$parts[1]] = "";
|
$entity[$parts[1]] = "";
|
||||||
|
@ -60,7 +60,13 @@ class ClientReportController extends BaseController
|
|||||||
// expect a list of visible fields, or use the default
|
// expect a list of visible fields, or use the default
|
||||||
|
|
||||||
// return response()->json(['message' => 'Processing'], 200);
|
// 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();
|
$csv = $export->run();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user