<?php

//function display_pr_allocation_line_details_for_edit($pr_line_id, $pr_po_line_id)
//{
//    global $Ajax;
//    
//    display_purchase_requisition_line($pr_line_id, $pr_po_line_id);
//    br();
//    
//    $Ajax->activate('pr_pod_div');
//    
//    div_start('pr_pod_div');
//    display_pr_po_details($pr_line_id, $pr_po_line_id);
////    display_purchase_requisition_allocation_edit_controls($pr_line_id, $pr_po_line_id);
//    div_end();
//}

function display_purchase_requisition_line($pr_line_id, $pr_po_line_id)
{
    
    $row = get_pr_detail_info_line($pr_line_id);

    display_heading2("Purchase Requisition Line");
    
    
//    $row = get_requisition_detail_edit($pr_line_id, $pr_po_line_id);  
    
    start_table(TABLESTYLE, "width=90%");

    $th = array(_("PR#"), "Line#", _("Project"), _("Location"), _("Item"), _("Req. Qty"), _("Unit"), _("Ord. Qty"), _("Required Date"), "Complete", "");

    table_header($th);
    $k = 0;
    
        alt_table_row_color($k);

//        $ordered_qty = get_pr_line_ordered_qty($row["id"]);
//        $total_qty_ordered = $ordered_qty['total_qty_ordered'];
        
        $total_qty_ordered = $row['quantity_ordered'];
        $previous_stock_id = $row["item_code"];
        
        $_POST["item_code"] = $row["item_code"];
        
        $no = $row["pr_no"];
        label_cell(get_supplier_trans_view_str(ST_PURCH_REQ,$no, $no),"align=center");
        label_cell($row["id"],"align=center");
        
        label_cell($row['project_description']);
        label_cell($row['pr_location_name']);
        label_cell($row['item_code'].'-'.$row['item_description']);
        label_cell(number_format($row["requested_unit_qty"],2),"align=right");
        $_POST['requested_unit'] = $row['requested_unit'];
        label_cell($row["requested_unit"]);
        
        if(isset($row['req_unit_conv_factor']) && $row['req_unit_conv_factor'] > 0){
            $req_unit_conv_factor = $row['req_unit_conv_factor'];
        }
        else{
            $req_unit_conv_factor = 1;
        }
        label_cell(number_format($total_qty_ordered/$req_unit_conv_factor,2),'align=right');
        label_cell(sql2date($row["required_date"]),'align=center');
        
        label_cell(get_yes_no_name($row["line_complete"]),'align=center');
        
        $_POST['row_state'] = $row['row_state'];

//        button_cell("close_pr_line", "Close PR Line");
        submit_cells('close_pr_line', _("Close"), "align=center", _('Add new item to document'), true);
        //submit_center_last("close_pr_line", "Close PR Line" );
        
        end_row();
        
     end_table();

}

function display_pr_po_details($pr_line_id,$pr_po_line_id)
{
    display_heading2("Purchase Requisition Line Allocation Details");
    
    $result = get_pr_po_details($pr_line_id);  
    
    start_table(TABLESTYLE, "width=90%");

    $th = array(_("PR. Allo. No"), "Purch. Officer", _("Supplier"),   _("Location"), _("Item Code"), _("Item Description"), _("Unit"), 
                _("Qty"), _("Price"), _("Discount"), _("Req Date"), _("PO No"), _(" "), _(" "));

    table_header($th);
    $k = 0;
    
    while($row = db_fetch($result))
    {
        if($pr_po_line_id != $row["id"]){
                
            
            alt_table_row_color($k);        
//                label_cell($row['pr_line_id'],'align=center');
                label_cell(get_supplier_trans_view_str(ST_PR_LINE_ALLOCATIONS,$row["pr_line_id"], $row["id"]),'align=center');
                
                $pofficer_name = '';
                if ($row['updated_pofficer_name'] != null) {
                    if(strlen($row['updated_pofficer_name']) > 0){
                        $pofficer_name = $row['updated_pofficer_name'];
                    }
                    else{
                        $pofficer_name = $row['created_pofficer_name'];
                    }
                }
                
                label_cell($pofficer_name,"align=center");
        
                label_cell($row["supp_name"]);
                label_cell($row["location_name"]);
                label_cell($row['item_code']);
                label_cell($row['item_description']);
//                label_cell($row['order_quantity'],"align=right");
                
                label_cell($row["order_unit"]);
                label_cell(number_format($row["order_unit_qty"],3),"align=right");
                label_cell(number_format($row["order_unit_price"],2),"align=right");
                label_cell(number_format($row["order_unit_price_discount"],2),"align=right");
                
                
                label_cell(sql2date($row["required_date"]),"align=center");
                
                if ($row["order_no"] > 0) {
                    label_cell(get_supplier_trans_view_str(ST_PURCHORDER,$row["order_no"], $row["order_no"]),'align=center');
                } else {
                    label_cell('');
                }
//                label_cell($row["row_state"],'align=center');
                
                    if($row['po_line_id'] > 0){
                        label_cell('');
                        label_cell('');
                    }
                    else{
                        edit_button_cell("Edit".$row['id'], _("Edit"));
                        delete_button_cell("Delete".$row['id'], _("Delete"));
                        
                    }
            end_row();
        }
        
        else{
            display_purchase_requisition_allocation_edit_controls($pr_line_id, $pr_po_line_id);
        }
    }
    
    if(!($pr_po_line_id > 0)){
        display_purchase_requisition_allocation_edit_controls($pr_line_id, -1);
    }
        
     end_table();
     
     get_global_supplier();
}

