diff --git a/app/Http/ValidationRules/Recurring/UniqueRecurringInvoiceNumberRule.php b/app/Http/ValidationRules/Recurring/UniqueRecurringInvoiceNumberRule.php new file mode 100644 index 000000000000..2103424b6e14 --- /dev/null +++ b/app/Http/ValidationRules/Recurring/UniqueRecurringInvoiceNumberRule.php @@ -0,0 +1,73 @@ +input = $input; + } + + /** + * @param string $attribute + * @param mixed $value + * @return bool + */ + public function passes($attribute, $value) + { + return $this->checkIfInvoiceNumberUnique(); //if it exists, return false! + } + + /** + * @return string + */ + public function message() + { + return "Recurring Invoice number {$this->input['number']} already taken"; + } + + /** + * @param $email + * + * //off,when_sent,when_paid + * + * @return bool + */ + private function checkIfInvoiceNumberUnique() : bool + { + if(empty($this->input['number'])) + return true; + + $invoice = RecurringInvoice::where('client_id', $this->input['client_id']) + ->where('number', $this->input['number']) + ->withTrashed() + ->exists(); + + if ($invoice) { + return false; + } + + return true; + } +}