Skip to content

Commit

Permalink
chore: reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
frknasir committed Aug 24, 2023
1 parent 36a9f49 commit 6814b3c
Show file tree
Hide file tree
Showing 33 changed files with 1,576 additions and 0 deletions.
54 changes: 54 additions & 0 deletions config/instrument.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
'update' => null,
'destroy' => '/',
],
'reports' => [
'store' => null,
'update' => null,
'destroy' => '/',
],
],

'route_names' => [
Expand Down Expand Up @@ -77,5 +82,54 @@
'update' => 'transactions.update',
'destroy' => 'transactions.destroy',
],
'reports' => [
'store' => 'reports.store',
'update' => 'reports.update',
'destroy' => 'reports.destroy',
],
],

/*
* The types
*/
'report_types' => [
'expense' => Expense::class,

Check failure on line 96 in config/instrument.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Expense not found.
'income' => Income::class,

Check failure on line 97 in config/instrument.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Income not found.
'profit_and_loss' => ProfitLoss::class,

Check failure on line 98 in config/instrument.php

View workflow job for this annotation

GitHub Actions / phpstan

Class ProfitLoss not found.
'tax' => Tax::class,

Check failure on line 99 in config/instrument.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Tax not found.
],

/**
* Group Options.
*/
'report_groups' => [
'category',
'customer',
'item',
],

/**
* Period Options.
*/
'report_periods' => [
'monthly',
'quarterly',
'yearly',
],

/**
* Basis Options.
*/
'report_accounting_basis' => [
'cash',
'accrual',
],

/**
* Chart Options.
*/
'report_charts' => [
// 'none',
// 'line',
],
];
19 changes: 19 additions & 0 deletions database/migrations/create_instrument_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ return new class extends Migration
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('team_id')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->string('type');
$table->string('name');
$table->json('meta');
Expand All @@ -33,6 +34,7 @@ return new class extends Migration
Schema::create('taxes', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('team_id')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->string('type')->nullable();
$table->string('name');
$table->double('rate', 15, 8, true);
Expand All @@ -54,6 +56,7 @@ return new class extends Migration
Schema::create('currencies', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('team_id')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->string('name');
$table->string('code');
$table->double('rate', 15, 8);
Expand Down Expand Up @@ -81,6 +84,7 @@ return new class extends Migration
$table->id();

$table->unsignedBigInteger('team_id')->nullable();
$table->unsignedBigInteger('user_id')->nullable();

// add fields
$table->unsignedBigInteger('parent_id')->nullable();
Expand All @@ -107,6 +111,7 @@ return new class extends Migration
$table->id();

$table->unsignedBigInteger('team_id')->nullable();
$table->unsignedBigInteger('user_id')->nullable();

$table->string('name');
$table->string('number');
Expand Down Expand Up @@ -139,6 +144,7 @@ return new class extends Migration
$table->id();

$table->unsignedBigInteger('team_id')->nullable();
$table->unsignedBigInteger('user_id')->nullable();

$table->unsignedBigInteger('account_id')->nullable();
$table->unsignedBigInteger('document_id')->nullable();
Expand All @@ -152,6 +158,18 @@ return new class extends Migration

$table->timestamps();
});

Schema::create('reports', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('team_id')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->string('type');
$table->string('name');
$table->string('description')->nullable();
$table->json('settings')->nullable();
$table->timestamps();
$table->softDeletes();
});
}

/**
Expand All @@ -168,5 +186,6 @@ return new class extends Migration
Schema::dropIfExists('documents');
Schema::dropIfExists('accounts');
Schema::dropIfExists('transactions');
Schema::dropIfExists('reports');
}
};
8 changes: 8 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,12 @@
'update' => config('instrument.route_names.transactions.update', 'transactions.update'),
'destroy' => config('instrument.route_names.transactions.destroy', 'transactions.destroy'),
]);

Route::resource('reports', Controllers\ReportController::class)
->only(['store', 'update', 'destroy'])
->names([
'store' => config('instrument.route_names.reports.store', 'reports.store'),
'update' => config('instrument.route_names.reports.update', 'reports.update'),
'destroy' => config('instrument.route_names.reports.destroy', 'reports.destroy'),
]);
});
47 changes: 47 additions & 0 deletions src/Actions/CreateReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Instrument\Actions;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;
use Instrument\Report;
use Instrument\Contracts\CreatesReports;
use Instrument\Events\ReportCreated;
use Instrument\Events\CreatingReport;
use Instrument\Instrument;

class CreateReport implements CreatesReports
{
/**
* Create a new report.
*/
public function __invoke(Model $user, array $data, $teamId = null): Report
{
event(new CreatingReport(user: $user, data: $data));

Validator::make($data, [
'type' => 'required|string|max:255|in:'.implode(',', collect(config('instrument.report_types'))->keys()->toArray()),
'name' => 'required|string|max:255',
'description' => 'required|string|max:255',
'settings' => 'nullable|array',
'settings.group' => 'nullable|string|max:255|in:'.implode(',', config('instrument.report_groups')),
'settings.period' => 'nullable|string|max:255|in:'.implode(',', config('instrument.report_periods')),
'settings.basis' => 'nullable|string|max:255|in:'.implode(',', config('instrument.report_accounting_basis')),
])->validateWithBag('createReport');

$fields = collect($data)->only([
'type',
'name',
'description',
'settings',
])->toArray();

$report = Instrument::$supportsTeams ?
Instrument::findTeamByIdOrFail($teamId)->reports()->create($fields) :
Instrument::newReportModel()->create($fields);

event(new ReportCreated(user: $user, report: $report, data: $data));

return $report;
}
}
24 changes: 24 additions & 0 deletions src/Actions/DeleteReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Instrument\Actions;

