<?php 
function construir_cabecera( &$a_Data )
{
    $o_GSS_CompaniaBN  = $a_Data['o_GSS_CompaniaBN'];
    $o_VTA_OrdenBN = $a_Data['o_VTA_OrdenBN'];
    ?>
        <table style="width: 100%;" border="0" cellpadding="0" cellspacing="0"  >
            <tr>
                <td style="width: 30%">

                    <div style="">
                        <table style="width: 100%;" border="0" cellpadding="0" cellspacing="0"  >
                            <tr>
                                <td style=""><img src="web/images/logo/<?=$o_GSS_CompaniaBN->getRuc()?>.jpg" style="width: 260px; height: 70px;" ></td>
                            </tr>
                        </table>
                    </div>
                </td>
                <td style="width: 38%">
                    <table style="width: 100%;" border="0" cellpadding="0" cellspacing="0" >
                        <tr>
                            <td style="font-size: 10px;" align="center">
                                <label><b><?=$o_GSS_CompaniaBN->getDireccionComun()?></b></label>
                            </td>
                        </tr>
                        <tr>
                            <td style="font-size: 10px;" align="center">
                                <label><?=$o_GSS_CompaniaBN->getDireccionAdicional()?></label>
                            </td>
                        </tr>
                    </table>
                </td>
                <td style="width: 30%;">

                    <div style=" vertical-align: top; background-color: #EDEDED; padding: 5px; 
                        border-top-right-radius: 5px; border-top-left-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px;
                        border: 1px solid #B9B5B5;
                        ">
                        <div style="text-align: center;"><label style="font-size: 18px; font-weight: bold;">RUC <?=$o_GSS_CompaniaBN->getRuc()?></label></div>
                        <div style="text-align: center;"><label style="font-size: 18px; font-weight: bold;">COTIZACION</label></div>
                        <div style="text-align: center;"> <label style="font-size: 18px; font-weight: bold;"><?=$o_VTA_OrdenBN->getOrdenNum()?></label> </div>
                    </div>

                </td>
            </tr>
        </table>

        <div style="border-top-right-radius: 5px; border-top-left-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; margin-top: 10px;
                border: 1px solid #B9B5B5; padding: 5px; ">
            <table style="width: 100%;" border="0" cellpadding="1" cellspacing="0">
                <tr>
                    <td style="width: 8%; font-weight: bold;" align="left">CLIENTE </td> 
                    <td style="width: 28%;"><?=$o_VTA_OrdenBN->getClienteNombre() ?></td>
                    <td style="width: 10%; font-weight: bold;" align="left">EMISION</td>
                    <td style="width: 15%;">&nbsp;<?=$o_VTA_OrdenBN->getOrdenFecha()?></td>
                    <td style="width: 10%; font-weight: bold;" align="left">VENCIMIENTO</td>
                    <td style="width: 10%;">&nbsp;<?=$o_VTA_OrdenBN->getOrdenVenc()?></td>
                </tr>
                <tr>
                    <td style="font-weight: bold;" align="left"><?= $o_VTA_OrdenBN->getClienteDocCod() ?></td>
                    <td>&nbsp;<?=$o_VTA_OrdenBN->getClienteDocNum() ?></td>
                    <td style="font-weight: bold;" align="left">PLAZO ENTREGA</td>
                    <td>&nbsp;<?=$o_VTA_OrdenBN->getPlazoEntregaOrdenDesc()?></td>
                    <td style="font-weight: bold;" align="left">VALIDEZ OFERTA</td>
                    <td>&nbsp;<?=$o_VTA_OrdenBN->getValidezOrdenDesc()?></td>
                </tr>
                <tr>
                    <td style="font-weight: bold;" align="left">DIRECCION</td>
                    <td colspan="5">&nbsp;<?=$o_VTA_OrdenBN->getFactDirec1()?> - <?=$o_VTA_OrdenBN->getFactDirec2()?></td>
                </tr>
                <tr>
                    <td style="font-weight: bold;" align="left">VENDEDOR</td>
                    <td>&nbsp;<?=strtoupper( $o_VTA_OrdenBN->getVendedorOrden() ) ?></td>
                    <td style="font-weight: bold;" align="left">CORREO VENDEDOR</td>
                    <td>&nbsp;<?= $o_VTA_OrdenBN->getObjectVendedor()->getEmail() ?></td>
                    <td style="font-weight: bold;" align="left">CELULAR VENDEDOR</td>
                    <td>&nbsp;<?= $o_VTA_OrdenBN->getObjectVendedor()->getCelular() ?></td>
                    
                </tr>
                <tr>
                    <td style="font-weight: bold;" align="left">MONEDA</td>
                    <td>&nbsp;<?=$o_VTA_OrdenBN->getMonedaCod()=="LO"?"SOLES":"DOLARES"?></td>
                    <?php if( $o_VTA_OrdenBN->getMonedaCod()=="LO" ){ ?>
                    <td style="font-weight: bold;" align="left">&nbsp;</td>
                    <td>&nbsp;</td>
                    <?php }else{ ?>
                    <td style="font-weight: bold;" align="left">TIPO CAMBIO</td>
                    <td><?= $o_VTA_OrdenBN->getMonedaCambioValor() ?></td>
                    <?php } ?>
                    <td style="font-weight: bold;" align="left">CONDICIÓN PAGO</td>
                    <td>&nbsp;<?= $o_VTA_OrdenBN->getFormaPagoDesc() ?></td>
                </tr>
                <tr>
                    <td style="font-weight: bold;" align="left">COMENTARIO</td>
                    <td colspan="3"> <?=$o_VTA_OrdenBN->getOrdenObs()?> </td>
                    <td style="font-weight: bold;" align="left">ORDEN DE COMPRA</td>
                    <td>&nbsp;<?= $o_VTA_OrdenBN->getOrdenCompraRef() ?></td>
                </tr>
            </table>
        </div>
    <?php
}

