<?php

require('../application/config/db.cfg.php');
require('../core/classes/db.class.php');
require('../core/classes/collection.class.php');

$db = new Db();

$login = (isset($_GET['login'])&&$_GET['login'])?$_GET['login']:false;
$password = (isset($_GET['password'])&&$_GET['password'])?$_GET['password']:false;

$db_pass = $db->get("SELECT * FROM ".DB_PREFIX."settings_hidden WHERE code='1C_users_db_pass';");

if (
	($login == '1C_users' && $password == $db_pass['value'])
	||
	($login == '1C_users' && $password == md5($db_pass['value']))
){
	
	$sql = "
		SELECT 
			A.id,
			A.email,
			A.name,
			A.phones,
			A.country,
			DC.name city,
			A.address,
			A.info,
			A.discount,
			FROM_UNIXTIME(A.dt),
			A.is_active,
			A.is_firm,
			A.firm_name,
			A.firm_inn,
			A.firm_kpp,
			A.firm_bank,
			A.firm_pc,
			A.firm_kc,
			A.firm_bnk,
			A.firm_ogrn,
			A.firm_okpo,
			A.firm_discount,
			A.balance,
			O.name office,
			U.name manager,
			A.account_code 	 	
		FROM ".DB_PREFIX."accounts A
		LEFT JOIN ".DB_PREFIX."dic_cities DC ON DC.id=A.city
		LEFT JOIN ".DB_PREFIX."offices O ON O.id=A.office_id
		LEFT JOIN ".DB_PREFIX."_user U ON U.id=A.set_manager_id
		ORDER BY `id` DESC;";
	
	$res = $db->query($sql);

	$ret = array();
	if (isset($res) && count($res)>0)
	{
		foreach ($res as $dd)
		{
			$ret []= $dd;
		}
		
		echo json_encode($ret);
		exit;
	}
	else 
	{
		echo json_encode(array("NO DATA"));
		exit;
	}
	
}
else 
{
	echo json_encode(array("LOGIN/PASSWORD INCORRECT"));
	exit;
}
exit();

?>