<?php
include_once($path_to_root . "/purchasing/includes/db/purchase_requisitions_db.inc");


function add_pr($pr_cart,$release = false)
{
    clear_session_variable('pr_messages');

	$apr_msg = add_purchase_requisition($pr_cart, $pr_cart->line_items, $pr_cart->pr_reference, $pr_cart->requesting_location, $pr_cart->pr_complete, 
                                            $pr_cart->project_id, $pr_cart->pr_date, $release, $pr_cart->source_type, $pr_cart->source_order_id);

	return $apr_msg;
} 

function update_pr($pr_cart, $release = false)
{
    clear_session_variable('pr_messages');
 
	$upr_msg = update_purchase_requisition($pr_cart->order_id,$pr_cart, $pr_cart->pr_reference, $pr_cart->requesting_location, $pr_cart->pr_complete, 
                                               $pr_cart->project_id, $pr_cart->pr_date,  $release, $pr_cart->row_state);

    return $upr_msg;
}

function validate_pr_item_data($pr_cart)
{
	global $Refs;

//	$adj = &$_SESSION['pr_items'];
    
    $user_id = $_SESSION['wa_current_user']->user;
    $cap = can_access_project($pr_cart->project_id, $user_id);
    if($cap['success'] !== true) 
    {
        $message = $cap["message"];
        $set_focus = "project_id";
        return array("success" => false, "message" => $message, "focus_to" => $set_focus);
    }
        
    if($pr_cart->order_id > 0){
        $pr_info = get_pr_edit_info($pr_cart->order_id);
        if(!($pr_info['approval_status'] == AS_NEW || $pr_info['approval_status'] == AS_WITH_THE_APPROVER || $pr_info['approval_status'] == AS_AWAITING_APPROVAL)){
            $message = ("This Purchase Requisition status does not allow further updating.");
            $set_focus = "";
            return array("success" => false, "message" => $message, "focus_to" => $set_focus);
        }
                
        $ced_msg = can_edit_document($pr_cart->trans_type, $pr_cart->project_id, $pr_cart->next_approval_group_id, 
                            $_SESSION['wa_current_user']->user, $pr_cart->created_by);
        if($ced_msg['success'] !== true){
                $message = ( $ced_msg['message']);
                $set_focus = ('issuing_location');
                return array("success" => false, "message" => $message, "focus_to" => $set_focus);
        }
    }
    
	if (count($pr_cart->line_items) == 0)	{
		$message = (_("You must enter at least one non empty item line."));
		$set_focus = ('stock_id');
		return array("success" => false, "message" => $message, "focus_to" => $set_focus);
	}
    
    if(!$pr_cart->order_id){
        if (!$Refs->is_valid($pr_cart->pr_reference)) 
        {
            $message = ( _("You must enter a reference."));
            $set_focus = ('pr_reference');
            return array("success" => false, "message" => $message, "focus_to" => $set_focus);
        }

        $ref_msg = reference_exists($pr_cart->pr_reference, ST_PURCH_REQ);        
        if($ref_msg)
        {
            $message = $ref_msg;
            $set_focus = ('pr_reference');
            return array("success" => false, "message" => $message, "focus_to" => $set_focus);
        }

    }
    if ($pr_cart->project_id == -1) 
	{
		$message = ( _("Select a project."));
		$set_focus = ('project_id');
		return array("success" => false, "message" => $message, "focus_to" => $set_focus);
	}
        
	return array("success" => true, "message" => "");
}


