<?php
/*
Autor: Lic. Carlos Alberto Vaca Coronado
Version: 3.3.0
Cambio: Se valida si ya esta como pagado el documento, si es asi, ya no deja cambiar el status, depende de la 
		configuracion del config.ini
*/

session_name('portal_absa');
session_start();
if(!isset($_SESSION['usuario_p_prov_absa']) || trim($_SESSION['usuario_p_prov_absa']) == "" || trim(strtoupper($_SESSION["URL_PORTAL"]."admin.php")) != trim(strtoupper($_SERVER['HTTP_REFERER']))){
	?><script>window.location.href="<?=$_SESSION["URL_PORTAL"]."index.php"?>"</script><?
	exit;
}
require_once "../conexion/conectar.php";
require_once "../libs/funciones.php";

$conexion = new CONDB();
$conexion->conectar();

if(!$BLOQUEAR_STATUS_PAGADA=trim(getINI("BLOQUEAR_STATUS_PAGADA=","../config.ini"))){
	$BLOQUEAR_STATUS_PAGADA="NO";
}

$rfc=$_REQUEST['id'];
$tipodocto=$_REQUEST['id2'];
$serie=$_REQUEST['id3'];
$folio=$_REQUEST['id4'];
$uuid=$_REQUEST['id5'];
$idunico=$_REQUEST['id6'];

$completaQuery="";

if(trim($_REQUEST['id3']) != "")
	$completaQuery.=" and serie='$serie'";
if(trim($_REQUEST['id4']) != "")
	$completaQuery.=" and folio='$folio'";
if(trim($_REQUEST['id5']) != "")
	$completaQuery.=" and uuid='$uuid'";

$result = mysqli_query($conexion->CONEXION,"select status_pago,total from recibedoce where rfcemisor='$rfc' and tipodocto='$tipodocto' $completaQuery limit 1");
if(mysqli_num_rows($result) > 0){
	$temp=mysqli_fetch_array($result,MYSQLI_ASSOC);
	$totalFactura=$temp['total'];
	
	$qSuma = "select sum(monto) as suma from pago_doc_detalle
	where rfcemisor='$rfc' and tipodocto='$tipodocto' $completaQuery";
	
	$resultSuma = mysqli_query($conexion->CONEXION,$qSuma);
	
	$suma=0;
	$totalSuma=0;
	if(mysqli_num_rows($resultSuma) > 0){
		$tmpSuma=mysqli_fetch_array($resultSuma,MYSQLI_ASSOC);
		$suma = $tmpSuma['suma'];
	}
	
}else{
	echo "<br/><br/><font color='red' style='font-size: 14px'><strong>No se encontro el registro seleccionado, intente nuevamente.</strong></font>";
	exit;	
}

?>
<script language="javascript" src="libs/Vanadium/vanadium.js"></script>

<style>
.modal {
    display:    none;
    position:   fixed;
    z-index:    1000;
    top:        0;
    left:       0;
    height:     100%;
    width:      100%;
    background: rgba( 255, 255, 255, .8 ) 
                url('http://i.stack.imgur.com/FhHRx.gif') 
                50% 50% 
                no-repeat;
}

body.loading {
    overflow: hidden;   
}

body.loading .modal {
    display: block;
}
</style>
<script>
$j_6(document).ready(function() {
	$j_6('#loading').hide();
	
    $j_6().ajaxStart(function() {
        $j_6('#loading').show();
        $j_6('#result').hide();
    }).ajaxStop(function() {
        $j_6('#result').fadeIn('slow');
		$j_6('#loading').hide();
    });
	
		/*$j_3('#formProveedor').ajaxForm(function(data) {
						$j_3('#result').html(data);
						$j_3('#loading').hide();
		});*/
		
		$j_6('#form, #fat, #form_status').submit(function() {
			document.getElementById('guardarPago').disabled=true;
			$j_6("body").addClass("loading");
        $j_6.ajax({
            type: 'POST',
            url: $j_6(this).attr('action'),
            data: $j_6(this).serialize(),
            success: function(data) {
				//alert(data);
					$j_6('#result').html(data);
					$j_6('#loading').hide();
					$j_6("body").removeClass("loading");
					document.getElementById('guardarPago').disabled=false;
            }
        })
        
        return false;
    });
	
	$j_6('#fecha_pago').mask("9999-99-99",{placeholder:"_"});
	
	$j_6('#ccPago').combobox({
        formatter:function(row){
            var imageFile = 'Imagenes/' + row.icon;
            return '<img class="item-img" src="'+imageFile+'"/><span class="item-text">'+row.text+'</span>';
        },
		onChange: function(param){
			$j_6.post("formularios/guardar/cambia_status_pago.php",{rfc: '<?=$rfc?>', tipodocto: '<?=$tipodocto?>', serie: '<?=$serie?>', folio: '<?=$folio?>', uuid: '<?=$uuid?>', status: param},function(data){
				$j_6("#div_pagos").html(data);
			});
		}
    });
		
});

/*function cambiaStatus_check(id){
	
	if(id.id=="check_revisado"){
		id.checked=true;
		document.getElementById('check_pagado').checked=false;
	}
	if(id.id=="check_pagado"){
		id.checked=true;
		document.getElementById('check_revisado').checked=false;
	}
	
	document.getElementById('status_pago').value=id.value;
}*/

