clsPmieducarMatriculaTurma.inc.php 42.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
<?php

/**
 * i-Educar - Sistema de gestדo escolar
 *
 * Copyright (C) 2006  Prefeitura Municipal de Itajaם
 *                     <ctima@itajai.sc.gov.br>
 *
 * Este programa י software livre; vocך pode redistribuם-lo e/ou modificב-lo
 * sob os termos da Licenחa Pתblica Geral GNU conforme publicada pela Free
 * Software Foundation; tanto a versדo 2 da Licenחa, como (a seu critיrio)
 * qualquer versדo posterior.
 *
 * Este programa י distribuם­do na expectativa de que seja תtil, porיm, SEM
 * NENHUMA GARANTIA; nem mesmo a garantia implם­cita de COMERCIABILIDADE OU
 * ADEQUAֳַO A UMA FINALIDADE ESPECֽFICA. Consulte a Licenחa Pתblica Geral
 * do GNU para mais detalhes.
 *
 * Vocך deve ter recebido uma cףpia da Licenחa Pתblica Geral do GNU junto
 * com este programa; se nדo, escreva para a Free Software Foundation, Inc., no
 * endereחo 59 Temple Street, Suite 330, Boston, MA 02111-1307 USA.
 *
 * @author    Prefeitura Municipal de Itajaם <ctima@itajai.sc.gov.br>
 * @category  i-Educar
 * @license   @@license@@
 * @package   iEd_Pmieducar
 * @since     Arquivo disponםvel desde a versדo 1.0.0
 * @version   $Id$
 */

require_once 'include/pmieducar/geral.inc.php';
require_once 'Avaliacao/Fixups/CleanComponentesCurriculares.php';

/**
 * clsPmieducarMatriculaTurma class.
 *
 * @author    Prefeitura Municipal de Itajaם <ctima@itajai.sc.gov.br>
 * @category  i-Educar
 * @license   @@license@@
 * @package   iEd_Pmieducar
 * @since     Classe disponםvel desde a versדo 1.0.0
 * @version   @@package_version@@
 */
class clsPmieducarMatriculaTurma
{
  var $ref_cod_matricula;
  var $ref_cod_turma;
  var $ref_usuario_exc;
  var $ref_usuario_cad;
  var $data_cadastro;
  var $data_exclusao;
  var $ativo;
  var $ref_cod_turma_transf;
  var $sequencial;
  var $sequencial_fechamento;

  /**
   * Armazena o total de resultados obtidos na תltima chamada ao mיtodo lista().
   * @var int
   */
  var $_total;

  /**
   * Nome do schema.
   * @var string
   */
  var $_schema;

  /**
   * Nome da tabela.
   * @var string
   */
  var $_tabela;

  /**
   * Lista separada por vםrgula, com os campos que devem ser selecionados na
   * prףxima chamado ao mיtodo lista().
   * @var string
   */
  var $_campos_lista;

  /**
   * Lista com todos os campos da tabela separados por vםrgula, padrדo para
   * seleחדo no mיtodo lista.
   * @var string
   */
  var $_todos_campos;

  /**
   * Valor que define a quantidade de registros a ser retornada pelo mיtodo lista().
   * @var int
   */
  var $_limite_quantidade;

  /**
   * Define o valor de offset no retorno dos registros no mיtodo lista().
   * @var int
   */
  var $_limite_offset;

  /**
   * Define o campo para ser usado como padrדo de ordenaחדo no mיtodo lista().
   * @var string
   */
  var $_campo_order_by;

