2019-10-03 14:02:31 +02:00
|
|
|
<?php
|
2020-07-01 03:06:40 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
2020-02-20 22:05:01 +01:00
|
|
|
namespace App\Services\Payment;
|
|
|
|
|
2020-09-03 13:37:02 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasUpdated;
|
2020-02-20 22:05:01 +01:00
|
|
|
use App\Helpers\Email\PaymentEmail;
|
|
|
|
use App\Jobs\Payment\EmailPayment;
|
2019-10-29 03:55:26 +01:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
2020-02-20 22:05:01 +01:00
|
|
|
use App\Models\Invoice;
|
2019-10-03 14:02:31 +02:00
|
|
|
use App\Models\SystemLog;
|
2020-09-03 13:37:02 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-08-31 04:00:43 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-10-03 14:02:31 +02:00
|
|
|
|
2020-02-20 22:05:01 +01:00
|
|
|
class UpdateInvoicePayment
|
|
|
|
{
|
2020-08-31 04:00:43 +02:00
|
|
|
use MakesHash;
|
|
|
|
|
2019-10-03 14:02:31 +02:00
|
|
|
public $payment;
|
2019-12-27 01:28:36 +01:00
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
public $payment_hash;
|
|
|
|
|
|
|
|
public function __construct($payment, $payment_hash)
|
2019-10-03 14:02:31 +02:00
|
|
|
{
|
|
|
|
$this->payment = $payment;
|
2020-08-31 04:00:43 +02:00
|
|
|
$this->payment_hash = $payment_hash;
|
2019-10-03 14:02:31 +02:00
|
|
|
}
|
|
|
|
|
2020-02-20 22:05:01 +01:00
|
|
|
public function run()
|
2019-10-03 14:02:31 +02:00
|
|
|
{
|
2020-08-31 04:00:43 +02:00
|
|
|
$paid_invoices = $this->payment_hash->invoices();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->get();
|
2020-08-31 04:00:43 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
collect($paid_invoices)->each(function ($paid_invoice) use ($invoices) {
|
|
|
|
$invoice = $invoices->first(function ($inv) use ($paid_invoice) {
|
2020-08-31 06:27:47 +02:00
|
|
|
return $paid_invoice->invoice_id == $inv->hashed_id;
|
2020-08-31 04:00:43 +02:00
|
|
|
});
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($invoice->id == $this->payment_hash->fee_invoice_id) {
|
2020-08-31 06:27:47 +02:00
|
|
|
$paid_amount = $paid_invoice->amount + $this->payment_hash->fee_total;
|
2020-09-06 11:38:10 +02:00
|
|
|
} else {
|
2020-08-31 06:27:47 +02:00
|
|
|
$paid_amount = $paid_invoice->amount;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-31 06:27:47 +02:00
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
$this->payment
|
|
|
|
->ledger()
|
2020-09-06 11:38:10 +02:00
|
|
|
->updatePaymentBalance($paid_amount * -1);
|
2020-08-31 04:00:43 +02:00
|
|
|
|
|
|
|
$this->payment
|
|
|
|
->client
|
|
|
|
->service()
|
2020-09-06 11:38:10 +02:00
|
|
|
->updateBalance($paid_amount * -1)
|
2020-08-31 06:27:47 +02:00
|
|
|
->updatePaidToDate($paid_amount)
|
2020-08-31 04:00:43 +02:00
|
|
|
->save();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$pivot_invoice = $this->payment->invoices->first(function ($inv) use ($paid_invoice) {
|
|
|
|
return $inv->hashed_id == $paid_invoice->invoice_id;
|
2020-09-01 01:28:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/*update paymentable record*/
|
|
|
|
$pivot_invoice->pivot->amount = $paid_amount;
|
|
|
|
$pivot_invoice->save();
|
2019-11-19 11:23:56 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invoice->service() //caution what if we amount paid was less than partial - we wipe it!
|
2020-09-01 01:28:37 +02:00
|
|
|
->clearPartial()
|
2020-09-06 11:38:10 +02:00
|
|
|
->updateBalance($paid_amount * -1)
|
2020-09-01 01:28:37 +02:00
|
|
|
->save();
|
2020-08-31 04:00:43 +02:00
|
|
|
|
2020-09-03 13:37:02 +02:00
|
|
|
event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars()));
|
2020-09-10 07:20:13 +02:00
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
});
|
|
|
|
|
2020-02-20 22:05:01 +01:00
|
|
|
return $this->payment;
|
2019-10-03 14:02:31 +02:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|