1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Export/CSV/BaseExport.php

131 lines
5.8 KiB
PHP
Raw Normal View History

2022-04-27 05:07:29 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2022-04-27 05:07:29 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Export\CSV;
2023-03-01 13:02:48 +01:00
use App\Models\Client;
use App\Utils\Traits\MakesHash;
2023-03-18 08:24:56 +01:00
use Illuminate\Support\Carbon;
2022-04-27 05:07:29 +02:00
class BaseExport
{
2023-03-01 12:59:24 +01:00
use MakesHash;
public array $input;
public string $date_key = '';
public array $entity_keys = [];
public string $start_date = '';
public string $end_date = '';
2023-03-01 13:02:48 +01:00
public string $client_description = 'All Clients';
2023-03-01 12:59:24 +01:00
protected function filterByClients($query)
{
2023-03-01 13:06:46 +01:00
if (isset($this->input['client_id']) && $this->input['client_id'] != 'all') {
2023-03-01 13:02:48 +01:00
$client = Client::withTrashed()->find($this->input['client_id']);
$this->client_description = $client->present()->name;
return $query->where('client_id', $this->input['client_id']);
2023-03-01 12:59:24 +01:00
}
2023-04-13 07:39:12 +02:00
elseif(isset($this->input['clients']) && count($this->input['clients']) > 0) {
2023-04-14 02:27:00 +02:00
2023-04-13 07:39:12 +02:00
$this->client_description = 'Multiple Clients';
return $query->whereIn('client_id', $this->input['clients']);
}
2023-03-01 12:59:24 +01:00
return $query;
}
2022-04-27 07:17:45 +02:00
protected function addDateRange($query)
2022-04-27 05:07:29 +02:00
{
$date_range = $this->input['date_range'];
if (array_key_exists('date_key', $this->input) && strlen($this->input['date_key']) > 1) {
2022-04-27 05:07:29 +02:00
$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) {
2022-04-27 05:07:29 +02:00
$custom_start_date = now()->startOfYear();
$custom_end_date = now();
}
2022-04-27 05:07:29 +02:00
switch ($date_range) {
case 'all':
2023-03-01 12:59:24 +01:00
$this->start_date = 'All available data';
$this->end_date = 'All available data';
2022-04-27 05:07:29 +02:00
return $query;
case 'last7':
2023-03-01 12:59:24 +01:00
$this->start_date = now()->subDays(7)->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [now()->subDays(7), now()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'last30':
2023-03-01 12:59:24 +01:00
$this->start_date = now()->subDays(30)->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [now()->subDays(30), now()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'this_month':
2023-03-01 12:59:24 +01:00
$this->start_date = now()->startOfMonth()->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [now()->startOfMonth(), now()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'last_month':
2023-03-01 12:59:24 +01:00
$this->start_date = now()->startOfMonth()->subMonth()->format('Y-m-d');
$this->end_date = now()->startOfMonth()->subMonth()->endOfMonth()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [now()->startOfMonth()->subMonth(), now()->startOfMonth()->subMonth()->endOfMonth()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'this_quarter':
2023-03-01 12:59:24 +01:00
$this->start_date = (new \Carbon\Carbon('-3 months'))->firstOfQuarter()->format('Y-m-d');
$this->end_date = (new \Carbon\Carbon('-3 months'))->lastOfQuarter()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [(new \Carbon\Carbon('-3 months'))->firstOfQuarter(), (new \Carbon\Carbon('-3 months'))->lastOfQuarter()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'last_quarter':
2023-03-01 12:59:24 +01:00
$this->start_date = (new \Carbon\Carbon('-6 months'))->firstOfQuarter()->format('Y-m-d');
$this->end_date = (new \Carbon\Carbon('-6 months'))->lastOfQuarter()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [(new \Carbon\Carbon('-6 months'))->firstOfQuarter(), (new \Carbon\Carbon('-6 months'))->lastOfQuarter()])->orderBy($this->date_key, 'ASC');
2023-04-14 03:18:50 +02:00
case 'last365_days':
$this->start_date = now()->startOfDay()->subDays(365)->format('Y-m-d');
$this->end_date = now()->startOfDay()->format('Y-m-d');
return $query->whereBetween($this->date_key, [now()->subDays(365), now()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'this_year':
2023-03-01 12:59:24 +01:00
$this->start_date = now()->startOfYear()->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [now()->startOfYear(), now()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
case 'custom':
2023-03-01 12:59:24 +01:00
$this->start_date = $custom_start_date->format('Y-m-d');
$this->end_date = $custom_end_date->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [$custom_start_date, $custom_end_date])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
default:
2023-03-01 12:59:24 +01:00
$this->start_date = now()->startOfYear()->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
2022-04-27 05:20:41 +02:00
return $query->whereBetween($this->date_key, [now()->startOfYear(), now()])->orderBy($this->date_key, 'ASC');
2022-04-27 05:07:29 +02:00
}
}
2023-04-14 05:04:28 +02:00
public function buildHeader() :array
2022-04-27 10:05:54 +02:00
{
$header = [];
foreach ($this->input['report_keys'] as $value) {
$key = array_search($value, $this->entity_keys);
2022-05-10 05:39:08 +02:00
$key = str_replace('item.', '', $key);
$key = str_replace('invoice.', '', $key);
$key = str_replace('client.', '', $key);
$key = str_replace('contact.', '', $key);
2022-05-07 09:10:23 +02:00
2022-04-27 10:05:54 +02:00
$header[] = ctrans("texts.{$key}");
}
return $header;
}
}