- change document numbering system (#106)
This commit is contained in:
parent
1ae23bd7cb
commit
3ab02f1df4
@ -6,12 +6,15 @@ namespace Toby\Architecture\Providers;
|
|||||||
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Toby\Eloquent\Models\User;
|
use Toby\Eloquent\Models\User;
|
||||||
|
use Toby\Eloquent\Models\VacationRequest;
|
||||||
use Toby\Eloquent\Observers\UserObserver;
|
use Toby\Eloquent\Observers\UserObserver;
|
||||||
|
use Toby\Eloquent\Observers\VacationRequestObserver;
|
||||||
|
|
||||||
class ObserverServiceProvider extends ServiceProvider
|
class ObserverServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
public function boot(): void
|
public function boot(): void
|
||||||
{
|
{
|
||||||
User::observe(UserObserver::class);
|
User::observe(UserObserver::class);
|
||||||
|
VacationRequest::observe(VacationRequestObserver::class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,11 +91,6 @@ class VacationRequest extends Model
|
|||||||
->where("to", ">=", $vacationRequest->from);
|
->where("to", ">=", $vacationRequest->from);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getNameAttribute(): string
|
|
||||||
{
|
|
||||||
return "{$this->id}/{$this->yearPeriod->year}";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function hasFlowSkipped(): bool
|
public function hasFlowSkipped(): bool
|
||||||
{
|
{
|
||||||
return $this->flow_skipped;
|
return $this->flow_skipped;
|
||||||
|
@ -15,6 +15,7 @@ use Illuminate\Support\Collection;
|
|||||||
* @property int $id
|
* @property int $id
|
||||||
* @property int $year
|
* @property int $year
|
||||||
* @property Collection $vacationLimits
|
* @property Collection $vacationLimits
|
||||||
|
* @property Collection $vacationRequests
|
||||||
* @property Collection $holidays
|
* @property Collection $holidays
|
||||||
*/
|
*/
|
||||||
class YearPeriod extends Model
|
class YearPeriod extends Model
|
||||||
@ -41,6 +42,11 @@ class YearPeriod extends Model
|
|||||||
return $this->hasMany(VacationLimit::class);
|
return $this->hasMany(VacationLimit::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function vacationRequests(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(VacationRequest::class);
|
||||||
|
}
|
||||||
|
|
||||||
public function holidays(): HasMany
|
public function holidays(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(Holiday::class);
|
return $this->hasMany(Holiday::class);
|
||||||
|
18
app/Eloquent/Observers/VacationRequestObserver.php
Normal file
18
app/Eloquent/Observers/VacationRequestObserver.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Toby\Eloquent\Observers;
|
||||||
|
|
||||||
|
use Toby\Eloquent\Models\VacationRequest;
|
||||||
|
|
||||||
|
class VacationRequestObserver
|
||||||
|
{
|
||||||
|
public function creating(VacationRequest $vacationRequest): void
|
||||||
|
{
|
||||||
|
$count = $vacationRequest->yearPeriod->vacationRequests()->count();
|
||||||
|
$number = $count + 1;
|
||||||
|
|
||||||
|
$vacationRequest->name = "{$number}/{$vacationRequest->yearPeriod->year}";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Toby\Infrastructure\Console\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Toby\Eloquent\Models\YearPeriod;
|
||||||
|
|
||||||
|
class RebuildDocumentNumberingSystem extends Command
|
||||||
|
{
|
||||||
|
protected $signature = "toby:rebuild-document-numbering-system";
|
||||||
|
protected $description = "Rebuilds the document numbering system to {number}/{year}";
|
||||||
|
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
$yearPeriods = YearPeriod::all();
|
||||||
|
|
||||||
|
foreach ($yearPeriods as $yearPeriod) {
|
||||||
|
$number = 1;
|
||||||
|
|
||||||
|
$vacationRequests = $yearPeriod
|
||||||
|
->vacationRequests()
|
||||||
|
->oldest()
|
||||||
|
->get();
|
||||||
|
|
||||||
|
foreach ($vacationRequests as $vacationRequest) {
|
||||||
|
$vacationRequest->update(["name" => "{$number}/{$yearPeriod->year}"]);
|
||||||
|
|
||||||
|
$number++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class() extends Migration {
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table("vacation_requests", function (Blueprint $table): void {
|
||||||
|
$table->string("name")->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table("vacation_requests", function (Blueprint $table): void {
|
||||||
|
$table->dropColumn("name");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user