2019-05-03 10:32:30 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @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-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-04-08 12:48:31 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-06-22 13:36:39 +02:00
|
|
|
use App\Utils\Traits\SavesDocuments;
|
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
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* PaymentRepository.
|
2019-05-03 10:32:30 +02:00
|
|
|
*/
|
|
|
|
class PaymentRepository extends BaseRepository
|
|
|
|
{
|
2020-04-08 12:48:31 +02:00
|
|
|
use MakesHash;
|
2020-06-22 13:36:39 +02:00
|
|
|
use SavesDocuments;
|
2020-04-08 12:48:31 +02:00
|
|
|
|
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
|
|
|
|
2020-06-06 03:07:31 +02:00
|
|
|
return $payment;
|
2020-01-11 23:01:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02: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-10-20 07:14:11 +02:00
|
|
|
$is_existing_payment = true;
|
2020-06-06 03:07:31 +02:00
|
|
|
|
2020-03-09 10:38:15 +01:00
|
|
|
//check currencies here and fill the exchange rate data if necessary
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $payment->id) {
|
2020-03-09 10:38:15 +01:00
|
|
|
$this->processExchangeRates($data, $payment);
|
2020-06-06 03:07:31 +02:00
|
|
|
|
2020-10-20 07:14:11 +02:00
|
|
|
$is_existing_payment = false;
|
|
|
|
|
2020-06-06 03:07:31 +02:00
|
|
|
/*We only update the paid to date ONCE per payment*/
|
|
|
|
if (array_key_exists('invoices', $data) && is_array($data['invoices']) && count($data['invoices']) > 0) {
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($data['amount'] == '') {
|
2020-06-06 06:00:59 +02:00
|
|
|
$data['amount'] = array_sum(array_column($data['invoices'], 'amount'));
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
|
|
|
|
2020-06-06 03:07:31 +02:00
|
|
|
$client = Client::find($data['client_id']);
|
2020-07-01 06:37:05 +02:00
|
|
|
|
2020-06-06 06:00:59 +02:00
|
|
|
$client->service()->updatePaidToDate($data['amount'])->save();
|
2020-06-06 03:07:31 +02:00
|
|
|
}
|
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-08-04 07:09:07 +02:00
|
|
|
/*Save documents*/
|
2020-06-22 13:36:39 +02:00
|
|
|
if (array_key_exists('documents', $data)) {
|
|
|
|
$this->saveDocuments($data['documents'], $payment);
|
|
|
|
}
|
2020-06-06 03:07:31 +02:00
|
|
|
|
2020-06-05 05:52:53 +02:00
|
|
|
/*Ensure payment number generated*/
|
2020-09-06 11:38:10 +02: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-08-04 07:09:07 +02:00
|
|
|
/*Set local total variables*/
|
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-06-06 03:07:31 +02:00
|
|
|
if (array_key_exists('invoices', $data) && is_array($data['invoices']) && count($data['invoices']) > 0) {
|
2020-01-17 22:10:38 +01:00
|
|
|
$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();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-11-18 11:46:01 +01:00
|
|
|
$payment->invoices()->saveMany($invoices);
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-09-19 07:02:34 +02:00
|
|
|
//todo optimize this into a single query
|
2020-01-17 22:10:38 +01:00
|
|
|
foreach ($data['invoices'] as $paid_invoice) {
|
2020-06-30 05:31:30 +02:00
|
|
|
$invoice = Invoice::whereId($paid_invoice['invoice_id'])->first();
|
2019-12-01 12:23:24 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($invoice) {
|
2020-06-06 03:07:31 +02:00
|
|
|
$invoice = $invoice->service()->markSent()->applyPayment($payment, $paid_invoice['amount'])->save();
|
2020-09-06 11:38:10 +02: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
|
2020-07-01 02:08:55 +02:00
|
|
|
//01-07-2020 i think we were duplicating the paid to date here.
|
2020-09-06 11:38:10 +02:00
|
|
|
//$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-09-19 07:02:34 +02:00
|
|
|
//todo optimize into a single query
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 07:14:11 +02:00
|
|
|
if(!$is_existing_payment)
|
|
|
|
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
|
2019-11-18 11:46:01 +01:00
|
|
|
|
2020-08-19 04:37:17 +02:00
|
|
|
/*info("invoice totals = {$invoice_totals}");
|
|
|
|
info("credit totals = {$credit_totals}");
|
|
|
|
info("applied totals = " . array_sum(array_column($data['invoices'], 'amount')));
|
|
|
|
*/
|
2020-08-04 07:09:07 +02:00
|
|
|
//$invoice_totals -= $credit_totals;
|
2020-01-09 21:15:10 +01:00
|
|
|
|
2020-08-04 07:09:07 +02:00
|
|
|
////$payment->amount = $invoice_totals; //creates problems when setting amount like this.
|
|
|
|
|
|
|
|
// if($credit_totals == $payment->amount){
|
|
|
|
// $payment->applied += $credit_totals;
|
|
|
|
// } elseif ($invoice_totals == $payment->amount) {
|
|
|
|
// $payment->applied += $payment->amount;
|
|
|
|
// } elseif ($invoice_totals < $payment->amount) {
|
|
|
|
// $payment->applied += $invoice_totals;
|
|
|
|
// }
|
|
|
|
|
2020-10-20 07:14:11 +02:00
|
|
|
$payment->applied += $invoice_totals; //wont work because - check tests
|
2020-01-09 21:15:10 +01:00
|
|
|
|
|
|
|
$payment->save();
|
2020-06-18 05:19:38 +02:00
|
|
|
|
2019-12-01 12:23:24 +01:00
|
|
|
return $payment->fresh();
|
2020-01-03 09:49:59 +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-09-06 11:38:10 +02:00
|
|
|
* the company currency, we need to set a record.
|
2020-03-09 10:38:15 +01:00
|
|
|
*/
|
|
|
|
private function processExchangeRates($data, $payment)
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
2020-06-28 00:24:08 +02:00
|
|
|
|
|
|
|
public function delete($payment)
|
|
|
|
{
|
2020-06-28 05:05:58 +02:00
|
|
|
//cannot double delete a payment
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($payment->is_deleted) {
|
2020-06-28 00:24:08 +02:00
|
|
|
return;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-06-28 00:24:08 +02:00
|
|
|
|
|
|
|
$payment->service()->deletePayment();
|
|
|
|
|
2020-06-28 05:05:58 +02:00
|
|
|
return parent::delete($payment);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function restore($payment)
|
|
|
|
{
|
|
|
|
//we cannot restore a deleted payment.
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($payment->is_deleted) {
|
2020-06-28 05:05:58 +02:00
|
|
|
return;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-06-28 05:05:58 +02:00
|
|
|
|
|
|
|
return parent::restore($payment);
|
2020-06-28 00:24:08 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|