<?php 

$path_to_root ="../../.."; 
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");

include_once("../kvcodes.inc");

if(isset($_GET['Line_chart'])){
	 $top_selling_items =  class_balances($_GET['Line_chart']); 
	 $area_chart =  array(); 
	 foreach($top_selling_items as $top) { 
	 	$area_chart[] =   array("class" => $top['class_name'] , "value" => abs($top['total']));  
	 } 
	 echo json_encode($area_chart); exit; 
}

if(isset($_GET['Area_chart'])){
	 $top_selling_items =  Top_selling_items($_GET['Area_chart']); 
	 $area_chart =  array(); 
	 foreach($top_selling_items as $top) { $area_chart[] =   array("y" => $top['description'] , "a" => round($top['total'], 2), "b" => round($top['costs'], 2));  } echo json_encode($area_chart); exit; 
}

if(isset($_GET['Customer_chart'])){
	$cutomers = get_top_customers($_GET['Customer_chart']);
	$donut_chart =  array(); 
	 foreach($cutomers as $top) { 
	 	$donut_chart[] =   array("label" => $top['name'] , "value" => round($top['total'], 2));  
	 } 
	 echo json_encode($donut_chart); exit;
}

if(isset($_GET['Supplier_chart'])){
	$suppliers = get_top_suppliers($_GET['Supplier_chart']);
	$donut_chart =  array(); 
	 foreach($suppliers as $top) { 
	 	$donut_chart[] =   array("label" => $top['supp_name'] , "value" => round($top['total'], 2));  
	 } 
	 echo json_encode($donut_chart); exit;
}

if(isset($_GET['Expense_chart'])){
	$cutomers = Expenses($_GET['Expense_chart']);
	$bar_chart =  array(); 
	 foreach($cutomers as $top) { 
	 	$bar_chart[] =   array("y" => htmlspecialchars_decode($top['name']) , "a" => round($top['balance'], 2));  
	 } 
	 if(empty($bar_chart)){
		$bar_chart[] = array("y" => "nothing" , "a" => 0);
	}
	 echo json_encode($bar_chart); exit;
}
if(isset($_GET['Tax_chart'])){
	$suppliers = get_tax_reports($_GET['Tax_chart']);
	$donut_chart =  array(); 
	 foreach($suppliers as $top) { 
	 	$donut_chart[] =   array("label" => $top['name'] , "value" => abs(round($top['total'], 2)));  
	 } 
	// $donut_chart['grandtotal'] = abs(round($suppliers['grandtotal'],2));
	 echo json_encode($donut_chart); exit;
}

