<?php 
ob_start();
session_start();
include "../config/config.php";
include "../config/session.php";

// Stock Transfer Delete Here
if($_POST['type']=="stock_transfer_delete"){
    
$delete_id = $_POST['delete_id'];



$statements_1 = $pdo->prepare("DELETE FROM tbl_stock_transfer WHERE id=?");
$results_1 = $statements_1->execute(array($delete_id)); 

$statements_2 = $pdo->prepare("DELETE FROM tbl_stock_transfer_items WHERE transfer_id=?");
$results_2 = $statements_2->execute(array($delete_id)); 



$category = "Stock Transfer";
$message = "Stock Transfer - (ID: $delete_id) Deleted By $session_name";
$type = 3;
$statements_2 = $pdo->prepare("INSERT INTO `tbl_notifications`(`message`, `category`, `ids`, `type`, `date`, `user_id`, `created_at`) VALUES (?,?,?,?,?,?,?)");
$results_2 = $statements_2->execute(array($message, $category, $delete_id, $type, $date, $session_id, $updated_at)); 

    
if($results_1 && $results_2){
    $message='Success..';
    $error='1';
}else{
    $message='Failed..';
    $error='0';
}


	$datas[] = array('message' => $message, 'error' => $error);                   
    echo $message;
}


// Expenses Delete
if($_POST['type']=="expenses_delete"){
    
$delete_id = $_POST['delete_id'];



$statements_1 = $pdo->prepare("DELETE FROM tbl_expenses WHERE id=?");
$results_1 = $statements_1->execute(array($delete_id)); 

$statements_2 = $pdo->prepare("DELETE FROM tbl_payments WHERE expense_id=?");
$results_2 = $statements_2->execute(array($delete_id)); 

    
if($results_1 && $results_2){
    $message='Success..';
    $error='1';
}else{
    $message='Failed..';
    $error='0';
}


	$datas[] = array('message' => $message, 'error' => $error);                   
    echo $message;
}


// Advance Voucher Delete
if($_POST['type']=="advance_voucher_delete"){
    
$delete_id = $_POST['delete_id'];



$statements_1 = $pdo->prepare("DELETE FROM tbl_advance_voucher WHERE id=?");
$results_1 = $statements_1->execute(array($delete_id)); 

$statements_2 = $pdo->prepare("DELETE FROM tbl_payments WHERE advance_id=?");
$results_2 = $statements_2->execute(array($delete_id)); 

    
if($results_1 && $results_2){
    $message='Success..';
    $error='1';
}else{
    $message='Failed..';
    $error='0';
}


	$datas[] = array('message' => $message, 'error' => $error);                   
    echo $message;
}


// Customer Scheme Delete
if($_POST['type']=="customer_scheme_delete"){
    
    $customer_scheme_id = $_POST['delete_id'];

    $statement = $pdo->prepare("UPDATE `tbl_customer_scheme` SET `soft_delete`=? WHERE id=?");
    $result = $statement->execute(array(1, $customer_scheme_id)); 
        


// $statements_1 = $pdo->prepare("DELETE FROM tbl_customer_scheme WHERE id=?");
// $results_1 = $statements_1->execute(array($customer_scheme_id)); 

// $statements_2 = $pdo->prepare("DELETE FROM tbl_payments WHERE scheme_id=?");
// $results_2 = $statements_2->execute(array($customer_scheme_id));  // $results_1 && $results_2

    
if($result){
    $message='Success..';
    $error='1';
}else{
    $message='Failed..';
    $error='0';
}


	$datas[] = array('message' => $message, 'error' => $error);                   
    echo $message;
}


// Purchase Payment Delete
if($_POST['type']=="payment_purchase_delete"){
    
$payment_id = $_POST['delete_id'];


$statement = $pdo->prepare("SELECT * FROM tbl_payments WHERE id=?");
$result = $statement->execute(array($payment_id)); 
$query = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();

if($totalData>0){
    $purchase_id = $query[0]['purchase_id'];
    $amount = $query[0]['amount'];
    
    $statement = $pdo->prepare("UPDATE tbl_purchases SET paid=paid-'$amount' WHERE id=?");
    $result = $statement->execute(array($purchase_id));
    
    $statements_7 = $pdo->prepare("DELETE FROM tbl_payments WHERE id=?");
    $results_7 = $statements_7->execute(array($payment_id)); 
    
    
}
    
if($result==1)
{
    $message='Success..';
    $error='1';
}
else
{
    $message='Failed..';
    $error='0';
}


	$datas[] = array('message' => $message, 'error' => $error);                   
    echo $message;
}