function construir_pie( &$a_Data )
{
    $a_CuentasBancarias = $a_Data['a_CuentasBancarias'];
    $o_GSS_CompaniaBN   = $a_Data['o_GSS_CompaniaBN'];
    ?>
    <table style="width: 100%; font-size: 10px; text-align:justify; " border="0" cellpadding="0" cellspacing="0"  >
        <tr>
            <td style="width: 82%; vertical-align: top;">

                <div style="border-top-right-radius: 5px; border-top-left-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; margin-top: 10px;
                    border: 1px solid #B9B5B5; padding: 5px; margin-right: 2px;">

                    <label> 
                        <b>Aviso Importante:</b> Para pagos en efectivo y cheques, por su seguridad realice los pagos mediante un cheque girado a nombre de <b><?=$o_GSS_CompaniaBN->getDescripcionCorta()?></b> 
                        o abone directamente a una de las siguientes cuentas recaudadoras:
                    </label>

                    <div style="padding-top: 10px;">
                        <table style="width: 100%;" border="0" cellpadding="2" cellspacing="0" >
                            <tr>
                                <th align="center" width="25%"><u>Banco</u></th>
                                <th align="center" width="25%"><u>Moneda</u></th>
                                <th align="center" width="25%"><u>Nro Cuenta</u></th>
                                <th align="center" width="25%"><u>CC Interbancario</u></th>
                            </tr>
                            <?php foreach( $a_CuentasBancarias as $o_GSS_ParametroMastBN ){ ?>
                            <tr>
                                <td align="center"><?=$o_GSS_ParametroMastBN->getIdParametroClave()?></td>
                                <td align="center"><?=$o_GSS_ParametroMastBN->getTexto()?></td>
                                <td align="center"><?=$o_GSS_ParametroMastBN->getDescripcionParametro()?></td>
                                <td align="center"><?=$o_GSS_ParametroMastBN->getExplicacion()?></td>
                            </tr>
                            <?php } ?>
                        </table>
                    </div>
            
                </div>

            </td>
        </tr>
    </table>

    <table style="width: 100%; font-size: 10px; text-align:justify; " border="0" cellpadding="0" cellspacing="0"  >
        <tr>
            <td style="width: 82%; vertical-align: top;">

                <div style="border-top-right-radius: 5px; border-top-left-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; margin-top: 10px;
                    border: 1px solid #B9B5B5; padding: 5px; margin-right: 2px; text-align: center;">

                    <!-- <label> Sistema creado por Corporación Hardsoft - Visite <span style="color: blue; text-decoration: underline;">www.corporacionhardsoft.com </span> </label> -->
            
                </div>

            </td>
        </tr>
    </table>
    <?php
}