function display_purchase_requisition_allocation_edit_controls($pr_line_id, $pr_po_line_id)
{   
    echo "<td colspan=12>";
    $previous_pr_po_line_id = 0;
    if(isset($_POST['previous_pr_po_line_id']) ){
        $previous_pr_po_line_id = $_POST['previous_pr_po_line_id'];
        
        if(($previous_pr_po_line_id != $pr_po_line_id)){// && ($previous_pr_po_line_id != "New Allocation Line")){
            unset($_POST['supplier_id']);
            unset($_POST['_supplier_id_edit']);
            unset($_POST['line_loc_code']);
            unset($_POST['pr_po_project_site_id']);
            unset($_POST['order_unit_qty']);
            unset($_POST['order_unit']);
            unset($_POST['order_unit_price']);
            unset($_POST['order_unit_price_discount']);
            
            unset($_POST['previous_supplier_id']);
            unset($_POST['previous_line_loc_code']);
            unset($_POST['previous_pr_po_project_site_id']);
            unset($_POST['row_state']);

        }
    }

    global $Ajax;
    
    if($previous_pr_po_line_id != $pr_po_line_id){
        $Ajax->activate('supplier_id');
    }
    
    
    if($pr_po_line_id > 0 ){
        $row = get_requisition_detail_edit($pr_line_id, $pr_po_line_id);
    
            $_POST['row_state'] = $row['row_state'];
        
            if($previous_pr_po_line_id != $pr_po_line_id){
                if($row['supplier_id'] > 0){
                    $_POST['supplier_id'] = $row['supplier_id'];
                }
                else{
                    //unset($_POST['supplier_id']);
                }
            }
                    
            if(isset($_POST['previous_supplier_id']) && $_POST['previous_supplier_id'] > 0){
                $previous_supplier_id = $_POST['previous_supplier_id'];

                if(isset($_POST['supplier_id']) && ($previous_supplier_id != $_POST['supplier_id'])){
//                    unset($_POST['supplier_id']);
                    unset($_POST['previous_supplier_id']);
                    
                }
            }else{
                if(!(isset($_POST['supplier_id']) && $_POST['supplier_id'] > 0 )){
                    $_POST['supplier_id'] = $row['supplier_id'];
                }
            }
    
            if(isset($_POST['previous_line_loc_code']) && strlen($_POST['previous_line_loc_code']) > 0){
                $previous_line_loc_code = $_POST['previous_line_loc_code'];

                if(isset($_POST['line_loc_code']) && ($previous_line_loc_code != $_POST['line_loc_code'])){
//                    unset($_POST['line_loc_code']);
                    unset($_POST['previous_line_loc_code']);
                        if($previous_pr_po_line_id != $pr_po_line_id){
                            if(strlen($row['line_loc_code']) > 0 ){
                                $_POST['line_loc_code'] = $row['line_loc_code'];
                            }
                        }
                    
                }
            }else{
                if(!(isset($_POST['line_loc_code']) && strlen($_POST['line_loc_code']) > 0 )){
                    $_POST['line_loc_code'] = $row['line_loc_code'];
                }
            }
            
            if(isset($_POST['previous_pr_po_project_site_id']) && $_POST['previous_pr_po_project_site_id'] > 0){
                $previous_pr_po_project_site_id = $_POST['previous_pr_po_project_site_id'];

                if(isset($_POST['pr_po_project_site_id']) && ($previous_pr_po_project_site_id != $_POST['pr_po_project_site_id'])){
//                    unset($_POST['pr_po_project_site_id']);
                    unset($_POST['previous_pr_po_project_site_id']);
                    if($previous_pr_po_line_id != $pr_po_line_id){
                        if($row['pr_po_project_site_id'] > 0 ){
                            $_POST['pr_po_project_site_id'] = $row['pr_po_project_site_id'];
                        }
                    }
                }
            }else{
                if(!(isset($_POST['pr_po_project_site_id']) && $_POST['pr_po_project_site_id'] > 0 )){
                    $_POST['pr_po_project_site_id'] = $row['pr_po_project_site_id'];
                }
            }
            
            
            
            
            
            
            $dec = 2;
            if(!(isset($_POST['order_unit_qty']) && $_POST['order_unit_qty'] > 0 )){
                $_POST['order_unit_qty'] = $row['order_unit_qty'];
            }
//            else{
//                $_POST['order_unit_qty'] = number_format2($row['order_unit_qty'], $dec);
//            }

            if(!(isset($_POST['order_unit']))){
                $_POST['order_unit'] = $row['order_unit'];
            } 
            
            if(!(isset($_POST['order_line_required_date']))){
                $_POST['delivery_required_date'] = sql2date($row['order_line_required_date']);
                $_POST['order_line_required_date'] = sql2date($row['order_line_required_date']);
            } 
            
            if(!(isset($_POST['order_unit_price']) && $_POST['order_unit_price'] > 0 )){
        //        $_POST['order_unit_price'] = number_format2(input_num('order_unit_price'), 2);
                $_POST['order_unit_price'] = $row['order_unit_price'];
            }
            
            if(!(isset($_POST['order_unit_price_discount']) && $_POST['order_unit_price_discount'] > 0 )){
        //        $row['order_unit_price_discount'] = number_format2(input_num('order_unit_price_discount'), 2);
                $_POST['order_unit_price_discount'] = $row['order_unit_price_discount'];
            }
            
            $pofficer_name = '';
            if ($row['updated_pofficer_name'] != null) {
                if(strlen($row['updated_pofficer_name']) > 0){
                    $pofficer_name = $row['updated_pofficer_name'];
                }
                else{
                    $pofficer_name = $row['created_pofficer_name'];
                }
            }

    }
    else{
        $row = get_pr_detail_info_line($pr_line_id);

        $today = Today();
        $_POST['order_line_required_date'] = $today;

        if(!(isset($_POST['supplier_id']) && $_POST['supplier_id'] > 0)){
            $_POST['supplier_id'] = get_global_supplier();
        }
        $_POST['row_state'] = 0;
        
        $pofficer_name = "";
             
    }
       
    
    br(1);

    start_outer_table(TABLESTYLE2); // outer table

    table_section(1);
    table_section_title("Purchase Requisition");
        label_row("PR No:", $row["pr_no"]);
        label_row("Project:", $row['project_description']);
        
        label_row("Department:", $row['department_name']);
        label_row("Requester:", $row['requester_name']);        
        
    table_section(2);
    table_section_title("Purchase Requisition Line");

        label_row("PR Line No:", $row['pr_line_id']);
        
        start_row();
        screen_field_label_cell("Item Code:");
        view_stock_status_cell($row["item_code"]);
//        $stock_status_link = view_stock_status($row['item_code']);
//        label_row("Item Code:", $stock_status_link);

        hidden('item_code',get_post('item_code'));
        end_row();

        $_POST['item_description'] = $row['item_description'];
        label_row("Item Description:", $row['item_description']);
        
        label_row("Budget:", $row['task_description']);
        label_row("PR Location:", $row['pr_location_name']);
        label_row("Requested Unit:", $row['requested_unit']);
        $dec = 2;
        label_row("Requested Unit Qty:", number_format2($row['requested_unit_qty'],$dec));
        label_row("Required Date:", sql2date($row['required_date']));
        label_row("Estimated Price:",number_format2($row['estimate_price'],2));
        
//        label_row("Requested Quantity:", number_format2($row['requested_quantity'],2));
        label_row(spaces(5),"");
        
        label_row("Site:", $row['project_site_name']);
        label_row("Line Comment:", $row['line_comment']);
        label_row("Total Qty Ordered:", number_format2($row['quantity_ordered']/$row['req_unit_conversion_factor'],3));        
        $qoh = get_qoh_on_date($row['item_code'], $row["pr_loc_code"]);
        label_row("Qty On Hand:", number_format2($qoh,2));

    table_section(3);
    table_section_title("Purchase Requisition Allocation Line");//-----------------------------------------------------------------------------


        hidden('project_id', $row['project_id']);

        if(isset($row["pr_po_line_id"]) && $row["pr_po_line_id"] > 0){
            $pr_po_line_id = $row["pr_po_line_id"];
        }
        else{
            
//            $pr_po_line_id = "New Allocation Line";
        }
        
        if($pr_po_line_id > 0){
            label_row("PR Allocation No:", $pr_po_line_id);
        }
        else{
            label_row("PR Allocation No:", "New PR Allocation");
        }

        label_row("Item Code:", $row['item_code']);


        if($row['editable_descri'] == true){
//            $item_description = '';
            if(isset($row["pr_po_item_description"]) && strlen($row["pr_po_item_description"]) > 0){
                $item_description  = $row["pr_po_item_description"];
            }
            elseif(isset($row["item_pr_description"]) && strlen($row["item_pr_description"]) > 0){
                $item_description  = $row["item_pr_description"];
            }
            else{
                $item_description  = $row["item_description"];
            }
//            $item_description  = $row["description"];
            text_row("Item Description:", 'pr_po_item_description', $item_description, 30, 200);
        }
        else{
            label_row("Item Description:", $row["item_description"]);
            hidden('pr_po_item_description', $row["item_description"]);
        } 
        
        
        
        if(isset($_POST['supplier_id']) && $_POST['supplier_id'] > 0){
            $supplier_id = $_POST['supplier_id'];
             
        }
        else{
            $supplier_id = null;    
        }
        
        supplier_list_row(_("Supplier:"), 'supplier_id', $supplier_id, "Select a Supplier", false);   
        $supplier = get_supplier($supplier_id);
        
        set_global_supplier($_POST['supplier_id']);
        hidden('previous_supplier_id', $_POST['supplier_id']);

        locations_list_row("Location:", 'line_loc_code', '', '', false);
        hidden('previous_line_loc_code', $_POST['line_loc_code']);
        
        if(!(isset($_POST['order_unit']) && strlen($_POST['order_unit']) > 0)){
            if(isset($row['order_unit']) && strlen($row['order_unit']) > 0){
                $_POST['order_unit'] = $row['order_unit'];
            }
            else{
                $_POST['order_unit'] = $row['requested_unit'];
            }
        }
        units_list_row(_("Order Unit:"), 'order_unit', $row['item_code'], '', false, true);
        hidden('previous_order_unit', $_POST['order_unit']);
        $order_unit = $_POST['order_unit'];
        get_convertion_factor2(get_post('item_code'), $order_unit, $convesion_factor);

        hidden('order_unit_conversion_factor', $convesion_factor);
        
        $balance_qty = $row['requested_quantity'] - $row['quantity_ordered'];
        if($balance_qty < 0){
            $balance_qty = 0;
        }
        
        if(list_updated('order_unit')){
            unset($_POST['order_unit_qty']);
        }
            
        if(!(isset($_POST['order_unit_qty']) && input_num('order_unit_qty') > 0)){
            $order_unit_balance_qty = $balance_qty / $convesion_factor;
            $order_unit_qty = $order_unit_balance_qty;             
        }
        else{
            $order_unit_qty = $_POST['order_unit_qty'];
        }

        qty_row(_("Order Unit Qty :"), 'order_unit_qty', number_format2($order_unit_qty, $dec), '', null, $dec);
        
        
        $pr_po_project_site_id = 0;
        if(isset($_POST['pr_po_project_site_id'])){
            $pr_po_project_site_id = $_POST['pr_po_project_site_id'];
        }
        hidden('previous_pr_po_project_site_id', $pr_po_project_site_id);
        
        $order_unit = "";
        

        if($order_unit == "")
        {
            
        }

        //$order_unit_conversion = get_convertion_factor($order_unit, get_post('item_code'));
        

        $order_unit_qty = 0;

        if(get_post("update_type") == "new_line"){
            $order_unit_qty = $row['requested_quantity'] / $convesion_factor;
        }

        if(get_post("update_type") == "update_pr_po_line"){
            $order_unit_qty = 0;
            
        }
        
        
        $dec = 3;
    //    $_POST['order_unit_qty'] = number_format2($order_unit_qty, $dec);
        
        
        
        

    //    if($new_record_selection){
//            if(get_post('update_type') == "update_pr_po_line"){
//                $_POST['delivery_required_date'] = $row['order_line_required_date'];
//            }
//            elseif(get_post('update_type') == "new_line"){
//                $_POST['delivery_required_date'] = $row['required_date'];
//            }
    //    }

        
        
        date_row("Delivery Required By:", 'delivery_required_date');

        if(list_updated('supplier_id') || list_updated('order_unit') || list_updated('delivery_required_date')){
            //$base_unit_supplier_price = get_purchase_price ($_POST['supplier_id'], get_post('item_code'));

            $order_unit_supplier_price = get_supplier_price(get_post('item_code'), $_POST['supplier_id'], get_post('project_id'), get_post('order_unit'), $_POST['delivery_required_date']);
            $order_unit_price = $order_unit_supplier_price['price'];
            $order_unit_price_discount = $order_unit_supplier_price['discount'];
            $row['order_unit_price'] = $order_unit_price;
            $row['order_unit_price_discount'] = $order_unit_price_discount;
        }

        if($supplier['supplier_id'] > 0){
            if($supplier['tax_included'] == 1){
                $order_unit_price_text = "Order Unit Price (After Tax)";
            }
            else{
                $order_unit_price_text = "Order Unit Price (Before Tax)";
            }
        }
        else{
            $order_unit_price_text = "Order Unit Price";
        }
        
        amount_row(_("$order_unit_price_text:"), 'order_unit_price', null, null, null, 2);

        
        amount_row(_("Discount(%):"), 'order_unit_price_discount', null, null, null, 2);

        
        project_sites_list_row("Site:", "pr_po_project_site_id", $row['project_id'], false, '');
        
        
        label_row("Purchasing Officer", $pofficer_name);
        
        //table_section_title("Minimum Price");
        
        

//        $order_unit_min_supplier_price = get_purchase_minimum_price($row['item_code'], $row['project_id'], 
//                                         get_post('order_unit'), $_POST['order_line_required_date']);
//        if($order_unit_min_supplier_price){
//            $order_unit_min_price = $order_unit_min_supplier_price['price'];
//            $order_unit_min_price_discount = $order_unit_min_supplier_price['discount'];
//            $order_unit_min_price_supp_id = $order_unit_min_supplier_price['supplier_id'];
//        }
//        else{
//            $order_unit_min_price = 0;
//            $order_unit_min_price_discount = 0;
//            $order_unit_min_price_supp_id = 0;
//        }

        $order_unit_min_price = 0;
        $order_unit_min_price_discount = 0;
        $order_unit_min_price_supp_id = 0;
        
        hidden('minimum_price', $order_unit_min_price);
        hidden('minimum_price_discount', $order_unit_min_price_discount);
        hidden('min_price_supplier_id', $order_unit_min_price_supp_id);
        hidden('row_state', $row["row_state"]);

        $min_price_supplier_name= get_supplier_name($order_unit_min_price_supp_id);
//        label_row("Minimum Price Supplier:", $min_price_supplier_name);
//        label_row("Minimum Price:", number_format2($order_unit_min_price,2));
//        label_row("Minimum Price Discount:", number_format2($order_unit_min_price_discount,2));
        $discounted_minimum_price = $order_unit_min_price * (1-$order_unit_min_price_discount/100);
//        label_row("Discounted Minimum Price:", number_format2($discounted_minimum_price,2));
//        label_row("Row State:", $row["row_state"]);

        hidden('previous_pr_po_line_id', $pr_po_line_id);

//        submit_center_first("update_pr_po_line", "Update PR Allocation Detail" );
        
//        table_section(4);
//        submit_center_first("update_pr_po_line", "Update PR Allocation Detail" );
        
        
    end_outer_table(1); // outer table
    
    echo "</td>";
    
    if ($pr_po_line_id > 0)
	{
//            submit_cells("update_pr_po_line", "Update", $extra="", $title=false, $async=false);
            submit_cells("update_pr_po_line", "", "align=center", "Save changes", false, ICON_UPDATE);
//            button_cell('update_pr_po_line', _("Update"),_('Confirm changes'), ICON_UPDATE);
            button_cell('CancelItemChanges', _("Cancel"), _('Cancel changes'), ICON_CANCEL);
		hidden('LineNo', $pr_po_line_id);
 		set_focus('qty');
	}
	else
	{
		submit_cells('new_pr_po_line', _("Add Item"), "colspan=2 align=center",
		    _('New allocation'), true);
	}


}



