2020-07-22 01:54:39 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-22 01:54:39 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-07-22 01:54:39 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-07-22 01:54:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\ValidationRules\Invoice;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class LockedInvoiceRule.
|
2020-07-22 01:54:39 +02:00
|
|
|
*/
|
|
|
|
class LockedInvoiceRule implements Rule
|
|
|
|
{
|
|
|
|
public $invoice;
|
|
|
|
|
|
|
|
public function __construct(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $attribute
|
|
|
|
* @param mixed $value
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function passes($attribute, $value)
|
|
|
|
{
|
|
|
|
return $this->checkIfInvoiceLocked(); //if it exists, return false!
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function message()
|
|
|
|
{
|
|
|
|
return ctrans('texts.locked_invoice');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
private function checkIfInvoiceLocked(): bool
|
2020-07-22 01:54:39 +02:00
|
|
|
{
|
|
|
|
$lock_invoices = $this->invoice->client->getSetting('lock_invoices');
|
|
|
|
|
|
|
|
switch ($lock_invoices) {
|
|
|
|
case 'off':
|
|
|
|
return true;
|
|
|
|
case 'when_sent':
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->invoice->status_id == Invoice::STATUS_SENT) {
|
2020-07-22 01:54:39 +02:00
|
|
|
return false;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-07-22 01:54:39 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 'when_paid':
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->invoice->status_id == Invoice::STATUS_PAID) {
|
2020-07-22 01:54:39 +02:00
|
|
|
return false;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-07-22 01:54:39 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|