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

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

page(_($help_context = "Item Tax Types")); 

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

if($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM'){
    $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_item_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 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 item tax type name cannot be empty."));
        set_focus('name');
    }else{
        $unique_tax_name = get_item_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 item tax type name already used for ID #".$unique_tax_name['id'].". Please enter unique item tax type name."));
            set_focus('name');
            return false;
        }
    }

    if($input_error != 1){
        // create an array of the exemptions
        $exempt_from = array();

        $tax_types = get_all_tax_types_simple();
        $i = 0;    	

        while($myrow = db_fetch($tax_types)){
            if(check_value('ExemptTax' . $myrow["id"])){
                $exempt_from[$i] = $myrow["id"];
                $i++;
            }
        }  

        if($selected_id != -1){    		
            update_item_tax_type($selected_id, $_POST['name'], $_POST['exempt'], $exempt_from);
            display_notification(_('Selected item tax type has been updated'));
        }else{
            add_item_tax_type($_POST['name'], $_POST['exempt'], $exempt_from, $_POST['tax_code']);
            display_notification(_('New item tax type has been added'));
        }

        $Mode = 'RESET';
    }
} 

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

function can_delete($selected_id){
    if(key_in_foreign_table($selected_id, 'stock_master', 'tax_type_id')){
        display_error(_("Cannot delete this item tax type because items have been created referring to it."));
        return false;
    }
    if(key_in_foreign_table($selected_id, 'stock_category', 'dflt_tax_type')){
        display_error(_("Cannot delete this item tax type because item categories have been created referring to it."));
        return false;
    }

    return true;
}

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

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

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


$result2 = $result = get_all_item_tax_types(check_value('show_inactive'));

start_form();
    start_table(TABLESTYLE, "width=50%");
        $th = array(_("ID"), _("Tax Code"), _("Tax Name"), _("Tax exempt"),'','');
        inactive_control_column($th);
        table_header($th);

        $k = 0;
        while($myrow = db_fetch($result2)){
            alt_table_row_color($k);	
                label_cell($myrow["id"], "align=center");
                label_cell($myrow["tax_code"]);
                label_cell($myrow["name"]);
                if($myrow["exempt"] == 0){
                    $disallow_text = _("No");
                }else{
                    $disallow_text = _("Yes");
                }
                label_cell($disallow_text, "align=center");
                inactive_control_cell($myrow["id"], $myrow["inactive"], 'item_tax_types', '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'){
            $myrow = get_item_tax_type($selected_id);
            unset($_POST); // clear exemption checkboxes
            
            $_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['exempt']) && $_POST['exempt'] > 0)){
                $_POST['exempt']  = $myrow["exempt"];
            }

            // read the exemptions and check the ones that are on
            $exemptions = get_item_tax_type_exemptions($selected_id);

            if(db_num_rows($exemptions) > 0){
                while($exmp = db_fetch($exemptions)){
                    $_POST['ExemptTax' . $exmp["tax_type_id"]] = 1;
                }
            }	
        }

        hidden('selected_id', $selected_id);
        $exemptions = get_item_tax_type_exemptions($selected_id);
        if(db_num_rows($exemptions) > 0){
            while($exmp = db_fetch($exemptions)){
                $_POST['ExemptTax' . $exmp["tax_type_id"]] = 1;
            }
        }	
    } 
    
    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);
    yesno_list_row(_("Is Fully Tax-exempt:"), 'exempt', null, "", "", true);

end_table(1);

if(!isset($_POST['exempt']) || $_POST['exempt'] == 0){

    display_note(_("Select which taxes this item tax type is exempt from."), 0, 1);
    
    start_table(TABLESTYLE2);
        $th = array(_("Tax Name"), _("Rate"), _("Is exempt"));
        table_header($th);

        $tax_types = get_all_tax_types_simple();    	
        while($myrow = db_fetch($tax_types)){
            alt_table_row_color($k);	
                label_cell($myrow["name"]);
                label_cell(percent_format($myrow["rate"])." %", "nowrap align=right");
                check_cells("", 'ExemptTax' . $myrow["id"], null, false, false, "align=center");
            end_row();
        }
    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();

?>