function new_pr_allocation_line($selected_id)
{
    if(isset($_POST['order_unit']) && strlen($_POST['order_unit']) > 0){
        $order_unit = $_POST['order_unit'];
    }
    else{
        $item_edit_info = get_item_edit_info($_POST['item_code']);
        $order_unit = $item_edit_info['units'];
    }
    
    $order_unit_price_discount = input_num('order_unit_price_discount');
    
    get_convertion_factor2($_POST['item_code'], $_POST['order_unit'], $convesion_factor);
    $base_unit_order_qty = input_num('order_unit_qty') * $convesion_factor;
    $base_unit_order_price = (input_num('order_unit_price') * (1 - $order_unit_price_discount/100)) / $convesion_factor;
    
    $project_site_id = 0;
    if(get_post('pr_po_project_site_id') > 0){
        $project_site_id = get_post('pr_po_project_site_id');
    }
    
    $ara = add_requisition_allocation($selected_id, $base_unit_order_qty, $base_unit_order_price,
                                      $_POST['order_unit'], input_num('order_unit_qty'), input_num('order_unit_price'), $convesion_factor,
                                      $order_unit_price_discount, $_POST['supplier_id'], get_post('line_loc_code'), 
                                      $_POST['delivery_required_date'], input_num('minimum_price'), input_num('minimum_price_discount'), 
                                      get_post('min_price_supplier_id'), get_post('item_code'), get_post('pr_po_item_description'),
                                      get_post('project_id'), $project_site_id);

    return $ara;
}

