<?php

class Memos
{

    private static function wsdlInsert(& $NODEMEMO, & $CONTMEMO)
    {
        $envoi = '	<MemoIn LngUtilisation="' . utilisateur::getLangue() . '">
					    <NumMemo>' . $NODEMEMO . '</NumMemo>
					    <Memo>' . ReplaceSqlPointeur($CONTMEMO) . '</Memo>
					</MemoIn>';
        return Wsdl::wsdlCallGen('Oxaproc_AO.wsdl', 'OXAPRC_UTI_GERE_MEMO', $envoi, true);
    }

    public static function getNumMemo($search = 0)
    {
       
      
           $xml ='<NoSessionIn LngUtilisation="'.utilisateur::getLangue() .'"></NoSessionIn>';
           $memowsdl = Wsdl::wsdlCallGen('Oxaproc_AO.wsdl', 'OXAPRC_DONNE_NO_SESSION', $xml, true);
           $_SESSION['numMemo'] = (int) $memowsdl['NumSession'];
           $req = "select max (NODEMEMO) as NODEMEMO  from MEMOS with(nolock) where NODEMEMO between ".$_SESSION['numMemo']."*100000 and ".$_SESSION['numMemo']."*100000+99999";
           $memoResult = (new RequeteSQL($req, DB_OXASQLDB))->Trouve1()['NODEMEMO'];
           
           if(substr($memoResult, -5) == '99999')
           {          
               return $this->generateNum($pdo, 1);
           }
           else
           {
               if($memoResult =='')
               {
                   return (int)$_SESSION['numMemo'].'00001';
               }
               else
               {
                   return $memoResult+1;
               }
           }
        
    }

    public static function &get(& $NODEMEMO)
    {
        $resultat = '';
        
        if (is_numeric($NODEMEMO) && 0 != $NODEMEMO) {
            $reqSql = SqlInfo::rqGetMemo($NODEMEMO)->Trouve1();
            if (is_array($reqSql))
                $resultat = $reqSql['CONTMEMO'];
        }
        return $resultat;
    }

    public static function set(& $NODEMEMO, & $CONTMEMO)
    {
        $result = '0';
        if (is_numeric($NODEMEMO)) {
            $resultatWsdl = Memos::wsdlInsert($NODEMEMO, $CONTMEMO);
            $result = $resultatWsdl['NumMemo'];
        }
        return $result;
    }

    public static function &insert(& $CONTMEMO)
    {
        $NODEMEMO = 0;
        return Memos::set($NODEMEMO, $CONTMEMO);
    }

    public static function &delete(& $NODEMEMO)
    {
        SqlInfo::rqDeleteMemo($NODEMEMO)->ExecuteSql();
    }
}