mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Fix PaymentRepository data parameter (#3217)
* Fix PaymentRepository.php parameters * Apply formatting * Fix return when $data['invoice'] !== $invoice_total_adjustment * Pass $request->all() instead of whole request * Pass $request->all() on update method
This commit is contained in:
parent
1d55e8aa3f
commit
67c485c1b8
@ -236,7 +236,7 @@ class PaymentController extends BaseController
|
|||||||
*/
|
*/
|
||||||
public function store(StorePaymentRequest $request)
|
public function store(StorePaymentRequest $request)
|
||||||
{
|
{
|
||||||
$payment = $this->payment_repo->save($request, PaymentFactory::create(auth()->user()->company()->id, auth()->user()->id));
|
$payment = $this->payment_repo->save($request->all(), PaymentFactory::create(auth()->user()->company()->id, auth()->user()->id));
|
||||||
|
|
||||||
return $this->itemResponse($payment);
|
return $this->itemResponse($payment);
|
||||||
}
|
}
|
||||||
@ -412,7 +412,7 @@ class PaymentController extends BaseController
|
|||||||
if($request->entityIsDeleted($payment))
|
if($request->entityIsDeleted($payment))
|
||||||
return $request->disallowUpdate();
|
return $request->disallowUpdate();
|
||||||
|
|
||||||
$payment = $this->payment_repo->save($request, $payment);
|
$payment = $this->payment_repo->save($request->all(), $payment);
|
||||||
|
|
||||||
return $this->itemResponse($payment);
|
return $this->itemResponse($payment);
|
||||||
}
|
}
|
||||||
|
@ -46,30 +46,30 @@ class PaymentRepository extends BaseRepository
|
|||||||
* Saves and updates a payment. //todo refactor to handle refunds and payments.
|
* Saves and updates a payment. //todo refactor to handle refunds and payments.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param Request $request the request object
|
* @param array $data the request object
|
||||||
* @param Payment $payment The Payment object
|
* @param Payment $payment The Payment object
|
||||||
* @return Object Payment $payment
|
* @return Payment|null Payment $payment
|
||||||
*/
|
*/
|
||||||
public function save(Request $request, Payment $payment) : ?Payment
|
public function save(array $data, Payment $payment): ?Payment
|
||||||
{
|
{
|
||||||
|
|
||||||
if ($payment->amount >= 0)
|
if ($payment->amount >= 0)
|
||||||
return $this->applyPayment($request, $payment);
|
return $this->applyPayment($data, $payment);
|
||||||
|
|
||||||
return $this->refundPayment($request, $payment);
|
return $this->refundPayment($data, $payment);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles a positive payment request
|
* Handles a positive payment request
|
||||||
* @param Request $request The request object
|
* @param array $data The data object
|
||||||
* @param Payment $payment The $payment entity
|
* @param Payment $payment The $payment entity
|
||||||
* @return Payment The updated/created payment object
|
* @return Payment The updated/created payment object
|
||||||
*/
|
*/
|
||||||
private function applyPayment(Request $request, Payment $payment) :?Payment
|
private function applyPayment(array $data, Payment $payment): ?Payment
|
||||||
{
|
{
|
||||||
|
|
||||||
$payment->fill($request->all());
|
$payment->fill($data);
|
||||||
$payment->status_id = Payment::STATUS_COMPLETED;
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
||||||
|
|
||||||
$payment->save();
|
$payment->save();
|
||||||
@ -83,15 +83,15 @@ class PaymentRepository extends BaseRepository
|
|||||||
$invoice_totals = 0;
|
$invoice_totals = 0;
|
||||||
$credit_totals = 0;
|
$credit_totals = 0;
|
||||||
|
|
||||||
if ($request->input('invoices') && is_array($request->input('invoices'))) {
|
if (array_key_exists('invoices', $data) && is_array($data['invoices'])) {
|
||||||
|
|
||||||
$invoice_totals = array_sum(array_column($request->input('invoices'),'amount'));
|
$invoice_totals = array_sum(array_column($data['invoices'], 'amount'));
|
||||||
|
|
||||||
$invoices = Invoice::whereIn('id', array_column($request->input('invoices'), 'invoice_id'))->company()->get();
|
$invoices = Invoice::whereIn('id', array_column($data['invoices'], 'invoice_id'))->company()->get();
|
||||||
|
|
||||||
$payment->invoices()->saveMany($invoices);
|
$payment->invoices()->saveMany($invoices);
|
||||||
|
|
||||||
foreach ($request->input('invoices') as $paid_invoice) {
|
foreach ($data['invoices'] as $paid_invoice) {
|
||||||
$invoice = Invoice::whereId($paid_invoice['invoice_id'])->company()->first();
|
$invoice = Invoice::whereId($paid_invoice['invoice_id'])->company()->first();
|
||||||
|
|
||||||
if ($invoice) {
|
if ($invoice) {
|
||||||
@ -103,17 +103,15 @@ class PaymentRepository extends BaseRepository
|
|||||||
ApplyClientPayment::dispatchNow($payment, $payment->company);
|
ApplyClientPayment::dispatchNow($payment, $payment->company);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($request->input('credits') && is_array($request->input('credits')))
|
if (array_key_exists('credits', $data) && is_array($data['credits'])) {
|
||||||
{
|
|
||||||
|
|
||||||
$credit_totals = array_sum(array_column($request->input('credits'),'amount'));
|
$credit_totals = array_sum(array_column($data['credits'], 'amount'));
|
||||||
|
|
||||||
$credits = Credit::whereIn('id', array_column($request->input('credits'), 'credit_id'))->company()->get();
|
$credits = Credit::whereIn('id', array_column($data['credits'], 'credit_id'))->company()->get();
|
||||||
|
|
||||||
$payment->credits()->saveMany($credits);
|
$payment->credits()->saveMany($credits);
|
||||||
|
|
||||||
foreach ($request->input('credits') as $paid_credit)
|
foreach ($data['credits'] as $paid_credit) {
|
||||||
{
|
|
||||||
$credit = Credit::whereId($paid_credit['credit_id'])->company()->first();
|
$credit = Credit::whereId($paid_credit['credit_id'])->company()->first();
|
||||||
|
|
||||||
if ($credit)
|
if ($credit)
|
||||||
@ -138,14 +136,14 @@ class PaymentRepository extends BaseRepository
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function refundPayment(Request $request, Payment $payment) :?Payment
|
private function refundPayment(array $data, Payment $payment): string
|
||||||
{
|
{
|
||||||
//temp variable to sum the total refund/credit amount
|
//temp variable to sum the total refund/credit amount
|
||||||
$invoice_total_adjustment = 0;
|
$invoice_total_adjustment = 0;
|
||||||
|
|
||||||
if($request->has('invoices') && is_array($request->input('invoices'))){
|
if (array_key_exists('invoices', $data) && is_array($data['invoices'])) {
|
||||||
|
|
||||||
foreach($request->input('invoices') as $adjusted_invoice) {
|
foreach ($data['invoices'] as $adjusted_invoice) {
|
||||||
|
|
||||||
$invoice = Invoice::whereId($adjusted_invoice['invoice_id'])->company()->first();
|
$invoice = Invoice::whereId($adjusted_invoice['invoice_id'])->company()->first();
|
||||||
|
|
||||||
@ -160,14 +158,13 @@ class PaymentRepository extends BaseRepository
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
//todo - generate Credit Note for $amount on $invoice - the assumption here is that it is a FULL refund
|
//todo - generate Credit Note for $amount on $invoice - the assumption here is that it is a FULL refund
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if($request->input('amount') != $invoice_total_adjustment)
|
if (array_key_exists('amount', $data) && $data['amount'] != $invoice_total_adjustment)
|
||||||
return 'Amount must equal the sum of invoice adjustments';
|
return 'Amount must equal the sum of invoice adjustments';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user