$j_6(function() {
	$j_6( "#fecha_pago" ).datepicker({
		showOn: "button",
		buttonImage: "Imagenes/calendar16.png",
		buttonImageOnly: true
	});
	$j_6( "#fecha_pago" ).datepicker("option", "dateFormat", "yy-mm-dd");
	
	$j_6('#fecha_pago').val('<?=$temp['fecha_pago']?>');
});

function cargaPagos(){
	$j_6.post("formularios/extras/carga_pagos.php",{id: '<?=$_REQUEST['id']?>',id2: '<?=$_REQUEST['id2']?>',id3: '<?=$_REQUEST['id3']?>',id4: '<?=$_REQUEST['id4']?>',id5: '<?=$_REQUEST['id5']?>'},function(data){
		$j_6("#div_pagos").html(data);
	});
	
}

function editarPago(id,referencia,fecha_pago,concepto_pago,monto){
	document.getElementById('id').value=id;
	document.getElementById('referencia').value=referencia;
	document.getElementById('fecha_pago').value=fecha_pago;
	document.getElementById('concepto_pago').value=concepto_pago;
	document.getElementById('monto').value=monto;
}

function eliminarPago(id){
	$j_6.post("formularios/extras/elimina_pago.php",{id: id, rfc: '<?=$rfc?>', tipodocto: '<?=$tipodocto?>', serie: '<?=$serie?>', folio: '<?=$folio?>', uuid: '<?=$uuid?>', idunico: '<?=$idunico?>'},function(data){
		
		$j_6("#result").html(data);
	});
}

/*function cambiaStatusPago(id){
	alert(document.getElementById('cc').value);
	/*$j_6.post("formularios/extras/elimina_pago.php",{id: id, rfc: '<?=$rfc?>', tipodocto: '<?=$tipodocto?>', serie: '<?=$serie?>', folio: '<?=$folio?>', uuid: '<?=$uuid?>', status: document.getElementById('cc').value, idunico: '<?=$idunico?>'},function(data){
		
		$j_6("#result").html(data);
	});*/
//}

function cancelarPago(){
	document.getElementById('id').value="";
	document.getElementById('referencia').value="";
	document.getElementById('fecha_pago').value="";
	document.getElementById('concepto_pago').value="";
	document.getElementById('monto').value="";
}

cargaPagos();
</script>
<div class="modal"></div>
<font color="gray" style="font-size: 14px"><strong>Fecha de pago</strong></font><br/><br/>
<form action="formularios/guardar/alta_fecha_pago.php" name="form_status" id="form_status">
	
	<input type="hidden" name="rfcemisor" id="rfcemisor" value="<?=$rfc?>" />
	<input type="hidden" name="tipodocto" id="tipodocto" value="<?=$tipodocto?>" />
	<input type="hidden" name="serie" id="serie" value="<?=$serie?>" />
	<input type="hidden" name="folio" id="folio" value="<?=$folio?>" />
	<input type="hidden" name="uuid" id="uuid" value="<?=$uuid?>" />
	<input type="hidden" name="id" id="id" />
	<input type="hidden" name="idunico" id="idunico" value="<?=$idunico?>"/>
	
	
		
		<?
		$readonly="";
		if($BLOQUEAR_STATUS_PAGADA=="SI"){
			if($temp['status_pago'] == "P" && $_SESSION['esAdmin_p_prov_absa'] != "SI"){
				$readonly='readonly="true"';
			}
		}?>
		<?if($temp['status_pago']!="P" || $_SESSION['esAdmin_p_prov_absa'] == "SI"){?>
		<table>
		<tr>
			<td>
				Status Pago: &nbsp;<input id="ccPago" style="width:200px"
				url="formularios/status_pago.json"
				valueField="id" textField="text" value="<?=$temp['status_pago']?>">
				</input>
			</td>
		</tr>
		
		<tr><td align="left">
			Referencia: &nbsp;<input <?=$readonly?> type="text" size="20" maxlength="20" name="referencia" id="referencia"/>&nbsp;&nbsp;
			Concepto: &nbsp;<input <?=$readonly?> type="text" size="20" name="concepto_pago" id="concepto_pago"/>&nbsp;&nbsp;
			Fecha de Pago: &nbsp;<input <?=$readonly?> type="text" size="10" name="fecha_pago" id="fecha_pago"/>&nbsp;&nbsp;
			<br/>
			<br/>
			Monto: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input <?=$readonly?> onkeyup="onlyNumsAndOneDotAndLimitDecimals(this,6)" type="text" size="10" name="monto" id="monto"/>&nbsp;&nbsp;
			<input type="submit" value="Guardar" id="guardarPago"/>
			<input type="button" value="Limpiar" onclick="cancelarPago();"/>
		
		</td></tr></table>
		<?}?>
</form>
<div id="div_pagos"></div>
<br/>
<div style="text-align: left">
<table width="300" class="td_general">
	<tr>
		<th width="150" style="text-align: left">Total Factura:</th>
		<td width="150" style="text-align: right"><?=round($totalFactura,6)?></td>
	</tr>
	<tr>
		<th style="text-align: left">Total Detalle:</th>
		<td id="totalDetalle" style="text-align: right"><?=round($suma,6)?></td>
	</tr>
	<tr>
		<th style="text-align: left">Diferencia:</th>
		<td id="diferencia" style="text-align: right"><?=round($totalFactura-$suma,6)?></td>
	</tr>
</table>
</div>