55 lines
1.8 KiB
PHP
55 lines
1.8 KiB
PHP
<?php
|
|
|
|
use App\Enums\DocumentModalityEnum;
|
|
use App\Enums\InvoiceStatusEnum;
|
|
use Illuminate\Database\Migrations\Migration;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
return new class extends Migration
|
|
{
|
|
public function up(): void
|
|
{
|
|
Schema::create('invoices', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->string('number')->unique();
|
|
$table->unsignedBigInteger('amount');
|
|
$table->string('status')->default(InvoiceStatusEnum::Pending->value);
|
|
$table->string('payment_method')->nullable();
|
|
$table->timestamp('paid_at')->nullable();
|
|
$table->timestamps();
|
|
|
|
$table->foreignId('document_id')->nullable()->constrained('documents')->cascadeOnDelete();
|
|
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
|
|
});
|
|
|
|
DB::table('documents')->orderBy('id')->each(function ($document) {
|
|
$modality = DocumentModalityEnum::tryFrom($document->modality);
|
|
|
|
if (!$modality) {
|
|
return;
|
|
}
|
|
|
|
$id = DB::table('invoices')->insertGetId([
|
|
'number' => 'pending',
|
|
'amount' => $modality->price(),
|
|
'status' => InvoiceStatusEnum::Pending->value,
|
|
'document_id' => $document->id,
|
|
'user_id' => $document->requester_id,
|
|
'created_at' => $document->created_at,
|
|
'updated_at' => $document->created_at,
|
|
]);
|
|
|
|
DB::table('invoices')->where('id', $id)->update([
|
|
'number' => str_pad((string) $id, 5, '0', STR_PAD_LEFT),
|
|
]);
|
|
});
|
|
}
|
|
|
|
public function down(): void
|
|
{
|
|
Schema::dropIfExists('invoices');
|
|
}
|
|
};
|