// Sales Payment Delete
if($_POST['type']=="sales_purchase_delete"){
    
$payment_id = $_POST['delete_id'];


$statement = $pdo->prepare("SELECT * FROM tbl_payments WHERE id=?");
$result = $statement->execute(array($payment_id)); 
$query = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();

if($totalData>0){
    $sales_id = $query[0]['sale_id'];
    $amount = $query[0]['amount'];
    
    $statement = $pdo->prepare("UPDATE tbl_sales SET paid=paid-'$amount' WHERE id=?");
    $result = $statement->execute(array($sales_id));
    
    $statements_7 = $pdo->prepare("DELETE FROM tbl_payments WHERE id=?");
    $results_7 = $statements_7->execute(array($payment_id)); 
    
    
}
    
if($result==1)
{
    $message='Success..';
    $error='1';
}
else
{
    $message='Failed..';
    $error='0';
}


	$datas[] = array('message' => $message, 'error' => $error);                   
    echo $message;
}













// Purchase Delete
if($_POST['type']=="purchase_delete"){
    
$purchase_id = $_POST['delete_id'];
 
$statement = $pdo->prepare("SELECT * FROM tbl_barcode_items WHERE barcode_id=?");
$result = $statement->execute(array($purchase_id)); 
$query = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();

if($totalData>0){
    foreach($query as $res){
        
        $barcode_items_id = $res['id'];
        
        $statement = $pdo->prepare("SELECT * FROM tbl_product_variants WHERE barcode_items_id=?");
        $result = $statement->execute(array($barcode_items_id)); 
        $query = $statement->fetchAll(PDO::FETCH_ASSOC);
        $totalData = $statement->rowCount();
        if($totalData>0){
            foreach($query as $pro_variant_id){
                $quantity = $pro_variant_id['quantity'];
                $purchase_quantity = $pro_variant_id['purchase_quantity'];
                $sales_status = $pro_variant_id['sales_status'];
                $tbl_product_id = $pro_variant_id['product_id'];
                
                if($sales_status==0){
                    $result="FALSE";
                }else{
                    $result="TRUE";
                }
                
                
                
        if($result=="FALSE"){
            $return_arr['status']=400;
            $return_arr['error'] = true;
            $return_arr['message'] = "Purchase Can't Delete...! Because This Purchase Product are Saled...";
            print_r(json_encode($return_arr));
            return false;
            exit(); 
        }
                
                
                
                
                
                
                
                
                
            }
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        }else{
                $result="TRUE";
        }
        
    }
}
 
 
 

if($result=="TRUE"){
    
    
// 1) Purchase Items and Products Delete
$statement = $pdo->prepare("SELECT * FROM tbl_barcode_items WHERE barcode_id=?");
$result = $statement->execute(array($purchase_id)); 
$query = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();

if($totalData>0){
    foreach($query as $res){
        
        $barcode_items_id = $res['id'];
        
        $statement = $pdo->prepare("SELECT * FROM tbl_product_variants WHERE barcode_items_id=?");
        $result = $statement->execute(array($barcode_items_id)); 
        $query = $statement->fetchAll(PDO::FETCH_ASSOC);
        $totalData = $statement->rowCount();
        if($totalData>0){
            foreach($query as $pro_variant_id){
                
                $product_variant_id = $pro_variant_id['id'];
                $product_id = $pro_variant_id['product_id'];
                $statements_4 = $pdo->prepare("DELETE FROM tbl_product_variants WHERE id=?");
                $results_4 = $statements_4->execute(array($product_variant_id));
                $statements_6 = $pdo->prepare("DELETE FROM tbl_products WHERE id=?");
                $results_6 = $statements_6->execute(array($product_id));
    
            }
        }
        
    $statements_5 = $pdo->prepare("DELETE FROM tbl_barcode_items WHERE id=?");
    $results_5 = $statements_5->execute(array($barcode_items_id));  
        
        

    }
}
    
    
// 2) Barcode Items Delete - Here
$statements_3 = $pdo->prepare("DELETE FROM tbl_barcode WHERE purchase_id=?");
$results_3 = $statements_3->execute(array($purchase_id));
    
    
// 3) Purchase Items Delete - Here
$statements_2 = $pdo->prepare("DELETE FROM tbl_purchases_items WHERE purchase_id=?");
$results_2 = $statements_2->execute(array($purchase_id));
    
    
    
    $statements_1 = $pdo->prepare("DELETE FROM tbl_purchases WHERE id=?");
    $results_1 = $statements_1->execute(array($purchase_id));
    
    
    
    

    
    
    $statements_7 = $pdo->prepare("DELETE FROM tbl_payments WHERE purchase_id=?");
    $results_7 = $statements_7->execute(array($purchase_id)); 












    
    
    
    
    
}



 
 
 
 


if($result=="TRUE"){
    $statements_1 = $pdo->prepare("DELETE FROM tbl_purchases WHERE id=?");
    $results_1 = $statements_1->execute(array($purchase_id));
    
    
    $return_arr['message']='Purchase Deleted Successfully...';
    $return_arr['status']=200;
}
else
{
    $return_arr['status']=400;
    $return_arr['message'] = "Purchase Can't Delete...! Because This Purchase Product are Saled...";
}


	echo json_encode($return_arr);
}





