<?php
/**********************************************************************
***********************************************************************/

//--------------------------------------------------------------------------------



function outstanding_purchase_requisition_details_filtering_controls()
{
    start_table(TABLESTYLE_NOBORDER);
    start_row();
    ref_cells(_(text_bold("PR No:")), 'pr_no', '',null, '', true);
    ref_cells(_(text_bold("Reference:")), 'reference', '',null, '', true);
    date_cells(_("from:"), 'OrdersAfterDate', '', null, -30);
    date_cells(_("to:"), 'OrdersToDate', '', null, +1);

    end_row();
    end_table();

    start_table(TABLESTYLE_NOBORDER);
    start_row();

    locations_list_cells(_("Location:"), 'StockLocation', null, '', false);
    project_list_cells("Project:", "project_id", null, '');
    user_list_cells("Originator", 'originator_id', null, '');

    //stock_items_list_cells(_("for item:"), 'SelectStockFromList', null, true);

    submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
    end_row();
    end_table(1);
}

function display_outstanding_purchase_requisition_details_inquiry($from_date_, $to_date_, $pr_no, $reference,
                                                            $StockLocation, $project_id, $originator_id)
{
    global $path_to_root, $stock_types;
    
    $from_date = date2sql($from_date_);
    $to_date = date2sql($to_date_);

    $result = get_outstanding_purchase_requisition_details($from_date, $to_date, $pr_no, $reference,
                                                            $StockLocation, $project_id, $originator_id);
    
    start_table(TABLESTYLE, "width=98%");
    $th = array(_("Project"), "PR No", "PR Date", "Item Code", "Item description",
                                "Type", "Req. Unit", "Req. Qty", "Qty Outstanding", "Est. Price", "Value of Outstanding Qty",
                                "PR Allocations", "POs", "GRNS", "PR Issues", "");

        table_header($th);
        
        $count = $total_outstanding_value = $pr_outstanding_value = $previous_pr_no = 0;
         while($myrow = db_fetch($result)){
            if($previous_pr_no != $myrow["requisition_id"] && $previous_pr_no > 0){
                start_row();
                label_cell('', 'colspan=7');
                label_cell(text_bold('Total outstanding for PR #'.$previous_pr_no), 'colspan=3, align=right');
                amount_cell($pr_outstanding_value, true);
                label_cell('', 'colspan=5');
                $pr_outstanding_value = 0;
            }
            
            alt_table_row_color($k);
//                label_cell($myrow["project_id"]);
                label_cell($myrow["project_description"]);
                $pr_link = get_trans_view_str(ST_PURCH_REQ, $myrow["requisition_id"]);
                label_cell($pr_link, 'align=center');
//                label_cell($myrow["reference"]);
                label_cell(sql2date($myrow["pr_date"]));
                label_cell($myrow["item_code"]);
                
                if(strlen($myrow["item_pr_description"]) > 2){
                    $item_description = $myrow["item_pr_description"];
                }else{
                    $item_description = $myrow["item_description"];
                }
                label_cell($item_description);
                
                label_cell($stock_types[$myrow["mb_flag"]]);
                label_cell($myrow["unit_name"]);
                
                $req_unit_qty = $myrow["quantity"] / $myrow["req_unit_conv_factor"];
                qty_cell($req_unit_qty, false,3);
                
                $req_unit_outstanding_qty = $myrow["quantity"]-($myrow["issued_qty"]-$myrow["returned_qty"]) / $myrow["req_unit_conv_factor"];
                qty_cell($req_unit_outstanding_qty, false, 3);
                
                amount_cell($myrow["estimate_price"]);
                
                $req_unit_outstanding_value = 0;
                if($req_unit_outstanding_qty > 0){
                    $req_unit_outstanding_value = $myrow["estimate_price"] * $req_unit_outstanding_qty;
                }
                amount_cell($req_unit_outstanding_value);
                $pr_outstanding_value += $req_unit_outstanding_value;
                $total_outstanding_value += $req_unit_outstanding_value;
//                label_cell($myrow["project_name"], "align=center");
                
                $pr_allocs_link = $pos_link = $grns_link = $pris_link = "";
                get_purchase_requisition_detail_related_transactions_array($myrow["id"], $pr_allocs_link, $pos_link, $grns_link, $pris_link);
                label_cell($pr_allocs_link, 'align=center');
                label_cell($pos_link, 'align=center');
                label_cell($grns_link, 'align=center');
                label_cell($pris_link, 'align=center');
//                $issue_link = pager_link( _("Issue"), "/purchasing/pr_issues_select_po.php?pr_no=".$row['pr_no'], ICON_DOC);
                $issue_link = $link = "<a href='$path_to_root/purchasing/pr_issues_select_po.php?pr_no=".$myrow["requisition_id"]."' target=_blank>" . "Issue" . "</a>";
                label_cell($issue_link);
            end_row();
            
            $previous_pr_no = $myrow["requisition_id"];
            $count++;
         }
         

        start_row();
        label_cell('', 'colspan=7');
        label_cell('Total for PR #'.$previous_pr_no, 'colspan=3, align=right');
        amount_cell($pr_outstanding_value, true);
        label_cell('', 'colspan=5');
            
            
        start_row();
        label_cell('', 'colspan=10');
        amount_cell($total_outstanding_value, true);
        label_cell('', 'colspan=5');
        end_table();

        if($count >= 1000){
            $msg = "Maximum of 1000 records reached. Please change your filtering parameters to narrow down the result";
            display_warning($msg);
        }
}


