<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_TAXRATES';
$path_to_root = "..";

include($path_to_root . "/includes/session.inc");
page(_($help_context = "Tax Types"));

include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/taxes/db/tax_types_db.inc");

simple_page_mode(true);
//-----------------------------------------------------------------------------------

function can_process()
{
    global $selected_id;
	
    if(!(strlen($_POST['tax_code']) > 0)){
        display_error(_("The tax code cannot be empty."));
        set_focus('tax_code');
        return false;
    }else{
        $unique_tax_code = get_tax_type_unique_tax_code($_POST['tax_code'], $selected_id);
        if(isset($unique_tax_code['tax_code']) && strlen($unique_tax_code['tax_code']) > 0){
            display_error(_("The entered tax code already used for tax type with Id #".$unique_tax_code['id'].". Please enter a different tax code."));
            set_focus('tax_code');
            return false;
        }
    }
    
    if(!(strlen($_POST['name']) > 0)){
        display_error(_("The tax type name cannot be empty."));
        set_focus('name');
        return false;
    }else{
        $unique_tax_name = get_tax_type_unique_tax_name($_POST['name'], $selected_id);
        if(isset($unique_tax_name['name']) && strlen($unique_tax_name['name']) > 0){
            display_error(_("The entered tax type name already used for ID #".$unique_tax_name['id'].". Please enter unique tax type name."));
            set_focus('name');
            return false;
        }
    }
    
    if(!check_num('rate', 0)){
        display_error( _("The default tax rate must be numeric and not less than zero."));
        set_focus('rate');
        return false;
    }

//    if(!is_tax_gl_unique(get_post('sales_gl_code'), get_post('purchasing_gl_code'), $selected_id)){
//        display_error( _("Selected GL Accounts cannot be used by another tax type."));
//        set_focus('sales_gl_code');
//        return false;
//    }
    return true;
}

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

if ($Mode=='ADD_ITEM' && can_process())
{

	add_tax_type(input_num('sequence'), input_num('add_to_price'), $_POST['name'], $_POST['sales_gl_code'],
                 $_POST['purchasing_gl_code'], input_num('rate', 0), $_POST['tax_code'], input_num('suspended'), $_POST['tax_category_id']);
	display_notification(_('New tax type has been added'));
	$Mode = 'RESET';
}

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

if ($Mode=='UPDATE_ITEM' && can_process())
{

	update_tax_type($selected_id, input_num('sequence'), input_num('add_to_price'), $_POST['name'], 
    	$_POST['sales_gl_code'], $_POST['purchasing_gl_code'], input_num('rate'), input_num('suspended'), $_POST['tax_category_id']);
	display_notification(_('Selected tax type has been updated'));
	$Mode = 'RESET';
}

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

function can_delete($selected_id)
{
	if (key_in_foreign_table($selected_id, 'tax_group_items', 'tax_type_id'))
	{
		display_error(_("Cannot delete this tax type because tax groups been created referring to it."));

		return false;
	}

	return true;
}


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

if ($Mode == 'Delete')
{

	if (can_delete($selected_id))
	{
		delete_tax_type($selected_id);
		display_notification(_('Selected tax type has been deleted'));
	}
	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['show_inactive'] = $sav;
}
//-----------------------------------------------------------------------------------

$result = get_all_tax_types(check_value('show_inactive'));

start_form();

display_note(_("To avoid problems with manual journal entry all tax types should have unique Sales/Purchasing GL accounts."), 0, 1);
start_table(TABLESTYLE, "width=90%");

$th = array(_("ID"), _("Tax Code"), _("Tax Name"), "Sequence", "Suspended", "Add To Price", _("Default Rate (%)"), 
                    _("Sales GL Account"), _("Purchasing GL Account") ,_("Category") , "", "");
inactive_control_column($th);
table_header($th);

