<?php

function fi_payroll_summaries($batch_no, $cutoff_date_, $timeout_at, $batch_size=1){
    global $systypes_array;
    
    $trans_type = ST_PAYROLL;
   
    
    $default_payroll_debit_act = get_company_pref('default_payroll_debit_act');
    if(!(strlen($default_payroll_debit_act) > 2)){
                $message = $systypes_array[$trans_type]." - Default payable account not found";
                write_fi_log($batch_no, $message);
                return;
    }
    
    $default_payroll_credit_act = get_company_pref('default_payroll_credit_act');
    if(!(strlen($default_payroll_credit_act) > 2)){
                $message = $systypes_array[$trans_type]." - Default payment discounts account not found";
                write_fi_log($batch_no, $message);
                return;
    }
    

    $list_code = 'PAYROLLS';
    $payroll_type_code = 'MAIN';
    $saw_msg = fi_payroll_group_companies('SAW', $list_code, $payroll_type_code, $trans_type, $batch_no, $cutoff_date_, $timeout_at, $batch_size,
                                $default_payroll_debit_act, $default_payroll_credit_act);
    if($saw_msg['success'] !== true){
        return $saw_msg;
    }
    
    $sawc_msg = fi_payroll_group_companies('SAWC', $list_code, $payroll_type_code, $trans_type, $batch_no, $cutoff_date_, $timeout_at, $batch_size,
                                $default_payroll_debit_act, $default_payroll_credit_act);
    if($sawc_msg['success'] !== true){
        return $sawc_msg;
    }
    
//    $vee_msg = fi_payroll_group_companies('VEE', $list_code, $payroll_type_code, $trans_type, $batch_no, $cutoff_date_, $timeout_at, $batch_size,
//                                $default_payroll_debit_act, $default_payroll_credit_act);
//    if($vee_msg['success'] !== true){
//        return $vee_msg;
//    }
//    
//    $veec_msg = fi_payroll_group_companies('VEEC', $list_code, $payroll_type_code, $trans_type, $batch_no, $cutoff_date_, $timeout_at, $batch_size,
//                                $default_payroll_debit_act, $default_payroll_credit_act);
//    if($veec_msg['success'] !== true){
//        return $veec_msg;
//    }
    
    $lps_msg = fi_payroll_group_companies('LPS', $list_code, $payroll_type_code, $trans_type, $batch_no, $cutoff_date_, $timeout_at, $batch_size,
                                $default_payroll_debit_act, $default_payroll_credit_act);    
    if($lps_msg['success'] !== true){
        return $lps_msg;
    }
        
    return array("success"=>true, "message"=>"");
    
}

function fi_payroll_group_companies($company_code, $list_code, $payroll_type_code, $trans_type, $batch_no, $cutoff_date_, $timeout_at, $batch_size,
                                    $default_payroll_debit_act, $default_payroll_credit_act)
{
    
    if($timeout_at < time()){
        return array("success"=>false, "message"=>get_fi_integration_timeout_message());
    }
        
    $group_company_db_name = get_group_company_db_name($company_code);
    if(!(strlen($group_company_db_name) > 1)){
        return array("success"=>true, "message"=>"");
    }
    $list_value = get_group_company_list_value($company_code, $list_code, $payroll_type_code);
    if(isset($list_value['id']) && $list_value['id'] > 0){
        $payroll_type_id = $list_value['id'];
        $msg = fi_group_company_payrolls($group_company_db_name, $company_code, $trans_type, $payroll_type_code, $batch_no, $cutoff_date_, $batch_size,
                                    $default_payroll_debit_act, $default_payroll_credit_act);
        if($msg['success'] !== true){
            return $msg;
        }
    }
    
    return array("success"=>true, "message"=>"");

}
    
function fi_group_company_payrolls($db_name, $company_code, $trans_type, $payroll_type_code, $batch_no, $cutoff_date_, $batch_size, 
                                    $default_payroll_debit_act, $default_payroll_credit_act)
{
    $cutoff_date = date2sql($cutoff_date_);
    $payrolls_result = get_fi_group_company_processed_payroll_runs_by_payroll_type($db_name, $payroll_type_code, $cutoff_date, $batch_size);
    while($payroll_run = db_fetch($payrolls_result)){
        $msg = fi_group_company_payroll_summaries($db_name, $company_code, $trans_type, $payroll_run, $batch_no, $default_payroll_debit_act, $default_payroll_credit_act);
        if($msg['success'] !== true){
            return $msg;
        }
    }
    
    return array("success"=>true, "message"=>"");
}


