<?php
require 'acesso.php';

	$res = array();
	$id_firma    = $_POST['id_firma'];
	$id_device = $_POST['id_device'];

	//$con = new PDO("mysql:dbname=$database; charset=utf8", $usuario , $senha);
	$query = "SELECT arcodcli, arnomcli, arfancli, arcodrot, arinscli, arinsmun, arendcli, arbaicli, arcidcli, ar_ufcli, arcepcli, artelcli,
				arsitcli, artelcm1, arfaxcom, arnumend, arlimcre, artipcli, arcpfcli, arcgccli, aremaill, arnparce, ardiapri, ardiaent, 
				arlatitude, arlongetude, arcodvde, cnpj_faturamento, dia_visita_venda,arboleto_banco, ardtucom, ardtultc
				FROM re03 ";
	if (isset($_POST['contagem'])) { // retorna o numero de registros a serem recebidos
		if (isset($_POST['vendedor'])) {
			$vendedor = $_POST['vendedor'];
		} else {
			$vendedor = "";
		}

		if ($vendedor == "") {
			$ret = $con->prepare("SELECT COUNT(arcodcli) AS contagem FROM re03 WHERE sr_deleted != 'T'");
		} else {
			$ret = $con->prepare("SELECT COUNT(arcodcli) AS contagem FROM re03 WHERE sr_deleted != 'T' AND arcodvde = '$vendedor'");
		}
	} elseif (isset($_POST['linha'])) { // mostra os registros dentro de limites x até y
		if (isset($_POST['vendedor'])) {
			$vendedor = $_POST['vendedor'];
		} else {
			$vendedor = "";
		}

		$linha = $_POST['linha'];
		if ($vendedor == "") {
			$ret = $con->prepare($query . "WHERE sr_deleted != 'T' AND arnomcli != '' HAVING arnomcli IS NOT NULL ORDER BY arnomcli LIMIT $linha, 100");
		} else {
			$ret = $con->prepare($query . "WHERE sr_deleted != 'T' AND arcodvde = '$vendedor' AND arnomcli != '' HAVING arnomcli IS NOT NULL ORDER BY arnomcli LIMIT $linha, 100");
		}
	} elseif (isset($_POST['verificar'])){ // verificar se o cliente existe
		$cpf_cnpj = $_POST['cpf_cnpj'];
		$ret = $con->prepare("SELECT arcodcli FROM re03 WHERE arcpfcli = '$cpf_cnpj' OR arcgccli = '$cpf_cnpj'");
	} elseif (isset($_POST['pesquisar'])){ // pesquisar clientes
		$campo = $_POST['campo'];
		$termo = $_POST['termo'];
		if (isset($_POST['contido'])) {
			switch ($campo) {
				case 0: // nome/razão social
					$like_termos = "";
					$like_array = explode(' ', $termo);
					foreach($like_array as $palavra) {
						$like_termos = $like_termos . "arnomcli LIKE '%$palavra%' AND ";
					}
					$like_termos = substr($like_termos, 0, -4);
					$ret = $con->prepare($query . "WHERE sr_deleted != 'T' AND arnomcli != '' AND $like_termos HAVING arnomcli IS NOT NULL ORDER BY arnomcli");
					break;
				case 1: // nome fantasia
					$like_termos = "";
					$like_array = explode(' ', $termo);
					foreach($like_array as $palavra) {
						$like_termos = $like_termos . "arfancli LIKE '%$palavra%' AND ";
					}
					$like_termos = substr($like_termos, 0, -4);
					$ret = $con->prepare($query . "WHERE sr_deleted != 'T' AND arnomcli != '' AND $like_termos HAVING arnomcli IS NOT NULL ORDER BY arnomcli");
					break;
				case 2: // cpf/cnpj
					$ret = $con->prepare($query . "WHERE sr_deleted != 'T' AND arnomcli != '' AND arcpfcli = '$termo' OR arcgccli = '$termo' HAVING arnomcli IS NOT NULL ORDER BY arnomcli");
					break;
				case 3: // código
					$codigo = str_pad($termo, 6, "0", STR_PAD_LEFT);
					$ret = $con->prepare($query . "WHERE sr_deleted != 'T' AND arnomcli != '' AND arcodcli = '$codigo' HAVING arnomcli IS NOT NULL ORDER BY arnomcli");
					break;
			}
		} else {
			switch ($campo) {
				case 0: // nome/razão social
					$ret = $con->prepare($query . "WHERE sr_deleted != 'T' AND arnomcli != '' AND arnomcli LIKE '$termo%' HAVING arnomcli IS NOT NULL ORDER BY arnomcli");
					break;
				case 1: // nome fantasia
					$ret = $con->prepare($query . "WHERE sr_deleted != 'T' AND arnomcli != '' AND arfancli LIKE '$termo%' HAVING arnomcli IS NOT NULL ORDER BY arnomcli");
					break;
				case 2: // cpf/cnpj
					$ret = $con->prepare($query . "WHERE sr_deleted != 'T' AND arnomcli != '' AND arcpfcli LIKE '$termo%' OR arcgccli LIKE '$termo%' HAVING arnomcli IS NOT NULL ORDER BY arnomcli");
					break;
				case 3: // código
					$codigo = str_pad($termo, 6, "0", STR_PAD_LEFT);
					$ret = $con->prepare($query . "WHERE sr_deleted != 'T' AND arnomcli != '' AND arcodcli = '$codigo' HAVING arnomcli IS NOT NULL ORDER BY arnomcli");
					break;
			}
		}
	} elseif (isset($_POST['novo'])) {
		$ret = $con->prepare("SELECT LPAD(Max(arcodcli)+1, 6, '0') AS codigo FROM re03");
		$ret->execute();
		$erro = $ret->errorInfo();
		$row = $ret->fetch(PDO::FETCH_OBJ);
		$codigo = $row->codigo;
		$res["codigo"] = $codigo;
		
		$arnomcli = $_POST['arnomcli'];
		$arfancli = $_POST['arfancli'];
		$arinscli = $_POST['arinscli'];
		$arinsmun = $_POST['arinsmun'];
		$arendcli = $_POST['arendcli'];
		$arnumend = $_POST['arnumend'];
		$arbaicli = $_POST['arbaicli'];
		$arcidcli = $_POST['arcidcli'];
		$ar_ufcli = $_POST['ar_ufcli'];
		$arcepcli = $_POST['arcepcli'];
		$artelcli = $_POST['artelcli'];
		$artelcm1 = $_POST['artelcm1'];
		$arfaxcom = $_POST['arfaxcom'];
		$artipcli = $_POST['artipcli'];
		$arcpfcli = $_POST['arcpfcli'];
		$arcgccli = $_POST['arcgccli'];
		$arsitcli = $_POST['arsitcli'];
		$arlimcre = $_POST['arlimcre'];
		$aremaill = $_POST['aremaill'];
		$arcodvde = $_POST['arcodvde'];
		if (strlen($_POST['arlatitude']) > 0) {
			$arlatitude = $_POST['arlatitude'];
		} else {
			$arlatitude = 0.0;
		}
		
		if (strlen($_POST['arlongetude']) > 0) {
			$arlongetude = $_POST['arlongetude'];
		} else {
			$arlongetude = 0.0;
		}
		
		$ret = $con->prepare("INSERT INTO re03 (arcodcli, arnomcli, arfancli, arinscli, arinsmun, arendcli, arnumend, arbaicli, arcidcli, ardtccli,
				ar_ufcli, arcepcli, artelcli, artelcm1, arfaxcli, artipcli, arcpfcli, arcgccli, arsitcli, arlimcre, aremaill, arcodvde, arlatitude, 
				arlongetude) VALUES ('$codigo', '$arnomcli', '$arfancli', '$arinscli', '$arinsmun', '$arendcli', '$arnumend', '$arbaicli', '$arcidcli', CURDATE(), 
				'$ar_ufcli', '$arcepcli', '$artelcli', '$artelcm1', '$arfaxcom', '$artipcli', '$arcpfcli', '$arcgccli', '$arsitcli', $arlimcre, '$aremaill',
				'$arcodvde', $arlatitude, $arlongetude)");
		$ret->execute();
		$mensagem = $ret->errorInfo();
	} elseif (isset($_POST['atualizar'])) {
		$arcodcli = $_POST['arcodcli'];
		$arnomcli = $_POST['arnomcli'];
		$arfancli = $_POST['arfancli'];
		$arinscli = $_POST['arinscli'];
		$arinsmun = $_POST['arinsmun'];
		$arendcli = $_POST['arendcli'];
		$arnumend = $_POST['arnumend'];
		$arbaicli = $_POST['arbaicli'];
		$arcidcli = $_POST['arcidcli'];
		$ar_ufcli = $_POST['ar_ufcli'];
		$arcepcli = $_POST['arcepcli'];
		$artelcli = $_POST['artelcli'];
		$artelcm1 = $_POST['artelcm1'];
		$arfaxcom = $_POST['arfaxcom'];
		$aremaill = $_POST['aremaill'];
		$arlatitude = $_POST['arlatitude'];
		$arlongetude = $_POST['arlongetude'];
		
		$ret = $con->prepare("UPDATE re03 SET arnomcli = '$arnomcli', arfancli = '$arfancli', arinscli = '$arinscli', arinsmun = '$arinsmun', 
				arendcli = '$arendcli', arnumend = '$arnumend', arbaicli = '$arbaicli', arcidcli = '$arcidcli', ar_ufcli = '$ar_ufcli', arcepcli = '$arcepcli', 
				artelcli = '$artelcli', artelcm1 = '$artelcm1', arfaxcom = '$arfaxcom', aremaill = '$aremaill', arlatitude = $arlatitude, 
				arlongetude = $arlongetude WHERE arcodcli = '$arcodcli'");
	} else {		
		$ret = $con->prepare($query . "WHERE sr_deleted != 'T' AND arnomcli != '' HAVING arnomcli IS NOT NULL ORDER BY arnomcli LIMIT 14");
	}
	$ret->execute();
	$mensagem = $ret->errorInfo();
	if($ret->rowCount() > 0) {
		$res["clientes"] = array();
		while ($row = $ret->fetch(PDO::FETCH_ASSOC)) {
			array_push($res["clientes"], $row);
		}
	    $res["sucesso"] = 1;
	} else {
	    $res["sucesso"] = 0;
	    $res["mensagem"] = $mensagem[2];
	}
	echo json_encode($res);
	$con = null;
?>

