Faza 3.4: Finanțe — Plăți + Cheltuieli + Cashflow

Schema:
- payments: client_id, work_order_id, user_id (operator), paid_at, amount,
  method (cash/card/transfer/mobile), reference, notes
- expenses: supplier_id, purchase_id, paid_at, category (salary/purchase/rent/
  utilities/advance/tax/fuel/tools/marketing/other), name, amount, method, ref

Logică auto:
- Payment::saved/deleted recalculează automat work_order.pay_status
  (unpaid → partial → paid) based on suma totală vs work_order.total
- WO model are noi metode: payments(), paidAmount(), balanceDue()

Filament resources (group Finanțe):
- PaymentResource: form cu legare opțională la WO + client; tabel cu
  Sum summary, filtre azi/luna_curentă/method
- ExpenseResource: 10 categorii preset, badge categ, total summary,
  filtru luna curentă
- PaymentsRelationManager pe WO: "Plăți" tab cu auto-fill client_id +
  user_id la creare

Widget FinanceOverview:
- Încasări (luna), Cheltuieli (luna), Profit (luna), Datorii clienți
- color coded: profit verde sau roșu, datorii galben/verde

Settings page fix (Filament v5):
- mount() folosește acum $this->form->fill([...]) în loc de $this->data direct
- Filament v5 cere fill explicit pentru a inițializa state-ul schemei

Seed:
- 1 plată parțială pe fișa BMW (200 din 750)
- 6 cheltuieli demo: 3 salarii, chirie, electricitate, achiziție piese

Total Filament tenant routes: 69.
This commit is contained in:
2026-05-06 22:55:50 +00:00
parent 7264dccffa
commit f0f9fdd555
18 changed files with 634 additions and 2 deletions
@@ -0,0 +1,60 @@
<?php
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::create('payments', function (Blueprint $t) {
$t->id();
$t->foreignId('company_id')->constrained()->cascadeOnDelete();
$t->foreignId('client_id')->nullable()->constrained()->nullOnDelete();
$t->foreignId('work_order_id')->nullable()->constrained()->nullOnDelete();
$t->foreignId('user_id')->nullable()->constrained()->nullOnDelete(); // operator înregistrare
$t->date('paid_at')->default(now());
$t->decimal('amount', 12, 2);
$t->string('method')->default('cash'); // cash / card / transfer / mobile
$t->string('reference')->nullable(); // nr. chitanță / tranzacție
$t->text('notes')->nullable();
$t->timestamps();
$t->softDeletes();
$t->index(['company_id', 'paid_at']);
$t->index(['company_id', 'work_order_id']);
$t->index(['company_id', 'method']);
});
Schema::create('expenses', function (Blueprint $t) {
$t->id();
$t->foreignId('company_id')->constrained()->cascadeOnDelete();
$t->foreignId('supplier_id')->nullable()->constrained()->nullOnDelete();
$t->foreignId('purchase_id')->nullable()->constrained()->nullOnDelete();
$t->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
$t->date('paid_at')->default(now());
$t->string('category'); // salary / purchase / rent / utilities / advance / tax / other
$t->string('name'); // descriere
$t->decimal('amount', 12, 2);
$t->string('method')->default('cash');
$t->string('reference')->nullable();
$t->text('notes')->nullable();
$t->timestamps();
$t->softDeletes();
$t->index(['company_id', 'paid_at']);
$t->index(['company_id', 'category']);
});
}
public function down(): void
{
Schema::dropIfExists('expenses');
Schema::dropIfExists('payments');
}
};
+25
View File
@@ -13,7 +13,9 @@ use App\Models\Tenant\Lead;
use App\Models\Tenant\Post;
use App\Models\Tenant\User;
use App\Models\Tenant\Vehicle;
use App\Models\Tenant\Expense;
use App\Models\Tenant\Part;
use App\Models\Tenant\Payment;
use App\Models\Tenant\Purchase;
use App\Models\Tenant\PurchaseItem;
use App\Models\Tenant\Supplier;
@@ -415,6 +417,29 @@ class DatabaseSeeder extends Seeder
}
$purchase->refresh()->recalcTotal();
// ─── Plăți demo ──────────────────────────────────────────
// Plată parțială pe fișa demo (BMW)
Payment::firstOrCreate(
['company_id' => $psauto->id, 'work_order_id' => $wo->id, 'paid_at' => today()->subDays(1), 'amount' => 200],
['client_id' => $c1->id, 'method' => 'cash', 'reference' => 'CHIT-001', 'user_id' => $admin->id]
);
// ─── Cheltuieli demo ────────────────────────────────────
$expensesData = [
['salary', 'Salariu Vasile Ivanov', 8000, today()->startOfMonth(), 'cash'],
['salary', 'Salariu Andrei Popov', 7500, today()->startOfMonth(), 'cash'],
['salary', 'Salariu Nicolae Lupu', 7000, today()->startOfMonth(), 'cash'],
['rent', 'Chirie spațiu service (luna curentă)', 4500, today()->startOfMonth(), 'transfer'],
['utilities', 'Electricitate', 850, today()->subDays(15), 'card'],
['purchase', 'Achiziție stoc (P-26-0001)', 1036, today()->subDays(7), 'transfer'],
];
foreach ($expensesData as [$cat, $name, $amount, $date, $method]) {
Expense::firstOrCreate(
['company_id' => $psauto->id, 'name' => $name, 'paid_at' => $date],
['category' => $cat, 'amount' => $amount, 'method' => $method, 'user_id' => $admin->id]
);
}
app(TenantManager::class)->clear();
}
}