<?php

function load_pr_line_details($selected_id, &$myrow)
{
    $current_rec_info = array("updated_type" => get_post('update_type'), "record_id" => $selected_id);
    $previous_rec_info = get_session_variable("pr_allocation_record_info");
    
    $refesh_type = "";
    if(isset($previous_rec_info["updated_type"]) && isset($previous_rec_info["record_id"]) && 
                    $current_rec_info["updated_type"] == $previous_rec_info["updated_type"] && 
                    $current_rec_info["record_id"] == $previous_rec_info["record_id"]){
        
        $refesh_type = "SAME_RECORD";
    }
    
    if($selected_id == -1){
        $_POST['pr_no']  = "";
        $_POST['pr_line_id']  = "";
        $_POST['pr_po_line_id']  = "";
        $_POST['editable_descri'] = false;
        $_POST['pr_po_item_code']  = "";
        $_POST['pr_po_item_description']  = "";
        $_POST['project_id']  = "";
        $_POST['project_description']  = "";
        $_POST['task_description']  = "";
        $_POST['item_code']  = "";
        $_POST['requested_quantity']  = "";
        $_POST['base_unit']  = "";
        $_POST['estimate_price']  = "";
        $_POST['description']  = "";
        $_POST['line_comment']  = "";
        $_POST['pr_location']  = "";
        $_POST['requested_unit']  = "";
        $_POST['requested_unit_qty']  = "";
        $_POST['required_date']  = "";
        $_POST['req_unit_conversion_factor']  = "";
        $_POST['requested_quantity'] = "";
        $_POST['project_site_id'] = "";
        $_POST['project_site_name'] = "";
    }
    else{
        $myrow = get_requisition_detail(get_post('update_type'), $selected_id);

        $_POST['pr_no']  = $myrow["pr_no"];
        $_POST['pr_line_id']  = $myrow["pr_line_id"];
        $_POST['pr_po_line_id']  = $myrow["pr_po_line_id"];
        $_POST['editable_descri'] = $myrow["editable_descri"];
        $_POST['pr_po_item_code']  = $myrow["pr_po_item_code"];
//        $_POST['pr_po_item_description']  = $myrow["pr_po_item_description"];
        $_POST['project_id']  = $myrow["project_id"];
        $_POST['project_description']  = $myrow["project_description"];
        $_POST['task_description']  = $myrow["task_description"];
        $_POST['item_code']  = $myrow["item_code"];
        $_POST['requested_quantity']  = $myrow["requested_quantity"];
        $_POST['base_unit']  = $myrow["base_unit"];
        $_POST['estimate_price']  = $myrow["estimate_price"];
        
        if(isset($myrow["pr_po_item_description"]) && strlen($myrow["pr_po_item_description"]) > 0){
            $_POST['description']  = $myrow["pr_po_item_description"];
        }
        elseif(isset($myrow["item_pr_description"]) && strlen($myrow["item_pr_description"]) > 0){
            $_POST['description']  = $myrow["item_pr_description"];
        }
        else{
            $_POST['description']  = $myrow["description"];
        }
        $_POST['line_comment']  = $myrow["line_comment"];
        $_POST['pr_location']  = $myrow["pr_loc_code"]." - ".$myrow["pr_location_name"];
        $_POST['requested_unit']  = $myrow["requested_unit"];
        $_POST['requested_unit_qty']  = $myrow["requested_unit_qty"];
        $_POST['required_date']  = $myrow["required_date"];
        $_POST['req_unit_conversion_factor']  = $myrow["req_unit_conversion_factor"];
        $_POST['requested_quantity'] = $myrow['requested_quantity'];
        $_POST['project_site_id'] = $myrow['project_site_id'];
        $_POST['project_site_name'] = $myrow['project_site_name'];
        $_POST['quantity_ordered'] = $myrow['quantity_ordered'];

        if(get_post("update_type") == "new_line"){
            if($refesh_type == "SAME_RECORD"){
                $_POST['pr_po_item_description']  = $_POST["description"];
            }
            else{
                $_POST['pr_po_item_description']  = $myrow["item_description"];
            }
        }
        else{ //update_pr_po_line
            if($refesh_type == "SAME_RECORD"){
                $_POST['pr_po_item_description']  = $_POST["pr_po_item_description"];
            }
            else{
                $_POST['pr_po_item_description']  = $myrow["pr_po_item_description"];
            }
        }

        
        $_POST['quantity_ordered'] = $myrow['quantity_ordered'];

        if(get_post("update_type") == "new_line"){
            if($refesh_type == "SAME_RECORD"){
                $_POST['supplier_id']  = $_POST["supplier_id"];
            }
            else{
                $_POST['supplier_id']  = "";
            }
        }
        else{ //update_pr_po_line
            if($refesh_type == "SAME_RECORD"){
                $_POST['supplier_id']  = isset($_POST["supplier_id"]) ? $_POST["supplier_id"] : "";
            }
            else{
                $_POST['supplier_id']  = $myrow["supplier_id"];
            }
        }

        
        if(get_post("update_type") == "new_line"){
            if($refesh_type == "SAME_RECORD"){
                $_POST['line_loc_code']  = isset($_POST["line_loc_code"]) ? $_POST["line_loc_code"] : "";
            }
            else{
                $_POST['line_loc_code']  = $myrow["pr_loc_code"];
            }
        }
        else{ //update_pr_po_line
            if($refesh_type == "SAME_RECORD"){
                $_POST['line_loc_code']  = isset($_POST["line_loc_code"]) ? $_POST["line_loc_code"] : "";
            }
            else{
                $_POST['line_loc_code']  = $myrow["line_loc_code"];
            }
        }        

        if(get_post("update_type") == "new_line"){
            if($refesh_type == "SAME_RECORD"){
                $_POST['pr_po_project_site_id']  = isset($_POST["pr_po_project_site_id"]) ? $_POST["pr_po_project_site_id"] : "";
            }
            else{
                $_POST['pr_po_project_site_id']  = $myrow["project_site_id"];
            }
        }
        else{ //update_pr_po_line
            if($refesh_type == "SAME_RECORD"){
                $_POST['pr_po_project_site_id']  = isset($_POST["pr_po_project_site_id"]) ? $_POST["pr_po_project_site_id"] : "";
            }
            else{
                $_POST['pr_po_project_site_id']  = $myrow["pr_po_project_site_id"];
            }
        }   
        
        
        if(get_post("update_type") == "new_line"){
            if($refesh_type == "SAME_RECORD"){
                $_POST['order_unit']  = isset($_POST["order_unit"]) ? $_POST["order_unit"] : "";
            }
            else{
                $_POST['order_unit']  = $myrow["requested_unit"];
            }
        }
        else{ //update_pr_po_line
            if($refesh_type == "SAME_RECORD"){
                $_POST['order_unit']  = isset($_POST["order_unit"]) ? $_POST["order_unit"] : "";
            }
            else{
                $_POST['order_unit']  = $myrow["order_unit"];
            }
        }
        
        if(get_post("update_type") == "new_line"){
            if($refesh_type == "SAME_RECORD"){
                $_POST['order_unit_qty']  = isset($_POST["order_unit_qty"]) ? $_POST["order_unit_qty"] : "";
            }
            else{
                $_POST['order_unit_qty']  = $myrow["requested_unit_qty"];
            }
        }
        else{ //update_pr_po_line
            if($refesh_type == "SAME_RECORD"){
                $_POST['order_unit_qty']  = isset($_POST["order_unit_qty"]) ? $_POST["order_unit_qty"] : "";
            }
            else{
                $_POST['order_unit_qty']  = $myrow["order_unit_qty"];
            }
        }
        
        $_POST['order_quantity']  = $myrow["order_quantity"];
        
        if(get_post("update_type") == "new_line"){
            if($refesh_type == "SAME_RECORD"){
                $_POST['order_line_required_date']  = isset($_POST["order_line_required_date"]) ? $_POST["order_line_required_date"] : "";
            }
            else{
                $_POST['order_line_required_date']  = ($myrow["required_date"]);
            }
        }
        else{ //update_pr_po_line
            if($refesh_type == "SAME_RECORD"){
                $_POST['order_line_required_date']  = isset($_POST["order_line_required_date"]) ? $_POST["order_line_required_date"] : "";
            }
            else{
                $_POST['order_line_required_date']  = ($myrow["order_line_required_date"]);
            }
        }
        
        
        if(get_post("update_type") == "new_line"){
            if($refesh_type == "SAME_RECORD"){
                $_POST['order_unit_price']  = isset($_POST["order_unit_price"]) ? $_POST["order_unit_price"] : "";
            }
            else{
                $_POST['order_unit_price']  = 0.00;
            }
        }
        else{ //update_pr_po_line
            if($refesh_type == "SAME_RECORD"){
                $_POST['order_unit_price']  = isset($_POST["order_unit_price"]) ? $_POST["order_unit_price"] : "";
            }
            else{
                $_POST['order_unit_price']  = $myrow["order_unit_price"];
            }
        }
    
        
        if(get_post("update_type") == "new_line"){
            if($refesh_type == "SAME_RECORD"){
                $_POST['order_unit_price_discount']  = $_POST["order_unit_price_discount"];
            }
            else{
                $_POST['order_unit_price_discount']  = 0.00;
            }
        }
        else{ //update_pr_po_line
            if($refesh_type == "SAME_RECORD"){
                $_POST['order_unit_price_discount']  = $_POST["order_unit_price_discount"];
            }
            else{
                $_POST['order_unit_price_discount']  = $myrow["order_unit_price_discount"];
            }
        }
        
        $_POST['row_state']  = $myrow["row_state"];

        hidden('pr_line_id', $_POST['pr_line_id']);
        hidden('row_state', $_POST['row_state']);
        hidden('selected_id', $selected_id);
    } 

    $res = get_item_edit_info(get_post('item_code'));
    $dec =  $res["decimals"] == '' ? 0 : $res["decimals"];
    $units = $res["units"] == '' ? _('kits') : $res["units"];
    
    
    $rec_info = array("updated_type" => get_post('update_type'), "record_id" => $selected_id);
    set_session_variable("pr_allocation_record_info", $rec_info);
    
}



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