function update_pr_allocation_details_line($selected_id)
{
    if(isset($_POST['order_unit']) && strlen($_POST['order_unit']) > 0){
        $order_unit = $_POST['order_unit'];
    }
    else{
        $item_edit_info = get_item_edit_info($_POST['item_code']);
        $order_unit = $item_edit_info['units'];
    }
    
    $order_unit_price_discount = input_num('order_unit_price_discount');
    
    get_convertion_factor2($_POST['item_code'], $_POST['order_unit'], $convesion_factor);
    $base_unit_order_qty = input_num('order_unit_qty') * $convesion_factor;
    $base_unit_order_price = (input_num('order_unit_price') * (1 - $order_unit_price_discount/100)) / $convesion_factor;
    
    $project_site_id = 0;
    if($_POST['pr_po_project_site_id'] > 0){
        $project_site_id = $_POST['pr_po_project_site_id'];
    }
       
    
    $ura = update_requisition_allocation($selected_id, get_post('pr_line_id'), $base_unit_order_qty, 
                                         $base_unit_order_price, $_POST['order_unit'], input_num('order_unit_qty'), 
                                         input_num('order_unit_price'), $convesion_factor, $order_unit_price_discount, 
                                         $_POST['supplier_id'],  get_post('line_loc_code'), $_POST['delivery_required_date'], 
                                         input_num('minimum_price'), input_num('minimum_price_discount'), get_post('min_price_supplier_id'), 
                                         get_post('pr_po_item_description'), get_post('project_id'), $project_site_id, 
                                         $_POST['row_state']);
    
    unset($_POST['selected_id']);

    return $ura;
}

