﻿<?php

    require_once('../../../config/config.php');
require_once (dirname(__FILE__).'/../../../clean_input.php');
	check_input();

	include('../../../common_fun.php');
	
include("../../common_pages/common_functions.php");
		//print_r($_REQUEST);
		
		if(isset($_REQUEST['task_new']))
		{
			$a = array();
			$i = 1;
			
			while($i<=10)
			{
				$n = split('#',$_REQUEST['a'.$i]);
				if($n[0]>0)
				{
					
					$a[$i]['qty'] = $n[0];
					$a[$i]['price'] = $n[1];
					 
				}
				$i++;
				
			}
			$_SESSION['a'] = $a;
			$data = find_offer_free2();
			$_SESSION['a'] = array();
			echo  $data['offer'].'||'.$data['amount'];
		}
		else
		{
			if(isset($_POST['queryString'])) {
	
				$queryString = ($_POST['queryString']);
	
				
				if(strlen($queryString) >0) {
	
								// $s = "update product SET product_sold_out='0'  ";
							  //mysql_query($s);
					 
					
					  $sql = "SELECT p.*,dt.title as d_name,mt.name as m_name ,gt.title as g_name,p.product_id FROM product p 
			
					INNER JOIN  diamond_type dt ON product_sold_out=0 AND (product_name LIKE '%".$queryString."%' or product_ccode LIKE '%".$queryString."%' or certificate_number  LIKE '%".$queryString."%')  AND dt.code=diamond_type_default
						INNER JOIN metal_type mt ON mt.code=metal_type_default
								LEFT JOIN gems_type gt ON gt.code=gems_type_default
					
					
					 		WHERE  not certificate_number=''  and  product_sale_inside=1 and is_stock=1 and product_sold_out=0 and p.is_deleted =0   ORDER BY product_name ";
							
							
							 
					$query = mysql_query($sql);
					
					if($query) {
	
					echo '<ul>';
	
						while ($result = mysql_fetch_assoc($query)) {
							$rows = $result;
							$a1 = daily_diamond_rate($result['diamond_type_default']);
							$a=$a1*$result['diamond_total_weight'];
							if($rows['diamond_discount_percentage']>0)
							{
				
									$a_before_discount = $a;
									$da= round(($a*$rows['diamond_discount_percentage'])/100,2); 
									$a = $a - $da;
							}
							
							
							
							$b1 = daily_metal_rate($result['metal_type_default']);
							$b=$b1*$result['metal_weight'];
							if($rows['metal_discount_percentage']>0){								 
								$b_before_discount = $b;
								$da= round(($b*$rows['metal_discount_percentage'])/100,2); 
								$b = $b - $da;
							}	 
							
							
							
							$c=gem_rate($result['product_id'],$result['gems_type_default']);
							if($rows['stone_discount_percentage']>0)
							{
				 				$c_before_discount = $c;
								$da= round(($c*$rows['stone_discount_percentage'])/100,2); 
								$c = $c - $da;
								
							}	
							
							
							if($result['making_charges']>0)
								$d=$result['making_charges'];
							else
								$d=($result['metal_weight']*900); //$d=($result['metal_weight']* (MAKING_CHARGES >0 ? MAKING_CHARGES : 1100));
							
							
							if($rows['making_charges_discount_percentage']>0){
							  
							  $d_before_discount = $d;
							  	 $da= round(($d*$rows['making_charges_discount_percentage'])/100,2); 
									echo $d = $d - $da;
							  }
				  
				  
							$text = 'Certificate Number : '.$result['certificate_number'].'<BR/>';
							$text.= $result['d_name']." @ ".$a1.' X '.$result['diamond_total_weight'].'ct =  ';
							if($a_before_discount>0)
							$text.='<strike>'.number_format($a_before_discount,2).'</strike>&nbsp;';	
							$text.=number_format($a,2);
							$text.= '<BR/>'.$result['m_name']." @ ".$b1.' X '.$result['metal_weight'].'gm =  ';
							if($b_before_discount>0)
								$text.='<strike>'.number_format($b_before_discount,2).'</strike>&nbsp;';	
							$text.=number_format($b,2);
							if($c>0)
							{	
								$text.= '<BR/>'.$result['g_name']." of size ".$result['gems_size'].'mm =  ';
								if($c_before_discount>0)
									$text.='<strike>'.number_format($c_before_discount,2).'</strike>&nbsp;';	
								$text.=number_format($c,2);
							
							}
								
							
							
							$text.= '<BR/>Making charges = ';
							if($d_before_discount>0)
								$text.='<strike>'.number_format($d_before_discount,2).'</strike>&nbsp;';	
							$text.=number_format($d,2);
							
							
							
							$t = ($a+$b+$c+$d); 
							
							
							 $t2 = $t;
							
							$tax = round(($t*3)/100,2);
					
							$tax2 = round(($t2*3)/100,2);
							
							$total_bd = round($t + $tax);
							$total = round($t2 + $tax2);
							
							
							
						 	$xx = '';
							$text.= '<BR/>GST (3%) =  '.number_format($tax2,2);
							$text.= '<BR/>-----------------------------------------------------------';
							if($result['discount_percantage']>0)
							{	$text.= '<BR/><b>Total Payable =  <strike>'.number_format($total_bd,2).'</strike>&nbsp;&nbsp;'.number_format($total,2);
								$xx = '<strike>'.number_format($total_bd,2).'</strike>&nbsp;&nbsp;';
							}else
								$text.= '<BR/><b>Total Payable =  '.number_format($total,2);
							$text.= '<BR/>-----------------------------------------------------------';
							
							
							
							$result['our_price'] = $total;
							$result['mrp'] = $total;
							 
						    $price = $total;
							
							$result['product_name'] = $result['product_ccode'].' - '.$result['product_name'].' [ D:'.$result['diamond_total_weight'].'ct, '.'G:'.$result['metal_weight'].'gm]';
	
							echo '<li onClick="fill_product(\''.$_POST[k2].'\',\''.$result['certificate_number'].' - '.addslashes($result['product_name']).'###'.$result['tax_p'].'###'.($result['product_id']).'###'.($price).'###'.($result['mrp']).'###'.($tax2).'###'.(addslashes($text)).'\');">'.$result['certificate_number'].' - '.$result['product_name'].'<BR/>'.$xx.'('.$total.')</li>';
	
						}
	
					echo '</ul>';
	
						
	
					} else {
	
						echo 'OOPS we had a problem :(';
	
					}
	
				} else {
	
					// do nothing
	
				}
	
			} else {
	
				echo 'There should be no direct access to this script!';
	
			}
		}
		
		
		
	

?>