$k = 0;
while ($myrow = db_fetch($result))
{

	alt_table_row_color($k);

	label_cell($myrow["id"], "align=center");
	label_cell($myrow["tax_code"]);
	label_cell($myrow["name"]);
        label_cell($myrow["sequence"], "align=right");
        label_cell(get_yes_no_name($myrow["suspended"]), "align=center");
        label_cell(get_yes_no_name($myrow["add_to_price"]), "align=center");
    
	label_cell($myrow["rate"], "align=right");
	label_cell($myrow["sales_gl_code"] . "&nbsp;" . $myrow["SalesAccountName"]);
	label_cell($myrow["purchasing_gl_code"] . "&nbsp;" . $myrow["PurchasingAccountName"]);
	label_cell($myrow["tax_category_name"]);

	inactive_control_cell($myrow["id"], $myrow["inactive"], 'tax_types', 'id');
 	edit_button_cell("Edit".$myrow["id"], _("Edit"));
        label_cell('');
// 	delete_button_cell("Delete".$myrow["id"], _("Delete"));

    end_row();
}

inactive_control_row($th);
end_table(1);
//-----------------------------------------------------------------------------------

start_table(TABLESTYLE2);

if ($selected_id > 0) 
{
// 	if ($Mode == 'Edit') {
		//editing an existing status code
                $myrow = get_tax_type($selected_id);
                $_POST['id']  = $myrow["id"];
                hidden("id", $myrow["id"]);
                $_POST['tax_code']  = $myrow["tax_code"];
                hidden("tax_code", $myrow["tax_code"]);
                
                if(!(isset($_POST['name']) && strlen($_POST['name']) > 0)){
                    $_POST['name']  = $myrow["name"];
                }
                if(!(isset($_POST['sequence']) && $_POST['sequence'] > 0)){
                    $_POST['sequence']  = $myrow["sequence"];
                }
                if(!(isset($_POST['add_to_price']) && $_POST['add_to_price'] > 0)){
                    $_POST['add_to_price']  = $myrow["add_to_price"];
                }
                if(!(isset($_POST['rate']) && $_POST['rate'] > 0)){
                    $_POST['rate']  = $myrow["rate"];
                }
                if(!(isset($_POST['sales_gl_code']) && $_POST['sales_gl_code'] > 0)){
                    $_POST['sales_gl_code']  = $myrow["sales_gl_code"];
                }
                if(!(isset($_POST['purchasing_gl_code']) && $_POST['purchasing_gl_code'] > 0)){
                    $_POST['purchasing_gl_code']  = $myrow["purchasing_gl_code"];
                }
                if(!(isset($_POST['tax_category_id']) && $_POST['tax_category_id'] > 0)){
                    $_POST['tax_category_id']  = $myrow["tax_category_id"];
                }
                if(!(isset($_POST['inactive']) && $_POST['inactive'] > 0)){
                    $_POST['inactive']  = $myrow["inactive"];
                }
                
                
                $_POST['name']  = $myrow["name"];     
                $_POST['sequence']  = $myrow["sequence"];            
                $_POST['suspended']  = $myrow["suspended"];            
                $_POST['add_to_price']  = $myrow["add_to_price"];
                $_POST['rate']  = $myrow["rate"];
                $_POST['sales_gl_code']  = $myrow["sales_gl_code"];
                $_POST['purchasing_gl_code']  = $myrow["purchasing_gl_code"]; 
                $_POST['tax_category_id']  = $myrow["tax_category_id"]; 
                
	
	hidden('selected_id', $selected_id);
}

if($selected_id > 0){
        label_row("ID:", $_POST["id"]);
        label_row("Tax Code:", $_POST["tax_code"]);
}else{
        text_row(_("Tax Code:"), 'tax_code', null, 20, 20);
}
    
text_row_ex(_("Description:"), 'name', 50);

small_amount_row(_("Sequence:"), 'sequence', '', "", "", 0);
yesno_list_row("Suspended:", 'suspended');
yesno_list_row("Add to Price", 'add_to_price');
amount_row(_("Default Rate:"), 'rate', '', "", "%", 5);

gl_all_accounts_list_row(_("Sales GL Account:"), 'sales_gl_code', null);
gl_all_accounts_list_row(_("Purchasing GL Account:"), 'purchasing_gl_code', null);

value_list_row("Tax Category:", 'tax_category_id', "TAX_CATEGORY", null, "Select tax category");

end_table(1);

submit_add_or_update_center($selected_id == -1, '', 'both');

end_form();

end_page();

?>
