1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Jobs/Invoice/ApplyPaymentToInvoice.php
David Bomba cda534e996
Explicitly call the service() method, rather than obfuscate. (#3281)
* Include fix as describe by @michael-hampton here #3280

* Refactor createinvitations away from jobs

* Clean up

* Fixes for service() refactoring

* Fixes for services refactor
2020-02-04 18:51:44 +11:00

121 lines
3.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\Jobs\Invoice;
use App\Events\Invoice\InvoiceWasPaid;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\PaymentTerm;
use App\Repositories\InvoiceRepository;
use App\Utils\Traits\NumberFormatter;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
class ApplyPaymentToInvoice implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter;
public $invoice;
public $payment;
private $company;
/**
* @deprecated confirm to be deleted
* Create a new job instance.
*
* @return void
*/
public function __construct(Payment $payment, Invoice $invoice, Company $company)
{
$this->invoice = $invoice;
$this->payment = $payment;
$this->company = $company;
}
/**
* Execute the job.
*
*
* @return void
*/
public function handle()
{
MultiDB::setDB($this->company->db);
/* The amount we are adjusting the invoice by*/
$adjustment = $this->payment->amount * -1;
/* Calculate if the amount paid is less than the partial value.
* Needed if there is a condition under which a value LESS
* than the partial amount has been paid. The Invoice will
* be updated to reflect the NEW partial amount
*/
$partial = max(0, $this->invoice->partial - $this->payment->amount);
/* check if partial exists */
if ($this->invoice->partial > 0) {
//if payment amount = partial
if ($this->formatvalue($this->invoice->partial, 4) == $this->formatValue($this->payment->amount, 4)) {
$this->invoice->partial = 0;
$this->invoice->partial_due_date = null;
}
//if payment amount < partial amount
if ($this->formatvalue($this->invoice->partial, 4) > $this->formatValue($this->payment->amount, 4)) {
//set the new partial amount to the balance
$this->invoice->partial = $partial;
}
if (!$this->invoice->due_date) {
$this->invoice->due_date = Carbon::now()->addDays(PaymentTerm::find($this->invoice->settings->payment_terms)->num_days);
}
}
/* Update Invoice Balance */
$this->invoice->balance = $this->invoice->balance + $adjustment;
/* Update Invoice Status */
if ($this->invoice->balance == 0) {
$this->invoice->status_id = Invoice::STATUS_PAID;
$this->invoice->save();
event(new InvoiceWasPaid($this->invoice, $this->invoice->company));
} elseif ($this->payment->amount > 0 && $this->invoice->balance > 0) {
$this->invoice->status_id = Invoice::STATUS_PARTIAL;
}
/*If auto-archive is enabled, and balance = 0 - archive invoice */
if ($this->invoice->settings->auto_archive_invoice && $this->invoice->balance == 0) {
$invoiceRepo = app('App\Repositories\InvoiceRepository');
$invoiceRepo->archive($this->invoice);
}
$this->invoice->save();
$this->invoice = $this->invoice->service()->applyNumber()->save();
return $this->invoice;
}
}