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

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

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

    $pmt['DatePaid'] = $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'];

    return $pmt;
}


function supplier_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']."]"));
    }
    
    $check_inputs_msg = check_inputs($payment);
    
    if($check_inputs_msg["passed"] == false){
        return $check_inputs_msg;
    }
    //return;    
    $payment_no = add_supplier_payment($payment);   
    
    if($payment_no >0){
        return array("passed" => true, "message" => _($payment_no));
    }
    else{
        return array("passed" => false, "message" => _("Could not add supplier payment."));
    }
}

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

	//$new_pmt = !$_SESSION['alloc']->trans_no;
	//Chaitanya : 13-OCT-2011 - To support Edit feature

        $payment_id = write_supp_payment($payment['trans_no'], $payment['supplier_id'], $payment['bank_account'],
                                        $payment['DatePaid'], $payment['reference'], $payment['amount'], 
                                        $payment['discount'], $payment['memo_'], $payment['charge'], 
                                        $payment['bank_amount']);
          
        return $payment_id;

}


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

function check_inputs($payment)
{
	global $Refs;

	if (!$payment['supplier_id'] > 0){
		$msg = "There is no supplier selected.";
		return array("passed" => false, "message" => $msg);
	} 


        if (!is_numeric($payment['amount']) || ($payment['amount'] < 0)){
		$msg =(_("The entered amount is invalid or less than zero."));
		return array("passed" => false, "message" => $msg);
	}

	if (!is_numeric($payment['charge']) || ($payment['charge'] < 0)){
		$msg = (_("The entered charge is invalid or less than zero."));
		return array("passed" => false, "message" => $msg);
	}

	if ($payment['charge'] > 0) {
		$charge_acct = get_company_pref('bank_charge_act');
		if (get_gl_account($charge_acct) == false) {
			$msg = (_("The Bank Charge Account has not been set in System and General GL Setup."));
			return array("passed" => false, "message" => $msg);
		}	
	}

        if (!is_numeric($payment['discount']) || ($payment['discount'] < 0)){
		$msg = (_("The entered discount is invalid or less than zero."));
		return array("passed" => false, "message" => $msg);
	}

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

        if (!is_numeric($payment['bank_amount']) || ($payment['bank_amount'] < 0)){
		$msg = (_("The entered bank amount is zero or negative."));
		return array("passed" => false, "message" => $msg);
	}


   	if (!is_date($payment['DatePaid'])){
		$msg = (_("The entered date is invalid."));
		return array("passed" => false, "message" => $msg);
	} 
        
	elseif (!is_date_in_fiscalyear($payment['DatePaid'])){
		$msg = (_("The entered date is out of fiscal year or is closed for further data entry."));
		return array("passed" => false, "message" => $msg);
	}

	$limit = get_bank_account_limit($payment['bank_account'], $payment['DatePaid']);

	if (($limit !== null) && (floatcmp($limit, $payment['amount']) < 0)){
		$msg = (sprintf(_("The total bank amount exceeds allowed limit (%s)."), price_format($limit)));
		return array("passed" => false, "message" => $msg);
	}

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

	if (!is_new_reference($payment['reference'], ST_SUPPAYMENT)){
		$msg = (_("The entered reference is already in use."));
		return array("passed" => false, "message" => $msg);
	}

	if (!db_has_currency_rates(get_supplier_currency($payment['supplier_id']), $payment['DatePaid'], true)){
		$msg = "Cannot retrieve exchange rate for supplier currency.";
                return array("passed" => false, "message" => $msg);
        }
        
        return array("passed" => true, "message" => _(""));
}

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


?>