<?php

function fi_petty_cash_float_adjustments($batch_no, $cutoff_date_, $timeout_at, $batch_size=1){
    global $systypes_array;
    $cutoff_date = date2sql($cutoff_date_);
    $tran_type = ST_PETTY_CASH_FLOAT_ADJ;
    
    $default_cash_act = get_company_pref('default_cash_act');
    if(!(strlen($default_cash_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default cash account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    $default_float_act = get_company_pref('default_pc_float_act');
    if(!(strlen($default_float_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default float account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    $default_expenses_act = get_company_pref('default_pc_expenses_act');
    if(!(strlen($default_expenses_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default expenses account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    $default_cash_adjustments_act = get_company_pref('cash_adjustments_act');
    if(!(strlen($default_expenses_act) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default cash adjustments account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }
    
    
    $result_h = get_fi_petty_cash_float_adjustments($cutoff_date, $batch_size);
    while($h = db_fetch($result_h)){
        
        if($timeout_at < time()){
            return array("success"=>false, "message"=>get_fi_integration_timeout_message());
        }
        
        $trans_no = $h['id'];
        $tran_date = $h['tran_date'];   
        $tran_date_ = sql2date($h['tran_date']);   
        
        if(strlen($h['account_code']) > 2){
            $float_account = $h['account_code'];
        }
        else{
            $float_account = $default_float_act;
        }
        
        $cgfit_msg = can_generate_fi_transaction($tran_type, $trans_no, $tran_date);
        if($cgfit_msg['success'] !== true){ 
            return $cgfit_msg;
        }

        begin_global_transaction();

        $detail_line_id = 0;
        if($h['amount'] == 0){
            continue;
        }

        $comment = "";
        $amount = $h['amount'];
        if($amount !== 0){
            add_fi_trans($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $default_cash_adjustments_act, 0, 0, $comment, -$amount);
            add_fi_trans($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $float_account, 0, 0, $comment, $amount);
        }
        
        $n = update_source_transaction_fi_batch_no($tran_type, $trans_no, $batch_no);
        if($n !== 1){
            cancel_transaction();
            $message = $systypes_array[$tran_type]." - Could not process #$trans_no.";
            write_fi_log($batch_no, $message);
            continue;
        }
        
        add_fi_batch_transaction($batch_no, $tran_type, $trans_no, $tran_date);
        
        commit_transaction();
    }
    
    return array("success"=>true, "message"=>"");
    
}

?>