<?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'];
$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 purchase_orders WHERE purchase_order_no = '$id'", $courier) or die(mysql_error());
$patient_row = mysql_fetch_assoc($sql);
$change_cost_price = getMenuPermission('Change Cost Price');

if($action == "view_purchase_order_pending"){
    $next_action = "edit_purchase_order_pending";
}else{
    $next_action = "edit_purchase_order";
}

?>


<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($action == "view_purchase_order_pending" || ($patient_row['order_status']!='Received' && $patient_row['approval_status']!='Approved' && $patient_row['order_type']!='Lens Order')){
                // if($patient_row['order_status']!='Received' && $patient_row['approval_status']!='Approved'){
                    ?>
                <li onclick="getstep('<?php echo $next_action; ?>','<?php echo $id; ?>');">
                    <a href="#">
                        <i class="fa fa-edit"></i> Edit Purchase Order
                    </a>
                </li>
                <li>
                    <a href="#" onclick="delInformtion('delete_purchase_order', '#currentarea', '<?php echo $id; ?>', 'Purchase Order No <?php echo $id; ?> ?', 'You are about to Delete Purchase Order No. <?php echo $id; ?> from the List of Purchase Orders.', 'Purchase 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 Purchase Order </a>
                </li>
                <?php } ?>
                <li onclick="getstep('list_purchase_orders');">
                    <a href="#">
                        <i class="fa fa-search"></i> Search Purchase 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">  Purchase 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;"> [ ORDER NO : <?php echo $patient_row['purchase_order_no'];?> ]</b></span></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 id,supplier FROM suppliers where id='$supplier_id' order by supplier");
                                                      $row_rsodest_supplier = mysql_fetch_assoc($rsodest_supplier);
                                                      if(empty($row_rsodest_supplier['supplier'])){ echo '&nbsp;'; }  echo $row_rsodest_supplier['supplier'];
                                                ?>
                                            </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>
                                </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 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">Memo</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>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


 <?php
     $purchase_id = $patient_row['purchase_order_no'];
    $query_claim = "select * from purchase_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 purchase_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 ">
            <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" style="color: blue;">
                    <b style="color:<?php if($patient_row['order_status']=='Not Saved'){echo 'red';}else{echo 'green';} ?>"> [ Status: <?php echo $patient_row['order_status']; ?> ]</b> &nbsp;|&nbsp;Expected Total Cost Price: GHS <?php echo number_format($row_rsclaim_total['totalprice'],2); ?>
                </div>
            </div>
            <div class="portlet-body form">
                <div class="row"  >
                    <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;">Pack Size</th> -->
                                                    <th style="text-align: right;">Price</th>
                                                    <th style="text-align: right;">Quantity</th>
                                                    <th style="text-align: right;"> Total Cost</th>
                                                    </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;"><?php echo $row_rsclaim['pack_size']; ?></td> -->
                    <td style="text-align: right;"><?php if($change_cost_price == true){ echo $currency." ".number_format($row_rsclaim['cost_price_before_tax'],2);}else{ echo '-';} ?></td>
                    <td style="text-align: right;"><?php echo $row_rsclaim['qty_expected']; ?></td>
                    <td style="text-align: right;"><?php if($change_cost_price == true){ echo $row_rsclaim['qty_expected']*$row_rsclaim['cost_price_before_tax'];}else{ echo '-';} ?></td>
                    </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>
                </div>
            </div>
        </div>
    </div>
</div>