<?php

include('db_connection.php');

$Mainclass=new DB_Controller();
$currentYr=date("y");
	    $currentMonth=date("m");
		if($currentMonth<04)
		{
			$currentYrFinacila=($currentYr - 1);
		}
		else
		{
			$currentYrFinacila=$currentYr;
		}
		
		$FinalcialRange=$currentYrFinacila."-".($currentYrFinacila+1);
if($_COOKIE['O_Code']!='')
{
//$Get_Order_Master=$Mainclass->order_master_det('gold_billing_order_master',$_REQUEST['Order']);
$Condition_Status=" Order_No='".$_REQUEST['Order']."' and Status='Confirmed'";
$Check_Order_Already_Open=$Mainclass->num_check('gold_billing_order_master',"SLN",$Condition_Status);
if($Check_Order_Already_Open==0)
{
$condition_check_order=" Order_No='".$_REQUEST['Order']."'";
$Get_Order_Master=$Mainclass->Fetch_individual_data('gold_billing_order_master','*',$condition_check_order);
$Customer_Name=$Mainclass->real_escapes($_REQUEST['Cus_name'])." ".$Mainclass->real_escapes($_REQUEST['cust_lname']);
$Bill_Date=date('Y-m-d H:i:s');
$Net_Amount=$Get_Order_Master['Net_Total'];
$Return_Adjusted=$Get_Order_Master['Return_Adjusted'];
	$Final_Amt=$Net_Amount - $Return_Adjusted;
$Round=round($Final_Amt) - $Final_Amt;
//$Bill_Date=date('Y-m-d H:i:s');
//=====================check stock==================//


	/*$table='gold_billing_order_details';
	$CheckStockOutProduct=$Mainclass->check_complete_order_product_stock($table,$_REQUEST['Order'],$_COOKIE['O_Code']);

	if($CheckStockOutProduct>0)
	{
	 
	   echo $Error=1;
	   exit;
	}*/
$Bday_Type=$_REQUEST['Bday_Type'];
	$Age=$_REQUEST['Bday_Age'];
	if($Bday_Type=="DOB")
	{
		$date1 = strtotime($_REQUEST['Bday']);  
$date2 = time();  
$Bday1=abs($date2 - $date1);
$years = floor($Bday1 / (365*60*60*24));
	}
	else
	{
		$years=$Age;
	}
//======================end check stock============================//
	if($Get_Order_Master['Ref_Advance_Order_No']!='')
	{
	$condition_check_advorder="Order_No='".$Get_Order_Master['Ref_Advance_Order_No']."' and Status!='Confirmed'";
$Get_Advance_Order=$Mainclass->num_check('advance_order_master',"SLN",$condition_check_advorder);
		$Advance_Num=$Get_Advance_Order;
	}
	else
	{
		$Advance_Num='0';
	}
	
if($Advance_Num==0)
{
	/*if($_REQUEST['Hearabout']=="SMS")
	{
		$Hear_about="FRIENDS AND RELATIVES";
	}
	else if($_REQUEST['Hearabout']=="WHATSAPP")
	{
		$Hear_about="PASSING BY";
	}
	else
	{
	$Hear_about=$_REQUEST['Hearabout'];
	
	}*/
	$Hear_about=$_REQUEST['Hearabout'];

$FieldValue_Address="Customer_Name='".$Customer_Name."',Contact='".$_REQUEST['mobile']."',Email='".$_REQUEST['emailid']."',Address_Line1='".$Mainclass->real_escapes($_REQUEST['address1'])."',Address_Line2='".$Mainclass->real_escapes($_REQUEST['address2'])."',State='".$_REQUEST['State']."',Pin='".$_REQUEST['pincode']."',Country='".$_REQUEST['Country']."',City='".$_REQUEST['City']."',Purchase_Reason='".$_REQUEST['Reason']."',Promotion='".$_REQUEST['Promotion']."',Hear_About='".$Hear_about."',Gender='".$_REQUEST['Gender']."',Marital_Status='".$_REQUEST['Marital']."',Salutation='".$_REQUEST['Salute']."',Building_No='".$_REQUEST['Building_No']."',Street_Name='".$_REQUEST['Street']."',Customer_Gst='".$_REQUEST['Customer_Gst']."',Ethinicity='".$_REQUEST['Ethni']."',Complete='Yes',Age='".$years."',DOB_Type='".$Bday_Type."'";
if($_REQUEST['Bday']!='')
{
	$FieldValue_Address.=",DOB='".date('Y-m-d',strtotime($_REQUEST['Bday']))."'";
}
if($_REQUEST['Aniversary']!='')
{
	$FieldValue_Address.=",Anniversary_Date='".date('Y-m-d',strtotime($_REQUEST['Aniversary']))."'";
}

$Condition_Address=" Order_No='".$_REQUEST['Order']."'";	

$Update_Cus_Details=$Mainclass->Update_Data('order_address_master_gold',$FieldValue_Address,$Condition_Address);




$Check=$Mainclass->member_check('member_master',$_REQUEST['mobile']);
$Check2=$Mainclass->member_check('inactive_member_master',$_REQUEST['mobile']);
$condition_check3=" Contact='".$_REQUEST['mobile']."' and Member='0'";
$Check3=$Mainclass->num_check('order_address_master_gold',"SLN",$condition_check3);
$Check4=$Mainclass->num_check('order_address_master',"SLN",$condition_check3);

//check if new customer or inactive customer//
if($Check==0 && $Check2==0)
{
	$New_Customer='1';//===New Customer===//
}
else if($Check==0 && $Check2>0)
{
	$New_Customer='2';//===Old Customer===//
}
	else if($Check>0 && $Check2==0 && $_REQUEST['Member']!=1)
{
	$New_Customer='2';//===Old Customer===//
}
	else if($Check3>1 && $_REQUEST['Member']!=1)
{
	$New_Customer='2';//===Old Customer===//
}
	else if($Check4>1 && $_REQUEST['Member']!=1)
{
	$New_Customer='2';//===Old Customer===//
}
else if($_REQUEST['Member']==1)
{
	$New_Customer='0';
}
	else
	{
		$New_Customer='0';
	}







if($Check==0 && $Check2==0)
{

//insert into member master//

 $Fields='Name,Phone,Email,Address_Line1,Address_Line2,State,Pin,Country,City,Modification_Date,Gender,Marital_Status,Last_Order_Dealer_Code,Salutation,Building_No,Street_Name,Customer_Gst,Ethinicity';
if($_REQUEST['Bday']!='')
{
	$Fields.=",DOB";
}
if($_REQUEST['Aniversary']!='')
{
	$Fields.=",Anniversary_Date";
}
$Values="'".$Customer_Name."','".$_REQUEST['mobile']."','".$_REQUEST['emailid']."','".$Mainclass->real_escapes($_REQUEST['address1'])."','".$Mainclass->real_escapes($_REQUEST['address2'])."','".$_REQUEST['State']."','".$_REQUEST['pincode']."','".$_REQUEST['Country']."','".$_REQUEST['City']."','".date('Y-m-d H:i:s')."','".$_REQUEST['Gender']."','".$_REQUEST['Marital']."','".$_COOKIE['O_Code']."','".$_REQUEST['Salute']."','".$_REQUEST['Building_No']."','".$_REQUEST['Street']."','".$_REQUEST['Customer_Gst']."','".$_REQUEST['Ethni']."'";

if($_REQUEST['Bday']!='')
{
	$Values.=",'".date('Y-m-d',strtotime($_REQUEST['Bday']))."'";
}
if($_REQUEST['Aniversary']!='')
{
	$Values.=",'".date('Y-m-d',strtotime($_REQUEST['Aniversary']))."'";
}

$Insert_Master=$Mainclass->Insert_Data('member_master',$Fields,$Values);

}

else if($Check>=1)
{

$FieldValue="Name='".$Customer_Name."',Email='".$_REQUEST['emailid']."',Address_Line1='".$Mainclass->real_escapes($_REQUEST['address1'])."',Address_Line2='".$Mainclass->real_escapes($_REQUEST['address2'])."',State='".$_REQUEST['State']."',Pin='".$_REQUEST['pincode']."',Country='".$_REQUEST['Country']."',City='".$_REQUEST['City']."',Modification_Date='".date('Y-m-d H:i:s')."',Gender='".$_REQUEST['Gender']."',Marital_Status='".$_REQUEST['Marital']."',Last_Order_Dealer_Code='".$_COOKIE['O_Code']."',Salutation='".$_REQUEST['Salute']."',Building_No='".$_REQUEST['Building_No']."',Street_Name='".$_REQUEST['Street']."',Customer_Gst='".$_REQUEST['Customer_Gst']."',Ethinicity='".$_REQUEST['Ethni']."'";
if($_REQUEST['Bday']!='')
{
	$FieldValue.=",DOB='".date('Y-m-d',strtotime($_REQUEST['Bday']))."'";
}
if($_REQUEST['Aniversary']!='')
{
	$FieldValue.=",Anniversary_Date='".date('Y-m-d',strtotime($_REQUEST['Aniversary']))."'";
}
//$Condition=" SLN='".$_REQUEST['Mem_sln']."'";	
$Condition=" Phone='".$_REQUEST['mobile']."' and Phone!=''";

$Update_master=$Mainclass->Update_Data('member_master',$FieldValue,$Condition);

}
else if($Check2>=1)
{

/*$FieldValue="Email='".$_REQUEST['emailid']."',Address_Line1='".$Mainclass->real_escapes($_REQUEST['address1'])."',Address_Line2='".$Mainclass->real_escapes($_REQUEST['address2'])."',State='".$_REQUEST['State']."',Pin='".$_REQUEST['pincode']."',Country='".$_REQUEST['Country']."',City='".$_REQUEST['City']."',DOB='".date('Y-m-d',strtotime($_REQUEST['Bday']))."',Anniversary_Date='".date('Y-m-d',strtotime($_REQUEST['Aniversary']))."',Modification_Date='".date('Y-m-d H:i:s')."',Gender='".$_REQUEST['Gender']."',Marital_Status='".$_REQUEST['Marital']."'";


$Condition=" Phone='".$_REQUEST['mobile']."'";

$Update_master=$Mainclass->Update_Data('inactive_member_master',$FieldValue,$Condition);*/




$Get_Details=$Mainclass->Fetch_individual_data('inactive_member_master',"*","Member_Id='".$_REQUEST['mobile']."' or Phone='".$_REQUEST['mobile']."'");


$Fields='Name,Phone,Email,Address_Line1,Address_Line2,State,Pin,Country,City,Modification_Date,Gender,Marital_Status,Points,Member,Member_Id,Last_Order_Dealer_Code,Salutation,Building_No,Street_Name,Customer_Gst,Ethinicity';
if($_REQUEST['Bday']!='')
{
	$Fields.=",DOB";
}
if($_REQUEST['Aniversary']!='')
{
	$Fields.=",Anniversary_Date";
}

$Values="'".$Customer_Name."','".$_REQUEST['mobile']."','".$_REQUEST['emailid']."','".$Mainclass->real_escapes($_REQUEST['address1'])."','".$Mainclass->real_escapes($_REQUEST['address2'])."','".$_REQUEST['State']."','".$_REQUEST['pincode']."','".$_REQUEST['Country']."','".$_REQUEST['City']."','".date('Y-m-d H:i:s')."','".$_REQUEST['Gender']."','".$_REQUEST['Marital']."','".$Get_Details['Points']."','".$Get_Details['Member']."','".$Get_Details['Member_Id']."','".$_COOKIE['O_Code']."','".$_REQUEST['Salute']."','".$_REQUEST['Building_No']."','".$_REQUEST['Street']."','".$_REQUEST['Customer_Gst']."','".$_REQUEST['Ethni']."'";
if($_REQUEST['Bday']!='')
{
	$Values.=",'".date('Y-m-d',strtotime($_REQUEST['Bday']))."'";
}
if($_REQUEST['Aniversary']!='')
{
	$Values.=",'".date('Y-m-d',strtotime($_REQUEST['Aniversary']))."'";
}

$Insert_Master=$Mainclass->Insert_Data('member_master',$Fields,$Values);

//$del_inactive_mem=$Mainclass->delete_member('inactive_member_master',$_REQUEST['mobile']);

}


if($Get_Order_Master['Invoice_Id']=='')
{
//generate invoice Number//

$Invoice_Number=$Mainclass->Generate_Gold_Invoice($_COOKIE['O_Code']);

}


//insert into invoice masters//

$Fields5='Invoice_No,Order_No,Dealer_Code,Invoice_date,Status,Financial_Year';

$Values5="'".$Invoice_Number."','".$_REQUEST['Order']."','".$_COOKIE['O_Code']."','".$Bill_Date."','Complete','".$FinalcialRange."'";

$Insert_Invoice_master=$Mainclass->Insert_Data('gold_invoice_master',$Fields5,$Values5);
	$Condition=" Order_No='".$_REQUEST['Order']."'";
	$FieldVal="Cart_Complete='Yes',Sold_By='".$_REQUEST['Sellby']."',Invoice_No='".$Invoice_Number."'";
$Update_Order_Details=$Mainclass->Update_Data('gold_billing_order_details',$FieldVal,$Condition);

$Get_Gst_Sum=$Mainclass->get_sum_field('gold_billing_order_details','GST',"Order_No='".$_REQUEST['Order']."' and Cart_Complete='Yes'" );
	
	$Cond_Product_Qty=" Order_No='".$_REQUEST['Order']."' and Product_Catagory!='Consumable' and Cart_Complete='Yes'";
	$Qty_Sum=$Mainclass->get_sum_field('gold_billing_order_details','Quantity',$Cond_Product_Qty);
	
	$Cond_Consumable_Qty=" Order_No='".$_REQUEST['Order']."' and Product_Catagory='Consumable' and Cart_Complete='Yes'";
	$Consumable_Qty_Sum=$Mainclass->get_sum_field('gold_billing_order_details','Quantity',$Cond_Consumable_Qty);
//insert into order masters//

$FieldValue="Customer_Contact='".$_REQUEST['mobile']."',Amount_Received='".$_REQUEST['billamt']."',Invoice_Id='".$Invoice_Number."',Status='Confirmed',Payment_Date='".$Bill_Date."',Sold_By='".$_REQUEST['Sellby']."',GST='".$Get_Gst_Sum."',Age='".$years."',New_Customer='".$New_Customer."',Member='".$_REQUEST['Member']."',Round_Off='".$Round."',Quantity='".$Qty_Sum."',Consumable_Quantity='".$Consumable_Qty_Sum."',Bill_Generated_By='".$_COOKIE['Emp_Name']."'";

//$Condition=" Order_No='".$_REQUEST['Order']."'";	

$Update_master=$Mainclass->Update_Data('gold_billing_order_master',$FieldValue,$Condition);




//====insert point=====//
$Amtless_gst=$_REQUEST['billamt'] + $Get_Order_Master['Advance_Received'] - $Get_Gst_Sum;
if($_REQUEST['Mem_Type']==1)
{
	
$Point=floor($Amtless_gst/50);
}
else
{
$Point=floor($Amtless_gst/100);	
}
$Day=date('d');
	$Month=date('m');
	$Year=date('Y',strtotime('+1 years'));
	
	$Point_Expiry_Date=$Year."-".$Month."-".$Day;
$F='Order_No,Order_Date,Contact,Bill_Amount,Points,Expiry_Date,Points_Balance,Dealer_Code';
$V="'".$_REQUEST['Order']."','".date('Y-m-d',strtotime($Bill_Date))."','".$_REQUEST['mobile']."','".$_REQUEST['billamt']."','".$Point."','".$Point_Expiry_Date."','".$Point."','".$_COOKIE['O_Code']."'";
//$Insert_Points=$Mainclass->Insert_Data('customer_point_master',$F,$V);

$condition="Contact='".$_REQUEST['mobile']."' and Expiry_Date>='".date('Y-m-d')."'";
//$Get_Points=$Mainclass->get_sum('customer_point_master','Points_Balance',$condition);

//update points//
$F1="Points='".$Get_Points."'";
$V1="Phone='".$_REQUEST['mobile']."' and Phone!=''";

//$Check=$Mainclass->member_check('member_master',$_REQUEST['mobile']);
/*if($Check>0)
{*/
//$Update_Points=$Mainclass->Update_Data('member_master',$F1,$V1);
/*}
else{
$Update_Points=$Mainclass->Update_Data('inactive_member_master',$F1,$V1);
}*/
//insert into payment masters//
if($_REQUEST['cashamt']!=0 || $_REQUEST['cashamt']!='')
{
$Cash=$_REQUEST['cashamt'] - abs($_REQUEST['RT_Amt']);
}
else
{
	$Cash=$_REQUEST['cashamt'];
}
$Amount_Tendered=$_REQUEST['cashamt'] + $_REQUEST['Bankamt'] + $_REQUEST['Otheramt'] + $_REQUEST['Otheramt1'] + $_REQUEST['Gift_Card_Amt'];
$Fields1='Invoice_Id,Order_No,Payment_Amount,Payment_Date,Amount_Receive,Due_Amount,Card_Number,Bank_Name,Pos_Name,Bank_Amount,Cash_Amount,Other_Payment_Name,Other_Payment_Amount,Other_Payment_Name2,Other_Payment_Amount2,Gift_Card_Number,Gift_Card_Amount,Complete,Amount_Tendered,Return_Amount,Dealer_Code';

$Values1="'".$Invoice_Number."','".$_REQUEST['Order']."','".$_REQUEST['billamt']."','".date('Y-m-d',strtotime($Bill_Date))."','".$_REQUEST['Amountpaid']."','".$_REQUEST['dueamt']."','".$_REQUEST['cardnum']."','".$_REQUEST['Bank']."','".$_REQUEST['posname']."','".$_REQUEST['Bankamt']."','".$Cash."','".$_REQUEST['Otherpaymode']."','".$_REQUEST['Otheramt']."','".$_REQUEST['Otherpaymode1']."','".$_REQUEST['Otheramt1']."','".$_REQUEST['Gift_Card']."','".$_REQUEST['Gift_Card_Amt']."','Yes','".$Amount_Tendered."','".$_REQUEST['RT_Amt']."','".$_COOKIE['O_Code']."'";	

$Insert_Payment_master=$Mainclass->Insert_Data('gold_payment_master',$Fields1,$Values1);

	$condition_ret_adv="Order_No='".$_REQUEST['Order']."'";	
$Get_Ret_Adv_Amt=$Mainclass->Fetch_individual_data('gold_billing_order_master',"Advance_Received,Return_Adjusted",$condition_ret_adv);	

	$FieldValue_Pay_Inv="Advance_Received='".$Get_Ret_Adv_Amt['Advance_Received']."',Return_Adjusted='".$Get_Ret_Adv_Amt['Return_Adjusted']."'";
	$Condition_Pay_Inv=" Order_No='".$_REQUEST['Order']."'";
	$Update=$Mainclass->Update_Data('gold_payment_master',$FieldValue_Pay_Inv,$Condition_Pay_Inv);
//============================Cash Movement=============================//
if($_REQUEST['cashamt']>0)
{
$ActualCash=$_REQUEST['cashamt']-abs($_REQUEST['RT_Amt']);
$Cash_Balance_Pearl=$Mainclass->Get_Cash_Movement_Balance_Pearl($_COOKIE['O_Code']);
$Cash_Balance_Gold=$Mainclass->Get_Cash_Movement_Balance_Gold($_COOKIE['O_Code']);
	
$Update_Balance_Pearl=$Cash_Balance_Pearl+$ActualCash;
$Update_Balance_Gold=$Cash_Balance_Gold+$ActualCash;
	
$Fields_CM='Dealer_Code,Date,Cash_In,Transaction_Id,Transaction_Type,Balance,Status';
$Values_CM="'".$_COOKIE['O_Code']."','".date('Y-m-d',strtotime($Bill_Date))."','".$ActualCash."','".$Invoice_Number."','Order Payment','".$Update_Balance_Pearl."','1'";
$Insert_Cash_Mov_Master=$Mainclass->Insert_Data('cash_movement_master',$Fields_CM,$Values_CM);
	
	
$Fields_CM1='Dealer_Code,Date,Cash_In,Transaction_Id,Transaction_Type,Balance,Status';
$Values_CM1="'".$_COOKIE['O_Code']."','".date('Y-m-d',strtotime($Bill_Date))."','".$ActualCash."','".$Invoice_Number."','Order Payment','".$Update_Balance_Gold."','1'";
$Insert_Cash_Mov_Master1=$Mainclass->Insert_Data('gold_cash_movement_master',$Fields_CM1,$Values_CM1);
	
	
}
//=============================end cash movement====================================//
	//====Update Payment Mode=======//
	$Mainclass->UpdatePaymentMode($_REQUEST['Order']);


//==update_current_year_purchase for members==//
//$Mainclass->Total_Amount_Spent($Get_Order_Master['Customer_Contact']);
	
	//=====Update Customer Extra Details=======//
	//$Mainclass->Update_Cust($Get_Order_Master['Customer_Contact']);

//==update Advance_Master===//
if($Get_Order_Master['Ref_Advance_Order_No']!='')
{
	$FieldValue11="Status='Claimed'";
	$Condition11=" Order_No='".$Get_Order_Master['Ref_Advance_Order_No']."'";
	$Update=$Mainclass->Update_Data('advance_order_master',$FieldValue11,$Condition11);
}

//insert into other payment masters//

$Fields2='Pay_Name,Active';

$Values2="'".$_REQUEST['Otherpaymode']."','Yes'";

if($_REQUEST['Otherpaymode']!='')

{	

$Insert_Other_Payment_master=$Mainclass->Insert_Data('other_payment_master',$Fields2,$Values2);

}	



//insert into other payment masters//

$Fields6='Pay_Name,Active';

$Values6="'".$_REQUEST['Otherpaymode1']."','Yes'";

if($_REQUEST['Otherpaymode1']!='')

{	

$Insert_Other_Payment_master=$Mainclass->Insert_Data('other_payment_master',$Fields6,$Values6);

}



//insert into bank masters//

$Fields3='Bank_Name,Active';

$Values3="'".$_REQUEST['Bank']."','Yes'";

if($_REQUEST['Bank']!='')

{

$Insert_Bank_master=$Mainclass->Insert_Data('bank_master',$Fields3,$Values3);

}	

//insert into POS masters//

$Fields4='Pos_Name,Active';

$Values4="'".$_REQUEST['posname']."','Yes'";

if($_REQUEST['posname']!='')

{

$Insert_Bank_master=$Mainclass->Insert_Data('pos_name_master',$Fields4,$Values4);

}	

//$Return=$Mainclass->Get_Returned_Product('gold_billing_order_master',$_REQUEST['Order']);

/*

$FieldValue22="Status='1'";

$Condition22=" Order_No='".$_REQUEST['Order']."'";	

$Update_master22=$Mainclass->Update_Data('return_adjustment_details',$FieldValue22,$Condition22);



$Return=$Mainclass->Get_Returned_Product('return_adjustment_details',$_REQUEST['Order']);

$FieldValue33="Return_Status='Returned'";



foreach($Return as $ReturnDetails)
{
	
	if($ReturnDetails['Data_Type']=='New')
	{
		$condition_return_inv=" Invoice_Id='".$ReturnDetails['Return_Order_No']."'";	
	$Get_Return_Order_No_From_Inv=$Mainclass->Fetch_individual_data('gold_billing_order_master',"Order_No",$condition_return_inv);
     $Condition33=" Order_No='".$Get_Return_Order_No_From_Inv['Order_No']."' and Product_Code='".$ReturnDetails['Product_Code']."'";
     $Update_master33=$Mainclass->Update_Data('gold_billing_order_details',$FieldValue33,$Condition33);
	}
	else if($ReturnDetails['Data_Type']=='Old')
	{
		$Condition33=" Order_No='".$ReturnDetails['Return_Order_No']."' and Product_Code='".$ReturnDetails['Product_Code']."'";
     $Update_master33=$Mainclass->Update_Data('temp_return_order_details',$FieldValue33,$Condition33);
	}
}

$condition_inv=" Order_No='".$_REQUEST['Order']."' and Status='1'";	
$Get_Returned_Data=$Mainclass->Get_Required_Field('return_adjustment_details',"Return_Order_No,Data_Type,Product_Code",$condition_inv);
//$Update_ReturnStock=$Mainclass->ReturnProductStock($ReturnDetails['Return_Order_No'],$_COOKIE['O_Code']);
foreach($Get_Returned_Data as $Get_Returned_Invoice_No)
{
$Update_ReturnStock=$Mainclass->ReturnProductStock($Get_Returned_Invoice_No['Return_Order_No'],$_COOKIE['O_Code'],$Get_Returned_Invoice_No['Data_Type'],$Get_Returned_Invoice_No['Product_Code']);	
}*/

/*

if($Get_Order_Master['Ref_Advance_Order_No']!='')
{
   
   $UpdateBlockProduct_Stock=$Mainclass->BlockProductStockOut($Get_Order_Master['Ref_Advance_Order_No'],$_COOKIE['O_Code']);
   
   $FieldValue11="Status='Claimed'";
	$Condition11=" Order_No='".$Get_Order_Master['Ref_Advance_Order_No']."'";
	$Update=$Mainclass->Update_Data('advance_order_master',$FieldValue11,$Condition11);   
    
}

*/

    // _____________________________  UPDATE STOCK DETAILS ______________________//


 $Update_Stock=$Mainclass->UpdateProductStock($_REQUEST['Order'],$_COOKIE['O_Code'],$Bill_Date);

    // _____________________________  UPDATE STOCK DETAILS ______________________//



if($_REQUEST['Gift_Card_Amt']!='')
{
 $SQL="SELECT * FROM `gift_coupon_master` WHERE Coupon_Code='".$_REQUEST['Gift_Card']."'";
 $QRY=mysqli_query($Mainclass->dbh,$SQL);
 $Fetch=mysqli_fetch_assoc($QRY);  

 $RemaningGiftValue=$Fetch['Discount_Amount']-$_REQUEST['Gift_Card_Amt'];
 
 $SqlCouponUpd="UPDATE `gift_coupon_master` SET `Discount_Amount`='".$RemaningGiftValue."' WHERE `Phone_No`='".$_REQUEST['mobile']."' and Coupon_Code='".$_REQUEST['Gift_Card']."'";
 $QRY=mysqli_query($Mainclass->dbh,$SqlCouponUpd);
 
  $Field='Order_No,Coupon_Code,Contact,Date,Discount_Amount,Dealer_User,Status';
  $Status=1;
  $Values="'".$_REQUEST['Order']."','".$_REQUEST['Gift_Card']."','".$_REQUEST['mobile']."','".date('Y-m-d',strtotime($Bill_Date))."',".$_REQUEST['Gift_Card_Amt'].",'".$_COOKIE['O_Code']."','".$Status."'";

  $Insert_Master=$Mainclass->Insert_Data('gift_coupon_details',$Field,$Values);
 
}
if($Get_Order_Master['Voucher_Discount_Value']>0 ||$Get_Order_Master['Voucher_Code']!='' )
{
   $SqlCouponDtl="UPDATE `gift_coupon_details` SET `Status`='1' WHERE `Order_No`='".$_REQUEST['Order']."' and Coupon_Code='".$Get_Order_Master['Voucher_Code']."'";
   $QRY=mysqli_query($Mainclass->dbh,$SqlCouponDtl);
}

//$Generate_Coupon=$Mainclass->Generate_Coupon($_REQUEST['Order']);

	
echo $_REQUEST['Order']."*".$_REQUEST['emailid'];
}
else
{
	$Vari='2';//===Advance already Cancelled=====//
echo $Vari."*".$_REQUEST['emailid'];	
}
}
else
{
	$Vari='0';
	echo $Vari."*".$_REQUEST['emailid'];
}
if($_REQUEST['emailid']!='' && $Advance_Num==0)
{
$Email=$_REQUEST['emailid'];
//$Mainclass->send_invoice_mail($Email,$_REQUEST['Order']);	
}
	if($_REQUEST['mobile']!='' && $Advance_Num==0)
	{
	$Mob=$_REQUEST['mobile'];
	$Orderid=base64_encode($_REQUEST['emailid']);
	//$Mainclass->Send_Invoice_SMS($Mob,$Orderid);
	}
	//====Whatsapp======//
	$Get_Order_Details=$Mainclass->Fetch_individual_data('order_address_master_gold',"Member_Id,Contact,Dealer_Code","Order_No='".$_REQUEST['Order']."'");
	if($Get_Order_Details['Member_Id']=='' && strtoupper($_COOKIE['O_Code'])=="JS")
	{
		$Get_Dealer_Info=$Mainclass->Fetch_individual_data('dealer_master',"Dealer_Name","Order_Code='".$Get_Order_Details['Dealer_Code']."'");
		$Mainclass->New_Membership_Whatsapp($Get_Order_Details['Contact'],$_REQUEST['Order'],$Get_Dealer_Info['Dealer_Name']);
	}
	
	//=====If_Reffered Gift and Purchase Same Day=======//
	/*$Get_Reffered_Date=$Mainclass->Fetch_individual_data('lead_master',"Gift_Given_On,Type","Reffered_To_Contact='".$_REQUEST['mobile']."'");
	if(date('Y-m-d',strtotime($Get_Reffered_Date['Gift_Given_On']))==date('Y-m-d'))
	{
		$Salesman=$_REQUEST['Sellby'];
		
		$Ins_Same_Day=$Mainclass->Insert_Data('customer_purchase_same_day_refer_gft',"Dealer_Code,Salesperson,Refer_Contact,Added_On,Type","'".$_COOKIE['O_Code']."','".$Salesman."','".$_REQUEST['mobile']."','".date('Y-m-d H:i:s')."','".$Get_Reffered_Date['Refferal_Type']."'");
	}*/
	mysqli_close($Mainclass->dbh);
}
else
{
	?>
	<script>
	window.location.href='logout.php'
	</script>
    <?php
    }
?>