<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_GLSETUP';
$path_to_root="..";
include($path_to_root . "/includes/session.inc");

page(_($help_context = "System and General GL Setup"));

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");

include_once($path_to_root . "/admin/db/company_db.inc");

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

function can_process()
{
	if (!check_num('po_over_receive', 0, 100))
	{
		display_error(_("The delivery over-receive allowance must be between 0 and 100."));
		set_focus('po_over_receive');
		return false;
	}

	if (!check_num('po_over_charge', 0, 100))
	{
		display_error(_("The invoice over-charge allowance must be between 0 and 100."));
		set_focus('po_over_charge');
		return false;
	}

	if (!check_num('past_due_days', 0, 100))
	{
		display_error(_("The past due days interval allowance must be between 0 and 100."));
		set_focus('past_due_days');
		return false;
	}
        
	if (!(get_post('pos_invoice_def_project') > 0))
	{
		display_error(_("POS Invoice Default Project should be selected."));
		set_focus('pos_invoice_def_project');
		return false;
	}
        
	if (!(get_post('pos_invoice_def_customer') > 0))
	{
		display_error(_("POS Invoice Default Customer should be selected."));
		set_focus('pos_invoice_def_customer');
		return false;
	}

//	$grn_act = get_company_pref('grn_clearing_act');
//	if ((get_post('grn_clearing_act') != $grn_act) && db_num_rows(get_grn_items(0, '', true)))
//	{
//		display_error(_("Before GRN Clearing Account can be changed all GRNs have to be invoiced"));
//		$_POST['grn_clearing_act'] = $grn_act;
//		set_focus('grn_clearing_account');
//		return false;
//	}
    $s=is_account_balancesheet(get_post('retained_earnings_act'));
	if (!is_account_balancesheet(get_post('retained_earnings_act')) || is_account_balancesheet(get_post('profit_loss_year_act')))
	{
		display_error(_("The Retained Earnings Account should be a Balance Account or the Profit and Loss Year Account should be an Expense Account (preferred the last one in the Expense Class)"));
		return false;
	}
	return true;
}

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

if (isset($_POST['submit']) && can_process())
{
	update_company_prefs(get_post(array('retained_earnings_act', 'profit_loss_year_act',
		'debtors_act', 'pyt_discount_act', 'creditors_act', 'freight_act',
		'exchange_diff_act', 'bank_charge_act', 'default_sales_act', 'default_sales_discount_act',
		'default_prompt_payment_act', 'default_inventory_act', 
                'default_cogs_act', 'default_coc_act', 'default_cost_act',
		'default_adj_act', 'default_inv_sales_act', 'default_assembly_act', 'legal_text',
		'past_due_days', 'default_workorder_required', 'default_dim_required',
		'default_delivery_required', 'grn_clearing_act',
		'allow_negative_stock'=> 0, 'accumulate_shipping'=> 0,
		'po_over_receive' => 0.0, 'po_over_charge' => 0.0, 'default_credit_limit'=>0.0, 
                'sales_order_appr_group_id', 'work_order_appr_group_id', 'workcentre_scheduling_days_limit',
                'transport_exp_act', 'subcontract_exp_act', 'fi_balances_act', 'default_proj_cost_act', 'default_proj_revenue_act',
                'default_ub_exp_debit_act', 'default_ub_exp_credit_act', 
                'default_cash_act', 'default_pc_float_act', 'default_pc_expenses_act','cash_adjustments_act', 'emp_pc_advances_act',
                'default_fn_loans_act', 'default_fn_loan_interests_act', 'default_fn_loan_late_interests_act',
                'default_payroll_debit_act', 'default_payroll_credit_act','asset_intransit_location',
                'pos_invoice_def_project', 'pos_invoice_def_location', 'pos_invoice_def_float', 'pos_invoice_def_customer'

    )));

	display_notification(_("The general GL setup has been updated."));

} /* end of if submit */

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

start_form();

start_outer_table(TABLESTYLE2);

table_section(1);

if (get_company_pref('grn_clearing_act') === null) { // available from 2.3.1, can be not defined on pre-2.4 installations
	set_company_pref('grn_clearing_act', 'glsetup.purchase', 'varchar', 15, 0);
	refresh_sys_prefs();
}

