Improve Recurring Expense Filters

This commit is contained in:
David Bomba 2024-02-07 11:45:34 +11:00
parent f16e8fdd15
commit fca4b83d8b
3 changed files with 131 additions and 45 deletions

View File

@ -32,12 +32,21 @@ class RecurringExpenseFilters extends QueryFilters
}
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.'%');
$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;
}
/**

View File

@ -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();

View File

@ -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);
}
//
@ -340,42 +339,32 @@ class CompanyImport implements ShouldQueue
{
$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;
}