mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Add ability to set country_id by country code and currency_id by currency code
This commit is contained in:
parent
ef11b3eea1
commit
bd0c523b59
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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'),
|
||||
|
Loading…
x
Reference in New Issue
Block a user