<?php
include('accesscheck2.php');
require_once('Connections/courier.php');
require_once 'functions/functions.php';
mysql_select_db($database_courier, $courier);

if (!function_exists("GetSQLValueString")) {

    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
        if (PHP_VERSION < 6) {
            $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
        }

        $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

        switch ($theType) {
            case "text":
                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                break;
            case "long":
            case "int":
                $theValue = ($theValue != "") ? intval($theValue) : "NULL";
                break;
            case "double":
                $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
                break;
            case "date":
                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                break;
            case "defined":
                $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
                break;
        }
        return $theValue;
    }

}

if(empty($id)){
    $id = $_POST['id'];
}
$sql = mysql_query("SELECT *, date_format(date_of_order,'%a, %e %M %Y') as purchase_order_date, date_format(expected_date,'%e %M %Y') as purchase_expected_date FROM requisition_orders WHERE purchase_order_no = '$id'", $courier) or die(mysql_error());
$patient_row = mysql_fetch_assoc($sql);?>


<div class="page-bar">
    <ul class="page-breadcrumb">
        <li>
            <i class="icon-home"></i>
            <a href="index.html">Home</a>
            <i class="fa fa-angle-right"></i>
        </li>
        <li>
            <span>Dashboard</span>
        </li>
    </ul>
    <div class="page-toolbar">
        <div class="btn-group pull-right">
            <button type="button" class="btn btn-fit-height grey-salt dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="1000" data-close-others="true"> Actions
                <i class="fa fa-angle-down"></i>
            </button>
            <ul class="dropdown-menu pull-right" role="menu">
                <li onclick="getstep('edit_requisition_order','<?php echo $id; ?>');">
                    <a href="#">
                        <i class="icon-refresh"></i> Refresh
                    </a>
                </li>
                <li>
                    <a href="#" onclick="delInformtion('delete_requisition_order', '#currentarea', '<?php echo $id; ?>', 'Requisition Order No <?php echo $id; ?> ?', 'You are about to Delete Requisition Order No. <?php echo $id; ?> from the List of Requisition Orders.', 'Requisition Order No. <?php echo $id; ?> Deleted Successfully !');"> <i id="trash_<?php echo $id; ?>" class="icon-trash" style="color:red;"></i> <i style="display: none;" id="spin_refresh_<?php echo $id; ?>" class="fa fa-refresh fa-spin fa-fw"></i> Delete Requisition Order </a>
                </li>
                <li onclick="getstep('list_requisition_orders');">
                    <a href="#">
                        <i class="fa fa-search"></i> Search Requisition Orders
                    </a>
                </li>
            </ul>
        </div>
    </div>
</div>