$myrow = get_company_prefs();

$_POST['retained_earnings_act']  = $myrow["retained_earnings_act"];
$_POST['profit_loss_year_act']  = $myrow["profit_loss_year_act"];
$_POST['debtors_act']  = $myrow["debtors_act"];
$_POST['creditors_act']  = $myrow["creditors_act"];
$_POST['freight_act'] = $myrow["freight_act"];
$_POST['pyt_discount_act']  = $myrow["pyt_discount_act"];

$_POST['exchange_diff_act'] = $myrow["exchange_diff_act"];
$_POST['bank_charge_act'] = $myrow["bank_charge_act"];
$_POST['default_sales_act'] = $myrow["default_sales_act"];
$_POST['default_sales_discount_act']  = $myrow["default_sales_discount_act"];
$_POST['default_prompt_payment_act']  = $myrow["default_prompt_payment_act"];
$_POST['pos_invoice_def_project']  = $myrow["pos_invoice_def_project"];
$_POST['pos_invoice_def_location']  = $myrow["pos_invoice_def_location"];
$_POST['pos_invoice_def_float']  = $myrow["pos_invoice_def_float"];
$_POST['pos_invoice_def_customer']  = $myrow["pos_invoice_def_customer"];

$_POST['default_inventory_act'] = $myrow["default_inventory_act"];
$_POST['default_cogs_act'] = $myrow["default_cogs_act"];
$_POST['default_coc_act'] = $myrow["default_coc_act"];
$_POST['default_cost_act'] = $myrow["default_cost_act"];
$_POST['default_adj_act'] = $myrow["default_adj_act"];
$_POST['default_inv_sales_act'] = $myrow['default_inv_sales_act'];
$_POST['default_assembly_act'] = $myrow['default_assembly_act'];

$_POST['allow_negative_stock'] = $myrow['allow_negative_stock'];

$_POST['po_over_receive'] = percent_format($myrow['po_over_receive']);
$_POST['po_over_charge'] = percent_format($myrow['po_over_charge']);
$_POST['past_due_days'] = $myrow['past_due_days'];

$_POST['grn_clearing_act'] = $myrow['grn_clearing_act'];
$_POST['transport_exp_act'] = $myrow['transport_exp_act'];
$_POST['subcontract_exp_act'] = $myrow['subcontract_exp_act'];


$_POST['default_credit_limit'] = $myrow['default_credit_limit'];
$_POST['legal_text'] = $myrow['legal_text'];
$_POST['accumulate_shipping'] = $myrow['accumulate_shipping'];

$_POST['default_workorder_required'] = $myrow['default_workorder_required'];
$_POST['default_dim_required'] = $myrow['default_dim_required'];
$_POST['default_delivery_required'] = $myrow['default_delivery_required'];

$_POST['sales_order_appr_group_id'] = $myrow['sales_order_appr_group_id'];
$_POST['work_order_appr_group_id'] = $myrow['work_order_appr_group_id'];

$_POST['workcentre_scheduling_days_limit'] = $myrow['workcentre_scheduling_days_limit'];

$_POST['fi_balances_act'] = $myrow['fi_balances_act'];

set_post_not_set_string('asset_intransit_location', $myrow['asset_intransit_location']);
//$_POST['asset_intransit_location'] = $myrow['asset_intransit_location'];

$_POST['default_proj_cost_act'] = $myrow['default_proj_cost_act'];

$_POST['default_proj_revenue_act'] = $myrow['default_proj_revenue_act'];

$_POST['default_ub_exp_debit_act'] = $myrow['default_ub_exp_debit_act'];
$_POST['default_ub_exp_credit_act'] = $myrow['default_ub_exp_credit_act'];

$_POST['default_cash_act'] = $myrow['default_cash_act'];
$_POST['default_pc_float_act'] = $myrow['default_pc_float_act'];
$_POST['default_pc_expenses_act'] = $myrow['default_pc_expenses_act'];
$_POST['cash_adjustments_act'] = $myrow['cash_adjustments_act'];
$_POST['emp_pc_advances_act'] = $myrow['emp_pc_advances_act'];

