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;
|
2020-02-01 21:45:23 +01:00
|
|
|
use App\Jobs\Credit\ApplyCreditPayment;
|
2020-04-19 12:29:58 +02:00
|
|
|
use App\Libraries\Currency\Conversion\CurrencyApi;
|
2020-03-09 10:38:15 +01:00
|
|
|
use App\Models\Client;
|
2020-01-07 10:35:55 +01:00
|
|
|
use App\Models\Credit;
|
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;
|
2020-04-08 12:48:31 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-05-03 10:32:30 +02:00
|
|
|
use Illuminate\Http\Request;
|
2020-04-19 12:29:58 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2019-05-03 10:32:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PaymentRepository
|
|
|
|
*/
|
|
|
|
class PaymentRepository extends BaseRepository
|
|
|
|
{
|
2020-04-08 12:48:31 +02:00
|
|
|
use MakesHash;
|
|
|
|
|
2020-01-03 10:34:10 +01:00
|
|
|
protected $credit_repo;
|
|
|
|
|
|
|
|
public function __construct(CreditRepository $credit_repo)
|
|
|
|
{
|
|
|
|
$this->credit_repo = $credit_repo;
|
2020-01-17 22:10:38 +01:00
|
|
|
}
|
2020-01-03 10:34:10 +01:00
|
|
|
|
2019-05-03 10:32:30 +02:00
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return Payment::class;
|
|
|
|
}
|
2020-01-17 22:10:38 +01:00
|
|
|
|
2019-12-29 07:28:57 +01:00
|
|
|
/**
|
2020-01-09 21:15:10 +01:00
|
|
|
* Saves and updates a payment. //todo refactor to handle refunds and payments.
|
2020-01-17 22:10:38 +01:00
|
|
|
*
|
|
|
|
* @param array $data the request object
|
|
|
|
* @param Payment $payment The Payment object
|
|
|
|
* @return Payment|null Payment $payment
|
2019-12-29 07:28:57 +01:00
|
|
|
*/
|
2020-01-17 22:10:38 +01:00
|
|
|
public function save(array $data, Payment $payment): ?Payment
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($payment->amount >= 0) {
|
2020-01-17 22:10:38 +01:00
|
|
|
return $this->applyPayment($data, $payment);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-17 22:10:38 +01:00
|
|
|
|
|
|
|
return $this->refundPayment($data, $payment);
|
2020-01-11 23:01:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a positive payment request
|
2020-06-05 05:52:53 +02:00
|
|
|
* @param array $data The data object
|
|
|
|
* @param Payment $payment The $payment entity
|
2020-01-11 23:01:28 +01:00
|
|
|
* @return Payment The updated/created payment object
|
|
|
|
*/
|
2020-01-17 22:10:38 +01:00
|
|
|
private function applyPayment(array $data, Payment $payment): ?Payment
|
2020-01-11 23:01:28 +01:00
|
|
|
{
|
2020-03-09 10:38:15 +01:00
|
|
|
//check currencies here and fill the exchange rate data if necessary
|
2020-03-21 06:37:30 +01:00
|
|
|
if (!$payment->id) {
|
2020-03-09 10:38:15 +01:00
|
|
|
$this->processExchangeRates($data, $payment);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-17 22:10:38 +01:00
|
|
|
|
2020-06-05 05:52:53 +02:00
|
|
|
/*Fill the payment*/
|
2020-01-17 22:10:38 +01:00
|
|
|
$payment->fill($data);
|
2020-01-03 09:49:59 +01:00
|
|
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
2019-11-18 11:46:01 +01:00
|
|
|
$payment->save();
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-06-05 05:52:53 +02:00
|
|
|
/*Ensure payment number generated*/
|
2020-03-21 06:37:30 +01:00
|
|
|
if (!$payment->number || strlen($payment->number) == 0) {
|
2020-01-09 21:15:10 +01:00
|
|
|
$payment->number = $payment->client->getNextPaymentNumber($payment->client);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-17 22:10:38 +01:00
|
|
|
|
2020-01-09 21:15:10 +01:00
|
|
|
$invoice_totals = 0;
|
|
|
|
$credit_totals = 0;
|
|
|
|
|
2020-06-05 05:52:53 +02:00
|
|
|
/*Iterate through invoices and apply payments*/
|
2020-01-17 22:10:38 +01:00
|
|
|
if (array_key_exists('invoices', $data) && is_array($data['invoices'])) {
|
|
|
|
$invoice_totals = array_sum(array_column($data['invoices'], 'amount'));
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-01-23 21:35:00 +01:00
|
|
|
$invoices = Invoice::whereIn('id', array_column($data['invoices'], 'invoice_id'))->get();
|
2019-11-18 11:46:01 +01:00
|
|
|
|
|
|
|
$payment->invoices()->saveMany($invoices);
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-01-17 22:10:38 +01:00
|
|
|
foreach ($data['invoices'] as $paid_invoice) {
|
2020-01-23 21:35:00 +01:00
|
|
|
$invoice = Invoice::whereId($paid_invoice['invoice_id'])->first();
|
2019-12-01 12:23:24 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($invoice) {
|
2020-02-04 08:51:44 +01:00
|
|
|
$invoice->service()->applyPayment($payment, $paid_invoice['amount'])->save();
|
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-06-05 05:52:53 +02:00
|
|
|
//payment is made, but not to any invoice, therefore we are applying the payment to the clients paid_to_date only
|
|
|
|
$payment->client->service()->updatePaidToDate($payment->amount)->save();
|
2019-11-16 04:12:29 +01:00
|
|
|
}
|
|
|
|
|
2020-01-17 22:10:38 +01:00
|
|
|
if (array_key_exists('credits', $data) && is_array($data['credits'])) {
|
|
|
|
$credit_totals = array_sum(array_column($data['credits'], 'amount'));
|
|
|
|
|
2020-04-08 12:48:31 +02:00
|
|
|
$credits = Credit::whereIn('id', $this->transformKeys(array_column($data['credits'], 'credit_id')))->get();
|
2020-01-07 10:35:55 +01:00
|
|
|
$payment->credits()->saveMany($credits);
|
|
|
|
|
2020-01-17 22:10:38 +01:00
|
|
|
foreach ($data['credits'] as $paid_credit) {
|
2020-04-08 12:48:31 +02:00
|
|
|
$credit = Credit::find($this->decodePrimaryKey($paid_credit['credit_id']));
|
2020-01-07 10:35:55 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($credit) {
|
2020-02-01 21:45:23 +01:00
|
|
|
ApplyCreditPayment::dispatchNow($credit, $payment, $paid_credit['amount'], $credit->company);
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-07 10:35:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:28:36 +01:00
|
|
|
event(new PaymentWasCreated($payment, $payment->company));
|
2019-11-18 11:46:01 +01:00
|
|
|
|
2020-01-09 21:15:10 +01:00
|
|
|
$invoice_totals -= $credit_totals;
|
|
|
|
|
2020-02-01 21:45:23 +01:00
|
|
|
//$payment->amount = $invoice_totals; //creates problems when setting amount like this.
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($invoice_totals == $payment->amount) {
|
2020-01-30 21:53:14 +01:00
|
|
|
$payment->applied += $payment->amount;
|
2020-03-21 06:37:30 +01:00
|
|
|
} elseif ($invoice_totals < $payment->amount) {
|
2020-01-30 21:53:14 +01:00
|
|
|
$payment->applied += $invoice_totals;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$payment->save();
|
2019-12-01 12:23:24 +01:00
|
|
|
return $payment->fresh();
|
2020-01-03 09:49:59 +01:00
|
|
|
}
|
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
/**
|
|
|
|
* @deprecated Refundable trait replaces this.
|
|
|
|
*/
|
2020-01-17 22:10:38 +01:00
|
|
|
private function refundPayment(array $data, Payment $payment): string
|
2020-01-03 09:49:59 +01:00
|
|
|
{
|
2020-01-29 03:03:47 +01:00
|
|
|
// //temp variable to sum the total refund/credit amount
|
|
|
|
// $invoice_total_adjustment = 0;
|
2020-01-03 09:49:59 +01:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
// if (array_key_exists('invoices', $data) && is_array($data['invoices'])) {
|
2020-01-17 22:10:38 +01:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
// foreach ($data['invoices'] as $adjusted_invoice) {
|
2020-01-03 10:34:10 +01:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
// $invoice = Invoice::whereId($adjusted_invoice['invoice_id'])->first();
|
2020-01-07 01:13:47 +01:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
// $invoice_total_adjustment += $adjusted_invoice['amount'];
|
2020-01-03 09:49:59 +01:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
// if (array_key_exists('credits', $adjusted_invoice)) {
|
2020-01-17 22:10:38 +01:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
// //process and insert credit notes
|
|
|
|
// foreach ($adjusted_invoice['credits'] as $credit) {
|
2020-01-03 10:34:10 +01:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
// $credit = $this->credit_repo->save($credit, CreditFactory::create(auth()->user()->id, auth()->user()->id), $invoice);
|
2020-01-03 10:34:10 +01:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
// }
|
2020-01-03 10:34:10 +01:00
|
|
|
|
2020-01-29 03:03:47 +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
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
// }
|
2020-01-03 09:49:59 +01:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
// if (array_key_exists('amount', $data) && $data['amount'] != $invoice_total_adjustment)
|
|
|
|
// return 'Amount must equal the sum of invoice adjustments';
|
|
|
|
// }
|
2020-01-03 09:49:59 +01:00
|
|
|
|
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
// //adjust applied amount
|
|
|
|
// $payment->applied += $invoice_total_adjustment;
|
2020-01-03 09:49:59 +01:00
|
|
|
|
2020-01-29 03:03:47 +01:00
|
|
|
// //adjust clients paid to date
|
|
|
|
// $client = $payment->client;
|
|
|
|
// $client->paid_to_date += $invoice_total_adjustment;
|
|
|
|
|
|
|
|
// $payment->save();
|
|
|
|
// $client->save();
|
2020-01-03 09:49:59 +01:00
|
|
|
}
|
|
|
|
|
2020-01-17 22:10:38 +01:00
|
|
|
|
2020-03-09 10:38:15 +01:00
|
|
|
/**
|
2020-03-21 06:37:30 +01:00
|
|
|
* If the client is paying in a currency other than
|
2020-03-09 10:38:15 +01:00
|
|
|
* the company currency, we need to set a record
|
|
|
|
*/
|
|
|
|
private function processExchangeRates($data, $payment)
|
|
|
|
{
|
2020-04-19 12:29:58 +02:00
|
|
|
|
2020-03-09 10:38:15 +01:00
|
|
|
$client = Client::find($data['client_id']);
|
|
|
|
|
|
|
|
$client_currency = $client->getSetting('currency_id');
|
|
|
|
$company_currency = $client->company->settings->currency_id;
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($company_currency != $client_currency) {
|
2020-03-09 10:38:15 +01:00
|
|
|
$currency = $client->currency();
|
|
|
|
|
2020-04-19 12:29:58 +02:00
|
|
|
$exchange_rate = new CurrencyApi();
|
|
|
|
|
|
|
|
$payment->exchange_rate = $exchange_rate->exchangeRate($client_currency, $company_currency, Carbon::parse($payment->date));
|
2020-03-09 10:38:15 +01:00
|
|
|
$payment->exchange_currency_id = $client_currency;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $payment;
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|