function check_pr_line_data($pr_line)
{
    
	if (!check_num2($pr_line['tran_qty'],0.01))
	{
		$message = (_("The quantity entered is negative or invalid."));
		$set_focus = ('qty');
		return array("success" => false, "meaasge" => $message, "focus_to" => $set_focus);
	}
        
    if (!check_num2($pr_line['estimate_price'],0))
	{
		$message = (_("The price entered is negative or invalid."));
		$set_focus = ('estimate_price');
		return array("success" => false, "meaasge" => $message, "focus_to" => $set_focus);
	}
    
    if (input_num2($pr_line['estimate_price']) == 0)
	{
		$message = (_("The price entered has to be greater than zero."));
		$set_focus = ('estimate_price');
		return array("success" => false, "meaasge" => $message, "focus_to" => $set_focus);
	}
        
    if (!is_date($pr_line['required_date'])) 
	{
		$message = (_("The entered date for the required date is invalid."));
		$set_focus = ('required_date');
		return array("success" => false, "meaasge" => $message, "focus_to" => $set_focus);
	} 
	
    if ($pr_line['task_id'] <= 0) 
	{
		$message = (_("Please select a budget."));
		$set_focus = ('task_id');
		return array("success" => false, "meaasge" => $message, "focus_to" => $set_focus);
	} 
    
//    if (!is_date_in_fiscalyear($pr_line['required_date'])) 
//	{
//		$message = (_("Entered date is not in fiscal year."));
//		$set_focus = ('required_date');
//		return array("success" => false, "meaasge" => $message, "focus_to" => $set_focus);
//	} 
    
   	return array("success" => true, "meaasge" => "");
}


function read_pr($pr_no, &$pr_cart)
{
    read_pr_header($pr_no, $pr_cart);
    read_pr_details($pr_cart);
}


function read_pr_header($pr_no, &$pr_cart)
{
        $myrow = get_pr_header($pr_no);
        
//        if(isset($_SESSION['pr_items'])){
//            $pr_cart = $_SESSION['pr_items'];
//        }
//        else{
            $pr_cart = new items_cart(ST_PURCH_REQ);
//        }
        
        $pr_cart->order_id = $myrow["pr_no"];
        $pr_cart->requesting_location = $myrow["location"];
        $pr_cart->reference = $myrow["reference"];
        $pr_cart->job_reference = $myrow["project_description"];
        $pr_cart->project_id = $myrow["project_id"];
        $pr_cart->pr_date = $myrow["pr_date"];
        $pr_cart->pr_complete = $myrow["pr_complete"];
        $pr_cart->approval_status = $myrow["approval_status"];
        $pr_cart->requestor_name = $myrow["requestor_name"];
        $pr_cart->next_approval_group_id  = $myrow["next_approval_group_id"];
        $pr_cart->created_by  = $myrow["created_by"];
        $pr_cart->row_state  = $myrow["row_state"];
  
}


function read_pr_details(&$pr_cart)
{
    $pr_details = get_pr_details($pr_cart->order_id);

    $element_no = 0;
    while ($prd_row = db_fetch($pr_details)) 
    {
        $pr_line = new line_item($prd_row["item_code"], 0, $standard_cost=null, 
                $description=null,sql2date($prd_row["required_date"]), $prd_row["line_comment"],
                $comment=null, $prd_row["id"], $prd_row["issued_qty"], $prd_row["quantity"], 0, 0, null, 0, 
                null, null, null, null, null, $prd_row["requested_unit"], $prd_row["requested_unit_qty"], 
                $prd_row["req_unit_conv_factor"]);

        $pr_line->row_id = $prd_row["id"];
//      $pr_line->line_complete = $prd_row["line_complete"];
        $pr_line->created_by = $prd_row["created_by"];
        $pr_line->created_on = $prd_row["created_on"];
        $pr_line->category_id = $prd_row["category_id"];
//      $pr_line->po_line_id = $prd_row["po_line_id"];
//      $pr_line->order_quantity = $prd_row["order_quantity"];
//      $pr_line->order_price = $prd_row["order_price"];
//      $pr_line->supplier_id = $prd_row["supplier_id"];
        $pr_line->estimate_price = $prd_row["estimate_price"];
        $pr_line->task_id = $prd_row["task_id"];
        $pr_line->task_description  = $prd_row["task_description"];
        $pr_line->project_site_id = $prd_row["project_site_id"];
        $pr_line->project_site_description = $prd_row["project_site_description"];
        $pr_line->quantity_ordered = $prd_row["quantity_ordered"];

        $pr_cart->line_items[$element_no] = $pr_line;
        $element_no++;
    }

}

