mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 07:34:35 -04:00
Activity json export
This commit is contained in:
parent
9735277eb1
commit
80e66d9146
@ -20,6 +20,7 @@ use App\Libraries\MultiDB;
|
|||||||
use App\Models\DateFormat;
|
use App\Models\DateFormat;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\App;
|
use Illuminate\Support\Facades\App;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use App\Transformers\ActivityTransformer;
|
use App\Transformers\ActivityTransformer;
|
||||||
|
|
||||||
class ActivityExport extends BaseExport
|
class ActivityExport extends BaseExport
|
||||||
@ -39,10 +40,6 @@ class ActivityExport extends BaseExport
|
|||||||
'address' => 'address',
|
'address' => 'address',
|
||||||
];
|
];
|
||||||
|
|
||||||
private array $decorate_keys = [
|
|
||||||
|
|
||||||
];
|
|
||||||
|
|
||||||
public function __construct(Company $company, array $input)
|
public function __construct(Company $company, array $input)
|
||||||
{
|
{
|
||||||
$this->company = $company;
|
$this->company = $company;
|
||||||
@ -50,45 +47,27 @@ class ActivityExport extends BaseExport
|
|||||||
$this->entity_transformer = new ActivityTransformer();
|
$this->entity_transformer = new ActivityTransformer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run()
|
public function returnJson()
|
||||||
{
|
{
|
||||||
MultiDB::setDb($this->company->db);
|
$query = $this->init();
|
||||||
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;
|
$headerdisplay = $this->buildHeader();
|
||||||
|
|
||||||
//load the CSV document from a string
|
$header = collect($this->input['report_keys'])->map(function ($key, $value) use($headerdisplay){
|
||||||
$this->csv = Writer::createFromString();
|
return ['identifier' => $value, 'display_value' => $headerdisplay[$value]];
|
||||||
|
})->toArray();
|
||||||
|
|
||||||
ksort($this->entity_keys);
|
$report = $query->cursor()
|
||||||
|
->map(function ($credit) {
|
||||||
if (count($this->input['report_keys']) == 0) {
|
return $this->buildActivityRow($credit);
|
||||||
$this->input['report_keys'] = array_values($this->entity_keys);
|
})->toArray();
|
||||||
}
|
|
||||||
|
return array_merge(['columns' => $header], $report);
|
||||||
//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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildRow(Activity $activity)
|
private function buildActivityRow(Activity $activity): array
|
||||||
{
|
{
|
||||||
|
return [
|
||||||
$this->csv->insertOne([
|
|
||||||
Carbon::parse($activity->created_at)->format($this->date_format),
|
Carbon::parse($activity->created_at)->format($this->date_format),
|
||||||
ctrans("texts.activity_{$activity->activity_type_id}",[
|
ctrans("texts.activity_{$activity->activity_type_id}",[
|
||||||
'client' => $activity->client ? $activity->client->present()->name() : '',
|
'client' => $activity->client ? $activity->client->present()->name() : '',
|
||||||
@ -108,7 +87,57 @@ class ActivityExport extends BaseExport
|
|||||||
'recurring_expense' => $activity->recurring_expense ? $activity->recurring_expense->number : '',
|
'recurring_expense' => $activity->recurring_expense ? $activity->recurring_expense->number : '',
|
||||||
]),
|
]),
|
||||||
$activity->ip,
|
$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));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ namespace App\Http\Controllers\Reports;
|
|||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use App\Jobs\Report\SendToAdmin;
|
use App\Jobs\Report\SendToAdmin;
|
||||||
use App\Export\CSV\ActivityExport;
|
use App\Export\CSV\ActivityExport;
|
||||||
|
use App\Jobs\Report\PreviewReport;
|
||||||
use App\Http\Controllers\BaseController;
|
use App\Http\Controllers\BaseController;
|
||||||
use App\Http\Requests\Report\GenericReportRequest;
|
use App\Http\Requests\Report\GenericReportRequest;
|
||||||
|
|
||||||
@ -31,14 +32,27 @@ class ActivityReportController extends BaseController
|
|||||||
|
|
||||||
public function __invoke(GenericReportRequest $request)
|
public function __invoke(GenericReportRequest $request)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/** @var \App\Models\User $user */
|
||||||
|
$user = auth()->user();
|
||||||
|
|
||||||
if ($request->has('send_email') && $request->get('send_email')) {
|
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);
|
return response()->json(['message' => 'working...'], 200);
|
||||||
}
|
}
|
||||||
// expect a list of visible fields, or use the default
|
// 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();
|
$csv = $export->run();
|
||||||
|
|
||||||
|
@ -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()
|
public function testUserSalesReportApiRoute()
|
||||||
{
|
{
|
||||||
$data = [
|
$data = [
|
||||||
@ -125,6 +158,8 @@ class ReportApiTest extends TestCase
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function testClientBalanceReportApiRoute()
|
public function testClientBalanceReportApiRoute()
|
||||||
{
|
{
|
||||||
$data = [
|
$data = [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user