<?php
$pdf->setMargins(1.27, 0.50, 1.27);
$pdf->AddPage();
$revenda = ($model->COT_SITUACAO == Contratos::PEDIDO_IMPLANTACAO) ? $model->Revenda : Revendas::model()->findByPk(1);

$goBreak = false;
/*HEADER*/
$HTML =  '<table width="100%">';
    $HTML .= '<tr>';
        $HTML .= '<td width="100%">';

            $HTML .= '<table width="100%" border="1" bordercolor="#000" >';
                $HTML .= '<tr>';
                    $HTML .= '<td>';
                        $HTML .= '<table width="100%" cellpadding="1">';
                            $HTML .= '<tr>';

                                $HTML .= '<td width="33%" align="center">';
                                    $HTML .= '<img src="'.$revenda->getLogoUrl().'" width="150" height="60" />';
                                $HTML .= '</td>';
                                $HTML .= '<td width="33%" align="center">';
                                    $HTML .= '<font size="9">';
                                    $HTML .= '<strong>';
                                        $HTML .= $revenda->REV_FANTASIA.'<br />'.$revenda->REV_ENDERECO.'<br />';
                                        $HTML .= ' Bairro '.$revenda->REV_BAIRRO.' CEP '.$revenda->REV_CEP.'<br />';
                                        $HTML .= $revenda->REV_FONE1.'<br />';
                                        $HTML .= $revenda->REV_EMAIL;
                                    $HTML .= '</strong>';
                                    $HTML .= '</font>';
                                $HTML .= '</td>';
                                $HTML .= '<td width="33%" align="center">';
                                    $HTML .= '<font size="10">';
                                        $HTML .= '<strong>PEDIDO<br /><br />Nº '.$model->COT_IDCONTRATO.'<br /><br />';
                                            $HTML .= 'Data de Emissão';
                                        $HTML .= '</strong> ';
                                        $HTML .= $model->COT_DTCADASTRO;
                                    $HTML .= '</font>';
                                $HTML .= '</td>';

                            $HTML .= '</tr>';
                        $HTML .= '</table>';
                    $HTML .= '</td>';
                $HTML .= '</tr>';
            $HTML .= '</table>';
            $HTML .= '<br /><br />';
/*FIM HEADER*/

/*CORPO PRINCIPAL*/
        $HTML .='<table width="100%" border="1" bordercolor="#000" cellpadding="0">';
            $HTML .= '<tr>';
                $HTML .= '<td>';
                    $HTML .= '<table width="100%" cellpadding="3">';
                        $HTML .='<tr>';
                            $HTML .='<td>';
                                $HTML .= '<font size="9"><strong>Razão Social:</strong> '.$model->COT_CLI_NOME.'</font>';
                            $HTML .= '</td>';
                        $HTML .= '</tr>';
                        $HTML .= '<tr>';
                            $HTML .= '<td width="60%">';
                                $HTML .= '<font size="9"><strong>Contato:</strong> ';
                                $HTML .= ((!empty($model->COT_REPRESENTANTES)) ? $model->COT_REPRESENTANTES : $model->COT_CLI_CONTATO);
                                $HTML .= '</font>';
                            $HTML .= '</td>';
                            $HTML .= '<td width="40%">';
                                $HTML .= '<font size="9"><strong>E-mail:</strong> '.$model->COT_CLI_EMAIL.'</font>';
                            $HTML .= '</td>';
                        $HTML .= '</tr>';
                        $HTML .= '<tr>';
                            $HTML .= '<td width="60%">';
                                $HTML .= '<font size="9"><strong>CNPJ / CPF:</strong> '.$model->getMaskedCpfCnpj().'</font>';
                            $HTML .= '</td>';
                            $HTML .= '<td width="40%">';
                                $HTML .= '<font size="9"><strong>Inscrição Estadual:</strong> '.$model->COT_CLI_INSCR_EST.'</font>';
                            $HTML .= '</td>';
                        $HTML .= '</tr>';
                        $HTML .= '<tr>';
                            $HTML .= '<td width="60%">';
                                $HTML .= '<font size="9"><strong>Endereço:</strong> '.$model->COT_CLI_ENDERECO.'</font>';
                                $HTML .= '</td>';
                            $HTML .= '<td width="40%">';
                                $HTML .= '<font size="9"><strong>Número:</strong> '.$model->COT_CLI_NUMERO.'</font>';
                            $HTML .= '</td>';
                        $HTML .= '</tr>';
                        $HTML .='<tr>';
                            $HTML .= '<td width="60%">';
                                $HTML .= '<font size="9"><strong>Complemento:</strong> '.$model->COT_CLI_COMPLEMENTO.'</font>';
                            $HTML .= '</td>';
                            $HTML .= '<td width="40%">';
                                $HTML .= '<font size="9"><strong>Bairro:</strong> '.$model->COT_CLI_BAIRRO.'</font>';
                            $HTML .= '</td>';
                        $HTML .= '</tr>';
                        $HTML .= '<tr>';
                            $HTML .= '<td width="60%">';
                                $HTML .= '<font size="9"><strong>Cidade:</strong> '.$model->Cidade->CID_DESCRICAO.'</font>';
                            $HTML .= '</td>';
                            $HTML .= '<td width="40%">';
                                $HTML .= '<font size="9"><strong>UF:</strong> '.$model->Cidade->CID_UF.'</font>';
                            $HTML .= '</td>';
                        $HTML .= '</tr>';
                        $HTML .='<tr>';
                            $HTML .= '<td width="60%">';
                                $HTML .= '<font size="9"><strong>CEP:</strong> '.$model->COT_CLI_CEP.'</font>';
                            $HTML .= '</td>';
                            $HTML .= '<td width="40%">';
                                $HTML .= '<font size="9"><strong>Telefone:</strong> '.$model->COT_CLI_FONE1.'</font>';
                            $HTML .= '</td>';
                        $HTML .= '</tr>';
                    $HTML .= '</table>';
                $HTML .= '</td>';
            $HTML .= '</tr>';
        $HTML .= '</table><br /><br />';
