<?php 
require_once "../modelos/Movimientobanco.php";

$movimientobanco    =   new Movimientobanco();

$idmovimientobanco  =   isset($_POST["idmovimientobanco"])? limpiarCadena($_POST["idmovimientobanco"]):"";
$idbancocuenta			=   isset($_POST["idbancocuenta"])? limpiarCadena($_POST["idbancocuenta"]):"";
$tipo_proveedor 		=   isset($_POST["tipo_cliente"])? limpiarCadena($_POST["tipo_cliente"]):"";
$nombre         		=   isset($_POST["nombre"])? limpiarCadena($_POST["nombre"]):"";

switch ($_GET["op"]) {
		
	case 'mostrar':
		$rspta=$movimientobanco->mostrar($idbancocuenta);
		echo json_encode($rspta);
	break;

  case 'listar':
		$rspta=$movimientobanco->listar();
		$data=Array();

		while ($reg=$rspta->fetch_object()) {
			$data[]=array(
			"0"=>'<button class="btn btn-warning btn-xs" onclick="mostrar('.$reg->idbancocuenta.')"><i class="fa fa-eye"></i></button>',
			"1"=>$reg->nombre,
			"2"=>number_format("$reg->cobrado",2,",","."),
			"3"=>number_format("$reg->pagado",2,",","."),
			"4"=>number_format("$reg->saldo",2,",","."),
			);
		}
		$results=array(
             "sEcho"=>1,//info para datatables
             "iTotalRecords"=>count($data),//enviamos el total de registros al datatable
             "iTotalDisplayRecords"=>count($data),//enviamos el total de registros a visualizar
             "aaData"=>$data); 
		echo json_encode($results);
	break;

	case 'listarpago':
		$rspta=$movimientobanco->listarpago();
		$data=Array();

		while ($reg=$rspta->fetch_object()) {
			if ($reg->estado=='Pago'){
				$estado='<span class="badge bg-blue">Pagado</span>';
			}else{
				$estado='<span class="badge bg-red">Anulado</span>';
			}
			$data[]=array(
			"0"=>(($reg->estado!='Anulado')?'<button class="btn btn-primary btn-xs" onclick="mostrarp('.$reg->idpagoproveedor.')"><i class="fa fa-eye"></i></button>'.' '.
			'<button class="btn btn-warning btn-xs" onclick="modificar('.$reg->idpagoproveedor.')"><i class="fa fa-pencil"></i></button>'.' '.		 
			'<button class="btn btn-danger btn-xs" onclick="anularp('.$reg->idpagoproveedor.')"><i class="fas fa-times"></i></button>'.' '.
			'<a target="_blank" href="reportes/pagos.php?id='.$reg->idpagoproveedor.'"><button class="btn btn-info btn-xs"><i class="fa fa-clone"></i></button></a>':
			'<button class="btn btn-primary btn-xs" onclick="mostrarp('.$reg->idpagoproveedor.')"><i class="fa fa-eye"></i></button>'),
			"1"=>$reg->fecha,
			"2"=>$reg->referencia,
			"3"=>number_format("$reg->total_pago",2,",","."),
			"4"=>number_format("$reg->retencion",2,",","."),
			"5"=>$reg->proveedor,
			"6"=>$reg->cuenta,
			"7"=>$estado,
			);
		}
		$results=array(
             "sEcho"=>1,//info para datatables
             "iTotalRecords"=>count($data),//enviamos el total de registros al datatable
             "iTotalDisplayRecords"=>count($data),//enviamos el total de registros a visualizar
             "aaData"=>$data); 
		echo json_encode($results);
	break;

	case 'listardeuda':
			$rspta=$proveedor->listardeuda();
			$data=Array();
			
			while ($reg=$rspta->fetch_object()) {
				if ((number_format("$reg->saldo",2,",","."))>0){
					$deuda='<span class="badge bg-red">Deuda</span>';
				}else{
					$deuda='<span class="badge bg-yellow">Credito</span>';
				}
				$data[]=array(
				"0"=>'<button class="btn btn-warning btn-xs" onclick="mostrar('.$reg->idproveedor.')"><i class="fa fa-eye"></i></button>',
				"1"=>$reg->nombre,
				"2"=>$reg->rif_ci,
				"3"=>number_format("$reg->saldo",2,",","."),
				"4"=>$deuda
				);
			}
			$results=array(
				 "sEcho"=>1,//info para datatables
				 "iTotalRecords"=>count($data),//enviamos el total de registros al datatable
				 "iTotalDisplayRecords"=>count($data),//enviamos el total de registros a visualizar
				 "aaData"=>$data); 
			echo json_encode($results);
	break;

	case 'listarDetalle':
			//recibimos el idventa
			$id=$_GET['id'];

			$rspta=$movimientobanco->listarDetalle($id);
			$saldo=0;
		
					
			
			echo '<thead style="background-color:#A9D0F5">
							<th>TipoMovimiento</th>
							<th>Cliente/Proveedor</th>
              <th>Monto</th>
              <th>Fecha</th>
              <th>Estado</th>
						</thead>';
			while ($reg=$rspta->fetch_object()) {
				if ($reg->tipo_movimiento=='Cobro') {
					$saldo						=	$saldo + $reg->montod;
				
				}else{
					
					$saldo						=	$saldo - $reg->montod;
				}
				$montod	=	number_format("$reg->montod",2,",",".");
				if ($reg->estado=='entrada'){
					echo '<tr class="filas">
					<td><button class="btn btn-success btn-xs" onclick="mostrarc('.$reg->idmovimiento.')">'.$reg->tipo_movimiento.' Ref: '.$reg->referencia.'</button></td>
					<td>'.$reg->nombre.'</td>
					<td>'.$montod.'</td>
					<td>'.date("d-m-Y", strtotime($reg->fecha)).'</td>
					<td><span class="badge bg-green">'.$reg->estado.'</span></td></tr>';
				}else{
					echo '<tr class="filas">
					<td><button class="btn btn-warning btn-xs" onclick="mostrarp('.$reg->idmovimiento.')">'.$reg->tipo_movimiento.' Ref: '.$reg->referencia.'</button></td>
					<td>'.$reg->nombre.'</td>
					<td>'.$montod.'</td>
					<td>'.date("d-m-Y", strtotime($reg->fecha)).'</td>
					<td><span class="badge bg-orange">'.$reg->estado.'</span></td></tr>';
				}
				
			}
			$saldo		=	number_format("$saldo",2,",",".");
			echo '<tfoot>
							<th><h4>Saldo</h4></th>
							<th></th>
            	<th><h4 id="total"> '.$saldo.'</h4><input type="hidden" name="total_venta" id="total_venta"></th>
							<th></th>
							<th></th>
						</tfoot>';
	break;

	case 'listarDetallecobro':
		//recibimos el idventa
		$id=$_GET['id'];

		$rspta=$proveedor->listarDetallecobro($id);
		$total=0;
		echo ' <thead style="background-color:#A9D0F5">
        <th>Seleccionar</th>
        <th>Documento</th>
		<th>Tipo</th>
		<th>Monto</th>
		<th>Abono</th>
		<th>Pendiente</th>
		<th>Estado</th>
		 </thead>';
		$cont=0; 
		while ($reg=$rspta->fetch_object()) {
			$montod	=	$reg->total_venta;
			$montop	=	$reg->montopendiente;
			if ($montop>0){
				$montod	= 	$montop;
			}
			$montod	=	number_format("$montod",2,",",".");
			echo '<tr class="filas">
			<td><input type="checkbox" name="iddocumento[]" id="iddocumento[]" onchange="change_checkbox('.$cont.','.this.')" value="'.$cont.'"></td>
			<td><input type="text" name="num_comprobante[]" value="'.$reg->num_comprobante.'" readonly></td>
			<td><input type="text" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'" readonly></td>
			<td><input type="text" style="text-align:right" name="montod[]" id="montod[]" step="0.01" value="'.$montod.'" readonly></td>
			<td style="text-align:right"><input type="text" style="text-align:right" name="abono[]" id="abono[]" step="0.01" value="0,00" readonly></td>
			<td><input type="text" style="text-align:right" name="pendiente[]" id="pendiente[]" step="0.01" value="0,00" readonly></td>
			<td><input type="text" name="estado[]" value="'.$reg->estado.'" readonly style="color:red"></td>
			<td style="visibility:hidden">
			<input type="hidden" name="idventa[]" id="idventa[]" value="'.$reg->idventa.'">
			
			</td>';
			$cont+=1;	
		}
		
		echo '<tfoot>
		 <tr>
		 <th></th>
		 <th></th>
		 
		 <th>Monto a pagar Bs.</th>
		 <th><input type="text" style="text-align:right" name="total_pago" id="total_pago" value="0.00" step="any"></th>
		 <th></th>
		 <th></th>
		 <th></th>
		 </tr>
		 <tr>
		 <th></th>
		 <th></th>
		 
		 <th>Diferencia Bs.</th>
		 <th><input type="text" style="text-align:right" name="diferencia" id="diferencia" value="0.00" step="any"></th>
		 <th></th>
		 <th></th>
		 <th></th>
		 </tr>
       </tfoot>';
	break;

	case 'listarDetallepago':
		//recibimos el idventa
		$id=$_GET['id'];

		$rspta=$proveedor->listarDetallepago($id);
		$total=0;
		echo ' <thead style="background-color:#A9D0F5">
        <th>Seleccionar</th>
        <th>Documento</th>
		<th>Tipo</th>
		<th>Monto</th>
		<th>Abono</th>
		<th>Pendiente</th>
		<th>Estado</th>
		 </thead>';
		$cont=0; 
		while ($reg=$rspta->fetch_object()) {
			$montod	=	$reg->total_compra;
			$montop	=	$reg->montopendiente;
			if ($montop>0){
				$montod	= 	$montop;
			}
			$montod	=	number_format("$montod",2,",",".");
			echo '<tr class="filas">
			<td><input type="checkbox" name="iddocumento[]" id="iddocumento[]" onchange="change_checkbox('.$cont.','.this.')" value="'.$cont.'"></td>
			<td><input type="text" name="num_comprobante[]" value="'.$reg->num_comprobante.'" readonly></td>
			<td><input type="text" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'" readonly></td>
			<td><input type="text" style="text-align:right" name="montod[]" id="montod[]" step="0.01" value="'.$montod.'" readonly></td>
			<td style="text-align:right"><input type="text" style="text-align:right" name="abono[]" id="abono[]" step="0.01" value="0,00" readonly></td>
			<td><input type="text" style="text-align:right" name="pendiente[]" id="pendiente[]" step="0.01" value="0,00" readonly></td>
			<td><input type="text" name="estado[]" value="'.$reg->estado.'" readonly style="color:red"></td>
			<td style="visibility:hidden">
			<input type="hidden" name="idventa[]" id="idventa[]" value="'.$reg->idingreso.'">
			
			</td>';
			$cont+=1;	
		}
		
		echo '<tfoot>
		 <tr>
		 <th></th>
		 <th></th>
		 
		 <th>Monto a pagar Bs.</th>
		 <th><input type="text" style="text-align:right" name="total_pago" id="total_pago" value="0.00" step="any"></th>
		 <th></th>
		 <th></th>
		 <th></th>
		 </tr>
		 <tr>
		 <th></th>
		 <th></th>
		 
		 <th>Diferencia Bs.</th>
		 <th><input type="text" style="text-align:right" name="diferencia" id="diferencia" value="0.00" step="any"></th>
		 <th></th>
		 <th></th>
		 <th></th>
		 </tr>
       </tfoot>';
	break;
	
	case 'listarmovimientocobro':
		$idmovimiento		=$_GET['idmovimiento'];
		require_once "../modelos/Pagocliente.php";
		$pagocliente 		= new Pagocliente();
		$rspta					=$pagocliente->listarmovimientocobro($idmovimiento);
		$data						=Array();

		while ($reg=$rspta->fetch_object()) {
			$data[]=array(
			"0"=>'<div style="text-align:center">'.$reg->num_comprobante.'</div>',
			"1"=>'<div style="text-align:center">'.$reg->tipo_comprobante.'</div>',
			"2"=>'<div style="text-align:center">'.number_format("$reg->montodocumento",2,",",".").'</div>',
			"3"=>'<div style="text-align:center">'.number_format("$reg->montod",2,",",".").'</div>',
			"4"=>'<div style="text-align:center">'.number_format("$reg->abono",2,",",".").'</div>',
			"5"=>'<div style="text-align:center">'.number_format("$reg->pendiente",2,",",".").'</div>',
			);
		}
		$results=array(
             "sEcho"=>1,//info para datatables
             "iTotalRecords"=>count($data),//enviamos el total de registros al datatable
             "iTotalDisplayRecords"=>count($data),//enviamos el total de registros a visualizar
             "aaData"=>$data); 
		echo json_encode($results);
	break;

	case 'listarmovimientocobrov':
		$idmovimiento		=$_GET['idmovimiento'];
		require_once "../modelos/Pagocliente.php";
		$pagocliente 		= new Pagocliente();
		$rspta					=$pagocliente->listarmovimientocobrov($idmovimiento);
		$data						=Array();
		$cont=0;
		while ($reg=$rspta->fetch_object()) {
			/* $_POST["iddocumento"], $_POST["num_comprobante"], $_POST["tipo_comprobante"], $_POST["mont
			od"],$_POST["montodd"] ,$_POST["abono"],
			$_POST["pendiente"], $_POST["estado"], $_POST["idventa"]) 
			
			iddetalle_cobro
			idcobrocliente
			iddocumento
			num_comprobante
			*/
			$documento = $reg->num_comprobanten;
			if ($reg->tipo_comprobante=='Factura'){
				$documento = $reg->num_comprobantef;
				if ($documento=='') {
					$documento = $reg->num_comprobanten;
				}
			}
			$data[]=array(
			"0"=>'<input style="border:none;width:150px;background-color:#D4D2D2" type="text" name="num_comprobante[]" id="num_comprobante[]" value="'.$documento.'" readonly>
			<input type="hidden" name="iddetalle_cobro[]" id="iddetalle_cobro[]" value="'.$reg->iddetalle_cobro.'">
			<input type="hidden" name="idcobrocliente[]" id="idcobrocliente[]" value="'.$reg->idcobrocliente.'">
			<input type="hidden" name="iddocumento[]" id="iddocumento[]" value="'.$reg->iddocumento.'">',
			"1"=>'<input style="border:none;width:100px;background-color:#D4D2D2" type="text" name="tipo_comprobante[]" id="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'" readonly>',
			"2"=>'<input style="border:none;width:100px;background-color:#D4D2D2"type="text" name="montod[]" id="montod[]" value="'.number_format("$reg->montod",2,",",".").'" readonly>',
			"3"=>'<input style="border:none;width:150px;background-color:#D4D2D2"type="text" name="montodb[]" id="montodb[]" value="'.number_format("$reg->montodb",2,",",".").'" readonly>',
			"4"=>'<input style="border:none;width:100px"type="text" onclick="posicion('.$cont.')" name="abono[]" id="abono[]" value="'.number_format("$reg->abono1",2,",",".").'" >',
			"5"=>'<input style="border:none;width:150px"type="text" onclick="posicion('.$cont.')" name="abonob[]" id="abonob[]" value="'.number_format("$reg->abonob1",2,",",".").'" >',
			"6"=>'<input style="border:none;width:100px"type="text" name="pendiente[]" id="pendiente[]" value="'.number_format("$reg->pendiente1",2,",",".").'" readonly>',
			"7"=>'<input style="border:none;width:150px"type="text" name="pendienteb[]" id="pendienteb[]" value="'.number_format("$reg->pendienteb1",2,",",".").'"readonly>',
			"8"=>$reg->montod,
			"9"=>$reg->montodb,
			"10"=>$reg->abono1,
			"11"=>$reg->abonob1,
			"12"=>$reg->pendiente1,
			"13"=>$reg->pendienteb1,
			);
			$cont=$cont+1;
		}
		$results=array(
             "sEcho"=>1,//info para datatables
             "iTotalRecords"=>count($data),//enviamos el total de registros al datatable
             "iTotalDisplayRecords"=>count($data),//enviamos el total de registros a visualizar
             "aaData"=>$data); 
		echo json_encode($results);
	break;

	case 'listarmovimientocobrov1':
		$idmovimiento		=$_GET['idmovimiento'];
		require_once "../modelos/Pagocliente.php";
		$pagocliente 		= new Pagocliente();
		$rspta					=$pagocliente->listarmovimientocobrov1($idmovimiento);
		$data						=Array();
		$cont=0;
		while ($reg=$rspta->fetch_object()) {
			/* $_POST["iddocumento"], $_POST["num_comprobante"], $_POST["tipo_comprobante"], $_POST["mont
			od"],$_POST["montodd"] ,$_POST["abono"],
			$_POST["pendiente"], $_POST["estado"], $_POST["idventa"]) 
			
			iddetalle_cobro
			idcobrocliente
			iddocumento
			num_comprobante
			*/
			$documento = $reg->num_comprobanten;
			if ($reg->tipo_comprobante=='Factura'){
				$documento = $reg->num_comprobantef;
			}
			$data[]=array(
			"0"=>'<input style="border:none;width:150px;background-color:#D4D2D2" type="text" name="num_comprobante[]" id="num_comprobante[]" value="'.$documento.'" readonly>
			<input type="hidden" name="iddetalle_cobro[]" id="iddetalle_cobro[]" value="'.$reg->iddetalle_cobro.'">
			<input type="hidden" name="idcobrocliente[]" id="idcobrocliente[]" value="'.$reg->idcobrocliente.'">
			<input type="hidden" name="iddocumento[]" id="iddocumento[]" value="'.$reg->iddocumento.'">'.
			
			/* Solo bocatto y Chocomilk*/
			 	'<input type="hidden" name="montodb[]" id="montodb[]" value="'.number_format($reg->montodb,2,",",".").'" >
				<input type="hidden" name="abonob[]" id="abonob[]" value="'.number_format($reg->abonob1,2,",",".").'" >
				<input type="hidden" name="pendienteb[]" id="pendienteb[]" value="'.number_format($reg->pendienteb1,2,",",".").'">',
			/* fin */

			"1"=>'<input style="border:none;width:100px;background-color:#D4D2D2" type="text" name="tipo_comprobante[]" id="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'" readonly>',
			"2"=>'<input style="border:none;width:100px;background-color:#D4D2D2"type="text" name="montod[]" id="montod[]" value="'.number_format($reg->montod,2,",",".").'" readonly>',
			/* "3"=>'<input style="border:none;width:150px;background-color:#D4D2D2"type="text" name="montodb[]" id="montodb[]" value="'.number_format($reg->montodb,2,",",".").'" readonly>', oculto bo y cho*/
			"3"=>'<input style="border:none;width:100px"type="text" onclick="posicion('.$cont.')" name="abono[]" id="abono[]" value="'.number_format($reg->abono1,2,",",".").'" >',
			/* "5"=>'<input style="border:none;width:150px"type="text" onclick="posicion('.$cont.')" name="abonob[]" id="abonob[]" value="'.number_format($reg->abonob1,2,",",".").'" >', oculto bo y cho */
			"4"=>'<input style="border:none;width:100px"type="text" name="pendiente[]" id="pendiente[]" value="'.number_format($reg->pendiente1,2,",",".").'" readonly>',
			/* "7"=>'<input style="border:none;width:150px"type="text" name="pendienteb[]" id="pendienteb[]" value="'.number_format($reg->pendienteb1,2,",",".").'"readonly>', oculto bo y cho*/
			"5"=>$reg->montod,
			"6"=>$reg->montodb,
			"7"=>$reg->abono1,
			"8"=>$reg->abonob1,
			"9"=>$reg->pendiente1,
			"10"=>$reg->pendienteb1,
			);
			$cont=$cont+1;
		}
		$results=array(
             "sEcho"=>1,//info para datatables
             "iTotalRecords"=>count($data),//enviamos el total de registros al datatable
             "iTotalDisplayRecords"=>count($data),//enviamos el total de registros a visualizar
             "aaData"=>$data); 
		echo json_encode($results);
	break;

	case 'listarmovimientopago':
		$idmovimiento		=$_GET['idmovimiento'];
		require_once "../modelos/Pagoproveedor.php";
		$pagoproveedor 	= new Pagoproveedor();
		$rspta					=$pagoproveedor->listarmovimientopago($idmovimiento);
		$data						=Array();

		while ($reg=$rspta->fetch_object()) {
			$data[]=array(
			"0"=>$reg->num_comprobante,
			"1"=>$reg->tipo_comprobante,
			"2"=>number_format("$reg->montod",2,",","."),
			"3"=>number_format("$reg->abono",2,",","."),
			"4"=>number_format("$reg->pendiente",2,",","."),
			);
		}
		$results=array(
             "sEcho"=>1,//info para datatables
             "iTotalRecords"=>count($data),//enviamos el total de registros al datatable
             "iTotalDisplayRecords"=>count($data),//enviamos el total de registros a visualizar
             "aaData"=>$data); 
		echo json_encode($results);
	break;




}
 ?>