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


function display_gl_bank_transactions_import_file_data(&$file_data, $validate = false, $import = false)
{
    start_table(TABLESTYLE, "width=98%");    
    $th = array("Is Payment", "Date", "Reference", "From / Pay To Type", 
                    "Customer / Supplier Code", "Customer / Supplier Name", "Branch name",
                    "Bank Account No", "GL Account Code", "Amount", "Memo", "Message");
              

    table_header($th);
    
    foreach ($file_data as &$line){    

        $glt = array();
        
        if($validate || $import){
            validate_gl_bank_transactions_import_data($line, $glt);
        }

        if($import && $line['success'] === true){         
            
            $msg = create_gl_bank_transaction($glt);
            if($msg['success'] === true){
                $line['success'] = true;
                $line['message'] = "imported. Transaction #".$msg['trans_no'];
            }
            else{
                $line['success'] = false;
                $line['message'] = "<font color='red'>".$msg['message']."</font>";
            }

        }

        display_manual_project_invoice_details_import_data_array($line);   
    }
    
    end_table();
    
}
    

   
    
function validate_gl_bank_transactions_import_data(&$line, &$glt)
{
    $success = true;
    
    if(in_array($line['is_payment'], array("1", "Y", "Yes"))){
        $glt['tran_type'] = ST_BANKPAYMENT;
    }
    elseif(in_array($line['is_payment'], array("0", "N", "No"))){
        $glt['tran_type'] = ST_BANKDEPOSIT;
    }
    else{
        $success = false;
        $m = "Incorrect payment or deposit";
        $line['is_payment'] .= "<font color='red'>".$m."</font>";
    }

            
    if(is_date($line['tran_date'])){
        $glt['tran_date_'] = $line['tran_date'];
    }
    else{
        $success = false;
        $m = "Invalid date";
        $line['tran_date'] .= "<font color='red'>".$m."</font>";
    }
    
       
    $glt['reference'] = trim($line['reference']);        
    
    
    if(strtoupper($line['from_or_to_type']) == "CUSTOMER"){
        $glt['from_or_to_type'] = PT_CUSTOMER;
    }
    elseif(strtoupper($line['from_or_to_type']) == "SUPPLIER"){
        $glt['from_or_to_type'] = PT_SUPPLIER;
    }
    elseif(strtoupper($line['from_or_to_type']) == "MISCELLANEOUS"){
        $glt['from_or_to_type'] = PT_MISC;
    }
    else{
        $success = false;
        $m = "Invalid From / Pay To type";
        $line['from_or_to_type'] .= "<font color='red'>".$m."</font>";
    }
    
    if(strtoupper($line['from_or_to_type']) == "CUSTOMER"){
            $msg = get_customer_id_by_cust_ref($line['customer_supplier_ref']);
            if($msg['success'] === true){
                    $glt['customer_supplier_id'] = $msg['message'];

                    if(strlen(trim($line['customer_branch_name'])) > 0){
                        $msg = get_customer_branch_id_by_name($line['customer_branch_name'], $glt['customer_supplier_id']);
                    }
                    else{
                        $msg = get_customer_default_branch_id($glt['customer_supplier_id']);
                    }
                    
                    if($msg['success'] === true){
                        $glt['branch_code'] = $msg['message'];
                        $glt['tax_group_id'] = $msg['tax_group_id'];
                        
                        if(strlen(trim($line['customer_branch_name'])) == 0){
                            $line['customer_branch_name'] = $msg['name'];
                        }
                    }
                    else{
                        $success = false;
                        $m = " ".$msg['message'];
                        $line['customer_branch_name'] .= "<font color='red'>".$m."</font>";
                    }
            }
            else{
                $success = false;
                $m = " ".$msg['message'];
                $line['customer_supplier_ref'] .= "<font color='red'>".$m."</font>";
            }
    }
    elseif(strtoupper($line['from_or_to_type']) == "SUPPLIER"){
            $msg = get_supplier_id_by_supp_ref($line['customer_supplier_ref']);
            if($msg['success'] === true){
                    $glt['customer_supplier_id'] = $msg['message'];
                    $glt['branch_code'] = -1;
            }
            else{
                $success = false;
                $m = " ".$msg['message'];
                $line['customer_supplier_ref'] .= "<font color='red'>".$m."</font>";
            }
    }
    elseif(strtoupper($line['from_or_to_type']) == "MISCELLANEOUS"){
            $success = false;
            $m = " Unsupported From / Pay To Type";
            $line['customer_supplier_ref'] .= "<font color='red'>".$m."</font>";
            
    }
    else{
            $success = false;
            $m = " Unsupported From / Pay To Type";
            $line['customer_supplier_ref'] .= "<font color='red'>".$m."</font>";
    }

    
    $glt['customer_supplier_name'] = $line['customer_supplier_name'];
    
    
    $msg = get_bank_account_id_by_account_no($line['bank_acc_no']);
    if($msg['success'] === true){
        $glt['bank_acc_id'] = $msg['message'];
    }
    else{
        $success = false;
        $m = " ".$msg['message'];
        $line['bank_acc_no'] .= "<font color='red'>".$m."</font>";
    }    
     
    
    $gl_acc = get_gl_account($line['line_gl_code']);
    if(isset($gl_acc['account_code']) && strlen($gl_acc['account_code']) > 2){
        $glt['line_gl_code'] = $gl_acc['account_code'];
    }
    else{
        $success = false;
        $m = " Unknown gl account code";
        $line['line_gl_code'] .= "<font color='red'>".$m."</font>";
    }    
     
    if(is_numeric($line['line_amount']) && $line['line_amount'] > 0){
        $glt['line_amount'] = input_num2($line['line_amount']);
    }
    else{
        $success = false;
        $m = "Invalid amount";
        $line['line_amount'] .= "<font color='red'>".$m."</font>";
    }
    
    
    $glt['memo'] = $line['memo'];
    
              
    if($success === true){
        $line['success'] = true;
        $line['message'] = "Can import";
    }
    else{
        $line['success'] = false;
        $line['message'] .= "<font color='red'>Can not import</font>";
    }
}


