<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_SALESPRICE';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/payroll/includes/db/salary_advances_accounts_setup_db.inc");

$heading_text = "Salary Advances Accounts Setup";
        
if (!@$_GET['popup'])
page(_($help_context = $heading_text));


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

simple_page_mode(true);
//---------------------------------------------------------------------------------------------------
$input_error = 0;

if (isset($_GET['payroll_id']))
{
	$_POST['payroll_id'] = $_GET['payroll_id'];
}

if (!isset($_POST['payroll_id']))
	$_POST['payroll_id'] = get_global_payroll_id();


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

br();

if (!@$_GET['popup'])
	start_form();

function clear_data(){
    
    global $selected_id;
    
    unset($_POST['salary_component_id']);
    unset($_POST['salary_component_id']);
    unset($_POST['deduction']);
    unset($_POST['calculation_type']);
    unset($_POST['amount']);
    unset($_POST['salary_total_code']);
    unset($_POST['table_id']);
    unset($_POST['effective_date']);
    unset($_POST['end_date']);
    unset($_POST['inactive']);
    $selected = -1;

}


function display_salary_components($payroll_id, $process_code, $heading_text)
{

    $result = get_salary_advance_accounts();


    $k = 0; //row colour counter
    $calculated = false;


    while ($myrow = db_fetch($result))
    {

            alt_table_row_color($k);

            label_cell($myrow["component_id"]);

            //label_cell($myrow["process_code"]);

            label_cell($myrow["component_code"]);

            label_cell($myrow["component_name"]);

            //label_cell($myrow["row_id"]);
            
            $debit_account = $myrow["debit_code"]."  ".$myrow["debit_account_name"];

            label_cell($debit_account);

            $credit_account = $myrow["credit_code"]."  ".$myrow["credit_account_name"];
            
            label_cell($credit_account);

            edit_button_cell("Edit".$myrow['component_id'], _("Edit"));

            end_row();

    }


}

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{    
    $error_no = 0;
    
    if(($_POST['debit_code'] == $_POST['credit_code']) && 
            ($_POST['debit_code'] != "" || $_POST['credit_code'] != ""))
    {
        display_error( _("Debit account and credit account can not be same."));
        //set_focus('debit_code');
        $error_no = 1;

    }
    
    if(($_POST['debit_code'] != "" && $_POST['credit_code'] == "") || 
            ($_POST['debit_code'] == "" && $_POST['credit_code'] != ""))
    {
        display_error( _("If one account is selected other account should be selected too."));
        //set_focus('debit_code');
        $error_no = 1;

    }
    
    
    if($error_no == 0 && $selected_id != -1){
        if (isset($_POST['row_id']) && $_POST['row_id'] > 0) 
        {
                update_salary_advance_account($_POST['row_id'], 
                        $_POST['debit_code'], $_POST['credit_code']);

        }
        else
        {
                add_salary_advance_account($_POST['component_id'],
                        $_POST['debit_code'], $_POST['credit_code']);

                
        }
        
        $msg = _("Payroll account has been updated.");
        display_notification($msg);
        $Mode = 'RESET';
        clear_data();
    }

}

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

if ($Mode == 'RESET')
{
	$selected_id = -1;
}

{
	$Ajax->activate('price_table');
	$Ajax->activate('price_details');
}

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

div_start('price_table');

$ppt_result = get_salary_advance_types();

        
display_heading("Salary Advances Payments Accounts");

start_table(TABLESTYLE, "width=60%");

    $th = array(
        _("Id"), _("Component Code"),
        _("Component Name"), 
        _("Debit Account"), _("Credit Account"),
        "");
    
    br();

    table_header($th);
    
    while ($ppt_row = db_fetch($ppt_result)){
            display_salary_components($_POST['payroll_id'], $ppt_row['list_code'], $ppt_row['list_name']);
    }
    end_table();
    
div_end();
//------------------------------------------------------------------------------------------------

br();

if ($selected_id != -1)
{
        $pa_row = get_salary_advance_account_detail($selected_id);

	$_POST['process_type_name'] = $pa_row["process_type_name"];
        $_POST['component_code'] = $pa_row["component_code"];
        $_POST['component_name'] = $pa_row["component_name"];
        $_POST['debit_code'] = $pa_row["debit_code"];
        $_POST['credit_code'] = $pa_row["credit_code"];
        $_POST['component_id'] = $pa_row["component_id"];
        $_POST['row_id'] = $pa_row["row_id"];
        
}
else{
        $_POST['component_name'] = "";
        $_POST['component_code'] = "";
        $_POST['process_type_name'] = "";
        $_POST['debit_code'] = "";
        $_POST['credit_code'] = "";
        $_POST['component_id'] = "";
        $_POST['row_id'] = "";
}

hidden('row_id');
hidden('component_id');
hidden('selected_id', $selected_id);


if (@$_GET['popup'])
{
	hidden('_tabs_sel', get_post('_tabs_sel'));
	hidden('popup', @$_GET['popup']);
}
div_start('price_details');
start_table(TABLESTYLE2);

label_row("Component Code", $_POST['component_code']);
label_row("Component Name", $_POST['component_name']);

gl_all_accounts_list_row(_("Debit Account:"), 'debit_code', $_POST['debit_code'], false, false, "");
gl_all_accounts_list_row(_("Credit account:"), 'credit_code', $_POST['credit_code'], false, false, "");

end_table(1);

if($selected_id != -1){
    submit_add_or_update_center($selected_id == -1, '', 'both');
}

div_end();

if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}	
?>
