diff --git a/app/Http/Requests/Client/StoreClientRequest.php b/app/Http/Requests/Client/StoreClientRequest.php index 3f09fc3df173..cea5c9ee22eb 100644 --- a/app/Http/Requests/Client/StoreClientRequest.php +++ b/app/Http/Requests/Client/StoreClientRequest.php @@ -19,6 +19,7 @@ use App\Models\Client; use App\Models\GroupSetting; use App\Utils\Traits\MakesHash; use Illuminate\Validation\Rule; +use Illuminate\Support\Facades\Cache; class StoreClientRequest extends Request { @@ -101,8 +102,11 @@ class StoreClientRequest extends Request } } elseif (!property_exists($settings, 'currency_id')) { $settings->currency_id = (string)auth()->user()->company()->settings->currency_id; - } + } + if (isset($input['currency_code'])) { + $settings->currency_id = $this->getCurrencyCode($input['currency_code']); + } $input['settings'] = $settings; @@ -130,6 +134,10 @@ class StoreClientRequest extends Request } } + if(isset($input['country_code'])) { + $input['country_id'] = $this->getCountryCode($input['country_code']); + } + $this->replace($input); } @@ -141,4 +149,27 @@ class StoreClientRequest extends Request 'contacts.*.email.required' => ctrans('validation.email', ['attribute' => 'email']), ]; } + + private function getCountryCode($country_code) + { + $countries = Cache::get('countries'); + + $country = $countries->filter(function ($item) use($country_code) { + return $item->iso_3166_2 == $country_code || $item->iso_3166_3 == $country_code; + })->first(); + + return (string) $country->id; + } + + private function getCurrencyCode($code) + { + $currencies = Cache::get('currencies'); + + $currency = $currencies->filter(function ($item) use($code){ + return $item->code == $code; + })->first(); + + return (string) $currency->id; + } + } diff --git a/tests/Feature/ClientTest.php b/tests/Feature/ClientTest.php index 95612ce11906..04483bcfa817 100644 --- a/tests/Feature/ClientTest.php +++ b/tests/Feature/ClientTest.php @@ -58,32 +58,66 @@ class ClientTest extends TestCase } + public function testStoreClientUsingCountryCode() + { + $data = [ + 'name' => 'Country Code Name', + 'country_code' => 'US', + 'currency_code' => 'USD', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/clients/', $data); + + + $arr = $response->json(); + $client = Client::find($this->decodePrimaryKey($arr['data']['id'])); + + $this->assertEquals(840, $client->country_id); + $this->assertEquals(1, $client->settings->currency_id); + + $data = [ + 'name' => 'Country Code Name', + 'country_code' => 'USA', + 'currency_code' => 'USD', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/clients/', $data); + + + $arr = $response->json(); + $client = Client::find($this->decodePrimaryKey($arr['data']['id'])); + + $this->assertEquals(840, $client->country_id); + $this->assertEquals(1, $client->settings->currency_id); + + + $data = [ + 'name' => 'Country Code Name', + 'country_code' => 'AU', + 'currency_code' => 'AUD', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/clients/', $data); + + + $arr = $response->json(); + $client = Client::find($this->decodePrimaryKey($arr['data']['id'])); + + $this->assertEquals(36, $client->country_id); + $this->assertEquals(12, $client->settings->currency_id); + } + public function testClientList() { - // $data = [ - // 'first_name' => $this->faker->firstName, - // 'last_name' => $this->faker->lastName, - // 'name' => $this->faker->company, - // 'email' => $this->faker->unique()->safeEmail, - // 'password' => 'ALongAndBrilliantPassword123', - // '_token' => csrf_token(), - // 'privacy_policy' => 1, - // 'terms_of_service' => 1 - // ]; - - - // $response = $this->withHeaders([ - // 'X-API-SECRET' => config('ninja.api_secret'), - // ])->post('/api/v1/signup?include=account', $data); - - - // $response->assertStatus(200); - - // $acc = $response->json(); - - // $account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id'])); - - // $this->token = $account->default_company->tokens->first()->token; $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'),