🌹🏠🌹？》《》？《》？<?php
session_start();
include_once "../Lib/PDF.php";
include_once "../Lib/inicio.php";
include_once "../Lib/r_mails.php";
include_once "../Lib/funciones.php";
include_once "../Lib/r_general.php";
include_once "../Lib/PHPExcel-1.8/Classes/PHPExcel.php";

$pdf = new PDF();
$objPHPExcel = new PHPExcel();

$p_titulo    = "Consulta Remitos con Series Despachadas";
$p_tit       = "";
$pdf->titulo = $p_titulo;
$pdf->sub_titulo = $p_tit;
$p_alto   = 5;
$p_borde  = 0;
$pa_tit   = array();
$pa_tit[] = array('campo'=>' ','tamano' => 30, 'centrado' => 'D');
$pa_tit[] = array('campo'=>'Articulo','tamano' => 30, 'centrado' => 'D');
$pa_tit[] = array('campo'=>'Descripcion','tamano' =>  60, 'centrado' => 'D');
//$pa_tit[] = array('campo'=>'Periodo' ,'tamano'=> 40, 'centrado' => 'D');
//$pa_tit[] = array('campo'=>'Concepto','tamano' => 75, 'centrado' => 'D');
$pa_tit[] = array('campo'=>'Serie','tamano' => 30, 'centrado' => 'D');
$pa_tit[] = array('campo'=>'Mac.Address','tamano'=>30, 'centrado'=>'D');
$pdf->encabezado = $pa_tit;

$pdf->AliasNbPages();
$pdf->SetLeftMargin(5);
$pdf->AddPage();
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','', 7);

$objPHPExcel->getProperties()->setCreator("GLSA")
                ->setLastModifiedBy('')
                ->setTitle($p_titulo)
                ->setSubject($p_titulo)
                ->setDescription("")
                ->setKeywords("")
                ->setCategory("");

$objPHPExcel->getActiveSheet()->setTitle('Series Despachadas');
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', $p_titulo)
            ->setCellValue('A2', $p_tit);

$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(20);

$styleArray = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => '#70296F'),
        'size'  => 15,
        'name'  => 'Verdana'
    ));

$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A3', 'Documento')
            ->setCellValue('B3', 'Cliente')
            ->setCellValue('C3', 'Direccion')
            ->setCellValue('D3', 'Provincia')
            ->setCellValue('E3', 'Localidad')
            ->setCellValue('F3', 'HR')
            ->setCellValue('G3', 'Fecha')
            ->setCellValue('H3', 'Articulo')
            ->setCellValue('I3', 'Descripcion')
            ->setCellValue('J3', 'Serie')
            ->setCellValue('K3', 'Mac.Adress');

$p_tot_fc=0;
$p_tot_lq=0;

$p_hruta_d=0;
$p_remito_ant="";
$p_row=4;
$sql=
" select a.empresa empresa, a.tdoc tdoc, a.letra letra, a.sucur sucur, a.numero numero,d.fecha_doc fecha_doc, d.fecha_ing fecha_ing,
         a.hruta_d hruta_d, a.cliente cliente,a.direccion direccion ,a.pais pais ,a.provincia provincia ,a.localidad localidad ,
         b.articulo articulo ,c.serie serie
   from {$Base->base_wms}hoja_ruta  e, {$Base->base_wms}hoja_ruta_det a, {$Base->base_wms}pre_expedicion_items b, 
        {$Base->base_wms}pre_expedicion_items_series c, {$Base->base_wms}pdcabe d 
  where e.hruta_d = a.hruta_d
    and a.hruta_d  = b.hruta_d 
    and a.nro_item = b.nro_item
    and b.hruta_d  = c.hruta_d
    and b.nro_item = c.nro_item
    and b.nro_pre  = c.nro_pre
    and b.nro_interno = c.nro_interno
    and a.empresa = d.empresa
    and a.tdoc = d.tdoc
    and a.letra = d.letra
    and a.sucur = d.sucur
    and a.numero = d.numero
    and a.tdoc = 'RTO'
    and b.estado != 'A'
    and b.estado != 'A'";
$sql.=" and a.empresa = '{$_GET['em']}'";
if (isset($_GET['le']) && strlen($_GET['le']) > 0 && $_GET['le'] != 0){
  $sql.=" and a.letra = '{$_GET['le']}'";
}
if (isset($_GET['su']) && strlen($_GET['su']) > 0 && $_GET['su'] != 0){
  $sql.=" and a.sucur = '{$_GET['su']}'";
}
if (isset($_GET['nu']) && strlen($_GET['nu']) > 0 && $_GET['nu'] != 0){
  $sql.=" and a.numero = '{$_GET['nu']}'";
}
if (isset($_GET['hr']) && strlen($_GET['hr']) > 0 && $_GET['hr'] != 0 ){
  $sql.=" and a.hruta_d = '{$_GET['hr']}'";
}
if (isset($_GET['cl']) && strlen($_GET['cl']) > 0){
  $sql.=" and a.cliente matches '*".trim(strtoupper($_GET['cl']))."*' ";
}
if (isset($_GET['fd']) && strlen($_GET['fd']) > 0  ){
  $r_fecha_desde=date("Ymd",strtotime($_GET['fd']));
  $sql.=" and e.fecha >= '$r_fecha_desde'";
}
if (isset($_GET['fh']) && strlen($_GET['fh']) > 0 ){
  $r_fecha_hasta=date("Ymd",strtotime($_GET['fh']));
  $sql.=" and e.fecha <= '$r_fecha_hasta'";
}
if (isset($_GET['ar']) && strlen($_GET['ar']) > 0 && $_GET['ar'] != 0){
    $p_art = rtrim($_GET['ar']);
    $sql.=" and b.articulo = '$p_art' ";
}
if (isset($_GET['se']) && strlen($_GET['se']) > 0 && $_GET['se'] != 0){
  $sql.=" and c.serie = '{$_GET['se']}' ";
}
$sql.=" order by a.letra,a.sucur,a.numero  ";

