From 64f3af9c318a84730d4609fdc5a61326ef054cb1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 24 Jun 2024 07:56:13 +1000 Subject: [PATCH] Additional column checks prior to export queries --- app/Export/CSV/ActivityExport.php | 2 +- app/Export/CSV/BaseExport.php | 19 ++++++++++++++++--- app/Export/CSV/ClientExport.php | 2 +- app/Export/CSV/ContactExport.php | 2 +- app/Export/CSV/CreditExport.php | 2 +- app/Export/CSV/DocumentExport.php | 2 +- app/Export/CSV/ExpenseExport.php | 2 +- app/Export/CSV/InvoiceExport.php | 2 +- app/Export/CSV/InvoiceItemExport.php | 2 +- app/Export/CSV/PaymentExport.php | 2 +- app/Export/CSV/ProductExport.php | 5 ++--- app/Export/CSV/ProductSalesExport.php | 2 +- app/Export/CSV/PurchaseOrderExport.php | 2 +- app/Export/CSV/PurchaseOrderItemExport.php | 2 +- app/Export/CSV/QuoteExport.php | 2 +- app/Export/CSV/QuoteItemExport.php | 2 +- app/Export/CSV/RecurringInvoiceExport.php | 2 +- app/Export/CSV/TaskExport.php | 2 +- app/Export/CSV/VendorExport.php | 2 +- .../Requests/Project/StoreProjectRequest.php | 5 ++++- app/Services/Report/ARDetailReport.php | 2 +- app/Services/Report/ClientBalanceReport.php | 2 +- app/Services/Report/ClientSalesReport.php | 2 +- app/Services/Report/TaxSummaryReport.php | 2 +- app/Services/Report/UserSalesReport.php | 2 +- 25 files changed, 44 insertions(+), 29 deletions(-) diff --git a/app/Export/CSV/ActivityExport.php b/app/Export/CSV/ActivityExport.php index 96d3b98106f5..fea9ba8722cd 100644 --- a/app/Export/CSV/ActivityExport.php +++ b/app/Export/CSV/ActivityExport.php @@ -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; } diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index 2ecdce7c4170..b3ab47c4a5d4 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -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']; } @@ -1608,5 +1608,18 @@ class BaseExport ZipDocuments::dispatch($documents, $this->company, $user); } } - + + /** + * 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); + } } diff --git a/app/Export/CSV/ClientExport.php b/app/Export/CSV/ClientExport.php index 8732f9800eef..f0a5314f76e6 100644 --- a/app/Export/CSV/ClientExport.php +++ b/app/Export/CSV/ClientExport.php @@ -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); diff --git a/app/Export/CSV/ContactExport.php b/app/Export/CSV/ContactExport.php index f21fb8157018..21c673b49d96 100644 --- a/app/Export/CSV/ContactExport.php +++ b/app/Export/CSV/ContactExport.php @@ -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; diff --git a/app/Export/CSV/CreditExport.php b/app/Export/CSV/CreditExport.php index 16ea287a0101..1295d8ec2b6d 100644 --- a/app/Export/CSV/CreditExport.php +++ b/app/Export/CSV/CreditExport.php @@ -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']; diff --git a/app/Export/CSV/DocumentExport.php b/app/Export/CSV/DocumentExport.php index 52757287cb1a..8adfe57a0525 100644 --- a/app/Export/CSV/DocumentExport.php +++ b/app/Export/CSV/DocumentExport.php @@ -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); diff --git a/app/Export/CSV/ExpenseExport.php b/app/Export/CSV/ExpenseExport.php index 8610193e824a..dfcc554b8891 100644 --- a/app/Export/CSV/ExpenseExport.php +++ b/app/Export/CSV/ExpenseExport.php @@ -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']); diff --git a/app/Export/CSV/InvoiceExport.php b/app/Export/CSV/InvoiceExport.php index e8c096778313..b4e00d42b8b7 100644 --- a/app/Export/CSV/InvoiceExport.php +++ b/app/Export/CSV/InvoiceExport.php @@ -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']; diff --git a/app/Export/CSV/InvoiceItemExport.php b/app/Export/CSV/InvoiceItemExport.php index 7068a33170e1..f6d5d814a763 100644 --- a/app/Export/CSV/InvoiceItemExport.php +++ b/app/Export/CSV/InvoiceItemExport.php @@ -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']; diff --git a/app/Export/CSV/PaymentExport.php b/app/Export/CSV/PaymentExport.php index 02b519346964..d427b3ebe1d6 100644 --- a/app/Export/CSV/PaymentExport.php +++ b/app/Export/CSV/PaymentExport.php @@ -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']; diff --git a/app/Export/CSV/ProductExport.php b/app/Export/CSV/ProductExport.php index 48d76b8577d2..69bdca55cffb 100644 --- a/app/Export/CSV/ProductExport.php +++ b/app/Export/CSV/ProductExport.php @@ -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); diff --git a/app/Export/CSV/ProductSalesExport.php b/app/Export/CSV/ProductSalesExport.php index 7b5eb83a243d..a8e2082d183b 100644 --- a/app/Export/CSV/ProductSalesExport.php +++ b/app/Export/CSV/ProductSalesExport.php @@ -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); diff --git a/app/Export/CSV/PurchaseOrderExport.php b/app/Export/CSV/PurchaseOrderExport.php index c09353387d15..2f83844a4186 100644 --- a/app/Export/CSV/PurchaseOrderExport.php +++ b/app/Export/CSV/PurchaseOrderExport.php @@ -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']; diff --git a/app/Export/CSV/PurchaseOrderItemExport.php b/app/Export/CSV/PurchaseOrderItemExport.php index fcebc7810f5c..bf39cc26efe7 100644 --- a/app/Export/CSV/PurchaseOrderItemExport.php +++ b/app/Export/CSV/PurchaseOrderItemExport.php @@ -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']; diff --git a/app/Export/CSV/QuoteExport.php b/app/Export/CSV/QuoteExport.php index 4844963f4ea2..7c77fd990595 100644 --- a/app/Export/CSV/QuoteExport.php +++ b/app/Export/CSV/QuoteExport.php @@ -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']; diff --git a/app/Export/CSV/QuoteItemExport.php b/app/Export/CSV/QuoteItemExport.php index 04caacf613ad..ddc7279605f9 100644 --- a/app/Export/CSV/QuoteItemExport.php +++ b/app/Export/CSV/QuoteItemExport.php @@ -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']; diff --git a/app/Export/CSV/RecurringInvoiceExport.php b/app/Export/CSV/RecurringInvoiceExport.php index 500137b88683..d6d26e283422 100644 --- a/app/Export/CSV/RecurringInvoiceExport.php +++ b/app/Export/CSV/RecurringInvoiceExport.php @@ -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']; diff --git a/app/Export/CSV/TaskExport.php b/app/Export/CSV/TaskExport.php index 07a5542273e5..f630517a2c81 100644 --- a/app/Export/CSV/TaskExport.php +++ b/app/Export/CSV/TaskExport.php @@ -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']; diff --git a/app/Export/CSV/VendorExport.php b/app/Export/CSV/VendorExport.php index 5b67792d2b7a..abb682f655eb 100644 --- a/app/Export/CSV/VendorExport.php +++ b/app/Export/CSV/VendorExport.php @@ -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); diff --git a/app/Http/Requests/Project/StoreProjectRequest.php b/app/Http/Requests/Project/StoreProjectRequest.php index fdaca3b2db6a..04c698feef29 100644 --- a/app/Http/Requests/Project/StoreProjectRequest.php +++ b/app/Http/Requests/Project/StoreProjectRequest.php @@ -45,7 +45,8 @@ 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); } diff --git a/app/Services/Report/ARDetailReport.php b/app/Services/Report/ARDetailReport.php index 048cb76cd0dd..be8c83069936 100644 --- a/app/Services/Report/ARDetailReport.php +++ b/app/Services/Report/ARDetailReport.php @@ -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); diff --git a/app/Services/Report/ClientBalanceReport.php b/app/Services/Report/ClientBalanceReport.php index c5ca05c5fa97..a4a6b9f7eb1b 100644 --- a/app/Services/Report/ClientBalanceReport.php +++ b/app/Services/Report/ClientBalanceReport.php @@ -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(), diff --git a/app/Services/Report/ClientSalesReport.php b/app/Services/Report/ClientSalesReport.php index c2bd9346bdcc..2e88dcccc109 100644 --- a/app/Services/Report/ClientSalesReport.php +++ b/app/Services/Report/ClientSalesReport.php @@ -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'); diff --git a/app/Services/Report/TaxSummaryReport.php b/app/Services/Report/TaxSummaryReport.php index b44b795570e2..a7b757aa6eb7 100644 --- a/app/Services/Report/TaxSummaryReport.php +++ b/app/Services/Report/TaxSummaryReport.php @@ -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())]); diff --git a/app/Services/Report/UserSalesReport.php b/app/Services/Report/UserSalesReport.php index 9b3dec875fa6..a94c4b0e9930 100644 --- a/app/Services/Report/UserSalesReport.php +++ b/app/Services/Report/UserSalesReport.php @@ -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);