diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php new file mode 100644 index 000000000000..79c89a5e8fcb --- /dev/null +++ b/app/Export/CSV/BaseExport.php @@ -0,0 +1,67 @@ +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()]); + + } + + } + +} \ No newline at end of file diff --git a/app/Export/CSV/ClientExport.php b/app/Export/CSV/ClientExport.php index 1bb24ab057ef..c98eab38eb89 100644 --- a/app/Export/CSV/ClientExport.php +++ b/app/Export/CSV/ClientExport.php @@ -19,17 +19,24 @@ use App\Transformers\ClientTransformer; use App\Utils\Ninja; use Illuminate\Support\Facades\App; use League\Csv\Writer; +use Illuminate\Support\Carbon; class ClientExport { private $company; - private $report_keys; + private $input; private $client_transformer; private $contact_transformer; + private string $start_date; + + private string $end_date; + + private string $date_key = 'created_at'; + private array $entity_keys = [ 'address1' => 'client.address1', 'address2' => 'client.address2', @@ -78,10 +85,10 @@ class ClientExport 'client.industry', ]; - public function __construct(Company $company, array $report_keys) + public function __construct(Company $company, array $input) { $this->company = $company; - $this->report_keys = $report_keys; + $this->input = $input; $this->client_transformer = new ClientTransformer(); $this->contact_transformer = new ClientContactTransformer(); } @@ -101,14 +108,16 @@ class ClientExport //insert the header $this->csv->insertOne($this->buildHeader()); - Client::with('contacts')->where('company_id', $this->company->id) - ->where('is_deleted',0) - ->cursor() - ->each(function ($client){ + $query = Client::query()->with('contacts') + ->where('company_id', $this->company->id) + ->where('is_deleted',0); + + $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(); @@ -120,7 +129,7 @@ class ClientExport $header = []; - foreach(array_keys($this->report_keys) as $key) + foreach(array_keys($this->input['report_keys']) as $key) $header[] = ctrans("texts.{$key}"); return $header; @@ -139,7 +148,7 @@ class ClientExport $entity = []; - foreach(array_values($this->report_keys) as $key){ + foreach(array_values($this->input['report_keys']) as $key){ $parts = explode(".",$key); $entity[$parts[1]] = ""; diff --git a/app/Http/Controllers/Reports/ClientReportController.php b/app/Http/Controllers/Reports/ClientReportController.php index 804cbce47d71..dccf680fdd74 100644 --- a/app/Http/Controllers/Reports/ClientReportController.php +++ b/app/Http/Controllers/Reports/ClientReportController.php @@ -60,7 +60,13 @@ class ClientReportController extends BaseController // expect a list of visible fields, or use the default // 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(); }