function check_pr_allocation_line_data()
{
    if (!get_post('project_id') > 0)
	{
		display_error(_("No project selected."));
		set_focus('project_id');
		return false;
	}
    
	if (!get_post('pr_line_id') > 0)
	{
		display_error(_("No PR line selected."));
		set_focus('order_unit_qty');
		return false;
	}
    
    if (!check_num('order_unit_qty',0.001))
	{
		display_error(_("Order quantity entered is negative or invalid."));
		set_focus('order_unit_qty');
		return false;
	}
    
    if (strlen($_POST['order_unit_qty']) == 0) 
	{
		display_error(_("Order quantity can not be zero."));
		set_focus('order_unit_qty');
        return false;
	}
        
	if (!check_num('order_unit_price',0))
	{
		display_error(_("Order price entered is negative or invalid."));
		set_focus('order_unit_price');
		return false;
	}        

    if (!check_num('order_unit_price_discount',0, 100))
	{
		display_error(_("Order price discount entered should be numeric and be between 0 and 100."));
		set_focus('order_unit_price_discount');
		return false;
	} 
    
	if (!is_date($_POST['delivery_required_date'])) 
	{
		display_error(_("Delivery date entered is not a valid date."));
		set_focus('delivery_required_date');
        return false;
	}
    
    return true;

}