/*FIM CORPO PRINCIPAL*/

if ($model->COT_SITUACAO == Contratos::PEDIDO_IMPLANTACAO) {
    /* CORPO CONVERSAO DE DADOS */
        if(isset($conversaoDados->CCD_IMPORTAR)) {
            $HTML .= '<table width="100%" border="1" bordercolor="#000" >';
                 $HTML .= '<tr>';
                        $HTML .= '<td>';
                            $HTML .= '<table width="100%" cellpadding="3">';
                                    $importar = $conversaoDados->CCD_IMPORTAR;

                                    if($importar == 0){
                                        $HTML .= '<tr>';
                                            $HTML .= '<td width="60%">';
                                                $HTML .= '<font size="9"><strong>Importar Dados:</strong> Não </font>';
                                            $HTML .= '</td>';
                                        $HTML .= '</tr>';
                                    } else {

                                        $clientes   = $conversaoDados->CCD_CLIENTES == 1 ? 'Sim' : 'Não';
                                        $fornecedor = $conversaoDados->CCD_FORNECEDORES == 1 ? 'Sim' : 'Não';
                                        $obsConversao = $conversaoDados->CCD_OBSERVACAO;

                                        $HTML .= '<tr>';
                                            $HTML .= '<td width="33%">';
                                                $HTML .= '<font size="9"><strong>Importar Dados:</strong> Sim </font>';
                                            $HTML .= '</td>';

                                            $HTML .= '<td width="33%">';
                                                $HTML .= '<font size="9"><strong>Importar Clientes:</strong> '.$clientes.'</font>';
                                            $HTML .= '</td>';

                                            $HTML .= '<td width="33%">';
                                                $HTML .= '<font size="9"><strong>Importar Fornecedores:</strong> '.$fornecedor.' </font>';
                                            $HTML .= '</td>';
                                        $HTML .= '</tr>';

                                        if(!empty($produtos)){
                                            $HTML .= '<tr>';
                                                $HTML .= '<td width="100%">';
                                                    $HTML .= '<font size="9"><strong>Produtos:</strong> '.$produtos.'</font>';

                                                $HTML .= '</td>';
                                            $HTML .= '</tr>';
                                        }
                                        if (!empty($obsConversao)) {
                                            $HTML .= '<tr>';
                                                $HTML .= '<td width="100%">';
                                                    $HTML .= '<font size="9"><strong>Observação:</strong> '.$obsConversao.'</font>';
                                                $HTML .= '</td>';
                                            $HTML .= '</tr>';
                                            $goBreak = true;
                                        }
                                    }

                            $HTML .= '</table>';
                        $HTML .= '</td>';
                 $HTML .= '</tr>';
            $HTML .= '</table>';
            $HTML .= '<br /><br />';
        }
    /* FIM CORPO CONVERSAO DE DADOS*/

    /* CORPO INFORMACOES PEDIDO */
        if(!empty($model->COT_IMPLANTACAO_SITUACAO)) {
            $HTML .= '<table width="100%" border="1" bordercolor="#000" >';
                $HTML .= '<tr>';
                    $HTML .= '<td>';
                        $HTML .= '<table width="100%" cellpadding="3">';
                            $urgente = $model->COT_IMPLANTACAO_URGENTE == 1 ? 'Sim' : 'Não';
                            $situacao = $model->COT_IMPLANTACAO_SITUACAO;
                            $local = $model->COT_IMPLANTACAO_LOCAL;
                            $obs = $model->COT_IMPLANTACAO_OBSERVACAO;

                            $HTML .= '<tr>';
                                $HTML .= '<td width="33%">';
                                    $HTML .= '<font size="9"><strong>Urgente:</strong> '.$urgente.' </font>';
                                $HTML .= '</td>';
                                // $HTML .= '<td width="33%">';
                                //     $HTML .= '<font size="9"><strong>Sem Chave:</strong> '.$semChave.'</font>';
                                // $HTML .= '</td>';
                                $HTML .= '<td width="33%">';
                                    $HTML .= '<font size="9"><strong>Situação:</strong> '.$situacao.' </font>';
                                $HTML .= '</td>';
                            $HTML .= '</tr>';

                            if (!empty($local)) {
                                $HTML .= '<tr>';
                                    $HTML .= '<td width="100%">';
                                        $HTML .= '<font size="9"><strong>Local de Implantação:</strong> '.$local.'</font>';
                                    $HTML .= '</td>';
                                $HTML .= '</tr>';
                            }

                            if (!empty($obs)) {
                                $HTML .= '<tr>';
                                    $HTML .= '<td width="100%">';
                                        $HTML .= '<font size="9"><strong>Observação:</strong> '.$obs.' </font>';
                                    $HTML .= '</td>';
                                $HTML .= '</tr>';
                                $goBreak = true;
                            }

                        $HTML .= '</table>';
                    $HTML .= '</td>';
                $HTML .= '</tr>';
            $HTML .= '</table>';
            $HTML .= '<br /><br />';
        }
    /* FIM CORPO INFORMACOES PEDIDO */
}
/*PRODUTOS E SERVICOS*/
        $HTML .= '<table width="100%" border="1" bordercolor="#000" cellpadding="3">';
            $HTML .= '<tr>';
                $HTML .= '<td width="80%" align="center">';
                    $HTML .= '<font size="9"><strong>PRODUTOS / SERVIÇOS</strong></font>';
                $HTML .= '</td>';
                $HTML .= '<td width="20%" align="center">';
                    $HTML .= '<font size="9"><strong>VALOR R$</strong></font>';
                $HTML .= '</td>';
            $HTML .= '</tr>';
            $HTML .='<tr>';
                $HTML .='<td>'.($model->COT_SITUACAO == Contratos::PEDIDO_IMPLANTACAO ? 'Serviços de Implantação' : 'Serviços de Reativação').'</td>';
                $HTML .= '<td>';
                    $HTML .= '<div align="center">'.Util::verMoeda($model->COT_VLRIMPLANTACAO).'</div>';
                $HTML .= '</td>';
            $HTML .='</tr>';
            $HTML .='<tr>';
                $HTML .='<td width="80%" align="right">';
                    $HTML .= '<strong>VALOR TOTAL DO PEDIDO</strong>';
                $HTML .= '</td>';
                $HTML .='<td width="20%" align="center">';
                    $HTML .= '<strong>'.Util::verMoeda($model->COT_VLRIMPLANTACAO).'</strong>';
                $HTML .= '</td>';
            $HTML .='</tr>';
        $HTML .= '</table>';
