<?php

function display_payroll_accounts_setup($payroll_id, $department_id) {
    
    display_heading("Payroll Components");
    
    div_start('price_table');

    $ppt_result = get_payroll_process_types($payroll_id);
        start_table(TABLESTYLE, "width=70%");
            $th = array(_("Id"), _("Component Code"), _("Component Name"), _("Debit Account"), _("Credit Account"), "");
            table_header($th);

            while ($ppt_row = db_fetch($ppt_result)){
                display_salary_components($payroll_id, $department_id, $ppt_row['list_code'], $ppt_row['list_name']);
            }
        end_table(1);

    div_end();
}

function display_salary_components($payroll_id, $department_id, $process_code, $heading_text) {
    label_cell("<b>$heading_text</b>", "align=center colspan=6");
    $result = get_payroll_accounts($payroll_id, $department_id, $process_code);

    $k = 0; //row colour counter

    if (db_num_rows($result) > 0) {
        while ($myrow = db_fetch($result)) {
            alt_table_row_color($k);
                label_cell($myrow["component_id"], 'align=center');
                label_cell($myrow["component_code"]);
                label_cell($myrow["component_name"]);
                $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();
        }
    } else {
        start_row();
        end_row();
    }
}

function payroll_accounts_setup_controls($payroll_id, $department_id, $selected_id, $status) {
    if ($status === true) {
        if ($selected_id != -1) {
            $pa_row = get_payroll_account_detail($payroll_id, $department_id, $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);
    
    br(2);

    div_start('price_details');
        start_table(TABLESTYLE2);
            if ($selected_id > 0) {
                label_row("Id:", $selected_id);
            }
            label_row("Process Type:", $_POST['process_type_name']);
            hidden('process_type_name', $_POST['process_type_name']);
            
            label_row("Component Code:", $_POST['process_type_name']);
            hidden('process_type_name', $_POST['process_type_name']);
            
            label_row("Component Name:", $_POST['component_name']);
            hidden('component_name', $_POST['component_name']);

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

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

function can_process_payroll_accout_setup() {
    if (! ($_POST['payroll_id'] > 0)) {
        set_focus('payroll_id');
        display_error('Please select a payroll.');
        return false;
    } 
    
    if (! ($_POST['department_id'] > 0)) {
        set_focus('department_id');
        display_error('Please select a department.');
        return false;
    } 
    
    if (($_POST['debit_code'] == $_POST['credit_code']) && ($_POST['debit_code'] != "" || $_POST['credit_code'] != "")) {
        display_error( _("Debit account and credit account can not be same."));
        return false;
    }

    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."));
        return false;
    }
    
    return true;
}

function clear_data() {
    global $selected_id;

    unset($_POST['component_name']);
    unset($_POST['component_code']);
    unset($_POST['process_type_name']);
    unset($_POST['debit_code']);
    unset($_POST['credit_code']);
    unset($_POST['component_id']);
    unset($_POST['row_id']);
    $selected_id = -1;
}



function display_hr_db_mismatching_departments()
{
    display_heading("Mismatching Departments");
    br(1);
    
    $m = "Below department codes are available in the HR system but not in this system. May be the codes do not match.";
    display_note("<font color='red'>".$m."</font>");
    br(1);
    
    start_table(TABLESTYLE, "width=40%");
    $th = array(_("Department Code"), _("Department Name"));
    table_header($th);
    $comp_result = get_group_companies();
    while ($comp_myrow = db_fetch($comp_result)) {
        start_row();
        label_cell(text_bold($comp_myrow['company_code']." - ".$comp_myrow['company_name']),'colspan=2  align=center');
        end_row();
        
        $k = 0;
        $hr_result = get_hr_db_mismatching_departments($comp_myrow['db_name']);
        if(db_num_rows($hr_result) == 0){
            alt_table_row_color($k);
            label_cell("No mismatching departments",'colspan=2');
            end_row();
        }
        else{
            while ($hr_myrow = db_fetch($hr_result)) {
                alt_table_row_color($k);
                label_cell($hr_myrow['hr_value_code']);
                label_cell($hr_myrow['hr_value_name']);
                end_row();

            }
        }
    }
    end_table();
        
}