From fca4b83d8b1cd8f00687087b1e53200b713d4391 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 7 Feb 2024 11:45:34 +1100 Subject: [PATCH] Improve Recurring Expense Filters --- app/Filters/RecurringExpenseFilters.php | 117 ++++++++++++++++++++++-- app/Jobs/Company/CompanyExport.php | 14 +-- app/Jobs/Company/CompanyImport.php | 45 ++++----- 3 files changed, 131 insertions(+), 45 deletions(-) diff --git a/app/Filters/RecurringExpenseFilters.php b/app/Filters/RecurringExpenseFilters.php index 5342d98ec2e0..b8f3825dc5e3 100644 --- a/app/Filters/RecurringExpenseFilters.php +++ b/app/Filters/RecurringExpenseFilters.php @@ -31,13 +31,22 @@ class RecurringExpenseFilters extends QueryFilters return $this->builder; } - return $this->builder->where(function ($query) use ($filter) { - $query->where('public_notes', 'like', '%'.$filter.'%') - ->orWhere('custom_value1', 'like', '%'.$filter.'%') - ->orWhere('custom_value2', 'like', '%'.$filter.'%') - ->orWhere('custom_value3', 'like', '%'.$filter.'%') - ->orWhere('custom_value4', 'like', '%'.$filter.'%'); + return $this->builder->where(function ($query) use ($filter) { + $query->where('number', 'like', '%' . $filter . '%') + ->orWhere('amount', 'like', '%' . $filter . '%') + ->orWhere('public_notes', 'like', '%' . $filter . '%') + ->orWhere('custom_value1', 'like', '%' . $filter . '%') + ->orWhere('custom_value2', 'like', '%' . $filter . '%') + ->orWhere('custom_value3', 'like', '%' . $filter . '%') + ->orWhere('custom_value4', 'like', '%' . $filter . '%') + ->orWhereHas('category', function ($q) use ($filter) { + $q->where('name', 'like', '%' . $filter . '%'); + }) + ->orWhereHas('vendor', function ($q) use ($filter) { + $q->where('name', 'like', '%' . $filter . '%'); + }); }); + } public function number(string $number = ''): Builder @@ -49,6 +58,74 @@ class RecurringExpenseFilters extends QueryFilters return $this->builder->where('number', $number); } + + /** + * Filter based on client status. + * + * Statuses we need to handle + * - all + * - logged + * - pending + * - invoiced + * - paid + * - unpaid + * + * @return Builder + */ + public function client_status(string $value = ''): Builder + { + if (strlen($value) == 0) { + return $this->builder; + } + + $status_parameters = explode(',', $value); + + if (in_array('all', $status_parameters)) { + return $this->builder; + } + + $this->builder->where(function ($query) use ($status_parameters) { + if (in_array('logged', $status_parameters)) { + $query->orWhere(function ($query) { + $query->where('amount', '>', 0) + ->whereNull('invoice_id') + ->whereNull('payment_date') + ->where('should_be_invoiced', false); + }); + } + + if (in_array('pending', $status_parameters)) { + $query->orWhere(function ($query) { + $query->where('should_be_invoiced', true) + ->whereNull('invoice_id'); + }); + } + + if (in_array('invoiced', $status_parameters)) { + $query->orWhere(function ($query) { + $query->whereNotNull('invoice_id'); + }); + } + + if (in_array('paid', $status_parameters)) { + $query->orWhere(function ($query) { + $query->whereNotNull('payment_date'); + }); + } + + if (in_array('unpaid', $status_parameters)) { + $query->orWhere(function ($query) { + $query->whereNull('payment_date'); + }); + } + }); + + // nlog($this->builder->toSql()); + + return $this->builder; + } + + /** * Sorts the list based on $sort. * @@ -63,9 +140,33 @@ class RecurringExpenseFilters extends QueryFilters return $this->builder; } - $dir = ($sort_col[1] == 'asc') ? 'asc' : 'desc'; + if ($sort_col[0] == 'client_id' && in_array($sort_col[1], ['asc', 'desc'])) { + return $this->builder + ->orderByRaw('ISNULL(client_id), client_id '. $sort_col[1]) + ->orderBy(\App\Models\Client::select('name') + ->whereColumn('clients.id', 'expenses.client_id'), $sort_col[1]); + } - return $this->builder->orderBy($sort_col[0], $dir); + if ($sort_col[0] == 'vendor_id' && in_array($sort_col[1], ['asc', 'desc'])) { + return $this->builder + ->orderByRaw('ISNULL(vendor_id), vendor_id '. $sort_col[1]) + ->orderBy(\App\Models\Vendor::select('name') + ->whereColumn('vendors.id', 'expenses.vendor_id'), $sort_col[1]); + + } + + if ($sort_col[0] == 'category_id' && in_array($sort_col[1], ['asc', 'desc'])) { + return $this->builder + ->orderByRaw('ISNULL(category_id), category_id '. $sort_col[1]) + ->orderBy(\App\Models\ExpenseCategory::select('name') + ->whereColumn('expense_categories.id', 'expenses.category_id'), $sort_col[1]); + } + + if (is_array($sort_col) && in_array($sort_col[1], ['asc', 'desc']) && in_array($sort_col[0], ['public_notes', 'date', 'id_number', 'custom_value1', 'custom_value2', 'custom_value3', 'custom_value4'])) { + return $this->builder->orderBy($sort_col[0], $sort_col[1]); + } + + return $this->builder; } /** diff --git a/app/Jobs/Company/CompanyExport.php b/app/Jobs/Company/CompanyExport.php index fd71232d2cc8..fecfb3b03c5b 100644 --- a/app/Jobs/Company/CompanyExport.php +++ b/app/Jobs/Company/CompanyExport.php @@ -74,16 +74,10 @@ class CompanyExport implements ShouldQueue $this->writer = new File($this->file_name); -$info = $this->writer->collection(''); - - - set_time_limit(0); - $this->export_data['app_version'] = config('ninja.app_version'); - $this->export_data['storage_url'] = Storage::url(''); - -$info->addItems($this->export_data); +$this->writer->value('app_version', config('ninja.app_version')); +$this->writer->value('storage_url', Storage::url('')); $this->export_data['activities'] = $this->company->all_activities->map(function ($activity) { $activity = $this->transformArrayOfKeys($activity, [ @@ -689,7 +683,9 @@ $this->writer->end(); nlog("cannot open {$zip_path}"); } - $zip->addFile($this->file_name, 'backup.json'); + $zip->addFile($this->file_name); + $zip->renameName($this->file_name, 'backup.json'); + // $zip->addFromString("backup.json", json_encode($this->export_data)); $zip->close(); diff --git a/app/Jobs/Company/CompanyImport.php b/app/Jobs/Company/CompanyImport.php index b240664b7c7d..4636fb40430e 100644 --- a/app/Jobs/Company/CompanyImport.php +++ b/app/Jobs/Company/CompanyImport.php @@ -272,8 +272,6 @@ class CompanyImport implements ShouldQueue nlog("Company ID = {$this->company->id}"); nlog("file_location ID = {$this->file_location}"); - // $this->backup_file = Cache::get($this->hash); - if (empty($this->file_location)) { throw new \Exception('No import data found, has the cache expired?'); } @@ -314,6 +312,7 @@ class CompanyImport implements ShouldQueue } unlink($tmp_file); + unlink($this->file_location); } // @@ -339,43 +338,33 @@ class CompanyImport implements ShouldQueue private function unzipFile() { $path = TempFile::filePath(Storage::disk(config('filesystems.default'))->get($this->file_location), basename($this->file_location)); - - nlog($path); + $zip = new ZipArchive(); - $res = $zip->open($path, ZipArchive::OVERWRITE); + $res = $zip->open($path); + $file_path = sys_get_temp_dir().'/'.sha1(microtime()); + if ($res === true) { + echo "ok"; + $extraction_res = $zip->extractTo($file_path); + nlog($extraction_res); -if ($res === true) { - echo 'ok'; - $zip->extractTo('test'); - $zip->close(); + $closer = $zip->close(); + nlog($closer); + } else { + echo "failed, code: " . $res; + } - // $file_path = sys_get_temp_dir().'/'.sha1(microtime()); + $file_path = "{$file_path}/backup.json"; + + nlog($file_path); - // nlog($file_path); - -// $result = $zip->extractTo($file_path); - -$result = $zip->extractTo("."); - - nlog($result); - - $$zip->close(); - -} else { - echo 'failed, code:' . $res; -} - - - $file_location = "backup.json"; - $file_path = $file_location; if (! file_exists($file_path)) { throw new NonExistingMigrationFile('Backup file does not exist, or is corrupted.'); } - return $file_location; + return $file_path; }