Commit cd3314dd authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix 3 schema mismatches found in full model-vs-DB audit

- Branch::users() was hasMany (users.branch_id doesn't exist), now
  belongsToMany through role_user pivot
- NotificationLog missing read_at in $fillable despite column existing
- Trainer::assignments() used wrong FK join (employee PK vs user FK),
  replaced with hasManyThrough via Employee
- POSService referenced phantom wallet.frozen_amount column
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 26d47cc6
......@@ -69,10 +69,16 @@ public function availabilities(): HasMany
return $this->hasMany(TrainerAvailability::class);
}
public function assignments(): HasMany
public function assignments()
{
return $this->hasMany(\App\Domain\Scheduling\Models\Assignment::class, 'user_id', 'employee_id')
->whereHas('employee', fn ($q) => $q->whereColumn('employees.user_id', 'assignments.user_id'));
return $this->hasManyThrough(
\App\Domain\Scheduling\Models\Assignment::class,
Employee::class,
'id', // employees.id
'user_id', // assignments.user_id
'employee_id', // trainers.employee_id
'user_id' // employees.user_id
);
}
// --- Scopes ---
......
......@@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Branch extends Model
......@@ -33,9 +34,9 @@ public function manager(): BelongsTo
return $this->belongsTo(\App\Models\User::class, 'manager_id');
}
public function users(): HasMany
public function users(): BelongsToMany
{
return $this->hasMany(\App\Models\User::class);
return $this->belongsToMany(\App\Models\User::class, 'role_user', 'branch_id', 'user_id');
}
public function participants(): HasMany
......
......@@ -29,6 +29,7 @@ class NotificationLog extends Model
'status',
'error_message',
'sent_at',
'read_at',
'metadata',
];
......
......@@ -87,7 +87,8 @@ public function processTransaction(
// Step 5: Calculate totals
$taxAmount = 0; // Tax configuration per academy — to be implemented
$serviceFeeAmount = $this->platformFeeService->calculate($subtotal);
$customerPays = $this->platformFeeService->customerPays();
$serviceFeeAmount = $customerPays ? $this->platformFeeService->calculate($subtotal) : 0;
$totalAmount = $subtotal + $taxAmount + $serviceFeeAmount;
// Step 6-7: Validate payment
......@@ -214,7 +215,7 @@ private function validatePayment(POSPaymentMethod $method, int $total, ?Particip
throw new DomainException('الدفع بالمحفظة يتطلب تحديد المشترك');
}
$wallet = $this->walletService->getOrCreateWallet($participant, $participant->academy_id);
$availableBalance = $wallet->balance - ($wallet->frozen_amount ?? 0);
$availableBalance = $wallet->balance;
if ($availableBalance < $total) {
throw new DomainException('رصيد المحفظة غير كافٍ');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment