diff --git a/app/Filters/ClientFilters.php b/app/Filters/ClientFilters.php index c1b2c2f0595d..c3039605e090 100644 --- a/app/Filters/ClientFilters.php +++ b/app/Filters/ClientFilters.php @@ -70,6 +70,16 @@ class ClientFilters extends QueryFilters } + public function client_id(string $client_id = '') :Builder + { + if (strlen($client_id) == 0) { + return $this->builder; + } + + return $this->builder->where('id', $this->decodePrimaryKey($client_id)); + + } + public function id_number(string $id_number):Builder { return $this->builder->where('id_number', $id_number); diff --git a/app/Services/Chart/ChartQueries.php b/app/Services/Chart/ChartQueries.php index 5cfab1b692ea..9e96587260f6 100644 --- a/app/Services/Chart/ChartQueries.php +++ b/app/Services/Chart/ChartQueries.php @@ -19,6 +19,12 @@ use Illuminate\Support\Facades\DB; trait ChartQueries { + // $currencies = Payment::withTrashed() + // ->where('company_id', $this->company->id) + // ->where('is_deleted', 0) + // ->distinct() + // ->get(['currency_id']); + public function getRevenueQuery($start_date, $end_date) { diff --git a/app/Services/Chart/ChartService.php b/app/Services/Chart/ChartService.php index 7f9a61f3e048..59c788285f80 100644 --- a/app/Services/Chart/ChartService.php +++ b/app/Services/Chart/ChartService.php @@ -35,13 +35,6 @@ class ChartService */ public function getCurrencyCodes() :array { - - // $currencies = Payment::withTrashed() - // ->where('company_id', $this->company->id) - // ->where('is_deleted', 0) - // ->distinct() - // ->get(['currency_id']); - /* Get all the distinct client currencies */ $currencies = Client::withTrashed() ->where('company_id', $this->company->id) diff --git a/tests/Unit/Chart/ChartCurrencyTest.php b/tests/Unit/Chart/ChartCurrencyTest.php index ae55578cf6d1..65171b4b2e7e 100644 --- a/tests/Unit/Chart/ChartCurrencyTest.php +++ b/tests/Unit/Chart/ChartCurrencyTest.php @@ -10,6 +10,8 @@ */ namespace Tests\Unit\Chart; +use App\DataMapper\ClientSettings; +use App\Models\Client; use App\Services\Chart\ChartService; use App\Utils\Ninja; use Tests\MockAccountData; @@ -17,6 +19,7 @@ use Tests\TestCase; /** * @test + * @covers App\Services\Chart\ChartService */ class ChartCurrencyTest extends TestCase { @@ -29,80 +32,109 @@ class ChartCurrencyTest extends TestCase $this->makeTestData(); } - // public function testClientServiceDataSetBuild() - // { + public function testgetCurrencyCodes() + { + $settings = ClientSettings::defaults(); + $settings->currency_id = "1"; //USD - // $haystack = [ - // [ - // 'currency_id' => null, - // 'amount' => 10 - // ], - // [ - // 'currency_id' => 1, - // 'amount' => 11 - // ], - // [ - // 'currency_id' => 2, - // 'amount' => 12 - // ], - // [ - // 'currency_id' => 3, - // 'amount' => 13 - // ], - // ]; + Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'settings' => $settings, + ]); - // $cs = new ChartService($this->company); + $settings = ClientSettings::defaults(); + $settings->currency_id = "2"; //GBP - // nlog($cs->totals(now()->subYears(10), now())); + Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'settings' => $settings, + ]); - // $this->assertTrue(is_array($cs->totals(now()->subYears(10), now()))); + $cs = new ChartService($this->company); - // } + $this->assertTrue(is_array($cs->getCurrencyCodes())); - // /* coalesces the company currency with the null currencies */ - // public function testFindNullValueinArray() - // { + $this->assertTrue(in_array("GBP", $cs->getCurrencyCodes())); + $this->assertTrue(in_array("USD", $cs->getCurrencyCodes())); + $this->assertFalse(in_array("AUD", $cs->getCurrencyCodes())); + } - // $haystack = [ - // [ - // 'currency_id' => null, - // 'amount' => 10 - // ], - // [ - // 'currency_id' => 1, - // 'amount' => 11 - // ], - // [ - // 'currency_id' => 2, - // 'amount' => 12 - // ], - // [ - // 'currency_id' => 3, - // 'amount' => 13 - // ], - // ]; + public function testClientServiceDataSetBuild() + { - // $company_currency_id = 1; + $haystack = [ + [ + 'currency_id' => null, + 'amount' => 10 + ], + [ + 'currency_id' => 1, + 'amount' => 11 + ], + [ + 'currency_id' => 2, + 'amount' => 12 + ], + [ + 'currency_id' => 3, + 'amount' => 13 + ], + ]; - // $c_key = array_search($company_currency_id , array_column($haystack, 'currency_id')); + $cs = new ChartService($this->company); - // $this->assertNotEquals($c_key, 2); - // $this->assertEquals($c_key, 1); + nlog($cs->totals(now()->subYears(10), now())); - // $key = array_search(null , array_column($haystack, 'currency_id')); + $this->assertTrue(is_array($cs->totals(now()->subYears(10), now()))); - // $this->assertNotEquals($key, 39); - // $this->assertEquals($key, 0); + } - // $null_currency_amount = $haystack[$key]['amount']; + /* coalesces the company currency with the null currencies */ + public function testFindNullValueinArray() + { - // unset($haystack[$key]); + $haystack = [ + [ + 'currency_id' => null, + 'amount' => 10 + ], + [ + 'currency_id' => 1, + 'amount' => 11 + ], + [ + 'currency_id' => 2, + 'amount' => 12 + ], + [ + 'currency_id' => 3, + 'amount' => 13 + ], + ]; - // $haystack[$c_key]['amount'] += $null_currency_amount; + $company_currency_id = 1; - // $this->assertEquals($haystack[$c_key]['amount'], 21); + $c_key = array_search($company_currency_id , array_column($haystack, 'currency_id')); - // } + $this->assertNotEquals($c_key, 2); + $this->assertEquals($c_key, 1); + + $key = array_search(null , array_column($haystack, 'currency_id')); + + $this->assertNotEquals($key, 39); + $this->assertEquals($key, 0); + + $null_currency_amount = $haystack[$key]['amount']; + + unset($haystack[$key]); + + $haystack[$c_key]['amount'] += $null_currency_amount; + + $this->assertEquals($haystack[$c_key]['amount'], 21); + + } public function testCollectionMerging()