function can_process_pr_issue($im_cart)
{
	global $Refs;

    if($im_cart->pr_complete == 1)
    {
        $message = ("This Purchase Requisition has been generated from a Material Requisition. No material issuing allowed from this Purchase Requisition.");
        $set_focus = "";
        return array("success" => false, "set_focus" => $set_focus,"message" => $message);
    }
    
    $total_qty = 0;
    if($im_cart->approval_status == AS_COMPLETED)
    {
        $message = ("This Purchase Requisition has been completed. Can not issue materials against it.");
        $set_focus = '';
        return array("success" => false, "set_focus" => $set_focus,"message" => $message);
    }
    
    
    if(!($im_cart->approval_status == AS_APPROVED || $im_cart->approval_status == AS_ISSUED))
    {
        $message = ("Purchase Requisition is not approved.Can not issue materials.");
        $set_focus = '';
        return array("success" => false, "set_focus" => $set_focus,"message" => $message);
    }
    

    
	foreach ($im_cart->line_items as $line_item)
    {
        if (!check_num2($line_item->tran_qty,0))
        {
            $message = (_("The quantity entered for item [".$line_item->stock_id."] is negative or invalid."));
            $set_focus = ($line_item->row_id."_tran_qty");
            return array("success" => false, "set_focus" => $set_focus,"message" => $message);
        }
        $total_qty += $line_item->tran_qty;

        if($total_qty == 0 && $line_item->pr_complete == 1){
            $message = "close_only";
            return array("success" => true, "set_focus" => '',"message" => $message);
        }
    }

	if (!$Refs->is_valid($im_cart->reference)) 
	{
		$message = ( _("You must enter a reference."));
		$set_focus = ('pri_reference');
		return array("success" => false, "set_focus" => $set_focus,"message" => $message);
	}

    if (!is_new_reference($im_cart->reference, ST_PR_ISSUE)) 
    {
        $message = ( _("The entered reference is already in use."));
        $set_focus = ('pr_reference');
        return array("success" => false, "set_focus" => $set_focus,"message" => $message);
    }
        
    if (!($im_cart->issuing_location) > 0) 
	{
		$message = ( _("Please select an issuing location."));
		$set_focus = ('issuing_location');
		return array("success" => false, "set_focus" => $set_focus,"message" => $message);
	}

        
    $vtqd_msg = validate_transaction_quantity_details($im_cart);		
    if ($vtqd_msg['success'] !== true) 		
    {
        $message = ($vtqd_msg['message']);
        return array("success" => false, "set_focus" => $set_focus,"message" => $message);
    }
    
	return array("success" => true, "set_focus" => '',"message" => '');
}


function can_process_pr_issue_from_grn($pr_complete,$approval_status, $reference, $issuing_location)
{
	global $Refs;

    if($pr_complete == 1)
    {
        $message = ("This Purchase Requisition has been generated from a Material Requisition. No material issuing allowed for this Purchase Requisition.");
        $set_focus = "";
        return array("success" => false, "set_focus" => $set_focus,"message" => $message);
    }
    

    if(!($approval_status == AS_APPROVED || $approval_status == AS_ISSUED))
    {
        $message = ("Ststus of this Purchase Requisition does not allow issuing materials against it.");
        $set_focus = '';
        return array("success" => false, "set_focus" => $set_focus,"message" => $message);
    }

    if (!$Refs->is_valid($reference)) 
    {
            $message = ( _("You must enter a reference."));
            $set_focus = ('pri_reference');
            return array("success" => false, "set_focus" => $set_focus,"message" => $message);
    }

    $ref_msg = reference_exists($reference, ST_PR_ISSUE);
    if($ref_msg){
        $message = "Please contact system administrator. ".$ref_msg;
        $set_focus = 'reference';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
//    if (!is_new_reference($reference, ST_PR_ISSUE)) 
//    {
//        $message = ( _("The entered reference is already in use."));
//        $set_focus = ('pr_reference');
//        return array("success" => false, "set_focus" => $set_focus,"message" => $message);
//    }
        
    if (!($issuing_location) > 0) 
    {
            $message = ( _("Please select an issuing location."));
            $set_focus = ('issuing_location');
            return array("success" => false, "set_focus" => $set_focus,"message" => $message);
    }

        
//    $vtqd_msg = validate_transaction_quantity_details($im_cart);		
//    if ($vtqd_msg['success'] !== true) 		
//    {
//        $message = ($vtqd_msg['message']);
//        return array("success" => false, "set_focus" => $set_focus,"message" => $message);
//    }
    
	return array("success" => true, "set_focus" => '',"message" => '');
}


//-----------------------------------------------------------------------------------------------
?>