From 6a11fae857e984385bd1177c7b278ab92ee3efc6 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 8 Jul 2020 00:50:51 +1000 Subject: [PATCH] Working on Autobill --- app/Services/Invoice/AutoBillInvoice.php | 27 +++++--- tests/Feature/CompanyGatewayTest.php | 69 +++++++++++++++++++++ tests/MockAccountData.php | 79 +++++++++--------------- 3 files changed, 118 insertions(+), 57 deletions(-) create mode 100644 tests/Feature/CompanyGatewayTest.php diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index cf79e31b372c..b03e8d517b6c 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -48,14 +48,27 @@ class AutoBillInvoice extends AbstractService { $gateway_tokens = $this->client->gateway_tokens->orderBy('is_default', 'DESC'); - $gateways->filter(function ($method) use ($amount) { - if ($method->min_limit !== null && $amount < $method->min_limit) { - return false; - } + $billing_gateway_token = null; - if ($method->max_limit !== null && $amount > $method->min_limit) { + $gateway_tokens->filter(function ($token) use ($amount){ + + if(isset($token->gateway->fees_and_limits)) + $fees_and_limits = $token->gateway->fees_and_limits->{"1"}; + else + return true; + + if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $amount < $fees_and_limits->min_limit) + return false; + + if ((property_exists($fees_and_limits, 'max_limit')) && $fees_and_limits->max_limit !== null && $amount > $fees_and_limits->min_limit) return false; - } - }); + + return true; + + })->all()->first(); + + + } + } diff --git a/tests/Feature/CompanyGatewayTest.php b/tests/Feature/CompanyGatewayTest.php new file mode 100644 index 000000000000..1ce75f3d639f --- /dev/null +++ b/tests/Feature/CompanyGatewayTest.php @@ -0,0 +1,69 @@ +makeTestData(); + + + } + + public function testGatewayExists() + { + + $company_gateway = CompanyGateway::first(); + $this->assertNotNull($company_gateway); + + } + + public function testFeesAndLimitsExists() + { + $data = []; + $data[1]['min_limit'] = 234; + $data[1]['max_limit'] = 65317; + $data[1]['fee_amount'] = 0.00; + $data[1]['fee_percent'] = 0.000; + $data[1]['fee_tax_name1'] = ''; + $data[1]['fee_tax_rate1'] = ''; + $data[1]['fee_tax_name2'] = ''; + $data[1]['fee_tax_rate2'] = ''; + $data[1]['fee_tax_name3'] = ''; + $data[1]['fee_tax_rate3'] = 0; + + $cg = new CompanyGateway; + $cg->company_id = $this->company->id; + $cg->user_id = $this->user->id; + $cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23'; + $cg->require_cvv = true; + $cg->show_billing_address = true; + $cg->show_shipping_address = true; + $cg->update_details = true; + $cg->config = encrypt(config('ninja.testvars.stripe')); + $cg->fees_and_limits = $data; + $cg->save(); + + $this->assertNotNull($cg->fees_and_limits); + $this->assertNotNull($cg->fees_and_limits->{"1"}); + + + //confirm amount filtering works + } +} \ No newline at end of file diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 62b96fa5e553..5c5105b607f8 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -11,9 +11,11 @@ namespace Tests; +use App\DataMapper\BaseSettings; use App\DataMapper\ClientSettings; use App\DataMapper\CompanySettings; use App\DataMapper\DefaultSettings; +use App\DataMapper\FeesAndLimits; use App\Factory\ClientFactory; use App\Factory\CompanyUserFactory; use App\Factory\CreditFactory; @@ -32,6 +34,7 @@ use App\Models\InvoiceInvitation; use App\Models\Quote; use App\Models\RecurringInvoice; use App\Models\User; +use App\Utils\Traits\CompanyGatewayFeesAndLimitsSaver; use App\Utils\Traits\GeneratesCounter; use App\Utils\Traits\MakesHash; use Illuminate\Support\Carbon; @@ -341,17 +344,18 @@ trait MockAccountData $gs->save(); if (config('ninja.testvars.stripe')) { - $cg = new CompanyGateway; - $cg->company_id = $this->company->id; - $cg->user_id = $this->user->id; - $cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23'; - $cg->require_cvv = true; - $cg->show_billing_address = true; - $cg->show_shipping_address = true; - $cg->update_details = true; - $cg->config = encrypt(config('ninja.testvars.stripe')); - $cg->save(); + $data = []; + $data[1]['min_limit'] = 234; + $data[1]['max_limit'] = 65317; + $data[1]['fee_amount'] = 0.00; + $data[1]['fee_percent'] = 0.000; + $data[1]['fee_tax_name1'] = ''; + $data[1]['fee_tax_rate1'] = ''; + $data[1]['fee_tax_name2'] = ''; + $data[1]['fee_tax_rate2'] = ''; + $data[1]['fee_tax_name3'] = ''; + $data[1]['fee_tax_rate3'] = 0; $cg = new CompanyGateway; $cg->company_id = $this->company->id; @@ -362,11 +366,26 @@ trait MockAccountData $cg->show_shipping_address = true; $cg->update_details = true; $cg->config = encrypt(config('ninja.testvars.stripe')); + $cg->fees_and_limits = $data; $cg->save(); + + $cg = new CompanyGateway; + $cg->company_id = $this->company->id; + $cg->user_id = $this->user->id; + $cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23'; + $cg->require_cvv = true; + $cg->show_billing_address = true; + $cg->show_shipping_address = true; + $cg->update_details = true; + $cg->fees_and_limits = $data; + $cg->config = encrypt(config('ninja.testvars.stripe')); + $cg->save(); + } } + private function buildLineItems() { $line_items = []; @@ -377,46 +396,6 @@ trait MockAccountData $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - // $line_items[] = $item; - return $line_items; } }