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
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @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;
|
2020-01-07 01:13:47 +01:00
|
|
|
use App\Factory\CreditFactory;
|
2019-11-18 11:46:01 +01:00
|
|
|
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
|
2019-12-01 12:23:24 +01:00
|
|
|
use App\Jobs\Invoice\ApplyClientPayment;
|
2020-01-03 10:34:10 +01:00
|
|
|
use App\Jobs\Invoice\ApplyInvoicePayment;
|
|
|
|
use App\Jobs\Invoice\UpdateInvoicePayment;
|
2019-11-18 11:46:01 +01:00
|
|
|
use App\Models\Invoice;
|
2019-05-03 10:32:30 +02:00
|
|
|
use App\Models\Payment;
|
2020-01-03 10:34:10 +01:00
|
|
|
use App\Repositories\CreditRepository;
|
2019-05-03 10:32:30 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PaymentRepository
|
|
|
|
*/
|
|
|
|
class PaymentRepository extends BaseRepository
|
|
|
|
{
|
2020-01-03 10:34:10 +01:00
|
|
|
protected $credit_repo;
|
|
|
|
|
|
|
|
public function __construct(CreditRepository $credit_repo)
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->credit_repo = $credit_repo;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-03 10:32:30 +02:00
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return Payment::class;
|
|
|
|
}
|
|
|
|
|
2019-12-29 07:28:57 +01:00
|
|
|
/**
|
2020-01-03 09:49:59 +01:00
|
|
|
* Saves a payment.
|
|
|
|
*
|
|
|
|
*
|
2019-12-30 22:59:12 +01:00
|
|
|
* @param Request $request the request object
|
|
|
|
* @param Payment $payment The Payment object
|
|
|
|
* @return Object Payment $payment
|
2019-12-29 07:28:57 +01:00
|
|
|
*/
|
2019-12-30 22:59:12 +01:00
|
|
|
public function save(Request $request, Payment $payment) : ?Payment
|
|
|
|
{
|
2019-12-29 07:28:57 +01:00
|
|
|
//todo this save() only works for new payments... will fail if a Payment is updated and saved through here.
|
2019-05-03 10:32:30 +02:00
|
|
|
$payment->fill($request->input());
|
2019-11-18 11:46:01 +01:00
|
|
|
|
2020-01-03 09:49:59 +01:00
|
|
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
2020-01-04 03:27:51 +01:00
|
|
|
$payment->number = $payment->client->getNextPaymentNumber($payment->client);
|
2020-01-07 01:13:47 +01:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$payment->save();
|
2019-05-03 10:32:30 +02:00
|
|
|
|
2020-01-03 10:34:10 +01:00
|
|
|
if ($request->input('invoices')) {
|
2020-01-07 01:13:47 +01:00
|
|
|
|
2019-12-30 22:59:12 +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-30 22:59:12 +01:00
|
|
|
foreach ($request->input('invoices') as $paid_invoice) {
|
2019-12-01 12:23:24 +01:00
|
|
|
$invoice = Invoice::whereId($paid_invoice['id'])->company()->first();
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($invoice) {
|
2019-12-27 01:28:36 +01:00
|
|
|
ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount'], $invoice->company);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-01 12:23:24 +01:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2020-01-03 09:49:59 +01:00
|
|
|
//payment 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-12-30 22:59:12 +01:00
|
|
|
}
|
2020-01-03 09:49:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates
|
|
|
|
*
|
|
|
|
* The update code path is different to the save path
|
|
|
|
* additional considerations come into play when 'updating'
|
|
|
|
* a payment.
|
|
|
|
*
|
|
|
|
* @param Request $request the request object
|
|
|
|
* @param Payment $payment The Payment object
|
|
|
|
* @return Object Payment $payment
|
|
|
|
*/
|
|
|
|
public function update(Request $request, Payment $payment) :?Payment
|
|
|
|
{
|
|
|
|
|
|
|
|
if($payment->amount >= 0)
|
|
|
|
return $this->applyPayment($request, $payment);
|
|
|
|
|
|
|
|
return $this->refundPayment($request, $payment);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function applyPayment(Request $request, Payment $payment) :?Payment
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function refundPayment(Request $request, Payment $payment) :?Payment
|
|
|
|
{
|
|
|
|
//temp variable to sum the total refund/credit amount
|
|
|
|
$invoice_total_adjustment = 0;
|
|
|
|
|
2020-01-07 01:13:47 +01:00
|
|
|
if($request->has('invoices') && is_array($request->input('invoices'))){
|
2020-01-03 09:49:59 +01:00
|
|
|
|
|
|
|
foreach($request->input('invoices') as $adjusted_invoice) {
|
2020-01-03 10:34:10 +01:00
|
|
|
|
2020-01-03 09:49:59 +01:00
|
|
|
$invoice = Invoice::whereId($adjusted_invoice['id'])->company()->first();
|
2020-01-07 01:13:47 +01:00
|
|
|
|
2020-01-03 09:49:59 +01:00
|
|
|
$invoice_total_adjustment += $adjusted_invoice['amount'];
|
|
|
|
|
2020-01-03 10:34:10 +01:00
|
|
|
if(array_key_exists('credits', $adjusted_invoice)){
|
|
|
|
|
|
|
|
//process and insert credit notes
|
|
|
|
foreach($adjusted_invoice['credits'] as $credit){
|
|
|
|
|
2020-01-07 01:13:47 +01:00
|
|
|
$credit = $this->credit_repo->save($credit, CreditFactory::create(auth()->user()->company()->id, auth()->user()->id), $invoice);
|
2020-01-03 10:34:10 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//todo - generate Credit Note for $amount on $invoice - the assumption here is that it is a FULL refund
|
2020-01-03 09:49:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if($request->input('amount') != $invoice_total_adjustment)
|
|
|
|
return 'Amount must equal the sum of invoice adjustments';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//adjust applied amount
|
|
|
|
$payment->applied += $invoice_total_adjustment;
|
|
|
|
|
|
|
|
//adjust clients paid to date
|
|
|
|
$client = $payment->client;
|
|
|
|
$client->paid_to_date += $invoice_total_adjustment;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|