Improvements for reports

This commit is contained in:
David Bomba 2023-03-01 22:59:24 +11:00
parent 3af4b7aa49
commit f2456ce397
2 changed files with 69 additions and 6 deletions

View File

@ -11,10 +11,32 @@
namespace App\Export\CSV; namespace App\Export\CSV;
use App\Utils\Traits\MakesHash;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
class BaseExport class BaseExport
{ {
use MakesHash;
public array $input;
public string $date_key = '';
public array $entity_keys = [];
public string $start_date = '';
public string $end_date = '';
protected function filterByClients($query)
{
if (array_key_exists('client_id', $this->input) && $this->input['client_id'] != 'all') {
return $query->where('client_id', $this->decodePrimaryKey($this->input['client_id']));
}
return $query;
}
protected function addDateRange($query) protected function addDateRange($query)
{ {
$date_range = $this->input['date_range']; $date_range = $this->input['date_range'];
@ -33,24 +55,44 @@ class BaseExport
switch ($date_range) { switch ($date_range) {
case 'all': case 'all':
$this->start_date = 'All available data';
$this->end_date = 'All available data';
return $query; return $query;
case 'last7': case 'last7':
$this->start_date = now()->subDays(7)->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
return $query->whereBetween($this->date_key, [now()->subDays(7), now()])->orderBy($this->date_key, 'ASC'); return $query->whereBetween($this->date_key, [now()->subDays(7), now()])->orderBy($this->date_key, 'ASC');
case 'last30': case 'last30':
$this->start_date = now()->subDays(30)->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
return $query->whereBetween($this->date_key, [now()->subDays(30), now()])->orderBy($this->date_key, 'ASC'); return $query->whereBetween($this->date_key, [now()->subDays(30), now()])->orderBy($this->date_key, 'ASC');
case 'this_month': case 'this_month':
$this->start_date = now()->startOfMonth()->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
return $query->whereBetween($this->date_key, [now()->startOfMonth(), now()])->orderBy($this->date_key, 'ASC'); return $query->whereBetween($this->date_key, [now()->startOfMonth(), now()])->orderBy($this->date_key, 'ASC');
case 'last_month': case 'last_month':
$this->start_date = now()->startOfMonth()->subMonth()->format('Y-m-d');
$this->end_date = now()->startOfMonth()->subMonth()->endOfMonth()->format('Y-m-d');
return $query->whereBetween($this->date_key, [now()->startOfMonth()->subMonth(), now()->startOfMonth()->subMonth()->endOfMonth()])->orderBy($this->date_key, 'ASC'); return $query->whereBetween($this->date_key, [now()->startOfMonth()->subMonth(), now()->startOfMonth()->subMonth()->endOfMonth()])->orderBy($this->date_key, 'ASC');
case 'this_quarter': case 'this_quarter':
$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');
return $query->whereBetween($this->date_key, [(new \Carbon\Carbon('-3 months'))->firstOfQuarter(), (new \Carbon\Carbon('-3 months'))->lastOfQuarter()])->orderBy($this->date_key, 'ASC'); return $query->whereBetween($this->date_key, [(new \Carbon\Carbon('-3 months'))->firstOfQuarter(), (new \Carbon\Carbon('-3 months'))->lastOfQuarter()])->orderBy($this->date_key, 'ASC');
case 'last_quarter': case 'last_quarter':
$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');
return $query->whereBetween($this->date_key, [(new \Carbon\Carbon('-6 months'))->firstOfQuarter(), (new \Carbon\Carbon('-6 months'))->lastOfQuarter()])->orderBy($this->date_key, 'ASC'); return $query->whereBetween($this->date_key, [(new \Carbon\Carbon('-6 months'))->firstOfQuarter(), (new \Carbon\Carbon('-6 months'))->lastOfQuarter()])->orderBy($this->date_key, 'ASC');
case 'this_year': case 'this_year':
$this->start_date = now()->startOfYear()->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
return $query->whereBetween($this->date_key, [now()->startOfYear(), now()])->orderBy($this->date_key, 'ASC'); return $query->whereBetween($this->date_key, [now()->startOfYear(), now()])->orderBy($this->date_key, 'ASC');
case 'custom': case 'custom':
$this->start_date = $custom_start_date->format('Y-m-d');
$this->end_date = $custom_end_date->format('Y-m-d');
return $query->whereBetween($this->date_key, [$custom_start_date, $custom_end_date])->orderBy($this->date_key, 'ASC'); return $query->whereBetween($this->date_key, [$custom_start_date, $custom_end_date])->orderBy($this->date_key, 'ASC');
default: default:
$this->start_date = now()->startOfYear()->format('Y-m-d');
$this->end_date = now()->format('Y-m-d');
return $query->whereBetween($this->date_key, [now()->startOfYear(), now()])->orderBy($this->date_key, 'ASC'); return $query->whereBetween($this->date_key, [now()->startOfYear(), now()])->orderBy($this->date_key, 'ASC');
} }
} }

View File

@ -19,23 +19,24 @@ use App\Models\Product;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Database\Eloquent\Collection;
class ProductSalesExport extends BaseExport class ProductSalesExport extends BaseExport
{ {
private Company $company; private Company $company;
protected array $input; public array $input;
protected $date_key = 'created_at'; public string $date_key = 'created_at';
protected $products; protected Collection $products;
public Writer $csv; public Writer $csv;
private $sales; private \Illuminate\Support\Collection $sales;
//translations => keys //translations => keys
protected array $entity_keys = [ public array $entity_keys = [
'date' => 'date', 'date' => 'date',
'product_key' => 'product_key', 'product_key' => 'product_key',
'notes' => 'notes', 'notes' => 'notes',
@ -108,6 +109,8 @@ class ProductSalesExport extends BaseExport
$query = $this->addDateRange($query); $query = $this->addDateRange($query);
$query = $this->filterByClients($query);
$query->cursor() $query->cursor()
->each(function ($invoice) { ->each(function ($invoice) {
foreach ($invoice->line_items as $item) { foreach ($invoice->line_items as $item) {
@ -137,9 +140,27 @@ class ProductSalesExport extends BaseExport
return $data; return $data;
}); });
$this->csv->insertOne([]);
$this->csv->insertOne([]);
$this->csv->insertOne([]);
$this->csv->insertOne([]);
$this->csv->insertOne([]);
$this->csv->insertOne([ctrans('texts.summary'),ctrans('texts.product_sales'), $this->start_date, $this->end_date]);
nlog($grouped); if ($grouped->count() >=1) {
$header = [];
foreach ($grouped->first() as $key => $value) {
$header[] = ctrans("texts.{$key}");
}
$this->csv->insertOne($header);
$grouped->each(function ($item) {
$this->csv->insertOne(array_values($item));
});
}
return $this->csv->toString(); return $this->csv->toString();
} }