function get_purchase_requisition_detail_related_transactions_array($prd_id, &$pr_allocs_link, &$pos_link, &$grns_link, &$pris_link)
{
    $pr_allocs = $pos = $grns = $pris = array();
    
    $result = get_purchase_requisition_detail_related_transactions($prd_id);
    while($myrow = db_fetch($result)){
        if ($myrow['pr_allocation_id'] > 0)
        {
            $pr_allocs[] = $myrow['pr_allocation_id'];
        }
        if ($myrow['po_no'] > 0)
        {
            $pos[] = $myrow['po_no'];
        }
        if ($myrow['grn_no'] > 0)
        {
            $grns[] = $myrow['grn_no'];
        }
        if ($myrow['pri_no'] > 0)
        {
            $pris[] = $myrow['pri_no'];
        }
    }
    
    $pr_allocs = array_unique($pr_allocs);
    $pos = array_unique($pos);
    $grns = array_unique($grns);
    $pris = array_unique($pris);
    
    foreach ($pr_allocs as $pr_alloc) {
        $link1 = get_supplier_trans_view_str(ST_PR_LINE_ALLOCATIONS,$prd_id, $pr_alloc);
//        get_pr_allocation_ids_string_of_pr($prd_id);
        if(strlen($pr_allocs_link) > 0){
            $pr_allocs_link .= " ".$link1;
        }else{
            $pr_allocs_link = $link1;
        }
    }
    
    foreach ($pos as $po) {
        $link2 = get_trans_view_str(ST_PURCHORDER, $po);
        if(strlen($pos_link) > 0){
            $pos_link .= " ".$link2;
        }else{
            $pos_link = $link2;
        }
    }
    
    foreach ($grns as $grn) {
        $link3 = get_trans_view_str(ST_SUPPRECEIVE,$grn);
        if(strlen($grns_link) > 0){
            $grns_link .= " ".$link3;
        }else{
            $grns_link = $link3;
        }
    }
        
    foreach ($pris as $pri) {
        $link4 = get_supplier_trans_view_str(ST_PR_ISSUE,$pri);
        if(strlen($pris_link) > 0){
            $pris_link .= " ".$link4;
        }else{
            $pris_link = $link4;
        }
    }
    
}