mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 07:34:35 -04:00
Allow invoice balances to change - but do no allow them to go negative
This commit is contained in:
parent
c90bac152d
commit
73bcaae3f6
@ -183,6 +183,16 @@ class InvoiceSum
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allow us to get the entity without persisting it
|
||||||
|
* @return Invoice the temp
|
||||||
|
*/
|
||||||
|
public function getTempEntity()
|
||||||
|
{
|
||||||
|
$this->setCalculatedAttributes();
|
||||||
|
return $this->invoice;
|
||||||
|
}
|
||||||
|
|
||||||
public function getInvoice()
|
public function getInvoice()
|
||||||
{
|
{
|
||||||
//Build invoice values here and return Invoice
|
//Build invoice values here and return Invoice
|
||||||
|
@ -196,6 +196,12 @@ class InvoiceSumInclusive
|
|||||||
return $this->invoice;
|
return $this->invoice;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getTempEntity()
|
||||||
|
{
|
||||||
|
$this->setCalculatedAttributes();
|
||||||
|
return $this->invoice;
|
||||||
|
}
|
||||||
|
|
||||||
public function getInvoice()
|
public function getInvoice()
|
||||||
{
|
{
|
||||||
//Build invoice values here and return Invoice
|
//Build invoice values here and return Invoice
|
||||||
|
@ -12,7 +12,9 @@
|
|||||||
namespace App\Http\Requests\Invoice;
|
namespace App\Http\Requests\Invoice;
|
||||||
|
|
||||||
use App\Http\Requests\Request;
|
use App\Http\Requests\Request;
|
||||||
|
use App\Http\ValidationRules\Invoice\InvoiceBalanceSanity;
|
||||||
use App\Http\ValidationRules\Invoice\LockedInvoiceRule;
|
use App\Http\ValidationRules\Invoice\LockedInvoiceRule;
|
||||||
|
use App\Models\Invoice;
|
||||||
use App\Utils\Traits\ChecksEntityStatus;
|
use App\Utils\Traits\ChecksEntityStatus;
|
||||||
use App\Utils\Traits\CleanLineItems;
|
use App\Utils\Traits\CleanLineItems;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
@ -55,6 +57,9 @@ class UpdateInvoiceRequest extends Request
|
|||||||
|
|
||||||
$rules['line_items'] = 'array';
|
$rules['line_items'] = 'array';
|
||||||
|
|
||||||
|
if($this->input('status_id') != Invoice::STATUS_DRAFT)
|
||||||
|
$rules['balance'] = new InvoiceBalanceSanity($this->invoice, $this->all());
|
||||||
|
|
||||||
return $rules;
|
return $rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
77
app/Http/ValidationRules/Invoice/InvoiceBalanceSanity.php
Normal file
77
app/Http/ValidationRules/Invoice/InvoiceBalanceSanity.php
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://opensource.org/licenses/AAL
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Http\ValidationRules\Invoice;
|
||||||
|
|
||||||
|
use App\Models\Invoice;
|
||||||
|
use Illuminate\Contracts\Validation\Rule;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class LockedInvoiceRule.
|
||||||
|
*/
|
||||||
|
class InvoiceBalanceSanity implements Rule
|
||||||
|
{
|
||||||
|
public $invoice;
|
||||||
|
|
||||||
|
public $input;
|
||||||
|
|
||||||
|
private $message;
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct(Invoice $invoice, $input)
|
||||||
|
{
|
||||||
|
$this->invoice = $invoice;
|
||||||
|
$this->input = $input;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $attribute
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function passes($attribute, $value)
|
||||||
|
{
|
||||||
|
return $this->checkIfInvoiceBalanceIsSane(); //if it exists, return false!
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function message()
|
||||||
|
{
|
||||||
|
return $this->message;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function checkIfInvoiceBalanceIsSane() : bool
|
||||||
|
{
|
||||||
|
|
||||||
|
$this->invoice->line_items = $this->input['line_items'];
|
||||||
|
|
||||||
|
DB::beginTransaction();
|
||||||
|
|
||||||
|
$temp_invoice = $this->invoice->calc()->getTempEntity();
|
||||||
|
|
||||||
|
DB::rollBack();
|
||||||
|
|
||||||
|
if($temp_invoice->balance < 0){
|
||||||
|
$this->message = 'Invoice balance cannot go negative';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -302,8 +302,14 @@ class BaseRepository
|
|||||||
/* Perform model specific tasks */
|
/* Perform model specific tasks */
|
||||||
if ($model instanceof Invoice) {
|
if ($model instanceof Invoice) {
|
||||||
|
|
||||||
|
nlog("in base");
|
||||||
|
nlog($state['finished_amount']);
|
||||||
|
nlog($state['starting_amount']);
|
||||||
|
nlog($model->status_id);
|
||||||
|
|
||||||
if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Invoice::STATUS_DRAFT)) {
|
if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Invoice::STATUS_DRAFT)) {
|
||||||
|
|
||||||
|
$model->service()->updateStatus()->save();
|
||||||
$model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']), "Update adjustment for invoice {$model->number}");
|
$model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']), "Update adjustment for invoice {$model->number}");
|
||||||
$model->client->service()->updateBalance(($state['finished_amount'] - $state['starting_amount']))->save();
|
$model->client->service()->updateBalance(($state['finished_amount'] - $state['starting_amount']))->save();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user