<?php

namespace App\Repository;

use App\Entity\Customer;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<Customer>
 *
 * @codeCoverageIgnore
 */
class CustomerRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Customer::class);
    }

    public function generateUniqueCodeComptable(Customer $customer): string
    {
        $base = $customer->generateCodeComptable();
        $code = $base;
        $suffix = 1;

        while (null !== $this->findOneBy(['codeComptable' => $code])) {
            $code = $base.'_'.$suffix;
            ++$suffix;
        }

        return $code;
    }
}