$resultado=@odbc_exec($Base->conexion,$sql);
if (!$resultado){
}

while($fila = odbc_fetch_array($resultado)) 
{
    if ($p_hruta_d != $fila['hruta_d']){
        $p_hruta_d_des="HR: ".$fila['hruta_d'];
        $pdf->cell(60,$p_alto,$p_hruta_d_des,$p_borde,0,'',true);
        $pdf->cell(10,$p_alto,'',$p_borde,1,'',true);
    }
    $p_hruta_d=$fila['hruta_d'];

    $p_remito = trim($fila['tdoc'])."-".$fila['letra']."-".$fila['sucur']."-".$fila['numero']; 
    if ($p_remito_ant != $p_remito){
        $p_provincia_des=r_provincia_des($fila['pais'],$fila['provincia']);
        $p_localidad_des=r_localidad_des($fila['pais'],$fila['provincia'],$fila['localidad']);

        $pdf->cell(35,$p_alto,"- REMITO:".$p_remito,$p_borde,0,'',true);
        $pdf->cell(40,$p_alto,$fila['cliente'],$p_borde,0,'',true);
        $pdf->cell(40,$p_alto,$fila['direccion'],$p_borde,0,'',true);
        $pdf->cell(30,$p_alto,$p_provincia_des,$p_borde,0,'',true);
        $pdf->cell(30,$p_alto,$p_localidad_des,$p_borde,1,'',true);
    }

    $p_fecha_doc = date("d-m-Y",strtotime($fila['fecha_doc']));
    if ($p_fecha_doc == '31-12-1969'){
        $p_fecha_doc = date("d-m-Y",strtotime($fila['fecha_ing']));
    }

    $p_remito_ant=$p_remito;
    $p_mac_adress    = "";
    $sql="select serie2 from {$Base->base_wms}series_nro_mac
           where empresa = '{$fila['empresa']}'
             and serie   = '{$fila['serie']}'";
    $resultado2=@odbc_exec($Base->conexion, $sql);
    if ($resultado2){
        $row2=odbc_fetch_array($resultado2);
        if ($row2){
            $p_mac_adress = $row2['serie2'];
        }
        odbc_free_result($resultado2);
    }
    if (strlen($p_mac_adress) <= 0){
        $sql="select serie from {$Base->base_wms}series_nro_mac
               where empresa = '{$fila['empresa']}'
                 and serie2  = '{$fila['serie']}'";
        $resultado2=@odbc_exec($Base->conexion, $sql);
        if ($resultado2){
            $row2=odbc_fetch_array($resultado2);
            if ($row2){
                $p_mac_adress = $row2['serie'];
            }
            odbc_free_result($resultado2);
        }       
    }

    $pdf->cell(30,$p_alto,"",$p_borde,0,'',true);
    $pdf->cell(30,$p_alto,$fila['articulo'],$p_borde,0,'',true);
    $p_articulo_des=r_articulo_des($fila['empresa'],$fila['articulo']);
    $pdf->cell(60,$p_alto,$p_articulo_des,$p_borde,0,'',true);
    $pdf->cell(30,$p_alto,$fila['serie'],$p_borde,0,'',true);
    $pdf->cell(30,$p_alto,$p_mac_adress,$p_borde,1,'',true);

    $p_columna = 0;
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($p_columna, $p_row, $p_remito);
    $p_columna++;
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($p_columna, $p_row, $fila['cliente']);
    $p_columna++;
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($p_columna, $p_row, $fila['direccion']);
    $p_columna++;
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($p_columna, $p_row, $p_provincia_des);
    $p_columna++;
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($p_columna, $p_row, $p_localidad_des);
    $p_columna++;
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($p_columna, $p_row, $fila['hruta_d']);
    $p_columna++;
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($p_columna, $p_row, $p_fecha_doc);
    $p_columna++;
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($p_columna, $p_row, $fila['articulo']);
    $p_columna++;
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($p_columna, $p_row, $p_articulo_des);
    $p_columna++;
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($p_columna, $p_row, $fila['serie']);
    $p_columna++;
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($p_columna, $p_row, $p_mac_adress);

    $p_row++;

}
odbc_free_result($resultado);

$pdf->cell(20,$p_alto,"",$p_borde,1);

$pdf->SetLineWidth(0.3);
$pdf->Line(5,$pdf->GetY(),200,$pdf->GetY());

switch ($_GET['donde']) {
case 'I':
    $pdf->Output();
    break;
case 'E':
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename=series.xlsx');
    header('Cache-Control: max-age=0');
    $objWriter->save('php://output');
    break;
 
default:   
    $pdf->Output("F","Series.pdf");
    r_envio($_SESSION['mail'],$pdf->titulo," ","Series.pdf");
    break;
} 

?>