function display_pr_line_details($selected_id, $pr_row)
{
    label_row("PR No:", $_POST["pr_no"]);
    label_row("PR Line No:", $_POST['pr_line_id']);
    

    if(isset($_POST['project_description'])){
        label_row("Project:", $_POST['project_description']);
    }
    else {
        label_row("Project:", '');
    }

    if(isset($_POST['task_description'])){
        label_row("Budget:", $_POST['task_description']);
    }
    else {
        label_row("Budeget:", '');
    }

    if(isset($_POST['item_code'])){
        label_row("Item Code:", $_POST['item_code']);
    }
    else {
        label_row("Item Code:", '');
    }
    hidden('item_code',get_post('item_code'));


    if(isset($_POST['description'])){
        label_row("Item Description:", $_POST['description']);
    }
    else {
        label_row("Item Description:", '');
    }

    if(isset($_POST['pr_location'])){
        //$_POST['pr_location']  = $myrow["pr_loc_code"]." - ".$myrow["pr_location_name"];
        label_row("PR Location:", $_POST['pr_location']);
    }
    else {
        label_row("PR Location:", '');
    }

    if(isset($_POST['requested_unit'])){
        label_row("Requested Unit:", $_POST['requested_unit']);
    }
    else {
        label_row("Requested Unit:", '');
    }

    $dec = 2;
    if(isset($_POST['requested_unit_qty'])){
        label_row("Requested Unit Qty:", number_format2($_POST['requested_unit_qty'],$dec));
    }
    else {
        label_row("Requested Unit Qty:", 0);
    }


    if(isset($_POST['estimate_price'])){
        label_row("Estimated Price:",number_format2($_POST['estimate_price'],2));
    }
    else {
        label_row("Estimated Price:", 0);
    }

    if(isset($_POST['required_date'])){
        label_row("Required Date:", sql2date($_POST['required_date']));
    }
    else {
        label_row("Required Date:", '');
    }

    if(isset($_POST['requested_quantity'])){
        label_row("Requested Quantity:", number_format2($_POST['requested_quantity'],2));
    }
    else {
        label_row("Requested Quantity:", '');
    }


    if(isset($_POST['quantity_ordered'])){
        label_row("Quantity Ordered:", number_format2($_POST['quantity_ordered'],2));
    }
    else {
        label_row("Quantity Ordered:", '');
    }

    if(isset($_POST['project_site_name'])){
        label_row("Site:", $_POST['project_site_name']);
    }
    else {
        label_row("Site:", '');
    }
    
    if(isset($_POST['line_comment'])){
        label_row("Line Comment:", $_POST['line_comment']);
    }
    else {
        label_row("Line Comment:", '');
    }
    
//    if(isset($_POST['pr_complete'])){
////        $pr_line_complete = $_POST['pr_complete'];
//        $pr_line_complete = get_yes_no_name($_POST['pr_complete']);
//    }
//    else {
//        $pr_line_complete = "No";
//    }
////    yesno_list_row("Close:", "pr_complete",$pr_line_complete);
//    label_row("Line Complete:", $pr_line_complete);
    
}




