<?php 
header("Access-Control-Allow-Origin: *");
header("Content-Type:application/json; charset=UTF-8");
header("Access-Control-Allow-Methods:POST");
header("Access-Control-Max-Age:3600");
header("Access-Control-Allow-Headers:Content-Type,Access-Control-Allow-Headers,Authorization,X-Requested-With");

include_once '../Database.php';
include_once 'pagos.php';

$database = new DataBase();
$db = $database->getConnection();

$pagos = new Pagos($db);
try{
	$data = json_decode(file_get_contents("php://input"));
	$seltmt = $pagos->select("select * from pagos where cotizacion='".$data->cotizacion."' limit 1");
		$num = $seltmt->rowCount();

		if($num==0){
        	$query = "INSERT INTO ".$pagos->table_name." SET cantidad=:cantidad,cotizacion=:cotizacion ";
        	$stm = $pagos->conn->prepare($query);
        	$stm->bindParam(":cantidad",$data->cantidad);
        	$stm->bindParam(":cotizacion",$data->cotizacion);
        	$stmt = $pagos->Insert($stm);
        	if($stmt !==false){
        		echo json_encode(array("Id"=>$stmt));
        	}else{
        		echo json_encode(array("Error"=>"No se ha creado el registro"));
        	}
        		    
		}else{
		    $row =$seltmt->fetch(PDO::FETCH_ASSOC);
		    extract($row);
		    echo json_encode(array("Id"=>$id));
		}
}catch(Exception $ex){
	echo json_encode(array("Error"=>"No se ha podido procesar la solicitud"));
}

?>