use Illuminate\Database\Eloquent\Model;
use Instrument\Report;
use Instrument\Contracts\DeletesReports;
use Instrument\Events\ReportDeleted;
use Instrument\Events\DeletingReport;

class DeleteReport implements DeletesReports
{
/**
* Delete a report.
*/
public function __invoke(Model $user, Report $report): void
{
event(new DeletingReport($user, report: $report));

$report->delete();

event(new ReportDeleted(user: $user, report: $report));
}
}
40 changes: 40 additions & 0 deletions src/Actions/UpdateReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Instrument\Actions;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;
use Instrument\Report;
use Instrument\Contracts\UpdatesReports;
use Instrument\Events\ReportUpdated;
use Instrument\Events\UpdatingReport;

class UpdateReport implements UpdatesReports
{
/**
* Update an existing report.
*/
public function __invoke(Model $user, Report $report, array $data): Report
{
event(new UpdatingReport(user: $user, report: $report, data: $data));

Validator::make($data, [
'name' => 'required|string|max:255',
'description' => 'required|string|max:255',
'settings' => 'nullable|array',
'settings.group' => 'nullable|string|max:255|in:'.implode(',', config('instrument.report_groups')),
'settings.period' => 'nullable|string|max:255|in:'.implode(',', config('instrument.report_periods')),
'settings.basis' => 'nullable|string|max:255|in:'.implode(',', config('instrument.report_accounting_basis')),
])->validateWithBag('updateReport');

$report->update(collect($data)->only([
'name',
'description',
'settings',
])->toArray());

event(new ReportUpdated(user: $user, report: $report, data: $data));

return $report->refresh();
}
}
1 change: 1 addition & 0 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function handle(): int
copy(__DIR__.'/../../stubs/app/Models/Currency.php', app_path('Models/Currency.php'));
copy(__DIR__.'/../../stubs/app/Models/PaymentMethod.php', app_path('Models/PaymentMethod.php'));
copy(__DIR__.'/../../stubs/app/Models/Transaction.php', app_path('Models/Transaction.php'));
copy(__DIR__.'/../../stubs/app/Models/Report.php', app_path('Models/Report.php'));

// Service Providers...
copy(__DIR__.'/../../stubs/app/Providers/InstrumentServiceProvider.php', app_path('Providers/InstrumentServiceProvider.php'));
Expand Down
14 changes: 14 additions & 0 deletions src/Contracts/CreatesReports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Instrument\Contracts;

use Illuminate\Database\Eloquent\Model;
use Instrument\Report;

interface CreatesReports
{
/**
* Create a new report.
*/
public function __invoke(Model $user, array $data, $teamId = null): Report;
}
14 changes: 14 additions & 0 deletions src/Contracts/DeletesReports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Instrument\Contracts;

use Illuminate\Database\Eloquent\Model;
use Instrument\Report;

interface DeletesReports
{
/**
* Delete an existing report.
*/
public function __invoke(Model $user, Report $report): void;
}
14 changes: 14 additions & 0 deletions src/Contracts/UpdatesReports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Instrument\Contracts;

use Illuminate\Database\Eloquent\Model;
use Instrument\Report;

interface UpdatesReports
{
/**
* Update an existing report.
*/
public function __invoke(Model $user, Report $report, array $data): Report;
}
7 changes: 7 additions & 0 deletions src/Events/CreatingReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Instrument\Events;

class CreatingReport extends ReportEvent
{
}
7 changes: 7 additions & 0 deletions src/Events/DeletingReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Instrument\Events;

class DeletingReport extends ReportEvent
{
}
7 changes: 7 additions & 0 deletions src/Events/ReportCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Instrument\Events;

class ReportCreated extends ReportEvent
{
}
7 changes: 7 additions & 0 deletions src/Events/ReportDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Instrument\Events;

class ReportDeleted extends ReportEvent
{
}
29 changes: 29 additions & 0 deletions src/Events/ReportEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Instrument\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

abstract class ReportEvent
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

/**
* Create a new event instance.
*
* @param mixed $user
* @param mixed $report
* @param array $data
* @return void
*/
public function __construct(
public $user = null,
public $report = null,
public $data = []
) {
}
}
7 changes: 7 additions & 0 deletions src/Events/ReportUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Instrument\Events;

class ReportUpdated extends ReportEvent
{
}
7 changes: 7 additions & 0 deletions src/Events/UpdatingReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Instrument\Events;

class UpdatingReport extends ReportEvent
{
}
Loading

0 comments on commit 6814b3c

Please sign in to comment.