<?php
// @(#) $Id$
// +-----------------------------------------------------------------------+
// | Copyright (C) 2008, http://yoursite                                   |
// +-----------------------------------------------------------------------+
// | This file is free software; you can redistribute it and/or modify     |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation; either version 2 of the License, or     |
// | (at your option) any later version.                                   |
// | This file is distributed in the hope that it will be useful           |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of        |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          |
// | GNU General Public License for more details.                          |
// +-----------------------------------------------------------------------+
// | Author: pFa                                                           |
// +-----------------------------------------------------------------------+
//
?>

<?php require_once('../Connections/PBXCDR.php'); ?>
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="Reporte de Llamadas"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Acceso Restringido - JDA Soluciones Avanzadas SRL';
    exit;
}

$user=$_SERVER['PHP_AUTH_USER'];
$pwrd=$_SERVER['PHP_AUTH_PW'];
if ($user!="jcaldazosa" || $pwrd!="sapoop23")
	{
	echo 'Acceso Restringido - WrPass - JDA Soluciones Avanzadas SRL';
    exit;
	}

$primday=date("Y-m-d", mktime(0, 0, 0, date("m")-1, 1));
$lastday=date("Y-m-d", mktime(0, 0, 0, date("m"), 0));
if (isset($_GET["actual"]))
	{
	$primday=date("Y-m-d", mktime(0, 0, 0, date("m"), 1));
	$lastday=date("Y-m-d", mktime(0, 0, 0, date("m")+1, 0));
	}
$primday="2018-09-01";
$lastday="2019-04-01";
$ext=99999;
$ppm=0.01333333333;
$ppmtotal=0;
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_PBXCDR, $PBXCDR);
$query_Llamadas = "SELECT *, IF (accountcode like '_______', SUBSTRING(accountcode,1,3), cnum) AS caller FROM cdr WHERE (accountcode <> '' OR cnum like '___') AND cnum <> '999' AND SUBSTRING(accountcode,1,3) <> '999' AND calldate >= '$primday' AND calldate <= '$lastday' AND dst like '____%' ORDER by calldate ";
$Llamadas = mysql_query($query_Llamadas, $PBXCDR) or die(mysql_error());
//$row_Llamadas = mysql_fetch_assoc($Llamadas);
$totalRows_Llamadas = mysql_num_rows($Llamadas);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>REPORTE DE LLAMADAS HOTEL MITRU SUR - <?= $primday ?> a <?= $lastday ?></title>
<style type="text/css">
.titulos {
	font-weight: bold;
	text-align: center;
}
.titulos {
	text-align: center;
	color: #FFF;
}
body,td,th {
	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
	font-size: 18px;
}
.TIT {
	font-weight: bold;
	font-style: italic;
}
</style>
</head>

<body>
<p><img src="../img/logologin.jpg" width="225" height="78" /></p>
<p class="TIT">REPORTE DE LLAMADAS HOTEL MITRU SUR - <?= $primday ?> a <?= $lastday ?></p>
<table width="1082" border="0" cellpadding="2" cellspacing="2">
  <?php while ($row_Llamadas = mysql_fetch_assoc($Llamadas)) { ?>
  <?php
  if ($row_Llamadas['caller']!=$ext)
  {
	$ext=$row_Llamadas['caller'];
 /* ?>
  <tr>
    <td colspan="7" bgcolor="#000000"><span class="titulos">EXTENSION 
      <?= $ext ?>    
    </span></td>
  </tr>

  <tr style="border-bottom:1pt solid black;">
    <td width="153" bgcolor="#999999"><span class="titulos">Fecha y Hora</span></td>
    <td width="121" bgcolor="#999999" style="font-weight: bold"><span class="titulos">Extensión</span></td>
    <td width="166" bgcolor="#999999" style="font-weight: bold"><span class="titulos">Destino</span></td>
    <td width="166" bgcolor="#999999"><span class="titulos">Canal de Salida</span></td>
    <td width="134" bgcolor="#999999"><span class="titulos">Segundos</span></td>
    <td width="168" bgcolor="#999999"><span class="titulos">Estado Llamada</span></td>
    <td width="130" bgcolor="#999999"><span class="titulos">Nombre</span></td>
  </tr>
  <?php
 */ }
  ?>
  
  
  
    <tr style="border-bottom:1pt solid black;">
      <td style="border-bottom:thin; border-bottom-width:thin; border-bottom-color:#999;" bgcolor="#F4F4F4"><?php echo $row_Llamadas['calldate']; ?></td>
      <td style="border-bottom:thin; border-bottom-width:thin; border-bottom-color:#999;" bgcolor="#F4F4F4"><?php echo $row_Llamadas['caller']; ?></td>
      <td style="border-bottom:thin; border-bottom-width:thin; border-bottom-color:#999;" bgcolor="#F4F4F4"><?php echo $row_Llamadas['dst']; ?></td>
      <td style="border-bottom:thin; border-bottom-width:thin; border-bottom-color:#999;" bgcolor="#F4F4F4"><?php
	  $tigo=false; 
	  if(strpos($row_Llamadas['dstchannel'],"SIP/TigoSIP")!== false) { echo("Troncal TIGO"); $tigo=true; }
	  if(strpos($row_Llamadas['dstchannel'],"DAHDI/1")!== false) echo("Línea Fija 1");
	  if(strpos($row_Llamadas['dstchannel'],"DAHDI/2")!== false) echo("Línea Fija 2");
	  if(strpos($row_Llamadas['dstchannel'],"DAHDI/3")!== false) echo("Línea Fija 3");
	  if(strpos($row_Llamadas['dstchannel'],"DAHDI/4")!== false) echo("Línea Fija 4");
	  if(strpos($row_Llamadas['dstchannel'],"SIP/Internacional")!== false) echo("Línea Internacional");
	  ?></td>
      <td style="border-bottom:thin; border-bottom-width:thin; border-bottom-color:#999;" bgcolor="#F4F4F4"><?php echo $row_Llamadas['billsec']; ?></td>
      <td style="border-bottom:thin; border-bottom-width:thin; border-bottom-color:#999;" bgcolor="#F4F4F4"><?php if ($row_Llamadas['disposition']=="ANSWERED") echo("CONTESTADA");
	  if ($row_Llamadas['disposition']=="NO ANSWER") echo("NO CONTESTADA"); ?></td>
      <td style="border-bottom:thin; border-bottom-width:thin; border-bottom-color:#999;" bgcolor="#F4F4F4"><?php echo $row_Llamadas['cnam']; ?></td>
      <td style="border-bottom:thin; border-bottom-width:thin; border-bottom-color:#999;" bgcolor="#F4F4F4"><? 
	  if ($tigo && !(strlen($row_Llamadas['dst'])==10 && substr($row_Llamadas['dst'], 0, 4)=="9800")) 
	  	{
		$ptot=$ppm*$row_Llamadas['billsec'];
		echo(number_format($ptot, 2,",","")." Bs.");
		$ppmtotal=$ppmtotal+$ptot;
		}
	  ?></td>
    </tr>
    <?php }  ?>
</table>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TOTAL CONSUMO TIGO: <? echo("Bs. ".number_format($ppmtotal,2, ",", "")); ?></p>
</body>
</html>
<?php
mysql_free_result($Llamadas);
?>
		