1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 06:02:39 +01:00
invoiceninja/app/Export/CSV/BaseExport.php

86 lines
3.1 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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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;
use Illuminate\Support\Carbon;
class BaseExport
{
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'];
2022-04-27 05:20:41 +02:00
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){
$custom_start_date = now()->startOfYear();
$custom_end_date = now();
}
switch ($date_range) {
case 'all':
return $query;
case 'last7':
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':
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':
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':
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':
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':
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');
2022-04-27 05:07:29 +02:00
case 'this_year':
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':
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:
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
}
}
2022-04-27 10:05:54 +02:00
protected function buildHeader() :array
{
$header = [];
foreach($this->input['report_keys'] as $value){
2022-05-10 05:39:08 +02:00
2022-04-27 10:05:54 +02:00
$key = array_search ($value, $this->entity_keys);
2022-05-07 09:10:23 +02:00
$key = str_replace("item.", "", $key);
$key = str_replace("invoice.", "", $key);
$key = str_replace("client.", "", $key);
$key = str_replace("contact.", "", $key);
2022-04-27 10:05:54 +02:00
$header[] = ctrans("texts.{$key}");
}
return $header;
}
2022-04-27 05:07:29 +02:00
}