This commit is contained in:
2026-07-12 19:21:35 +02:00
parent f2d944b117
commit 41cb412cb0
49 changed files with 3170 additions and 52 deletions
@@ -0,0 +1,34 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateReceipts extends Migration
{
public function up(): void
{
$this->forge->addField([
'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
'file_path' => ['type' => 'VARCHAR', 'constraint' => 255],
'original_name' => ['type' => 'VARCHAR', 'constraint' => 255],
'mime' => ['type' => 'VARCHAR', 'constraint' => 100],
'merchant' => ['type' => 'VARCHAR', 'constraint' => 190, 'null' => true],
'receipt_date' => ['type' => 'DATE', 'null' => true],
'total' => ['type' => 'DECIMAL', 'constraint' => '12,2', 'null' => true],
'status' => ['type' => 'ENUM', 'constraint' => ['parsing', 'parsed', 'failed', 'imported'], 'default' => 'parsing'],
'raw_json' => ['type' => 'LONGTEXT', 'null' => true],
'error_msg' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
'created_at' => ['type' => 'DATETIME', 'null' => true],
'updated_at' => ['type' => 'DATETIME', 'null' => true],
]);
$this->forge->addKey('id', true);
$this->forge->addKey('status');
$this->forge->createTable('receipts', true, ['ENGINE' => 'InnoDB']);
}
public function down(): void
{
$this->forge->dropTable('receipts', true);
}
}
@@ -0,0 +1,36 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateReceiptItems extends Migration
{
public function up(): void
{
$this->forge->addField([
'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
'receipt_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
'name' => ['type' => 'VARCHAR', 'constraint' => 255],
'qty' => ['type' => 'DECIMAL', 'constraint' => '10,3', 'null' => true],
'amount' => ['type' => 'DECIMAL', 'constraint' => '12,2'],
'suggested_category_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
'chosen_category_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
'created_at' => ['type' => 'DATETIME', 'null' => true],
'updated_at' => ['type' => 'DATETIME', 'null' => true],
]);
$this->forge->addKey('id', true);
$this->forge->addKey('receipt_id');
// Kasacja paragonu kasuje jego pozycje.
$this->forge->addForeignKey('receipt_id', 'receipts', 'id', 'CASCADE', 'CASCADE');
// Kasacja kategorii zeruje podpowiedz/wybor (nie blokuje).
$this->forge->addForeignKey('suggested_category_id', 'categories', 'id', 'SET NULL', 'SET NULL');
$this->forge->addForeignKey('chosen_category_id', 'categories', 'id', 'SET NULL', 'SET NULL');
$this->forge->createTable('receipt_items', true, ['ENGINE' => 'InnoDB']);
}
public function down(): void
{
$this->forge->dropTable('receipt_items', true);
}
}
@@ -0,0 +1,28 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateReceiptCategoryMap extends Migration
{
public function up(): void
{
$this->forge->addField([
'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
'item_name' => ['type' => 'VARCHAR', 'constraint' => 255],
'category_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
'updated_at' => ['type' => 'DATETIME', 'null' => true],
]);
$this->forge->addKey('id', true);
// Znormalizowana nazwa pozycji jest kluczem uczenia (jedno mapowanie na nazwe).
$this->forge->addUniqueKey('item_name');
$this->forge->addForeignKey('category_id', 'categories', 'id', 'CASCADE', 'CASCADE');
$this->forge->createTable('receipt_category_map', true, ['ENGINE' => 'InnoDB']);
}
public function down(): void
{
$this->forge->dropTable('receipt_category_map', true);
}
}
@@ -0,0 +1,23 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class AddReceiptIdToOperations extends Migration
{
public function up(): void
{
$this->forge->addColumn('operations', [
'receipt_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true, 'after' => 'category_id'],
]);
// Kasacja paragonu nie kasuje operacji, tylko zrywa powiazanie.
$this->forge->addForeignKey('receipt_id', 'receipts', 'id', 'SET NULL', 'SET NULL', 'operations_receipt_id_fk');
}
public function down(): void
{
$this->forge->dropForeignKey('operations', 'operations_receipt_id_fk');
$this->forge->dropColumn('operations', 'receipt_id');
}
}
@@ -0,0 +1,21 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class AddDisplayNameToReceiptItems extends Migration
{
public function up(): void
{
$this->forge->addColumn('receipt_items', [
// Ladna, czytelna nazwa do wyswietlania i opisu operacji (name = surowa z paragonu, klucz mapowania).
'display_name' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true, 'after' => 'name'],
]);
}
public function down(): void
{
$this->forge->dropColumn('receipt_items', 'display_name');
}
}
@@ -0,0 +1,29 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateRememberTokens extends Migration
{
public function up(): void
{
$this->forge->addField([
'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
'selector' => ['type' => 'VARCHAR', 'constraint' => 32],
'validator_hash' => ['type' => 'CHAR', 'constraint' => 64],
'expires_at' => ['type' => 'DATETIME'],
'created_at' => ['type' => 'DATETIME', 'null' => true],
'updated_at' => ['type' => 'DATETIME', 'null' => true],
]);
$this->forge->addKey('id', true);
$this->forge->addUniqueKey('selector');
$this->forge->addKey('expires_at');
$this->forge->createTable('remember_tokens', true, ['ENGINE' => 'InnoDB']);
}
public function down(): void
{
$this->forge->dropTable('remember_tokens', true);
}
}