<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
//---------------------------------------------------------------------------
//
//	Entry/Modify Sales Invoice against single delivery
//	Entry/Modify Batch Sales Invoice against batch of deliveries
//
$page_security = 'SA_SALESINVOICE';
$path_to_root = "..";
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/admin/db/fiscalyears_db.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");

$js = "";
if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}
if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

$_SESSION['page_title'] = _($help_context = "Edit Customer Balance");
page($_SESSION['page_title'], TRUE, false, "", $js);

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

if (isset($_GET['AddedID'])) {
	display_notification(_("Cusomer Opening Balance has been Updated"), true);
	//display_footer_exit();

}

if (isset($_POST['Update'])) {
	$Ajax->activate('CustBal');
}

$selected_id = $_REQUEST['customer_id'];
if($selected_id){
    $myrow = get_customer($selected_id);
} else {
    display_error(_('NoCustomer selected'));
    exit();
}

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

function check_data()
{
	
	if (!isset($_POST['OpenningBalDate']) || !is_date($_POST['OpenningBalDate'])) {
		display_error(_("The entered  date is invalid."));
		set_focus('OpenningBalDate');
		return false;
	}
        
        if (!isset($_POST['branch_id']) || $_POST['branch_id'] == "") {
		display_error(_("The selected customer does not have any branches. Please create at least one branch."));
		set_focus('branch_id');
		return false;
	}
        
        if (!is_numeric(input_num('cust_bal'))) {
		display_error(_("The entered balance is not invalid."));
		set_focus('cust_bal');
		return false;
	}
	return true;
}

//-----------------------------------------------------------------------------
$cust_bal = 0;
$trans_no = 0;
$_POST['OpenningBalDate'] = get_fiscalyear_begin_for_date(date('d/m/Y'));
$ref = 'Opening Balance';
$bal_res = get_customer_opening_balance($selected_id);
if(db_num_rows($bal_res) > 0){
    $bal_row = db_fetch($bal_res);
    $cust_bal = $bal_row['ov_amount'];
    $trans_no = $bal_row['trans_no'];
    $_POST['OpenningBalDate'] = sql2date($bal_row['tran_date']);
}
if (isset($_POST['UpdateCustBal']) && check_data()) {
    $branch_id = $_POST['branch_id'];
    write_cust_opening_bal($selected_id,$branch_id,input_num('cust_bal'),$_POST['OpenningBalDate'],ST_CUSTOPENINGBAL,$trans_no,$ref);
    meta_forward($_SERVER['PHP_SELF'], "AddedID=1&customer_id=".$selected_id );
		
}

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

$comp_currency = get_company_currency();

start_form();

div_start('CustBal');
hidden('customer_id',$selected_id);
start_table(TABLESTYLE2);


label_row(_("Customer:"), $myrow["name"]);

customer_branches_list_row(_("Branch:"),
  $selected_id, 'branch_id', null, false, true, true, true);

start_row();
date_row(_("Balance Date:"), 'OpenningBalDate', '', true, 0, 0, 0, null, true);
end_row();

start_row();
amount_row(_("Opening Balance:"), 'cust_bal', $cust_bal, '', $comp_currency);
end_row();

end_table(1);

submit_center('UpdateCustBal', _("Update Bal"), true, '', 'default');

div_end();
end_form();
end_page(FALSE, false, false);

