From 797c3fb3f69ecb96b34035aa79505064140d6777 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 6 Feb 2020 08:54:20 +1100 Subject: [PATCH] Fixes for refunds (#3288) * Working on invoice designs * Fix unusual form request issue in tests vs production * Fixes for form requests * Fixes for refunds --- app/Designs/Modern.php | 5 ++-- app/Http/Controllers/InvoiceController.php | 2 +- .../Requests/Payment/RefundPaymentRequest.php | 12 ++++++-- .../Payment/ValidRefundableRequest.php | 30 +++++++++++-------- .../ValidRefundableInvoices.php | 17 +++++++---- app/Jobs/Invoice/CreateInvoicePdf.php | 2 ++ app/Utils/Traits/MakesHash.php | 4 +++ app/Utils/Traits/MakesInvoiceValues.php | 19 +++++++----- tests/Feature/RefundTest.php | 2 +- 9 files changed, 59 insertions(+), 34 deletions(-) diff --git a/app/Designs/Modern.php b/app/Designs/Modern.php index 6ee268ff6f18..1f4ab88f58a3 100644 --- a/app/Designs/Modern.php +++ b/app/Designs/Modern.php @@ -25,6 +25,7 @@ class Modern + $number @@ -91,9 +92,7 @@ class Modern - $table_body - @@ -136,7 +135,7 @@ class Modern { return ' -
+
diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index e51ba98cf552..fdb423150a5c 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -625,7 +625,7 @@ class InvoiceController extends BaseController } break; case 'mark_sent': - $invoice->markSent(); + $invoice->service()->markSent()->save(); if (!$bulk) { return $this->itemResponse($invoice); diff --git a/app/Http/Requests/Payment/RefundPaymentRequest.php b/app/Http/Requests/Payment/RefundPaymentRequest.php index 8f3909a37ef6..e040f70eb9c5 100644 --- a/app/Http/Requests/Payment/RefundPaymentRequest.php +++ b/app/Http/Requests/Payment/RefundPaymentRequest.php @@ -33,7 +33,9 @@ class RefundPaymentRequest extends Request protected function prepareForValidation() { + $input = $this->all(); + if(!isset($input['gateway_refund'])) $input['gateway_refund'] = false; @@ -64,14 +66,16 @@ class RefundPaymentRequest extends Request public function rules() { + $input = $this->all(); + $rules = [ 'id' => 'required', - 'id' => new ValidRefundableRequest(), + 'id' => new ValidRefundableRequest($input), 'amount' => 'numeric', 'date' => 'required', 'invoices.*.invoice_id' => 'required', 'invoices.*.amount' => 'required', - 'invoices' => new ValidRefundableInvoices(), + 'invoices' => new ValidRefundableInvoices($input), ]; return $rules; @@ -79,6 +83,8 @@ class RefundPaymentRequest extends Request public function payment() :?Payment { - return Payment::whereId(request()->input('id'))->first(); + $input = $this->all(); + + return Payment::whereId($input['id'])->first(); } } diff --git a/app/Http/ValidationRules/Payment/ValidRefundableRequest.php b/app/Http/ValidationRules/Payment/ValidRefundableRequest.php index e41f8a81b0f1..56daad730f99 100644 --- a/app/Http/ValidationRules/Payment/ValidRefundableRequest.php +++ b/app/Http/ValidationRules/Payment/ValidRefundableRequest.php @@ -34,11 +34,18 @@ class ValidRefundableRequest implements Rule */ private $error_msg; + private $input; + + + public function __construct($input) + { + $this->input = $input; + } public function passes($attribute, $value) { - $payment = Payment::whereId(request()->input('id'))->first(); + $payment = Payment::whereId($this->input['id'])->first(); if(!$payment) { @@ -46,14 +53,14 @@ class ValidRefundableRequest implements Rule return false; } - $request_invoices = request()->has('invoices') ? request()->input('invoices') : []; - $request_credits = request()->has('credits') ? request()->input('credits') : []; + $request_invoices = request()->has('invoices') ? $this->input['invoices'] : []; + $request_credits = request()->has('credits') ? $this->input['credits'] : []; - foreach($request_invoices as $key => $value) - $request_invoices[$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']); + // foreach($request_invoices as $key => $value) + // $request_invoices[$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']); - foreach($request_credits as $key => $value) - $request_credits[$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']); + // foreach($request_credits as $key => $value) + // $request_credits[$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']); if($payment->invoices()->exists()) @@ -62,22 +69,18 @@ class ValidRefundableRequest implements Rule $this->checkInvoice($paymentable_invoice, $request_invoices); } - // if($payment->credits()->exists()) // { // foreach($payment->credits as $paymentable_credit) // $this->checkCredit($paymentable_credit, $request_credits); // } - foreach($request_invoices as $request_invoice) $this->checkInvoiceIsPaymentable($request_invoice, $payment); - // foreach($request_credits as $request_credit) // $this->checkCreditIsPaymentable($request_credit, $payment); - if(strlen($this->error_msg) > 0 ) return false; @@ -86,7 +89,8 @@ class ValidRefundableRequest implements Rule private function checkInvoiceIsPaymentable($invoice, $payment) { - $invoice = Invoice::find($invoice['invoice_id']); + + $invoice = Invoice::whereId($invoice['invoice_id'])->whereCompanyId($payment->company_id)->first(); if($payment->invoices()->exists()) { @@ -108,7 +112,7 @@ class ValidRefundableRequest implements Rule private function checkCreditIsPaymentable($credit, $payment) { - $credit = Credit::find($credit['credit_id']); + $credit = Credit::whereId($credit['credit_id'])->whereCompanyId($payment->company_id)->first(); if($payment->credits()->exists()) { diff --git a/app/Http/ValidationRules/ValidRefundableInvoices.php b/app/Http/ValidationRules/ValidRefundableInvoices.php index 7c7b81df92ba..e3fbc75d3202 100644 --- a/app/Http/ValidationRules/ValidRefundableInvoices.php +++ b/app/Http/ValidationRules/ValidRefundableInvoices.php @@ -32,12 +32,19 @@ class ValidRefundableInvoices implements Rule private $error_msg; + private $input; + + public function __construct($input) + { + $this->input = $input; + } + + + public function passes($attribute, $value) { - //\Log::error(request()->input('id')); - - $payment = Payment::whereId(request()->input('id'))->first(); + $payment = Payment::whereId($this->input['id'])->first(); if(!$payment){ $this->error_msg = "Payment couldn't be retrieved cannot be refunded "; @@ -53,7 +60,7 @@ class ValidRefundableInvoices implements Rule $invoices = []; if (is_array($value)) { - $invoices = Invoice::whereIn('id', array_column($value, 'invoice_id'))->company()->get(); + $invoices = Invoice::whereIn('id', array_column($this->input['invoices'], 'invoice_id'))->company()->get(); } else return true; @@ -65,7 +72,7 @@ class ValidRefundableInvoices implements Rule } - foreach ($value as $val) { + foreach ($this->input['invoices'] as $val) { if ($val['invoice_id'] == $invoice->id) { //$pivot_record = $invoice->payments->where('id', $invoice->id)->first(); diff --git a/app/Jobs/Invoice/CreateInvoicePdf.php b/app/Jobs/Invoice/CreateInvoicePdf.php index 63467337e625..e178402e4cde 100644 --- a/app/Jobs/Invoice/CreateInvoicePdf.php +++ b/app/Jobs/Invoice/CreateInvoicePdf.php @@ -158,6 +158,8 @@ class CreateInvoicePdf implements ShouldQueue //->showBrowserHeaderAndFooter() //->headerHtml($header) //->footerHtml($footer) + ->deviceScaleFactor(1) + ->showBackground() ->waitUntilNetworkIdle(false)->pdf(); //->margins(10,10,10,10) //->savePdf('test.pdf'); diff --git a/app/Utils/Traits/MakesHash.php b/app/Utils/Traits/MakesHash.php index d9f220cb4b80..f836681d2800 100644 --- a/app/Utils/Traits/MakesHash.php +++ b/app/Utils/Traits/MakesHash.php @@ -60,11 +60,15 @@ trait MakesHash public function decodePrimaryKey($value) : string { + // \Log::error("pre decode = {$value}"); + try { $hashids = new Hashids('', 10); $decoded_array = $hashids->decode($value); + // \Log::error($decoded_array); + return $decoded_array[0]; } catch (\Exception $e) { return response()->json(['error'=>'Invalid primary key'], 400); diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index 6029837288ac..3cee087cdbf3 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -264,12 +264,12 @@ trait MakesInvoiceValues if(!$contact) $contact = $this->client->primary_contact()->first(); - $data['$contact_name'] = $contact->present()->name() ?: 'no contact name on record'; + $data['$contact_name'] = isset($contact) ? $contact->present()->name() : 'no contact name on record'; $data['$contact.name'] = &$data['$contact_name']; - $data['$contact.custom_value1'] = $contact->custom_value1; - $data['$contact.custom_value2'] = $contact->custom_value2; - $data['$contact.custom_value3'] = $contact->custom_value3; - $data['$contact.custom_value4'] = $contact->custom_value4; + $data['$contact.custom_value1'] = isset($contact) ? $contact->custom_value1 : ''; + $data['$contact.custom_value2'] = isset($contact) ? $contact->custom_value2 : ''; + $data['$contact.custom_value3'] = isset($contact) ? $contact->custom_value3 : ''; + $data['$contact.custom_value4'] = isset($contact) ? $contact->custom_value4 : ''; $data['$company.city_state_postal'] = $this->company->present()->cityStateZip($settings->city, $settings->state, $settings->postal_code, false); $data['$company.postal_city_state'] = $this->company->present()->cityStateZip($settings->city, $settings->state, $settings->postal_code, true); @@ -290,7 +290,7 @@ trait MakesInvoiceValues $logo = $this->company->present()->logo($settings); - $data['$company.logo'] = ""; + $data['$company.logo'] = "logo"; $data['$company_logo'] = &$data['$company.logo']; $data['$company.custom_value1'] = $this->company->custom_value1; $data['$company.custom_value2'] = $this->company->custom_value2; @@ -376,14 +376,16 @@ trait MakesInvoiceValues { /* Table Header */ - $table_header = ''; + //$table_header = ''; + $table_header = ''; + $column_headers = $this->transformColumnsForHeader($columns); foreach ($column_headers as $column) $table_header .= '' . ctrans('texts.'.$column.'') . ''; - $table_header .= ''; + //$table_header .= ''; return $table_header; @@ -391,6 +393,7 @@ trait MakesInvoiceValues public function table_body(array $columns, array $css) :?string { + $table_body = ''; /* Table Body */ $columns = $this->transformColumnsForLineItems($columns); diff --git a/tests/Feature/RefundTest.php b/tests/Feature/RefundTest.php index d2359fadd8ea..ebd84eed5679 100644 --- a/tests/Feature/RefundTest.php +++ b/tests/Feature/RefundTest.php @@ -100,7 +100,7 @@ class RefundTest extends TestCase $response->assertStatus(200); $payment_id = $arr['data']['id']; - + $this->assertEquals(50, $arr['data']['amount']); $payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();