<?php
require 'acesso.php';
	$res = array();
	$arcodvde = $_POST['arcodvde'];
	$res["faturas"] = array();
	$linha = 0;
	$var1 = false;
	$var2 = false;
	
	//$con = new PDO("mysql:dbname=$database; charset=utf8", $usuario , $senha);
	if (isset($_POST['contagem'])) {
		//$ret = $con->prepare("SELECT COUNT(arcodcli) AS contagem FROM re04 WHERE arcodcli = '$cliente' AND sr_deleted != 'T'");
		$ret = $con->prepare("SELECT COUNT(*) AS contagem FROM (SELECT * FROM (SELECT *, RANK() OVER (PARTITION BY arcodcli ORDER BY sr_recno DESC) AS rnk FROM re04
		WHERE sr_deleted <> 'T' AND  ardtpgto IS NOT NULL ) AS x WHERE rnk <= 5 UNION SELECT *, '0' AS rnk FROM re04 WHERE sr_deleted <> 'T' AND ardtpgto IS NULL
		ORDER BY arcodcli, sr_recno) as T");
		$ret->execute();
		$mensagem = $ret->errorInfo();
		if ($ret->rowCount() > 0) {
			while ($row = $ret->fetch(PDO::FETCH_ASSOC)) {
				array_push($res["faturas"], $row);
			}
			$var1 = true;
		}
	} else if (isset($_POST['linha'])) {
		$linha = intval($_POST['linha']);
		if ($linha == 0) {
			$ret = $con->prepare("SELECT arcodcli, CONCAT( arsequen, arcontro,'-', COALESCE( arparcel, '01' ) ) AS arfatura, ardtemis, ardtvenc, ardtpgto, 
			arvrdupl, arvrfatu, arvrjuro, arvrpgto, arvrdesc, arobserv, arseqban, arsaldos, b.arnomloc FROM re04 a LEFT JOIN re05 b ON 
			a.arcodloc = b.arcodloc WHERE arcodcli = '$cliente' AND A.sr_deleted != 'T' AND ardtpgto IS NOT NULL ORDER BY ardtvenc DESC LIMIT 5");
			$ret->execute();
			$mensagem = $ret->errorInfo();
			if ($ret->rowCount() > 0) {
				while ($row = $ret->fetch(PDO::FETCH_ASSOC)) {
					array_push($res["faturas"], $row);
				}
				$var1 = true;
			}
		} else {
			$ret = $con->prepare("SELECT arcodcli, CONCAT( arsequen, arcontro,'-',COALESCE( arparcel, '01' ) ) AS arfatura, ardtemis, ardtvenc, ardtpgto,
			arvrdupl, arvrfatu, arvrjuro, arvrpgto, arvrdesc, arobserv, arseqban, arsaldos, b.arnomloc FROM (SELECT * FROM (SELECT *, RANK() OVER (PARTITION BY arcodcli ORDER BY sr_recno DESC) AS rnk FROM re04
			WHERE sr_deleted <> 'T' AND  ardtpgto IS NOT NULL ) AS x WHERE rnk <= 5 UNION SELECT *, '0' AS rnk FROM re04 WHERE sr_deleted <> 'T' AND ardtpgto IS NULL
			ORDER BY arcodcli, ardtvenc DESC ) as a LEFT JOIN re05 B ON a.arcodloc = b.arcodloc  LIMIT ".$linha.", 100");
			
			/*$ret = $con->prepare("SELECT arcodcli, CONCAT( arsequen, arcontro,'-',COALESCE( arparcel, '01' ) ) AS arfatura, ardtemis, ardtvenc, ardtpgto,
			arvrdupl, arvrfatu, arvrjuro, arvrpgto, arvrdesc, arobserv, arseqban, arsaldos, B.arnomloc FROM re04 A LEFT JOIN re05 B ON
			A.arcodloc = B.arcodloc WHERE arcodcli = '$cliente' AND A.sr_deleted != 'T' AND ardtpgto IS NOT NULL ORDER BY ardtvenc DESC LIMIT ".$linha.", 100");*/
			$ret->execute();
			$mensagem = $ret->errorInfo();
			if ($ret->rowCount() > 0) {
				while ($row = $ret->fetch(PDO::FETCH_ASSOC)) {
					array_push($res["faturas"], $row);
				}
				$var2 = true;
			}
		}
	} else {
		$ret = $con->prepare("SELECT arcodcli, CONCAT( arsequen, arcontro,'-', COALESCE( arparcel, '01' ) ) AS arfatura, ardtemis, ardtvenc, ardtpgto,
				arvrdupl, arvrfatu, arvrjuro, arvrpgto, arvrdesc, arobserv, arseqban, arsaldos, B.arnomloc FROM re04 A LEFT JOIN re05 B ON
				A.arcodloc = B.arcodloc WHERE arcodcli = '$cliente' AND A.sr_deleted != 'T' AND ardtpgto IS NOT NULL ORDER BY ardtvenc DESC LIMIT 5");
		$ret->execute();
		$mensagem = $ret->errorInfo();
		if ($ret->rowCount() > 0) {
			while ($row = $ret->fetch(PDO::FETCH_ASSOC)) {
				array_push($res["faturas"], $row);
			}
			$var1 = true;
		}
		$ret = $con->prepare("SELECT arcodcli, CONCAT( arsequen, arcontro,'-',COALESCE( arparcel, '01' ) ) AS arfatura, ardtemis, ardtvenc, ardtpgto,
				arvrdupl, arvrfatu, arvrjuro, arvrpgto, arvrdesc, arobserv, arseqban, arsaldos, B.arnomloc FROM re04 A LEFT JOIN re05 B ON
				A.arcodloc = B.arcodloc WHERE arcodcli = '$cliente' AND A.sr_deleted != 'T' AND ardtpgto IS NULL ORDER BY ardtvenc DESC");
		$ret->execute();
		$mensagem = $ret->errorInfo();
		if ($ret->rowCount() > 0) {
			while ($row = $ret->fetch(PDO::FETCH_ASSOC)) {
				array_push($res["faturas"], $row);
			}
			$var2 = true;
		}
	}
	
	if ($var1 || $var2) {
		$res["sucesso"] = 1;
		echo json_encode($res);
	} else {
		$res["sucesso"] = 0;
		$res["mensagem"] = $mensagem[2];
		echo json_encode($res);
	}

	$con = null;
?>