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

$path_to_root = "..";
$page_security = 'SA_PURCHASEORDER';

include_once($path_to_root . "/purchasing/includes/po_class.inc");
include_once($path_to_root . "/purchasing/includes/ui/purchasing_ui.inc");

function build_new_po_header($trans_type, $trans_no, $supplier_id, $order_date, $location, $project_id)
{
	global $Refs;

	if (isset($_SESSION['PO']))
		unset ($_SESSION['PO']);

	$cart = new purch_order;

//	if (!is_date_in_fiscalyear($order_date)){
//		$order_date = end_fiscalyear();
//    }
        
    get_supplier_details_to_order($cart, $supplier_id); 
        
    get_payment_terms_to_order($cart, $supplier_id);
            
	$cart->due_date = $cart->orig_order_date = $order_date;

	$cart->trans_type = $trans_type;
	$cart->order_no = $trans_no;
    $cart->project_id =  $project_id;
    $cart->Location =  $location;
    $cart->task_id =  0;
    $cart->generated_po =  1;
    
    $comp_currency = get_company_currency();
        
    if ($cart->curr_code != $comp_currency){
        $rate = get_date_exchange_rate($currency, $date('Y/m/d'));
        $cart->ex_rate = $rate;
    }
    else{
        $cart->ex_rate = 1;
    }            

	$cart->reference = $Refs->get_next($trans_type);
        
        $cart->Location = $location;
        
        $result = get_po_delivery_address($location);
        
        if (db_num_rows($result) == 1)
        {
            $loc_row = db_fetch($result);
            $cart->delivery_address = $loc_row["delivery_address"];
        }
        else
        { 
            $cart->delivery_address = "";
        }
        
	$_SESSION['PO'] = &$cart;
}

function add_built_po_line_to_cart($cart, $stock_id, $description, $order_quantity, $order_price,$req_del_date,$pr_line_id, $requested_unit, 
                                            $requested_unit_qty, $req_unit_conv_factor, 
                                            $minimum_price, $minimum_price_discount, $min_price_sup_id, $order_unit_price, $order_unit_price_discount,
                                            $project_site_id, $tax_group_id, $tax_included)
{
    $new_line_no = count($cart->line_items);
    
    $cart->add_to_order ($new_line_no, $stock_id, $order_quantity, 
					$description, $order_price, null, $req_del_date, 0, 0, 0, $pr_line_id, 0, $requested_unit, $requested_unit_qty, $req_unit_conv_factor,
                    $minimum_price, $min_price_sup_id, $order_unit_price);
    
    $cart->line_items[$new_line_no]->project_site_id = $project_site_id;
    $cart->line_items[$new_line_no]->order_unit_price_discount = $order_unit_price_discount;
    $cart->line_items[$new_line_no]->minimum_price_discount = $minimum_price_discount;
    $cart->line_items[$new_line_no]->tax_group_id = $tax_group_id;
    $cart->line_items[$new_line_no]->tax_included = $tax_included;
    
}

function validate_po_line_data($stock_id, $line_location, $order_quantity, $order_price, $req_del_date)
{
    
    if (!(strlen($stock_id) > 0))
    {
        return array("success" => false, "message" => "No item selected.");   
    }

    if (!(strlen($line_location) > 0))
    {
        return array("success" => false, "message" => "No Location selected for the item $stock_id.");   
    }
    
    $dec = get_qty_dec($stock_id);
    $min = 1 / pow(10, $dec);
    if ($order_quantity < $min)
    {
        $min = number_format2($min, $dec);
        return array("success" => false, "message" => "The quantity of the item $stock_id must be numeric and not less than ".$min);
    }

    if ($order_price < 0)
    {
        return array("success" => false, "message" => "The price of the item $stock_id must be numeric and not less than zero.");   
    }
    
    if (!is_date($req_del_date))
    {
        return array("success" => false, "message" => "The required delivery date of the item $stock_id is in an invalid format.");  	 
    }
     
    return array("success" => true, "message" => "");	
}

function can_commit_po($cart)
{
	global $Refs;

	if (!($cart->supplier_id) || !($cart->supplier_id > 0)) 
	{
		return array("success" => false, "message" => "There is no supplier selected.");
	}
    
    if (!($cart->project_id) || !($cart->project_id > 0)) 
	{
		return array("success" => false, "message" => "There is no project selected.");
	}
    
        if (!($cart->Location) || !(strlen($cart->Location) > 0)) 
	{
		return array("success" => false, "message" => "There is no Location selected.");
	}
	
	if (!is_date($cart->orig_order_date)) 
	{
		return array("success" => false, "message" => "The entered order date is invalid.");
	} 
	
	//if (!is_date_in_fiscalyear($cart->orig_order_date)) 
	//{
	//	return array("success" => false, "message" => "The entered order date is not in fiscal year");
	//}
    	
//        if (!is_new_reference($cart->reference, $cart->trans_type)) 
//        {
//            return array("success" => false, "message" => "The entered reference is already in use.");
//        }
	$ref_msg = reference_exists($cart->reference, $cart->trans_type);
        if($ref_msg){
            return array("success" => false, "message" => $ref_msg);
        }
        
	if ($cart->delivery_address == '')
	{
		return array("success" => false, "message" => "There is no delivery address specified.");
	} 
	if ($cart->Location == '')
	{
		return array("success" => false, "message" => "There is no location specified to move any items into.");
	} 
	if (!db_has_currency_rates($cart->curr_code, $cart->orig_order_date)){
		return array("success" => false, "message" => "No currency code selected.");
    }
        
	if ($cart->order_has_items() == false)
	{
        return array("success" => false, "message" => "The order cannot be placed because there are no lines entered on this order.");
	}
		
	return array("success" => true, "message" => "");
}


?>
