<?php
	include("../config.php");
	$response = array();
	if ($_POST['delete']) {
		$id 			   = $_POST['delete'];
		$type              = 'delete_mr';
	    $user_id           = $_SESSION['user_detail']['id'];
	    $data              = serialize($_POST);
	    $created_datetime  = date('Y-m-d H:i:s');
	    $created_id        = $_SESSION['user_detail']['id'];
	    $updated_datetime  = date('Y-m-d H:i:s');
	    $updated_id        = $_SESSION['user_detail']['id'];

	    $temp_mr 	 	   = "SELECT * FROM mr WHERE mr_no = '".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
	    $temp_mr_res       = mysqli_query($conn,$temp_mr);
	    $mr_details        = array();
	    while ($temp_mr_row= mysqli_fetch_assoc($temp_mr_res)) {
	    	$mr_details[]  = $temp_mr_row;
	    }

	    foreach ($mr_details as $key => $details) {
	    	$trans_data 		= "SELECT * FROM `trans` WHERE `voucher_no` = '".$details['bill_no']."' AND (flag='BILLING' OR flag='GENERAL SALES') AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";

     		$trans_data_res 	= mysqli_query($conn,$trans_data);
     		$trans_data_row 	= mysqli_fetch_assoc($trans_data_res);
     		$extra_add_sub 	    = abs($details['ind_addition'] - $details['ind_deduction']) ;
     		$bill_amount        = $details['bill_rec'] - $extra_add_sub;
     		$adjust 			= $trans_data_row['adjust'] - $bill_amount;
     		$balance			= $bill_amount + $trans_data_row['balance'] + $extra_add_sub + $details['tds'] + $details['cash_diss'] + $details['late_delivery'] + $details['claim'];

     		
     		$last_add         	= abs($trans_data_row['other_add'] - $details['ind_addition']);
     		$last_discount      = '0';
     		$last_tds      		= '0';
            $last_less          = abs($trans_data_row['other_less'] - $details['ind_deduction'] - $details['late_delivery'] - 
            	                  $details['claim']-$details['cash_diss']);
     		
     		$update_mrs    		= "UPDATE `trans` SET `adjust`='".$adjust."',`balance`='".$balance."',`discount`='".$last_discount."',`tds`='".$last_tds."',`other_add`='".$last_add."',`other_less`='".$last_less."' WHERE `voucher_no` = '".$details['bill_no']."' AND (flag = 'BILLING' OR flag = 'GENERAL SALES') AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
	     	$mrs_res = mysqli_query($conn,$update_mrs);

	     	$trans_detail = "UPDATE `trans_details` SET `adjust`='".$adjust."',`balance`='".$balance."',`discount`='".$last_discount."',`tds`='".$last_tds."',`other_add`='".$last_add."',`other_less`='".$last_less."' WHERE voucher_no = '".$details['bill_no']."' AND (flag = 'BILLING' OR flag = 'GENERAL SALES') AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
          	$trans_details_res = mysqli_query($conn,$trans_detail);

	    }
	    
	    $lhch = "SELECT * FROM mr_payment_status WHERE  mr_no = '".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' ";
    	$lhch_res = mysqli_query($conn,$lhch);
    	$vou = array();
    	while ($lhch_row = mysqli_fetch_array($lhch_res)) {
      		$vou[] = $lhch_row['voucher_no'];
    	}

    	foreach ($vou as $key => $vou) {
      		$gl    		= "DELETE FROM `general_ledger` WHERE `voucher_no` = '".$vou."' AND flag='MR' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
      		$gl_res  	= mysqli_query($conn,$gl);

      		$pay   		= "DELETE FROM `payment` WHERE `voucher_no` = '".$vou."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
      		$pay_res 	= mysqli_query($conn,$pay);
		}

    	$mr_d         	= "DELETE FROM `mr` WHERE `mr_no` = '".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    	$mr_d_res     	= mysqli_query($conn,$mr_d);

    	$mr_de        	= "DELETE FROM `mr_details` WHERE `mr_no` = '".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    	$mr_de_res    	= mysqli_query($conn,$mr_de);

    	$lhcp 	  	  	= "DELETE FROM `mr_payment_status` WHERE `mr_no` = '".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
	   	$lhcp_res  	  	= mysqli_query($conn,$lhcp);
	   	
    	$gls          	= "DELETE FROM `general_ledger` WHERE `voucher_no` = '".$id."' AND flag='MR' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    	$result       	= mysqli_query($conn,$gls);

		$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    	$acttivity_res = mysqli_query($conn,$activiti);

		if ($result) {
			$response['status']  = 'success';
			$response['message'] = 'MR Deleted Successfully ...';
		} else {		
			$response['status']  = 'error';
			$response['message'] = 'Unable to delete product ...';
		}
		echo json_encode($response);
	}
?>