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


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;
    }

}

mysql_select_db($database_courier, $courier);
$id = $_POST['id'];
//echo"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'";
$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);
$currency = 'GHS';

$branch_type = mysql_fetch_assoc(mysql_query("SELECT type FROM branches WHERE id='".$_SESSION['branch_id_user']."'"));
$supply_req_orders = getMenuPermission('Supply Requisition Orders');
?>


<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('<?php echo $action; ?>','<?php echo $id; ?>');">
                    <a href="#">
                        <i class="icon-refresh"></i> Refresh
                    </a>
                </li>
                <?php if($patient_row['order_status'] == 'Not Saved' || empty($patient_row['order_status']) ||$patient_row['order_status'] == 'Unapproved') { ?>
                <li onclick="getstep('edit_requisition_order','<?php echo $id; ?>');">
                    <a href="#">
                        <i class="fa fa-edit"></i> Edit Requisition Order
                    </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>
                <?php } ?>
                <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 </span>
                </div>
                <div class="actions uppercase">
                    <strong>Order Date:</strong> <span style="color:red; font-weight:bold;"><?php echo $patient_row['purchase_order_date']; ?></span> <b style="color:green;"> [ REQUISITION NO : <?php echo $patient_row['purchase_order_no'];?> ]</b>
                 <?php if($patient_row['order_status']=='In-Transit' && $patient_row['delivery_location']==$_SESSION['branch_id_user']){?>
                    &nbsp; <a class="btn green" onclick="receiveSupplyQuantity('accept_supply_requisition_order', '#currentarea', '<?php echo $id; ?>', 'Yes, Receive Order Now!', 'Receive Requisition Order ?', 'By Accepting/Receiving Requisition Order No. <?php echo $id; ?>, This means you have received and counted the Quantities Supplied and everything is accurate. Please Note Items rejected would be disregarded but items accepted Quantities will Increaded immediatley after this action', 'form#save_requisition_supply');return false;"> Receive Requisition Order <i class="fa fa-check"></i> </a>
                     <?php } ?>
                 <?php
                 if($supply_req_orders == true){
                 if($patient_row['order_status']=='Approved' && $patient_row['order_status_from']=='Approved' && $patient_row['supplier']==$_SESSION['branch_id_user']){?>
                    &nbsp; <a class="btn green" onclick="receiveSupplyQuantity('save_supply_requisition_order', '#currentarea', '<?php echo $id; ?>', 'Yes, Supply Order Now!', 'Supply Requisition Order ?', 'You are about to Supply Requisition Order No. <?php echo $id; ?> from the List of approved Requisition Orders. Please Note that your Quantity Available will reduced immediatley after this action', 'form#save_requisition_supply');return false;"> Supply Requisition Order <i class="fa fa-check"></i> </a>
                     <?php } ?>
                 <?php if($patient_row['order_status_from'] == 'Approved' && $patient_row['order_status'] == 'Awaiting' && $patient_row['supplier']==$_SESSION['branch_id_user']){?>
                    &nbsp; <a class="btn green" onclick="getstep('supply_requisition_order_page','<?php echo $id; ?>');"> Go to Supply Page <i class="fa fa-check"></i> </a>
                     <?php }

                     if($patient_row['order_status_from'] == 'Approved' && $patient_row['order_status'] == 'Unapproved' && $patient_row['supplier']==$_SESSION['branch_id']){
                         echo "abasa";
                         ?>
                    &nbsp; <a class="btn green" onclick="saveInformation2('approve_requisition_order_supply','<?php echo $id; ?>','form#save_requisition_order','#currentarea');return false;"> Approve Supply <i class="fa fa-check"></i> </a>
                     <?php }


                     } ?>
                 <?php if($patient_row['order_status_from'] == 'Unapproved' && $patient_row['delivery_location']==$_SESSION['branch_id_user']){?>
                    &nbsp; <a class="btn green" onclick="getstep('edit_requisition_order','<?php echo $id; ?>');"> Go to Approval Page <i class="fa fa-check"></i> </a>
                     <?php } ?>
                </div>
            </div>
            <div class="portlet-body form">
                <div class="row"  >
                    <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"> Supplier's Information </span>
                                        </div>
                                    </div>
                                    <div class="portlet-body form">
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Supplier's Name</span></label>
                                            <label class="col-md-7 control-label bold"> 
                                                <?php $supplier_id=$patient_row['supplier'];
                                                      $rsodest_supplier = mysql_query("SELECT * FROM branches where id='$supplier_id'");
                                                      $row_rsodest_supplier = mysql_fetch_assoc($rsodest_supplier);
                                                      if(empty($row_rsodest_supplier['branch_name'])){ echo '&nbsp;'; } echo $row_rsodest_supplier['branch_name'];
                                                ?>
                                            </label>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Supplier's Address</span></label>
                                            <label class="col-md-7 control-label bold"> <?php if(empty($patient_row['supplier_address'])){ echo '&nbsp;'; } echo $patient_row['supplier_address']; ?> </label>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Memo</span></label>
                                            <label class="col-md-7 control-label bold"> <?php if(empty($patient_row['memo'])){ echo '&nbsp;'; } echo $patient_row['memo']; ?> </label>
                                        </div>
                                         <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Approved By </span> </label>
                                            <label class="col-md-7 control-label bold">  <?php if(empty($patient_row['approved_by_user_name'])){ echo '&nbsp;'; } echo $patient_row['approved_by_user_name']; ?> </label>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Supplied By </span> </label>
                                            <label class="col-md-7 control-label bold"><?php if(empty($patient_row['supplied_by_user_name'])){ echo '&nbsp;'; } echo $patient_row['supplied_by_user_name']; ?> </label>
                                        </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"> Delivery 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>
                                            <label class="col-md-7 control-label bold"> 
                                             <?php 
                                                  $store_id=$patient_row['delivery_location'];
                                                  $rsodest_supplier = mysql_query("SELECT * FROM branches where id='$store_id'");
                                                  $row_rsodest_supplier = mysql_fetch_assoc($rsodest_supplier);
                                                 if(empty($row_rsodest_supplier['branch_name'])){ echo '&nbsp;'; } echo $row_rsodest_supplier['branch_name'];
                                               ?>
                                            </label>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Delivery Address</span></label>
                                            <label class="col-md-7 control-label bold"> <?php if(empty($patient_row['delivery_address'])){ echo '&nbsp;'; } echo $patient_row['delivery_address']; ?> </label>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Due Date</span></label>
                                            <label class="col-md-7 control-label bold"> <?php if(empty($patient_row['purchase_expected_date'])){ echo '&nbsp;'; } echo $patient_row['purchase_expected_date']; ?> </label>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Requisition Raised By</span></label>
                                            <label class="col-md-7 control-label bold"> <?php if(empty($patient_row['user_name'])){ echo '&nbsp;'; } echo $patient_row['user_name']; ?> </label>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Approved By</span></label>
                                            <label class="col-md-7 control-label bold"> <?php if(empty($patient_row['approved_by_user_name_from'])){ echo '&nbsp;'; } echo $patient_row['approved_by_user_name_from']; ?> </label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

 <?php
     $purchase_id = $patient_row['purchase_order_no'];
    $query_claim = "select * from requisition_order_details where purchase_order_no='$purchase_id' order by id desc;";
    $rsclaim = mysql_query($query_claim, $courier) or die(mysql_error());
    $row_rsclaim = mysql_fetch_assoc($rsclaim);
    $totalRows_rsclaim = mysql_num_rows($rsclaim);

    $query_claim_total = "select sum(qty_expected*cost_price_before_tax) as totalprice from requisition_order_details where purchase_order_no='$purchase_id';";
    $rsclaim_total = mysql_query($query_claim_total, $courier) or die(mysql_error());
    $row_rsclaim_total = mysql_fetch_assoc($rsclaim_total);
    $totalRows_rsclaim_total = mysql_num_rows($rsclaim_total);
    ?>