function display_pr_allocation_line_details($selected_id, $pr_row, $new_record_selection)
{
    hidden('project_id', $_POST['project_id']);
    
    if(get_post("pr_po_line_id") > 0){
        $pr_po_line_id = get_post("pr_po_line_id");
    }
    else{
        $pr_po_line_id = "New Allocation Line";
    }
    label_row("PR Allocation No:", $pr_po_line_id);
    
    label_row("Item Code::", $_POST['item_code']);
    
    
    if($_POST['editable_descri'] == true){
        $item_description = '';
        if(isset($_POST["pr_po_item_description"]) && strlen($_POST["pr_po_item_description"]) > 0){
            $item_description  = $_POST["pr_po_item_description"];
        }
        elseif(isset($_POST["item_pr_description"]) && strlen($_POST["item_pr_description"]) > 0){
            $item_description  = $_POST["item_pr_description"];
        }
        else{
            $item_description  = $_POST["description"];
        }
        $item_description  = $_POST["description"];
        text_row("Item Description :", 'pr_po_item_description', $item_description, 30, 200);
    }
    else{
        label_row("Item Description :", $_POST["description"]);
        hidden('pr_po_item_description', $_POST["description"]);
    }    
    
    
    supplier_list_row(_("Supplier :"), 'supplier_id', get_post('supplier_id', 0), "Select a Supplier", true);

    locations_list_row("Location:", 'line_loc_code', get_post('line_loc_code', ''), '');

    project_sites_list_row("Site:", "pr_po_project_site_id", $_POST['project_id'], null, '');
    
    $order_unit = "";
    if(isset($_POST['order_unit'])){
        $order_unit = $_POST['order_unit'];
    }

    if($order_unit == "")
    {
        $order_unit = get_post('requested_unit');
    }

    //$order_unit_conversion = get_convertion_factor($order_unit, get_post('item_code'));
    get_convertion_factor2(get_post('item_code'), $order_unit, $convesion_factor);
    
    hidden('order_unit_conversion_factor', $convesion_factor);

    $order_unit_qty = 0;

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

    if(get_post("update_type") == "update_pr_po_line"){
        $order_unit_qty = 0;
        if(list_updated('order_unit')){
            $order_unit_qty = $_POST['order_unit_qty'];
//            $order_unit_qty = $_POST['requested_quantity'] / $convesion_factor;
        }
        else{
            $order_unit_qty = get_post('order_unit_qty');
        }
    }

    $dec = 3;
    $_POST['order_unit_qty'] = number_format2($order_unit_qty, $dec);

    units_list_row(_("Order Unit:"), 'order_unit', get_post('item_code'), $order_unit, false, true);
    qty_row(_("Order Unit Qty:"), 'order_unit_qty', number_format2($order_unit_qty, $dec), '', null, $dec);

    if($new_record_selection){
        if(get_post('update_type') == "update_pr_po_line"){
            $_POST['delivery_required_date'] = sql2date($_POST['order_line_required_date']);
        }
        elseif(get_post('update_type') == "new_line"){
            $_POST['delivery_required_date'] = sql2date($_POST['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'];
        $_POST['order_unit_price'] = $order_unit_price;
        $_POST['order_unit_price_discount'] = $order_unit_price_discount;
    }

    if(isset($_POST['order_unit_price'])){
        $_POST['order_unit_price'] = number_format2(input_num('order_unit_price'), 2);
    }
    else{
        $_POST['order_unit_price'] = 0.00;
    }
    amount_row(_("Order Unit Price :"), 'order_unit_price', null, null, null, 2);
    
    if(isset($_POST['order_unit_price_discount'])){
        $_POST['order_unit_price_discount'] = number_format2(input_num('order_unit_price_discount'), 2);
    }
    else{
        $_POST['order_unit_price_discount'] = 0.00;
    }
    amount_row(_("Order Unit Price Discount:"), 'order_unit_price_discount', null, null, null, 2);
    
    table_section_title("Minimum Price");
    
    $order_unit_min_supplier_price = get_purchase_minimum_price($_POST['item_code'], $_POST['project_id'], get_post('order_unit'), $_POST['delivery_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;
    }

    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);

    $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));
    
}
//-----------------------------------------------------------------------------------


