<?php
session_start();
include('accesscheck2.php');
require_once('Connections/courier.php');
require_once('functions/functions.php');

$action = $_POST["act"];
$id = $_POST['id'];
$user_id = $_SESSION['myMM_Userid'];
$user_name = $_SESSION['myMM_Fullname'];
$purchase_id = $_POST['purchase_order_id'];
$branch_id = $_SESSION['branch_id'];

if ($action == 'edit_special_orders_details') {
    $branch_type = mysql_fetch_assoc(mysql_query("SELECT type FROM branches WHERE id='$branch_id'"));
    if ($branch_type['type'] == 'branch') {
        $table = 'service_items';
        $item_id_use = 'id';
    } else {
        $table = 'stock_items';
        $item_id_use = 'item_id';
    }

    if ($id > 0) {
        $queryItems = "SELECT * FROM $table WHERE $item_id_use='$id'";
        $resultItems = mysql_query($queryItems) or die(mysql_error());
        $arrayItems = mysql_fetch_assoc($resultItems);
        $totalRows = mysql_num_rows($resultItems);

        $item_id = $arrayItems[$item_id_use];
        $ins_item_code = $arrayItems['ins_item_code'];
        $item = $arrayItems['item'];
        $service = $arrayItems['service'];
        $alternate_lookup = $arrayItems['alternate_lookup'];

            $item_qty = 1;
            $item_order_status = 'Added';

            $status = 'success';
            $message = strtoupper($item) . ' has been added to the list Successfully !';
            $pack_size_qty = 1;

            $query_insert = "INSERT INTO `special_order_details` (`item_id`, `ins_item_code`, `item`, `service`, `qty_expected`, `date_expected`, `item_order_status`, `user_id`) VALUES ('$item_id',  '$ins_item_code', '$item', '$service', '$item_qty', '$expected_date', '$item_order_status','".$_SESSION['myMM_Userid']."');";
            $rsinsert = mysql_query($query_insert, $courier) or die(mysql_error());
            $insert_id = mysql_insert_id();
    }

    echo $status . '@@@' . $message . '@@@';
    include('special_orders_items_added.php');
}


if ($action == 'del_special_order_details') {
    $query_insert = "delete from special_order_details where id='$id';";
    $rsinsert = mysql_query($query_insert, $courier) or die(mysql_error());
    include('special_orders_items_added.php');
}

if ($action == 'up_special_order_details') {
    $queryItems = "SELECT * FROM special_order_details WHERE id='$id' ";
    $resultItems = mysql_query($queryItems) or die(mysql_error());
    $arrayItems = mysql_fetch_assoc($resultItems);
    $totalRows = mysql_num_rows($resultItems);

    $branch_type = mysql_fetch_assoc(mysql_query("SELECT type FROM branches WHERE id='$branch_id'"));
    if ($branch_type['type'] == 'branch') {
        $table = 'service_items';
        $item_id_use = 'id';
    } else {
        $table = 'stock_items';
        $item_id_use = 'item_id';
    }

    $resultItems2 = mysql_query("SELECT item FROM $table WHERE $item_id_use='" . $arrayItems['item_id'] . "'") or die(mysql_error());
    $arrayItems2 = mysql_fetch_assoc($resultItems2);
    $item = $arrayItems2['item'];

    if ($totalRows > 0) {
        $item_qty = $arrayItems['qty_expected'] + 1;
        $total_claim = $item_qty * $arrayItems['item_unit_price'];

        $query_insert = "update special_order_details set qty_expected='$item_qty' where id='$id'";
        $rsinsert = mysql_query($query_insert, $courier) or die(mysql_error());

        $status = 'success';
        $message = "Quantity has been Increased from " . $arrayItems['qty_expected'] . " to $item_qty - " . strtoupper($item);
        echo $status . '@@@' . $message . '@@@';
    }
    include('special_orders_items_added.php');
}


if ($action == 'down_special_order_details') {
    $queryItems = "SELECT * FROM special_order_details WHERE id='$id' ";
    $resultItems = mysql_query($queryItems) or die(mysql_error());
    $arrayItems = mysql_fetch_assoc($resultItems);
    $totalRows = mysql_num_rows($resultItems);
    
     $branch_type = mysql_fetch_assoc(mysql_query("SELECT type FROM branches WHERE id='$branch_id'"));
    if ($branch_type['type'] == 'branch') {
        $table = 'service_items';
        $item_id_use = 'id';
    } else {
        $table = 'stock_items';
        $item_id_use = 'item_id';
    }

    $resultItems2 = mysql_query("SELECT item FROM $table WHERE $item_id_use='" . $arrayItems['item_id'] . "'") or die(mysql_error());
    $arrayItems2 = mysql_fetch_assoc($resultItems2);
    $item = $arrayItems2['item'];

    if (($totalRows > 0) && ($arrayItems['qty_expected'] > 1)) {
        $item_qty = $arrayItems['qty_expected'] - 1;
        $total_claim = $item_qty * $arrayItems['item_unit_price'];

        $query_insert = "update special_order_details set qty_expected='$item_qty' where id='$id'";
        $rsinsert = mysql_query($query_insert, $courier) or die(mysql_error());

        $status = 'success';
        $message = "Quantity has been Decreased from " . $arrayItems['qty_expected'] . " to $item_qty - " . strtoupper($item);
        echo $status . '@@@' . $message . '@@@';
    }
    include('special_orders_items_added.php');
}

if ($action == 'updateqty_special_order_details') {
    $value = $_POST["value"];
    $queryItems = "SELECT * FROM special_order_details WHERE id='$id' ";
    $resultItems = mysql_query($queryItems) or die(mysql_error());
    $arrayItems = mysql_fetch_assoc($resultItems);
    $totalRows = mysql_num_rows($resultItems);
    $unit_price = $arrayItems['qty_expected'];
    
    $branch_type = mysql_fetch_assoc(mysql_query("SELECT type FROM branches WHERE id='$branch_id'"));
    if ($branch_type['type'] == 'branch') {
        $table = 'service_items';
        $item_id_use = 'id';
    } else {
        $table = 'stock_items';
        $item_id_use = 'item_id';
    }

    $resultItems2 = mysql_query("SELECT item FROM $table WHERE $item_id_use='" . $arrayItems['item_id'] . "'") or die(mysql_error());
    $arrayItems2 = mysql_fetch_assoc($resultItems2);
    $item = $arrayItems2['item'];

    if ($totalRows > 0) {
        $item_qty = $value;
        $total_claim = $item_qty * $arrayItems['item_unit_price'];
        $query_insert = "update special_order_details set qty_expected='$item_qty' where id='$id'";
        $rsinsert = mysql_query($query_insert, $courier) or die(mysql_error());

        if ($value > $unit_price) {
            $status = 'success';
            $message = "Quantity has been Increased from $unit_price to $value - " . strtoupper($item);
        } else if ($value < $unit_price) {
            $status = 'success';
            $message = "Quantity has been Decreased from $unit_price to $value - " . strtoupper($item);
        }
        echo $status . '@@@' . $message . '@@@';
    }
    include('special_orders_items_added.php');
}


if ($action == 'create_special_orders') {
    include 'edit_special_orders.php';
}