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

$action=$_POST["act"];
$id = $_POST['id'];

$supplier = $_POST['supplier'];
$supplier_address = $_POST['supplier_address'];
$memo = $_POST['memo'];
$delivery_location = $_POST['delivery_location'];
$delivery_address = $_POST['delivery_address'];
$expected_date = $_POST['expected_date'];
$user_id = $_SESSION['myMM_Userid'];
$user_name = $_SESSION['myMM_Fullname'];

if ($action == 'update_items_details_lab') {
    $idd = $_POST['id'];
    list($id, $job_nos, $job_int) = explode("_",$_POST['id']);
    $job_number = $job_nos.'/'.$job_int;

    $provider_id = $_POST['provider_id'];
    $ins_company = $_POST['ins_company'];
    $comment = $_POST['comment_' . $idd];
    $total = $_POST['total'];
    $current_time = gmdate('Y-m-d h:i:s');
    $audit_status = $_POST['audit_status_' . $idd];
    $audit_status_other = $_POST['audit_status_other_' . $idd];
    $branch_id = $_POST['branch_id_' . $idd];

    $check = mysql_num_rows(mysql_query("SELECT id FROM job_activities_status WHERE process_claim_no='$id' AND job_number='$job_number' AND job_status='$audit_status' AND arrival='$branch_id' LIMIT 1"));
//    if ((($audit_status == 'Job Received' || $audit_status == 'Job In-Progress') && $check > 0)) {
//    } else {
        $update = "update job_numbers set front_desk_date=now(), audit_status = '$audit_status', audit_status_other='$audit_status_other', status_location_id='$branch_id' where process_claim_no='$id' AND job_number='$job_number';";
        $update = $update . "update claims_details set audit_status = '$audit_status' where process_claim_no='$id' and provider_id='$provider_id';";

        $departure_info = mysql_fetch_assoc(mysql_query("SELECT * FROM job_activities_status WHERE process_claim_no='$id' AND job_number='$job_number' ORDER BY id DESC LIMIT 1"));
        if ($departure_info['job_status'] == 'Job In-Transit' && ($audit_status == 'Job Received' || $audit_status == 'Job In-Progress')) {
            mysql_query("UPDATE job_activities_status SET arrival_time=now() WHERE id='" . $departure_info['id'] . "'");
        }

        if ($audit_status == 'Job In-Transit') {
            mysql_query("INSERT INTO job_activities_status (branch_id,process_claim_no,job_number,departure,arrival,departure_time,job_status,job_status_other,user_id,previous_id) VALUES('".$_SESSION['branch_id'] . "','$id','$job_number','" . $_SESSION['branch_id'] . "','$branch_id',now(),'$audit_status','$audit_status_other','" . $user_id . "','" . $departure_info['id'] . "')");
            mysql_query("UPDATE job_activities_status SET status='closed' WHERE id='".$departure_info['id']."'");
        } else {
            mysql_query("INSERT INTO job_activities_status (branch_id,process_claim_no,job_number,departure,arrival,arrival_time,job_status,user_id,previous_id) VALUES('".$_SESSION['branch_id'] . "','$id','$job_number','" . $departure_info['departure'] . "','$branch_id',now(),'$audit_status','" . $user_id . "','" . $departure_info['id'] . "')");
            mysql_query("UPDATE job_activities_status SET status='closed' WHERE id='".$departure_info['id']."'");
        }
//    }

    for ($x = 1; $x <= $total; $x++) {
        $itemid22 = $_POST['chkquery_' . $id . '_' . $x];
        $itemdecisionstatus[$x] = $_POST["itemdecisionstatus_" . $id . "_" . $x];
        $itemreason[$x] = $_POST["decision_reason_" . $id . "_" . $x];
        $update = $update . 'update claims_details set decision_status="' . $itemdecisionstatus[$x] . '", decision_reason="' . $itemreason[$x] . '" where id="' . $itemid22 . '";';
    }

    if ($comment) {
        $update = $update . "INSERT INTO `claims_details_comments` (`id`, `provider_id`, `claimno`, `job_number` `comment`, `date_updated`, `user_id`) VALUES (NULL, '$provider_id', '$id','$job_number', '$comment', '$current_time', '" . $_SESSION['myMM_Userid'] . "');";
    }
    require('Connections/couriermysqli_local.php');
    $query_rsodest = $update;
    $rsodest = mysqli_multi_query($couriermysqli_local, $query_rsodest);
    mysqli_close($couriermysqli_local);
}


