From 1415434d345239145de64eaae372d8790acd18e0 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 13 Aug 2020 16:15:46 +1000 Subject: [PATCH] tests --- .../Requests/Credit/StoreCreditRequest.php | 3 + .../Credit/UniqueCreditNumberRule.php | 70 +++++++++++++++++++ .../Requests/Credit/UpdateCreditRequest.php | 3 + app/Http/Requests/Quote/StoreQuoteRequest.php | 3 + .../Requests/Quote/UniqueQuoteNumberRule.php | 70 +++++++++++++++++++ .../Requests/Quote/UpdateQuoteRequest.php | 4 ++ 6 files changed, 153 insertions(+) create mode 100644 app/Http/Requests/Credit/UniqueCreditNumberRule.php create mode 100644 app/Http/Requests/Quote/UniqueQuoteNumberRule.php diff --git a/app/Http/Requests/Credit/StoreCreditRequest.php b/app/Http/Requests/Credit/StoreCreditRequest.php index 41d17230e1..4b18e185af 100644 --- a/app/Http/Requests/Credit/StoreCreditRequest.php +++ b/app/Http/Requests/Credit/StoreCreditRequest.php @@ -2,6 +2,7 @@ namespace App\Http\Requests\Credit; +use App\Http\ValidationRules\Credit\UniqueCreditNumberRule; use App\Models\Credit; use App\Utils\Traits\CleanLineItems; use App\Utils\Traits\MakesHash; @@ -42,6 +43,8 @@ class StoreCreditRequest extends FormRequest } $rules['client_id'] = 'required|exists:clients,id,company_id,'.auth()->user()->company()->id; + + $rules['number'] = new UniqueCreditNumberRule($this->all()); return $rules; } diff --git a/app/Http/Requests/Credit/UniqueCreditNumberRule.php b/app/Http/Requests/Credit/UniqueCreditNumberRule.php new file mode 100644 index 0000000000..2374ff7169 --- /dev/null +++ b/app/Http/Requests/Credit/UniqueCreditNumberRule.php @@ -0,0 +1,70 @@ +input = $input; + } + + /** + * @param string $attribute + * @param mixed $value + * @return bool + */ + public function passes($attribute, $value) + { + return $this->checkIfCreditNumberUnique(); //if it exists, return false! + } + + /** + * @return string + */ + public function message() + { + return "Credit number already taken"; + } + + /** + * @param $email + * + * //off,when_sent,when_paid + * + * @return bool + */ + private function checkIfCreditNumberUnique() : bool + { + + $credit = Credit::where('client_id', $this->input['client_id']) + ->where('number', $this->input['number']) + ->withTrashed() + ->exists(); + + if($credit) + return false; + + return true; + } +} diff --git a/app/Http/Requests/Credit/UpdateCreditRequest.php b/app/Http/Requests/Credit/UpdateCreditRequest.php index 6e268a1a4b..226ab271bb 100644 --- a/app/Http/Requests/Credit/UpdateCreditRequest.php +++ b/app/Http/Requests/Credit/UpdateCreditRequest.php @@ -42,6 +42,9 @@ class UpdateCreditRequest extends FormRequest $rules['documents'] = 'file|mimes:png,ai,svg,jpeg,tiff,pdf,gif,psd,txt,doc,xls,ppt,xlsx,docx,pptx|max:20000'; } + if($this->input('number')) + $rules['number'] = 'unique:quotes,number,' . $this->id . ',id,company_id,' . $this->invoice->company_id; + return $rules; } diff --git a/app/Http/Requests/Quote/StoreQuoteRequest.php b/app/Http/Requests/Quote/StoreQuoteRequest.php index fce0c0df72..a51f44042e 100644 --- a/app/Http/Requests/Quote/StoreQuoteRequest.php +++ b/app/Http/Requests/Quote/StoreQuoteRequest.php @@ -12,6 +12,7 @@ namespace App\Http\Requests\Quote; use App\Http\Requests\Request; +use App\Http\ValidationRules\Quote\UniqueQuoteNumberRule; use App\Models\Quote; use App\Utils\Traits\CleanLineItems; use App\Utils\Traits\MakesHash; @@ -93,6 +94,8 @@ class StoreQuoteRequest extends Request $rules['documents'] = 'file|mimes:png,ai,svg,jpeg,tiff,pdf,gif,psd,txt,doc,xls,ppt,xlsx,docx,pptx|max:20000'; } + $rules['number'] = new UniqueQuoteNumberRule($this->all()); + return $rules; } diff --git a/app/Http/Requests/Quote/UniqueQuoteNumberRule.php b/app/Http/Requests/Quote/UniqueQuoteNumberRule.php new file mode 100644 index 0000000000..31821683da --- /dev/null +++ b/app/Http/Requests/Quote/UniqueQuoteNumberRule.php @@ -0,0 +1,70 @@ +input = $input; + } + + /** + * @param string $attribute + * @param mixed $value + * @return bool + */ + public function passes($attribute, $value) + { + return $this->checkIfQuoteNumberUnique(); //if it exists, return false! + } + + /** + * @return string + */ + public function message() + { + return "Quote number already taken"; + } + + /** + * @param $email + * + * //off,when_sent,when_paid + * + * @return bool + */ + private function checkIfQuoteNumberUnique() : bool + { + + $quote = Quote::where('client_id', $this->input['client_id']) + ->where('number', $this->input['number']) + ->withTrashed() + ->exists(); + + if($quote) + return false; + + return true; + } +} diff --git a/app/Http/Requests/Quote/UpdateQuoteRequest.php b/app/Http/Requests/Quote/UpdateQuoteRequest.php index db30733f32..a32a930f1e 100644 --- a/app/Http/Requests/Quote/UpdateQuoteRequest.php +++ b/app/Http/Requests/Quote/UpdateQuoteRequest.php @@ -49,6 +49,10 @@ class UpdateQuoteRequest extends Request } elseif ($this->input('documents')) { $rules['documents'] = 'file|mimes:png,ai,svg,jpeg,tiff,pdf,gif,psd,txt,doc,xls,ppt,xlsx,docx,pptx|max:20000'; } + + if($this->input('number')) + $rules['number'] = 'unique:quotes,number,' . $this->id . ',id,company_id,' . $this->invoice->company_id; + return $rules; }