<?php session_start();
require_once('../../../config/config.php');
require_once (dirname(__FILE__).'/../../../clean_input.php');
	check_input(); 
include("../../common_pages/paging_crm.php");
include("../../common_pages/common_functions.php");
require_once('constants.php');
refresh_access();
if($_SESSION['admin_code']<=0)
	die("Error : Seesion timeout, Please re Login");
if($_SESSION['is_panel_access']<=0)	
	die("Error : Access Denied");
require_once("../../common_pages/enc.php");
$converter = new Encryption($_SESSION['unique_enc_id']);
?>
<?php 

function find_ID_ran($p=1)
	
	
	{
		    $n = '8'.random_number99("7");
			$qr9="select code FROM mobile_confirm_sale where auth_code='".$n."'";
			$re9=mysql_query($qr9);
			if(mysql_num_rows($re9) > 0) 
			{	
				$rows = mysql_fetch_assoc($re9);
				 find_ID_ran($p);					
			}
			return $n;			
			
	}
	function random_number99($length)
	{
		$random= "";
		srand((double)microtime()*1000000);
		$data = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
		//$data .= "0FGH45OP89";
		for($i = 0; $i < $length; $i++)
		{
			$random .= substr($data, (rand()%(strlen($data))), 1);
		}
		return $random;
	}
	
	
if (isset($_REQUEST))

