2019-05-03 10:32:30 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2019-05-03 10:32:30 +02:00
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
use App\Events\Payment\PaymentWasCreated;
|
|
|
|
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
|
|
|
|
use App\Jobs\Invoice\UpdateInvoicePayment;
|
2019-12-01 12:23:24 +01:00
|
|
|
use App\Jobs\Invoice\ApplyInvoicePayment;
|
|
|
|
use App\Jobs\Invoice\ApplyClientPayment;
|
2019-11-18 11:46:01 +01:00
|
|
|
use App\Models\Invoice;
|
2019-05-03 10:32:30 +02:00
|
|
|
use App\Models\Payment;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PaymentRepository
|
|
|
|
*/
|
|
|
|
class PaymentRepository extends BaseRepository
|
|
|
|
{
|
|
|
|
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return Payment::class;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save(Request $request, Payment $payment) : ?Payment
|
|
|
|
{
|
2019-11-16 04:12:29 +01:00
|
|
|
|
2019-05-03 10:32:30 +02:00
|
|
|
$payment->fill($request->input());
|
2019-11-18 11:46:01 +01:00
|
|
|
|
|
|
|
$payment->save();
|
2019-05-03 10:32:30 +02:00
|
|
|
|
2019-11-16 04:12:29 +01:00
|
|
|
if($request->input('invoices'))
|
|
|
|
{
|
|
|
|
|
2019-12-01 12:23:24 +01:00
|
|
|
$invoices = Invoice::whereIn('id', array_column($request->input('invoices'),'id'))->company()->get();
|
2019-11-18 11:46:01 +01:00
|
|
|
|
|
|
|
$payment->invoices()->saveMany($invoices);
|
|
|
|
|
2019-12-01 12:23:24 +01:00
|
|
|
foreach($request->input('invoices') as $paid_invoice)
|
|
|
|
{
|
|
|
|
|
|
|
|
$invoice = Invoice::whereId($paid_invoice['id'])->company()->first();
|
|
|
|
|
|
|
|
if($invoice)
|
2019-12-27 01:28:36 +01:00
|
|
|
ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount'], $invoice->company);
|
2019-12-01 12:23:24 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//paid is made, but not to any invoice, therefore we are applying the payment to the clients credit
|
2019-12-27 01:28:36 +01:00
|
|
|
ApplyClientPayment::dispatchNow($payment, $payment->company);
|
2019-11-16 04:12:29 +01:00
|
|
|
}
|
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
event(new PaymentWasCreated($payment, $payment->company));
|
2019-11-18 11:46:01 +01:00
|
|
|
|
2019-12-01 12:23:24 +01:00
|
|
|
//UpdateInvoicePayment::dispatchNow($payment);
|
2019-11-18 11:46:01 +01:00
|
|
|
|
2019-12-01 12:23:24 +01:00
|
|
|
return $payment->fresh();
|
2019-11-18 11:46:01 +01:00
|
|
|
|
2019-05-03 10:32:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|