<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PAYROLL_ACC_SETUP';
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/ui/payroll_accounts_setup_ui.inc");
include_once($path_to_root . "/payroll/includes/db/payroll_accounts_setup_db.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");


$heading_text = "Payroll accounts setup";
        
if (! @$_GET['popup'])
page(_($help_context = $heading_text));

simple_page_mode(true);

global $Ajax;
$status = true;

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

if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM') {    
    if ($status = can_process_payroll_accout_setup()) {
        if (isset($_POST['row_id']) && $_POST['row_id'] > 0) {
            update_payroll_account($_POST['row_id'], $_POST['debit_code'], $_POST['credit_code']);
        } else {
            add_payroll_account($_POST['payroll_id'], $_POST['department_id'], $_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;
    clear_data();
}

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

start_form();

    start_table(TABLESTYLE2);
        value_list_row('Payroll:', 'payroll_id', 'PAYROLLS', null, "Select a Payroll", true);
        value_list_row(_("Department:"), 'department_id', 'DEPARTMENTS', null, "Select a Department", true);
    end_table(2);

    display_hr_db_mismatching_departments();
    
    br(2);
    
    display_payroll_accounts_setup($_POST['payroll_id'], $_POST['department_id']);
    
    payroll_accounts_setup_controls($_POST['payroll_id'],  $_POST['department_id'], $selected_id, $status);
    br(2);
    
if (! @$_GET['popup']) {
    end_form();
    end_page(@$_GET['popup'], false, false);
}

?>