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

include($path_to_root . "/includes/session.inc");

page(_($help_context = "Tax Groups"));

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

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

simple_page_mode(true);
	
check_db_has_tax_types(_("There are no tax types defined. Define tax types before defining tax groups."));

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

if($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM'){
	//initialise no input errors assumed initially before we test
	$input_error = 0;
        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_groups_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 ID #".$unique_tax_code['id'].". Please enter unique tax code."));
                set_focus('tax_code');
                return false;
            }
        }
	if(strlen($_POST['name']) == 0){
            $input_error = 1;
            display_error(_("The tax group name cannot be empty."));
            set_focus('name');
	}else{
            $unique_tax_name = get_tax_groups_unique_tax_name($_POST['name'], $selected_id);
            if(isset($unique_tax_name['name']) && strlen($unique_tax_name['name']) > 0){
                display_error(_("The entered tax group name already used for ID #".$unique_tax_name['id'].". Please enter unique tax group name."));
                set_focus('name');
                return false;
            }
        }

	if($input_error != 1){
            // create an array of the taxes and array of rates
            $taxes = array();
            $rates = array();

            $items = get_tax_group_rates($selected_id!=-1 ? $selected_id : null);
                while ($item = db_fetch_assoc($items)) {
                    $tax_type_id = $item['tax_type_id'];
                    
                    if (isset($_POST["tax_type_id$tax_type_id"]) && $_POST["tax_type_id$tax_type_id"] == 1) {
                        $taxes[] = $tax_type_id;
                        $rates[] = get_tax_type_default_rate($tax_type_id);
                    }
                }
                
            if($selected_id != -1){
                update_tax_group($selected_id, $_POST['name'], $_POST['tax_shipping'], $taxes, $rates, input_num2($_POST['display_sequence']), $_POST['inactive']);
                display_notification(_('Selected tax group has been updated'));
            }else{
                add_tax_group($_POST['name'], $_POST['tax_shipping'], $taxes, $rates, input_num2($_POST['display_sequence']), $_POST['inactive'], $_POST['tax_code']);
                display_notification(_('New tax group has been added'));
            }

            $Mode = 'RESET';
	}
}

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

function can_delete($selected_id){
    if($selected_id == -1)
        return false;
    if(key_in_foreign_table($selected_id, 'cust_branch', 'tax_group_id')){
        display_error(_("Cannot delete this tax group because customer branches been created referring to it."));
        return false;
    }

    if(key_in_foreign_table($selected_id, 'suppliers', 'tax_group_id')){
        display_error(_("Cannot delete this tax group because suppliers been created referring to it."));
        return false;
    }

    return true;
}

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

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

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

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

start_form();

start_table(TABLESTYLE, "width=60%");
$th = array(_("ID"),_("Tax Code"), _("Tax Name"), _("Display Sequence"), _("Shipping Tax"), _("Inactive"), "", "");
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["display_sequence"], "align=center");
	
	if ($myrow["tax_shipping"])
            label_cell(_("Yes"), "align=center");
	else
            label_cell(_("No"), "align=center");
	if ($myrow["inactive"])
            label_cell(_("Yes"), "align=center");
	else
            label_cell(_("No"), "align=center");

	inactive_control_cell($myrow["id"], $myrow["inactive"], 'tax_groups', 'id');
 	edit_button_cell("Edit".$myrow["id"], _("Edit"));
 	delete_button_cell("Delete".$myrow["id"], _("Delete"));
    end_row();
}

inactive_control_row($th);
end_table(1);

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

start_table(TABLESTYLE2);

    if($selected_id != -1){
        if(isset($_POST['previous_selected_id']) && $_POST['previous_selected_id'] != $selected_id){
            unset($_POST);
        }
        if($Mode == 'Edit'){
            $group = get_tax_group($selected_id);

            $_POST['id']  = $group["id"];
            hidden("id", $group["id"]);
            $_POST['tax_code']  = $group["tax_code"];
            hidden("tax_code", $group["tax_code"]);
            if(!(isset($_POST['name']) && strlen($_POST['name']) > 0)){
                $_POST['name']  = $group["name"];
            }
            if(!(isset($_POST['display_sequence']) && $_POST['display_sequence'] > 0)){
                $_POST['display_sequence']  = $group["display_sequence"];
            }
            if(!(isset($_POST['tax_shipping']) && $_POST['tax_shipping'] > 0)){
                $_POST['tax_shipping']  = $group["tax_shipping"];
            }
            if(!(isset($_POST['inactive']) && $_POST['inactive'] > 0)){
                $_POST['inactive']  = $group["inactive"];
            }
        }
        hidden('selected_id', $selected_id);
    }

    if($selected_id != -1){
        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(_("Tax Name:"), 'name', 40);
    amount_row(_("Display Sequence:"), 'display_sequence', null, null, null, 0);
    yesno_list_row(_("Tax applied to Shipping:"), 'tax_shipping', null, "", "", true);
    yesno_list_row(_("Inactive:"), 'inactive', null, "", "", true);

end_table();

    display_note(_("Select the taxes that are included in this group."), 1, 1);

    // null means transport tax group, but for new we do not use real rates
    $items = get_tax_group_rates($selected_id!=-1 ? $selected_id : null);
    $th = array(_("Tax"), "");
    start_table(TABLESTYLE2);
        table_header($th);

        while($item = db_fetch_assoc($items)){
            check_row($item['tax_type_name'], 'tax_type_id' . $item['tax_type_id'], $selected_id!=-1 && isset($item['rate']), "align='center'");
        }

    end_table(1);

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

if(isset($_POST['id']) && $_POST['id'] > 0){
    hidden("id", $_POST["id"]);
    hidden("tax_code", $_POST["tax_code"]);
}

hidden("previous_selected_id", $selected_id);

end_form();

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

end_page();

?>
