<?php

/**
 * 
 * @param unknown $codeUser
 * @param unknown $arrayArticle
 * @return RequeteSQL
 */
class SqlFavorisArticle
{

    public static function &getArrayCodeArticle(& $arrayArticle)
    {
        if (array_key_exists('CODE_ART', $arrayArticle)) // Intuition ou DB SQL
            $return = & $arrayArticle['CODE_ART'];
        else if (array_key_exists('COD_ART', $arrayArticle)) // DB OXAPROC
            $return = & $arrayArticle['COD_ART'];
        else
            $return = '';
        
        return $return;
    }

    public static function &getArrayCodeFournisseur(& $arrayArticle)
    {
        if (array_key_exists('COD_FOURN', $arrayArticle)) // DB OXAPROC
            $return = & $arrayArticle['COD_FOURN'];
        else if (array_key_exists('CODE_FOU', $arrayArticle)) // Intuition ou DB SQL
            $return = & $arrayArticle['CODE_FOU'];
        else if (array_key_exists('COD_FOU', $arrayArticle)) // Intuition ou DB SQL
            $return = & $arrayArticle['COD_FOU'];
        else
            $return = '';
        
        return $return;
    }

    public static function &getArrayCodeAcd(& $arrayArticle)
    {
        if (array_key_exists('COD_ACD', $arrayArticle)) // DB OXAPROC
            $return = & $arrayArticle['COD_ACD'];
        else if (array_key_exists('CODE_ACD', $arrayArticle)) // Intuition ou DB SQL
            $return = & $arrayArticle['CODE_ACD'];
        else
            $return = '';
        
        return $return;
    }

    public static function rqGetFavorisArticle($codeUser, & $arrayArticle)
    {
        $sql = 'select * from FAVEPROC with(nolock) ' . 'where CODE_USER = \'' . ReplaceSqlPointeur($codeUser) . '\' ' . 'and   CODEFAB   = \'' . ReplaceSqlPointeur($arrayArticle['COD_FAB']) . '\' ' . 'and   REFARTFAB = \'' . ReplaceSqlPointeur($arrayArticle['REF_FAB']) . '\' ' . 'and   CODE_FOU  = \'' . ReplaceSqlPointeur(SqlFavorisArticle::getArrayCodeFournisseur($arrayArticle)) . '\' ' . 'and   REFARTFOU = \'' . ReplaceSqlPointeur($arrayArticle['REF_FOURN']) . '\' ' . 'and   CODE_ART  = \'' . ReplaceSqlPointeur(SqlFavorisArticle::getArrayCodeArticle($arrayArticle)) . '\' ' . 'and   CODE_ACD  = \'' . ReplaceSqlPointeur(SqlFavorisArticle::getArrayCodeAcd($arrayArticle)) . '\' ' . '';
        return new RequeteSQL($sql, DB_OXACATDB);
    }

    /**
     *
     * @param unknown $codeUser
     * @param unknown $arrayArticle
     * @return RequeteSQL
     */
    public static function rqInsertFavorisArticle($codeUser, & $arrayArticle)
    {
        $sql = 'insert into FAVEPROC ' . '(CODE_USER,CODEFAB,REFARTFAB,CODE_FOU,REFARTFOU,CODE_ART,CODE_ACD,STATUT) ' . 'values ' . '(\'' . ReplaceSqlPointeur($codeUser) . '\' ' . 
        ',\'' . ReplaceSqlPointeur($arrayArticle['COD_FAB']) . '\' ' . ',\'' . ReplaceSqlPointeur($arrayArticle['REF_FAB']) . '\' ' . ',\'' . ReplaceSqlPointeur(SqlFavorisArticle::getArrayCodeFournisseur($arrayArticle)) . '\' ' . ',\'' . ReplaceSqlPointeur($arrayArticle['REF_FOURN']) . '\' ' . ',\'' . ReplaceSqlPointeur(SqlFavorisArticle::getArrayCodeArticle($arrayArticle)) . '\' ' . ',\'' . ReplaceSqlPointeur(SqlFavorisArticle::getArrayCodeAcd($arrayArticle)) . '\' ' . 
        ',\'' . FavoriArticle::FAV_ATT_IDX . '\' ' . ')';
        
        return new RequeteSQL($sql, DB_OXACATDB);
    }

    /**
     *
     * @param unknown $codeUser
     * @param unknown $arrayArticle
     * @param unknown $favoris
     * @return RequeteSQL
     */
    public static function rqMajFavorisArticle($codeUser, & $arrayArticle, $favoris)
    {
        $sql = 'update FAVEPROC ' . 'set STATUT = \'' . ($favoris ? FavoriArticle::FAV_ACTIF : FavoriArticle::FAV_ATT_SUP) . '\' ' . 
        'where CODE_USER = \'' . ReplaceSqlPointeur($codeUser) . '\' ' . 'and   CODEFAB   = \'' . ReplaceSqlPointeur($arrayArticle['COD_FAB']) . '\' ' . 'and   REFARTFAB = \'' . ReplaceSqlPointeur($arrayArticle['REF_FAB']) . '\' ' . 'and   CODE_FOU  = \'' . ReplaceSqlPointeur(SqlFavorisArticle::getArrayCodeFournisseur($arrayArticle)) . '\' ' . 'and   REFARTFOU = \'' . ReplaceSqlPointeur($arrayArticle['REF_FOURN']) . '\' ' . 'and   CODE_ART  = \'' . ReplaceSqlPointeur(SqlFavorisArticle::getArrayCodeArticle($arrayArticle)) . '\' ' . 'and   CODE_ACD  = \'' . ReplaceSqlPointeur(SqlFavorisArticle::getArrayCodeAcd($arrayArticle)) . '\' ' . '';
        
        return new RequeteSQL($sql, DB_OXACATDB);
    }

    public static function rqDelFavorisArticle($codeUser, & $arrayArticle)
    {
        $sql = 'delete from FAVEPROC ' . 'where CODE_USER = \'' . ReplaceSqlPointeur($codeUser) . '\' ' . 'and   CODEFAB   = \'' . ReplaceSqlPointeur($arrayArticle['COD_FAB']) . '\' ' . 'and   REFARTFAB = \'' . ReplaceSqlPointeur($arrayArticle['REF_FAB']) . '\' ' . 'and   CODE_FOU  = \'' . ReplaceSqlPointeur(SqlFavorisArticle::getArrayCodeFournisseur($arrayArticle)) . '\' ' . 'and   REFARTFOU = \'' . ReplaceSqlPointeur($arrayArticle['REF_FOURN']) . '\' ' . 'and   CODE_ART  = \'' . ReplaceSqlPointeur(SqlFavorisArticle::getArrayCodeArticle($arrayArticle)) . '\' ' . 'and   CODE_ACD  = \'' . ReplaceSqlPointeur(SqlFavorisArticle::getArrayCodeAcd($arrayArticle)) . '\' ' . '';
        
        return new RequeteSQL($sql, DB_OXACATDB);
    }
}