<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">  Requisition Order Details for Order No: <?php echo $patient_row['purchase_order_no']; ?></span>
                </div>
                <div class="actions">
                    <?php if($patient_row['order_status_from'] == 'Awaiting'){ ?>
                    <a class="btn green" onclick="saveInformation2('save_requisition_order','<?php echo $patient_row['purchase_order_no']; ?>','form#save_requisition_order','#currentarea');return false;"> Send Requisition Order <i class="fa fa-check"></i> </a>
                    <?php } 
                    if($patient_row['order_status_from'] == 'Unapproved' && $_SESSION['branch_id']==$patient_row['delivery_location']){?>
                       <a class="btn green" onclick="saveInformation2('approve_requisition_order_from','<?php echo $patient_row['purchase_order_no']; ?>','form#save_requisition_order','#currentarea');return false;"> Approve Requisition Order <i class="fa fa-check"></i> </a>
                    <?php } ?>
                </div>
            </div>
            <div class="portlet-body form">
                <div class="row"  >
                    <form role="form" action="#" class="form-horizontal" id="save_requisition_order">
                            <div class="form-body">
                    <div class="col-lg-12 col-md-12 col-xs-12 col-sm-12">
                        <div class="col-md-6">
                            <div class="row">
                                <div class="portlet light ">
                                    <div class="portlet-title">
                                        <div class="caption">
                                            <span class="caption-subject font-dark"> Branch Information </span>
                                        </div>
                                    </div>
                                    <div class="portlet-body form">
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Branch Name</span></label>
                                            <div class="col-md-7"> 
                                                <select name="supplier" id="supplier" class="form-control" onChange="process_information('supplier_address_requisition',this.value,'','supplier_address');">
                                  <option value="">Please Select</option>
                                  <?php
								  $rsodest = mysql_query("SELECT * FROM branches WHERE id <> '".$_SESSION['branch_id']."'");
								  $row_rsodest = mysql_fetch_assoc($rsodest);
								  do{?>
                                  <option value="<?php echo $row_rsodest['id']; ?>" <?php if($row_rsodest['id']==$patient_row['supplier']){echo "selected";}?>><?php echo strtoupper($row_rsodest['branch_name']); ?></option>
                                  <?php } while ($row_rsodest = mysql_fetch_assoc($rsodest)); ?>
                                  </select>
                                            </div>
                                        </div>
                                        <!-- <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Supplier's Address</span></label>
                                            <div class="col-md-7"> <textarea name="supplier_address" rows="2" id="supplier_address" class="form-control"><?php echo $patient_row['supplier_address']; ?></textarea> </div>
                                        </div> -->
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Memo</span></label>
                                            <div class="col-md-7"> <input name="memo" type="text" id="memo" class="form-control" value="<?php echo $patient_row['memo']; ?>"> </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="row">
                                <div class="portlet light ">
                                    <div class="portlet-title">
                                        <div class="caption">
                                            <span class="caption-subject font-dark"> Branch Information </span>
                                        </div>
                                    </div>
                                    <div class="portlet-body form">
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Deliver To</span></label>
                                            <div class="col-md-7"> 
                                                <select name="delivery_location" id="delivery_location" class="form-control" onChange="process_information('delivery_address_requisition',this.value,'','delivery_address');">
                              <option value="">Please Select</option>
                              <?php 
								  $rsodest = mysql_query("SELECT * FROM branches WHERE id='".$_SESSION['branch_id']."' order by branch_name");
								  $row_rsodest = mysql_fetch_assoc($rsodest);
								  do{?>
                              <option select value="<?php echo $row_rsodest['id']; ?>" <?php if($row_rsodest['id']==$patient_row['delivery_location']){echo "selected";}?>><?php echo strtoupper($row_rsodest['branch_name']); ?></option>
                              <?php } while ($row_rsodest = mysql_fetch_assoc($rsodest)); ?>
                              </select>
                                            </label>
                                        </div>
                                        </div>
                                        <!-- <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Delivery Address</span></label>
                                            <div class="col-md-7"> <textarea name="delivery_address" id="delivery_address" class="form-control"><?php echo $patient_row['delivery_address']; ?></textarea> </div>
                                        </div> -->
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Due Date</span></label>
                                            <div class="col-md-7">  <input name="expected_date" type="text" id="expected_date" class="form-control date_time_picker" value="<?php echo $patient_row['expected_date']; ?>" readonly> </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                      </form>
                     </div>
            </div>
        </div>
    </div>
</div>

<form name="form" id="" action="">
<div class="row">
    <div class="col-lg-12 col-md-12 col-xs-12 col-sm-12" style="margin-top:-20px;">
        <div class="col-md-5">
            <div class="row">
                <div class="portlet light">
                    <div class="portlet-title">
                        <div class="caption">
                            <input type="text" name="searchtext_query_item_name" onkeyup="MainsearchItemsName('search_items_requisition_orders','#view_items_response_wrapper',this.value);" class="form-control input-circle" placeholder="Item Name">
                            <input type="hidden" name="purchase_order_id" value="<?php echo $id; ?>">
                        </div>
                        <div class="actions">
                            <select name="search_by" id="search_by" class="form-control input-circle">
                                <option value="contains">Contains</option>
                                <option value="begins">Begins</option>
                            </select>
                        </div>
                    </div>
                    <div class="portlet-body" id="">
                        <div style="height: 300px;overflow-y: scroll;overflow-x: hidden;">
                            <div class="" id="view_items_response_wrapper"> 
                                <?php include 'requisition_order_search_items.php'; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-md-7" id="view_items_response_wrapper_2">
            <?php 
            $purchase_id = $patient_row['purchase_order_no'];
            include 'list_of_added_items.php'; ?>
        </div>
    </div>
</div>
</form>