mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Report previews
This commit is contained in:
parent
2900db2e90
commit
2752a2e651
@ -16,6 +16,7 @@ use App\Models\Company;
|
||||
use App\Models\Credit;
|
||||
use App\Transformers\CreditTransformer;
|
||||
use App\Utils\Ninja;
|
||||
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use League\Csv\Writer;
|
||||
|
||||
@ -80,14 +81,40 @@ class CreditExport extends BaseExport
|
||||
$this->credit_transformer = new CreditTransformer();
|
||||
}
|
||||
|
||||
public function run()
|
||||
public function returnJson(string $hash)
|
||||
{
|
||||
$query = $this->init();
|
||||
|
||||
$header = $this->buildHeader();
|
||||
|
||||
$report = $query->cursor()
|
||||
->map(function ($credit) {
|
||||
return $this->buildRow($credit);
|
||||
})->toJson();
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
$query = Credit::query()
|
||||
->withTrashed()
|
||||
->with('client')->where('company_id', $this->company->id)
|
||||
->where('is_deleted', 0);
|
||||
|
||||
$query = $this->addDateRange($query);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$query = $this->init();
|
||||
//load the CSV document from a string
|
||||
$this->csv = Writer::createFromString();
|
||||
|
||||
@ -98,12 +125,7 @@ class CreditExport extends BaseExport
|
||||
//insert the header
|
||||
$this->csv->insertOne($this->buildHeader());
|
||||
|
||||
$query = Credit::query()
|
||||
->withTrashed()
|
||||
->with('client')->where('company_id', $this->company->id)
|
||||
->where('is_deleted', 0);
|
||||
|
||||
$query = $this->addDateRange($query);
|
||||
|
||||
|
||||
$query->cursor()
|
||||
->each(function ($credit) {
|
||||
|
@ -78,7 +78,9 @@ class EmailController extends BaseController
|
||||
$entity_obj->service()->markSent()->save();
|
||||
|
||||
$mo->invitation_id = $invitation->id;
|
||||
|
||||
$mo->client_id = $invitation->contact->client_id ?? null;
|
||||
$mo->vendor_id = $invitation->contact->vendor_id ?? null;
|
||||
|
||||
Email::dispatch($mo, $invitation->company);
|
||||
}
|
||||
});
|
||||
|
@ -62,14 +62,26 @@ class CreditReportController 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(), CreditExport::class, $this->filename);
|
||||
SendToAdmin::dispatch($user->company(), $request->all(), CreditExport::class, $this->filename);
|
||||
|
||||
return response()->json(['message' => 'working...'], 200);
|
||||
}
|
||||
// expect a list of visible fields, or use the default
|
||||
|
||||
$export = new CreditExport(auth()->user()->company(), $request->all());
|
||||
$export = new CreditExport($user->company(), $request->all());
|
||||
|
||||
if($request->has('output') && $request->input('output') == 'json') {
|
||||
|
||||
$hash = \Illuminate\Support\Str::uuid();
|
||||
|
||||
$data = $export->returnJson($hash);
|
||||
|
||||
return response()->json(['message' => $hash], 200);
|
||||
}
|
||||
|
||||
$csv = $export->run();
|
||||
|
||||
|
47
app/Http/Controllers/Reports/ReportPreviewController.php
Normal file
47
app/Http/Controllers/Reports/ReportPreviewController.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Http\Controllers\Reports;
|
||||
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use App\Http\Controllers\BaseController;
|
||||
use App\Http\Requests\Report\ReportPreviewRequest;
|
||||
|
||||
class ReportPreviewController extends BaseController
|
||||
{
|
||||
use MakesHash;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function __invoke(ReportPreviewRequest $request, ?string $hash)
|
||||
{
|
||||
/** @var \App\Models\User $user */
|
||||
$user = auth()->user();
|
||||
|
||||
$report = Cache::get($hash);
|
||||
|
||||
if(!$report)
|
||||
return response()->json(['message' => 'Still working.....'], 409);
|
||||
|
||||
if($report){
|
||||
|
||||
Cache::forget($hash);
|
||||
|
||||
return response()->json($report, 200);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
42
app/Http/Requests/Report/ReportPreviewRequest.php
Normal file
42
app/Http/Requests/Report/ReportPreviewRequest.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Http\Requests\Report;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
|
||||
class ReportPreviewRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize() : bool
|
||||
{
|
||||
/** @var \App\Models\User $user */
|
||||
$user = auth()->user();
|
||||
|
||||
return $user->isAdmin() || $user->hasPermission('view_reports');
|
||||
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'hash' => 'bail|required|string',
|
||||
];
|
||||
}
|
||||
|
||||
public function prepareForValidation()
|
||||
{
|
||||
}
|
||||
}
|
@ -93,6 +93,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
});
|
||||
|
||||
Mailer::macro('postmark_config', function (string $postmark_key) {
|
||||
// @phpstan-ignore /** @phpstan-ignore-next-line **/
|
||||
Mailer::setSymfonyTransport(app('mail.manager')->createSymfonyTransport([
|
||||
'transport' => 'postmark',
|
||||
'token' => $postmark_key
|
||||
@ -101,8 +102,10 @@ class AppServiceProvider extends ServiceProvider
|
||||
return $this;
|
||||
});
|
||||
|
||||
|
||||
Mailer::macro('mailgun_config', function (string $secret, string $domain, string $endpoint = 'api.mailgun.net') {
|
||||
Mailer::setSymfonyTransport(app('mail.manager')->createSymfonyTransport([
|
||||
// @phpstan-ignore /** @phpstan-ignore-next-line **/
|
||||
Mailer::setSymfonyTransport(app('mail.manager')->createSymfonyTransport([
|
||||
'transport' => 'mailgun',
|
||||
'secret' => $secret,
|
||||
'domain' => $domain,
|
||||
|
@ -138,7 +138,9 @@ class Email implements ShouldQueue
|
||||
|
||||
$this->email_object->company = $this->company;
|
||||
|
||||
$this->email_object->client_id ? $this->email_object->settings = $this->email_object->client->getMergedSettings() : $this->email_object->settings = $this->company->settings;
|
||||
$this->email_object->client_id ? $this->email_object->settings = $this->email_object->client->getMergedSettings() : $this->email_object->settings = $this->company->settings;
|
||||
|
||||
$this->email_object->client_id ? nlog("client settings") : nlog("company settings ");
|
||||
|
||||
$this->email_object->whitelabel = $this->company->account->isPaid() ? true : false;
|
||||
|
||||
|
@ -30,6 +30,7 @@ class TaskTransformer extends EntityTransformer
|
||||
|
||||
protected $defaultIncludes = [
|
||||
'documents',
|
||||
'project',
|
||||
];
|
||||
|
||||
/**
|
||||
|
10
phpstan.neon
10
phpstan.neon
@ -1,18 +1,22 @@
|
||||
includes:
|
||||
- ./vendor/nunomaduro/larastan/extension.neon
|
||||
- ./vendor/spaze/phpstan-stripe/extension.neon
|
||||
- phpstan-baseline.neon
|
||||
parameters:
|
||||
level: 2
|
||||
paths:
|
||||
- 'app/'
|
||||
- app
|
||||
excludePaths:
|
||||
- 'vendor/'
|
||||
- 'resources/views/*'
|
||||
- 'vendor/*'
|
||||
- '../resources/*'
|
||||
- resources/
|
||||
- resources/*
|
||||
- 'app/Jobs/Ninja/*'
|
||||
- 'app/Models/Presenters/*'
|
||||
- 'app/Console/Commands/*'
|
||||
- 'app/DataMapper/Analytics/*'
|
||||
- 'app/PaymentDrivers/Authorize/*'
|
||||
- 'app/PaymentDrivers/AuthorizePaymentDriver.php'
|
||||
- 'app/Utils/Traits/*'
|
||||
universalObjectCratesClasses:
|
||||
- App\DataMapper\Tax\RuleInterface
|
||||
|
@ -312,7 +312,9 @@ Route::group(['middleware' => ['throttle:api', 'api_db', 'token_auth', 'locale']
|
||||
Route::post('reports/client_sales_report', ClientSalesReportController::class);
|
||||
Route::post('reports/tax_summary_report', TaxSummaryReportController::class);
|
||||
Route::post('reports/user_sales_report', UserSalesReportController::class);
|
||||
|
||||
Route::post('reports/preview/{hash}', UserSalesReportController::class);
|
||||
|
||||
|
||||
Route::resource('task_schedulers', TaskSchedulerController::class);
|
||||
Route::post('task_schedulers/bulk', [TaskSchedulerController::class, 'bulk'])->name('task_schedulers.bulk');
|
||||
|
||||
|
@ -674,6 +674,38 @@ class ReportCsvGenerationTest extends TestCase
|
||||
|
||||
}
|
||||
|
||||
public function testCreditJsonReport()
|
||||
{
|
||||
|
||||
// Credit::factory()->create([
|
||||
// 'user_id' => $this->user->id,
|
||||
// 'company_id' => $this->company->id,
|
||||
// 'client_id' => $this->client->id,
|
||||
// 'amount' => 100,
|
||||
// 'balance' => 50,
|
||||
// 'number' => '1234',
|
||||
// 'status_id' => 2,
|
||||
// 'discount' => 10,
|
||||
// 'po_number' => '1234',
|
||||
// 'public_notes' => 'Public',
|
||||
// 'private_notes' => 'Private',
|
||||
// 'terms' => 'Terms',
|
||||
// ]);
|
||||
|
||||
// $data = [
|
||||
// 'date_range' => 'all',
|
||||
// 'report_keys' => ["client.name","credit.number","credit.amount","payment.date", "payment.amount"],
|
||||
// 'send_email' => false,
|
||||
// ];
|
||||
|
||||
// $response = $this->withHeaders([
|
||||
// 'X-API-SECRET' => config('ninja.api_secret'),
|
||||
// 'X-API-TOKEN' => $this->token,
|
||||
// ])->post('/api/v1/reports/credits?output=json', $data);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function testCreditCustomColumnsCsvGeneration()
|
||||
{
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user