<?php
include_once('DB.php');
date_default_timezone_set("Asia/Kolkata");

function checkAllDocStatus($id){
	$bdd=new DB();
	$crow=$bdd->getOne("SELECT order_status,receive,fittment,fitness,permit,rcCopy,status FROM customer WHERE customer_id=".$_POST['id']);
	$bdd->disconnect();
	if($crow['order_status']=='1' && $crow['receive']=='1' && $crow['fittment']=='1' && $crow['fitness']!='' && $crow['permit']!='' && $crow['rcCopy']!='' && $crow['status']!='FullSuccessfull'){
		$bdd=new DB();
		$result=$bdd->execute("UPDATE customer SET status='FullSuccessfull' WHERE customer_id=".$_POST['id']);
		$bdd->disconnect();
	}
}

if($_POST['action']=='openOption')
{
	$id=$_POST['id'];
	include('xcrud/xcrud.php');
	echo Xcrud::load_css();
	$xcrud_op=Xcrud::get_instance();
	$xcrud_op->table('menu_option');
	$xcrud_op->where('menu_id',$id);
	$xcrud_op->relation('menu_id','menu','menu_id','menu_name');
	$xcrud_op->pass_default('menu_id',$id);
	$xcrud_op->columns('menu_id', true);
	$xcrud_op->unset_title();
	$xcrud_op->unset_print();
	$xcrud_op->unset_csv();
	$xcrud_op->unset_search();
	$xcrud_op->unset_limitlist();
	$xcrud_op->label('menu_id','Menu');
	$xcrud_op->readonly('menu_id');
	$xcrud_op->after_insert('update_menu_option_after_insert', 'functions.php');
	$xcrud_op->after_update('update_menu_option_after_update','functions.php');
	echo $xcrud_op->render();
	echo Xcrud::load_js();
}
if($_POST['action']=='openLocation')
{
	$IP = $_SERVER['SERVER_ADDR'];
	$id=$_POST['id'];
	$bdd=new DB();
	$row=$bdd->getOne('SELECT name,contact_no,date,chassis_no FROM customer WHERE customer_id='.$id);
	$bdd->disconnect();
	$year=date('Y',strtotime($row['date']));
	$month=date('m',strtotime($row['date']));
	//$path ='scanner\uploads\\'.$year.'\\'.$row['name'].'-'.$row['contact_no'];
	//exec('start \\\\192.168.0.21\\'.$path);
	//$path ='http://192.168.0.3:8080/scanner/uploads/'.$year.'\\'.$month.'\\'.$row['name'].'-'.$row['contact_no'].'-'.$row['chassis_no'];
	$path ='http://'.$IP.':8080/scanner/uploads/'.$year.'\\'.$month.'\\'.$row['name'].'-'.$row['contact_no'].'-'.$row['chassis_no'];
	echo $path;
}

if($_POST['action']=='openLocationPath')
{
	$id=$_POST['id'];
	$bdd=new DB();
	$row=$bdd->getOne('SELECT name,contact_no,date,chassis_no FROM customer WHERE customer_id='.$id);
	$bdd->disconnect();
	$year=date('Y',strtotime($row['date']));
	$month=date('m',strtotime($row['date']));
	$path="\\\\192.168.0.3\scanner\uploads\\".$year."\\".$month."\\".$row['name']."-".$row['contact_no'].'-'.$row['chassis_no'];
	echo $path;
}

if($_POST['action']=='search_customer_record')
{
	include ('xcrud/xcrud.php');
	echo Xcrud::load_css();
	$customer_name=$_POST['customer_name'];

	$xcrud_sc=Xcrud::get_instance();
	$xcrud_sc->table('customer');
	$xcrud_sc->relation('added_by','user','user_id','name');
	$xcrud_sc->where('',"`customer`.`name` LIKE '$customer_name%'");
	$xcrud_sc->unset_add();
	$xcrud_sc->unset_edit();
	$xcrud_sc->unset_remove();
	$xcrud_sc->unset_print();
	$xcrud_sc->unset_csv();
	$xcrud_sc->unset_title();
	$xcrud_sc->unset_search();
	//$xcrud_sc->unset_view();
	$xcrud_sc->unset_limitlist();
	echo $xcrud_sc->render();
	echo Xcrud::load_js();
}
if($_POST['action']=='search_customer_contactNO_record')
{
	include ('xcrud/xcrud.php');
	echo Xcrud::load_css();
	$contact_no=$_POST['contact_no'];

	$xcrud_sc=Xcrud::get_instance();
	$xcrud_sc->table('customer');
	$xcrud_sc->relation('added_by','user','user_id','name');
	$xcrud_sc->where('',"`customer`.`contact_no` LIKE '$contact_no%'");
	$xcrud_sc->unset_add();
	$xcrud_sc->unset_edit();
	$xcrud_sc->unset_remove();
	$xcrud_sc->unset_print();
	$xcrud_sc->unset_csv();
	$xcrud_sc->unset_title();
	$xcrud_sc->unset_search();
	//$xcrud_sc->unset_view();
	$xcrud_sc->unset_limitlist();
	echo $xcrud_sc->render();
	echo Xcrud::load_js();
}

if($_POST['action']=='search_customer_chassisNo_record')
{
	include ('xcrud/xcrud.php');
	echo Xcrud::load_css();
	$chassis_no=$_POST['chassis_no'];

	$xcrud_sc=Xcrud::get_instance();
	$xcrud_sc->table('customer');
	$xcrud_sc->relation('added_by','user','user_id','name');
	$xcrud_sc->where('',"`customer`.`chassis_no` LIKE '$chassis_no%'");
	$xcrud_sc->unset_add();
	$xcrud_sc->unset_edit();
	$xcrud_sc->unset_remove();
	$xcrud_sc->unset_print();
	$xcrud_sc->unset_csv();
	$xcrud_sc->unset_title();
	$xcrud_sc->unset_search();
	//$xcrud_sc->unset_view();
	$xcrud_sc->unset_limitlist();
	echo $xcrud_sc->render();
	echo Xcrud::load_js();
}

if($_POST['action']=='search_customer_registerNo_record')
{
	include ('xcrud/xcrud.php');
	echo Xcrud::load_css();
	$registration_no=$_POST['registration_no'];

	$xcrud_sc1=Xcrud::get_instance();
	$xcrud_sc1->table('customer');
	$xcrud_sc1->relation('added_by','user','user_id','name');
	$xcrud_sc1->where('',"`customer`.`registration_no` LIKE '$registration_no%'");
	$xcrud_sc1->unset_add();
	$xcrud_sc1->unset_edit();
	$xcrud_sc1->unset_remove();
	$xcrud_sc1->unset_print();
	$xcrud_sc1->unset_csv();
	$xcrud_sc1->unset_title();
	$xcrud_sc1->unset_search();
	//$xcrud_sc->unset_view();
	$xcrud_sc1->unset_limitlist();
	echo $xcrud_sc1->render();
	echo Xcrud::load_js();
}

if($_POST['action']=='checkStatus'){
	$id=$_POST['id'];
	$bdd=new DB();
	$mrow=$bdd->getOne("SELECT count(menu_id) as m FROM menu WHERE visibility='Yes' and compulsory='yes'");
	$bdd->disconnect();
	$bdd=new DB();
	$cmrow=$bdd->getOne("SELECT count(customer_menu_id) as cm FROM customer_menu WHERE customer_id=".$id);
	$bdd->disconnect();
	if($mrow['m']>$cmrow['cm']){
		echo 0;
	}else{
		echo 1;
	}
}
if($_POST['action']=='updateCustInfo'){
	$bdd=new DB();
	$crow=$bdd->getOne("SELECT name,contact_no,chassis_no FROM customer WHERE customer_id=".$_POST['id']);
	$bdd->disconnect();
	echo json_encode($crow);
}

if ($_POST['action'] == 'updateRegistrationNo') {
    $bdd = new DB();
    $crow = $bdd->getOne("SELECT name FROM customer WHERE customer_id=" . $_POST['id']);
    $bdd->disconnect();

    $bdd = new DB();
    $row = $bdd->getOne('SELECT name, contact_no, date, chassis_no FROM customer WHERE customer_id=' . $_POST['id']);
    $bdd->disconnect();
    $year = date('Y', strtotime($row['date']));
    $month = date('m', strtotime($row['date']));
    $path = 'uploads/' . $year . '/' . $month . '/' . $row['name'] . '-' . $row['contact_no'] . '-' . $row['chassis_no'];

    $data = array();
	
	$data['permitpdf'] = 'nothing';
	$data['permitjpg'] = 'nothing';
	
	$data['fitnesspdf'] = 'nothing';
	$data['fitnessjpg'] = 'nothing';

    if (file_exists($path . '/Permit.pdf')) {
        $data['permitpdf'] = $path . '/Permit.pdf';
    }
	elseif (file_exists($path . '/Permit.jpg')) {
        $data['permitjpg'] = $path . '/Permit.jpg';
    } 

    if (file_exists($path . '/Fitness.pdf')) {
        $data['fitnesspdf'] = $path . '/Fitness.pdf';
    } 
	elseif (file_exists($path . '/Fitness.jpg')) {
        $data['fitnessjpg'] = $path . '/Fitness.jpg';
    }

    $data['name'] = $crow['name'];
	$data['id'] = $_POST['id'];

    echo json_encode($data);
}


if($_POST['action']=='changeOrderStatus'){
	$data=array();
	$bdd=new DB();
	$crow=$bdd->getOne("SELECT order_status FROM customer WHERE customer_id=".$_POST['id']);
	$bdd->disconnect();

	if($crow['order_status']=='0'){
		$td=date('Y-m-d');
		$bdd=new DB();
		$result=$bdd->execute("UPDATE customer SET order_status='1', order_date='$td' WHERE customer_id=".$_POST['id']);
		$bdd->disconnect();
		if($result){
			checkAllDocStatus($_POST['id']);
			$data['status']='ok';
			$data['message']='Order Updated';
		}else{
			$data['status']='error';
			$data['message']='Error. Try Again';
		}
	}else{
		$data['status']='error';
		$data['message']='Order is Already Updated';
	}
	
	echo json_encode($data);
}

if($_POST['action']=='changeReceiveStatus'){
	$data=array();
	$bdd=new DB();
	$crow=$bdd->getOne("SELECT receive FROM customer WHERE customer_id=".$_POST['id']);
	$bdd->disconnect();

	if($crow['receive']=='0'){
		$td=date('Y-m-d');
		$bdd=new DB();
		$result=$bdd->execute("UPDATE customer SET receive='1', receive_date='$td', status='Fitment' WHERE customer_id=".$_POST['id']);
		$bdd->disconnect();
		if($result){
			checkAllDocStatus($_POST['id']);
			$data['status']='ok';
			$data['message']='Receive Updated';
		}else{
			$data['status']='error';
			$data['message']='Error. Try Again';
		}
	}else{
		$data['status']='error';
		$data['message']='Receive is Already Updated';
	}
	
	echo json_encode($data);
}

if($_POST['action']=='changeFittmentStatus'){
	$data=array();
	$bdd=new DB();
	$crow=$bdd->getOne("SELECT fittment FROM customer WHERE customer_id=".$_POST['id']);
	$bdd->disconnect();

	if($crow['fittment']=='0'){
		$td=date('Y-m-d');
		$bdd=new DB();
		$result=$bdd->execute("UPDATE customer SET fittment='1', fittment_date='$td', status='Register' WHERE customer_id=".$_POST['id']);
		$bdd->disconnect();
		if($result){
			checkAllDocStatus($_POST['id']);
			$data['status']='ok';
			$data['message']='Fittment Updated';
		}else{
			$data['status']='error';
			$data['message']='Error. Try Again';
		}
	}else{
		$data['status']='error';
		$data['message']='Fittment is Already Updated';
	}
	
	echo json_encode($data);
}

if($_POST['action']=='userPasswordConfrm'){
	$data=array();
	
	$bdd=new DB();
	$crow=$bdd->getOne("SELECT password FROM password WHERE password='".$_POST['password']."'");
	$bdd->disconnect();
	
	if(!empty($crow)){
		$data['status']='ok';
	}else{
		$data['status']='error';
	}
	
	echo json_encode($data);
}

/*if($_POST['action']=='cvs_password')
{
	$csv_pass=$_POST['csv_pass'];
	$bdd=new DB();
	$prow=$bdd->getOne("select export from password");
	$bdd->disconnect();
	if($csv_pass==$prow['export']){
		echo 1;
	}
	else{
		echo 0;
	}
}*/
?>