diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index 96990889b935..d8754cb598d2 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -11,10 +11,32 @@ namespace App\Export\CSV; +use App\Utils\Traits\MakesHash; use Illuminate\Support\Carbon; 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) { $date_range = $this->input['date_range']; @@ -33,24 +55,44 @@ class BaseExport switch ($date_range) { case 'all': + $this->start_date = 'All available data'; + $this->end_date = 'All available data'; return $query; 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'); 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'); 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'); 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'); 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'); 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'); 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'); 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'); 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'); } } diff --git a/app/Export/CSV/ProductSalesExport.php b/app/Export/CSV/ProductSalesExport.php index 65d3864df18b..87055711cff2 100644 --- a/app/Export/CSV/ProductSalesExport.php +++ b/app/Export/CSV/ProductSalesExport.php @@ -19,23 +19,24 @@ use App\Models\Product; use App\Libraries\MultiDB; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\App; +use Illuminate\Database\Eloquent\Collection; class ProductSalesExport extends BaseExport { 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; - private $sales; + private \Illuminate\Support\Collection $sales; //translations => keys - protected array $entity_keys = [ + public array $entity_keys = [ 'date' => 'date', 'product_key' => 'product_key', 'notes' => 'notes', @@ -108,6 +109,8 @@ class ProductSalesExport extends BaseExport $query = $this->addDateRange($query); + $query = $this->filterByClients($query); + $query->cursor() ->each(function ($invoice) { foreach ($invoice->line_items as $item) { @@ -137,9 +140,27 @@ class ProductSalesExport extends BaseExport 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(); }