diff --git a/app/Export/CSV/ActivityExport.php b/app/Export/CSV/ActivityExport.php index 21aa0ecb0979..4ae02fb63e4e 100644 --- a/app/Export/CSV/ActivityExport.php +++ b/app/Export/CSV/ActivityExport.php @@ -20,6 +20,7 @@ use App\Libraries\MultiDB; use App\Models\DateFormat; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\App; +use Illuminate\Database\Eloquent\Builder; use App\Transformers\ActivityTransformer; class ActivityExport extends BaseExport @@ -39,10 +40,6 @@ class ActivityExport extends BaseExport 'address' => 'address', ]; - private array $decorate_keys = [ - - ]; - public function __construct(Company $company, array $input) { $this->company = $company; @@ -50,45 +47,27 @@ class ActivityExport extends BaseExport $this->entity_transformer = new ActivityTransformer(); } - public function run() + public function returnJson() { - MultiDB::setDb($this->company->db); - App::forgetInstance('translator'); - App::setLocale($this->company->locale()); - $t = app('translator'); - $t->replace(Ninja::transformTranslations($this->company->settings)); + $query = $this->init(); - $this->date_format = DateFormat::find($this->company->settings->date_format_id)->format; + $headerdisplay = $this->buildHeader(); - //load the CSV document from a string - $this->csv = Writer::createFromString(); + $header = collect($this->input['report_keys'])->map(function ($key, $value) use($headerdisplay){ + return ['identifier' => $value, 'display_value' => $headerdisplay[$value]]; + })->toArray(); - ksort($this->entity_keys); - - if (count($this->input['report_keys']) == 0) { - $this->input['report_keys'] = array_values($this->entity_keys); - } - - //insert the header - $this->csv->insertOne($this->buildHeader()); - - $query = Activity::query() - ->where('company_id', $this->company->id); - - $query = $this->addDateRange($query); - - $query->cursor() - ->each(function ($entity) { - $this->buildRow($entity); - }); - - return $this->csv->toString(); + $report = $query->cursor() + ->map(function ($credit) { + return $this->buildActivityRow($credit); + })->toArray(); + + return array_merge(['columns' => $header], $report); } - private function buildRow(Activity $activity) + private function buildActivityRow(Activity $activity): array { - - $this->csv->insertOne([ + return [ Carbon::parse($activity->created_at)->format($this->date_format), ctrans("texts.activity_{$activity->activity_type_id}",[ 'client' => $activity->client ? $activity->client->present()->name() : '', @@ -108,7 +87,57 @@ class ActivityExport extends BaseExport 'recurring_expense' => $activity->recurring_expense ? $activity->recurring_expense->number : '', ]), $activity->ip, - ]); + ]; + + } + + private function init(): Builder + { + MultiDB::setDb($this->company->db); + App::forgetInstance('translator'); + App::setLocale($this->company->locale()); + $t = app('translator'); + $t->replace(Ninja::transformTranslations($this->company->settings)); + + $this->date_format = DateFormat::find($this->company->settings->date_format_id)->format; + + ksort($this->entity_keys); + + if (count($this->input['report_keys']) == 0) { + $this->input['report_keys'] = array_values($this->entity_keys); + } + + $query = Activity::query() + ->where('company_id', $this->company->id); + + $query = $this->addDateRange($query); + + return $query; + } + + public function run() + { + $query = $this->init(); + + //load the CSV document from a string + $this->csv = Writer::createFromString(); + + //insert the header + $this->csv->insertOne($this->buildHeader()); + + + $query->cursor() + ->each(function ($entity) { + $this->buildRow($entity); + }); + + return $this->csv->toString(); + } + + private function buildRow(Activity $activity) + { + + $this->csv->insertOne($this->buildActivityRow($activity)); } diff --git a/app/Http/Controllers/Reports/ActivityReportController.php b/app/Http/Controllers/Reports/ActivityReportController.php index 24e1feb2e587..ed241a9e38f2 100644 --- a/app/Http/Controllers/Reports/ActivityReportController.php +++ b/app/Http/Controllers/Reports/ActivityReportController.php @@ -14,6 +14,7 @@ namespace App\Http\Controllers\Reports; use App\Utils\Traits\MakesHash; use App\Jobs\Report\SendToAdmin; use App\Export\CSV\ActivityExport; +use App\Jobs\Report\PreviewReport; use App\Http\Controllers\BaseController; use App\Http\Requests\Report\GenericReportRequest; @@ -31,14 +32,27 @@ class ActivityReportController extends BaseController public function __invoke(GenericReportRequest $request) { + + /** @var \App\Models\User $user */ + $user = auth()->user(); + if ($request->has('send_email') && $request->get('send_email')) { - SendToAdmin::dispatch(auth()->user()->company(), $request->all(), ActivityExport::class, $this->filename); + SendToAdmin::dispatch($user->company(), $request->all(), ActivityExport::class, $this->filename); return response()->json(['message' => 'working...'], 200); } // expect a list of visible fields, or use the default - $export = new ActivityExport(auth()->user()->company(), $request->all()); + if($request->has('output') && $request->input('output') == 'json') { + + $hash = \Illuminate\Support\Str::uuid(); + + PreviewReport::dispatch($user->company(), $request->all(), ActivityExport::class, $hash); + + return response()->json(['message' => $hash], 200); + } + + $export = new ActivityExport($user->company(), $request->all()); $csv = $export->run(); diff --git a/tests/Feature/Export/ReportApiTest.php b/tests/Feature/Export/ReportApiTest.php index 14fd1f57297b..a6f45b825e3a 100644 --- a/tests/Feature/Export/ReportApiTest.php +++ b/tests/Feature/Export/ReportApiTest.php @@ -42,6 +42,39 @@ class ReportApiTest extends TestCase } + + public function testActivityCSVExport() + { + $data = [ + 'send_email' => false, + 'date_range' => 'all', + 'report_keys' => [], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/reports/activities', $data) + ->assertStatus(200); + + } + + public function testActivityCSVExportJson() + { + $data = [ + 'send_email' => false, + 'date_range' => 'all', + 'report_keys' => [], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/reports/activities?output=json', $data) + ->assertStatus(200); + + } + public function testUserSalesReportApiRoute() { $data = [ @@ -125,6 +158,8 @@ class ReportApiTest extends TestCase } + + public function testClientBalanceReportApiRoute() { $data = [