<div class="row">
    <div class="col-lg-12 col-md-12 col-xs-12 col-sm-12">
        <div class="portlet light " style="margin-top: -20px;">
            <div class="portlet-title">
                <div class="caption">
                    <span class="caption-subject bold uppercase font-dark"> LIST OF ITEMS </span>
                </div>
                <div class="actions uppercase bold">
                          <b style="color:<?php if($patient_row['order_status_from']=='Unapproved'){echo 'red;';}else{ echo 'green;';} ?>;">
                           <?php if(!empty($patient_row['order_status_from'])) { echo $patient_row['order_status_from'].' <sup> BY ORDERING BRANCH</sup>';} ?> </b>
                           <b style="color:<?php if($patient_row['order_status']=='Unapproved'||$patient_row['order_status']=='Awaiting'||$patient_row['order_status_from']=='Accepted With Rejections'){echo 'red;';}else{ echo 'green;';} ?>;">
                               <?php if(!empty($patient_row['order_status'])) { echo ' | '. $patient_row['order_status'].' <sup> BY SUPPLYING BRANCH</sup>';} ?> 
                           </b>
                </div>
            </div>
            <div class="portlet-body form">
                <div class="row">
                    <form name="save_requisition_supply" id="save_requisition_supply" action="">
                        <input type="hidden" name="total_items" id="total_items" value="<?php echo $totalRows_rsclaim; ?>">
                    <div class="col-lg-12 col-md-12 col-xs-12 col-sm-12">
                        <table width="100%" border="0" align="center" cellpadding="1" cellspacing="1" class="table table-bordered">
                                                <thead>
                                                    <tr>
                                                    <th class="count">Item</th>
                                                    <th style="text-align: right"> Qty Available </th>
                                                    <th style="text-align: right;">Qty Requested</th>
                                                    <th style="text-align: right;">Qty <?php if($patient_row['order_status']=='In-Transit'){echo 'Supplied';}else{ echo' to be Supplied';}?></th>
                                                     <?php if(($patient_row['order_status']=='In-Transit'||$patient_row['order_status_from']=='Accepted With No Rejections'||$patient_row['order_status_from']=='Accepted With Rejections') && $patient_row['delivery_location']==$_SESSION['branch_id_user']){?>
                                                    <th>Action Taken</th>
                                                    <th>Reason</th>
                                                     <?php } ?>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                <?php if ($totalRows_rsclaim> 0) { ?>   
                                                    <?php
                                                    do {
                                                        $i = $i + 1;
                                                        ?>
                                                        <tr>
                                                            <td><?php echo strtoupper($row_rsclaim['item']); ?></td>
                                                            <td style="text-align: right;color: green;font-weight: bold;">
                        <?php 
                            if ($branch_type['type'] == 'branch') {
                                    echo quantityAvailable($row_rsclaim['item_id']);
                                } else {
                                    echo quantityAvailable2($row_rsclaim['item_id']);
                                }
                             ?>
                    </td>
                    <td style="text-align: right;"><?php echo $row_rsclaim['qty_expected']; ?></td>
                    <td style="text-align: right;"><?php echo $row_rsclaim['qty_received']; ?></td>
                    
                    <?php if(($patient_row['order_status']=='In-Transit'||$patient_row['order_status_from']=='Accepted With No Rejections'||$patient_row['order_status_from']=='Accepted With Rejections') && $patient_row['delivery_location']==$_SESSION['branch_id_user']){?>
                    <td>
                          <?php if($patient_row['order_status']=='In-Transit'){ ?>
                        <select onchange="if(this.value == 'Rejected'){$('#reason_<?php echo $purchase_id.'_'.$i;?>').show();}else{$('#reason_<?php echo $purchase_id.'_'.$i;?>').hide();}" class="form-control" name="action_taken_<?php echo $purchase_id.'_'.$i;?>" id="action_taken_<?php echo $purchase_id.'_'.$i;?>">
                            <option value="Accepted">Accept</option>
                            <option value="Rejected">Reject</option>
                        </select>
                          <?php }else{ ?>
                        <span style="color: <?php if($row_rsclaim['item_order_status']=='Rejected'){echo 'red;';} ?>"> <?php echo  $row_rsclaim['item_order_status']; ?></span>
                         <?php } ?>
                    </td>
                    <td>
                         <?php if($patient_row['order_status']=='In-Transit'){ ?>
                        <input style="display: none; " type="text" name="reason_<?php echo $purchase_id.'_'.$i;?>" id="reason_<?php echo $purchase_id.'_'.$i;?>" class="form-control" placeholder="Reason">
                        <input type="hidden" name="item_details_id_<?php echo $purchase_id.'_'.$i;?>" id="item_details_id_<?php echo $purchase_id.'_'.$i;?>" value="<?php echo $row_rsclaim['id'];?>">
                         <?php }else{
                             echo $row_rsclaim['reason'];
                         } ?>
                    </td>
                     <?php } ?>
                    </tr>
                  <?php }  while ($row_rsclaim = mysql_fetch_assoc($rsclaim)); ?>
                  <?php }else{echo '<tr><td colspan="7" style="color:red;"><div class="none">No Items Found. Search Items to Add to Purchase Order.</div></td></tr>';}?>
                  </tbody>
                </table> 
                    </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>