2019-05-13 08:18:46 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Invoice;
|
|
|
|
|
2019-11-20 06:41:49 +01:00
|
|
|
use App\Events\Invoice\InvoiceWasPaid;
|
2019-11-08 01:38:22 +01:00
|
|
|
use App\Jobs\Invoice\ApplyInvoiceNumber;
|
2019-05-13 08:18:46 +02:00
|
|
|
use App\Models\Invoice;
|
2019-05-14 06:05:05 +02:00
|
|
|
use App\Models\Payment;
|
2019-08-27 03:21:25 +02:00
|
|
|
use App\Models\PaymentTerm;
|
2019-05-13 08:18:46 +02:00
|
|
|
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;
|
2019-05-14 06:05:05 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2019-05-13 08:18:46 +02:00
|
|
|
|
|
|
|
class ApplyPaymentToInvoice implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter;
|
|
|
|
|
|
|
|
public $invoice;
|
|
|
|
|
|
|
|
public $payment;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(Payment $payment, Invoice $invoice)
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
|
|
|
|
$this->payment = $payment;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
|
2019-05-14 12:27:47 +02:00
|
|
|
/* The amount we are adjusting the invoice by*/
|
2019-05-13 08:18:46 +02:00
|
|
|
$adjustment = $this->payment->amount * -1;
|
|
|
|
|
2019-05-14 06:05:05 +02:00
|
|
|
/* 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
|
|
|
|
*/
|
2019-05-13 08:18:46 +02:00
|
|
|
$partial = max(0, $this->invoice->partial - $this->payment->amount);
|
|
|
|
|
2019-05-14 06:05:05 +02:00
|
|
|
/* check if partial exists */
|
2019-05-13 08:18:46 +02:00
|
|
|
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;
|
2019-05-14 06:05:05 +02:00
|
|
|
|
|
|
|
$this->invoice->partial_due_date = null;
|
|
|
|
|
2019-05-13 08:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-14 06:05:05 +02:00
|
|
|
if(!$this->invoice->due_date)
|
2019-10-01 06:27:04 +02:00
|
|
|
$this->invoice->due_date = Carbon::now()->addDays(PaymentTerm::find($this->invoice->settings->payment_terms)->num_days);
|
2019-05-14 06:05:05 +02:00
|
|
|
|
2019-05-13 08:18:46 +02:00
|
|
|
}
|
|
|
|
|
2019-05-14 06:05:05 +02:00
|
|
|
/* Update Invoice Balance */
|
|
|
|
$this->invoice->balance = $this->invoice->balance + $adjustment;
|
2019-05-13 08:18:46 +02:00
|
|
|
|
2019-05-14 06:05:05 +02:00
|
|
|
/* Update Invoice Status */
|
2019-11-20 06:41:49 +01:00
|
|
|
if($this->invoice->balance == 0){
|
2019-05-14 06:05:05 +02:00
|
|
|
$this->invoice->status_id = Invoice::STATUS_PAID;
|
2019-11-20 06:41:49 +01:00
|
|
|
$this->invoice->save();
|
|
|
|
event(new InvoiceWasPaid($this->invoice));
|
|
|
|
}
|
2019-05-14 06:05:05 +02:00
|
|
|
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);
|
|
|
|
}
|
2019-05-13 08:18:46 +02:00
|
|
|
|
2019-05-14 06:05:05 +02:00
|
|
|
$this->invoice->save();
|
2019-05-13 08:18:46 +02:00
|
|
|
|
2019-11-08 01:38:22 +01:00
|
|
|
$this->invoice = ApplyInvoiceNumber::dispatchNow($this->invoice, $invoice->client->getMergedSettings());
|
|
|
|
|
2019-05-16 01:40:53 +02:00
|
|
|
return $this->invoice;
|
2019-05-13 08:18:46 +02:00
|
|
|
}
|
|
|
|
}
|