/* FIM PRODUTOS E SERVICOS*/

/*OBSERVACOES*/
        $breakVenc = "";
        $height = 0;

        if (strlen($model->COT_OBS) > 0) {
            $height = 150;

            if($goBreak){
                $breakVenc = "<BR/><BR/><BR/><BR/><BR/><BR/>";
            }
        }

        $HTML .= '<table width="100%" border="1" bordercolor="#000" cellpadding="3">';
            $HTML .= '<tr>';
                $HTML .= '<td height="'.$height.'">'.$model->COT_OBS.'</td>';
            $HTML .= '</tr>';
            $HTML .= '<tr>';
                $HTML .= '<td height="50">';
                    $HTML .= '<font size="10">';
                        $HTML .= 'Para os serviços prestados será disponibilizado a quantidade de <strong>';
                        $HTML .= $model->COT_HORASIMPLANTACAO.' horas</strong>';
                        $HTML .= ', sendo que as referidas horas deverão ser utilizadas no prazo de cento e vinte (120) dias corridos a contar';
                        $HTML .= ' da data de assinatura deste pedido. A não utilização das horas disponibilizadas neste documento não gera';
                        $HTML .= ' direito a nenhum tipo de ressarcimento (técnico ou econômico) ao contratante.';
                    $HTML .= '</font>';
                $HTML .= '</td>';
            $HTML .= '</tr>';
        $HTML .= '</table>';
