1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 03:02:34 +01:00
invoiceninja/app/Services/Invoice/ApplyPayment.php
David Bomba 3ec996ee5d
Invoice Designer (#3283)
* Working self-updater package

* Fixes for travis

* Working on invoice designs

* Working on invoice builder

* Tests for invoice design

* Working on invoice designs

* Minor fixes

* Minor fixes for randomdataseeder
2020-02-05 15:06:03 +11:00

61 lines
2.4 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\Invoice;
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
use App\Models\Invoice;
use App\Models\Payment;
use App\Services\Client\ClientService;
class ApplyPayment
{
private $invoice;
public function __construct($invoice)
{
$this->invoice = $invoice;
}
public function __invoke($payment, $payment_amount)
{
UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment_amount*-1), $payment->company);
$payment->client->service()->updateBalance($payment_amount*-1)->save();
/* Update Pivot Record amount */
$payment->invoices->each(function ($inv) use($payment_amount){
if ($inv->id == $this->invoice->id) {
$inv->pivot->amount = $payment_amount;
$inv->pivot->save();
}
});
if ($this->invoice->hasPartial()) {
//is partial and amount is exactly the partial amount
if ($this->invoice->partial == $payment_amount) {
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($payment_amount*-1);
} elseif ($this->invoice->partial > 0 && $this->invoice->partial > $payment_amount) { //partial amount exists, but the amount is less than the partial amount
$this->invoice->service()->updatePartial($payment_amount*-1)->updateBalance($payment_amount*-1);
} elseif ($this->invoice->partial > 0 && $this->invoice->partial < $payment_amount) { //partial exists and the amount paid is GREATER than the partial amount
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($payment_amount*-1);
}
} elseif ($payment_amount == $this->invoice->balance) { //total invoice paid.
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($payment_amount*-1);
} elseif($payment_amount < $this->invoice->balance) { //partial invoice payment made
$this->invoice->service()->clearPartial()->updateBalance($payment_amount*-1);
}
return $this->invoice;
}
}