1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 22:22:32 +01:00

Fixes for handling partial payments with credits

This commit is contained in:
David Bomba 2022-09-06 19:18:05 +10:00
parent 84e634c28c
commit a0d1635a58
3 changed files with 34 additions and 25 deletions

View File

@ -901,9 +901,10 @@ class BaseController extends Controller
return redirect('/')->with(['signup' => 'true']);
}
// 06-09-2022 - parse the path if loaded in a subdirectory for canvaskit resolution
$canvas_path_array = parse_url(config('ninja.app_url'));
$canvas_path = (array_key_exists('path', $canvas_path_array)) ? $canvas_path_array['path'] : '';
$canvas_path = rtrim(str_replace("index.php", "", $canvas_path),'/');
$data = [];

View File

@ -35,6 +35,9 @@ class AutoBillInvoice extends AbstractService
protected $db;
/*Specific variable for partial payments */
private bool $is_partial_amount = false;
public function __construct(Invoice $invoice, $db)
{
$this->invoice = $invoice;
@ -46,7 +49,8 @@ class AutoBillInvoice extends AbstractService
{
MultiDB::setDb($this->db);
$this->client = $this->invoice->client->fresh();
/* Harvest Client*/
$this->client = $this->invoice->client;
$is_partial = false;
@ -68,6 +72,10 @@ class AutoBillInvoice extends AbstractService
$this->applyCreditPayment();
}
//If this returns true, it means a partial invoice amount was paid as a credit and there is no further balance payable
if($this->is_partial_amount && $this->invoice->partial == 0)
return;
$amount = 0;
/* Determine $amount */
@ -169,9 +177,9 @@ class AutoBillInvoice extends AbstractService
$payment->invoices()->attach($this->invoice->id, ['amount' => $amount]);
$this->invoice
->service()
->setStatus(Invoice::STATUS_PAID)
->save();
->service()
->setCalculatedStatus()
->save();
foreach ($this->used_credit as $credit) {
$current_credit = Credit::find($credit['credit_id']);
@ -191,18 +199,18 @@ class AutoBillInvoice extends AbstractService
->updatePaymentBalance($amount * -1)
->save();
$client = $this->invoice->client->fresh();
$client->service()
->updateBalance($amount * -1)
->updatePaidToDate($amount)
->adjustCreditBalance($amount * -1)
->save();
$this->invoice
->client
->service()
->updateBalanceAndPaidToDate($amount * -1, $amount)
// ->updateBalance($amount * -1)
// ->updatePaidToDate($amount)
->adjustCreditBalance($amount * -1)
->save();
$this->invoice->ledger() //09-03-2022
// ->updateInvoiceBalance($amount * -1, "Invoice {$this->invoice->number} payment using Credit {$current_credit->number}")
->updateCreditBalance($amount * -1, "Credit {$current_credit->number} used to pay down Invoice {$this->invoice->number}")
->save();
->updateCreditBalance($amount * -1, "Credit {$current_credit->number} used to pay down Invoice {$this->invoice->number}")
->save();
event('eloquent.created: App\Models\Payment', $payment);
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
@ -235,16 +243,14 @@ class AutoBillInvoice extends AbstractService
return;
}
$is_partial_amount = false;
if ($this->invoice->partial > 0) {
$is_partial_amount = true;
$this->is_partial_amount = true;
}
$this->used_credit = [];
foreach ($available_credits as $key => $credit) {
if ($is_partial_amount) {
if ($this->is_partial_amount) {
//more credit than needed
if ($credit->balance > $this->invoice->partial) {

View File

@ -62,15 +62,17 @@ class UpdateInvoicePayment
$paid_amount = $paid_invoice->amount;
}
\DB::connection(config('database.default'))->transaction(function () use($client, $paid_amount){
$client->service()->updateBalanceAndPaidToDate($paid_amount*-1, $paid_amount);
// \DB::connection(config('database.default'))->transaction(function () use($client, $paid_amount){
$update_client = Client::withTrashed()->where('id', $client->id)->lockForUpdate()->first();
// $update_client = Client::withTrashed()->where('id', $client->id)->lockForUpdate()->first();
$update_client->paid_to_date += $paid_amount;
$update_client->balance -= $paid_amount;
$update_client->save();
// $update_client->paid_to_date += $paid_amount;
// $update_client->balance -= $paid_amount;
// $update_client->save();
}, 1);
// }, 1);
/* Need to determine here is we have an OVER payment - if YES only apply the max invoice amount */
if($paid_amount > $invoice->partial && $paid_amount > $invoice->balance)