Fixes for refunds (#3288)

* Working on invoice designs

* Fix unusual form request issue in tests vs production

* Fixes for form requests

* Fixes for refunds
This commit is contained in:
David Bomba 2020-02-06 08:54:20 +11:00 committed by GitHub
parent 11960e25e7
commit 797c3fb3f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 59 additions and 34 deletions

View File

@ -25,6 +25,7 @@ class Modern
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title>$number</title>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge"> <meta http-equiv="x-ua-compatible" content="ie=edge">
@ -91,9 +92,7 @@ class Modern
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
$table_body $table_body
</tbody> </tbody>
</table> </table>
@ -136,7 +135,7 @@ class Modern
{ {
return ' return '
<div class="bg-orange-600 flex justify-between py-8 px-12"> <div class="bg-orange-600 flex justify-between py-8 px-12" style="page-break-inside: avoid;">
<div class="w-1/2"> <div class="w-1/2">
<!-- // --> <!-- // -->
</div> </div>

View File

@ -625,7 +625,7 @@ class InvoiceController extends BaseController
} }
break; break;
case 'mark_sent': case 'mark_sent':
$invoice->markSent(); $invoice->service()->markSent()->save();
if (!$bulk) { if (!$bulk) {
return $this->itemResponse($invoice); return $this->itemResponse($invoice);

View File

@ -33,7 +33,9 @@ class RefundPaymentRequest extends Request
protected function prepareForValidation() protected function prepareForValidation()
{ {
$input = $this->all(); $input = $this->all();
if(!isset($input['gateway_refund'])) if(!isset($input['gateway_refund']))
$input['gateway_refund'] = false; $input['gateway_refund'] = false;
@ -64,14 +66,16 @@ class RefundPaymentRequest extends Request
public function rules() public function rules()
{ {
$input = $this->all();
$rules = [ $rules = [
'id' => 'required', 'id' => 'required',
'id' => new ValidRefundableRequest(), 'id' => new ValidRefundableRequest($input),
'amount' => 'numeric', 'amount' => 'numeric',
'date' => 'required', 'date' => 'required',
'invoices.*.invoice_id' => 'required', 'invoices.*.invoice_id' => 'required',
'invoices.*.amount' => 'required', 'invoices.*.amount' => 'required',
'invoices' => new ValidRefundableInvoices(), 'invoices' => new ValidRefundableInvoices($input),
]; ];
return $rules; return $rules;
@ -79,6 +83,8 @@ class RefundPaymentRequest extends Request
public function payment() :?Payment public function payment() :?Payment
{ {
return Payment::whereId(request()->input('id'))->first(); $input = $this->all();
return Payment::whereId($input['id'])->first();
} }
} }

View File

@ -34,11 +34,18 @@ class ValidRefundableRequest implements Rule
*/ */
private $error_msg; private $error_msg;
private $input;
public function __construct($input)
{
$this->input = $input;
}
public function passes($attribute, $value) public function passes($attribute, $value)
{ {
$payment = Payment::whereId(request()->input('id'))->first(); $payment = Payment::whereId($this->input['id'])->first();
if(!$payment) if(!$payment)
{ {
@ -46,14 +53,14 @@ class ValidRefundableRequest implements Rule
return false; return false;
} }
$request_invoices = request()->has('invoices') ? request()->input('invoices') : []; $request_invoices = request()->has('invoices') ? $this->input['invoices'] : [];
$request_credits = request()->has('credits') ? request()->input('credits') : []; $request_credits = request()->has('credits') ? $this->input['credits'] : [];
foreach($request_invoices as $key => $value) // foreach($request_invoices as $key => $value)
$request_invoices[$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']); // $request_invoices[$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']);
foreach($request_credits as $key => $value) // foreach($request_credits as $key => $value)
$request_credits[$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']); // $request_credits[$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']);
if($payment->invoices()->exists()) if($payment->invoices()->exists())
@ -62,22 +69,18 @@ class ValidRefundableRequest implements Rule
$this->checkInvoice($paymentable_invoice, $request_invoices); $this->checkInvoice($paymentable_invoice, $request_invoices);
} }
// if($payment->credits()->exists()) // if($payment->credits()->exists())
// { // {
// foreach($payment->credits as $paymentable_credit) // foreach($payment->credits as $paymentable_credit)
// $this->checkCredit($paymentable_credit, $request_credits); // $this->checkCredit($paymentable_credit, $request_credits);
// } // }
foreach($request_invoices as $request_invoice) foreach($request_invoices as $request_invoice)
$this->checkInvoiceIsPaymentable($request_invoice, $payment); $this->checkInvoiceIsPaymentable($request_invoice, $payment);
// foreach($request_credits as $request_credit) // foreach($request_credits as $request_credit)
// $this->checkCreditIsPaymentable($request_credit, $payment); // $this->checkCreditIsPaymentable($request_credit, $payment);
if(strlen($this->error_msg) > 0 ) if(strlen($this->error_msg) > 0 )
return false; return false;
@ -86,7 +89,8 @@ class ValidRefundableRequest implements Rule
private function checkInvoiceIsPaymentable($invoice, $payment) 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()) if($payment->invoices()->exists())
{ {
@ -108,7 +112,7 @@ class ValidRefundableRequest implements Rule
private function checkCreditIsPaymentable($credit, $payment) 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()) if($payment->credits()->exists())
{ {

View File

@ -32,12 +32,19 @@ class ValidRefundableInvoices implements Rule
private $error_msg; private $error_msg;
private $input;
public function __construct($input)
{
$this->input = $input;
}
public function passes($attribute, $value) public function passes($attribute, $value)
{ {
//\Log::error(request()->input('id')); $payment = Payment::whereId($this->input['id'])->first();
$payment = Payment::whereId(request()->input('id'))->first();
if(!$payment){ if(!$payment){
$this->error_msg = "Payment couldn't be retrieved cannot be refunded "; $this->error_msg = "Payment couldn't be retrieved cannot be refunded ";
@ -53,7 +60,7 @@ class ValidRefundableInvoices implements Rule
$invoices = []; $invoices = [];
if (is_array($value)) { 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 else
return true; 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) { if ($val['invoice_id'] == $invoice->id) {
//$pivot_record = $invoice->payments->where('id', $invoice->id)->first(); //$pivot_record = $invoice->payments->where('id', $invoice->id)->first();

View File

@ -158,6 +158,8 @@ class CreateInvoicePdf implements ShouldQueue
//->showBrowserHeaderAndFooter() //->showBrowserHeaderAndFooter()
//->headerHtml($header) //->headerHtml($header)
//->footerHtml($footer) //->footerHtml($footer)
->deviceScaleFactor(1)
->showBackground()
->waitUntilNetworkIdle(false)->pdf(); ->waitUntilNetworkIdle(false)->pdf();
//->margins(10,10,10,10) //->margins(10,10,10,10)
//->savePdf('test.pdf'); //->savePdf('test.pdf');

View File

@ -60,11 +60,15 @@ trait MakesHash
public function decodePrimaryKey($value) : string public function decodePrimaryKey($value) : string
{ {
// \Log::error("pre decode = {$value}");
try { try {
$hashids = new Hashids('', 10); $hashids = new Hashids('', 10);
$decoded_array = $hashids->decode($value); $decoded_array = $hashids->decode($value);
// \Log::error($decoded_array);
return $decoded_array[0]; return $decoded_array[0];
} catch (\Exception $e) { } catch (\Exception $e) {
return response()->json(['error'=>'Invalid primary key'], 400); return response()->json(['error'=>'Invalid primary key'], 400);

View File

@ -264,12 +264,12 @@ trait MakesInvoiceValues
if(!$contact) if(!$contact)
$contact = $this->client->primary_contact()->first(); $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.name'] = &$data['$contact_name'];
$data['$contact.custom_value1'] = $contact->custom_value1; $data['$contact.custom_value1'] = isset($contact) ? $contact->custom_value1 : '';
$data['$contact.custom_value2'] = $contact->custom_value2; $data['$contact.custom_value2'] = isset($contact) ? $contact->custom_value2 : '';
$data['$contact.custom_value3'] = $contact->custom_value3; $data['$contact.custom_value3'] = isset($contact) ? $contact->custom_value3 : '';
$data['$contact.custom_value4'] = $contact->custom_value4; $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.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); $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); $logo = $this->company->present()->logo($settings);
$data['$company.logo'] = "<img src='{$logo}' class='w-48'>"; $data['$company.logo'] = "<img src='{$logo}' class='w-48' alt='logo'>";
$data['$company_logo'] = &$data['$company.logo']; $data['$company_logo'] = &$data['$company.logo'];
$data['$company.custom_value1'] = $this->company->custom_value1; $data['$company.custom_value1'] = $this->company->custom_value1;
$data['$company.custom_value2'] = $this->company->custom_value2; $data['$company.custom_value2'] = $this->company->custom_value2;
@ -376,14 +376,16 @@ trait MakesInvoiceValues
{ {
/* Table Header */ /* Table Header */
$table_header = '<thead><tr class="'.$css['table_header_thead_class'].'">'; //$table_header = '<thead><tr class="'.$css['table_header_thead_class'].'">';
$table_header = '';
$column_headers = $this->transformColumnsForHeader($columns); $column_headers = $this->transformColumnsForHeader($columns);
foreach ($column_headers as $column) foreach ($column_headers as $column)
$table_header .= '<td class="'.$css['table_header_td_class'].'">' . ctrans('texts.'.$column.'') . '</td>'; $table_header .= '<td class="'.$css['table_header_td_class'].'">' . ctrans('texts.'.$column.'') . '</td>';
$table_header .= '</tr></thead>'; //$table_header .= '</tr></thead>';
return $table_header; return $table_header;
@ -391,6 +393,7 @@ trait MakesInvoiceValues
public function table_body(array $columns, array $css) :?string public function table_body(array $columns, array $css) :?string
{ {
$table_body = '';
/* Table Body */ /* Table Body */
$columns = $this->transformColumnsForLineItems($columns); $columns = $this->transformColumnsForLineItems($columns);

View File

@ -100,7 +100,7 @@ class RefundTest extends TestCase
$response->assertStatus(200); $response->assertStatus(200);
$payment_id = $arr['data']['id']; $payment_id = $arr['data']['id'];
$this->assertEquals(50, $arr['data']['amount']); $this->assertEquals(50, $arr['data']['amount']);
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first(); $payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();