function display_open_purchase_requisitions($department_id)
{
    $result = get_open_requisition_details($department_id);
    
    start_table(TABLESTYLE, "width=98%");

    $th = array(_("PR#"), _("Project"), _("Location"), _("Item"), _("Req. Qty"), 
                _("Units"), _("Ord. Qty"), _("Required Date"), 
                _("Supplier"), _("Location"),_("Unit"), _("Qty"), _("Price"), _("Dis."), "", "", "");

    table_header($th);
    $k = 0;
    $previous_stock_id = "";
    $previous_pr_line_id = 0;
    $total_qty_ordered = 0;

    while ($myrow = db_fetch($result)) 
    {
        if($previous_pr_line_id == $myrow["pr_line_id"]){
            //$qty_ordered += $myrow["order_quantity"];
        }
        else{
            $ordered_qty = get_pr_line_ordered_qty($myrow["pr_line_id"]);
            $total_qty_ordered = $ordered_qty['total_qty_ordered'];
            $previous_stock_id = $myrow["item_code"];
        }

        $previous_stock_id = $myrow["item_code"];
        $previous_pr_line_id = $myrow["pr_line_id"];


        alt_table_row_color($k);
        $no = $myrow["pr_no"];
        label_cell(get_supplier_trans_view_str(ST_PURCH_REQ,$no, $no),"align=left");
        label_cell($myrow["project_description"]);
        label_cell($myrow["pr_loc_code"]);
        
        if(isset($myrow['pr_po_item_description']) && strlen($myrow['pr_po_item_description']) > 0){
            $item_description = $myrow['pr_po_item_description'];
        }
        elseif(isset($myrow['item_pr_description']) && strlen($myrow['item_pr_description']) > 0){
            $item_description = $myrow['item_pr_description'];
        }
        else{
            $item_description = $myrow['description'];
        }
        label_cell($myrow["item_code"]." - ".$item_description);
        label_cell(get_supplier_trans_view_str(ST_PR_DETAIL_INDENTS,$myrow["pr_line_id"], number_format($myrow["requested_unit_qty"],2)),"align=right");

        label_cell($myrow["requested_unit"]);
        
        if(isset($myrow['req_unit_conv_factor']) && $myrow['req_unit_conv_factor'] > 0){
            $req_unit_conv_factor = $myrow['req_unit_conv_factor'];
        }
        else{
            $req_unit_conv_factor = 1;
        }
        
        label_cell(get_supplier_trans_view_str(ST_PR_LINE_ALLOCATIONS,$myrow["pr_line_id"], number_format($total_qty_ordered/$req_unit_conv_factor,2)),"align=right");
        label_cell(sql2date($myrow["required_date"]), "align=center");
        //label_cell($myrow["line_comment"]);

        //$supplier_name = get_supplier_name($myrow['supplier_id']);
        label_cell($myrow['supplier_name']);
        label_cell($myrow['line_loc_code']);
        
        if($myrow["order_unit"] == ""){
            label_cell('');
        }
        else{
            label_cell($myrow["order_unit"]);
        }
        
        if($myrow["order_unit_qty"] == 0){
            label_cell('');
        }
        else{
            label_cell(number_format($myrow["order_unit_qty"], 3), "align=right");
        }
        
        if($myrow["order_unit_price"] == 0){
            label_cell('');
        }
        else{
            label_cell($myrow["order_unit_price"]);
        }
        
        if($myrow["order_unit_price_discount"] == 0){
            label_cell('');
        }
        else{
            amount_cell($myrow["order_unit_price_discount"]);
        }
        
//        label_cell($myrow['po_line_id']);

        if(isset($myrow['pr_line_id']) && $myrow['pr_line_id'] > 0){
            //edit_button_cell("EditNewLine".$myrow['pr_line_id'], _("New"));
            button_cell("EditNewLine".$myrow['pr_line_id'], _("New"), false, 'add.png');
        }
        else{
            label_cell('');
        }
        
        if(isset($myrow['pr_po_line_id']) && $myrow['pr_po_line_id'] > 0){
            if($myrow['po_line_id'] > 0){
                label_cell('');
                label_cell('');
            }
            else{
                edit_button_cell("EditUpdatePrPoLine".$myrow['pr_po_line_id'], _("Edit"));
                delete_button_cell("Delete".$myrow['pr_po_line_id'], _("Delete"));
            }
            
        }
        else{
            label_cell('');
            label_cell('');
        }
        
        end_row();
    }
    end_table(1);

    

}


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 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;
    
}

?>