1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Additional exports

This commit is contained in:
David Bomba 2023-08-28 07:41:02 +10:00
parent 427bc341fb
commit ac8fffcdc2
3 changed files with 129 additions and 61 deletions

View File

@ -18,6 +18,7 @@ use App\Models\Company;
use App\Transformers\ClientContactTransformer;
use App\Transformers\ClientTransformer;
use App\Utils\Ninja;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
@ -32,54 +33,6 @@ class ContactExport extends BaseExport
public string $date_key = 'created_at';
public array $entity_keys = [
'address1' => 'client.address1',
'address2' => 'client.address2',
'balance' => 'client.balance',
'city' => 'client.city',
'country' => 'client.country_id',
'credit_balance' => 'client.credit_balance',
'custom_value1' => 'client.custom_value1',
'custom_value2' => 'client.custom_value2',
'custom_value3' => 'client.custom_value3',
'custom_value4' => 'client.custom_value4',
'id_number' => 'client.id_number',
'industry' => 'client.industry_id',
'last_login' => 'client.last_login',
'name' => 'client.name',
'number' => 'client.number',
'paid_to_date' => 'client.paid_to_date',
'client_phone' => 'client.phone',
'postal_code' => 'client.postal_code',
'private_notes' => 'client.private_notes',
'public_notes' => 'client.public_notes',
'shipping_address1' => 'client.shipping_address1',
'shipping_address2' => 'client.shipping_address2',
'shipping_city' => 'client.shipping_city',
'shipping_country' => 'client.shipping_country_id',
'shipping_postal_code' => 'client.shipping_postal_code',
'shipping_state' => 'client.shipping_state',
'state' => 'client.state',
'vat_number' => 'client.vat_number',
'website' => 'client.website',
'currency' => 'client.currency',
'first_name' => 'contact.first_name',
'last_name' => 'contact.last_name',
'contact_phone' => 'contact.phone',
'contact_custom_value1' => 'contact.custom_value1',
'contact_custom_value2' => 'contact.custom_value2',
'contact_custom_value3' => 'contact.custom_value3',
'contact_custom_value4' => 'contact.custom_value4',
'email' => 'contact.email',
];
private array $decorate_keys = [
'client.country_id',
'client.shipping_country_id',
'client.currency',
'client.industry',
];
public function __construct(Company $company, array $input)
{
$this->company = $company;
@ -88,29 +41,39 @@ class ContactExport extends BaseExport
$this->contact_transformer = new ClientContactTransformer();
}
public function run()
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));
//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 = ClientContact::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 ($contact) {
$this->csv->insertOne($this->buildRow($contact));
});
@ -118,6 +81,26 @@ class ContactExport extends BaseExport
return $this->csv->toString();
}
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 ($contact) {
return $this->buildRow($contact);
})->toArray();
return array_merge(['columns' => $header], $report);
}
private function buildRow(ClientContact $contact) :array
{
$transformed_contact = false;

View File

@ -11,12 +11,13 @@
namespace App\Http\Controllers\Reports;
use Illuminate\Http\Response;
use App\Utils\Traits\MakesHash;
use App\Jobs\Report\SendToAdmin;
use App\Export\CSV\ContactExport;
use App\Jobs\Report\PreviewReport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\GenericReportRequest;
use App\Jobs\Report\SendToAdmin;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Response;
class ClientContactReportController extends BaseController
{
@ -62,13 +63,29 @@ class ClientContactReportController 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(), ContactExport::class, $this->filename);
SendToAdmin::dispatch($user->company(), $request->all(), ContactExport::class, $this->filename);
return response()->json(['message' => 'working...'], 200);
}
// expect a list of visible fields, or use the default
$export = new ContactExport(auth()->user()->company(), $request->all());
if($request->has('output') && $request->input('output') == 'json') {
$hash = \Illuminate\Support\Str::uuid();
PreviewReport::dispatch($user->company(), $request->all(), ContactExport::class, $hash);
return response()->json(['message' => $hash], 200);
}
// expect a list of visible fields, or use the default
$export = new ContactExport($user->company(), $request->all());
$csv = $export->run();

View File

@ -11,11 +11,15 @@
namespace Tests\Feature\Export;
use App\Export\CSV\ActivityExport;
use Tests\TestCase;
use Tests\MockAccountData;
use App\Utils\Traits\MakesHash;
use App\Export\CSV\ClientExport;
use App\Export\CSV\CreditExport;
use App\Export\CSV\ContactExport;
use App\Jobs\Report\PreviewReport;
use Illuminate\Support\Facades\Cache;
use Illuminate\Routing\Middleware\ThrottleRequests;
/**
@ -59,6 +63,56 @@ class ReportPreviewTest extends TestCase
])->postJson('/api/v1/reports/clients?output=json', $data)
->assertStatus(200);
$data = [
'send_email' => false,
'date_range' => 'all',
'report_keys' => ['client.name','client.balance'],
];
$p = (new PreviewReport($this->company, $data, ClientExport::class, 'client_export1'))->handle();
$this->assertNull($p);
$r = Cache::pull('client_export1');
$this->assertNotNull($r);
}
public function testClientContactExportJsonLimitedKeys()
{
$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/client_contacts?output=json', $data)
->assertStatus(200);
$data = [
'send_email' => false,
'date_range' => 'all',
'report_keys' => ['client.name','client.balance','contact.email'],
];
$p = (new PreviewReport($this->company, $data, ContactExport::class, '123'))->handle();
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
nlog($r);
}
public function testActivityCSVExportJson()
@ -75,6 +129,16 @@ class ReportPreviewTest extends TestCase
])->postJson('/api/v1/reports/activities?output=json', $data)
->assertStatus(200);
$p = (new PreviewReport($this->company, $data, ActivityExport::class, '123'))->handle();
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
}
public function testCreditExportPreview()
@ -90,6 +154,10 @@ class ReportPreviewTest extends TestCase
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
}
public function testCreditPreview()