invoiceninja/app/Services/Invoice/ApplyPayment.php
David Bomba f57339f185
Fixes and Refactors for Invoice Emails. (#3339)
* Working on emailing invoices

* Working on emailing and displaying email

* Working on emailing and displaying email

* Email invoices

* Fixes for html emails

* Ensure valid client prior to store

* Ensure client exists when storing an entity

* Update variable name send -> send_email for client_contacts

* Mailable download files

* Extend timeouts of password protected routes when a protected route is hit

* Add default portal design to company settings

* Minor fixes

* Fixes for Tests

* Fixes for invoicing emails

* Refactors for InvoiceEmail

* Implement abstractservice

* Refactors for services

* Refactors for emails

* Fixes for Invoice Emails
2020-02-17 20:37:44 +11:00

68 lines
2.7 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\AbstractService;
use App\Services\Client\ClientService;
class ApplyPayment extends AbstractService
{
private $invoice;
private $payment;
private $payment_amount;
public function __construct($invoice, $payment, $payment_amount)
{
$this->invoice = $invoice;
$this->payment = $payment;
$this->payment_amount = $payment_amount;
}
public function run()
{
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($this->payment_amount*-1), $this->payment->company);
$this->payment->client->service()->updateBalance($this->payment_amount*-1)->save();
/* Update Pivot Record amount */
$this->payment->invoices->each(function ($inv){
if ($inv->id == $this->invoice->id) {
$inv->pivot->amount = $this->payment_amount;
$inv->pivot->save();
}
});
if ($this->invoice->hasPartial()) {
//is partial and amount is exactly the partial amount
if ($this->invoice->partial == $this->payment_amount) {
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($this->payment_amount*-1);
} elseif ($this->invoice->partial > 0 && $this->invoice->partial > $this->payment_amount) { //partial amount exists, but the amount is less than the partial amount
$this->invoice->service()->updatePartial($this->payment_amount*-1)->updateBalance($this->payment_amount*-1);
} elseif ($this->invoice->partial > 0 && $this->invoice->partial < $this->payment_amount) { //partial exists and the amount paid is GREATER than the partial amount
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($this->payment_amount*-1);
}
} elseif ($this->payment_amount == $this->invoice->balance) { //total invoice paid.
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($this->payment_amount*-1);
} elseif($this->payment_amount < $this->invoice->balance) { //partial invoice payment made
$this->invoice->service()->clearPartial()->updateBalance($this->payment_amount*-1);
}
return $this->invoice;
}
}