  /**
   * Construtor.
   */
  function clsPmieducarMatriculaTurma($ref_cod_matricula = NULL,
    $ref_cod_turma = NULL, $ref_usuario_exc = NULL, $ref_usuario_cad = NULL,
    $data_cadastro = NULL, $data_exclusao = NULL, $ativo = NULL,
    $ref_cod_turma_transf = NULL,$sequencial = NULL, $data_enturmacao = NULL
  ) {
    $db = new clsBanco();
    $this->_schema = "pmieducar.";
    $this->_tabela = "{$this->_schema}matricula_turma";

    $this->_campos_lista = $this->_todos_campos = "mt.ref_cod_matricula, mt.remanejado, mt.transferido, mt.ref_cod_turma, mt.ref_usuario_exc, mt.ref_usuario_cad, mt.data_cadastro, mt.data_exclusao, mt.ativo, mt.sequencial, mt.data_enturmacao, (SELECT pes.nome FROM cadastro.pessoa pes, pmieducar.aluno alu, pmieducar.matricula mat WHERE pes.idpes = alu.ref_idpes AND mat.ref_cod_aluno = alu.cod_aluno AND mat.cod_matricula = mt.ref_cod_matricula ) AS nome, (SELECT pes.nome FROM cadastro.pessoa pes, pmieducar.aluno alu, pmieducar.matricula mat WHERE pes.idpes = alu.ref_idpes AND mat.ref_cod_aluno = alu.cod_aluno AND mat.cod_matricula = mt.ref_cod_matricula ) AS nome_ascii";

    if (is_numeric($ref_usuario_exc)) {
      if (class_exists("clsPmieducarUsuario")) {
        $tmp_obj = new clsPmieducarUsuario($ref_usuario_exc);
        if (method_exists($tmp_obj, "existe")) {
          if ($tmp_obj->existe()) {
            $this->ref_usuario_exc = $ref_usuario_exc;
          }
        }
        else if (method_exists($tmp_obj, "detalhe")) {
          if ($tmp_obj->detalhe()) {
            $this->ref_usuario_exc = $ref_usuario_exc;
          }
        }
      }
      else {
        if ($db->CampoUnico("SELECT 1 FROM pmieducar.usuario WHERE cod_usuario = '{$ref_usuario_exc}'")) {
          $this->ref_usuario_exc = $ref_usuario_exc;
        }
      }
    }

    if (is_numeric($ref_usuario_cad)) {
      if (class_exists("clsPmieducarUsuario")) {
        $tmp_obj = new clsPmieducarUsuario($ref_usuario_cad);
        if (method_exists($tmp_obj, "existe")) {
          if ($tmp_obj->existe()) {
            $this->ref_usuario_cad = $ref_usuario_cad;
          }
        }
        else if (method_exists($tmp_obj, "detalhe")) {
          if ($tmp_obj->detalhe()) {
            $this->ref_usuario_cad = $ref_usuario_cad;
          }
        }
      }
      else {
        if ($db->CampoUnico("SELECT 1 FROM pmieducar.usuario WHERE cod_usuario = '{$ref_usuario_cad}'")) {
          $this->ref_usuario_cad = $ref_usuario_cad;
        }
      }
    }

    if (is_numeric($ref_cod_turma)) {
      if (class_exists("clsPmieducarTurma")) {
        $tmp_obj = new clsPmieducarTurma($ref_cod_turma);
        if (method_exists($tmp_obj, "existe")) {
          if ($tmp_obj->existe()) {
            $this->ref_cod_turma = $ref_cod_turma;
          }
        }
        else if (method_exists($tmp_obj, "detalhe")) {
          if ($tmp_obj->detalhe()) {
            $this->ref_cod_turma = $ref_cod_turma;
          }
        }
      }
      else {
        if ($db->CampoUnico("SELECT 1 FROM pmieducar.turma WHERE cod_turma = '{$ref_cod_turma}'")) {
          $this->ref_cod_turma = $ref_cod_turma;
        }
      }
    }

    if (is_numeric($ref_cod_matricula)) {
      if (class_exists("clsPmieducarMatricula")) {
        $tmp_obj = new clsPmieducarMatricula($ref_cod_matricula);
        if (method_exists($tmp_obj, "existe")) {
          if ($tmp_obj->existe()) {
            $this->ref_cod_matricula = $ref_cod_matricula;
          }
        }
        else if (method_exists($tmp_obj, "detalhe")) {
          if ($tmp_obj->detalhe()) {
            $this->ref_cod_matricula = $ref_cod_matricula;
          }
        }
      }
      else {
        if ($db->CampoUnico("SELECT 1 FROM pmieducar.matricula WHERE cod_matricula = '{$ref_cod_matricula}'"))
        {
          $this->ref_cod_matricula = $ref_cod_matricula;
        }
      }
    }

    if (is_string($data_cadastro)) {
      $this->data_cadastro = $data_cadastro;
    }

    if (is_string($data_exclusao)) {
      $this->data_exclusao = $data_exclusao;
    }

    if (is_numeric($ativo)) {
      $this->ativo = $ativo;
    }

    if (is_numeric($ref_cod_turma_transf)) {
      if (class_exists("clsPmieducarTurma")) {
        $tmp_obj = new clsPmieducarTurma($ref_cod_turma_transf);
        if (method_exists($tmp_obj, "existe")) {
          if ($tmp_obj->existe()) {
            $this->ref_cod_turma_transf = $ref_cod_turma_transf;
          }
        }
        else if (method_exists($tmp_obj, "detalhe")) {
          if ($tmp_obj->detalhe()) {
            $this->ref_cod_turma_transf = $ref_cod_turma_transf;
          }
        }
      }
      else {
        if ($db->CampoUnico("SELECT 1 FROM pmieducar.turma WHERE cod_turma = '{$ref_cod_turma_transf}'")) {
          $this->ref_cod_turma_transf = $ref_cod_turma_transf;
        }
      }
    }

    if (is_numeric($sequencial)) {
      $this->sequencial = $sequencial;
    }

    if (is_string($data_enturmacao)) {
      $this->data_enturmacao = $data_enturmacao;
    }
  }

  /**
   * Cria um novo registro.
   * @return bool
   */
  function cadastra()
  {
    if (is_numeric($this->ref_cod_matricula) && is_numeric($this->ref_cod_turma) &&
      is_numeric($this->ref_usuario_cad))
    {
      $db = new clsBanco();

      $campos = "";
      $valores = "";
      $gruda = "";

      if (is_numeric($this->ref_cod_matricula)) {
        $campos .= "{$gruda}ref_cod_matricula";
        $valores .= "{$gruda}'{$this->ref_cod_matricula}'";
        $gruda = ", ";
      }

      if (is_numeric($this->ref_cod_turma)) {
        $campos .= "{$gruda}ref_cod_turma";
        $valores .= "{$gruda}'{$this->ref_cod_turma}'";
        $gruda = ", ";
      }

      if (is_numeric($this->ref_usuario_cad)) {
        $campos .= "{$gruda}ref_usuario_cad";
        $valores .= "{$gruda}'{$this->ref_usuario_cad}'";
        $gruda = ", ";
      }

      $this->sequencial = $this->buscaSequencialMax();

      $campos .= "{$gruda}sequencial";
      $valores .= "{$gruda}'{$this->sequencial}'";
      $gruda = ", ";

      $campos .= "{$gruda}data_cadastro";
      $valores .= "{$gruda}NOW()";
      $gruda = ", ";

      $campos .= "{$gruda}ativo";
      $valores .= "{$gruda}'1'";
      $gruda = ", ";

      if (is_string($this->data_enturmacao)) {
        $campos .= "{$gruda}data_enturmacao";
        $valores .= "{$gruda}'{$this->data_enturmacao}'";
        $gruda = ", ";
      }

      $this->sequencial_fechamento = $this->getSequencialFechamento($this->ref_cod_matricula, $this->ref_cod_turma, $this->data_enturmacao);

      if(is_numeric($this->sequencial_fechamento)){
        $campos .= "{$gruda}sequencial_fechamento";
        $valores .= "{$gruda}'{$this->sequencial_fechamento}'";
        $gruda = ", ";
      }

      $db->Consulta("INSERT INTO {$this->_tabela} ($campos) VALUES ($valores)");

      $this->limpaComponentesCurriculares();

      return TRUE;
    }

    return FALSE;
  }

  /**
   * Edita os dados de um registro.
   * @return bool
   */
  function edita()
  {
    if (is_numeric($this->ref_cod_matricula) && is_numeric($this->ref_cod_turma) &&
      is_numeric($this->ref_usuario_exc) && is_numeric($this->sequencial)) {
      $db = new clsBanco();
      $set = "";

      if (is_numeric($this->ref_usuario_exc)) {
        $set .= "{$gruda}ref_usuario_exc = '{$this->ref_usuario_exc}'";
        $gruda = ", ";
      }

      if (is_numeric($this->ref_usuario_cad)) {
        $set .= "{$gruda}ref_usuario_cad = '{$this->ref_usuario_cad}'";
        $gruda = ", ";
      }

      if (is_string($this->data_cadastro)) {
        $set .= "{$gruda}data_cadastro = '{$this->data_cadastro}'";
        $gruda = ", ";
      }

      $set .= "{$gruda}data_exclusao = NOW()";
      $gruda = ", ";

      if (is_numeric($this->ativo)) {
        $set .= "{$gruda}ativo = '{$this->ativo}'";
        $gruda = ", ";
        if ($this->ativo == 1){
          $set .= "{$gruda}remanejado = null, transferido = null";
          $gruda = ", ";
        }
      }

      if (is_numeric($this->ref_cod_turma_transf)) {
        $set .= "{$gruda}ref_cod_turma= '{$this->ref_cod_turma_transf}'";
        $gruda = ", ";
      }

      if (is_string($this->data_enturmacao)) {
        $set .= "{$gruda}data_enturmacao = '{$this->data_enturmacao}'";
        $gruda = ", ";
      }

      if ($set) {
        $db->Consulta("UPDATE {$this->_tabela} SET $set WHERE ref_cod_matricula = '{$this->ref_cod_matricula}' AND ref_cod_turma = '{$this->ref_cod_turma}' and sequencial = '$this->sequencial' ");
        $this->limpaComponentesCurriculares();
        return TRUE;
      }
    }

    return FALSE;
  }