function construir_cuerpo( &$a_Data, $ini, $fin, $s_vHtml )
{
    $o_VTA_OrdenBN      = $a_Data['o_VTA_OrdenBN'];
    $a_VTA_OrdenDetalle = $a_Data['a_VTA_OrdenDetalle'];
    ?>
        <?php if( $ini != 0 ){ ?> 
            <?php if( !$s_vHtml ){ ?>
                <div class="new-page"> 
            <?php } ?>
        <?php } ?>

        <?=construir_cabecera( $a_Data )?>

        <div style="border-top-right-radius: 5px; border-top-left-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; margin-top: 10px;
                    border: 1px solid #B9B5B5; padding: 5px; ">

            <table style="width: 100%;" border="0" cellpadding="5" cellspacing="0" >
                <tr>
                    <!-- 1% -->
                    <td style="width: 5%; background-color: #EEEEEE; border-bottom: 1px solid ; border-left: 1px solid; border-top: 1px solid; border-color: #B9B5B5;
                            border-top-left-radius: 5px; border-bottom-left-radius: 5px;
                            " align="center">
                        <!--<b>Nº</b>
                    </td>
                    <td style="width: 5%; background-color: #EEEEEE; border-bottom: 1px solid; border-top: 1px solid; border-color: #B9B5B5;" align="center">-->
                        <b>ITEM</b>
                    </td>
                    <td style="width: 10%; background-color: #EEEEEE; border-bottom: 1px solid; border-top: 1px solid; border-color: #B9B5B5;" align="center"><b>CNT</b></td>
                    <!--<td style="width: 6%; background-color: #EEEEEE; border-bottom: 1px solid; border-top: 1px solid; border-color: #B9B5B5;" align="center"><b>U.M</b></td>-->
                    <td style="width: 10%; background-color: #EEEEEE; border-bottom: 1px solid; border-top: 1px solid; border-color: #B9B5B5;" align="center"><b>MARCA</b></td>
                    <td style="width: 50%; background-color: #EEEEEE; border-bottom: 1px solid; border-top: 1px solid; border-color: #B9B5B5;"><b>DESCRIPCION</b></td>
                    <!--<td style="width: 6%; background-color: #EEEEEE; border-bottom: 1px solid; border-top: 1px solid; border-color: #B9B5B5;" align="center"><b>FE.VEN</b></td>-->
                    <td style="width: 10%; background-color: #EEEEEE; border-bottom: 1px solid; border-top: 1px solid; border-color: #B9B5B5;" align="center"><b>GARANTIA</b></td>
                    <td style="width: 10%; background-color: #EEEEEE; border-bottom: 1px solid; border-top: 1px solid; border-color: #B9B5B5;" align="right"><b>P/U</b></td>
                    <td style="width: 10%; background-color: #EEEEEE; border-bottom: 1px solid; border-top: 1px solid; border-right: 1px solid; border-color: #B9B5B5; 
                               border-bottom-right-radius: 5px; border-top-right-radius: 5px;" align="center">
                        <b>IMPORTE</b>
                    </td>
                </tr>

<?php if ( $a_VTA_OrdenDetalle ){ ?>
    <?php for ( $i=$ini; $i < $fin; $i++) { ?>
        <?php if( isset( $a_VTA_OrdenDetalle[$i] ) ){ ?>
        <?php
        $s_Background = $i%2==0 ? "" : "background-color: #F0F0F0;";
        $b_Control = $a_VTA_OrdenDetalle[$i]->getItemTipo()==VTA_OrdenDetalleBN::TIPO_PRODUCTO ? true : false;
        ?>
            <tr style="<?=$s_Background?>">
                <!--<td style="" align="center"><? //=$i+1 ?> </td>-->
                <td style="" align="center"><?=$a_VTA_OrdenDetalle[$i]->getItemCod() ?></td>
                <td style="" align="center"><?=$b_Control?$a_VTA_OrdenDetalle[$i]->getOrdenDetalleCantidad():"" ?></td>
                <td style="" align="center"><?=$a_VTA_OrdenDetalle[$i]->getMarcaDesc()?></td>
                <td style=""><?= $a_VTA_OrdenDetalle[$i]->getOrdenDetalleDescripcion() ?></td>
                <td style="" align="center"><?=$a_VTA_OrdenDetalle[$i]->getObjectItem()->getObjectTipoGarantia()->getNombre()?></td>
                <td style="" align="right"><?=$b_Control?number_format( $a_VTA_OrdenDetalle[$i]->getOrdenDetallePrecioUnidad(), 2 ):"" ?></td>
                <td style="" align="right"><?=$b_Control?number_format( round( $a_VTA_OrdenDetalle[$i]->getOrdenDetalleSubTotalVenta(), 2 ), 2 ):"" ?></td>
            </tr>
        <?php }else{ ?>
            <tr>
                <!--<td style="" align="center">&nbsp;</td>-->
                <td style="" align="center">&nbsp;</td>
                <td style="" align="center">&nbsp;</td>
                <td style="" align="center">&nbsp;</td>
                <td style="" align="center">&nbsp;</td>
                <td style="" align="center">&nbsp;</td>
                <td style="" align="right">&nbsp;</td>
            </tr>
        <?php } ?>
    <?php } ?>
<?php } ?>
                <tr>
                    <td style="" colspan="4">&nbsp;</td>
                    <td style="" align="right" colspan="2">
                        <b>
                            SUB TOTAL <?=GSS_MonedaMastFN::getSiglaxId( $o_VTA_OrdenBN->getMonedaCod() )?>
                        </b>&nbsp;&nbsp;
                    </td>
                    <td style="" align="right"><b><?=number_format( ( $o_VTA_OrdenBN->getOrdenVenta() ), 2 )?></b></td>
                </tr>
                <tr>
                    <td style="" colspan="4">&nbsp;</td>
                    <td style="" align="right" colspan="2">
                        <b>
                            IGV <?=GSS_ParametroMastFN::getTextoIgvActual()?> <?=GSS_MonedaMastFN::getSiglaxId( $o_VTA_OrdenBN->getMonedaCod() )?>
                        </b>&nbsp;&nbsp;
                    </td>
                    <td style="" align="right"><b><?=number_format( ( $o_VTA_OrdenBN->getMontoImpuestos() ), 2 )?></b></td>
                </tr>
                <tr>
                    <td style="" colspan="4">&nbsp;</td>
                    <td style="" align="right" colspan="2"><b>TOTAL <?=GSS_MonedaMastFN::getSiglaxId( $o_VTA_OrdenBN->getMonedaCod() )?></b>&nbsp;&nbsp;</td>
                    <td style="" align="right"><b><?=number_format( ( $o_VTA_OrdenBN->getOrdenNeto() ), 2 )?></b></td>
                </tr>
            </table>
        </div>

        <?=construir_pie( $a_Data )?>

    <?php
}
?>