// Sales Delete
if($_POST['type']=="sales_delete"){
    
$sales_id = $_POST['delete_id'];
 
$statement = $pdo->prepare("SELECT * FROM tbl_sale_items WHERE sale_id=?");
$result = $statement->execute(array($sales_id)); 
$query = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();

// if(!empty($query[0]['payment_scheme_ids'])){
    
//     $payment_update = $pdo->prepare("UPDATE tbl_customer_scheme SET sale_id=? WHERE sale_id=? AND type=1 ");
//     $payment_result = $payment_update->execute(array(0,$sales_id)); 
    
// }elseif(!empty($query[0]['weight_scheme_ids'])){
    
//     $weight_update = $pdo->prepare("UPDATE tbl_customer_scheme SET sale_id=? WHERE sale_id=? AND type=2 ");
//     $weight_result = $weight_update->execute(array(0,$sales_id)); 
    
// }elseif(!empty($query[0]['advance_ids'])){
    
//     $advance_update = $pdo->prepare("UPDATE tbl_advance_voucher SET sale_id=? WHERE sale_id=? ");
//     $advance_result = $advance_update->execute(array(0,$sales_id)); 
    
// }

if($totalData>0){
    foreach($query as $res){
        
        $sale_items_id = $res['id'];
        $product_id = $res['product_id'];
        $quantity = $res['quantity'];
        
        
        $statementss_0 = $pdo->prepare("UPDATE tbl_product_variants SET quantity=quantity+'$quantity' WHERE id=?");
        $resultss_0 = $statementss_0->execute(array($product_id)); 
        
        
        $statementss_1 = $pdo->prepare("UPDATE tbl_product_variants SET sales_status=1 WHERE id=?");
        $resultss_1 = $statementss_1->execute(array($product_id)); 
        
        $statementss_2 = $pdo->prepare("DELETE FROM tbl_sale_items WHERE id=?");
        $resultss_2 = $statementss_2->execute(array($sale_items_id));
        
        
        
        
    }
}
 
 
$statementss_3 = $pdo->prepare("DELETE FROM tbl_sales WHERE id=?");
$resultss_3 = $statementss_3->execute(array($sales_id));

$statementss_4 = $pdo->prepare("DELETE FROM tbl_payments WHERE sale_id=?");
$resultss_4 = $statementss_4->execute(array($sales_id));
 
$statementss_5 = $pdo->prepare("DELETE FROM tbl_sale_old_items WHERE sale_id=?");
$resultss_5 = $statementss_5->execute(array($sales_id));

$statementss_6 = $pdo->prepare("DELETE FROM tbl_sales_totals WHERE sale_id=?");
$resultss_6 = $statementss_6->execute(array($sales_id));


if($resultss_1 && $resultss_2 && $resultss_3 && $resultss_4 && $resultss_5){
    $message='Success..';
    $error='1';
}
else
{
    $message='Failed..';
    $error='0';
}


	$datas[] = array('message' => $message, 'error' => $error);                   
    echo $message;
}





