$_POST['default_fn_loans_act'] = $myrow['default_fn_loans_act'];
$_POST['default_fn_loan_interests_act'] = $myrow['default_fn_loan_interests_act'];
$_POST['default_fn_loan_late_interests_act'] = $myrow['default_fn_loan_late_interests_act'];

$_POST['default_payroll_debit_act'] = $myrow['default_payroll_debit_act'];
$_POST['default_payroll_credit_act'] = $myrow['default_payroll_credit_act'];


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


table_section_title(_("General GL"));

text_row(_("Past Due Days Interval:"), 'past_due_days', $_POST['past_due_days'], 6, 6, '', "", _("days"));

gl_all_accounts_list_row(_("Retained Earnings:"), 'retained_earnings_act', $_POST['retained_earnings_act']);

gl_all_accounts_list_row(_("Profit/Loss Year:"), 'profit_loss_year_act', $_POST['profit_loss_year_act']);

gl_all_accounts_list_row(_("Exchange Variances Account:"), 'exchange_diff_act', $_POST['exchange_diff_act']);

gl_all_accounts_list_row(_("Bank Charges Account:"), 'bank_charge_act', $_POST['bank_charge_act']);


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



table_section_title(_("Customers and Sales"));

text_row(_("Default Credit Limit:"), 'default_credit_limit', $_POST['default_credit_limit'], 12, 12);

check_row(_("Accumulate batch shipping:"), 'accumulate_shipping', null);

textarea_row(_("Legal Text on Invoice:"), 'legal_text', $_POST['legal_text'], 32, 3);

gl_all_accounts_list_row(_("Shipping Charged Account:"), 'freight_act', $_POST['freight_act']);

approval_group_list_row(_("Sales Order Approval Group:"), 'sales_order_appr_group_id', $_POST['sales_order_appr_group_id']);

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

table_section_title(_("Customers and Sales Defaults"));
// default for customer branch
gl_all_accounts_list_row(_("Receivable Account:"), 'debtors_act');

gl_all_accounts_list_row(_("Sales Account:"), 'default_sales_act', null,
	false, false, true);

gl_all_accounts_list_row(_("Sales Discount Account:"), 'default_sales_discount_act');

gl_all_accounts_list_row(_("Prompt Payment Discount Account:"), 'default_prompt_payment_act');

text_row(_("Delivery Required By:"), 'default_delivery_required', $_POST['default_delivery_required'], 6, 6, '', "", _("days"));

project_list_row("POS Invoice Default Project", 'pos_invoice_def_project');

locations_list_row("POS Invoice Default Location", 'pos_invoice_def_location', null, false, false);

petty_cash_floats_list_row("POS Invoice Default Float", 'pos_invoice_def_float');

customer_list_row ("POS Invoice Default Customer", 'pos_invoice_def_customer', null, '');

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

table_section_title(_("Projects Defaults"));

gl_all_accounts_list_row(_("Project Cost Account:"), 'default_proj_cost_act', $_POST['default_proj_cost_act']);

gl_all_accounts_list_row(_("Project Revenue Account:"), 'default_proj_revenue_act', $_POST['default_proj_revenue_act']);


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

table_section_title(_("HR & Payroll Defaults"));

gl_all_accounts_list_row(_("Debit Account:"), 'default_payroll_debit_act', $_POST['default_payroll_debit_act']);

gl_all_accounts_list_row(_("Credit Account:"), 'default_payroll_credit_act', $_POST['default_payroll_credit_act']);


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

table_section_title(_("Utility Bill Expenses Defaults"));

gl_all_accounts_list_row(_("Debit Account:"), 'default_ub_exp_debit_act', $_POST['default_ub_exp_debit_act']);

gl_all_accounts_list_row(_("Credit Account:"), 'default_ub_exp_credit_act', $_POST['default_ub_exp_credit_act']);


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

table_section_title(_("Petty Cash Defaults"));

gl_all_accounts_list_row(_("Cash Account:"), 'default_cash_act', $_POST['default_cash_act']);

gl_all_accounts_list_row(_("Float Account:"), 'default_pc_float_act', $_POST['default_pc_float_act']);

gl_all_accounts_list_row(_("Expenses Account:"), 'default_pc_expenses_act', $_POST['default_pc_expenses_act']);