function fi_group_company_payroll_summaries($db_name, $company_code, $trans_type, $payroll_run, $batch_no, $default_payroll_debit_act, $default_payroll_credit_act)
{
    global $systypes_array;
    
    begin_global_transaction();
    
        $payroll_run_id = $payroll_run['id'];
        $payroll_type_id = $payroll_run['payroll_id'];
        $payroll_type_code = $payroll_run['payroll_type_code'];
        $tran_date = $payroll_run['tran_date'];
        $tran_date_ = sql2date($tran_date);
        $trans_no = $payroll_run_id;
        
        $cgfit_msg = can_generate_fi_transaction($trans_type, $trans_no, $tran_date);
        if($cgfit_msg['success'] !== true){ 
            return $cgfit_msg;
        }
        
//        $n = get_fi_payroll_mismatching_summaries($db_name, $payroll_type_code, $payroll_run_id);
//        if(!($n == 0)){
//            $message = "Company code - ".$company_code." : There are components with whole company total and all departments total mismatching. Can not generate FI accounts";
//            return array("success"=>false, "message"=>$message);
//        }
        
        $result_d = get_all_list_values('DEPARTMENTS');
//        $result_s = get_fi_payroll_summaries($db_name, $payroll_type_id, $payroll_run_id);
        while($department = db_fetch($result_d)){
            $dep_res_msg = fi_group_company_payroll_department_summaries($db_name, $company_code, $trans_type, $payroll_run, $batch_no, 
                                                        $default_payroll_debit_act, $default_payroll_credit_act, $department);
            if($dep_res_msg['success'] !== true){
                cancel_transaction();
                return $dep_res_msg;
            }

        }
        
        $n = update_group_company_source_transaction_fi_batch_no($db_name, $trans_type, $trans_no, $batch_no);
        if($n !== 1){
            cancel_transaction();
            $message = $systypes_array[$trans_type]." #".$trans_no." has been processed already";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
        }
        
        add_fi_batch_transaction($batch_no, $trans_type, $trans_no, $tran_date, "", $db_name);

        commit_transaction();
        
        return array("success"=>true, "message"=>"");
    
}

function fi_group_company_payroll_department_summaries($db_name, $company_code, $trans_type, $payroll_run, $batch_no, 
                                                        $default_payroll_debit_act, $default_payroll_credit_act, $department)
{

        $payroll_run_id = $payroll_run['id'];
        $payroll_type_code = $payroll_run['payroll_type_code'];
        $tran_date = $payroll_run['tran_date'];
        $tran_date_ = sql2date($tran_date);
        $trans_no = $payroll_run_id;
        $department_code = $department['value_code'];
        
        $count = 0;
        
        $pay_acc_res = get_defined_payroll_accounts($payroll_type_code, $department_code);
        
        while($pay_acc = db_fetch($pay_acc_res)){           
            
            $component_code = $pay_acc['component_code'];
            
//            if($department_code = 'HR'){
//                $eee = 1235;
//            }
            $summary_value = get_fi_payroll_department_summary_value($db_name, $payroll_run_id, $department_code, $component_code);
            $amount = abs($summary_value['value']);
            if($amount == 0){
                continue;
            }
            
            $detail_line_id = $pay_acc['component_id'];
            

            $debit_account = $default_payroll_debit_act;
            if(strlen($pay_acc['debit_code']) > 2){
                $debit_account = $pay_acc['debit_code'];
            }
           
            $debit_memo = "Company: ".$company_code.", Payroll run: ".$payroll_run_id.",";
            $debit_memo .= " Department: ".$department['value_name']." (".$department_code."),";
            $debit_memo .= " Component: ".$pay_acc['component_name']." (".$component_code.")";
            
            $credit_account = $default_payroll_credit_act;
            if(strlen($pay_acc['credit_code']) > 2){
                $credit_account = $pay_acc['credit_code'];
            }

            $credit_memo = $debit_memo;
            
            add_fi_trans($batch_no, $trans_type, $trans_no, $detail_line_id, $tran_date_, $debit_account, 0, 0, $debit_memo, $amount);
            add_fi_trans($batch_no, $trans_type, $trans_no, $detail_line_id, $tran_date_, $credit_account, 0, 0, $credit_memo, -$amount);
            
            $count++;
        }
        
        return array("success"=>true, "count"=>$count, "message"=>"");
    
}


?>