2019-10-03 14:02:31 +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-18 11:46:01 +01:00
|
|
|
use App\Jobs\Client\UpdateClientBalance;
|
2019-11-19 22:06:48 +01:00
|
|
|
use App\Jobs\Client\UpdateClientPaidToDate;
|
2019-10-03 14:02:31 +02:00
|
|
|
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
|
|
|
|
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
|
2019-10-29 03:55:26 +01:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
2019-12-27 01:28:36 +01:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Company;
|
2019-10-03 14:02:31 +02:00
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\SystemLog;
|
|
|
|
use App\Utils\Traits\SystemLogTrait;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
|
|
class UpdateInvoicePayment implements ShouldQueue
|
|
|
|
{
|
|
|
|
use SystemLogTrait, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public $payment;
|
2019-12-27 01:28:36 +01:00
|
|
|
|
|
|
|
private $company;
|
|
|
|
|
2019-10-03 14:02:31 +02:00
|
|
|
/**
|
|
|
|
* Create the event listener.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-12-27 01:28:36 +01:00
|
|
|
public function __construct(Payment $payment, Company $company)
|
2019-10-03 14:02:31 +02:00
|
|
|
{
|
|
|
|
$this->payment = $payment;
|
2019-12-27 01:28:36 +01:00
|
|
|
$this->company = $company;
|
2019-10-03 14:02:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the event.
|
|
|
|
*
|
|
|
|
* @param object $event
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2019-12-27 01:28:36 +01:00
|
|
|
MultiDB::setDB($this->company->db);
|
2019-10-03 14:02:31 +02:00
|
|
|
|
2019-10-04 08:22:22 +02:00
|
|
|
$invoices = $this->payment->invoices()->get();
|
2019-10-03 14:02:31 +02:00
|
|
|
|
|
|
|
$invoices_total = $invoices->sum('balance');
|
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
/* Simplest scenario - All invoices are paid in full*/
|
2019-10-04 08:22:22 +02:00
|
|
|
if(strval($invoices_total) === strval($this->payment->amount))
|
2019-10-03 14:02:31 +02:00
|
|
|
{
|
|
|
|
$invoices->each(function ($invoice){
|
|
|
|
|
|
|
|
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1));
|
2019-11-19 22:06:48 +01:00
|
|
|
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1);
|
|
|
|
UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->balance);
|
2019-11-19 11:23:56 +01:00
|
|
|
|
|
|
|
$invoice->pivot->amount = $invoice->balance;
|
|
|
|
$invoice->pivot->save();
|
|
|
|
|
2019-10-03 14:02:31 +02:00
|
|
|
$invoice->clearPartial();
|
|
|
|
$invoice->updateBalance($invoice->balance*-1);
|
2019-11-18 11:46:01 +01:00
|
|
|
|
2019-10-03 14:02:31 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2019-11-18 11:46:01 +01:00
|
|
|
/*Combination of partials and full invoices are being paid*/
|
2019-10-03 14:02:31 +02:00
|
|
|
else {
|
|
|
|
|
|
|
|
$total = 0;
|
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
/* Calculate the grand total of the invoices*/
|
2019-10-04 08:22:22 +02:00
|
|
|
foreach($invoices as $invoice)
|
2019-10-03 14:02:31 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
if($invoice->hasPartial())
|
|
|
|
$total += $invoice->partial;
|
|
|
|
else
|
|
|
|
$total += $invoice->balance;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
/*Test if there is a batch of partial invoices that have been paid */
|
2019-10-03 14:02:31 +02:00
|
|
|
if($this->payment->amount == $total)
|
|
|
|
{
|
|
|
|
|
|
|
|
$invoices->each(function ($invoice){
|
|
|
|
|
|
|
|
if($invoice->hasPartial()) {
|
|
|
|
|
|
|
|
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->partial*-1));
|
2019-11-19 22:06:48 +01:00
|
|
|
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->partial*-1);
|
|
|
|
UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->partial);
|
2019-11-19 11:23:56 +01:00
|
|
|
$invoice->pivot->amount = $invoice->partial;
|
|
|
|
$invoice->pivot->save();
|
|
|
|
|
2019-10-03 14:02:31 +02:00
|
|
|
$invoice->updateBalance($invoice->partial*-1);
|
|
|
|
$invoice->clearPartial();
|
|
|
|
$invoice->setDueDate();
|
2019-10-11 04:20:04 +02:00
|
|
|
$invoice->setStatus(Invoice::STATUS_PARTIAL);
|
2019-11-18 11:46:01 +01:00
|
|
|
|
2019-11-19 22:06:48 +01:00
|
|
|
|
|
|
|
|
2019-10-03 14:02:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-19 22:06:48 +01:00
|
|
|
|
2019-10-03 14:02:31 +02:00
|
|
|
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1));
|
2019-11-19 22:06:48 +01:00
|
|
|
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1);
|
|
|
|
UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->balance);
|
2019-11-19 11:23:56 +01:00
|
|
|
|
|
|
|
$invoice->pivot->amount = $invoice->balance;
|
|
|
|
$invoice->pivot->save();
|
|
|
|
|
2019-10-03 14:02:31 +02:00
|
|
|
$invoice->clearPartial();
|
|
|
|
$invoice->updateBalance($invoice->balance*-1);
|
2019-11-18 11:46:01 +01:00
|
|
|
|
2019-11-19 22:06:48 +01:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
|
2019-10-03 14:02:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2019-10-29 03:55:26 +01:00
|
|
|
SystemLogger::dispatch(
|
|
|
|
[
|
|
|
|
'payment' => $this->payment,
|
|
|
|
'invoices' => $invoices,
|
|
|
|
'invoices_total' => $invoices_total,
|
|
|
|
'payment_amount' => $this->payment->amount,
|
2019-12-22 11:28:41 +01:00
|
|
|
'partial_check_amount' => $total,
|
|
|
|
],
|
2019-10-29 03:55:26 +01:00
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_PAYMENT_RECONCILIATION_FAILURE,
|
|
|
|
SystemLog::TYPE_LEDGER,
|
|
|
|
$this->payment->client
|
|
|
|
);
|
|
|
|
|
2019-11-19 22:06:48 +01:00
|
|
|
throw new \Exception("payment amount {$this->payment->amount} does not match invoice totals {$invoices_total} reversing payment");
|
|
|
|
|
|
|
|
$this->payment->invoice()->delete();
|
|
|
|
$this->payment->is_deleted=true;
|
|
|
|
$this->payment->save();
|
|
|
|
$this->payment->delete();
|
2019-10-03 14:02:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
$this->payment = $event->payment;
|
|
|
|
$invoice = $this->payment->invoice;
|
|
|
|
$adjustment = $this->payment->amount * -1;
|
|
|
|
$partial = max(0, $invoice->partial - $this->payment->amount);
|
|
|
|
|
|
|
|
$invoice->updateBalances($adjustment, $partial);
|
|
|
|
$invoice->updatePaidStatus(true);
|
|
|
|
|
|
|
|
// store a backup of the invoice
|
|
|
|
$activity = Activity::wherePaymentId($this->payment->id)
|
|
|
|
->whereActivityTypeId(ACTIVITY_TYPE_CREATE_PAYMENT)
|
|
|
|
->first();
|
|
|
|
$activity->json_backup = $invoice->hidePrivateFields()->toJSON();
|
|
|
|
$activity->save();
|
|
|
|
|
|
|
|
if ($invoice->balance == 0 && $this->payment->account->auto_archive_invoice) {
|
|
|
|
$invoiceRepo = app('App\Ninja\Repositories\InvoiceRepository');
|
|
|
|
$invoiceRepo->archive($invoice);
|
|
|
|
}
|
|
|
|
*/
|