diff --git a/app/Http/Controllers/ChartController.php b/app/Http/Controllers/ChartController.php index bc0b254f8c27..aedb958f18a7 100644 --- a/app/Http/Controllers/ChartController.php +++ b/app/Http/Controllers/ChartController.php @@ -12,6 +12,7 @@ namespace App\Http\Controllers; use App\Http\Requests\Chart\ShowChartRequest; +use App\Services\Chart\ChartService; use Illuminate\Http\Request; use Illuminate\Http\Response; @@ -25,8 +26,8 @@ class ChartController extends BaseController /** * @OA\Post( - * path="/api/v1/charts", - * operationId="getCharts", + * path="/api/v1/charts/totals", + * operationId="getChartTotals", * tags={"charts"}, * summary="Get chart data", * description="Get chart data", @@ -67,10 +68,11 @@ class ChartController extends BaseController * @param Request $request * @return Response|mixed */ - public function index(ShowChartRequest $request) + public function totals(ShowChartRequest $request) { + $cs = new ChartService(auth()->user()->company()); - return response()->json([],200); + return response()->json($cs->totals($request->input('start_date'), $request->input('end_date')),200); } } diff --git a/app/Http/Requests/Chart/ShowChartRequest.php b/app/Http/Requests/Chart/ShowChartRequest.php index 0c510f1940ec..a817a3c035f5 100644 --- a/app/Http/Requests/Chart/ShowChartRequest.php +++ b/app/Http/Requests/Chart/ShowChartRequest.php @@ -29,6 +29,22 @@ class ShowChartRequest extends Request public function rules() { return [ + 'start_date' => 'date', + 'end_date' => 'date', ]; } + + protected function prepareForValidation() + { + $input = $this->all(); + + if(!array_key_exists('start_date', $input)) + $input['start_date'] = now()->subDays(20); + + if(!array_key_exists('end_date', $input)) + $input['end_date'] = now(); + + $this->replace($input); + + } } diff --git a/app/Services/Chart/ChartService.php b/app/Services/Chart/ChartService.php index 59c788285f80..320b766fca70 100644 --- a/app/Services/Chart/ChartService.php +++ b/app/Services/Chart/ChartService.php @@ -50,7 +50,7 @@ class ChartService ->where('company_id', $this->company->id) ->where('is_deleted', 0) ->distinct() - ->get(['currency_id']); + ->pluck('currency_id as id'); /* Merge and filter by unique */ $currencies = $currencies->merge($expense_currencies)->unique(); @@ -72,10 +72,11 @@ class ChartService - public function totals($start_date, $end_date) + public function totals($start_date, $end_date) :array { $data = []; + $data['currencies'] = $this->getCurrencyCodes(); $data['revenue'] = $this->getRevenue($start_date, $end_date); $data['outstanding'] = $this->getOutstanding($start_date, $end_date); $data['expenses'] = $this->getExpenses($start_date, $end_date); @@ -110,7 +111,7 @@ class ChartService } - private function getRevenue($start_date, $end_date) + private function getRevenue($start_date, $end_date) :array { $revenue = $this->getRevenueQuery($start_date, $end_date); $revenue = $this->parseTotals($revenue); @@ -119,7 +120,7 @@ class ChartService return $revenue; } - private function getOutstanding($start_date, $end_date) + private function getOutstanding($start_date, $end_date) :array { $outstanding = $this->getOutstandingQuery($start_date, $end_date); $outstanding = $this->parseTotals($outstanding); @@ -128,16 +129,15 @@ class ChartService return $outstanding; } - private function getExpenses($start_date, $end_date) + private function getExpenses($start_date, $end_date) :array { $expenses = $this->getExpenseQuery($start_date, $end_date); $expenses = $this->parseTotals($expenses); $expenses = $this->addCountryCodes($expenses); - return $expenses; } - private function parseTotals($data_set) + private function parseTotals($data_set) :array { /* Find the key where the company currency amount lives*/ $c_key = array_search($this->company->id , array_column($data_set, 'currency_id')); @@ -160,22 +160,25 @@ class ChartService } - private function addCountryCodes($data_set) + private function addCountryCodes($data_set) :array { $currencies = Cache::get('currencies'); foreach($data_set as $key => $value) { - $data_set[$key]['code'] = $this->getCode($currencies, $value); + $data_set[$key]->currency_id = str_replace('"', '', $value->currency_id); + $data_set[$key]->code = $this->getCode($currencies, $data_set[$key]->currency_id); } return $data_set; } - private function getCode($currencies, $currency_id) + private function getCode($currencies, $currency_id) :string { + nlog($currency_id); + $currency = $currencies->filter(function ($item) use($currency_id) { return $item->id == $currency_id; })->first(); diff --git a/routes/api.php b/routes/api.php index 796b50cc43bf..e0756307c8b7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -31,6 +31,9 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::get('activities', 'ActivityController@index'); Route::get('activities/download_entity/{activity}', 'ActivityController@downloadHistoricalEntity'); + + Route::post('charts/totals', 'ChartController@totals')->name('chart.totals'); + Route::post('claim_license', 'LicenseController@index')->name('license.index'); Route::resource('clients', 'ClientController'); // name = (clients. index / create / show / update / destroy / edit diff --git a/tests/Unit/Chart/ChartCurrencyTest.php b/tests/Unit/Chart/ChartCurrencyTest.php index 65171b4b2e7e..a18b07d3d1a6 100644 --- a/tests/Unit/Chart/ChartCurrencyTest.php +++ b/tests/Unit/Chart/ChartCurrencyTest.php @@ -12,8 +12,10 @@ namespace Tests\Unit\Chart; use App\DataMapper\ClientSettings; use App\Models\Client; +use App\Models\Invoice; use App\Services\Chart\ChartService; use App\Utils\Ninja; +use Illuminate\Foundation\Testing\DatabaseTransactions; use Tests\MockAccountData; use Tests\TestCase; @@ -24,6 +26,7 @@ use Tests\TestCase; class ChartCurrencyTest extends TestCase { use MockAccountData; + use DatabaseTransactions; public function setUp() :void { @@ -32,6 +35,42 @@ class ChartCurrencyTest extends TestCase $this->makeTestData(); } + public function testRevenueValues() + { + + Invoice::factory()->create([ + 'client_id' => $this->client->id, + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'paid_to_date' => 100, + 'status_id' => 4, + 'date' => now(), + 'due_date'=> now(), + 'number' => 'db_record' + ]); + + $this->assertDatabaseHas('invoices', ['number' => 'db_record']); + + $cs = new ChartService($this->company); + nlog($cs->getRevenueQuery(now()->subDays(20)->format('Y-m-d'), now()->addDays(100)->format('Y-m-d'))); + + $data = [ + 'start_date' => now()->subDays(30)->format('Y-m-d'), + 'end_date' => now()->addDay()->format('Y-m-d') + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/charts/totals', $data); + + $response->assertStatus(200); + + nlog($response->json()); + + } + + public function testgetCurrencyCodes() { $settings = ClientSettings::defaults(); @@ -61,6 +100,23 @@ class ChartCurrencyTest extends TestCase $this->assertFalse(in_array("AUD", $cs->getCurrencyCodes())); } + public function testGetChartTotalsApi() + { + + $data = [ + 'start_date' => now()->subDays(30)->format('Y-m-d'), + 'end_date' => now()->format('Y-m-d') + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/charts/totals', $data); + + $response->assertStatus(200); + + } + public function testClientServiceDataSetBuild() {