<!-- https://ourcodeworld.com/articles/read/687/how-to-configure-a-header-and-footer-in-dompdf -->
<html>
    <head>
        <style type="text/css">
            @page 
            {
                size: A4 portrait;
                margin: 0cm 0cm;
            }

            header 
            {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 8px;
            }

            footer 
            {
                bottom: 0cm; 
                left: 0cm; 
                right: 0cm;
            }

            body 
            {
                margin : 0.5cm;
                /*margin-top: 1cm;*/
                font-family: Arial, Helvetica, sans-serif;
                font-size: 8px;
                /*margin-top: 0.5cm;
                margin-left: 0.5cm;
                margin-right: 0.5cm;
                margin-bottom: 0.5cm;*/
            }

            table
            {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 8px;
            }

            .new-page{ page-break-before: always; }

        </style>
    </head>
    <!--<body style="page-break-before: always;">-->
    <body>

        <?php
        $a_VTA_OrdenDetalle = $a_Data['a_VTA_OrdenDetalle'];

        $i_RowTot = 25;
        $i_Loop   = ceil( count( $a_VTA_OrdenDetalle ) / $i_RowTot );

        $i_RowIni  = 0;
        $i_RowFin  = $i_RowTot;

        $j = 1;
        while ( $j <= $i_Loop ) 
        {
            if( $s_vHtml )
            {
                $i_RowFin = count( $a_VTA_OrdenDetalle );
            }
            construir_cuerpo( $a_Data, $i_RowIni, $i_RowFin, $s_vHtml );

            $i_RowIni = $i_RowIni + $i_RowTot;
            $i_RowFin = $i_RowFin + $i_RowTot;
            
            $j++;

            if( $s_vHtml )
            {
                break;
            }
        }
        ?>

    </body>
</html>