// Product Hard Delete Here - (Pending Delete Main Products)
if($_POST['type']=="product_delete"){
    
    
$statement = $pdo->prepare("DELETE FROM tbl_product_variants WHERE id=?");
$result = $statement->execute(array($_POST['delete_id']));

if($result==1)
{
    $message='Success..';
    $error='1';
}
else
{
    $message='Failed..';
    $error='0';
}


	$datas[] = array('message' => $message, 'error' => $error);                   
    echo $message;
}

// Barcode Items Delete & Products Delete Here
if($_POST['type']=="barcode_items_delete"){
    
    
$statement = $pdo->prepare("DELETE FROM tbl_barcode_items WHERE id=?");
$result = $statement->execute(array($_POST['delete_id']));

$statements = $pdo->prepare("DELETE FROM tbl_product_variants WHERE barcode_items_id=?");
$results = $statements->execute(array($_POST['delete_id']));

$statements = $pdo->prepare("DELETE FROM tbl_products WHERE barcode_items_id=?");
$results = $statements->execute(array($_POST['delete_id']));

if($result==1)
{
    $message='Success..';
    $error='1';
}
else
{
    $message='Failed..';
    $error='0';
}


	$datas[] = array('message' => $message, 'error' => $error);                   
    echo $message;
}





if($_POST['type']=="user_delete"){
$statement = $pdo->prepare("UPDATE tbl_user SET status=? WHERE id=?");
$result=$statement->execute(array(0,$_POST['delete_id']));
if($result==1)
	{
	    $message='Success..';
	    $error='1';
	}
	else
	{
	    $message='Failed..';
	    $error='0';
	}
		$datas[] = array('message' => $message, 'error' => $error);                   
        echo $message;
}


if($_POST['type']=="scheme_delete"){
$statement = $pdo->prepare("DELETE FROM tbl_customer_scheme WHERE id=?");
$result = $statement->execute(array($_POST['delete_id']));

$statements = $pdo->prepare("DELETE FROM tbl_payments WHERE scheme_id=? and accounts=?");
$results = $statements->execute(array($_POST['delete_id'], 3));

if($result==1 && $results==1)
	{
	    
	    $message='Success..';
	    $error='1';
	}
	else
	{
	    $message='Failed..';
	    $error='0';
	}
		$datas[] = array('message' => $message, 'error' => $error);                   
        echo $message;
}
// **************************************** Common Delete Starts Here ******************************************************** //


// Soft Delete Here
if($_POST['type']=="soft_delete"){
$statement = $pdo->prepare("UPDATE ".$_POST['table']." SET status=? WHERE id=?");
$result=$statement->execute(array(0, $_POST['delete_id']));
if($result==1)
	{
	    $message='Success..';
	    $error='1';
	}
	else
	{
	    $message='Failed..';
	    $error='0';
	}
		$datas[] = array('message' => $message, 'error' => $error);                   
        echo $message;
}

// Hard Delete Here
if($_POST['type']=="hard_delete"){
    $id=$_POST['delete_id'];
    $statement = $pdo->prepare("DELETE from ".$_POST['table']." WHERE id=$id");
    $result=$statement->execute();
if($result==1)
	{
	    $message='Success..';
	    $error='1';
	}
	else
	{
	    $message='Failed..';
	    $error='0';
	}
		$datas[] = array('message' => $message, 'error' => $error);                   
        echo $message;
}

if($_POST['type']=="dynamic_in_active"){
$statement = $pdo->prepare("UPDATE ".$_POST['table']." SET status=? WHERE id=?");
$result=$statement->execute(array(0,$_POST['delete_id']));
if($result==1)
	{
	    $message='Success..';
	    $error='1';
	}
	else
	{
	    $message='Failed..';
	    $error='0';
	}
		$datas[] = array('message' => $message, 'error' => $error);                   
        echo $message;
}

if($_POST['type']=="dynamic_active"){
$statement = $pdo->prepare("UPDATE ".$_POST['table']." SET status=? WHERE id=?");
$result=$statement->execute(array(1,$_POST['delete_id']));
if($result==1)
	{
	    $message='Success..';
	    $error='1';
	}
	else
	{
	    $message='Failed..';
	    $error='0';
	}
		$datas[] = array('message' => $message, 'error' => $error);                   
        echo $message;
}




// **************************************** Common Delete Endsss Here ******************************************************** //
?>