<?php

function create_cart($data)
{
    global $Refs;


    $cart = new items_cart($data['trans_type']);
    $cart->order_id = 0;

    $cart->reference = $Refs->get_next(0);
    $cart->tran_date = $data['order_date'];
    if (!is_date_in_fiscalyear($cart->tran_date))
            $cart->tran_date = end_fiscalyear();
    
    $cart->memo_ = $data['comment'];


    return $cart;
}

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

function check_journal_header_data($cart)
{
        global $Refs;

	$input_error = 0;

	if ($cart->count_gl_items() < 1) {
		$msg = "You must enter at least one journal line.";
                return array("passed" => false, "message" => $msg);
	}
        
	if (abs($cart->gl_items_total()) > 0.0001)
	{
		$msg = "The journal must balance (debits equal to credits) before it can be processed.";
		return array("passed" => false, "message" => $msg);
	}

	if (!is_date($cart->tran_date)) 
	{
		$msg = "The entered date is invalid.";
		return array("passed" => false, "message" => $msg);
	} 
	elseif (!is_date_in_fiscalyear($cart->tran_date)) 
	{
		$msg = "The entered date is not in fiscal year.";
		return array("passed" => false, "message" => $msg);
	} 
	if (!$Refs->is_valid($cart->reference)) 
	{
		$msg = "You must enter a reference.";
		return array("passed" => false, "message" => $msg);
	} 
	elseif ($Refs->exists(ST_JOURNAL, $cart->reference)) 
	{
            $msg = "The entered reference is already in use.";
            return array("passed" => false, "message" => $msg);
	}
	
        return array("passed" => true, "message" => "");
}

function journal_check($data, &$cart)
{
	$cart = create_cart($data);
          
        foreach ($data['items'] as $item){
            $add_new_item_result = add_new_item($cart, $item);
            if($add_new_item_result["passed"] == false){
                return $add_new_item_result;
            }
        }
        
        $check_journal_header_data_result = check_journal_header_data($cart);
            return $check_journal_header_data_result;
        
}


function journal_add($cart)
{
        $trans_no = 0;
 
        foreach ($cart->gl_items as $item){
            add_gl_trans($cart->trans_type, $cart->order_id, $cart->tran_date, $item->code_id,
			$item->dimension_id, $item->dimension2_id,
			$item->reference, $item->amount);
        }
        
        $cart->clear_items();
        
        
        return array("passed" => true, "message" => "");


	


}

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

function check_item_data($item)
{
	if (isset($item['dimension_id']) && $item['dimension_id'] != 0 && dimension_is_closed($item['dimension_id'])) 
	{
		$msg = "Dimension is closed.";
    		return array("passed" => false, "message" => $msg);
	}

	if (isset($item['dimension2_id']) && $item['dimension2_id'] != 0 && dimension_is_closed($item['dimension2_id'])) 
	{
		$msg = "Dimension is closed.";
		return array("passed" => false, "message" => $msg);
	}
        
        if(!is_numeric($item['amount_debit'])){
            $msg = "Debit amount entered is not numeric.";
		return array("passed" => false, "message" => $msg);
        }
        
        if(!is_numeric($item['amount_credit'])){
            $msg = "Credit amount entered is not numeric.";
		return array("passed" => false, "message" => $msg);
        }
        
	if (!($item['amount_debit']!=0 ^ $item['amount_credit']!=0) )
	{
		$msg = "You must enter either a debit amount or a credit amount.";
		return array("passed" => false, "message" => $msg);
  	}

	if($item['amount_debit'] > 0) 
	{
            if($item['amount_credit'] > 0)
            {
    		$msg = "The credit amount entered should be less than zero.";
		return array("passed" => false, "message" => $msg);
            }
                
  	}
        
        if($item['amount_credit'] > 0) 
	{
            if($item['amount_debit'] > 0) 
            {
    		$msg = "The debit amount entered should be less than zero.";
		return array("passed" => false, "message" => $msg);
            }
  	}
	
	if (!is_tax_gl_unique($item['code_id'])) {
   		$msg = "Cannot post to GL account used by more than one tax type.";
		return array("passed" => false, "message" => $msg);
	}

	if (!$_SESSION["wa_current_user"]->can_access('SA_BANKJOURNAL') && is_bank_account($item['code_id'])) 
	{
		$msg = "You cannot make a journal entry for a bank account. Please use one of the banking functions for bank transactions.";
		return array("passed" => false, "message" => $msg);
	}

   	return array("passed" => true, "message" => "");
}

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


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

function add_new_item($cart, $item)
{
	$item_check_result = check_item_data($item);
        if($item_check_result["passed"] == false)
            return $item_check_result;

	if ($item['amount_debit'] > 0)
		$amount = $item['amount_debit'];
	else
		$amount = -$item['amount_credit'];
	
	$add_gl_item_result = $cart->add_gl_item($item['code_id'], $item['dimension_id'],
                                        $item['dimension2_id'], $amount, $item['LineMemo']);
        if($add_gl_item_result == false){
            return array("passed" => false, "message" => "Could not add line item. add_gl_item returned false.");
        }
        else{
            return array("passed" => true, "message" => "");
        }
	
}

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



?>
