Additional column checks prior to export queries

This commit is contained in:
David Bomba 2024-06-24 07:56:13 +10:00
parent 06233240ce
commit 64f3af9c31
25 changed files with 44 additions and 29 deletions

View File

@ -109,7 +109,7 @@ class ActivityExport extends BaseExport
$query = Activity::query()
->where('company_id', $this->company->id);
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'activities');
return $query;
}

View File

@ -1245,13 +1245,13 @@ class BaseExport
* @param Builder $query
* @return Builder
*/
protected function addDateRange(Builder $query): Builder
protected function addDateRange(Builder $query, ?string $table_name = null): Builder
{
$query = $this->applyProductFilters($query);
$date_range = $this->input['date_range'];
if (array_key_exists('date_key', $this->input) && strlen($this->input['date_key']) > 1) {
if (array_key_exists('date_key', $this->input) && strlen($this->input['date_key']) > 1 && ($this->table_name && $this->columnExists($table_name, $this->input['date_key']))) {
$this->date_key = $this->input['date_key'];
}
@ -1609,4 +1609,17 @@ class BaseExport
}
}
/**
* Tests that the column exists
* on the table prior to adding it to
* the query builder
*
* @param string $table
* @param string $column
* @return bool
*/
public function columnExists($table, $column): bool
{
return \Illuminate\Support\Facades\Schema::hasColumn($table, $column);
}
}

View File

@ -131,7 +131,7 @@ class ClientExport extends BaseExport
$query->where('is_deleted', 0);
}
$query = $this->addDateRange($query);
$query = $this->addDateRange($query,' clients');
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);

View File

@ -63,7 +63,7 @@ class ContactExport extends BaseExport
$q->where('is_deleted', false);
});
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'client_contacts');
return $query;

View File

@ -108,7 +108,7 @@ class CreditExport extends BaseExport
->where('company_id', $this->company->id)
->where('is_deleted', $this->input['include_deleted'] ?? false);
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'credits');
$clients = &$this->input['client_id'];

View File

@ -76,7 +76,7 @@ class DocumentExport extends BaseExport
$query = Document::query()->where('company_id', $this->company->id);
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'documents');
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);

View File

@ -89,7 +89,7 @@ class ExpenseExport extends BaseExport
$query->where('is_deleted', 0);
}
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'expenses');
if($this->input['status'] ?? false) {
$query = $this->addExpenseStatusFilter($query, $this->input['status']);

View File

@ -67,7 +67,7 @@ class InvoiceExport extends BaseExport
$query->where('is_deleted', 0);
}
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'invoices');
$clients = &$this->input['client_id'];

View File

@ -79,7 +79,7 @@ class InvoiceItemExport extends BaseExport
$query->where('is_deleted', 0);
}
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'invoices');
$clients = &$this->input['client_id'];

View File

@ -62,7 +62,7 @@ class PaymentExport extends BaseExport
->where('company_id', $this->company->id)
->where('is_deleted', 0);
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'payments');
$clients = &$this->input['client_id'];

View File

@ -75,12 +75,11 @@ class ProductExport extends BaseExport
->withTrashed()
->where('company_id', $this->company->id);
if(!$this->input['include_deleted'] ?? false) {
if(!$this->input['include_deleted'] ?? false) { //@phpstan-ignore-line
$query->where('is_deleted', 0);
}
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'products');
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);

View File

@ -129,7 +129,7 @@ class ProductSalesExport extends BaseExport
->where('is_deleted', 0)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID]);
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'invoices');
$query = $this->filterByClients($query);

View File

@ -67,7 +67,7 @@ class PurchaseOrderExport extends BaseExport
$query->where('is_deleted', 0);
}
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'purchase_orders');
$clients = &$this->input['client_id'];

View File

@ -71,7 +71,7 @@ class PurchaseOrderItemExport extends BaseExport
$query->where('is_deleted', 0);
}
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'purchase_orders');
$clients = &$this->input['client_id'];

View File

@ -73,7 +73,7 @@ class QuoteExport extends BaseExport
$query->where('is_deleted', 0);
}
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'quotes');
$clients = &$this->input['client_id'];

View File

@ -74,7 +74,7 @@ class QuoteItemExport extends BaseExport
$query->where('is_deleted', 0);
}
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'quotes');
$clients = &$this->input['client_id'];

View File

@ -65,7 +65,7 @@ class RecurringInvoiceExport extends BaseExport
$query->where('is_deleted', 0);
}
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'recurring_invoices');
$clients = &$this->input['client_id'];

View File

@ -74,7 +74,7 @@ class TaskExport extends BaseExport
$query->where('is_deleted', 0);
}
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'tasks');
$clients = &$this->input['client_id'];

View File

@ -68,7 +68,7 @@ class VendorExport extends BaseExport
$query->where('is_deleted', 0);
}
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'vendors');
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);

View File

@ -45,6 +45,7 @@ class StoreProjectRequest extends Request
$rules['name'] = 'required';
$rules['client_id'] = 'required|exists:clients,id,company_id,'.$user->company()->id;
$rules['budgeted_hours'] = 'sometimes|numeric';
$rules['task_rate'] = 'required|bail|numeric';
if (isset($this->number)) {
$rules['number'] = Rule::unique('projects')->where('company_id', $user->company()->id);
@ -79,6 +80,8 @@ class StoreProjectRequest extends Request
$input['budgeted_hours'] = 0;
}
$input['task_rate'] = isset($input['task_rate']) ? $input['task_rate'] : 0;
$this->replace($input);
}

View File

@ -100,7 +100,7 @@ class ARDetailReport extends BaseExport
->orderBy('due_date', 'ASC')
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]);
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'invoices');
$query = $this->filterByClients($query);

View File

@ -110,7 +110,7 @@ class ClientBalanceReport extends BaseExport
$query = Invoice::query()->where('client_id', $client->id)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]);
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'invoices');
return [
$client->present()->name(),

View File

@ -103,7 +103,7 @@ class ClientSalesReport extends BaseExport
$query = Invoice::query()->where('client_id', $client->id)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID]);
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'invoices');
$amount = $query->sum('amount');
$balance = $query->sum('balance');

View File

@ -81,7 +81,7 @@ class TaxSummaryReport extends BaseExport
->where('is_deleted', 0)
->orderBy('balance', 'desc');
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'invoices');
$this->csv->insertOne([ctrans('texts.tax_summary')]);
$this->csv->insertOne([ctrans('texts.created_on'),' ',$this->translateDate(now()->format('Y-m-d'), $this->company->date_format(), $this->company->locale())]);

View File

@ -69,7 +69,7 @@ class UserSalesReport extends BaseExport
->where('is_deleted', 0)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL, Invoice::STATUS_PAID]);
$query = $this->addDateRange($query);
$query = $this->addDateRange($query, 'invoices');
$query = $this->filterByClients($query);