mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Client export - json output
This commit is contained in:
parent
80e66d9146
commit
427bc341fb
@ -17,6 +17,7 @@ use App\Models\Company;
|
||||
use App\Transformers\ClientContactTransformer;
|
||||
use App\Transformers\ClientTransformer;
|
||||
use App\Utils\Ninja;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use League\Csv\Writer;
|
||||
|
||||
@ -87,7 +88,27 @@ class ClientExport extends BaseExport
|
||||
$this->contact_transformer = new ClientContactTransformer();
|
||||
}
|
||||
|
||||
public function run()
|
||||
public function returnJson()
|
||||
{
|
||||
$query = $this->init();
|
||||
|
||||
$headerdisplay = $this->buildHeader();
|
||||
|
||||
$header = collect($this->input['report_keys'])->map(function ($key, $value) use($headerdisplay){
|
||||
return ['identifier' => $value, 'display_value' => $headerdisplay[$value]];
|
||||
})->toArray();
|
||||
|
||||
$report = $query->cursor()
|
||||
->map(function ($client) {
|
||||
return $this->buildRow($client);
|
||||
})->toArray();
|
||||
|
||||
return array_merge(['columns' => $header], $report);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function init(): Builder
|
||||
{
|
||||
MultiDB::setDb($this->company->db);
|
||||
App::forgetInstance('translator');
|
||||
@ -95,16 +116,10 @@ class ClientExport extends BaseExport
|
||||
$t = app('translator');
|
||||
$t->replace(Ninja::transformTranslations($this->company->settings));
|
||||
|
||||
//load the CSV document from a string
|
||||
$this->csv = Writer::createFromString();
|
||||
|
||||
if (count($this->input['report_keys']) == 0) {
|
||||
$this->input['report_keys'] = array_values($this->entity_keys);
|
||||
$this->input['report_keys'] = array_values($this->client_report_keys);
|
||||
}
|
||||
|
||||
//insert the header
|
||||
$this->csv->insertOne($this->buildHeader());
|
||||
|
||||
$query = Client::query()->with('contacts')
|
||||
->withTrashed()
|
||||
->where('company_id', $this->company->id)
|
||||
@ -112,6 +127,20 @@ class ClientExport extends BaseExport
|
||||
|
||||
$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 ($client) {
|
||||
$this->csv->insertOne($this->buildRow($client));
|
||||
|
@ -11,13 +11,14 @@
|
||||
|
||||
namespace App\Http\Controllers\Reports;
|
||||
|
||||
use App\Models\Client;
|
||||
use Illuminate\Http\Response;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Export\CSV\ClientExport;
|
||||
use App\Jobs\Report\SendToAdmin;
|
||||
use App\Jobs\Report\PreviewReport;
|
||||
use App\Http\Controllers\BaseController;
|
||||
use App\Http\Requests\Report\GenericReportRequest;
|
||||
use App\Jobs\Report\SendToAdmin;
|
||||
use App\Models\Client;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class ClientReportController extends BaseController
|
||||
{
|
||||
@ -63,14 +64,26 @@ class ClientReportController 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(), ClientExport::class, $this->filename);
|
||||
SendToAdmin::dispatch($user->company(), $request->all(), ClientExport::class, $this->filename);
|
||||
|
||||
return response()->json(['message' => 'working...'], 200);
|
||||
}
|
||||
// expect a list of visible fields, or use the default
|
||||
|
||||
$export = new ClientExport(auth()->user()->company(), $request->all());
|
||||
// expect a list of visible fields, or use the default
|
||||
if($request->has('output') && $request->input('output') == 'json') {
|
||||
|
||||
$hash = \Illuminate\Support\Str::uuid();
|
||||
|
||||
PreviewReport::dispatch($user->company(), $request->all(), ClientExport::class, $hash);
|
||||
|
||||
return response()->json(['message' => $hash], 200);
|
||||
}
|
||||
|
||||
$export = new ClientExport($user->company(), $request->all());
|
||||
|
||||
$csv = $export->run();
|
||||
|
||||
|
@ -59,21 +59,7 @@ class ReportApiTest extends TestCase
|
||||
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
|
@ -44,6 +44,39 @@ class ReportPreviewTest extends TestCase
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function testClientExportJson()
|
||||
{
|
||||
$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/clients?output=json', $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 testCreditExportPreview()
|
||||
{
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user