if ($action == 'save_new_supplier') {
    $supplier_name = $_POST['supplier_name'];
    $supplier_address = $_POST['address'];
    $query_insert = "INSERT INTO suppliers(supplier,supplier_address) VALUES('$supplier_name','$supplier_address')";
    $rsinsert = mysql_query($query_insert, $courier) or die(mysql_error());

    if ($rsinsert == TRUE) {
        echo 'Success! New Supplier Saved Successfully.@@@success@@@';
    } else {
        echo 'Oops! Failed to Save New Supplier.@@@error@@@';
    }
    include('list_of_suppliers.php');
}

if($action == 'edit_supplier_details'){ 
    $row = mysql_fetch_assoc(mysql_query("SELECT * FROM suppliers WHERE id = '$id'")); ?>
    <div class="row">
        <div class="col-lg-12 col-md-12 col-xs-12 col-sm-12">
            <div class="portlet light ">
                <div class="portlet-title">
                    <div class="caption">
                        <span class="caption-subject bold uppercase font-dark">SUPPLIERS DETAILS</span>
                    </div>
                </div>
                <div class="portlet-body form">
                    <form role="form" action="#" id="edit_supplier_details_form" class="form-horizontal">
                        <div class="form-body">
                            <div class="row" id="investment_plan_wrapper">    
                                <div class="col-lg-12 col-xs-12 col-md-12">
                                    <div class="form-group">
                                        <label class="col-md-3 control-label"><span class="pull-left">Supplier's Name</span></label>
                                        <div class="col-md-7">
                                            <input name="supplier_name" id="supplier_name" type="text" class="form-control" value="<?php echo $row['supplier'];?>">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-3 control-label"><span class="pull-left">Address/Phone/Email</span></label>
                                        <div class="col-md-7">
                                            <textarea name="address" id="address" rows="3" class="form-control"><?php echo $row['supplier_address']; ?></textarea>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-actions right">
                            <a class="btn default"> <i class="fa fa-refresh"></i> Reset </a>
                            <a class="btn green" onclick="saveInformation2('update_supplier_details','<?php echo $id; ?>','form#edit_supplier_details_form','#currentarea');return false;"> Update Supplier <i class="fa fa-check"></i> </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
<?php
}

if($action == 'update_supplier_details'){
    $supplier_name = $_POST['supplier_name'];
    $supplier_address = $_POST['address'];
    $rsupdate_records = mysql_query("UPDATE suppliers SET supplier='$supplier_name',supplier_address='$supplier_address' WHERE id = '$id'");
    if ($rsupdate_records == TRUE) {
        echo 'Success! Supplier Details Updated.@@@success@@@';
    } else {
        echo 'Oops! Failed to Update Supplier Details.@@@error@@@';
    }
    include('list_of_suppliers.php');
}

$purchase_id=$_SESSION['purchase_order_id'];

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

if($totalRows>0){
$id=$arrayItems['id'];	
}else{
$id='';	
}
	}	
	 
	if($id>0){
	
$query_insert="UPDATE purchase_orders set supplier='$supplier', supplier_address='$supplier_address', delivery_location='$delivery_location', delivery_address='$delivery_address', expected_date='$expected_date', memo='$memo', supplier_opening_balance='$supplier_opening_balance', user_id='$user_id' ,user_name='$user_name' where purchase_order_no='$purchase_id';";
//echo $query_insert;
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());

	

$queryItems = "SELECT * FROM service_items WHERE id='$id'";
$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);
$totalRows = mysql_num_rows($resultItems);

$item_id=$arrayItems['id'];
$ins_item_code=$arrayItems['ins_item_code'];
$item=$arrayItems['item'];
$service=$arrayItems['service'];
$alternate_lookup=$arrayItems['alternate_lookup'];
$cost_price_before_tax=$arrayItems['purchase_price'];
//$total_price=1*$cost_price_before_tax;
//$total_award='';

$queryItems = "SELECT * FROM purchase_order_details WHERE purchase_order_no='$purchase_id' and item_id='$item_id' and pack_size=1";
//echo $queryItems;
$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);
$totalRows = mysql_num_rows($resultItems);




