1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Services/Payment/UpdateInvoicePayment.php

86 lines
2.4 KiB
PHP
Raw Normal View History

<?php
2020-07-01 03:06:40 +02:00
/**
* 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\Payment;
use App\Events\Invoice\InvoiceWasUpdated;
use App\Helpers\Email\PaymentEmail;
use App\Jobs\Payment\EmailPayment;
use App\Jobs\Util\SystemLogger;
use App\Models\Invoice;
use App\Models\SystemLog;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
class UpdateInvoicePayment
{
use MakesHash;
public $payment;
public $payment_hash;
public function __construct($payment, $payment_hash)
{
$this->payment = $payment;
$this->payment_hash = $payment_hash;
}
public function run()
{
$paid_invoices = $this->payment_hash->invoices();
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->get();
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 06:27:47 +02:00
if($invoice->id == $this->payment_hash->fee_invoice_id)
$paid_amount = $paid_invoice->amount + $this->payment_hash->fee_total;
else
$paid_amount = $paid_invoice->amount;
$this->payment
->ledger()
2020-08-31 06:27:47 +02:00
->updatePaymentBalance($paid_amount*-1);
$this->payment
->client
->service()
2020-08-31 06:27:47 +02:00
->updateBalance($paid_amount*-1)
->updatePaidToDate($paid_amount)
->save();
2020-09-01 01:28:37 +02:00
$pivot_invoice = $this->payment->invoices->first(function ($inv) use($paid_invoice){
return $inv->hashed_id == $paid_invoice->invoice_id;
});
/*update paymentable record*/
$pivot_invoice->pivot->amount = $paid_amount;
$pivot_invoice->save();
2020-09-01 01:28:37 +02:00
$invoice->service() //caution what if we amount paid was less than partial - we wipe it!
->clearPartial()
->updateBalance($paid_amount*-1)
->save();
event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars()));
});
return $this->payment;
}
}