  /**
   * Retorna uma lista de registros filtrados de acordo com os parגmetros.
   * @return array
   */
  function lista($int_ref_cod_matricula = NULL, $int_ref_cod_turma = NULL,
    $int_ref_usuario_exc = NULL, $int_ref_usuario_cad = NULL,
    $date_data_cadastro_ini = NULL, $date_data_cadastro_fim = NULL,
    $date_data_exclusao_ini = NULL, $date_data_exclusao_fim = NULL, $int_ativo = NULL,
    $int_ref_cod_serie = NULL, $int_ref_cod_curso = NULL, $int_ref_cod_escola = NULL,
    $int_ref_cod_instituicao = NULL, $int_ref_cod_aluno = NULL, $mes = NULL,
    $aprovado = NULL, $mes_menor_que = NULL, $int_sequencial = NULL,
    $int_ano_matricula = NULL, $tem_avaliacao = NULL, $bool_get_nome_aluno = FALSE,
    $bool_aprovados_reprovados = NULL, $int_ultima_matricula = NULL,
    $bool_matricula_ativo = NULL, $bool_escola_andamento = FALSE,
    $mes_matricula_inicial = FALSE, $get_serie_mult = FALSE,
    $int_ref_cod_serie_mult = NULL, $int_semestre = NULL,
    $pegar_ano_em_andamento = FALSE, $parar=NULL)
  {
    if ($bool_get_nome_aluno === true) {
      $nome = " ,pessoa.nome as nome_aluno";
    }
    $tab_aluno = ", {$this->_schema}aluno a";
    $where_nm_aluno = " AND a.cod_aluno = m.ref_cod_aluno AND a.ativo=1";

    if ( $bool_escola_andamento) {
      if ($pegar_ano_em_andamento) {
        $from = ", pmieducar.escola_ano_letivo eal ";

        $where = " AND eal.ref_cod_escola = m.ref_ref_cod_escola
              AND eal.ano = (select max(ano) from pmieducar.escola_ano_letivo let where
                      let.ref_cod_escola= eal.ref_cod_escola and andamento=1)
              AND eal.ano = m.ano
              AND eal.andamento = '1' ";
      }
      else {
        $ano = date("Y");

        $from = ", pmieducar.escola_ano_letivo eal ";

        $where = " AND eal.ref_cod_escola = m.ref_ref_cod_escola
              AND eal.ano = '{$ano}'
              AND eal.ano = m.ano
              AND eal.andamento = '1' ";
      }
    }

    $sql = "SELECT {$this->_campos_lista}, c.nm_curso, t.nm_turma, i.nm_instituicao, m.ref_ref_cod_serie, m.ref_cod_curso, m.ref_ref_cod_escola, c.ref_cod_instituicao, m.ref_cod_aluno,t.hora_inicial $nome FROM {$this->_tabela} mt, {$this->_schema}matricula m, {$this->_schema}curso c, {$this->_schema}turma t, {$this->_schema}instituicao i{$tab_aluno} {$from}, cadastro.pessoa ";

    $whereAnd = " AND ";
    $filtros = " WHERE mt.ref_cod_matricula = m.cod_matricula AND idpes = a.ref_idpes AND m.ref_cod_curso = c.cod_curso AND t.cod_turma = mt.ref_cod_turma AND i.cod_instituicao = c.ref_cod_instituicao {$where_nm_aluno} {$where}";

    if (is_numeric($int_ref_cod_matricula)) {
      $filtros .= "{$whereAnd} mt.ref_cod_matricula = '{$int_ref_cod_matricula}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_turma)) {
      $filtros .= "{$whereAnd} mt.ref_cod_turma = '{$int_ref_cod_turma}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_usuario_exc)) {
      $filtros .= "{$whereAnd} mt.ref_usuario_exc = '{$int_ref_usuario_exc}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_usuario_cad)) {
      $filtros .= "{$whereAnd} mt.ref_usuario_cad = '{$int_ref_usuario_cad}'";
      $whereAnd = " AND ";
    }

    if (is_string($date_data_cadastro_ini)) {
      $filtros .= "{$whereAnd} mt.data_cadastro >= '{$date_data_cadastro_ini}'";
      $whereAnd = " AND ";
    }

    if (is_string($date_data_cadastro_fim)) {
      $filtros .= "{$whereAnd} mt.data_cadastro <= '{$date_data_cadastro_fim}'";
      $whereAnd = " AND ";
    }

    if (is_string($date_data_exclusao_ini)) {
      $filtros .= "{$whereAnd} mt.data_exclusao >= '{$date_data_exclusao_ini}'";
      $whereAnd = " AND ";
    }

    if (is_string($date_data_exclusao_fim)) {
      $filtros .= "{$whereAnd} mt.data_exclusao <= '{$date_data_exclusao_fim}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ativo)) {
      if ($int_ativo == 1) {
        $filtros .= "{$whereAnd} mt.ativo = '1'";
        $whereAnd = " AND ";
      }elseif ($int_ativo == 2) {
        $filtros .= "{$whereAnd} (mt.ativo = '1' OR mt.transferido OR mt.remanejado)";
        $whereAnd = " AND ";
      }
      else {
        $filtros .= "{$whereAnd} mt.ativo = '0'";
        $whereAnd = " AND ";
      }
    }

    if (!is_null($bool_matricula_ativo) && is_bool($bool_matricula_ativo)) {
      if ($bool_matricula_ativo) {
        $filtros .= "{$whereAnd} m.ativo = '1'";
        $whereAnd = " AND ";
      }
      else {
        $filtros .= "{$whereAnd} m.ativo = '0'";
        $whereAnd = " AND ";
      }
    }

    if (is_numeric($int_ref_cod_serie)) {
      if (!is_numeric($int_ref_cod_serie_mult)) {
        $filtros .= "{$whereAnd} m.ref_ref_cod_serie = '{$int_ref_cod_serie}'";
        $whereAnd = " AND ";
      }
      else {
        $filtros .= "{$whereAnd} (m.ref_ref_cod_serie = '{$int_ref_cod_serie}' OR ref_ref_cod_serie_mult='{$int_ref_cod_serie_mult}')";
        $whereAnd = " AND ";
      }
    }

    if (is_numeric($int_ref_cod_curso)) {
      $filtros .= "{$whereAnd} m.ref_cod_curso = '{$int_ref_cod_curso}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_escola)) {
      $filtros .= "{$whereAnd} m.ref_ref_cod_escola = '{$int_ref_cod_escola}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_instituicao)) {
      $filtros .= "{$whereAnd} c.ref_cod_instituicao = '{$int_ref_cod_instituicao}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_aluno)) {
      $filtros .= "{$whereAnd} m.ref_cod_aluno = '{$int_ref_cod_aluno}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ultima_matricula)) {
      $filtros .= "{$whereAnd} m.ultima_matricula = '{$int_ultima_matricula}'";
      $whereAnd = " AND ";
    }

    if (is_array($aprovado)) {
      $filtros .= "{$whereAnd} ( ";
      $whereAnd = "";

      foreach ($aprovado as $value) {
        $filtros .= "{$whereAnd} m.aprovado = '{$value}'";
        $whereAnd = " OR ";
      }

      $filtros .= " )";
      $whereAnd = " AND ";
    }
    elseif (is_numeric($aprovado)) {
      $filtros .= "{$whereAnd} m.aprovado = '{$aprovado}' ";
      $whereAnd = " AND ";
    }

    if (is_bool($bool_aprovados_reprovados)) {
      if ($bool_aprovados_reprovados == true) {
        $filtros .= "{$whereAnd} ( m.aprovado = '1'";
        $whereAnd = " OR ";
        $filtros .= "{$whereAnd} m.aprovado = '2' )";
        $whereAnd = " AND ";
      }
    }

    if ($int_ano_matricula) {
      $int_ano_matricula = (int) $int_ano_matricula;
      $filtros .= "{$whereAnd} m.ano = '{$int_ano_matricula}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_semestre))  {
      $filtros .= "{$whereAnd} m.semestre = '{$int_semestre}'";
      $whereAnd = " AND ";
    }

    if ($mes) {
      $mes = (int) $mes;
        $filtros .= "{$whereAnd} ( to_char(mt.data_cadastro,'MM')::int = '$mes'
                      OR to_char(mt.data_exclusao,'MM')::int = '$mes' )";
      $whereAnd = " AND ";
    }