function display_manual_project_invoice_details_import_data_array($row)
{
    $j = $k = 0;
  
    if($row == ''){
        return;
    }

    //header
    label_cell($row['is_payment']);
    label_cell($row['tran_date']);
    label_cell($row['reference']);
    label_cell($row['from_or_to_type']);
    label_cell($row['customer_supplier_ref']);

    label_cell($row['customer_supplier_name']);
    label_cell($row['customer_branch_name']);
    label_cell($row['bank_acc_no']);
    label_cell($row['line_gl_code']);
    
    if($row['line_amount'] > 0){
        label_cell(number_format($row['line_amount'], 2), 'align=right');
    }
    else{
        label_cell($row['line_amount']);
    }
    label_cell($row['memo']);
    
    
    label_cell($row['message']);

    end_row();

}



function read_gl_bank_transaction_details_import_file_data(&$file_data, $line)
{
    $l = $line;

    //Is Payment	Date	Reference	From / Pay To Type	Customer / Supplier Code	Customer / Supplier Name	Bank Account No	GL Account Code	Amount	Memo

    $is_payment = str_replace('"', '', $l[0]);
    $tran_date = str_replace('"', '', $l[1]);
    $reference = str_replace('"', '', $l[2]);                    
    $from_or_to_type = str_replace('"', '', $l[3]);
    $customer_supplier_ref = str_replace('"', '', $l[4]);
    
    $customer_supplier_name = str_replace('"', '', $l[5]);
    $customer_branch_name = str_replace('"', '', $l[6]);
    $bank_acc_no = str_replace('"', '', $l[7]);    

    
    $line_gl_code = str_replace('"', '', $l[8]);
    $line_amount = str_replace('"', '', $l[9]);
    $memo = str_replace('"', '', $l[10]);
    

    $file_data_line = array("is_payment" => $is_payment , 
                        "tran_date" => $tran_date, 
                        "reference" => $reference, 
                        "from_or_to_type" => $from_or_to_type,
                        "customer_supplier_ref" => $customer_supplier_ref, 
                        "customer_supplier_name" => $customer_supplier_name, 
                        "customer_branch_name" => $customer_branch_name, 
                        "bank_acc_no" => $bank_acc_no,
                        "line_gl_code" => $line_gl_code, 
                        "line_amount" => $line_amount,
                        "memo" => $memo,                        
                        "message" => "");
    
    $file_data[] = $file_data_line;
}


function handle_new_item($glt)
{

	$amount = ($_SESSION['pay_items']->trans_type == ST_BANKPAYMENT ? 1:-1) * $glt['line_amount'];

	return $_SESSION['pay_items']->add_gl_item($glt['line_gl_code'], 0, 0, $amount, $glt['memo']);

}

function create_gl_bank_transaction($glt)
{
    
        create_cart($glt['tran_type'], 0);
        
        if($glt['reference'] != ""){
            $_SESSION['pay_items']->reference = $glt['reference'];
        }
        
        $msg = check_gl_bank_transactions_import_data($glt);
        {
            if($msg["success"] !== true){
                return array("success"=>false, "message"=>$msg["message"]);
            }
        }
        
    
        if(!handle_new_item($glt)){
            return array("success"=>false, "message"=>"Could not add details");
        }
        
	begin_transaction();

	$_SESSION['pay_items'] = &$_SESSION['pay_items'];

	$trans = write_bank_transaction(
		$_SESSION['pay_items']->trans_type, $_SESSION['pay_items']->order_id, $glt['bank_acc_id'],
		$_SESSION['pay_items'], $glt['tran_date_'],
		$glt['from_or_to_type'], $glt['customer_supplier_id'], $glt['branch_code'],
		$_SESSION['pay_items']->reference, $glt['memo'], true, $glt['line_amount']);


	$_SESSION['pay_items']->clear_items();
	unset($_SESSION['pay_items']);
        
        $gl_bank_trans_no = $trans[1];
        if($gl_bank_trans_no > 0){
            commit_transaction();
            return array("success"=>true, "trans_no"=>$gl_bank_trans_no, "message"=>"");	
        }
        else{
            cancel_transaction();
            return array("success"=>false, "trans_no"=>-1, "message"=>"Could not add gl bank transaction");
        }

}


function create_cart($type, $trans_no)
{
	global $Refs;

	if (isset($_SESSION['pay_items']))
	{
		unset ($_SESSION['pay_items']);
	}

	$cart = new items_cart($type);
        $cart->order_id = $trans_no;


	$cart->reference = $Refs->get_next($cart->trans_type);
        $cart->tran_date = new_doc_date();

	$_SESSION['pay_items'] = &$cart;
}



function check_gl_bank_transactions_import_data(&$glt)
{

        $m = reference_exists($_SESSION['pay_items']->reference, $glt['tran_type']);
	if ($m != "")
	{
                return array("success"=>false, "message"=>$m);
	}


	if (!db_has_currency_rates(get_bank_account_currency($glt['bank_acc_id']), $glt['tran_date_'], true)){
		return array("success"=>false, "message"=>$m);
        }

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