<?php
//
// Gravar os telefones
Function GravarTelefones($nTipCad, $nCodCad, $a_ListaTel) {
   $nTel_CodigoN=0;
   $RowT = MYSQL_QUERY("SELECT TEL_SEQUEN FROM cad_telefones ORDER BY TEL_SEQUEN DESC LIMIT 1;") or die(MYSQL_ERROR());
   If ($RowT = MYSQL_FETCH_ARRAY($RowT)) {
      $nTel_CodigoN = $RowT[0];
   }
   $a_ListaTelN = ARRAY();
   ForEach ( $a_ListaTel As $aTel ) {
      $a_ListaTelN[ $aTel['TEL_NUMERO'] ] = $aTel;
      $RowT = MYSQL_QUERY("SELECT TEL_SEQUEN FROM cad_telefones WHERE TEL_NUMERO='".$aTel['TEL_NUMERO']."' ;") or die(MYSQL_ERROR());
      If ( $RowT = MYSQL_FETCH_ARRAY($RowT) ) {
	     $nTel_Codigo = $RowT[0]+0;
	  } Else {
	     $nTel_CodigoN++;
		 $nTel_Codigo = $nTel_CodigoN;
		 // Cria um novo registro
         MYSQL_QUERY("INSERT INTO cad_telefones(TEL_SEQUEN) VALUES (".$nTel_Codigo."); ") or die(MYSQL_ERROR());
      }
	  MYSQL_QUERY(
         "UPDATE cad_telefones SET ".
		    "TEL_TIPCAD=".$nTipCad.",".
			"TEL_CODCAD=".$nCodCad.",".
		    "TEL_CODCTT=" .$aTel['TEL_CODCTT']. ",".
			"TEL_NUMERO='".$aTel['TEL_NUMERO']."',".
			"TEL_OBSERV='".$aTel['TEL_OBSERV']."' ".
		 "WHERE TEL_SEQUEN=".$nTel_Codigo.";"
	  ) Or Die(MYSQL_ERROR());
   }
   $RowsT = MYSQL_QUERY("SELECT TEL_NUMERO, TEL_SEQUEN FROM cad_telefones WHERE TEL_TIPCAD=".$nTipCad." AND TEL_CODCAD=".$nCodCad." ;") Or Die(MYSQL_ERROR());
   While ( $RowT = MYSQL_FETCH_OBJECT($RowsT) ) {
      If ( ! ISSET($a_ListaTelN[ $RowT->TEL_NUMERO ]) ) {
         MYSQL_QUERY("DELETE FROM cad_telefones WHERE TEL_SEQUEN=".$RowT->TEL_SEQUEN."; ") or die(MYSQL_ERROR());
	  }
   }
}
// Puxar os telefones
Function CapturarTelefones($nTipCad, $nCodCad) {
   $aCad_Telefones = ARRAY();

   $sSQLQuery =
      "SELECT TEL_SEQUEN, ".
             "TEL_CODCTT, ".
             "TEL_TIPCAD, ".
             "TEL_CODCAD, ".
             "TEL_NUMERO, ".
             "TEL_OBSERV, ".
             "TEL_TRAREG  ".
      "FROM cad_telefones ".
      "WHERE ".
          "TEL_TIPCAD='".$nTipCad."' AND TEL_CODCAD='".$nCodCad."' AND ".
          "(TEL_TRAREG=0)  ".
      "ORDER BY TEL_NUMERO ASC LIMIT 6;";
   $RowsT = MYSQL_QUERY($sSQLQuery) Or Die($sSQLQuery.'<br>'.MYSQL_ERROR());
   While ($RowT = MYSQL_FETCH_ARRAY($RowsT)) {
     $aCad_Telefones[] = $RowT['TEL_NUMERO'];
   }

   Return $aCad_Telefones;
}
//
?>