function display_pr_item_purchase_history($item_code, $description)
{
    if(!(strlen($description)>1)){
        $item = get_stock_item_edit_info($item_code);
        $description = $item['description'];
        $_POST['item_description'] = $description;
    }
    display_heading2('Purchase History of item '.$item_code.' - '.$description);
    $result = get_pr_item_purchase_history($item_code);
    
    start_table(TABLESTYLE, "width=96%");

    $th = array(_("PO#"), _("Supplier"), 
                _("Unit"), _("Qty"), _("Unit Price"), _("Discount"),  _("Delivery Date"),
                _("Supplier Address"), _("Contact Numbers"), _("Email") );

    table_header($th);
    $k = 0;
    
        while($row = db_fetch($result))
        {
            alt_table_row_color($k);
                label_cell(get_supplier_trans_view_str(ST_PR_LINE_ALLOCATIONS,$row["order_no"], $row["order_no"]),'align=center');
                label_cell($row['supp_name']);
                
                
                label_cell($row['order_unit']);
                label_cell($row['order_unit_qty'],'align=right');
                label_cell(number_format2($row['order_unit_price'],2),'align=right');
                label_cell(number_format2($row['order_unit_price_discount'],2),'align=right');
                label_cell(sql2date($row['delivery_date']),'align=center');
                
                label_cell($row['supp_address']);
                
                $string_contacts = generate_supplier_contacts($row['supplier_id']);
                label_cell($string_contacts['contacts']);
                label_cell($string_contacts['emails']);
                
                
            end_row();
        }
        
    end_table();
}

