<?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 FROM transfer_orders WHERE purchase_order_no = '$id'";
$sql = mysql_query("SELECT *, date_format(date_of_order,'%a, %e %M %Y') as purchase_order_date FROM transfer_orders WHERE purchase_order_no = '$id'", $courier) or die(mysql_error());
$order_detail_row = mysql_fetch_assoc($sql);
$currency = 'GHS';

// $branch_type_info = mysql_fetch_assoc(mysql_query("SELECT type FROM branches WHERE id='".$_SESSION['branch_id']."'"));
$branch_type = 'store';
$supply_transfer_order = getMenuPermission('Supply Transfer Orders');
 $received_transfer_orders = getMenuPermission('Receive Transfer Order');
$see_quantity_avail_store = getMenuPermission('Can View Store Quantity Available');
?>


<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($order_detail_row['order_status'] == 'Not Saved'||$order_detail_row['order_status'] == 'Unapproved') { ?>
                <li onclick="getstep('edit_transfer_order','<?php echo $id; ?>');">
                    <a href="#">
                        <i class="fa fa-edit"></i> Edit Transfer Order
                    </a>
                </li>
                <li>
                    <a href="#" onclick="delInformtion('delete_transfer_order', '#currentarea', '<?php echo $id; ?>', 'Transfer Order No <?php echo $id; ?> ?', 'You are about to Delete Transfer Order No. <?php echo $id; ?>.', 'Trasnfer 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 Transfer Order </a>
                </li>
                <?php } ?>
                <li onclick="getstep('list_transfer_orders','','<?php echo $order_detail_row['order_status']; ?>');">
                    <a href="#">
                        <i class="fa fa-search"></i> Search Transfer 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">  Transfer Order Details</span>
                </div>
                <div class="actions uppercase">
                    <strong>Date Created:</strong> <span style="color:red; font-weight:bold;"><?php echo $order_detail_row['purchase_order_date']; ?></span> <b style="color:green;"> [ TRANSFER NO : <?php echo $order_detail_row['purchase_order_no'];?> ]</b>
                 <?php if($order_detail_row['order_status']=='Approved'){?>
                 <?php if($supply_transfer_order == true) { ?>
                    &nbsp;  <a class="btn green" onclick="receiveSupplyQuantity('save_supply_transfer_order', '#currentarea', '<?php echo $order_detail_row['purchase_order_no']; ?>', 'Yes, Supply Order Now!', 'Supply Transfer?', 'You are about to Approve & Supply Transfer Order No. <?php echo $order_detail_row['purchase_order_no']; ?>. Please Note that your Quantity Available will reduced immediatley after this action', 'form#save_transfer_order');return false;"> Supply Transfer <i class="fa fa-check"></i> </a>
                 <?php } ?>
                     <?php } ?>
                 <?php if($order_detail_row['order_status']=='In-Transit' && $order_detail_row['delivery_location']==$_SESSION['branch_id'] || $_SESSION['security']=='admin'){?>
                 <?php if($received_transfer_orders == true){ ?>
                    &nbsp; <a class="btn green" onclick="receiveSupplyQuantity('receive_transfer_order', '#currentarea', '<?php echo $id; ?>', 'Receive Transfer!', 'Receive Transfer Order ?', 'By Receiving Transfer Order No. <?php echo $id; ?>, This means you have received and counted the Quantities Transfered and everything is accurate.', 'form#save_transfer_supply');return false;"> Receive Transfer Order <i class="fa fa-check"></i> </a>
                 <?php } ?>
                     <?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"> Store/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">Supplier's Name</span></label>
                                            <label class="col-md-7 control-label bold"> 
                                                <?php $supplier_id=$order_detail_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($order_detail_row['supplier_address'])){ echo '&nbsp;'; } echo $order_detail_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($order_detail_row['memo'])){ echo '&nbsp;'; } echo $order_detail_row['memo']; ?> </label>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Transfer Raised By</span></label>
                                            <label class="col-md-7 control-label bold"> <?php if(empty($order_detail_row['user_name'])){ echo '&nbsp;'; } echo $order_detail_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($order_detail_row['approved_by_user_name'])){ echo '&nbsp;'; } echo $order_detail_row['approved_by_user_name'].' on '; if(!empty($order_detail_row['approved_date'])){ date('jS F,Y g:i a',  strtotime($order_detail_row['approved_date']));} ?> </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($order_detail_row['supplied_by_user_name'])){ echo '&nbsp;'; } echo $order_detail_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=$order_detail_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($order_detail_row['delivery_address'])){ echo '&nbsp;'; } echo $order_detail_row['delivery_address']; ?> </label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

 <?php
     $purchase_id = $order_detail_row['purchase_order_no'];
    $query_claim = "select * from transfer_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);
    ?>

<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 TO BE TRANSFERED </span>
                </div>
                <div class="actions uppercase bold" style="color: blue;">
                   <b style="color:<?php if($order_detail_row['order_status']=='Unapproved'||$order_detail_row['order_status']=='Not Saved'||$order_detail_row['order_status']=='Accepted With Rejections'){echo 'red;';}else{ echo 'green;';} ?>;"> Status: <?php echo $order_detail_row['order_status']; ?> </b>
                </div>
            </div>
            <div class="portlet-body form">
                <div class="row"  >
                    <form name="save_transfer_supply" id="save_transfer_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 style="width:65%" class="count">Item</th>
                                                    <?php if($order_detail_row['order_status']!='In-Transit'){ ?>
                                                    <?php if($see_quantity_avail_store == true) { ?>
                                                        <th style="text-align: right;">Qty Available in Store</th>
                                                    <?php } ?>
                                                    <th style="text-align: right;">Qty Available in Branch</th>
                                                    <th style="text-align: right;">Qty Transferable</th>
                                                    <?php }else{ ?>
                                                    <th style="text-align: right;">Qty Transfered</th>
                                                    <?php } ?>
                                                    <th style="text-align: center;">Status</th>
                                                     <?php if($order_detail_row['order_status']=='In Transit'||$order_detail_row['order_status']=='Accepted With No Rejections'||$order_detail_row['order_status']=='Accepted With Rejections'){?>
                                                    <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>
                                                            <?php if($order_detail_row['order_status']!='In-Transit'){ ?>
                                                            <?php  if($see_quantity_avail_store == true){ ?>
                                                            <td style="text-align: right;"><?php 
                                                                if($branch_type=='branch'){echo quantityAvailable($row_rsclaim['item_id']);}else{
                                                                echo quantityAvailable2($row_rsclaim['item_id']);
                                                         }
                                                             ?></td>
                                                             <?php } ?>
                                                            <td style="text-align:right;"><?php echo quantityAvailable($row_rsclaim['item_id'],$order_detail_row['delivery_location']); ?></td>
                                                            <td style="text-align: right;"><?php echo $row_rsclaim['qty']; ?></td>
                                                            <?php }else{ ?>
                                                            <td style="text-align: right;"><?php echo $row_rsclaim['qty']; ?></td>
                                                            <?php } ?>
                                                            <td style="text-align: center;color: <?php if($row_rsclaim['order_status']=='supplied'){echo 'green';}else{echo 'red';}  ?>"><?php echo ucfirst($row_rsclaim['order_status']); ?></td>
                     <?php if($order_detail_row['order_status']=='In Transit'||$order_detail_row['order_status']=='Accepted With No Rejections'||$order_detail_row['order_status']=='Accepted With Rejections'){?>
                    <td>
                          <?php if($order_detail_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($order_detail_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>