diff --git a/app/DataMapper/ClientSettings.php b/app/DataMapper/ClientSettings.php index e9a9049a5279..62c93798cdf3 100644 --- a/app/DataMapper/ClientSettings.php +++ b/app/DataMapper/ClientSettings.php @@ -41,7 +41,6 @@ class ClientSettings extends BaseSettings public $payment_terms; public $language_id; - public $currency_id; public $precision; public $default_task_rate; public $send_reminders; diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index 7e7cef1a1fb5..12a3441ba8ad 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -27,7 +27,7 @@ class CompanySettings extends BaseSettings public $financial_year_start; public $language_id; - public $currency_id; + public $precision; public $show_currency_symbol; public $show_currency_code; @@ -138,7 +138,6 @@ class CompanySettings extends BaseSettings 'entity' => Company::class, 'timezone_id' => config('ninja.i18n.timezone_id'), 'language_id' => config('ninja.i18n.language_id'), - 'currency_id' => config('ninja.i18n.currency_id'), 'precision' => 2, 'payment_terms' => config('ninja.i18n.payment_terms'), 'datetime_format' => config('ninja.i18n.datetime_format'), diff --git a/app/Factory/ClientFactory.php b/app/Factory/ClientFactory.php index eb1bb9615062..428ac427e836 100644 --- a/app/Factory/ClientFactory.php +++ b/app/Factory/ClientFactory.php @@ -29,7 +29,7 @@ class ClientFactory $client->country_id = 4; $client->is_deleted = 0; $client->client_hash = str_random(40); - + $client->currency_id = config('ninja.i18n.currency_id'); $client->settings = new ClientSettings(ClientSettings::defaults()); $client_contact = ClientContactFactory::create($company_id, $user_id); diff --git a/app/Http/Controllers/ClientPortal/InvoiceController.php b/app/Http/Controllers/ClientPortal/InvoiceController.php index a689316058f9..8acb3344b62e 100644 --- a/app/Http/Controllers/ClientPortal/InvoiceController.php +++ b/app/Http/Controllers/ClientPortal/InvoiceController.php @@ -62,9 +62,9 @@ class InvoiceController extends Controller })->editColumn('due_date', function ($invoice){ return $this->formatDate($invoice->due_date, $invoice->client->date_format()); })->editColumn('balance', function ($invoice) { - return Number::formatMoney($invoice->balance, $invoice->client->currency(), $invoice->client->country, $invoice->client->getMergedSettings()); + return Number::formatMoney($invoice->balance, $invoice->client->currency, $invoice->client->country, $invoice->client->getMergedSettings()); })->editColumn('amount', function ($invoice) { - return Number::formatMoney($invoice->amount, $invoice->client->currency(), $invoice->client->country, $invoice->client->getMergedSettings()); + return Number::formatMoney($invoice->amount, $invoice->client->currency, $invoice->client->country, $invoice->client->getMergedSettings()); }) ->rawColumns(['checkbox', 'action', 'status_id']) ->make(true); @@ -125,14 +125,14 @@ class InvoiceController extends Controller $invoices->filter(function ($invoice){ return $invoice->isPayable(); })->map(function ($invoice){ - $invoice->balance = Number::formatMoney($invoice->balance, $invoice->client->currency(), $invoice->client->country, $invoice->client->getMergedSettings()); + $invoice->balance = Number::formatMoney($invoice->balance, $invoice->client->currency, $invoice->client->country, $invoice->client->getMergedSettings()); $invoice->due_date = $this->formatDate($invoice->due_date, $invoice->client->date_format()); return $invoice; }); - $formatted_total = Number::formatMoney($total, auth()->user()->client->currency(), auth()->user()->client->country, auth()->user()->client->getMergedSettings()); + $formatted_total = Number::formatMoney($total, auth()->user()->client->currency, auth()->user()->client->country, auth()->user()->client->getMergedSettings()); $payment_methods = auth()->user()->client->getPaymentMethods($total); diff --git a/app/Models/Client.php b/app/Models/Client.php index e933caa91542..700cc019ee7d 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -143,7 +143,8 @@ class Client extends BaseModel public function currency() { - return Currency::find($this->getMergedSettings()->currency_id); + return $this->belongsTo(Currency::class); + //return Currency::find($this->getMergedSettings()->currency_id); } public function getMergedSettings() diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index e7abb2218abf..f02dd36c8fb4 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -151,7 +151,7 @@ class CompanyGateway extends BaseModel $fee = $this->calcGatewayFee($amount); if($fee > 0 ){ - $fee = Number::formatMoney(round($fee, 2), $client->currency(), $client->country(), $client->getMergedSettings()); + $fee = Number::formatMoney(round($fee, 2), $client->currency, $client->country(), $client->getMergedSettings()); $label = ' - ' . $fee . ' ' . ctrans('texts.fee'); } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 18eef7e5e14b..b5fda91ac916 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -172,17 +172,6 @@ class Invoice extends BaseModel return $this->client->getMergedSettings()->lock_sent_invoices; } - /** - * Gets the currency from the settings object. - * - * @return Eloquent Model The currency. - */ - public function getCurrency() - { - return Currency::find($this->settings->currency_id); - } - - /** * Determines if invoice overdue. * diff --git a/app/Utils/Statics.php b/app/Utils/Statics.php index 69ee00d4ea25..1ea46991cab6 100644 --- a/app/Utils/Statics.php +++ b/app/Utils/Statics.php @@ -71,13 +71,11 @@ class Statics $data = []; - $cached_tables = config('ninja.cached_tables'); - - foreach ($cached_tables as $name => $class) { + foreach (config('ninja.cached_tables') as $name => $class) { $data[$name] = Cache::get($name); } -Log::error($data); +// Log::error($data); if ($locale) { diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index 0f25cbf203b2..dd593e947556 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -167,13 +167,13 @@ trait MakesInvoiceValues // $data['$quantity'] = ; // $data['$line_total'] = ; // $data['$paid_to_date'] = ; - $data['$discount'] = Number::formatMoney($this->calc()->getTotalDiscount(), $this->client->currency(), $this->client->country, $this->client->getMergedSettings()); - $data['$subtotal'] = Number::formatMoney($this->calc()->getSubTotal(), $this->client->currency(), $this->client->country, $this->client->getMergedSettings()); - $data['$balance_due'] = Number::formatMoney($this->balance, $this->client->currency(), $this->client->country, $this->client->getMergedSettings()); - $data['$partial_due'] = Number::formatMoney($this->partial, $this->client->currency(), $this->client->country, $this->client->getMergedSettings()); - $data['$total'] = Number::formatMoney($this->calc()->getTotal(), $this->client->currency(), $this->client->country, $this->client->getMergedSettings()); - $data['$balance'] = Number::formatMoney($this->calc()->getBalance(), $this->client->currency(), $this->client->country, $this->client->getMergedSettings()); - $data['$taxes'] = Number::formatMoney($this->calc()->getTotalTaxes(), $this->client->currency(), $this->client->country, $this->client->getMergedSettings()); + $data['$discount'] = Number::formatMoney($this->calc()->getTotalDiscount(), $this->client->currency, $this->client->country, $this->client->getMergedSettings()); + $data['$subtotal'] = Number::formatMoney($this->calc()->getSubTotal(), $this->client->currency, $this->client->country, $this->client->getMergedSettings()); + $data['$balance_due'] = Number::formatMoney($this->balance, $this->client->currency, $this->client->country, $this->client->getMergedSettings()); + $data['$partial_due'] = Number::formatMoney($this->partial, $this->client->currency, $this->client->country, $this->client->getMergedSettings()); + $data['$total'] = Number::formatMoney($this->calc()->getTotal(), $this->client->currency, $this->client->country, $this->client->getMergedSettings()); + $data['$balance'] = Number::formatMoney($this->calc()->getBalance(), $this->client->currency, $this->client->country, $this->client->getMergedSettings()); + $data['$taxes'] = Number::formatMoney($this->calc()->getTotalTaxes(), $this->client->currency, $this->client->country, $this->client->getMergedSettings()); $data['$terms'] = $this->terms; // $data['$your_invoice'] = ; // $data['$quote'] = ; @@ -365,14 +365,14 @@ trait MakesInvoiceValues foreach($items as $item) { - $item->cost = Number::formatMoney($item->cost, $this->client->currency(), $this->client->country, $this->client->getMergedSettings()); - $item->line_total = Number::formatMoney($item->line_total, $this->client->currency(), $this->client->country, $this->client->getMergedSettings()); + $item->cost = Number::formatMoney($item->cost, $this->client->currency, $this->client->country, $this->client->getMergedSettings()); + $item->line_total = Number::formatMoney($item->line_total, $this->client->currency, $this->client->country, $this->client->getMergedSettings()); if(isset($item->discount) && $item->discount > 0) { if($item->is_amount_discount) - $item->discount = Number::formatMoney($item->discount, $this->client->currency(), $this->client->country, $this->client->getMergedSettings()); + $item->discount = Number::formatMoney($item->discount, $this->client->currency, $this->client->country, $this->client->getMergedSettings()); else $item->discount = $item->discount . '%'; } @@ -403,7 +403,7 @@ trait MakesInvoiceValues { $data .= ''; $data .= ''. $tax['name'] .''; - $data .= ''. Number::formatMoney($tax['total'], $this->client->currency(), $this->client->country, $this->client->getMergedSettings()) .''; + $data .= ''. Number::formatMoney($tax['total'], $this->client->currency, $this->client->country, $this->client->getMergedSettings()) .''; } return $data; diff --git a/config/ninja.php b/config/ninja.php index 37e9b283a046..b7a8fdab9724 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -40,7 +40,7 @@ return [ 'i18n' => [ 'timezone_id' => env('DEFAULT_TIMEZONE', 15), 'country_id' => env('DEFAULT_COUNTRY', 840), // United Stated - 'currency_id' => env('DEFAULT_CURRENCY', 1), //USD + 'currency_id' => env('DEFAULT_CURRENCY', 1), 'language_id' => env('DEFAULT_LANGUAGE', 1), //en 'date_format' => env('DEFAULT_DATE_FORMAT', 'M j, Y'), 'date_picker_format' => env('DEFAULT_DATE_PICKER_FORMAT', 'M d, yyyy'), diff --git a/database/factories/ClientFactory.php b/database/factories/ClientFactory.php index 026cf155bbf8..1f8a55cf212e 100644 --- a/database/factories/ClientFactory.php +++ b/database/factories/ClientFactory.php @@ -21,6 +21,7 @@ $factory->define(App\Models\Client::class, function (Faker $faker) { 'city' => $faker->city, 'state' => $faker->state, 'postal_code' => $faker->postcode, + 'currency_id' => 1, 'country_id' => 4, 'shipping_address1' => $faker->buildingNumber, 'shipping_address2' => $faker->streetAddress, diff --git a/tests/Feature/LoginTest.php b/tests/Feature/LoginTest.php index 0dc42d7692ed..4c80b007beca 100644 --- a/tests/Feature/LoginTest.php +++ b/tests/Feature/LoginTest.php @@ -4,6 +4,7 @@ namespace Tests\Feature; use App\Models\Account; use App\Models\Client; +use App\Models\CompanyToken; use App\Models\User; use App\Utils\Traits\UserSessionAttributes; use Illuminate\Foundation\Testing\DatabaseTransactions; @@ -12,6 +13,7 @@ use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Http\Request; use Illuminate\Support\Facades\Session; use Tests\TestCase; +use Illuminate\Support\Facades\Log; /** * @test @@ -142,18 +144,37 @@ class LoginTest extends TestCase 'password' => \Hash::make('123456') ]); - $company = factory(\App\Models\Company::class)->make([ + $company = factory(\App\Models\Company::class)->create([ 'account_id' => $account->id, 'domain' => 'ninja.test', ]); + $account->default_company_id = $account->id; + $account->save(); + + + $ct = CompanyToken::create([ + 'user_id' => $user->id, + 'account_id' => $account->id, + 'token' => str_random(64), + 'name' => $user->first_name. ' '. $user->last_name, + 'company_id' => $company->id, + ]); + $user->companies()->attach($company->id, [ 'account_id' => $account->id, 'is_owner' => 1, 'is_admin' => 1, ]); + $user->fresh(); + + $this->assertTrue($user->companies !== null); + $this->assertTrue($user->user_companies !== null); + $this->assertTrue($user->user_companies->first() !== null); + $this->assertTrue($user->user_companies->first()->account !== null); + $data = [ 'email' => 'test@example.com', 'password' => '123456' diff --git a/tests/Unit/CollectionMergingTest.php b/tests/Unit/CollectionMergingTest.php index 7df52cffdc73..2f557d6d3835 100644 --- a/tests/Unit/CollectionMergingTest.php +++ b/tests/Unit/CollectionMergingTest.php @@ -34,7 +34,7 @@ class CollectionMergingTest extends TestCase public function testMergingCollection() { - $payment_terms = collect(unserialize(CACHED_PAYMENT_TERMS)); + $payment_terms = collect(config('ninja.payment_terms')); $new_terms = $this->terms->map(function($term) { return $term['num_days']; @@ -47,7 +47,7 @@ class CollectionMergingTest extends TestCase public function testSortingCollection() { - $payment_terms = collect(unserialize(CACHED_PAYMENT_TERMS)); + $payment_terms = collect(config('ninja.payment_terms')); $new_terms = $this->terms->map(function($term) { return $term['num_days']; @@ -65,7 +65,7 @@ class CollectionMergingTest extends TestCase public function testSortingCollectionLast() { - $payment_terms = collect(unserialize(CACHED_PAYMENT_TERMS)); + $payment_terms = collect(config('ninja.payment_terms')); $new_terms = $this->terms->map(function($term) { return $term['num_days']; diff --git a/tests/Unit/CompanySettingsTest.php b/tests/Unit/CompanySettingsTest.php index 35c0dad07a46..5b4d7db369e9 100644 --- a/tests/Unit/CompanySettingsTest.php +++ b/tests/Unit/CompanySettingsTest.php @@ -28,13 +28,6 @@ class CompanySettingsTest extends TestCase } - public function testCurrencyId() - { - - $this->assertEquals($this->company_settings->currency_id, 1); - - } - public function testLanguageId() { @@ -52,7 +45,7 @@ class CompanySettingsTest extends TestCase public function testPropertyIsSet() { - $this->assertTrue(isset($this->company_settings->currency_id)); + $this->assertTrue(isset($this->company_settings->timezone_id)); } } diff --git a/tests/Unit/CompareObjectTest.php b/tests/Unit/CompareObjectTest.php index 0f291e9c9c2e..a0c52f78681c 100644 --- a/tests/Unit/CompareObjectTest.php +++ b/tests/Unit/CompareObjectTest.php @@ -46,7 +46,6 @@ class CompareObjectTest extends TestCase $build_client_settings = $this->buildClientSettings(); $this->assertEquals($build_client_settings->timezone_id, 15); - $this->assertEquals($build_client_settings->currency_id, 1); $this->assertEquals($build_client_settings->language_id, 1); $this->assertEquals($build_client_settings->payment_terms, 1); } @@ -56,7 +55,6 @@ class CompareObjectTest extends TestCase $settings = ClientSettings::buildClientSettings(new CompanySettings(CompanySettings::defaults()), new ClientSettings(ClientSettings::defaults())); $this->assertEquals($settings->timezone_id, 15); - $this->assertEquals($settings->currency_id, 1); $this->assertEquals($settings->language_id, 1); $this->assertEquals($settings->payment_terms, 1); $this->assertEquals($settings->custom_taxes1, 'FALSE');