function generate_supplier_contacts($supplier_id)
{
    $contacts = get_crm_persons('supplier', null, $supplier_id);
    $string_contacts = '';
    $string_emails = '';
        while($con_row = db_fetch($contacts)){
            if(isset($con_row['phone']) && $con_row['phone'] > 0 ){
                $string_contacts .= ' '.$con_row['phone'].',';
            }
            if(isset($con_row['phone2']) && $con_row['phone2'] > 0 ){
                $string_contacts .= ' '.$con_row['phone2'].',';
            }
            
            if(isset($con_row['email']) && strlen($con_row['email']) > 0 ){
                $string_emails .= ' '.$con_row['email'].',';
            } 
        }
    
    $contacts = substr($string_contacts,0,strlen($string_contacts)-1);
    $emails = substr($string_emails,0,strlen($string_emails)-1);
    
    return array('contacts' => $contacts, 'emails' => $emails);
}


function can_delete_pr_allocation_line($selected_id)
{
    $pr_po = get_pr_allocation_record($selected_id);
    if(isset($pr_po['po_line_id']) && $pr_po['po_line_id'] > 0){
        display_error(_("A PO has been created for the selected line and it can not be deleted."));
        return false;
    }
    
    return true;
    
}

function delete_pr_allocation_line($selected_id)
{
    return delete_requisition_allocation_line($selected_id, true);

}


function close_pr_line($pr_line_id)
{
    complete_pr_line($pr_line_id);	
    
    return array("success" => true, "message" => "Selected PR line has been closed.");
}


?>






































































































































































































































































































































































































































































































































































































































































