<?php 

include("db_connection.php");
$Mainclass=new DB_Controller();
$Order_Id = $_REQUEST['Order_Id'];
$cancellationReason = mysqli_real_escape_string($Mainclass->dbh,$_REQUEST['cancellationReason']);

$Bill_Date = date('Y-m-d');

$Condition=" Order_No='".$Order_Id."'";
$FieldValue=" Status='Bill_Cancelled',Bill_Cancel_Reason='".$cancellationReason."'";
$FieldValue1=" Complete='No'";
$FieldValue2=" Status='Cancelled'";
$FieldValue3=" Cart_Complete='No'"; 
$Update_Order_Master=$Mainclass->Update_Data('gold_billing_order_master',$FieldValue,$Condition);
$Update_Order_Details=$Mainclass->Update_Data('gold_billing_order_details',$FieldValue3,$Condition);
$Update_Payment_Master=$Mainclass->Update_Data('gold_payment_master',$FieldValue1,$Condition);
$Update_Address_Master=$Mainclass->Update_Data('order_address_master_gold',$FieldValue1,$Condition);
$Update_Invoice_Master=$Mainclass->Update_Data('gold_invoice_master',$FieldValue2,$Condition);

$get_payment_mode=$Mainclass->Fetch_individual_data("gold_billing_order_master","Mode,Amount_Received,Order_Date,Invoice_Id",$Condition);

$Get_Product_to_Return=$Mainclass->Fetch_multiple_data('gold_billing_order_details','Product_Code,Quantity,Barcode',$Condition);
foreach($Get_Product_to_Return as $Stock_Return)
{
	$Condition1=" Barcode='".$Stock_Return['Barcode']."' and Dealer_Code='".$_COOKIE['O_Code']."'";

			$Get_Quantity=$Mainclass->Challan_Details('dealer_gold_stock_master',$Condition1);
			
			$Qty=$Get_Quantity['Quantity'] + $Stock_Return['Quantity'];
			
			
			$FieldValue="Quantity='".$Qty."'";

			$Condition2=" Product_Code='".$Stock_Return['Product_Code']."' and Dealer_Code='".$_COOKIE['O_Code']."'";

			$Update_Stock=$Mainclass->Update_Data('dealer_gold_stock_master',$FieldValue,$Condition2);
			
			
			
			$Total_Gold_wt=$Get_Quantity['gold_wt']+$Stock_Return['gold_wt'];
			$Total_Diamond_wt=$Get_Quantity['diamond_ct']+$Stock_Return['Diamond_Carat'];
			$Total_Solitair_wt=$Get_Quantity['solitaire_ct']+$Stock_Return['sollitaire_Carat'];

				 $Fields='Challan_No,Date,Dealer_Code,Product_Code,Category_Code,Stock_In,Balance,Booked_Balance,gold_purity,gold_wt,diamond_ct,solitaire_ct,Movement_Type,DRP_Balance,gold_purity';

				$Values="'".$Order_Id."','".$Bill_Date."','".$_COOKIE['O_Code']."','".$Stock_Return['Product_Code']."','".$Get_Quantity['Category_Code']."','".$Stock_Return['Quantity']."','".$Qty."','".$Get_Quantity['Booked']."','".$Get_Product['GoldPurity']."','".$Total_Gold_wt."','".$Total_Diamond_wt."','".$Total_Solitair_wt."','Bill Cancelled','".$Get_Quantity['DRP']."','".$Get_Quantity['gold_purity']."'";

				$Insert_Transaction=$Mainclass->Insert_Data('gold_stock_transaction_master',$Fields,$Values);
}

if($get_payment_mode['Mode']=='Cash')
{
//============================Cash Movement=============================//
$Cash_Balance=$Mainclass->Get_Cash_Movement_Balance($_COOKIE['O_Code']);
$Update_Balance=$Cash_Balance-$get_payment_mode['Amount_Received'];
$Fields_CM='Dealer_Code,Date,Cash_Out,Transaction_Id,Transaction_Type,Balance,Status';
$Values_CM="'".$_COOKIE['O_Code']."','".date('Y-m-d',strtotime($Get_Bill_Details['Order_Date']))."','".$Payment_Mode['Cash_Amount']."','".$Get_Bill_Details['Invoice_Id']."','Cancelled Bill Repayment','".$Update_Balance."','1'";
$Insert_Cash_Mov_Master=$Mainclass->Insert_Data('gold_cash_movement_master',$Fields_CM,$Values_CM);
}
		

	
	//$Update_Ret_Billing_Details=$Mainclass->Update_Data('gold_billing_order_details',"Return_Quantity=(`Return_Quantity`-'".$Get_Return_Adjustment['Qty']."'),Remaining_Quantity=(`Quantity`-`Return_Quantity`)",$Condition_ret_adj);
		

?>