mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Payment fixes (#3204)
* Fixes for unused variables and annotating the paymentrepository for future work * Refactor Payment repository * rename variables for payments
This commit is contained in:
parent
0878decf18
commit
2c99d3478b
@ -44,7 +44,7 @@ class StorePaymentRequest extends Request
|
||||
|
||||
if (isset($input['invoices']) && is_array($input['invoices']) !== false) {
|
||||
foreach ($input['invoices'] as $key => $value) {
|
||||
$input['invoices'][$key]['id'] = $this->decodePrimaryKey($value['id']);
|
||||
$input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']);
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,7 +54,7 @@ class StorePaymentRequest extends Request
|
||||
|
||||
if (isset($input['credits']) && is_array($input['credits']) !== false) {
|
||||
foreach ($input['credits'] as $key => $value) {
|
||||
$input['credits'][$key]['id'] = $this->decodePrimaryKey($value['id']);
|
||||
$input['credits'][$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,7 +66,7 @@ class UpdatePaymentRequest extends Request
|
||||
|
||||
if (isset($input['invoices']) && is_array($input['invoices']) !== false) {
|
||||
foreach ($input['invoices'] as $key => $value) {
|
||||
$input['invoices'][$key]['id'] = $this->decodePrimaryKey($value['id']);
|
||||
$input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']);
|
||||
}
|
||||
}
|
||||
$this->replace($input);
|
||||
|
@ -42,7 +42,7 @@ class PaymentAmountsBalanceRule implements Rule
|
||||
|
||||
private function calculateAmounts() :bool
|
||||
{
|
||||
$data = [];
|
||||
|
||||
$payment_amounts = 0;
|
||||
$invoice_amounts = 0;
|
||||
|
||||
|
@ -51,7 +51,6 @@ class PaymentAppliedValidAmount implements Rule
|
||||
if(!$payment)
|
||||
return false;
|
||||
|
||||
$data = [];
|
||||
$payment_amounts = 0;
|
||||
$invoice_amounts = 0;
|
||||
|
||||
|
@ -45,14 +45,13 @@ class ValidCreditsPresentRule implements Rule
|
||||
|
||||
private function validCreditsPresent() :bool
|
||||
{
|
||||
$data = [];
|
||||
//todo need to ensure the clients credits are here not random ones!
|
||||
|
||||
if(request()->input('credits') && is_array(request()->input('credits')))
|
||||
{
|
||||
foreach(request()->input('credits') as $credit)
|
||||
{
|
||||
$cred = Credit::find($credit['id']);
|
||||
$cred = Credit::find($credit['invoice_id']);
|
||||
|
||||
if($cred->balance >= $credit['amount'])
|
||||
return false;
|
||||
|
@ -37,7 +37,7 @@ class ValidPayableInvoicesRule implements Rule
|
||||
$invoices = [];
|
||||
|
||||
if (is_array($value)) {
|
||||
$invoices = Invoice::whereIn('id', array_column($value, 'id'))->company()->get();
|
||||
$invoices = Invoice::whereIn('id', array_column($value, 'invoice_id'))->company()->get();
|
||||
}
|
||||
|
||||
foreach ($invoices as $invoice) {
|
||||
|
@ -32,12 +32,9 @@ class PaymentRepository extends BaseRepository
|
||||
|
||||
protected $credit_repo;
|
||||
|
||||
|
||||
public function __construct(CreditRepository $credit_repo)
|
||||
{
|
||||
|
||||
$this->credit_repo = $credit_repo;
|
||||
|
||||
}
|
||||
|
||||
public function getClassName()
|
||||
@ -56,7 +53,22 @@ class PaymentRepository extends BaseRepository
|
||||
public function save(Request $request, Payment $payment) : ?Payment
|
||||
{
|
||||
|
||||
//todo this save() only works for new payments... will fail if a Payment is updated and saved through here.
|
||||
if($payment->amount >= 0)
|
||||
return $this->applyPayment($request, $payment);
|
||||
|
||||
return $this->refundPayment($request, $payment);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a positive payment request
|
||||
* @param Request $request The request object
|
||||
* @param Payment $payment The $payment entity
|
||||
* @return Payment The updated/created payment object
|
||||
*/
|
||||
private function applyPayment(Request $request, Payment $payment) :?Payment
|
||||
{
|
||||
|
||||
$payment->fill($request->all());
|
||||
$payment->status_id = Payment::STATUS_COMPLETED;
|
||||
|
||||
@ -75,12 +87,12 @@ class PaymentRepository extends BaseRepository
|
||||
|
||||
$invoice_totals = array_sum(array_column($request->input('invoices'),'amount'));
|
||||
|
||||
$invoices = Invoice::whereIn('id', array_column($request->input('invoices'), 'id'))->company()->get();
|
||||
$invoices = Invoice::whereIn('id', array_column($request->input('invoices'), 'invoice_id'))->company()->get();
|
||||
|
||||
$payment->invoices()->saveMany($invoices);
|
||||
|
||||
foreach ($request->input('invoices') as $paid_invoice) {
|
||||
$invoice = Invoice::whereId($paid_invoice['id'])->company()->first();
|
||||
$invoice = Invoice::whereId($paid_invoice['invoice_id'])->company()->first();
|
||||
|
||||
if ($invoice) {
|
||||
ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount'], $invoice->company);
|
||||
@ -96,13 +108,13 @@ class PaymentRepository extends BaseRepository
|
||||
|
||||
$credit_totals = array_sum(array_column($request->input('credits'),'amount'));
|
||||
|
||||
$credits = Credit::whereIn('id', array_column($request->input('credits'), 'id'))->company()->get();
|
||||
$credits = Credit::whereIn('id', array_column($request->input('credits'), 'credit_id'))->company()->get();
|
||||
|
||||
$payment->credits()->saveMany($credits);
|
||||
|
||||
foreach ($request->input('credits') as $paid_credit)
|
||||
{
|
||||
$credit = Credit::whereId($paid_credit['id'])->company()->first();
|
||||
$credit = Credit::whereId($paid_credit['credit_id'])->company()->first();
|
||||
|
||||
if($credit)
|
||||
ApplyCreditPayment::dispatchNow($paid_credit, $payment, $paid_credit['amount'], $credit->company);
|
||||
@ -123,45 +135,6 @@ class PaymentRepository extends BaseRepository
|
||||
$payment->save();
|
||||
|
||||
return $payment->fresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
$invoice_totals = array_sum(array_column($request->input('invoices'),'amount'));
|
||||
|
||||
$invoices = Invoice::whereIn('id', array_column($request->input('invoices'), 'id'))->company()->get();
|
||||
|
||||
$payment->invoices()->saveMany($invoices);
|
||||
|
||||
foreach ($request->input('invoices') as $paid_invoice) {
|
||||
$invoice = Invoice::whereId($paid_invoice['id'])->company()->first();
|
||||
|
||||
if ($invoice) {
|
||||
ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount'], $invoice->company);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -174,7 +147,7 @@ class PaymentRepository extends BaseRepository
|
||||
|
||||
foreach($request->input('invoices') as $adjusted_invoice) {
|
||||
|
||||
$invoice = Invoice::whereId($adjusted_invoice['id'])->company()->first();
|
||||
$invoice = Invoice::whereId($adjusted_invoice['invoice_id'])->company()->first();
|
||||
|
||||
$invoice_total_adjustment += $adjusted_invoice['amount'];
|
||||
|
||||
|
@ -132,7 +132,7 @@ class PaymentTest extends TestCase
|
||||
'amount' => $this->invoice->amount,
|
||||
'invoices' => [
|
||||
[
|
||||
'id' => $this->invoice->hashed_id,
|
||||
'invoice_id' => $this->invoice->hashed_id,
|
||||
'amount' => $this->invoice->amount
|
||||
],
|
||||
],
|
||||
@ -182,7 +182,7 @@ class PaymentTest extends TestCase
|
||||
'client_id' => $client->hashed_id,
|
||||
'invoices' => [
|
||||
[
|
||||
'id' => $this->invoice->hashed_id,
|
||||
'invoice_id' => $this->invoice->hashed_id,
|
||||
'amount' => $this->invoice->amount
|
||||
],
|
||||
],
|
||||
@ -299,7 +299,7 @@ class PaymentTest extends TestCase
|
||||
'client_id' => $client->hashed_id,
|
||||
'invoices' => [
|
||||
[
|
||||
'id' => $this->invoice->hashed_id,
|
||||
'invoice_id' => $this->invoice->hashed_id,
|
||||
'amount' => 2.0
|
||||
],
|
||||
],
|
||||
@ -377,7 +377,7 @@ class PaymentTest extends TestCase
|
||||
'client_id' => $client->hashed_id,
|
||||
'invoices' => [
|
||||
[
|
||||
'id' => $this->invoice->hashed_id,
|
||||
'invoice_id' => $this->invoice->hashed_id,
|
||||
'amount' => 6.0
|
||||
],
|
||||
],
|
||||
@ -438,7 +438,7 @@ class PaymentTest extends TestCase
|
||||
'client_id' => $client->hashed_id,
|
||||
'invoices' => [
|
||||
[
|
||||
'id' => $this->invoice->hashed_id,
|
||||
'invoice_id' => $this->invoice->hashed_id,
|
||||
'amount' => 2.0
|
||||
],
|
||||
],
|
||||
@ -499,7 +499,7 @@ class PaymentTest extends TestCase
|
||||
'client_id' => $client->hashed_id,
|
||||
'invoices' => [
|
||||
[
|
||||
'id' => $this->invoice->hashed_id,
|
||||
'invoice_id' => $this->invoice->hashed_id,
|
||||
'amount' => 2.0
|
||||
],
|
||||
],
|
||||
@ -556,7 +556,7 @@ class PaymentTest extends TestCase
|
||||
'client_id' => $client->hashed_id,
|
||||
'invoices' => [
|
||||
[
|
||||
'id' => $this->invoice->hashed_id,
|
||||
'invoice_id' => $this->invoice->hashed_id,
|
||||
'amount' => 2.0
|
||||
],
|
||||
],
|
||||
@ -690,7 +690,7 @@ class PaymentTest extends TestCase
|
||||
'client_id' => $this->encodePrimaryKey($client->id),
|
||||
'invoices' => [
|
||||
[
|
||||
'id' => $this->encodePrimaryKey($this->invoice->id),
|
||||
'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
|
||||
'amount' => 10,
|
||||
]
|
||||
],
|
||||
@ -748,7 +748,7 @@ class PaymentTest extends TestCase
|
||||
'client_id' => $this->encodePrimaryKey($client->id),
|
||||
'invoices' => [
|
||||
[
|
||||
'id' => $this->encodePrimaryKey($this->invoice->id),
|
||||
'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
|
||||
'amount' => 10,
|
||||
]
|
||||
],
|
||||
@ -802,7 +802,7 @@ class PaymentTest extends TestCase
|
||||
'client_id' => $this->encodePrimaryKey($client->id),
|
||||
'invoices' => [
|
||||
[
|
||||
'id' => $this->encodePrimaryKey($this->invoice->id),
|
||||
'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
|
||||
'amount' => 10,
|
||||
]
|
||||
],
|
||||
@ -861,7 +861,7 @@ class PaymentTest extends TestCase
|
||||
'client_id' => $this->encodePrimaryKey($client->id),
|
||||
'invoices' => [
|
||||
[
|
||||
'id' => $this->encodePrimaryKey($this->invoice->id),
|
||||
'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
|
||||
'amount' => 10,
|
||||
]
|
||||
],
|
||||
@ -906,7 +906,7 @@ class PaymentTest extends TestCase
|
||||
'client_id' => $this->encodePrimaryKey($client->id),
|
||||
'invoices' => [
|
||||
[
|
||||
'id' => $this->encodePrimaryKey($this->invoice->id),
|
||||
'invoice_id' => $this->encodePrimaryKey($this->invoice->id),
|
||||
'amount' => 10,
|
||||
]
|
||||
],
|
||||
|
Loading…
x
Reference in New Issue
Block a user