if(isset($_GET['ChangeCompany'])) {
	if(isset($db_connections[$_GET['ChangeCompany']])){
		$_SESSION['wa_current_user']->company = $_GET['ChangeCompany'];
		$db_table_name = $db_connections[$_SESSION['wa_current_user']->company]['dbname'].'.'.$db_connections[$_SESSION['wa_current_user']->company]['tbpref'];
		$sql = "SELECT id,user_bank_acc FROM ".$db_table_name."users WHERE user_id =".db_escape($_SESSION['wa_current_user']->loginname)." LIMIT 1";
		$res = db_query($sql, "Can't get user Account");
		if(db_num_rows($res) == 1){
			if($row = db_fetch($res)){
				$current_user_id = $row['id'];
			} else {
				$current_user_id = -1;
			}
		} else 
			$current_user_id = -1;

		if($current_user_id == -1){
			$sql = "SELECT id,user_bank_acc FROM ".$db_table_name."users ORDER BY id LIMIT 1";
				$res = db_query($sql, "Can't get user Account");
				$row = db_fetch($res);
				$current_user_id = $row['id'];
		}
		
			$user_bank_acc = explode(',',$row["user_bank_acc"]);
			$save_user_bank_acc=array();
			foreach ($user_bank_acc as $key => $value) 
			$save_user_bank_acc[] = db_escape($value);
			$_SESSION['wa_current_user']->user_bank_acc = implode(', ', $save_user_bank_acc);  // $row["location"];


		echo $_SESSION['wa_current_user']->user = $current_user_id;
	}

	echo $_SESSION['wa_current_user']->company;
} elseif(isset($_GET['GetCompany']) && $_GET['GetCompany'] == 'yes') {
	$filtered =[];
	include_once($path_to_root."/themes/LTE/includes/users.php");
	$row = get_master_login($_SESSION['wa_current_user']->loginname);
	$companies = unserialize(base64_decode($row['companies']));

	if(isset($_GET['term']) && $_GET['term'] != ''){		
		foreach($db_connections as $cid => $data){
			if (strpos($_GET['term'], $data['name']) !== FALSE && (isset($companies) && in_array($cid, $companies))) { // Yoshi version
			        $filtered[$cid] = $data['name'];
   			}
		}
	}
	if(!empty($filtered))
		echo json_encode($filtered);
	else
		echo false;
}
if(isset($_GET['search_dropdown']) && ($_GET['search_dropdown'] == 'stock_id' || $_GET['search_dropdown'] == 'row_stock_id' || $_GET['search_dropdown'] == 'finish_stock_id')){
	$stock_types = ['sales', 'manufactured', 'purchasable', 'costable', 'component', 'kits', 'all'];

	$search_stock_id = kv_get_items_list($_GET['q'], (isset($_GET['type']) && in_array(trim($_GET['type']), $stock_types) ? $_GET['type'] : 'sales'));
	$search_stock = [];
	while ($r = db_fetch($search_stock_id)) {
            $search_stock[] = ['id' => $r['item_code'], 'text' => $r['description']];
        }
	// $donut_chart['grandtotal'] = abs(round($suppliers['grandtotal'],2));
	 echo json_encode($search_stock); exit;
}

if(isset($_GET['search_dropdown']) && $_GET['search_dropdown'] == 'customer_id'){

	$search_stock_id = kv_get_customers_list($_GET['q']);
	$search_stock = [];
	while ($r = db_fetch($search_stock_id)) {
            $search_stock[] = ['id' => $r['debtor_no'], 'text' => $r['name']];
        }
	// $donut_chart['grandtotal'] = abs(round($suppliers['grandtotal'],2));
	 echo json_encode($search_stock); exit;
}if(isset($_GET['search_dropdown']) && $_GET['search_dropdown'] == 'supplier_id'){

	$search_stock_id = kv_get_suppliers_list($_GET['q']);
	$search_stock = [];
	while ($r = db_fetch($search_stock_id)) {
            $search_stock[] = ['id' => $r['supplier_id'], 'text' => $r['supp_name']];
        }
	// $donut_chart['grandtotal'] = abs(round($suppliers['grandtotal'],2));
	 echo json_encode($search_stock); exit;
}

