<?php
$currency='GHC';
$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, sum(qty_received*received_price) as totalactualprice 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);

$change_cost_price = getMenuPermission('View PO Cost Price');

?> 

<form name="form">
    <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>
                <?php if($action == 'receive_purchase_order_pending'){?>
                    <div class="actions uppercase bold" style="color: blue;">
                     Expected Total Cost: <?php echo $currency." ". number_format($row_rsclaim_total['totalprice'],2); ?>&nbsp; | &nbsp;<b style="color:green;">Actual Total Cost: <?php echo $currency." ". number_format($row_rsclaim_total['totalactualprice'],2); ?></b>
                     <input type="hidden" name="purchase_order_id" id="purchase_order_id" value="<?php echo $purchase_id; ?>">
                 </div>
             <?php } ?>
         </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;">Price</th>
                                <th style="text-align: right;">Quantity</th>
                                <th style="text-align: right;"> Total Cost</th>
                                <th style="text-align: right;"> Actual Price </th>
                                <th style="text-align: right;"> Actual Quantity</th>
                                <th style="text-align: right;"> Actual Total Cost </th>
                                <th style="text-align: right;"> PO Status </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 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 $currency." ".number_format(( $row_rsclaim['qty_expected']*$row_rsclaim['cost_price_before_tax']),2);}else{ echo '-';} ?></td>

                                        <td style="text-align: right;">
                                            <?php
                                            $orderStatus = getPurchaseOrderStatus($row_rsclaim['purchase_order_no'], $courier);
                                            if($change_cost_price == true){

                                              if($orderStatus =='Received'){ echo number_format($row_rsclaim['received_price'],2);}else{ ?> <input type="text" id="itemprice<?php echo $row_rsclaim['id']; ?>" name="itemprice<?php echo $row_rsclaim['id']; ?>" style="width:50px; text-align:right" min="1" value="<?php echo $row_rsclaim['received_price']; ?>" onChange="searchItemsName('updateprice_purchase_order_details_actual','#view_items_response_wrapper_2',<?php echo $row_rsclaim['id']; ?>,'no_field',this.value);"><?php }
                                          }else{ echo '-';}?>
                                      </td>

                                      <td style="text-align: right;">
                                         <?php 
                                         $result = getPurchaseExpectedQtySingle($row_rsclaim['id'], $courier);
                                         if($orderStatus =='Received' && $row_rsclaim['qty_match'] == 'Yes' && ($row_rsclaim['qty_mismatch_status'] == 'Closed' || is_null($row_rsclaim['qty_mismatch_status']))){ echo $row_rsclaim['qty_received'];} 
                                         else if ($orderStatus =='Received' && $row_rsclaim['qty_match'] != 'Yes' && $row_rsclaim['qty_mismatch_status'] == 'Closed') {
                                            echo $row_rsclaim['qty_received'];
                                        }else{ ?> 
                                            <input type="number" id="itemqty<?php echo $row_rsclaim['id']; ?>" name="itemqty<?php echo $row_rsclaim['id']; ?>" style="width:50px;" min="1" value="<?php echo $row_rsclaim['qty_received']; ?>" onChange="searchItemsName('updateqty_purchase_order_details_actual','#view_items_response_wrapper_2',<?php echo $row_rsclaim['id']; ?>,'no_field',this.value);"> <?php } ?>
                                        </td>
                                        <td style="text-align: right;"><?php if($change_cost_price == true){ echo $currency." ".number_format(($row_rsclaim['qty_received']*$row_rsclaim['received_price']),2);}else{ echo '-';} ?></td>
                                        <td style="text-align: right;">
                                         <?php 
                                         $result = getPurchaseExpectedQtySingle($row_rsclaim['id'], $courier);
                                         if($orderStatus =='Received' && $row_rsclaim['qty_match'] == 'Yes' && ($row_rsclaim['qty_mismatch_status'] == 'Closed' || is_null($row_rsclaim['qty_mismatch_status']))){
                                            echo "Closed";
                                            ?> 
                                        <?php }else if ($orderStatus =='Received' && $row_rsclaim['qty_match'] != 'Yes' && $row_rsclaim['qty_mismatch_status'] == 'Closed') {
                                            echo "Closed";
                                        }else{ ?> 
                                            <input onchange="delInformtion('closepo', '#view_items_response_wrapper_2', '<?php echo $row_rsclaim['id']; ?>', 'Purchase order No. <?php echo $row_rsclaim['purchase_order_no']; ?> ?', 'You are about to close PO for <?php echo $row_rsclaim['item']; ?>', '<?php echo $row_rsclaim['item']; ?> PO Successfully Closed!', '<?php echo $row_rsclaim['purchase_order_no']; ?>');" type="checkbox" class="item_check_box" multiple="" name="closepo_<?php echo $row_rsclaim['id']; ?>" id="closepo_<?php echo $row_rsclaim['id']; ?>" value="Closed">
                                        <?php } ?>
                                    </td>
                                </tr>
                            <?php }  while ($row_rsclaim = mysql_fetch_assoc($rsclaim)); ?>
                        <?php }else{echo '<tr><td colspan="10" style="background: linen"><div class="none">No Items Found. Search Items to Add to Purchase Order.</div></td></tr>';}?>
                    </tbody>
                </table> 
            </div>
        </div>
    </div>
</div>
</div>
</form>

