Skip to content

Instantly share code, notes, and snippets.

@backstageel
Created August 10, 2015 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save backstageel/bfe5027df276701af581 to your computer and use it in GitHub Desktop.
Save backstageel/bfe5027df276701af581 to your computer and use it in GitHub Desktop.
Funcao para preencher os campos do ficheiro de Financiamento
<?php
public function base_fund()
{
//Lemos a biblioteca PHPExcel
App::import('Helper', 'PhpExcel');
$this->PHPExcel = new PhpExcelHelper(new View());
//Lemos o ficheiro excel a preencher. Aquele que foi fornecido pelos financiadores
$xls = $this->PHPExcel->loadWorksheetFromS3('/Reports/Financas/base_fund.xlsx');
$worksheet = $xls->getActiveSheet();
$linhaActual = 3;
$naoEnconttados = [];
//Vamos ler linha por linha do ficheiro por preencheer, comecando pela linha 3
foreach ($worksheet->getRowIterator() as $row) {
//Se encontrar uma linha em branco eh porque terminamos o ficheiro todo
if ($worksheet->getCell('A' . $linhaActual)->getValue() == '') {
break;
}
//Pegamos o codigo da instuicao para apenas preencher as linhas da UEM(ou da UP nesse caso)
$codigoInstituicao = $worksheet->getCell('C' . $linhaActual)->getValue();
if ($codigoInstituicao != 1) {
$this->out('Saltando Linha -------' . $linhaActual);
$linhaActual++;
continue;
}
$nomeCurso = $worksheet->getCell('F' . $linhaActual)->getValue();
$nomeGrau = $worksheet->getCell('K' . $linhaActual)->getValue();
$nomeCompletoCurso = $nomeGrau . ' em ' . $nomeCurso;
//Pegamos o nome do curso e procuramos por aquele curso na nossa BD MySQL
$curso = $this->Aluno->Curso->findByName($nomeCompletoCurso);
if (empty($curso)) {
$fim = substr($nomeCompletoCurso, -3);
$nomeCompletoCurso = str_replace(' ', '%', trim($nomeCompletoCurso));
if ($fim === 'lab') {
$nomeCompletoCurso = $nomeCompletoCurso . '%';
}
$curso = $this->Aluno->Curso->find('first', [
'conditions' => [
'Curso.name
LIKE' => $nomeCompletoCurso
]
]);
if (empty($curso)) {
$naoEnconttados[] = $nomeCompletoCurso;
$linhaActual++;
continue;
}
}
$ano = $worksheet->getCell('E' . $linhaActual)->getValue();
$sexo = $worksheet->getCell('M' . $linhaActual)->getValue();
if ($sexo == 1) {
$genero = 'Masculino';
} else {
$genero = 'Feminino';
}
$S = $worksheet->getCell('S' . $linhaActual)->getFormattedValue();
$T = $worksheet->getCell('T' . $linhaActual)->getFormattedValue();
$AG = $worksheet->getCell('AG' . $linhaActual)->getValue();
$letras = ['V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE'];
foreach ($letras as $letra) {
$conditions = [
'Aluno.ano_ingresso' => $ano,
'Entidade.genero_id' => $sexo,
'Aluno.curso_id' => $curso['Curso']['id'],
'Aluno.estado_aluno_id' => [1, 11, 14]
];
$this->Aluno->contain(['Entidade']);
//Ja temos todos requisitos(Curso, Ano de Ingresso e Sexo) entao podemos fazer um Select count(*)
$total = $this->Aluno->find('count', [
'conditions' => $conditions
]);
$worksheet->setCellValue($letra . $linhaActual, $total);
$this->out($nomeCompletoCurso . '-----' . $ano . '------------' . $genero . '----------' . $total . '------------'
. $letra);
$ano = $ano - 1;
$this->hr();
}
unset($conditions['Aluno.ano_ingresso']);
$conditions['Aluno.ano_ingresso <=']=$ano;
$this->Aluno->contain(['Entidade']);
$total = $this->Aluno->find('count', [
'conditions' => $conditions
]);
unset($conditions['Aluno.ano_ingresso <=']);
$worksheet->setCellValue('AF' . $linhaActual, $total);
$this->out($nomeCompletoCurso . '-----RESTO------------' . $genero . '----------' . $total . '------------'
. $letra);
$worksheet->getStyle('AG'.$linhaActual)
->getNumberFormat()
->setFormatCode(
PHPExcel_Style_NumberFormat::FORMAT_NUMBER
);
$this->hr();
$worksheet->setCellValue('AG' . $linhaActual, $AG);
$worksheet->setCellValue('S' . $linhaActual, $S);
$worksheet->setCellValue('T' . $linhaActual, $T);
$linhaActual++;
}
//Por fim gravamos o excel ja preenchido num novo ficheiro.
$objWriter = PHPExcel_IOFactory::createWriter($xls, 'Excel2007');
$objWriter->save('base_fund2.xlsx');
$xls->disconnectWorksheets();
unset($xls);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment