<?php
require_once("../config/dbconnect.php");
$today = date('Y-m-d');
session_start();
//////////////////  Get city by selecting state	dropdown
if ($_POST['circle']) {
    echo "<select name='locationstate' id='locationstate' class='form-control required' onchange='get_citydiv();' required><option value=''>--Please Select--</option>";
    $state_query = "select distinct(state) from state_master where zone='" . $_POST['circle'] . "' order by state";
    $state_res = mysqli_query($link1, $state_query);
    while ($row_res = mysqli_fetch_array($state_res)) {
        echo "<option value='" . $row_res['state'] . "'>";
        echo $row_res['state'] . "</option>";
    }
    echo "</select>";
}
//////////////////  Get cgst,igst, sgst per///////////////////
if ($_POST['productinfo']) {
    $prodname = mysqli_fetch_assoc(mysqli_query($link1, "select productname,hsn_code from product_master where productcode='" . $_POST['productinfo'] . "'"));	
	$gst = mysqli_fetch_assoc(mysqli_query($link1, "select sgst,cgst,igst from tax_hsn_master where hsn_code='" . $prodname['hsn_code'] . "'"));
 $stk_query = "SELECT price,mrp from price_master where product_code='" . $_POST['productinfo'] . "' and state='" . $_POST['fromstate'] . "' and location_type='" .$_POST['idtype'] . "'";
    $stk_res = mysqli_query($link1, $stk_query);
    $stk_row = mysqli_fetch_array($stk_res);
	if ($stk_row['price']) {
        $price = $stk_row['price'] ;
    } else {
        $price = "0.00";
    }      

   //////  reward point logic ////////////////////////////////////////////
	    $reward_det = mysqli_query($link1 , " select reward_point , reward_startDate , reward_endDate from product_master where productcode = '".$_POST['productinfo']."' ");
											if(mysqli_num_rows($reward_det)>0){
											 $res_data  = mysqli_fetch_array($reward_det);
											 if($res_data['reward_endDate'] >= $today){
											     $str = 'Y';
												 $msg = "You earned Reward Point";
												 $msg2 = "for product";
												 $response .= "<tr><td>" .$msg." ".$res_data['reward_point']." ".$msg2." ".$prodDescrip. "</td></tr>";
											   }
											
											 }
											 else {
											    $str = 'N';
												$response = '';
											   }

      echo  $gst['sgst'] . "~" . $gst['cgst'] . "~" . $gst['igst']. "~".$price."~".$str."~".$res_data['reward_point']."~".$response;
}

//////////////////  Get city by selecting state	dropdown
if ($_POST['state']) {
    echo "<select  name='locationcity' id='locationcity' class='form-control required' required><option value=''>--Please Select--</option>";
    $city_query = "SELECT distinct city FROM district_master where state='" . $_POST['state'] . "' order by city";
    $city_res = mysqli_query($link1, $city_query);
    while ($row_city = mysqli_fetch_array($city_res)) {
        echo "<option value='" . $row_city['city'] . "'>";
        echo $row_city['city'] . "</option>";
    }
    echo "<option value='Others'>Others</option>";
    echo "</select>";
}
//////////////////  Get Parent Location by selecting location type dropdown
if ($_POST['loctype']) {
    echo "<select  name='parentid' id='parentid' class='form-control required' required><option value=''>--Please Select--</option>";
    $parent_query = "SELECT uid,name,city,state FROM asc_master where user_level<'" . $_POST['loctype'] . "' order by id_type,name";
    $parent_res = mysqli_query($link1, $parent_query);
    while ($row_parent = mysqli_fetch_array($parent_res)) {
        echo "<option value='" . $row_parent[uid] . "'>";
        echo $row_parent['name'] . "," . $row_parent['city'] . "," . $row_parent['state'] . "</option>";
    }
    echo "<option value='NONE'>NONE</option>";
    //if($_POST['loctype']==1){ echo "<option value='NONE'>NONE</option>";}
    echo "</select>";
}
//////////////////  Get available stock of product for selected location
if ($_POST['locstk']) {
    $stk_query = "SELECT " . $_POST['stktype'] . " FROM stock_status where asc_code='" . $_POST['loccode'] . "' and partcode='" . $_POST['locstk'] . "'";
    $stk_res = mysqli_query($link1, $stk_query);
    $prodname = mysqli_fetch_assoc(mysqli_query($link1, "select productname,hsn_code from product_master where productcode='" . $_POST['locstk'] . "'"));
    $loc_state = mysqli_fetch_assoc(mysqli_query($link1, "select state from asc_master where asc_code='" . $_POST['loccode'] . "'"));
    $ven_state = mysqli_fetch_assoc(mysqli_query($link1, "select state from vendor_master where id='" . $_POST['vendorCode'] . "'"));
    $gst = mysqli_fetch_assoc(mysqli_query($link1, "select sgst,cgst,igst from tax_hsn_master where hsn_code='" . $prodname['hsn_code'] . "'"));
    $stk_row = mysqli_fetch_array($stk_res);
    if( $loc_state['state'] == $ven_state['state']){
        $sgst_per = $gst['sgst'];
        $cgst_per = $gst['cgst'];
        $igst_per = '0';
    }else{
        $sgst_per = 0;
        $cgst_per = 0;
        $igst_per = $gst['igst'];
    }
    echo $stk_row[0] . "~" . $_POST['indxx']."~" . $sgst_per . "~" . $cgst_per . "~" . $igst_per."~" .$_POST['qty']."~" .$_POST['locnindex']."~".$_POST['prodtindex'];
}

//////////////////  Get available stock of product for selected location
if ($_POST['locstknew']) {
    $stk_query = "SELECT " . $_POST['stktype'] . " FROM stock_status where asc_code='" . $_POST['fromloc'] . "' and partcode='" . $_POST['locstknew'] . "'";
    $stk_res = mysqli_query($link1, $stk_query);
    $prodname = mysqli_fetch_assoc(mysqli_query($link1, "select productname,hsn_code from product_master where productcode='" . $_POST['locstknew'] . "'"));
    $loc_state = mysqli_fetch_assoc(mysqli_query($link1, "select state from asc_master where asc_code='" . $_POST['loccode'] . "'"));
    $ven_state = mysqli_fetch_assoc(mysqli_query($link1, "select state from asc_master where asc_code='" . $_POST['fromloc'] . "'"));
    $gst = mysqli_fetch_assoc(mysqli_query($link1, "select sgst,cgst,igst from tax_hsn_master where hsn_code='" . $prodname['hsn_code'] . "'"));
    $stk_row = mysqli_fetch_array($stk_res);
    if( $loc_state['state'] == $ven_state['state']){
        $sgst_per = $gst['sgst'];
        $cgst_per = $gst['cgst'];
        $igst_per = '0';
    }else{
        $sgst_per = 0;
        $cgst_per = 0;
        $igst_per = $gst['igst'];
    }
    echo $stk_row[0] . "~" . $_POST['indxx']."~" . $sgst_per . "~" . $cgst_per . "~" . $igst_per;
}



//////////////////  Check duplicate document code for selected location
if ($_POST['doccode']) {
    $doccode_query = "SELECT id from document_counter where financial_year='" . $_POST['fcyear'] . "' and doc_code='" . strtoupper($_POST['doccode']) . "'";
    $doccode_res = mysqli_query($link1, $doccode_query);
    $doccode_row = mysqli_num_rows($doccode_res);
    echo $doccode_row;
}
//////////////////  Check duplicate Product code
if ($_POST['pcode']) {
    $prodcode_query = "SELECT id from product_master where productcode='" . $_POST['pcode'] . "'";
    $prodcode_res = mysqli_query($link1, $prodcode_query);
    $prodcode_row = mysqli_num_rows($prodcode_res);
    echo $prodcode_row;
}
//////////////////  Get price of product 
if ($_POST['product']) {
    $stk_query = "SELECT price,mrp from price_master where product_code='" . $_POST['product'] . "' and state='" . $_POST['locstate'] . "' and location_type='" . $_POST['lctype'] . "'";
    $stk_res = mysqli_query($link1, $stk_query);
    $stk_row = mysqli_fetch_array($stk_res);
    if ($stk_row['price']) {
        echo $stk_row['price'] . "~" . $stk_row['mrp'];
    } else {
        echo "0.00~0.00";
    }
}
//////////////////  Get product details by searching IMEI 
if ($_POST['prodimei']) {
    //// check imei is in stock or not
  $imei_query = "SELECT owner_code,prod_code from billing_imei_data where imei1='" . $_POST['prodimei'] . "' or imei2='" . $_POST['prodimei'] . "' order by id desc";
    $imei_res = mysqli_query($link1, $imei_query);
	$imei_row = mysqli_fetch_array($imei_res);
  if( $_POST['loccode'] == rtrim($imei_row['owner_code'], " " )){
       $stkflag = "Y";
       $prodname = mysqli_fetch_assoc(mysqli_query($link1, "select productname,hsn_code from product_master where productcode='" . $imei_row['prod_code'] . "'"));
       $prodDescrip = $prodname['productname'];
    } else {
       $stkflag = "N";
       $prodDescrip = "Not in stock";
    }
    ////// check price of product
    if ($imei_row['prod_code']) {
        $stk_query = "SELECT price,mrp from price_master where product_code='" . $imei_row['prod_code'] . "' and state='" . $_POST['locstate'] . "' and location_type='" . $_POST['lctype'] . "'";
        $stk_res = mysqli_query($link1, $stk_query);
        $stk_row = mysqli_fetch_array($stk_res);
        if ($stk_row['price']) {
            $pricflag = $stk_row['price'] . "~" . $stk_row['mrp'];
        } else {
            $pricflag = "0.00~0.00";
        }
    } else {
        $pricflag = "0.00~0.00";
    }

   if($prodDescrip) {
	//////  reward point logic ////////////////////////////////////////////
	    $reward_det = mysqli_query($link1 , " select reward_point , reward_startDate , reward_endDate from product_master where productcode = '".$imei_row['prod_code']."' ");
											if(mysqli_num_rows($reward_det)>0){
											 $res_data  = mysqli_fetch_array($reward_det);
											 if($res_data['reward_endDate'] >= $today){
											     $str = 'Y';
												 $msg = "You earned Reward Point";
												 $msg2 = "for product";
												 $response .= "<tr><td>" .$msg." ".$res_data['reward_point']." ".$msg2." ".$prodDescrip. "</td></tr>";
											   }
											
											 }
											 else {
											    $str = 'N';
												$response = '';
											   }
	
	 }
	
          ///////check coupon code is available  on product or not /////////////////////////////
						       $product_det = mysqli_fetch_array(mysqli_query($link1 , "select id , productcategory, productsubcat from product_master where productcode = '".$imei_row['prod_code']."' "));      
							  
		                         ///// check coupon code is available on productid of product/////////////////////////////
							    $coupon_det = mysqli_query($link1 , "select coupon_code from coupon_mapping where  status = 'Y' and  find_in_set('".$product_det['id']."',productid) <> 0     ");	
			                     if(mysqli_num_rows($coupon_det) >0)  {
								
								  $coupon_str = '';
								   while($res = mysqli_fetch_array($coupon_det))
								   {
								     if($coupon_str == ''){
								    $coupon_str.= $res['coupon_code'];
									 }
									 else {
									 $coupon_str.="','".$res['coupon_code'];
									   }
								    }
								   }
								   else {
									  //////  check coupon code availabel on product subcategory 
									   $coupon_subcatdet = mysqli_query($link1 , " select coupon_code from coupon_mapping where  status = 'Y' and  find_in_set('".$product_det['productsubcat']."',prod_subcat) <> 0   ");	
									    if(mysqli_num_rows($coupon_subcatdet) >0){
										$coupon_str = '';
										   while($res = mysqli_fetch_array($coupon_subcatdet))
										   {
											 if($coupon_str == ''){
											 $coupon_str.= $res['coupon_code'];
											 }
											 else {
											 $coupon_str.="','".$res['coupon_code'];
											   }
											}
										}
										else {
										 $coupon_prodcatdet = mysqli_query($link1 , " select coupon_code from coupon_mapping where  status = 'Y' and find_in_set('".$product_det['productcategory']."',prod_cat) <> 0   ");
										
										 if(mysqli_num_rows($coupon_prodcatdet) >0){
										 $coupon_str = '';
										   while($res = mysqli_fetch_array($coupon_prodcatdet))
										   {
											 if($coupon_str == ''){
											 $coupon_str.= $res['coupon_code'];
											 }
											 else {
											 $coupon_str.="','".$res['coupon_code'];
											   }
											}
										
										    }		
										}
									}	
									 	 
	    ///////  check validity ////////////////////////////////////////////////////////////////////

		$coupon_data = mysqli_fetch_array(mysqli_query($link1 , "select coupon_code , max(amount) as amt from coupon_master  where valid_to >= $today and  coupon_code in ( '$coupon_str')  and  amount = (select max(amount) from coupon_master where coupon_code in (  '$coupon_str') )"));
		if($coupon_data['coupon_code'] != ''){
		 $coupon_check = $coupon_data['coupon_code'];
		 $coupon_amt = $coupon_data['amt'];
		 }else {
		 $coupon_check = '';	
		 $coupon_amt = '';	 
		  }

    $state = mysqli_fetch_assoc(mysqli_query($link1, "select state from asc_master where asc_code='" . $_POST['loccode'] . "'"));
    $gst = mysqli_fetch_assoc(mysqli_query($link1, "select sgst,cgst,igst from tax_hsn_master where hsn_code='" . $prodname['hsn_code'] . "'"));
    echo $pricflag . "~" . $stkflag . "~" . $imei_row['prod_code'] . "~" . $prodDescrip . "~" . $gst['sgst'] . "~" . $gst['cgst'] . "~" . $gst['igst'] . "~" . $state['state']."~".$str."~".$res_data['reward_point']."~".$response."~".$coupon_check."~".$coupon_amt;
	}
//////////////////  check Serial no. is available or not on 17 dec 2019 by shekhar
if ($_POST['checkSerialIsAvl']) {
	//// check imei is in stock or not
  	$imei_query = "SELECT owner_code,prod_code from billing_imei_data where imei1='" . $_POST['checkSerialIsAvl'] . "' or imei2='" . $_POST['checkSerialIsAvl'] . "' order by id desc";
    $imei_res = mysqli_query($link1, $imei_query);
	$imei_row = mysqli_fetch_array($imei_res);
  	if( $_POST['loccode'] == rtrim($imei_row['owner_code'], " " )){
    	$stkflag = "Y";
       	$prodname = mysqli_fetch_assoc(mysqli_query($link1, "select productname,hsn_code from product_master where productcode='" . $imei_row['prod_code'] . "'"));
       	$prodDescrip = $prodname['productname'];
    } else {
       	$stkflag = "N";
       	$prodDescrip = "Not in stock";
    }
	echo $stkflag . "~" . $imei_row['prod_code'] . "~" . $prodDescrip;
}
/// get product specification devlop by shekhar on 26 march 2019
if($_POST["prodSpecif"]){
	//// get product id behalf of product code
	$prdid = mysqli_fetch_assoc(mysqli_query($link1,"SELECT id,productcategory,warranty_days FROM product_master where productcode='".$_POST["prodSpecif"]."'"));
	$response = "<table class=table table-bordered width=100%><thead><tr><th>Specification</th><th>Description</th></tr></thead><tbody>";
	$sql = "SELECT parameter_id,parameter_details FROM pr_specification where product_id='".$prdid["id"]."'";
	$res = mysqli_query($link1, $sql);
	if(mysqli_num_rows($res)>0){
		while($row = mysqli_fetch_array($res)){
			///// get parameter name
			$pmn = mysqli_fetch_assoc(mysqli_query($link1,"SELECT parameter_name FROM pr_parameter_master where parameter_id='".$row["parameter_id"]."'"));
			$response .= "<tr><td>".$pmn["parameter_name"]."</td><td>".$row['parameter_details']."</td></tr>";
		}
	}else{
		$response .= "<tr><td colspan=2>No specification found for selected product</td></tr>";
	}
	$response .= "</tbody></table>";
	echo $response."~".$_POST['indxx']."~".$prdid["productcategory"]."~".$prdid["warranty_days"];
}
/// get product delivery date devlop by shekhar on 26 march 2019
if($_POST["prodDelivdate"]){
	$sql = "SELECT delivery_days FROM product_delivery_matrix where from_location='".$_POST["fromloc"]."' and to_location='".$_POST["toloc"]."' and productcategory='".$_POST["prodDelivdate"]."'";
	$res = mysqli_query($link1, $sql);
	if(mysqli_num_rows($res)>0){
		$row = mysqli_fetch_array($res);
		$deliv_date = date('Y-m-d', strtotime(date("Y-m-d"). ' + '.$row["delivery_days"].' days'));
	}else{
		$deliv_date = "";
	}
	echo $deliv_date."~".$_POST['indxx'];
}
///// check scanned IMEI is belong to requested location stock or not
///// developed by shekhar on 22 april 2019
if($_POST["scanimei"]){
	//echo "SELECT id,owner_code,prod_code,imei1 FROM billing_imei_data WHERE imei1 = '".$_POST["scanimei"]."' order by id desc".$_POST['reqmodel'];
	$res_imei = mysqli_query($link1,"SELECT id FROM billing_imei_data WHERE imei1 = '".$_POST["scanimei"]."'");
	if(mysqli_num_rows($res_imei)>0){
		echo "Serial nos. is already in stock~".$_POST["indx"]."~danger~fa-ban";
	}else{
		echo "Correct~".$_POST["indx"]."~success~fa-check-circle";
	}
}
if($_POST["scanimeisale"]){
	//echo "SELECT id,owner_code,prod_code,imei1 FROM billing_imei_data WHERE imei1 = '".$_POST["scanimei"]."' order by id desc".$_POST['reqowner'];
	$res_imei = mysqli_query($link1,"SELECT id,owner_code,prod_code,imei1 FROM billing_imei_data WHERE imei1 = '".$_POST["scanimeisale"]."' order by id desc");
	if(mysqli_num_rows($res_imei)>0){
		$row_imei = mysqli_fetch_assoc($res_imei);
		///// check IMEI owner
		//echo $_POST['reqowner']." == ".$row_imei["owner_code"]."<br><br>";
		if($_POST['reqowner'] == $row_imei["owner_code"]){
			///// check IMEI belongs to requested model
			if($_POST['reqmodel'] == $row_imei["prod_code"]){
				echo "Correct~".$_POST["indx"]."~success~fa-check-circle";
			}else{
				echo "Wrong IMEI picked~".$_POST["indx"]."~warning~fa-exclamation";	
			}
		}else{
			echo "Not in stock~".$_POST["indx"]."~danger~fa-ban";
		}
	}else{
		echo "Not found in DB~".$_POST["indx"]."~danger~fa-ban";
	}
}
///// find the scheme benifite /////////////
if($_POST["schemeInfo"]){
	$foc_flag = 0;
	/////// Select Scheme details ////////
	$sql_schm = "SELECT scheme_based_on, scheme_given_type, scheme_given, scheme_based_type FROM scheme_master where   scheme_code = '".$_POST['schemeApplicable']."'   "; 
	$res_schm = mysqli_fetch_array(mysqli_query($link1,$sql_schm));
	
	if($res_schm['scheme_given_type'] == "Discount Amount"){
		$totdisc = $res_schm['scheme_given'];
		$foc_flag = 0;
	}else if($res_schm['scheme_given_type'] == "Discount Percentage"){
		$totdisc = (($_POST["schemeInfo"] * $res_schm['scheme_given'])/100);
		$foc_flag = 0;
	}else if($res_schm['scheme_given_type'] == "FOC Qty"){
		$totdisc = $res_schm['scheme_given'];
		$foc_flag = 1;
	}else{}
	if($foc_flag == 1){ $tot_disc = (int)$totdisc; }else{ $tot_disc = $totdisc; }
	echo $tot_disc."~".$foc_flag."~".$_POST['schemeInfo']."~".$_POST['valINDNo']."~".$_POST['valPrd']."~".$_POST['prdDtlVal']."~".$_POST['schemeApplicable'];
}
//////////////////  Get scheme	dropdown
if ($_POST['sbt']) {
	echo "<select name='scheme_given_type' id='scheme_given_type' class='form-control required' required onchange='show_INput(this.value);'><option value=''> -- Please Select -- </option>";
	if($_POST['sbt'] == "Total Amount"){
		echo "<option value='Discount Amount'>Discount Amount</option><option value='Discount Percentage'>Discount Percentage</option>";
	}else{
		echo "<option value='FOC Qty'>FOC Qty</option>";
	}
	echo "</select>";
}

///// check coupon code validity //////////////////////////////////////////////////////////////
if($_POST["checkCouponIsAvl"]){
	///////check coupon code is available  on product or not /////////////////////////////
						       $product_det = mysqli_fetch_array(mysqli_query($link1 , "select id , productcategory, productsubcat from product_master where productcode = '".$_POST['productcode']."' "));      
		                         ///// check coupon code is available on productid of product/////////////////////////////	 
							$coupon_det =    mysqli_query($link1 , "select coupon_code from coupon_mapping where coupon_code = '".$_POST["checkCouponIsAvl"]."' and status = 'Y' and   find_in_set('".$product_det['id']."',productid) <> 0      ");	
							if(mysqli_num_rows($coupon_det) >0){
							  $check_flag = 'Y';
							  }else {
							  $check_flag = '';
							  }
					
							
									  //////  check coupon code availabel on product subcategory 
									   $coupon_subcatdet =mysqli_query($link1 , " select coupon_code from coupon_mapping where  coupon_code = '".$_POST["checkCouponIsAvl"]."' and status = 'Y' and  find_in_set('".$product_det['productsubcat']."',prod_subcat) <> 0  ");	
									    if(mysqli_num_rows($coupon_subcatdet)> 0){
										 $check_flagsub = 'Y';
										  }
										  else {
										  $check_flagsub = '';
										  }
										  //// check coupon_code available on product category ////////////////
										   $coupon_prodcatdet = mysqli_query($link1 , " select coupon_code from coupon_mapping where coupon_code = '".$_POST["checkCouponIsAvl"]."' and status = 'Y' and   find_in_set('".$product_det['productcategory']."',prod_cat) <> 0   ");
										     if(mysqli_num_rows($coupon_prodcatdet) >0){
											 $check_flagprod = 'Y';
											 }
											 else {
											   $check_flagprod = '';
											 
											 }	
		if($check_flagprod == 'Y' || $check_flagsub == 'Y' || $check_flag == 'Y') {							
	    ///////  check validity ////////////////////////////////////////////////////////////////////
		$coupon_data = mysqli_fetch_array(mysqli_query($link1 , "select coupon_code , amount from coupon_master  where valid_to >= $today and  coupon_code = '".$_POST["checkCouponIsAvl"]."' "));
		if($coupon_data['coupon_code'] != ''){
		 $coupon_check = $coupon_data['coupon_code'];
		 $coupon_amt = $coupon_data['amount'];
		 }else {
		 $coupon_check = '';	
		 $coupon_amt = '';	 
		  }
		 } 
	 echo $coupon_check."~".$coupon_amt."~".$_POST['indexval'];
}



?>