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

function check_payment_exists($payment_reference)
{
    $pr_row = get_payment_record($payment_reference);
    if($pr_row == null){
        return false;
    }
    else{
        return true;
    }
}

function can_process($payment)
{
	global $Refs;

	if (!$payment['customer_id'])
	{
		return array("passed" => false, "message" => _("There is no customer selected."));
	} 
	
	if (!$payment['branch_id'])
	{
		return array("passed" => false, "message" => _("This customer has no branch defined."));
	} 
	
	if (!is_date($payment['DateBanked'])) {
		return array("passed" => false, "message" => _("The entered date is invalid. Please enter a valid date for the payment."));

	} elseif (!is_date_in_fiscalyear($payment['DateBanked'])) {
		return array("passed" => false, "message" => _("The entered date is not in fiscal year."));
	}

	if (!$Refs->is_valid($payment['reference'])) {
		return array("passed" => false, "message" => _("You must enter a reference."));
	}

	//Chaitanya : 13-OCT-2011 - To support Edit feature
	if (isset($payment['trans_no']) && $payment['trans_no'] == 0 && (!is_new_reference($payment['reference'], ST_CUSTPAYMENT))) {
		return array("passed" => false, "message" => _("The entered reference is already in use."));
	}
	//Avoid duplicate reference while modifying
//	elseif ($payment['ref'] != $payment['old_ref'] && !is_new_reference($payment['ref'], ST_CUSTPAYMENT))
//	{
//		return array("passed" => false, "message" => _("The entered reference is already in use."));
//	}

	if (!is_numeric($payment['amount'])) {
		return array("passed" => false, "message" => _("The entered amount is invalid and cannot be processed."));
	}

        if ($payment['amount'] <= 0) {
        return array("passed" => false, "message" => _("The entered amount is negative or zero and cannot be processed."));
	}
        
	if (!is_numeric($payment['charge'])){
		return array("passed" => false, "message" => _("The entered amount is invalid and cannot be processed."));
	}
        
        if($payment['charge'] < 0){
		return array("passed" => false, "message" => _("The entered amount is negative and cannot be processed."));
        }
        
        
	if ( !is_numeric($payment['charge']) || !$payment['charge'] >= 0) {
		$charge_acct = get_company_pref('bank_charge_act');
		if (get_gl_account($charge_acct) == false) {
			return array("passed" => false, "message" => _("The Bank Charge Account has not been set in System and General GL Setup."));
		}	
	}

//	if (isset($_POST['_ex_rate']) && !check_num('_ex_rate', 0.000001))
//	{
//		display_error(_("The exchange rate must be numeric and greater than zero."));
//		set_focus('_ex_rate');
//		return false;
//	}

	if ($payment['discount'] == "") 
	{
		$payment['discount'] = 0;
	}

	if (!is_numeric($payment['discount'])) {
		return array("passed" => false, "message" => _("The entered discount is not a valid number."));
	}

	//if ((input_num('amount') - input_num('discount') <= 0)) {
	if ($payment['amount'] <= 0) {
		return array("passed" => false, "message" => _("The balance of the amount and discount is zero or negative. Please enter valid amounts."));
	}

	if (!is_numeric($payment['bank_amount']))
	{
		return array("passed" => false, "message" => _("The entered payment amount is invalid."));
	}

        if ($payment['bank_amount'] <= 0)
	{
		return array("passed" => false, "message" => _("The entered payment amount is zero or negative."));
	}
        
	if (!db_has_currency_rates(get_customer_currency($payment['customer_id']), $payment['DateBanked'], true))
		return array("passed" => false, "message" => _("Cannot retrieve exchange rate for currency."));

	//$_SESSION['alloc']->amount = input_num('amount');

//	if (isset($_POST["TotalNumberOfAllocs"]))
//		return check_allocations();
//	else
		return array("passed" => true, "message" => _(""));
}

function load_payment_data($info){
    
    $pmt['trans_no'] = 00;
    $pmt['customer_id'] = $info['customer_id'];
    $pmt['branch_id'] = $info['branch_id'];
    
    $pmt['bank_account'] = $info['bank_account_id'];

    $pmt['DateBanked'] = $info['payment_date'];
    
    $pmt['reference'] = $info['reference'];
    $pmt['amount'] = $info['amount'];
    $pmt['memo_'] = $info['comment'];
    $pmt['charge'] = $info['banck_charge'];
    $pmt['bank_amount'] = $info['bank_amount'];
    
    $pmt['discount'] = $info['discount'];

    $pmt['order_category_id'] = $info['order_category_id'];
    
    return $pmt;
}


function customer_payment_add($info){
    
    $payment = load_payment_data($info);
    
    if(check_payment_exists($payment['reference']) == true){
        return  array("passed" => false, "message" => _("Payment record exisits [".$payment['reference']."]"));
    }
    
    $validation_result = can_process($payment);
    
    if($validation_result["passed"] == false){
        return $validation_result;
    }
    //return;    
    $payment_no = add_payment($payment);   
    
    if($payment_no >0){
        return array("passed" => true, "message" => _($payment_no));
    }
    else{
        return array("passed" => false, "message" => _("Could not add customer payment."));
    }
}

        
function add_payment($payment)
        {
	new_doc_date($payment['DateBanked']);

	//$new_pmt = !$_SESSION['alloc']->trans_no;
	//Chaitanya : 13-OCT-2011 - To support Edit feature
	$payment_no = write_customer_payment($payment['trans_no'], $payment['customer_id'], $payment['branch_id'],
                                            $payment['bank_account'], $payment['DateBanked'], $payment['reference'],
                                            $payment['amount'], $payment['discount'], $payment['memo_'], 
                                            0, $payment['charge'], $payment['bank_amount'], $payment['order_category_id']);
        
        return $payment_no;

}




?>