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

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);
$approve_purchase_order = getMenuPermission('Approve Purchase Orders Received');
$view_cost_price = getMenuPermission('Can View Cost Price');
?>


<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']!='Received' && $patient_row['approval_status']!='Approved'){ ?>
                <li onclick="getstep('edit_purchase_order','<?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>
<form name="form" id="save_received_order">
<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"> RECEIVE ORDER DETAILS </span>
                </div>
                <div class="actions uppercase">
                    <strong>Order Status:</strong><span style="color:red; font-weight:bold;"><?php if($patient_row['order_status']=='Saved'){echo " Waiting to Receive Items";}else{echo " Items ".$patient_row['order_status'];}?> <b style="color:#000;"> <strong>Order Date:</strong> <span style="color:red; font-weight:bold;"><?php echo $patient_row['purchase_order_date']; ?></span></b> <b style="color:green;"> [ ORDER NO : <?php echo $patient_row['purchase_order_no'];?> ]</b></span>
                 <?php if($patient_row['order_status']=='Saved'){?>
                   &nbsp;    <a class="btn green" onclick="receiveSupplyQuantity('save_received_order', '#currentarea', '<?php echo $id; ?>', 'Yes, Receive Order Now!', 'Receive Purchase Order ?', 'You are about to Receive Purchase Order No. <?php echo $id; ?> from the List of approved Purchase Orders. Please Note that your Quantity Available will Increased after It has been approved.', 'form#save_received_order');return false;"> Receive Purchase Order <i class="fa fa-check"></i> </a>
                    <?php } ?>
                <?php if($approve_purchase_order == true){ ?>
                 <?php if($patient_row['order_status']=='Received' && $patient_row['receive_approval_status']=='Not Approved'){?>
                   &nbsp;    <a class="btn green" onclick="receiveSupplyQuantity('save_received_order_approved', '#currentarea', '<?php echo $id; ?>', 'Approve Received Order!', 'Approve Received Order ?', 'You are about to Approve the Receipt of Purchase Order No. <?php echo $id; ?> from the List of Received Orders. Please Note that your Quantity Available will Increased immediatley after this action with the respective Quantities Received.', 'form#save_received_order');return false;"> Approve Received Order <i class="fa fa-check"></i> </a>
                    <?php } ?>
                <?php } ?>
                </div>
            </div>
            <div class="portlet-body form">
               <?php
              $supplier_id = $patient_row['supplier'];
              $row_rsodest_supplier = mysql_fetch_assoc(mysql_query("SELECT sum(credit_amt) as supplier_credit, sum(debit_amt) as supplier_debit FROM suppliers_bill_payments where supplier_id='$supplier_id'"));
              $supplier_credit = $row_rsodest_supplier['supplier_credit'] - $row_rsodest_supplier['supplier_debit'];

               $rsodest_supplier = mysql_query("SELECT id,supplier FROM suppliers where id='$supplier_id' order by supplier");
               $row_rsodest_supplier = mysql_fetch_assoc($rsodest_supplier);?>
                
                <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">
                                       <?php if($view_cost_price == true){ ?>
                                        <div class="caption">
                                            <span class="caption-subject font-dark"> Supplier's Information  &nbsp;&nbsp; <b>[ OUTSTANDING BALANCE:  <span style="color:<?php if($supplier_credit<=0){echo "green";}else{echo "red";}?>;">GHC <?php echo number_format($supplier_credit,2);?></span> ] </b> </span>
                                        </div>
                                        <?php } ?>
                                    </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 if(empty($row_rsodest_supplier['supplier'])){ echo '&nbsp;'; } echo $row_rsodest_supplier['supplier']; ?>
                                                <input type="hidden" id="supplier_opening_balance" name="supplier_opening_balance" value="<?php echo $supplier_credit; ?>"> 
                                            </label>
                                            <input type="hidden" id="supplier_id" name="supplier_id" value="<?php echo $patient_row['supplier'];?>">
                                        </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">Bill Date </span> </label>
                                            <label class="col-md-7 control-label bold">  <?php if($patient_row['order_status']=='Received'){ if(empty($patient_row['bill_date'])){ echo '&nbsp;'; } echo date('D, jS F, Y',  strtotime($patient_row['bill_date'])); }else{?> <input type="text" name="bill_date" id="bill_date" class="date_time_picker"><?php } ?> </label>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Bill Ref </span> </label>
                                            <label class="col-md-7 control-label bold">  <?php if($patient_row['order_status']=='Received'){ if(empty($patient_row['bill_ref'])){ echo '&nbsp;'; }echo $patient_row['bill_ref']; }else{?> <input type="text" name="bill_ref" id="bill_ref"> <?php } ?> </label>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left">Bill Due Date </span> </label>
                                            <label class="col-md-7 control-label bold"> <?php if($patient_row['order_status']=='Received'){ if(empty($patient_row['bill_due_date'])){ echo '&nbsp;'; }echo date('D, jS F, Y',  strtotime($patient_row['bill_due_date'])); }else{?> <input type="text" name="bill_due_date" id="bill_due_date" class="date_time_picker"> <?php } ?> </label>
                                        </div>
                                        <div class="form-group hide">
                                            <label class="col-md-5 control-label"><span class="pull-left">Bill Amt (GHS) </span> </label>
                                            <label class="col-md-7 control-label bold">  <?php if($patient_row['order_status']=='Received'){ if(empty($patient_row['bill_amt'])){ echo '&nbsp;'; }echo number_format($patient_row['bill_amt'],2); }else{?> <input type="text" name="bill_amt" id="bill_amt"> <?php } ?> </label>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left"> Discount % </span> </label>
                                            <label class="col-md-7 control-label bold">  <?php if($patient_row['order_status']=='Received'){ if(empty($patient_row['bill_discount'])){ echo '&nbsp;'; }echo $patient_row['bill_discount']; }else{?> <input type="text" name="bill_discount" id="bill_discount"> <?php } ?> </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">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"> Date Received </span> </label>
                                            <label class="col-md-7 control-label bold"> <?php if($patient_row['order_status']=='Received'){ if(empty($patient_row['received_date'])){ echo '&nbsp;'; } echo date('D, jS F, Y',  strtotime($patient_row['received_date'])); }else{?> <input type="text" name="received_date" id="received_date" class="date_time_picker"><?php } ?> </label>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-5 control-label"><span class="pull-left"> Delivery Memo </span> </label>
                                            <label class="col-md-7 control-label bold">  <?php if($patient_row['order_status']=='Received'){ if(empty($patient_row['delivery_memo'])){ echo '&nbsp;'; } echo $patient_row['delivery_memo']; }else{?> <input type="text" name="delivery_memo" id="delivery_memo"> <?php } ?> </label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                </div>
            </div>
        </div>
    </div>
</div>
</form>

<div class="row" id="view_items_response_wrapper_2">
   <?php
       $purchase_id = $patient_row['purchase_order_no'];  
       include 'list_items_received.php';
   ?>
</div>