/*FIM OBSERVACOES*/
        $HTML .= $breakVenc;
/* VENCIMENTOS */
        $HTML .= '<table width="100%" cellpadding="3">';
            $HTML .= '<tr>';
                $HTML .= '<td align="center">';
                    $HTML .= '<strong>VENCIMENTOS</strong>';
                $HTML .= '</td>';
            $HTML .= '</tr>';
        $HTML .= '</table>';
        $HTML .= '<table width="100%" border="1" bordercolor="#000" cellpadding="3">';
            $HTML .= '<tr>';
                $HTML .= '<td width="25%">';
                    $HTML .= '<div align="center">';
                        $HTML .= '<strong>Data</strong>';
                    $HTML .= '</div>';
                $HTML .= '</td>';
                $HTML .= '<td width="25%">';
                    $HTML .= '<div align="center"><strong>Valor da Parcela</strong></div>';
                $HTML .= '</td>';
                $HTML .= '<td width="25%">';
                    $HTML .= '<div align="center"><strong>Data</strong></div>';
                $HTML .= '</td>';
                $HTML .= '<td width="25%">';
                    $HTML .= '<div align="center"><strong>Valor da Parcela</strong></div>';
                $HTML .= '</td>';
            $HTML .= '</tr>';
            $HTML .= '<tr>';

            $nro_vencimentos = count($model->Vencimentos);
            foreach($model->Vencimentos as $key => $vencimento) {
                    $HTML .= '<td>';
                        $HTML .= '<div align="center">'.$vencimento->COV_DATA.'</div>';
                    $HTML .= '</td>';
                    $HTML .= '<td>';
                        $HTML .= '<div align="center">'.Util::verMoeda($vencimento->COV_VALOR).'</div>';
                    $HTML .= '</td>';

                if($key%2 != 0 && $nro_vencimentos > ($key +1)){
                    $HTML .= '</tr>';
                    $HTML .= '<tr>';
                }

            }

            if($nro_vencimentos%2 != 0){
                $HTML .= '<td>';
                    $HTML .= '<div align="center"></div>';
                $HTML .= '</td>';
                $HTML .= '<td>';
                    $HTML .= '<div align="center"></div>';
                $HTML .= '</td>';
            }

            $HTML .= '</tr>';
        $HTML .='</table><br /><br />';
/*FIM VENCIMENTOS*/

/* ASSINATURAS */
        $HTML .= '<table width="100%">';
            $HTML .= '<tr>';
                $HTML .= '<td>';
                    $HTML .= '<p align="center"><font size="7"><strong>*PEDIDO SUJEITO A APROVAÇÃO DA '.Util::converteMaiusculo($revenda->REV_FANTASIA).' MEDIANTE ASSINATURA</strong></font></p>';
                $HTML .= '</td>';
            $HTML .= '</tr>';
        $HTML .= '</table>';

        $HTML .= '<table width="100%" border="0" bordercolor="#000" cellpadding="0">';
            $HTML .= '<tr>';
                $HTML .= '<td>';
                        $HTML .= '<table width="100%" cellpadding="0">';
                            $HTML .= '<tr>';
                                $HTML .='<td width="30%">';
                                    $HTML .='<p align="center"><font size="9"><br />_______________________<br />Assinatura do Cliente</font></p>';
                                $HTML .= '</td>';
                                $HTML .= '<td width="30%">';
                                    $HTML .='<p align="center"><font size="9"><br />_______________________<br />Departamento de Vendas</font></p>';
                                $HTML .= '</td>';
                                $HTML .= '<td width="40%">';
                                    $HTML .= '<p align="center"><font size="9"><br />_______________________<br />'.$revenda->REV_FANTASIA.'</font></p>';
                                $HTML .= '</td>';
                            $HTML .= '</tr>';
                        $HTML .= '</table>';
                $HTML .= '</td>';
            $HTML .= '</tr>';
        $HTML .= '</table>';
/* FIM ASSINATURAS */

        $HTML .= '</td>';
    $HTML .= '</tr>';
$HTML .= '</table>';

$pdf->writeHTML(utf8_encode($HTML), true, 0, true, true);
$pdf->Output($generatePath.'.pdf', "FI");