Files
clinicpro/src/Settlement/Repository/SettlementRepository.php
T
hamed 9603b702c1 feat: implement domain guard for commission calculation and enhance representation dashboard
- Added domain guard in CommissionService to ensure commission is calculated only when the appointment is booked under the same representation as the doctor.
- Updated RepresentationController to filter statistics by representation, ensuring accurate data is shown for each representative.
- Introduced new endpoints for the representation dashboard to provide summary statistics, doctor performance, and financial reports.
- Created new pages for RepresentationFinance and RepresentationSettlement to display financial data and allow for settlement requests.
- Added migration to include booking_representation_id in appointments for tracking the representative under which the appointment was booked.
2026-06-24 16:14:41 +03:30

62 lines
2.1 KiB
PHP

<?php
namespace App\Settlement\Repository;
use App\Auth\Entity\User;
use App\Settlement\Entity\Settlement;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
class SettlementRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Settlement::class);
}
public function findByUuid(string $uuid): ?Settlement
{
return $this->findOneBy(['uuid' => $uuid]);
}
/** @return Settlement[] */
public function findByUser(User $user): array
{
return $this->findBy(['user' => $user], ['createdAt' => 'DESC']);
}
/** Balance = sum of credits - sum of debits from wallet_transactions */
public function getWalletBalance(User $user): int
{
$em = $this->getEntityManager();
$credit = (int) ($em->createQuery(
'SELECT SUM(w.amountRials) FROM App\Settlement\Entity\WalletTransaction w
WHERE w.user = :user AND w.type = :type'
)->setParameters(['user' => $user, 'type' => 'credit'])->getSingleScalarResult() ?? 0);
$debit = (int) ($em->createQuery(
'SELECT SUM(w.amountRials) FROM App\Settlement\Entity\WalletTransaction w
WHERE w.user = :user AND w.type = :type'
)->setParameters(['user' => $user, 'type' => 'debit'])->getSingleScalarResult() ?? 0);
return $credit - $debit;
}
/** @param string[] $statuses */
public function sumByStatus(User $user, array $statuses): int
{
return (int) ($this->getEntityManager()->createQuery(
'SELECT SUM(s.amountRials) FROM App\Settlement\Entity\Settlement s
WHERE s.user = :user AND s.status IN (:statuses)'
)->setParameters(['user' => $user, 'statuses' => $statuses])
->getSingleScalarResult() ?? 0);
}
public function save(Settlement $entity, bool $flush = true): void
{
$this->getEntityManager()->persist($entity);
if ($flush) $this->getEntityManager()->flush();
}
}