<?php

function fi_miscellaneous_project_costs($batch_no, $cutoff_date_, $batch_size=1){
    global $systypes_array;
    $cutoff_date = date2sql($cutoff_date_);
    $tran_type = ST_MISCELLANEOUS_PROJECT_COST;
    
    $result_h = get_fi_miscellaneous_project_costs($cutoff_date, $batch_size);
    while($h = db_fetch($result_h)){
        $tran_date = $h['tran_date'];
        $tran_date_ = sql2date($tran_date);
        $trans_no = $h["trans_no"];
        $project_id = $h["project_id"];
//        $cost_account = $h["cost_account"];
//        if(!(strlen($cost_account) > 2)){
//            $cost_account = get_company_pref('default_proj_cost_act');
//        }
//        
//
//        if(!(strlen($cost_account) > 2)){
//            $message = $systypes_array[$tran_type]." - Invalid Cost Account";
//            write_fi_log($batch_no, $message);
//            continue;            
//        }
        
        $cgfit_msg = can_generate_fi_transaction($tran_type, $trans_no, $tran_date);
        if($cgfit_msg['success'] !== true){ 
            return $cgfit_msg;
        }
        
        begin_global_transaction();
        
        $result_d = get_fi_miscellaneous_project_cost_details($trans_no);
        if(!(db_num_rows($result_d) > 0)){
            write_fi_log($batch_no, $systypes_array[$tran_type]." #$trans_no - No Details found.");
            $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;
            }else{
                commit_transaction();
                continue;
            }
        }
        
        $error = 0;
        
        while($d = db_fetch($result_d)){
            $detail_line_id = $d['id'];
            $detail_debit_account = $detail_credit_account = '';

            if($d['quantity'] == 0 || $d['rate'] == 0){
                continue;
            }
            
            if(strlen($d['debit_account']) > 2){
                $detail_debit_account = $d['debit_account'];
            }
            
            if(strlen($d['credit_account']) > 0){
                $detail_credit_account = $d['credit_account'];
            }

            if(strlen($detail_debit_account) > 2){
                if(!(strlen($detail_credit_account) > 2)){
                    $message = $systypes_array[$tran_type]." #$trans_no - Detail id: $detail_line_id - Debit account selected but credit account not selected.";
                    write_fi_log($batch_no, $message);
                    $error = 1;
                    break;
                }
            }
            
            if(strlen($detail_credit_account) > 2){
                if(!(strlen($detail_debit_account) > 2)){
                    $message = $systypes_array[$tran_type]." #$trans_no - Detail id: $detail_line_id - Credit account selected but debit account not selected.";
                    write_fi_log($batch_no, $message);
                    $error = 1;
                    break;
                }
            }

            $comment = "";
                    
            $debits = $credits = 0;
            $amount = $d['rate'] * $d['quantity'];
            if($amount > 0){
                if(strlen($d['debit_account']) > 0){
                    $debits = add_fi_trans($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $detail_debit_account, $project_id, 0, $comment, $amount);
                }
                
                if(strlen($d['credit_account']) > 0){
                    $credits = add_fi_trans($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $detail_credit_account, $project_id, 0, $comment, -$amount);
                }
            }
        }
        
        if($error > 0){ // if fails inner loop then continue with outer loop
            cancel_transaction();
            continue;
        }

        $balance = $debits + $credits;
        if($balance != 0){
            $m = "Non zero balance";
            add_fi_balance($batch_no, $tran_type, $trans_no, $tran_date_, -$balance, null, PT_MISC, 0, $m, $project_id);
        }
        
        $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();
    }    
}

?>