gl_all_accounts_list_row(_("Cash Adjustments Account:"), 'cash_adjustments_act', $_POST['cash_adjustments_act']);

gl_all_accounts_list_row(_("Employee Advances Account:"), 'emp_pc_advances_act', $_POST['emp_pc_advances_act']);

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

table_section(2);

table_section_title(_("Dimension Defaults"));

text_row(_("Dimension Required By After:"), 'default_dim_required', $_POST['default_dim_required'], 6, 6, '', "", _("days"));


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

table_section_title(_("Suppliers and Purchasing"));

percent_row(_("Delivery Over-Receive Allowance:"), 'po_over_receive');

percent_row(_("Invoice Over-Charge Allowance:"), 'po_over_charge');

table_section_title(_("Suppliers and Purchasing Defaults"));

gl_all_accounts_list_row(_("Payable Account:"), 'creditors_act', $_POST['creditors_act']);

gl_all_accounts_list_row(_("Purchase Discount Account:"), 'pyt_discount_act', $_POST['pyt_discount_act']);

gl_all_accounts_list_row(_("GRN Clearing Account:"), 'grn_clearing_act', get_post('grn_clearing_act'), true, false, _("No postings on GRN"));

gl_all_accounts_list_row(_("Transport Expenses Account:"), 'transport_exp_act', $_POST['transport_exp_act']);

gl_all_accounts_list_row(_("Subcontractor Expenses Account:"), 'subcontract_exp_act', $_POST['subcontract_exp_act']);

//------------------
table_section_title(_("Inventory"));

check_row(_("Allow Negative Inventory:"), 'allow_negative_stock', null);
label_row(null, _("Warning:  This may cause a delay in GL postings"), "", "class='stockmankofg' colspan=2"); 

//----------------
table_section_title(_("Items Defaults"));
gl_all_accounts_list_row(_("Sales Account:"), 'default_inv_sales_act', $_POST['default_inv_sales_act']);

gl_all_accounts_list_row(_("Inventory Account:"), 'default_inventory_act', $_POST['default_inventory_act']);
// this one is default for items and suppliers (purchase account)
gl_all_accounts_list_row(_("C.O.G.S. Account:"), 'default_cogs_act', $_POST['default_cogs_act']);

gl_all_accounts_list_row(_("C.O.C. Account:"), 'default_coc_act', $_POST['default_coc_act']);

gl_all_accounts_list_row(_("Cost Account:"), 'default_cost_act', $_POST['default_cost_act']);

gl_all_accounts_list_row(_("Inventory Adjustments Account:"), 'default_adj_act', $_POST['default_adj_act']);

gl_all_accounts_list_row(_("Item Assembly Costs Account:"), 'default_assembly_act', $_POST['default_assembly_act']);

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

table_section_title(_("Manufacturing Defaults"));

approval_group_list_row(_("Work Order Approval Group:"), 'work_order_appr_group_id', $_POST['work_order_appr_group_id']);

text_row(_("Workcentre Scheduling Days Limit:"), 'workcentre_scheduling_days_limit', $_POST['workcentre_scheduling_days_limit'], 6, 6, '', "", _("days"));

text_row(_("Work Order Required By After:"), 'default_workorder_required', $_POST['default_workorder_required'], 6, 6, '', "", _("days"));


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

table_section_title(_("Finance Defaults"));

gl_all_accounts_list_row(_("Loans Account:"), 'default_fn_loans_act', $_POST['default_fn_loans_act']);

gl_all_accounts_list_row(_("Loan Interests Account:"), 'default_fn_loan_interests_act', $_POST['default_fn_loan_interests_act']);

gl_all_accounts_list_row(_("Late Payment Interests Account:"), 'default_fn_loan_late_interests_act', $_POST['default_fn_loan_late_interests_act']);

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

table_section_title(_("Assets"));

asset_locations_list_row(_("Asset Intrasit Location:"), 'asset_intransit_location'); 

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

table_section_title(_("Financial Integration"));

gl_all_accounts_list_row(_("FI Balances Account:"), 'fi_balances_act', $_POST['fi_balances_act']);

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

end_outer_table(1);

submit_center('submit', _("Update"), true, '', 'default');

end_form(2);

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

end_page();

?>