{
	 
		
	// print_r($_REQUEST);
	switch($_REQUEST['task'])
	{
		case 'jbc':
		{
			$sql = "Select * from customer where member_code = '".trim($_REQUEST['jbc'])."'  ";
			$res = mysql_query($sql);
			$rows2 = mysql_fetch_assoc($res);
			if($rows2['code']>0)
			{	echo '<div style=" background-color:#FFFF99; padding:3px;  float:left; color:#00F"><strong>'.$rows2['name'].' -- <b style=" color:#F00">'.$rows2['mobile'].'</b></strong></span>&nbsp;&nbsp;';
				?> <input type="checkbox"    class="policy_list_d" value="0" name="policy" id="policy0" checked="checked"   /> 
                <?php 
				/*$sql99 = "Select * from customer_booking where customer_code = '".trim($rows2['code'])."' and payment_status=2  ";
				$res99 = mysql_query($sql99);
				while($rows99 = mysql_fetch_assoc($res99))
				{	
					?>
					
                    <label><input type="checkbox" class="policy_list_d" value="<?=$rows99['code'];?>|<?=$rows99['order_no'];?>|<?=$rm=value_remaining($rows99['code']);?>"  name="policy" id="policy<?=$rows99['code'];?>" /><?=$rows99['order_no'];?> (<?=$rows99['wc_no'];?>) Rs. <?=$rm;?></label>&nbsp;&nbsp;&nbsp;
					
				<?php	
				}*/	
			}
			else
				echo  '<div style=" color:#F00">Invalid Customer Code</div>';
		}
		break;
		
		case 'policy_d':
		{
				
			//	print_r($_REQUEST);
				
				if($_REQUEST['policy']=='0')
				{
					$sql99 = "Select * from customer  where member_code  = '".trim($_REQUEST['customer_code'])."'  ";
					$res99 = mysql_query($sql99);
					$rows99 = mysql_fetch_assoc($res99);
					
					 
					 
					$rows99['country'] = display_feild('bm_countries_for_ip','countries_name','code',$rows99['country']);
					$rows99['state'] = display_feild('bm_states','state_name','code',$rows99['state']);
					$rows99['city'] = 	display_feild('bm_districts','district_name','code',$rows99['city']);
					
					echo  '##|##'.$rows99['name'].'##|##'.$rows99['mobile'].'##|##'.$rows99['add_1'].' '.$rows99['add_2'].'##|##'.$rows99['city'].'##|##'.$rows99['state'].'##|##'.$rows99['country'].'##|##'.$rows99['pin_code'].'##|##0';

				
				
				}else
				{
				
					$sql99 = "Select * ,cb.code from customer_booking cb,customer c where cb.code = '".trim($_REQUEST['policy'])."' and cb.payment_status=2 AND cb.customer_code = c.code ";
					$res99 = mysql_query($sql99);
					$rows99 = mysql_fetch_assoc($res99);
					
					if($rows99['billing_add_1']=='')
						$rows99['billing_add_1'] = $rows99['add_1'];
					if($rows99['billing_add_2']=='')
						$rows99['billing_add_2'] = $rows99['add_2'];
					
					if($rows99['billing_pin_code']<>'')
						$rows99['pin_code'] = $rows99['billing_pin_code'];
					
					echo  '##|##'.$rows99['billing_name'].'##|##'.$rows99['billing_mobile'].'##|##'.$rows99['billing_add_1'].' '.$rows99['billing_add_2'].'##|##'.$rows99['billing_city'].'##|##'.$rows99['billing_state'].'##|##'.$rows99['billing_country'].'##|##'.$rows99['pin_code'].'##|##'.$rows99['code'];
					
			 	}
		}
		break;
		
		case 'fill_policy_details':
		{
			 	
					$sql99 = "Select * from customer  where member_code  = '".trim($_REQUEST['customer_code'])."'  ";
					$res99 = mysql_query($sql99);
					$rows  = mysql_fetch_assoc($res99);
					
				echo '<table style="float:right;">';
				$sql99 = "Select * from customer_booking where customer_code = '".trim($rows['code'])."' and payment_status=2  ";
				$res99 = mysql_query($sql99);
				while($rows99 = mysql_fetch_assoc($res99))
				{	
					?>
					
                    	<tr>
                    	<td style="color:#FF00CC;"><?php
                        $rm=value_remaining($rows99['code']);
						
                        echo $rows99['order_no'];?> (<?=$rows99['wc_no'];?>) Rs. <?=$rm;?>
                        </td>
                        
                        <td><input style="text-align:right; color:#FF00CC" type="text" class="policy_dis"   id="policy[<?=$rows99['code']?>]" name="policy[<?=$rows99['code']?>]" value="<?=$rm;?>" ></td> 
                        
                        </tr>
					
				<?php	
				}
				echo '</table>';
				
				$sql99 = "Select sum(amt) as amt from pw_consolidate where userid = '".trim($rows['code'])."'  ";
				$res99 = mysql_query($sql99);
				while($rows99 = mysql_fetch_assoc($res99))
				{	
					echo 'IIIII_IIIII';
					echo '<table style="float:right;">';
					
					?>
                    <tr>
                    	<td style="color:#FF0000;">Promotional Wallet (<?=$rows99['amt'];?>)
                        </td>
                        
                        <td><input style="text-align:right; color:#FF0000;" type="text" class="policy_dis2"   id="promotional_wallet" name="promotional_wallet" value="<?=round($rows99['amt'],2);?>"  >
                        
                        <input style="display:none;" type="text"  id="promotional_wallet_max" name="promotional_wallet_max" value="<?=round($rows99['amt'],2);?>"  >
                        </td> 
                        
                        </tr>
                        <?php 
						echo '</table>';
				
                        
					 	
				}
				
				
				 
		}
		break;
		
		case 'mobile_number':
		{
			if(!is_numeric($_REQUEST['mobile_number']))
			{

				echo '<div style=" background-color:#FFFF99; padding:3px;   color:#F00">Enter a Valid Mobile Number Please</div>';
				return;

			}
			if(strlen($_REQUEST['mobile_number'])<>10)
			{

				echo '<div style=" background-color:#FFFF99; padding:3px;   color:#F00">Enter a Valid 10 Digits Mobile Number Please</div>';
				return;

			}
			
			    $q = "SELECT * FROM  customer where mobile='".$_REQUEST['mobile_number']."' ";
				$qr = mysql_query($q);
				$re2 = mysql_fetch_assoc($qr);
				if($re2['code']>0)
				{
					echo '<div style=" background-color:#FFFF99; padding:2px;color:#F00">
					Sorry Mobile Number Seems already Registered!!</div> ';
				}
				else
				{
					echo  '<div style=" background-color:#FFFF99; padding:2px;  color:#00F">
					<strong>Congratulations!</strong> You are eligible to join.</div> ';	
				}
				
				 
		}
		break;
		
		case'GeneralDetails':
		{
		
			//print_r($_REQUEST);
			
			$searchtext = mysql_real_escape_string($_REQUEST['searchtext']);
			//$prtstatus = $_REQUEST['status'];
			//$prtcat = $_REQUEST['instructions'];
			$searchtext = trim($searchtext);
			$where = '';			
			$fields = array('o.order_no','name','net_amount','email','mobile','customer_code','billing_name','billing_mobile');	
			

			if(!empty($searchtext))
			{
				foreach($fields as $field)
				{
				
					if($where=="")
						$where = ' AND ( ';
					else
						$where .= ' OR ';
					$where .= $field." LIKE '%".$searchtext."%' ";
				}
				$where .=')';
			}
					
				
			/* if($_SESSION['access_type']<2)	
				$where.= " and (sale_employee_code_new='".$_SESSION['admin_code']."' or cc_admin_id='".$_SESSION['admin_code']."')";
*/
 
			
			/*if($_REQUEST['s_mtype']<>"")
			{
				 $where .= " and mtype='".$_REQUEST['s_mtype']."'";
			}	
				
			if($_REQUEST['s_team']<>"")
			{
				 $where .= " and team='".$_REQUEST['s_mtype']."'";
			}	
			if($_REQUEST['s_se']>0)
			{
				 $where .= " and sale_employee_code<=0 ";
			}
			if($_REQUEST['s_sale_employee_code']>0)
			{
				 $where .= " and  sale_employee_code='".$_REQUEST['s_sale_employee_code']."'";
			}
			if($_REQUEST['s_status_c']>0)
			{
				 $where .= " and  customer_calling_status='".$_REQUEST['s_status_c']."'";
			}
			if($_REQUEST['s_sms']<>"")
			{
				 $where .= " and  sms='".$_REQUEST['s_sms']."'";
			}	
*/
			
		  		
  
			
			
			 $where .= " and  o.c_date >='".date("Y-m-d",strtotime($_REQUEST['s_date_from']))."' and o.c_date <='".date("Y-m-d",strtotime($_REQUEST['s_date_to']))."' ";
			
			 $q = "SELECT c.*,o.*,o.code as code FROM customer_orders o Left Join members c ON  c.code=o.customer_code WHERE o.is_deleted=0   $where ORDER BY o.code desc ";
			
				
				
			$qr = mysql_query($q) or die('Error: '.mysql_error());
			
			?> 
            
 			<table id="datatable1" class="table display responsive nowrap" width="100%">
              
            <thead>
             
            <tr>
              <th>Sr. #</th> 
              <th >Order Number</th> 	 	
              <th >Order Date</th>
              <th >Customer Code</th>
              <th >Name</th>
              <th >Email</th>
              <th >Mobile</th>
              <th >Amount</th>
              <th >Created By</th>
              <th >Shiping Status</th>
			 <!-- <th>Status</th> -->
              <th></th> 	
            </tr> </thead><tbody>
            <?php	
			//print_r($p);
        	$i = 1;
			if($p['Page']!="1")
				$i = $p['Page'] * $p['Per_Page'] - $p['Per_Page']+ 1;//$i = $p[current] * $p[perPage] - $p[perPage]+ 1;
            
			while ($rows=mysql_fetch_array($qr))
            {             	
			?>
            <tr <?php	if(($i%2)==0) echo 'class="alt_new"';?>>
             <td><?=$i;?></td>
             
			 
              
             
              <td><?=$rows['order_no']; ?></td>
              <td><?=date("d-m-Y",strtotime($rows['c_date']));?></td>
              <td><?=$rows['customer_code'];?></td>
              <td><?=$rows['billing_name'];?></td>
              <td><?=$rows['email'];?></td>
              <td><?=$rows['billing_mobile'];?></td>
              <td style="text-align:right;"><?=$rows['net_amount'];?></td>
              <td><?=display_feild("admin_users","name","admin_id",$rows['admin_id']);?></td>
              
              <td><?php if($rows['shiping_status']==0) echo '<span style="color:#F00;">Pending</span>';
			 	 else
			  		 echo 'Done';?></td>
                
                
              <td class="controler"><!--<?php if(isdelete('customer_orders','code',$rows['code'])){ ?>
                <img src="../../images/icon-delete.gif" align="absmiddle" onclick="delete_row('<?=$rows['code']; ?>','main_result_div',<?=$p['Page'];?>)"  />
              <?php } ?>-->
              
              <?php 
			  if($rows['code']>0)
			  {
			  ?>
              
              <!--<span onclick="add_edit('<?=$rows['code']; ?>','main_result_div','<?=$p['Page'];?>');" class="  btn-info btn-icon rounded-circle btn-sm  " title="View">
              <div><i class="fa fa-eye"></i></div></span> -->
              
              
               <a target="_blank" href="print_bill/print_bill.php?code=<?=$rows['code']; ?>" class="  btn-info btn-icon rounded-circle btn-sm  " title="Print"><div><i class="fa fa-print"></i></div></a> 
 
              <?php } ?>              </td> 
            </tr>
            
             
              
              
              
                
             <!-- <td><?=print_status($rows['status'])?></td>-->
                
               
                
                <!--<?php 
				if($_SESSION['access_type']==2)	{
				?>
               <span onclick="add_edit('<?=$rows['code']; ?>','main_result_div','<?=$p['Page'];?>');" class="  btn-info btn-icon rounded-circle btn-sm  " title="Edit"><div><i class="fa fa-edit"></i></div></span> 
              
              <?php if(isdelete('customer_booking',"customer_code",$rows['code'],$where=' ')) { ?> 
                  <span onclick="delete_row('<?=$rows['code']; ?>','main_result_div','<?=$p['Page'];?>');" class="  btn-danger btn-icon rounded-circle btn-sm  " title="Delete"><div><i class="fa fa-close"></i></div></span>
              
                
              
              <?php  } 
			  
			  }?>-->
               
              
             
              
             
            <?php
			$i++;
			}
       ?>
         
       </tbody>
          </table>
           
<?php //paging_new('',$p); ?>
		 

    <?php			

    #-------------> Pagination Finish			

		

		
		}

		break;

		 
		case 'add_edit':
		{
		    $i =3;
			$key = array_keys($_REQUEST);	
			$n = count($_REQUEST);					
			while($i<=($n))
			{
				
				$s = $s."&".$key[$i]."=".$_REQUEST[$key[$i]]."";
				$i++;
			}
			require_once("index.class.php");
			$objpl=new General();
			$objpl->add_edit(fetch_details(TABLE_NAME,'code',$_REQUEST['id']),$_REQUEST);	
		}
		break;
		 
		 case 'send_otp':
		{
			
			$data  = $_REQUEST;
			/* echo '<PRE>';
			print_r($_REQUEST);
			echo '</PRE>';*/ 
			
			if($data['policy']<=0)
			{
					echo '<span class="error">Please select a customer and his/her associated Policy</span>';
					return;
			}
			if(trim($data['receiver_name'])=="")
			{
					echo '<span class="error">Please Enter Receiver Name</span>';
					return;
			}
			if($data['t_amount']<=0 or $data['order_total_payable']<=0 )
			{
					echo '<span class="error">Please Select Product!</span>';
					return;
			}
			
			
			 $sql99 = "Select  * from customer_booking cb  where  code = '".trim($data['policy'])."' and cb.payment_status=2 ";
			$res99 = mysql_query($sql99);
			$rows99 = mysql_fetch_assoc($res99);
			if($rows99['code']<=0)
			{
				echo  '<div style=" color:#F00">Error: Wrong Policy Selected</div>';
				return;
			}
				
				
				
			
					//Work Stoped
					$auth_code = find_ID_ran();
					
					
					/*$sql = "Select * from mobile_confirm_sale where m_to='".$rows99['customer_code']."' and m_for='".$rows99['code']."' and t_amount='".$data['t_amount']."',order_total_payable='".$data['order_total_payable']."' and used='' order by code desc limit 0,1";
					$rr = mysql_query($sql);
					$old_d = mysql_fetch_assoc($rr);
					
					if($old_d['code']>0)
					{
						
						$d1 = $old_d['date_b'];
						$d2 = date("Y-m-d H:i:s",strtotime("-2 minutes"));
						
						if($d1>$d2)
						{
							echo '<span style="color:#006" class="sucess">Auth code already sent to mobile no '.$data['mobile'].' please wait for at least 2 minutes.</span>';
							return;	
						}
					}*/
					
					
					 $sql = "Insert into mobile_confirm_sale SET mobile='".$data['mobile']."', auth_code='".$auth_code."',m_to='".$rows99['customer_code']."',m_for='".$rows99['code']."',t_amount='".$data['t_amount']."',order_total_payable='".$data['order_total_payable']."' ";
					if(mysql_query($sql))
					{
						
						 
						 sms_jshine($rows999['customer_code'],$data['mobile'],"Hello, Mr. ".$data['receiver_name']." want to receive a product for youe Policy Number ".$rows99['order_no'].", Your Sale Total is : Rs. ".$data['t_amount'].", where you have to pay Rs. ".$data['order_total_payable'].", Your OTP is ".$auth_code.", kindly do not share this code with unauthorized or unknown person.");
						  
						//echo   $curl_scraped_page;
						echo '<span style="color:#006" class="sucess">OTP sent to mobile no '.$data['mobile'].'</span>';
					}	
				 
				
				
				 
				 
			 
			
		}break;
		
		
		case 'save_key':
		{
			$data = $_POST;
			$data['confirm']  = $_REQUEST['confirm'];
			 
				if($data['customer_type']=='new')
				{
					$q = "SELECT * FROM  customer where mobile='".trim($data['mobile'])."' ";
					$qr = mysql_query($q);
					$re2 = mysql_fetch_assoc($qr);
					if($re2['code']>0)
					{
						echo  '0|0|<div style=" color:#F00">Error: Mobile Number is already registered!</div>';
						return;
					}
					else
					{
					
						if($data['confirm']>0)
						{
								$mce = '9'.substr(time(),3,11);
								
								$ss = "INSERT INTO customer SET 
								name ='".$data['name']."'
								,mobile ='".trim($data['mobile'])."'
								,add_1 ='".trim($data['add_1'])."'
								,add_2 ='".trim($data['add_2'])."'
								,password ='".(rand(2323,234234))."'
								,member_code ='".$mce."'
								,status ='1'
								,country ='".display_feild('bm_countries_for_ip','code','countries_name',trim($data['country']))."'
								,state ='".display_feild('bm_states','code','state_name',trim($data['state']))."'
								,city ='".display_feild('bm_districts','code','district_name',trim($data['city']))."'
								,pin_code ='".trim($data['pin_code'])."'
								
								"; 
								//echo $ss;
								if(mysql_query($ss))
									$data['customer_code'] = $mce;
								else
								{
									echo  '0|0|<div style=" color:#F00">Error: while Saving new Customer</div>';
									return;
								}	
					
						}
						 //
						
					}
				}
			 		

				if($data['confirm']>0)	
				{
					
				}
				
					$q = "SELECT * FROM  customer where member_code='".$data['customer_code']."' ";
					$qr = mysql_query($q);
					$customer_details = $re2 = mysql_fetch_assoc($qr);
					if($re2['code']<=0)
					{
						echo  '0|0|<div style=" color:#F00">Error: Wrong Customer Code</div>';
						return;
					}
					
					
				//echo '0|0|<PRE>';	
				//print_r($data);
				
				$dis = 0;
				$rm = 0;
				$data['discount_name'] = array();	
				if(count($data['policy'])>0)
				foreach($data['policy'] as $policy_code=>$v)
				{
					
					if($v>0)
					{
						$sql99 = "Select cb.* from customer_booking cb,customer c where cb.code = '".trim($policy_code)."' and cb.payment_status=2 AND c.member_code='".$data['customer_code']."' AND cb.customer_code = c.code ";
						$res99 = mysql_query($sql99);
						$rows99 = mysql_fetch_assoc($res99);
						if($rows99['code']<=0)
						{
							echo  '0|0|<div style=" color:#F00">Error: Wrong Policy Selected</div>';
							return;
						}else{
							$rmmm = value_remaining($rows99['code']);
							//echo 'R : '.$rm;
							if($v>$rmmm)
							{
								echo  '0|0|<div style=" color:#F00">Error: '.$rows99['order_no'].' have only Rs. '.$rmmm.' and you are trying to use Rs. '.$data['policy'][$policy_code].'</div>';
								return;
							}
							$data['discount_name'][]=$rows99['order_no'].' '.$v;
						
						}
						$rm+=$v;
					}
				}
					//print_r($re2);
				if($data['promotional_wallet']>0)
				{
					$sql99 = "Select sum(amt) as amt from pw_consolidate where userid = '".trim($re2['code'])."'   ";
					$res99 = mysql_query($sql99);
					$rows99 = mysql_fetch_assoc($res99);
					 
					  
						if($data['promotional_wallet']>$rows99['amt'])
						{
							echo  '0|0|<div style=" color:#F00">Error: You have only '.$rows99['amt'].' Promotional Wallets & you are trying to use '.$data['promotional_wallet'].'</div>';
							return;
						}
						$discount_name_p ='Promotional Wallets '.$data['promotional_wallet'];
						$data['discount_name'][]=$discount_name_p;
					
					 
					//$rm+=$data['promotional_wallet'];
				}
			
			
				if(trim($data['state'])=='')
				{
					echo  '0|0|<div style=" color:#F00">Error: Please Select State</div>';
					return;
				}
					$cgst = 0;
					$sgst = 0;
					$igst = 0;
				if(strtolower(trim($data['state']))=='punjab')
				{
					$cgst = 1.5;
					$sgst = 1.5;
					
				}else
					$igst = 3;
					
				
				
				 $total = 0;
				 $total_tax = 0;
				 $total_making = 0;
				 $mastet_t = 0;
				 $t_discount = 0;
				 $i = 1;
				 $p = array();
				 $pro = array();
				 while($i<=10)
				 {
				 	
					
					if($data['product_code_'.$i]>0 and $data['product_qty_'.$i]>0 and $data['product_price_'.$i]>0  and $data['product_total_'.$i]>0)
					{
						$total+=($data['product_qty_'.$i] * $data['product_price_'.$i]);
						if (in_array($data['product_code_'.$i], $p))
						{
							echo  '0|0|<div style=" color:#F00">Error: You can not order same product twice</div>';
								return;
							
						}
						
						if($data['product_qty_'.$i]<>1)
						{
							echo  '0|0|<div style=" color:#F00">Error: Product Quantity can only be 1</div>';
							return;
							
						}
						
						$p[] = $data['product_code_'.$i];
						
						
						 
						 
						 $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_id ='".$data['product_code_'.$i]."'  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 product_sale_inside=1 and p.is_deleted =0   ORDER BY product_name ";
							
							
							 
							$query = mysql_query($sql);
					        $result = mysql_fetch_assoc($query);
							if($result['product_id']<=0)
							{
								echo  '0|0|<div style=" color:#F00">Error: Please reselect the product seems wrong product selected</div>';
								return;
							}
							
							if($result['product_sold_out']==1)
							{
								echo  '0|0|<div style=" color:#F00">Error: Sorry one of Product in Cart is already soldout, Please select another one </div>';
								return;
							}
							
							
							
							
							$a1 = daily_diamond_rate($result['diamond_type_default']);
							$a=$a1*$result['diamond_total_weight'];
							if($result['diamond_discount_percentage']>0)
							{
				
									$a_before_discount = $a;
									$da= round(($a*$result['diamond_discount_percentage'])/100,2); 
									$a = $a - $da;
							}
							
							
							
							$b1 = daily_metal_rate($result['metal_type_default']);
							$b=$b1*$result['metal_weight'];
							if($result['metal_discount_percentage']>0){								 
								$b_before_discount = $b;
								$da= round(($b*$result['metal_discount_percentage'])/100,2); 
								$b = $b - $da;
							}	 
							
							
							
							$c=gem_rate($result['product_id'],$result['gems_type_default']);
							if($result['stone_discount_percentage']>0)
							{
				 				$c_before_discount = $c;
								$da= round(($c*$result['stone_discount_percentage'])/100,2); 
								$c = $c - $da;
								
							}	
							
							
							if($result['making_charges']>0)
								$d=$result['making_charges'];
							else
								$d=($result['metal_weight']*900);
							
							
							if($result['making_charges_discount_percentage']>0){
							  
							  $d_before_discount = $d;
							  	 $da= round(($d*$result['making_charges_discount_percentage'])/100,2); 
									echo $d = $d - $da;
							  }
				  
								
							 $total_making+=$d;
							 
							
							
							
							$t = ($a+$b+$c+$d); 
							
							
							
							if($result['discount_percantage']>0)
							{
								$dis = round(($t*$result['discount_percantage'])/100);
								$t2 = $t - $dis;
								 	//echo $text.= '<BR/><span style=\'color:#F00\'>Discount ('.$result['discount_percantage'].'%) =  '.number_format($dis,2).'   --- '.$t.'</span>';
							}else
								$t2 = $t;
							
							$discount_amt = $dis;
							$t_discount+=$discount_amt;
							
							$t_cgst = round(($t*$cgst)/100,2);
							$t_sgst = round(($t*$sgst)/100,2);
							$t_igst = round(($t*$igst)/100,2);
							
							$tax = ($t_cgst+$t_sgst+$t_igst);
							
							$tt_cgst+=$t_cgst;
							$tt_sgst+=$t_sgst;
							$tt_igst+=$t_igst;
							
							
							$t_cgst2 = round(($t2*$cgst)/100,2);
							$t_sgst2 = round(($t2*$sgst)/100,2);
							$t_igst2 = round(($t2*$igst)/100,2);
							
							$tax2 = ($t_cgst2+$t_sgst2+$t_igst2);
							 
							$tt_cgst2+=$t_cgst2;
							$tt_sgst2+=$t_sgst2;
							$tt_igst2+=$t_igst2;
							
							$mastet_t+= $t;
							
							$total_tax+=$tax2;
							$total_bd = round($t + $tax);
							$total = round($t2 + $tax2);
							
							
							 
							$total_total+=$total;
							
							$pro[] = array(
										'code'=>$result['product_id']
										,'name'=>$result['product_name']
										
											,'size'=>$result['product_size_default']
										,'sku'=>$result['product_ccode']
										
										,'certificate_number'=>$result['certificate_number']
										,'diamond_name'=>$result['d_name']
										,'diamond_total_weight'=>$result['diamond_total_weight']
										,'diamond_rate'=>$a1
										,'diamond_total'=>$a
										,'diamond_total'=>$a
										
										,'metal_name'=>$result['m_name']
										,'metal_total_weight'=>$result['metal_weight']
										,'metal_rate'=>$b1
										,'discount'=>$result['discount_percantage']
										,'discount_amt'=>$discount_amt
										
										
										,'gem_name'=>$result['g_name']
										,'gem_total_weight'=>$result['gems_size']
										,'gem_rate'=>$c
										,'gem_total'=>$c
										
										,'making_charges'=>$d
										,'tax'=>$tax
										
										,'t_cgst'=>$t_cgst2
										,'t_sgst'=>$t_sgst2
										,'t_igst'=>$t_igst2
										
										,'cgst'=>$cgst
										,'sgst'=>$sgst
										,'igst'=>$igst
										
										,'total'=>$t
										,'total_all'=>($t*$data['product_qty_'.$i])
										,'qty'=>($data['product_qty_'.$i])
										
									);
											
											
											
							if($total<>round($data['product_price_'.$i]))
							{
								echo  '0|0|<div style=" color:#F00">Error: Please reselect the product seems there is change in product price. Total ='.$total.' & Product price is = '.round($data['product_price_'.$i]).' </div>';
								return;
							}
							
						
					 
					
				 }$i++; 
				 }
				 
				$total = $total_total;
				if($total<=0)
				{
					echo  '<div style=" color:#F00">Error: Please select Products</div>';;
					return;
				}
				
				if($total<$rm)
				{
					echo  '<div style=" color:#F00">Error: Can not use Policy credit more than Order total </div>';;
					return;
				}
				
				//NEw Total 
				 
			 
			 	//echo  '<BR>'.$total;
				//	echo  '<BR>'.$tax2;
				//	echo  '<BR>'.$data['promotional_wallet'];
					
					$total_bt = round((($total) - $data['promotional_wallet'] - $rm + $data['shipping']),0);
					
					$tax2 = round( ($total_bt - (($total_bt/103)*100)),0);
					
					
					 
					// echo  '<BR>'.$total_bt;
					//echo  '<BR>'.$tax2; 
					//echo  '<BR>'.$rm; 
					//echo  '<BR>'.$rm; 
					//echo  '<div style=" color:#F00">Error: '.$total.' </div>';;
				
				
				
				
				 
						$tt_cgst2 = 0;
						$tt_sgst2 = 0;
						$tt_igst2 = 0;
				
				 		if($cgst>0)
							$tt_cgst2 = round($tax2/2,2);
						if($sgst>0)
							$tt_sgst2 = round($tax2/2,2);
						if($igst>0)
							$tt_igst2 = round($tax2,2);
				
				 
				
				   
					
							
				
				
				$total = ($total_bt);
					//die(); 
				/*echo  '<BR>'.$total_bt;
				echo  '<BR>T : '.$total;
				echo  '<BR>'.$tax2; 
				echo  '<BR>'.$rm; 
				echo  '<div style=" color:#F00">Error: '.$tt_igst2.' </div>';die();
				 */
				$rem = $total;
				
				if($data['payment']=='cash')
					$pmode= 'by CASH';
				else
					$pmode= ' via DD/Cheque';
				
				
				
				if($data['product_taking_for']=='other')
				{
				
						echo  $sql = "select * FROM mobile_confirm_sale where mobile='".$data['mobile']."' AND auth_code='".$data['otp']."' AND m_to='".$rows99['customer_code']."' AND m_for='".$rows99['code']."' AND t_amount='".$rem."' AND order_total_payable='".$total."' ";
						$rr = mysql_query($sql);
						$ro_code = mysql_fetch_assoc($rr);
						
						if($ro_code['code']>0 and $ro_v['used']=="")
						{
								
						}else
						{
							echo  '<div style=" color:#F00">Error: Wrong auth Code</div>';;
							return;
						}
						
				}
				
				
				
				
				if($data['confirm']<=0)
					{
						
						//echo '0|99|<span style="font-size:14px;  background-color:#FFFF00; padding:4px;"><input type="checkbox" value="1" id="confirm" name="confirm"> Please verify that you have checked product and quantity twice and you are receiving  <b style="font-size:17px;">Rs. '.number_format($rem+$tax2,2).' '.$pmode.' </b>
						echo '0|99|<span style="font-size:14px;  background-color:#FFFF00; padding:4px;"><input type="checkbox" value="1" id="confirm" name="confirm"> Please verify that you have checked product and quantity twice and you are receiving  the amount as mentioned above
'; 
							 
			return;
					}
				
				 
			if($data['payment']=='other')
			{
				if(trim($_REQUEST['payment_mode'])=="")
				{
					echo '<div class="error">Enter Payment Mode Please</div>';
					return;	
				} 
				if(trim($_REQUEST['dd_no'])=="")
				{
					echo '<div class="error">Enter Ch/DD/Bank Number Please</div>';
					return;	
				}
				 
			}
			
			
			
			 
			
			
			
				
				//echo '0|0|';	
				
				$k = array(); 
				//unset($_SESSION["b"]);
				
				
				
				$s = "INSERT INTO customer_orders ";
				
				 
					
					$s .= "SET customer_code ='".$data['customer_code']."'";
					$s .= ",billing_email ='".trim($data['email'])."'";
					$s .= ",billing_name ='".trim($data['name'])."'";
					$s .= ",billing_mobile ='".trim($data['mobile'])."'";
					$s .= ",billing_add_1 ='".trim($data['add_1'])."'";
					$s .= ",billing_add_2 ='".trim($data['add_2'])."'";
					$s .= ",billing_city ='".trim($data['city'])."'";
					$s .= ",billing_state ='".trim($data['state'])."'";
					$s .= ",billing_country ='".trim($data['country'])."'";
					$s .= ",billing_pin_code ='".trim($data['postal_code'])."'";
					
					 if($data['shipping_same']==1)
					 {
						 $data['shipping_name'] = $data['name'];
						 $data['shipping_mobile'] = $data['mobile'];
						 $data['shipping_add_1'] = $data['add_1'];
						 $data['shipping_add_2'] = $data['add_2'];
						 $data['shipping_city'] = $data['city'];
						 $data['shipping_state'] = $data['state'];
						 $data['shipping_country'] = $data['country'];
						 $data['shipping_postal_code'] = $data['postal_code'];
						 
					 }
				//  echo '<PRE>';
				//  print_r($data);
					/*$country = display_feild('bm_countries_for_ip','countries_name','code',$data['country']);
					$state = display_feild('bm_states','state_name','code',$data['state']);
					$city = $data['city'];
					*/
					
					$s .= ",shipping_same ='".trim($data['shipping_same'])."'";
					$s .= ",shipping_name ='".trim($data['shipping_name'])."'";
					$s .= ",shipping_mobile ='".trim($data['shipping_mobile'])."'";
					$s .= ",shipping_add_1 ='".trim($data['shipping_add_1'])."'";
					$s .= ",shipping_add_2 ='".trim($data['shipping_add_2'])."'";
					$s .= ",shipping_country ='".trim($data['shipping_country'])."'";
					$s .= ",shipping_state ='".trim($data['shipping_state'])."'";
					$s .= ",shipping_city ='".trim($data['shipping_city'])."'";
					$s .= ",shipping_pin_code ='".trim($data['shipping_postal_code'])."'";
					
					$dispatch_details = "Name : ".$data['shipping_name'].'\n ';
					$dispatch_details.= "Contact Number : ".$data['shipping_mobile'].'\n ';
					$dispatch_details.= "".$data['shipping_add_1'].'\n ';
					$dispatch_details.= "".$data['shipping_add_2'].'\n ';
					$dispatch_details.= "".$data['shipping_city'].','.$data['shipping_state'].','.$data['shipping_city'].'\n ';
					$dispatch_details.= "".$data['shipping_postal_code'].'\n ';
					
				 
				$s .= ",admin_id ='".$_SESSION['admin_code']."'";
				//$s .= ",customer_type ='".trim($data['customer_type'])."'";
				$s .= ",total_amount ='".trim($data['order_total'])."'";
				
				
				
				$s .= ",t_cgst ='".$tt_cgst2."'";
				$s .= ",t_sgst ='".$tt_sgst2."'";
				$s .= ",t_igst ='".$tt_igst2."'";
				
				
				$s .= ",cgst ='".$cgst."'";
				$s .= ",sgst ='".$sgst."'";
				$s .= ",igst ='".$igst."'"; 
				
				$s .= ",promotional_wallet ='".trim($data['promotional_wallet'])."'";
				
				$s .= ",discount ='".($data['promotional_wallet'])."'";//$t_discount
				$s .= ",discount_name ='".$discount_name_p."'";
				
				
				$s .= ",policy_code ='".trim($data['policy_code'])."'";
				$s .= ",policy_amount ='".trim($rm)."'";
				$s .= ",policy_remarks ='".trim(implode(",",$data['discount_name']))."'";
				
				
				$s .= ",product_taking_for ='".trim($data['product_taking_for'])."'";
				$s .= ",receiver_name ='".trim($data['receiver_name'])."'";
				$s .= ",receiver_mobile ='".trim($data['receiver_mobile'])."'";
				$s .= ",otp ='".trim($data['otp'])."'";
				
				
			//	if($data['shipping_option']=='ship')
				//	$s .= ",shiping_status ='0'";
			//	else
			//	{	
					$s .= ",shiping_status ='1'";
					$s .= ",shiping_remarks ='Products delivered (Picked From Office)'";
				//}
				$s .= ",shiping ='".trim($data['shipping'])."'";	
				$s .= ",net_amount ='".trim($total)."'";
				$s .= ",paid_after_did ='".trim($rem)."'";
				$s .= ",coupon_code ='".trim('')."'"; 
				$s .= ",tax_p ='3'"; 
				$s .= ",tax ='".$total_tax."'"; 
				$s .= ",making ='".$total_making."'"; 
				$s .= ",payment_type ='".trim($data['payment'])."'"; 
				
				$s .= ",payment_mode ='".trim($data['payment_mode'])."'"; 
				$s .= ",dd_no ='".trim($data['dd_no'])."'"; 
				$s .= ",remarks ='".mysql_real_escape_string(trim($data['remarks']))."'"; 
				$s .= ",order_in ='1'";
				$s .= ",ip ='".getRealIpAddr()."'";
				$s .= ",c_date ='".date("Y-m-d")."'";
				// echo  '0|0|'.$s;die();
				//$total = $_SESSION['order']['total'];
				
				if(@mysql_query($s))
				{
					$order_id = mysql_insert_id();
					$order_no_text = "JS/".date("mY")."/S/".$order_id;
					
					$s = "update customer_orders SET order_no='".$order_no_text."'  where code='".$order_id."' ";
					mysql_query($s);
					
					
					
					if($data['promotional_wallet']>0)
					{
						$s = "INSERT INTO pw_consolidate SET userid = '".$re2['code']."' ,amt='-".$data['promotional_wallet']."', date='".date("Y-m-d")."', remarks='Promotional Wallet used against order number ".$order_id."'";
						mysql_query($s);
						
					}
					//
					
					
					if($data['product_taking_for']=='other')
					{
					
							$sql = "update mobile_confirm_sale SET used = 1 where code='".$ro_code['code']."' ";
							$rr = mysql_query($sql);
					}
					
					 
					if($rm>0)
					{
						
						if(count($data['policy'])>0)
						foreach($data['policy'] as $policy_code=>$v)
						{
							
							if($v>0)
							{
								 $sql = "INSERT INTO consolidate_policy_used SET policy_code='".$policy_code."', remarks='"."".$order_no_text."', paid='1', date='".date("Y-m-d")."',amt='".$v."'";
								mysql_query($sql);
							}
						}
					}
					
					if($rem>0)
					{
						$sql = "INSERT INTO payment_collection 
							SET policy_code='".$data['policy_code']."', 
							customer_code='".$data['customer_code']."', 
							order_no='"."".$order_no_text."', 
							order_no_no='"."".$order_id."', 
							remarks='"."Payment Received for ".$order_id."', ";
							
							if($data['payment']=='cash')
								$sql.= "p_type='Cash', ";
							else
								$sql.= "p_type='Cheque/DD',";
							
							$sql.= "cheque_no='".$data['dd_no']."',";
							$sql.= "cheque_remarks='".$data['payment_mode']."',";
							$sql.= "admin_id='".$_SESSION['admin_code']."',";
							$sql.= " paid='1', date='".date("Y-m-d")."',amt='".$rem."'";
						mysql_query($sql);
					}
					
					$sql = "INSERT INTO product_dispatch SET m_code='".$data['customer_code']."', order_no='"."".$order_no_text."', entery_type='Online', dispatch_address='".mysql_real_escape_string(trim($dispatch_details))."'";
					if($data['shipping_option']=='self')
					{
						$sql .= ",status='1'";
						$sql .= ",dispatching_notes='Products delivered (Picked From Office)'";
						 
					}
				 
					mysql_query($sql);
					$dispatch_id = mysql_insert_id();
						
						
						$i = 1;
						$j = 1;
						foreach($pro as $det)
						{
						
								$s = "INSERT INTO customer_order_items ";
								$s .= "SET order_no ='".$order_id."'";
								$s .= ",c_code ='".$data['customer_code']."'";
								$s .= ",product_code ='".$det['code']."'";
								$s .= ",certificate_number ='".$det['certificate_number']."'";
								$s .= ",product_name ='".$det['name']."'";
								$s .= ",product_size ='".$det['size']."'";
								$s .= ",product_sku ='".$det['sku']."'";
								$s .= ",purchase_price	 ='".$det['total']."'";
								$s .= ",our_price ='".$det['total']."'";
								$s .= ",mrp ='".$det['total']."'";
									
								$s .= ",diamond_name ='".$det['diamond_name']."'";
								$s .= ",diamond_total_weight ='".$det['diamond_total_weight']."'";
								$s .= ",diamond_rate ='".$det['diamond_rate']."'";
								$s .= ",diamond_total ='".$det['diamond_total']."'";
								
							 	$s .= ",metal_name ='".$det['metal_name']."'";
								$s .= ",metal_total_weight ='".$det['metal_total_weight']."'";
								$s .= ",metal_rate ='".$det['metal_rate']."'";
								$s .= ",metal_total ='".$det['metal_total']."'";
								
								
								 		
								$s .= ",gem_name ='".$det['gem_name']."'";
								$s .= ",gem_total_weight ='".$det['gem_total_weight']."'";
								$s .= ",gem_rate ='".$det['gem_rate']."'";
								$s .= ",gem_total ='".$det['gem_total']."'";
								
								
								$s .= ",making_charges ='".$det['making_charges']."'";
								$s .= ",tax ='".$det['tax']."'";
								
								$s .= ",net_amount ='".$det['total_all']."'";
								$s .= ",offer_price ='".$det['total']."'";
								
								$s .= ",discount_amt ='".$det['discount_amt']."'";
								$s .= ",discount_type ='".$det['discount']."'";
								$s .= ",rsp ='".$det['rsp']."'";
								 
								$s .= ",qty ='".$det['qty']."'";
								
								$s .= ",t_cgst ='".$det['t_cgst']."'";
								$s .= ",t_sgst ='".$det['t_sgst']."'";
								$s .= ",t_igst ='".$det['t_igst']."'";
								
								
								$s .= ",cgst ='".$det['cgst']."'";
								$s .= ",sgst ='".$det['sgst']."'";
								$s .= ",igst ='".$det['igst']."'"; 
							 	//echo  '<BR/>'.$s;
								mysql_query($s);
								
								
								
								$s = "update product SET product_sold_out='1',sold_policy='".$data['policy_code']."',sold_customer='".$data['customer_code']."'  where product_id='".$det['code']."' ";
								mysql_query($s);
								
					 
								$s = "INSERT INTO despatch_details ";
								$s.= " SET pins_master_code=''";
								$s.= " ,dispatch_code='".$dispatch_id."'";
								if($data['shipping_option']=='ship')
									$s.= " ,psstatus='0'";				
								else
									$s.= " ,psstatus='1'";				
								$s.= " ,product_name='".$det['name'].'('.$det['size'].")'";
								$s.= " ,qty='".$data['product_qty_'.$i]."'";
								$s.= " ,product_code='".$det['code']."'";
								$s.= " ,vat='".$det['tax_p']."'";
								$s.= " ,total='".($nt * $data['product_qty_'.$i])."'";
								$s.= " ,subtotal='".($nt)."'";
								$s.= " ,date='".date("Y-m-d")."'";  
								  
								mysql_query($s);
								 
								
								$product.= $det['name'].'('.$det['size'].') X '.$data['product_qty_'.$i].'\n';
								
								 
								 
								$j++;
							
							$i++;
						}
					
					 $s = "Update product_dispatch SET products_dispatched='".mysql_real_escape_string($product)."' where code='".$dispatch_id."'";
					 mysql_query($s);
							
					 
							$sql = "select * from sms_template where code='8' ";
							$r = mysql_query($sql);
							$ro_4 = mysql_fetch_assoc($r);		
										
							
							$ro_4['template'] = str_replace("[NAME]",ucwords($customer_details['name']),$ro_4['template']);
							$ro_4['template'] = str_replace("[TOTAL_PAYABLE_AMOUNT]",number_format($data['order_total'],2),$ro_4['template']);
							$ro_4['template'] = str_replace("[TOTAL_BILL_AMOUNT]",number_format($total,2),$ro_4['template']);
							
							//$customer_details['mobile'] = '9888864504';
							sms_jshine($customer_details['code'],$customer_details['mobile'],$ro_4['template'],1,$ro_4['sender_id']);
					  
						echo '1|1|Order Created Successfully '.$order_no_text; 
						 
					}else
						
					 echo '1|0|Error while creating new Order'; 
		 
			
				
				 
			
			
			 
			
		}
		break;
		
		case 'delete_row':
		{
			//print_r($_REQUEST);
			if($_REQUEST['id']>0)
			{
				$sql = "Select * from  ".TABLE_NAME." where code = '".$_REQUEST['id']."'";
				$res = mysql_query($sql);
				$rows = mysql_fetch_assoc($res);
							
					if($_SESSION['access_type']<2)
					{
						 		echo '1|0|Access Denied!';
								 return;
					}
									
				 if(isdelete('customer_booking',"customer_code",$rows['code'],$where=' '))
				 { 
					 $sql = "update ".TABLE_NAME." SET is_deleted=1 WHERE code='".$_REQUEST['id']."' ";
					if(mysql_query($sql))
					{	
						create_log(TABLE_NAME,$_REQUEST,'code');	
						echo '1|1|Record Deleted';
						return true;
					}
					else
					{	echo '0|0|Error : while deleting this record!!';	
						return true;
					}
				 }
			 }
					echo '0|0|Error : Can\'t Delete record!!';
		}
		break;
		
		
	};

	

}
function fetch_products2($code)
		{
			$sql = "SELECT *,ps.code as code FROM product p INNER JOIN product_sizes ps ON p.code=ps.product_code and p.status=1 and ps.status=1 and ps.code='".$code."'  ";
			$result = mysql_query($sql);
			$rows = mysql_fetch_assoc($result);
			
			return $rows;
		 }
		
		function getRealIpAddr()
		{
			if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
			{
			  $ip=$_SERVER['HTTP_CLIENT_IP'];
			}
			elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
			{
			  $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
			}
			else
			{
			  $ip=$_SERVER['REMOTE_ADDR'];
			}
			return $ip;
		}
?>