if($totalRows>0){
$item_qty=$arrayItems['qty_expected']+1;
$total_price=$item_qty*$cost_price_before_tax;	
	
$query_insert="update purchase_order_details set qty_expected='$item_qty' where purchase_order_no='$purchase_id' and item_id='$id'";	
//echo $query_insert;
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	
}else{
	$item_qty=1;
	$item_order_status='Outstanding';
	
	$queryItems = "SELECT * FROM pack_sizes WHERE item_id='$item_id' order by pack_size_qty";
$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);
$totalRows = mysql_num_rows($resultItems);

//if($totalRows>0){
//$cost_price_before_tax=$arrayItems['pack_size_price'];
$pack_size_qty=$arrayItems['pack_size_qty'];
//}else{
//$cost_price_before_tax=$arrayItems['pack_size_price'];
$pack_size_qty=1;	
//}
	
	
	$query_insert = "INSERT INTO `purchase_order_details` (`id`, `purchase_order_no`, `item_id`, `ins_item_code`, `item`, `service`, `qty_expected`, `qty_received`, `cost_price_before_tax`, `date_expected`, `datetime_received`, `item_order_status`, `pack_size`) VALUES (NULL, '$purchase_id', '$item_id',  '$ins_item_code', '$item', '$service', '$item_qty', '$qty_received', '$cost_price_before_tax', '$expected_date', '$datetime_received', '$item_order_status', '$pack_size_qty');";
	//echo $query_insert;
	//exit;	
	$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
}



}

}////end edit_purchase_details

if($action=='del_purchase_order_details'){
$query_insert = "delete from purchase_order_details where id='$id';";
	$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());	
}

if($action=='up_purchase_order_details'){

$queryItems = "SELECT * FROM purchase_order_details WHERE id='$id' ";
$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);
$totalRows = mysql_num_rows($resultItems);




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


if($action=='down_purchase_order_details'){

$queryItems = "SELECT * FROM purchase_order_details WHERE id='$id' ";
$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);
$totalRows = mysql_num_rows($resultItems);




if(($totalRows>0) && ($arrayItems['qty_expected']>1)){
$item_qty=$arrayItems['qty_expected']-1;
$total_claim=$item_qty*$arrayItems['item_unit_price'];	
	
$query_insert="update purchase_order_details set qty_expected='$item_qty' where id='$id'";
//echo $query_insert;	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	
}
}

if($action=='updateqty_purchase_order_details'){
$totalqty = $_POST["itemqty"];
//echo $totalqty;
$queryItems = "SELECT * FROM purchase_order_details WHERE id='$id' ";
$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);
$totalRows = mysql_num_rows($resultItems);




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


if($action=='updateprice_purchase_order_details'){
$totalqty = $_POST["itemqty"];
//echo $totalqty;
$queryItems = "SELECT * FROM purchase_order_details WHERE id='$id' ";
$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);
$totalRows = mysql_num_rows($resultItems);




if($totalRows>0){
$item_qty=$totalqty;
$total_claim=$item_qty*$arrayItems['qty_expected'];	
	
$query_insert="update purchase_order_details set cost_price_before_tax='$item_qty' where id='$id'";
//echo $query_insert;	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	
}
}


if($action=='updatepacksize_purchase_order_details'){
$totalqty = $_POST["itemqty"];
//echo $totalqty;
$queryItems = "SELECT * FROM purchase_order_details WHERE id='$id' ";
$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);
$totalRows = mysql_num_rows($resultItems);

$item_id=$arrayItems['item_id'];






if($totalRows>0){
$item_qty=$totalqty;

$queryItems = "SELECT * FROM pack_sizes WHERE item_id='$item_id' and pack_size_qty='$item_qty' ";
$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);
$totalRows = mysql_num_rows($resultItems);

$packprice=$arrayItems['pack_size_price'];
	
$query_insert="update purchase_order_details set pack_size='$item_qty', cost_price_before_tax='$packprice'  where id='$id'";
//echo $query_insert;	
$rsinsert=mysql_query($query_insert, $courier);
echo mysql_error();
	
}
}


/*$query_invoice="select * from purchase_order_details where purchase_order_no='$purchase_id'";
$rsinovice=mysql_query($query_invoice, $courier) or die(mysql_error());
$row_rsinovice = mysql_fetch_assoc($rsinovice);
$totalRows_rsinovice = mysql_num_rows($rsinovice);*/


?>

 <?php include('view_items_purchase.php'); ?>