function kv_get_items_list($description, $type='sales'){
    global $SysPrefs;

    $sql = "SELECT COUNT(i.item_code) AS kit, i.item_code, i.description, c.description category
		FROM " . TB_PREF . "stock_master s, " . TB_PREF . "item_codes i
			LEFT JOIN " . TB_PREF . "stock_category c ON i.category_id=c.category_id
		WHERE i.stock_id=s.stock_id
			AND !i.inactive AND !s.inactive
			AND ( i.item_code LIKE " . db_escape("%" . $description . "%") . " OR 
				i.description LIKE " . db_escape("%" . $description . "%") . " OR 
				c.description LIKE " . db_escape("%" . $description . "%") . ") ";
				
	// if($_SESSION['wa_current_user']->item_category!='')
	//  		$sql .= " AND s.category_id IN(".$_SESSION['wa_current_user']->item_category.")";						


    switch ($type) {
        case "sales":
            $sql .= " AND !s.no_sale AND mb_flag <> 'F'";
            break;
        case "manufactured":
            $sql .= " AND mb_flag = 'M'";
            break;
        case "purchasable":
            $sql .= " AND NOT no_purchase AND mb_flag <> 'F' AND i.item_code=i.stock_id";
            break;
        case "costable":
            $sql .= " AND mb_flag <> 'D' AND mb_flag <> 'F' AND  i.item_code=i.stock_id";
            break;
        case "component":
            $parent = $_GET['parent'];
            $sql .= " AND  i.item_code=i.stock_id AND i.stock_id <> '$parent' AND mb_flag <> 'F' ";
            break;
        case "kits":
            $sql .= " AND !i.is_foreign AND i.item_code!=i.stock_id AND mb_flag <> 'F'";
            break;
        case "all":
            $sql .= " AND mb_flag <> 'F' AND i.item_code=i.stock_id";
            break;
    }

    if (isset($SysPrefs->max_rows_in_search))
        $limit = $SysPrefs->max_rows_in_search;
    else
        $limit = 10;

    $sql .= " GROUP BY i.category_id,i.item_code ORDER BY i.category_id,i.description LIMIT 0," . (int)($limit);
    
    return db_query($sql, "Failed in retreiving item list.");
}

function kv_get_customers_list($customer){
    global $SysPrefs;

    if (isset($SysPrefs->max_rows_in_search))
        $limit = $SysPrefs->max_rows_in_search;
    else
        $limit = 10;

    $sql = "SELECT debtor_no, name FROM ".TB_PREF."debtors_master 
	  WHERE (  name LIKE " . db_escape("%" . $customer. "%") . " OR 
    		 debtor_ref LIKE " . db_escape("%" . $customer. "%") . " OR 
	        address LIKE " . db_escape("%" . $customer. "%") . " OR 
    	     tax_id LIKE " . db_escape("%" . $customer. "%").")
	  ORDER BY name LIMIT 0,".(int)($limit);

    return db_query($sql, "Failed in retreiving customer list.");
}

function kv_get_suppliers_list($supplier){
    global $SysPrefs;

    if (isset($SysPrefs->max_rows_in_search))
        $limit = $SysPrefs->max_rows_in_search;
    else
        $limit = 10;

    $sql = "SELECT supplier_id , supp_name
		FROM ".TB_PREF."suppliers
		WHERE (supp_name LIKE " . db_escape("%" . $supplier. "%") . " OR 
			supp_ref LIKE " . db_escape("%" . $supplier. "%") . " OR 
			address LIKE " . db_escape("%" . $supplier. "%") . " OR 
			gst_no LIKE " . db_escape("%" . $supplier. "%") . ")
		ORDER BY supp_name LIMIT 0,".(int)($limit);

    return db_query($sql, "Failed in retreiving supplier list.");
}

function kv_get_coa_list($like, $skip=false){
    global $SysPrefs;

    if (isset($SysPrefs->max_rows_in_search))
        $limit = $SysPrefs->max_rows_in_search;
    else
        $limit = 10;

    if ($skip)
        $sql = "SELECT chart.account_code, chart.account_name
			FROM (".TB_PREF."chart_master chart,".TB_PREF."chart_types type) "
            ."LEFT JOIN ".TB_PREF."bank_accounts acc "
            ."ON chart.account_code=acc.account_code
				WHERE acc.account_code  IS NULL
			AND chart.account_type=type.id ";
    else
        $sql = "SELECT chart.account_code, chart.account_name
			FROM ".TB_PREF."chart_master chart,".TB_PREF."chart_types type WHERE chart.account_type=type.id ";
    $sql .= "AND (
          	chart.account_name LIKE " . db_escape("%" . $like. "%") . " OR
          	chart.account_code LIKE " . db_escape("%" . $like. "%") . "
        	) 
    		ORDER BY chart.account_code LIMIT 0,".(int)($limit); // We only display 10 items.
    return db_query($sql, "Failed in retreiving GL account list.");
}

?>