    if ($mes_menor_que) {
      $mes_menor_que = (int) $mes_menor_que;
        $filtros .= "{$whereAnd} ( ( to_char(mt.data_cadastro,'MM')::int < '$mes_menor_que' AND mt.data_exclusao  IS NULL )
                    OR ( to_char(mt.data_exclusao,'MM')::int < '$mes_menor_que'  AND mt.data_exclusao  IS NOT NULL ) )";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_sequencial)) {
      $filtros .= "{$whereAnd} mt.sequencial = '{$int_sequencial}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($mes_matricula_inicial)) {
      $filtros .= "AND ((TO_CHAR(mt.data_cadastro,'MM')::int < '{$mes_matricula_inicial}'
                  AND NOT EXISTS (SELECT 1
                                FROM pmieducar.transferencia_solicitacao ts
                             WHERE ref_cod_matricula_saida = m.cod_matricula
                               AND to_char(ts.data_cadastro,'MM')::int < '{$mes_matricula_inicial}' )
                    )
                    OR (TO_CHAR(mt.data_cadastro,'MM')::int < '{$mes_matricula_inicial}'
                         AND EXISTS (SELECT 1
                           FROM pmieducar.transferencia_solicitacao
                          WHERE ref_cod_matricula_saida = m.cod_matricula
                            AND (TO_CHAR(data_transferencia,'MM')::int = '{$mes_matricula_inicial}' OR m.aprovado = 3))
                    )
              )
              and not(TO_CHAR(mt.data_exclusao,'MM')::int < '$mes_matricula_inicial' and mt.ativo = 0)
              ";
    }

    $db = new clsBanco();
    $countCampos = count(explode(',', $this->_campos_lista));
    $resultado = array();
    $sql .= $filtros . $this->getOrderby() . $this->getLimite();

    if ($parar) {
      die($sql);
    }

    $this->_total = $db->CampoUnico("SELECT COUNT(0) FROM {$this->_tabela} mt, cadastro.pessoa, {$this->_schema}matricula m, {$this->_schema}curso c, {$this->_schema}turma t, {$this->_schema}instituicao i{$tab_aluno} {$from} {$filtros} {$where}");
    $db->Consulta($sql);

    if ($countCampos > 1) {
      while ($db->ProximoRegistro()) {
        $tupla = $db->Tupla();

        $tupla["_total"] = $this->_total;
        $resultado[] = $tupla;
      }
    }
    else {
      while ($db->ProximoRegistro()) {
        $tupla = $db->Tupla();
        $resultado[] = $tupla[$this->_campos_lista];
      }
    }

    if (count($resultado)) {
      return $resultado;
    }

    return FALSE;
  }

  function lista2($int_ref_cod_matricula = NULL, $int_ref_cod_turma = NULL,
    $int_ref_usuario_exc = NULL, $int_ref_usuario_cad = NULL,
    $date_data_cadastro_ini = NULL, $date_data_cadastro_fim = NULL,
    $date_data_exclusao_ini = NULL, $date_data_exclusao_fim = NULL,
    $int_ativo = NULL, $int_ref_cod_serie = NULL, $int_ref_cod_curso = NULL,
    $int_ref_cod_escola = NULL, $int_ref_cod_instituicao = NULL,
    $int_ref_cod_aluno = NULL, $em_andamento = TRUE, $mes = NULL, $aprovado = NULL,
    $mes_menor_que = NULL, $int_sequencial = NULL, $int_ano_matricula = NULL
  )
  {
    $sql = "SELECT {$this->_campos_lista}, m.ref_ref_cod_serie, m.ref_cod_curso, m.ref_ref_cod_escola, c.ref_cod_instituicao, i.nm_instituicao, m.ref_cod_aluno,t.nm_turma,s.nm_serie,c.nm_curso FROM {$this->_tabela} mt, {$this->_schema}matricula m, {$this->_schema}curso c, {$this->_schema}turma t left outer join {$this->_schema}serie s on (t.ref_ref_cod_serie = s.cod_serie), {$this->_schema}instituicao i";
    $filtros = "";

    $whereAnd = " WHERE mt.ref_cod_matricula = m.cod_matricula AND m.ref_cod_curso = c.cod_curso AND mt.ref_cod_turma = t.cod_turma AND c.ref_cod_instituicao = i.cod_instituicao AND";

    if (is_numeric($int_ref_cod_matricula)) {
      $filtros .= "{$whereAnd} mt.ref_cod_matricula = '{$int_ref_cod_matricula}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_turma)) {
      $filtros .= "{$whereAnd} mt.ref_cod_turma = '{$int_ref_cod_turma}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_usuario_exc)) {
      $filtros .= "{$whereAnd} mt.ref_usuario_exc = '{$int_ref_usuario_exc}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_usuario_cad)) {
      $filtros .= "{$whereAnd} mt.ref_usuario_cad = '{$int_ref_usuario_cad}'";
      $whereAnd = " AND ";
    }

    if (is_string($date_data_cadastro_ini)) {
      $filtros .= "{$whereAnd} mt.data_cadastro >= '{$date_data_cadastro_ini}'";
      $whereAnd = " AND ";
    }

    if (is_string($date_data_cadastro_fim)) {
      $filtros .= "{$whereAnd} mt.data_cadastro <= '{$date_data_cadastro_fim}'";
      $whereAnd = " AND ";
    }

    if (is_string($date_data_exclusao_ini)) {
      $filtros .= "{$whereAnd} mt.data_exclusao >= '{$date_data_exclusao_ini}'";
      $whereAnd = " AND ";
    }

    if (is_string($date_data_exclusao_fim)) {
      $filtros .= "{$whereAnd} mt.data_exclusao <= '{$date_data_exclusao_fim}'";
      $whereAnd = " AND ";
    }

    if (is_null($int_ativo) || $int_ativo) {
      $filtros .= "{$whereAnd} mt.ativo = '1'";
      $whereAnd = " AND ";
    }
    else {
      $filtros .= "{$whereAnd} mt.ativo = '0'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_serie)) {
      $filtros .= "{$whereAnd} m.ref_ref_cod_serie = '{$int_ref_cod_serie}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_curso)) {
      $filtros .= "{$whereAnd} m.ref_cod_curso = '{$int_ref_cod_curso}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_escola)) {
      $filtros .= "{$whereAnd} m.ref_ref_cod_escola = '{$int_ref_cod_escola}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_instituicao)) {
      $filtros .= "{$whereAnd} c.ref_cod_instituicao = '{$int_ref_cod_instituicao}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_aluno)) {
      $filtros .= "{$whereAnd} m.ref_cod_aluno = '{$int_ref_cod_aluno}'";
      $whereAnd = " AND ";
    }

    if (!is_numeric($aprovado)) {
      if ($em_andamento == TRUE) {
        $filtros .= "{$whereAnd} (m.aprovado = '3'";
        $whereAnd = " OR ";
        $filtros .= "{$whereAnd} m.aprovado = '7')";
        $whereAnd = " AND ";
      }
    }

    if($int_ano_matricula) {
      $int_ano_matricula = (int) $int_ano_matricula;
      $filtros .= "{$whereAnd} (to_char(m.ano,'YYYY')::int = '$int_ano_matricula')";
      $whereAnd = " AND ";
    }

    if ($mes) {
      $mes = (int) $mes;
        $filtros .= "{$whereAnd} (to_char(mt.data_cadastro,'MM')::int = '$mes'
                      OR to_char(mt.data_exclusao,'MM')::int = '$mes')";
      $whereAnd = " AND ";
    }

    if($mes_menor_que) {
      $mes_menor_que = (int) $mes_menor_que;
        $filtros .= "{$whereAnd} (to_char(mt.data_cadastro,'MM')::int < '$mes_menor_que'
                      OR to_char(mt.data_exclusao,'MM')::int < '$mes_menor_que')";
      $whereAnd = " AND ";
    }

    if(is_numeric($aprovado)) {
        $filtros .= "{$whereAnd} m.aprovado = '$aprovado'";
        $whereAnd = " AND ";
    }

    if (is_numeric($int_sequencial)) {
      $filtros .= "{$whereAnd} mt.sequencial = '{$int_sequencial}'";
      $whereAnd = " AND ";
    }

    $db = new clsBanco();
    $countCampos = count(explode(',', $this->_campos_lista));
    $resultado = array();

    $sql .= $filtros . $this->getOrderby() . $this->getLimite();

    $this->_total = $db->CampoUnico( "SELECT COUNT(0) FROM {$this->_tabela} mt, {$this->_schema}matricula m, {$this->_schema}curso c, {$this->_schema}turma t left outer join {$this->_schema}serie s on (t.ref_ref_cod_serie = s.cod_serie), {$this->_schema}instituicao i {$filtros}" );

    $db->Consulta($sql);

    if ($countCampos > 1) {
      while ($db->ProximoRegistro()) {
        $tupla = $db->Tupla();

        $tupla["_total"] = $this->_total;
        $resultado[] = $tupla;
      }
    }
    else {
      while ($db->ProximoRegistro()) {
        $tupla = $db->Tupla();
        $resultado[] = $tupla[$this->_campos_lista];
      }
    }

    if (count($resultado)) {
      return $resultado;
    }

    return FALSE;
  }

  function lista3($int_ref_cod_matricula = NULL, $int_ref_cod_turma = NULL,
    $int_ref_usuario_exc = NULL, $int_ref_usuario_cad = NULL,
    $date_data_cadastro_ini = NULL, $date_data_cadastro_fim = NULL,
    $date_data_exclusao_ini = NULL, $date_data_exclusao_fim = NULL,
    $int_ativo = NULL, $int_ref_cod_serie = NULL, $int_ref_cod_curso = NULL,
    $int_ref_cod_escola = NULL, $int_ref_cod_aluno = NULL, $aprovado = NULL,
    $int_sequencial = NULL, $int_ano_matricula = NULL, $int_ultima_matricula = NULL,
    $int_matricula_ativo = NULL, $int_semestre = NULL
  ) {
    $sql = "SELECT {$this->_campos_lista}, c.nm_curso, s.nm_serie, t.nm_turma, c.ref_cod_instituicao, m.ref_ref_cod_escola, m.ref_cod_curso, m.ref_ref_cod_serie, m.ref_cod_aluno, p.nome,a.tipo_responsavel,f.data_nasc FROM {$this->_tabela} mt, {$this->_schema}matricula m, {$this->_schema}curso c, {$this->_schema}serie s, {$this->_schema}turma t, {$this->_schema}aluno a, cadastro.pessoa p, cadastro.fisica f {$join}";
    $filtros = "";

    $whereAnd = " WHERE mt.ref_cod_matricula = m.cod_matricula AND m.ref_cod_curso = c.cod_curso AND t.cod_turma = mt.ref_cod_turma AND s.cod_serie = m.ref_ref_cod_serie AND a.cod_aluno = m.ref_cod_aluno AND p.idpes = a.ref_idpes AND p.idpes = f.idpes AND";

    if (is_numeric($int_ref_cod_matricula)) {
      $filtros .= "{$whereAnd} mt.ref_cod_matricula = '{$int_ref_cod_matricula}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_turma)) {
      $filtros .= "{$whereAnd} mt.ref_cod_turma = '{$int_ref_cod_turma}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_usuario_exc)) {
      $filtros .= "{$whereAnd} mt.ref_usuario_exc = '{$int_ref_usuario_exc}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_usuario_cad)) {
      $filtros .= "{$whereAnd} mt.ref_usuario_cad = '{$int_ref_usuario_cad}'";
      $whereAnd = " AND ";
    }

    if (is_string($date_data_cadastro_ini)) {
      $filtros .= "{$whereAnd} mt.data_cadastro >= '{$date_data_cadastro_ini}'";
      $whereAnd = " AND ";
    }

    if (is_string($date_data_cadastro_fim)) {
      $filtros .= "{$whereAnd} mt.data_cadastro <= '{$date_data_cadastro_fim}'";
      $whereAnd = " AND ";
    }

    if (is_string($date_data_exclusao_ini)) {
      $filtros .= "{$whereAnd} mt.data_exclusao >= '{$date_data_exclusao_ini}'";
      $whereAnd = " AND ";
    }

    if (is_string($date_data_exclusao_fim)) {
      $filtros .= "{$whereAnd} mt.data_exclusao <= '{$date_data_exclusao_fim}'";
      $whereAnd = " AND ";
    }

    if (is_null($int_ativo) || $int_ativo) {
      $filtros .= "{$whereAnd} mt.ativo = '1'";
      $whereAnd = " AND ";
    }
    else {
      $filtros .= "{$whereAnd} mt.ativo = '0'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_serie)) {
      $filtros .= "{$whereAnd} m.ref_ref_cod_serie = '{$int_ref_cod_serie}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_curso)) {
      $filtros .= "{$whereAnd} m.ref_cod_curso = '{$int_ref_cod_curso}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_escola)) {
      $filtros .= "{$whereAnd} m.ref_ref_cod_escola = '{$int_ref_cod_escola}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_instituicao)) {
      $filtros .= "{$whereAnd} c.ref_cod_instituicao = '{$int_ref_cod_instituicao}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ref_cod_aluno)) {
      $filtros .= "{$whereAnd} m.ref_cod_aluno = '{$int_ref_cod_aluno}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_ultima_matricula)) {
      $filtros .= "{$whereAnd} m.ultima_matricula = '{$int_ultima_matricula}'";
      $whereAnd = " AND ";
    }

    if (is_array($aprovado)) {
      $filtros .= "{$whereAnd} ( ";
      $whereAnd = "";

      foreach ($aprovado as $value) {
        $filtros .= "{$whereAnd} m.aprovado = '{$value}'";
        $whereAnd = " OR ";
      }

      $filtros .= " )";
      $whereAnd = " AND ";
    }
    elseif(is_numeric($aprovado)) {
      $filtros .= "{$whereAnd} m.aprovado = '{$aprovado}' ";
      $whereAnd = " AND ";
    }

    if($int_ano_matricula)
    {
      $int_ano_matricula = (int) $int_ano_matricula;
        $filtros .= "{$whereAnd} m.ano = '{$int_ano_matricula}'";
      $whereAnd = " AND ";
    }

    if (is_numeric($int_sequencial)) {
      $filtros .= "{$whereAnd} mt.sequencial = '{$int_sequencial}'";
      $whereAnd = " AND ";
    }

    if (!is_null($int_matricula_ativo) && is_bool($int_matricula_ativo)) {
      if ($int_matricula_ativo) {
        $filtros .= "{$whereAnd} m.ativo = '1'";
        $whereAnd = " AND ";
      }
      else {
        $filtros .= "{$whereAnd} m.ativo = '0'";
        $whereAnd = " AND ";
      }
    }

    if (is_numeric($int_semestre)) {
      $filtros .= "{$whereAnd} m.semestre = '{$int_semestre}'";
      $whereAnd = " AND ";
    }

    $db = new clsBanco();
    $countCampos = count(explode(',', $this->_campos_lista));
    $resultado = array();

    $sql .= $filtros . $this->getOrderby() . $this->getLimite();

    $this->_total = $db->CampoUnico("SELECT COUNT(0) FROM {$this->_tabela} mt, {$this->_schema}matricula m, {$this->_schema}curso c, {$this->_schema}serie s, {$this->_schema}turma t, {$this->_schema}aluno a, cadastro.pessoa p, cadastro.fisica f {$join} {$filtros}");
    $db->Consulta($sql);

    if ($countCampos > 1) {
      while ($db->ProximoRegistro()) {
        $tupla = $db->Tupla();

        $tupla["_total"] = $this->_total;
        $resultado[] = $tupla;
      }
    }
    else {
      while ($db->ProximoRegistro()) {
        $tupla = $db->Tupla();
        $resultado[] = $tupla[$this->_campos_lista];
      }
    }

    if (count($resultado)) {
      return $resultado;
    }

    return FALSE;
  }

  /**
   * Retorna um array com os dados de um registro.
   * @return array
   */
  function detalhe()
  {
    if (is_numeric($this->ref_cod_matricula) && is_numeric($this->ref_cod_turma) &&
      is_numeric($this->sequencial)) {

      $db = new clsBanco();
      $db->Consulta("SELECT {$this->_todos_campos} FROM {$this->_tabela} mt WHERE mt.ref_cod_matricula = '{$this->ref_cod_matricula}' AND mt.ref_cod_turma = '{$this->ref_cod_turma}' AND mt.sequencial = '{$this->sequencial}'");
      $db->ProximoRegistro();
      return $db->Tupla();
    }

    return FALSE;
  }

  /**
   * Retorna um array com os dados de um registro.
   * @return array
   */
  function existe()
  {
    if (is_numeric($this->ref_cod_matricula) && is_numeric($this->ref_cod_turma) &&
      is_numeric($this->sequencial)) {
      $db = new clsBanco();
      $db->Consulta( "SELECT 1 FROM {$this->_tabela} WHERE ref_cod_matricula = '{$this->ref_cod_matricula}' AND ref_cod_turma = '{$this->ref_cod_turma}' AND sequencial = '{$this->sequencial}'" );
      $db->ProximoRegistro();
      return $db->Tupla();
    }

    return FALSE;
  }

  /**
   * Exclui um registro.
   * @return bool
   */
  function excluir()
  {
    if (is_numeric($this->ref_cod_matricula) && is_numeric($this->ref_cod_turma) &&
      is_numeric($this->ref_usuario_exc) && is_numeric($this->sequencial)) {
      $this->ativo = 0;
      return $this->edita();
    }

    return FALSE;
  }

  /**
   * Define quais campos da tabela serדo selecionados no mיtodo Lista().
   */
  function setCamposLista($str_campos)
  {
    $this->_campos_lista = $str_campos;
  }

  /**
   * Define que o mיtodo Lista() deverpa retornar todos os campos da tabela.
   */
  function resetCamposLista()
  {
    $this->_campos_lista = $this->_todos_campos;
  }

  /**
   * Define limites de retorno para o mיtodo Lista().
   */
  function setLimite($intLimiteQtd, $intLimiteOffset = NULL)
  {
    $this->_limite_quantidade = $intLimiteQtd;
    $this->_limite_offset = $intLimiteOffset;
  }

  /**
   * Retorna a string com o trecho da query responsבvel pelo limite de
   * registros retornados/afetados.
   *
   * @return string
   */
  function getLimite()
  {
    if (is_numeric($this->_limite_quantidade)) {
      $retorno = " LIMIT {$this->_limite_quantidade}";
      if (is_numeric($this->_limite_offset)) {
        $retorno .= " OFFSET {$this->_limite_offset} ";
      }
      return $retorno;
    }
    return '';
  }

  /**
   * Define o campo para ser utilizado como ordenaחדo no mיtodo Lista().
   */
  function setOrderby($strNomeCampo)
  {
    if (is_string($strNomeCampo) && $strNomeCampo ) {
      $this->_campo_order_by = $strNomeCampo;
    }
  }

  /**
   * Retorna a string com o trecho da query responsבvel pela Ordenaחדo dos
   * registros.
   *
   * @return string
   */
  function getOrderby()
  {
    if (is_string($this->_campo_order_by)) {
      return " ORDER BY {$this->_campo_order_by} ";
    }
    return '';
  }

  function buscaSequencialMax()
  {
    if (is_numeric($this->ref_cod_matricula) && is_numeric($this->ref_cod_turma)) {
      $db = new clsBanco();
      $max = $db->CampoUnico("SELECT COALESCE(MAX(sequencial),0) + 1 AS MAX FROM {$this->_tabela} WHERE ref_cod_matricula = '{$this->ref_cod_matricula}'");

      //removido filtro pois tornou-se possivel enturmar uma matricula em mais de uma turma
      //AND ref_cod_turma = '{$this->ref_cod_turma}'");

      return $max;
    }

    return FALSE;
  }

  function alunosNaoEnturmados($ref_cod_escola = NULL, $ref_cod_serie = NULL,
    $ref_cod_curso = NULL, $ano = NULL)
  {
    if ((is_numeric($ref_cod_escola) && is_numeric($ref_cod_serie)) ||
      is_numeric($ref_cod_curso)) {
      $db = new clsBanco();

      $sql = "SELECT
            m.cod_matricula
          FROM
            pmieducar.matricula m
          WHERE
            m.cod_matricula NOT IN
            (
              SELECT
                ref_cod_matricula
              FROM
                {$this->_tabela} mt,
                   pmieducar.turma t
              WHERE
                t.cod_turma = mt.ref_cod_turma
                AND t.ref_ref_cod_escola = '{$ref_cod_escola}'
                AND mt.ativo = '1'
                AND t.ativo  = '1'";

      if ($ref_cod_curso) {
        $sql .= " AND m.ref_cod_curso = t.ref_cod_curso  ";
      }

      if ($ref_cod_serie) {
        $sql .= " AND m.ref_ref_cod_serie = t.ref_ref_cod_serie";
      }

      $sql .= ")
          AND m.ativo = '1'
          AND m.ultima_matricula = '1'
          AND
          (
            m.aprovado = '1'
            OR m.aprovado = '2'
            OR m.aprovado = '3'
          )";

      if ($ref_cod_curso) {
        $sql .= " AND m.ref_cod_curso = '{$ref_cod_curso}'";
      }

      if ($ref_cod_escola && $ref_cod_serie) {
        $sql .= " AND m.ref_ref_cod_serie = '{$ref_cod_serie}'
            AND m.ref_ref_cod_escola = '{$ref_cod_escola}'";
      }

      if (is_numeric($ano)) {
        $sql .= " AND m.ano = {$ano}";
      }

      $db->Consulta($sql);

      $resultado = array();
      while ($db->ProximoRegistro()) {
        $tupla = $db->Tupla();
        $resultado[] = $tupla["cod_matricula"];
      }

      return $resultado;
    }

    return FALSE;
  }

  function marcaAlunoRemanejado($data = null){

    if ($this->ref_cod_matricula && $this->sequencial){
      $db = new clsBanco();
      $cod_instituicao = $db->CampoUnico("SELECT ref_cod_instituicao from pmieducar.escola
                                            INNER JOIN pmieducar.matricula ON (ref_ref_cod_escola = cod_escola)
                                            WHERE cod_matricula = {$this->ref_cod_matricula}");
      $dataBaseRemanejamento = $db->CampoUnico("SELECT data_base_remanejamento
                                                  FROM pmieducar.instituicao WHERE cod_instituicao = {$cod_instituicao}");
      $data = $data ? $data : date('Y-m-d');
      if ($dataBaseRemanejamento && strtotime($dataBaseRemanejamento) < strtotime($data) ) {
        $db->CampoUnico("UPDATE pmieducar.matricula_turma SET remanejado = TRUE WHERE ref_cod_matricula = {$this->ref_cod_matricula} AND sequencial = {$this->sequencial}");
      }
    }
  }

  function marcaAlunoTransferido($data = null){

    if ($this->ref_cod_matricula && $this->sequencial){
      $db = new clsBanco();
      $cod_instituicao = $db->CampoUnico("SELECT ref_cod_instituicao from pmieducar.escola
                                            INNER JOIN pmieducar.matricula ON (ref_ref_cod_escola = cod_escola)
                                            WHERE cod_matricula = {$this->ref_cod_matricula}");
      $dataBaseRemanejamento = $db->CampoUnico("SELECT data_base_transferencia
                                                  FROM pmieducar.instituicao WHERE cod_instituicao = {$cod_instituicao}");
      $data = $data ? $data : date('Y-m-d');
      if ($dataBaseRemanejamento && strtotime($dataBaseRemanejamento) < strtotime($data) ) {
        $db->CampoUnico("UPDATE pmieducar.matricula_turma SET transferido = TRUE WHERE ref_cod_matricula = {$this->ref_cod_matricula} AND sequencial = {$this->sequencial}");
      }
    }
  }

  function dadosAlunosNaoEnturmados($ref_cod_escola = NULL, $ref_cod_serie = NULL,
    $ref_cod_curso = NULL, $int_ano = NULL, $verificar_multiseriado = FALSE,
    $semestre = NULL)
  {
    if (is_numeric($int_ano) && (is_numeric($ref_cod_escola) ||
      is_numeric($ref_cod_serie) || is_numeric($ref_cod_curso))) {
      $db = new clsBanco();
      $complemento_sql = '';

      if ($verificar_multiseriado) {
        $complemento_sql = ", m.ref_ref_cod_escola
                  , m.cod_matricula";
      }

      $sql = "
      SELECT
        a.cod_aluno
        , p.nome
        , m.ref_ref_cod_serie
        , s.ref_cod_curso
        , s.nm_serie
        , c.nm_curso
        , f.sexo
        , f.data_nasc
        , a.tipo_responsavel
        {$complemento_sql}
      FROM
        pmieducar.matricula m
        , pmieducar.aluno a
        , cadastro.pessoa p
        , cadastro.fisica f
        , pmieducar.curso c
        , pmieducar.serie s
      WHERE
        m.cod_matricula NOT IN
        (
          SELECT
            ref_cod_matricula
          FROM
            pmieducar.matricula_turma mt
            , pmieducar.turma t
          WHERE
            t.cod_turma = mt.ref_cod_turma
            AND mt.ativo = '1'
            AND m.ref_cod_curso = t.ref_cod_curso
            AND m.ref_ref_cod_serie = t.ref_ref_cod_serie
        )
        AND a.ref_idpes = p.idpes
        AND p.idpes = f.idpes
        AND a.cod_aluno = m.ref_cod_aluno
        AND m.ref_ref_cod_serie = s.cod_serie
        AND s.ref_cod_curso = c.cod_curso
        AND m.ativo = '1'
        AND m.ano = '{$int_ano}'
        AND m.aprovado IN (1,2,3)
        AND m.ultima_matricula = '1'";

      if ($ref_cod_curso) {
        $sql .= " AND m.ref_cod_curso = '{$ref_cod_curso}'";
      }

      if ($ref_cod_serie) {
        $sql .= " AND m.ref_ref_cod_serie = '{$ref_cod_serie}'";
      }

      if ($ref_cod_escola) {
        $sql .= " AND m.ref_ref_cod_escola = '{$ref_cod_escola}'";
      }

      if (is_numeric($semestre)) {
        $sql .= " AND m.semestre = {$semestre} ";
      }

      $db->Consulta($sql . $this->getOrderby());

      $resultado = array();

      while ($db->ProximoRegistro()) {
        $tupla = $db->Tupla();
        if ($verificar_multiseriado) {
          if (is_numeric($tupla["ref_ref_cod_serie"]) && is_numeric($tupla["ref_ref_cod_escola"]) && is_numeric($tupla["cod_matricula"])) {
            $sql = "SELECT
                  1
                FROM
                  pmieducar.matricula_turma mt,
                  pmieducar.turma t
                WHERE
                  mt.ativo = 1
                  AND t.ativo = 1
                  AND t.ref_ref_cod_serie_mult = {$tupla["ref_ref_cod_serie"]}
                  AND t.ref_ref_cod_escola = {$tupla["ref_ref_cod_escola"]}
                  AND t.cod_turma = mt.ref_cod_turma
                  AND mt.ref_cod_matricula = {$tupla["cod_matricula"]}";
            $db3 = new clsBanco();
            $aluno_esta_enturmado = $db3->CampoUnico($sql);
          }

          if (!is_numeric($aluno_esta_enturmado)) {
            $resultado[] = $tupla;
          }
        }
        else {
          $resultado[] = $tupla;
        }
      }

      return $resultado;
    }

    return FALSE;
  }

  function reclassificacao()
  {
    if (is_numeric($this->ref_cod_matricula)) {
      $db = new clsBanco();
      $consulta = "UPDATE {$this->_tabela} SET ativo = 0 WHERE ref_cod_matricula = '{$this->ref_cod_matricula}'";
      $db->Consulta($consulta);

      return TRUE;
    }

    return FALSE;
  }
  function getSequencialFechamento($matriculaId, $turmaId, $dataEnturmacao){
    $db = new clsBanco();
    $possui_fechamento = $db->CampoUnico("SELECT data_fechamento FROM pmieducar.turma WHERE cod_turma = {$turmaId}");
    if (is_string($possui_fechamento)){
      if (strtotime($possui_fechamento) < strtotime($dataEnturmacao))
        return $db->CampoUnico("SELECT MAX(sequencial_fechamento)+1 FROM {$this->_tabela} where ref_cod_turma = {$turmaId}");
      else
        return 0;
    }else
      return 0;
  }

  function limpaComponentesCurriculares(){
    $db = new clsBanco();
    $ano = $db->CampoUnico("SELECT ano FROM pmieducar.matricula WHERE cod_matricula = {$this->ref_cod_matricula}");
    echo "$ano --";
    CleanComponentesCurriculares::destroyOldResources($ano, $this->ref_cod_matricula);
  }

}