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:
Benjamin Beganović 2020-01-17 22:10:38 +01:00 committed by David Bomba
parent 1d55e8aa3f
commit 67c485c1b8
2 changed files with 50 additions and 53 deletions

View File

@ -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);
} }

View File

@ -46,35 +46,35 @@ 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();
if(!$payment->number) if (!$payment->number)
$payment->number = $payment->client->getNextPaymentNumber($payment->client); $payment->number = $payment->client->getNextPaymentNumber($payment->client);
//we only ever update the ACTUAL amount of money transferred //we only ever update the ACTUAL amount of money transferred
@ -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,20 +103,18 @@ 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)
ApplyCreditPayment::dispatchNow($paid_credit, $payment, $paid_credit['amount'], $credit->company); ApplyCreditPayment::dispatchNow($paid_credit, $payment, $paid_credit['amount'], $credit->company);
} }
@ -126,9 +124,9 @@ class PaymentRepository extends BaseRepository
$invoice_totals -= $credit_totals; $invoice_totals -= $credit_totals;
if($invoice_totals == $payment->amount) if ($invoice_totals == $payment->amount)
$payment->applied = $payment->amount; $payment->applied = $payment->amount;
elseif($invoice_totals < $payment->amount) elseif ($invoice_totals < $payment->amount)
$payment->applied = $invoice_totals; $payment->applied = $invoice_totals;
//UpdateInvoicePayment::dispatchNow($payment); //UpdateInvoicePayment::dispatchNow($payment);
@ -138,36 +136,35 @@ 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();
$invoice_total_adjustment += $adjusted_invoice['amount']; $invoice_total_adjustment += $adjusted_invoice['amount'];
if(array_key_exists('credits', $adjusted_invoice)){ if (array_key_exists('credits', $adjusted_invoice)) {
//process and insert credit notes //process and insert credit notes
foreach($adjusted_invoice['credits'] as $credit){ foreach ($adjusted_invoice['credits'] as $credit) {
$credit = $this->credit_repo->save($credit, CreditFactory::create(auth()->user()->company()->id, auth()->user()->id), $invoice); $credit = $this->credit_repo->save($credit, CreditFactory::create(auth()->user()->company()->id, auth()->user()->id), $invoice);
} }
} } 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';
} }