<?php

class AO_Panier_detail_split_ligne
{

    const fichierXsl = 'application/xsl/ao_panier/detail_split_ligne.xsl';

    const fichierPhp = 'ao_panier_detail_split_ligne.php';

    const actionAffichePopup = 'AffichePopupSplit';

    const changeMode = 'ChangeMode';

    const id_btn_mode_synthe = 'btn_mode_synth';

    const id_btn_mode_detail = 'btn_mode_detail';

    const id_div_mode_synthe = 'div_mode_synth';

    const id_div_mode_detail = 'div_mode_detail';

    const modeDetaille = 'detail';

    const modeSynthetique = 'synthetique';

    const id_div_tableau = 'div_tableau_split';

    private $no_ao;

    private $no_po;

    private $code_fou;

    private $type_fg;

    private $dte_rep;

    private $c_v;

    private $dte_ao;

    function __construct(& $no_ao, & $no_po, & $code_fou, & $type_fg, & $dte_rep, & $c_v, & $dte_ao)
    {
        $this->no_ao = $no_ao;
        $this->no_po = $no_po;
        $this->code_fou = $code_fou;
        $this->type_fg = $type_fg;
        $this->dte_rep = $dte_rep;
        $this->c_v = $c_v;
        $this->dte_ao = $dte_ao;
    }

    public static function &getUrlAction($action, & $no_ao, & $no_po, & $code_fou, & $type_fg, & $dte_rep, & $c_v, $dte_ao, $mode = '')
    {
        $url = AO_Panier_detail_split_ligne::fichierPhp . '?action=' . urlencode($action) . '&no_ao=' . urlencode($no_ao) . '&no_po=' . urlencode($no_po) . '&code_fou=' . urlencode($code_fou) . '&type_fg=' . urlencode($type_fg) . '&dte_rep=' . urlencode($dte_rep) . '&c_v=' . urlencode($c_v) . '&dte_ao=' . urlencode($dte_ao) . '&mode=' . $mode;
        return $url;
    }

    function &buildXmlLignes()
    {
        $array = array();
        
        // Pour la présentation des qté / prix
        $nb_decimal_pu = 2;
        $nb_decimal_qte = 3;
        $sepDecimales = Param::AllParamsGetValue('SEPARATEUR_DECIMALES');
        $sepMilliers = Param::AllParamsGetValue('SEPARATEUR_MILLIERS');
        
        $infoSL = SqlAo::getAOInfoRepSL($this->no_ao, $this->no_po, $this->code_fou, $this->type_fg, $this->dte_rep, $this->c_v)->TrouveTous();
        if (is_array($infoSL)) {
            foreach ($infoSL as & $ligne) {
                $array[] = array(
                    '!CODE_ART' => $ligne['CODE_ART'],
                    '!DES_ART' => $ligne['DES_ART'],
                    '!DCOMP' => $ligne['DCOMP'],
                    '!QTE_PROP' => number_format($ligne['QTE_PROP'], $nb_decimal_qte, $sepDecimales, $sepMilliers),
                    'UNITE' => array(
                        '!CODE_UNI' => $ligne['UNITE'],
                        '!LIBELLE' => $ligne['LIB_UNITE']
                    ),
                    '!PU' => number_format($ligne['PU'], $nb_decimal_pu, $sepDecimales, $sepMilliers),
                    '!DEVISE' => $ligne['DEVISE'],
                    '!REF_FOUR' => $ligne['REF_FOUR'],
                    '!REF_FAB' => $ligne['REF_FAB'],
                    '!DES_FOUR' => $ligne['DES_FOUR'],
                    '!COD_FAB' => $ligne['COD_FAB'],
                    '!COD_GAM' => $ligne['COD_GAM']
                );
            }
        }
        
        return $array;
    }

    function &buildXml(& $action, & $presentation = '')
    {
        $array = array();
        
        // Titre de la popup
        $array['popup_title'] = Trad('Appel d\'offre N°') . ' ' . $this->no_ao . '  ' . Trad('du') . ' ' . convertDateSqlToFr($this->dte_ao);
        $array['action'] = $action;
        
        $info_four = SqlFrs::rqGetRecordFournis($this->code_fou)->Trouve1();
        if (is_array($info_four)) {
            $lib_fou = $info_four['RS'];
            if ('' == $lib_fou)
                $lib_fou = $info_four['ENSCOMM'];
        }
        $str_four = ('' != $lib_fou) ? $lib_fou . ' (' . $this->code_fou . ')' : $this->code_fou;
        $type_c_v = ('V' == $this->c_v) ? Trad('Variante') : Trad('Conforme');
        
        $info_darep = SqlAo::getAOInfoDAREP($this->no_ao, $this->code_fou, $this->type_fg, $this->dte_rep, $this->c_v)->Trouve1();
        if (is_array($info_darep)) {
            $type_c_v .= ' ' . trad('du') . ' ' . convertDateSqlToFr($info_darep['DTE_REP']);
            if ('' != $info_darep['REFRPFOU'])
                $type_c_v .= ' ' . trad('Référence') . ' : ' . $info_darep['REFRPFOU'];
        }
        
        // bouton de présentation
        if ('' == $presentation) // présentation par défaut
            $presentation = AO_Panier_detail_split_ligne::modeSynthetique;
        
        // Bouton mode détaillé
        $id_btn = AO_Panier_detail_split_ligne::id_btn_mode_detail;
        $lbl_btn = Trad('Mode détaillé');
        $action_btn = 'CallUrl("' . $this->getUrlAction(AO_Panier_detail_split_ligne::changeMode, $this->no_ao, $this->no_po, $this->code_fou, $this->type_fg, $this->dte_rep, $this->c_v, $this->dte_ao, AO_Panier_detail_split_ligne::modeDetaille) . '");';
        $compXsl = new ComposantXslAction($id_btn);
        $compXsl->setLibelle($lbl_btn);
        $compXsl->setAction($action_btn);
        $array['btn_mode_detail'] = $compXsl->buildXml();
        $array['div_mode_detail'] = AO_Panier_detail_split_ligne::id_div_mode_detail;
        
        // Bouton mode synthétique
        $id_btn = AO_Panier_detail_split_ligne::id_btn_mode_synthe;
        $lbl_btn = trad('Mode synthétique');
        $action_btn = 'CallUrl("' . $this->getUrlAction(AO_Panier_detail_split_ligne::changeMode, $this->no_ao, $this->no_po, $this->code_fou, $this->type_fg, $this->dte_rep, $this->c_v, $this->dte_ao, AO_Panier_detail_split_ligne::modeSynthetique) . '");';
        $compXsl = new ComposantXslAction($id_btn);
        $compXsl->setLibelle($lbl_btn);
        $compXsl->setAction($action_btn);
        $array['btn_mode_synth'] = $compXsl->buildXml();
        $array['div_mode_synth'] = AO_Panier_detail_split_ligne::id_div_mode_synthe;
        
        // Informations en haut de la page
        $array['titre'] = array(
            '!str_four' => $str_four,
            '!c_v' => $type_c_v,
            '!no_po' => $this->no_po
        );
        
        $array['tableau_split'] = array(
            '!id_div' => AO_Panier_detail_split_ligne::id_div_tableau,
            '!mode' => $presentation
        );
        
        // Informations sur les lignes
        $array['lignes'] = $this->buildXmlLignes();
        
        return $array;
    }
}