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
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace App\Models\Tenant;
use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class Expense extends Model
{
use BelongsToTenant, SoftDeletes;
public const CATEGORIES = [
'salary' => 'Salariu',
'purchase' => 'Achiziție piese',
'rent' => 'Chirie',
'utilities' => 'Utilități',
'advance' => 'Avans',
'tax' => 'Taxe',
'fuel' => 'Combustibil',
'tools' => 'Scule / consumabile',
'marketing' => 'Marketing',
'other' => 'Altele',
];
public const METHODS = [
'cash' => 'Numerar',
'card' => 'Card',
'transfer' => 'Virament',
'mobile' => 'Mobile pay',
];
protected $fillable = [
'company_id', 'supplier_id', 'purchase_id', 'user_id',
'paid_at', 'category', 'name', 'amount', 'method', 'reference', 'notes',
];
protected $casts = [
'paid_at' => 'date',
'amount' => 'decimal:2',
];
public function supplier(): BelongsTo
{
return $this->belongsTo(Supplier::class);
}
public function purchase(): BelongsTo
{
return $this->belongsTo(Purchase::class);
}
}
+78
View File
@@ -0,0 +1,78 @@
<?php
namespace App\Models\Tenant;
use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class Payment extends Model
{
use BelongsToTenant, SoftDeletes;
public const METHODS = [
'cash' => 'Numerar',
'card' => 'Card',
'transfer' => 'Virament',
'mobile' => 'Mobile pay',
];
protected $fillable = [
'company_id', 'client_id', 'work_order_id', 'user_id',
'paid_at', 'amount', 'method', 'reference', 'notes',
];
protected $casts = [
'paid_at' => 'date',
'amount' => 'decimal:2',
];
public function client(): BelongsTo
{
return $this->belongsTo(Client::class);
}
public function workOrder(): BelongsTo
{
return $this->belongsTo(WorkOrder::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* After save/delete, recompute the work order's pay_status.
*/
protected static function booted(): void
{
$sync = function (self $payment) {
if (! $payment->work_order_id) {
return;
}
$wo = WorkOrder::withoutGlobalScopes()->find($payment->work_order_id);
if (! $wo) {
return;
}
$paid = (float) static::withoutGlobalScopes()
->where('work_order_id', $wo->id)
->whereNull('deleted_at')
->sum('amount');
$total = (float) $wo->total;
if ($paid <= 0) {
$wo->pay_status = 'unpaid';
} elseif ($paid + 0.01 < $total) {
$wo->pay_status = 'partial';
} else {
$wo->pay_status = 'paid';
}
$wo->save();
};
static::saved($sync);
static::deleted($sync);
}
}
+15
View File
@@ -73,6 +73,21 @@ class WorkOrder extends Model
return $this->hasMany(WorkOrderPart::class);
}
public function payments(): HasMany
{
return $this->hasMany(Payment::class);
}
public function paidAmount(): float
{
return (float) $this->payments()->sum('amount');
}
public function balanceDue(): float
{
return max(0.0, (float) $this->total - $this->paidAmount());
}
public function recalcTotal(): void
{
$worksTotal = $this->works()->sum('total');