<?php

namespace Modules\Additional\Specifique\Client155\Modules\Declaratif\Repository;

use Helpers\QueryBuilder\AbsRepository;

class Commande extends AbsRepository
{
    /**
     * @param string $codeFou
     * @param string $periode
     * @param string $ssfam
     * @return mixed
     */
    public function findExistingCde(string $codeFou, string $periode, string $ssfam)
    {
        $stmt = "
            SELECT 
                CAT.NO_CDE, CAL.PU, (CAT.AVENANT + 1) as AVENANT, CAT.DTE_VALID
            FROM 
                CA_ENTET CAT with (nolock) 
            LEFT JOIN 
                CA_LIGNE CAL with (nolock) ON CAT.NO_CDE = CAL.NO_CDE 
           WHERE 
               CAT.CODE_FOU = :CODE_FOU AND 
               CAT.DTE_PERIO = :DTE_PERIO AND 
               CAL.SSFAM = :CODE_SSFAM
        ";

        $data = [
            'CODE_FOU' => $codeFou,
            'DTE_PERIO' => $periode,
            'CODE_SSFAM' => $ssfam
        ];

        $query = $this->pdo->prepare($stmt);
        $query->execute($data);

        return $query->fetch($this->pdo::FETCH_ASSOC);
    }
}
