<?php

function fi_petty_cash_item_receipts($batch_no, $cutoff_date_, $tran_type, $timeout_at, $batch_size=1){
    global $systypes_array;
    $cutoff_date = date2sql($cutoff_date_);
//    $tran_type = ST_PETTY_CASH_ITEM_RECEIPT;

    $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_coc_account = get_company_pref('default_coc_act');
    if(!(strlen($default_coc_account) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default COC account";
            write_fi_log($batch_no, $message);
            return;
    }
    
    $def_inventory_account = get_company_pref('default_inventory_act');
    if(!(strlen($def_inventory_account) > 2)){
            $message = $systypes_array[$tran_type]." - could not find default inventory account";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
    }

    $result_d = get_fi_petty_cash_item_receipts($cutoff_date, $batch_size);
    $num_rows = db_num_rows($result_d);
    
    if(!($num_rows > 0)){
        write_fi_log($batch_no, $systypes_array[$tran_type]." - No Details found.");
    }
    
    while($d = db_fetch($result_d)){
        $trans_no = $d['trans_no'];
        $tran_date = $d['tran_date'];
        $project_type_id = $d['project_type_id'];
        $project_id = $d['project_id'];
        $tran_date_ = sql2date($tran_date);  
        
        $cgfit_msg = can_generate_fi_transaction($tran_type, $trans_no, $tran_date);
        if($cgfit_msg['success'] !== true){ 
            return $cgfit_msg;
        }
        
        if($timeout_at < time()){
            return array("success"=>false, "message"=>get_fi_integration_timeout_message());
        }

        begin_global_transaction();
        if(!($num_rows > 0)){
            write_fi_log($batch_no, $systypes_array[$tran_type]." #$trans_no - No Details found.");
        }
        $error = 0;        

        $detail_line_id = $d['detail_line_id'];

        $unit_cost = 0;
        if(isset($d['trans_id']) && $d['trans_id'] > 0){ // There is a movement record. Should be a stock item
            $unit_cost = $d['standard_cost'];
        }
        else{
            $unit_cost = $d['tran_unit_cost']/$d['conversion_factor']; // Non stock item. Petty Cash Item Receipt detail unit cost
        }
                
        if($d['quantity'] == 0 || $unit_cost == 0){
            write_fi_log($batch_no, $systypes_array[$tran_type]." #$trans_no : Quantity: ".$d['quantity']." Unit cost:".$unit_cost." Ignored");
            continue;
        }

        $detail_expense_account = $default_expenses_act;
        $detail_coc_account = $default_coc_account;
        
        $expense_line_id = $d['source_detail_id'];
            
        $ioub_d = get_fi_petty_cash_item_receipts_iou_batch_expense_detail($expense_line_id);
        if(strlen($ioub_d['debit_account']) > 2){
            $detail_expense_account = $ioub_d['debit_account'];              
        }  

        if(strlen($ioub_d['coc_account']) > 2){
            $detail_coc_account = $ioub_d['coc_account'];              
        }  

        set_fi_applicable_expense_account($project_type_id, $detail_expense_account, $detail_coc_account);

        if(strlen($d['inventory_account']) > 2){
            $inventory_account = $d['inventory_account'];
        }
        else{
            $inventory_account = $def_inventory_account;
        }


        $comment = "Line Id $detail_line_id : ".$d['stock_id']." - ".$d['description'];
        $amount = $unit_cost * $d['quantity'];
        if($amount !== 0){
            add_fi_trans_std_cost($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $detail_expense_account, $project_id, 0, $comment, -$amount);
            add_fi_trans_std_cost($batch_no, $tran_type, $trans_no, $detail_line_id, $tran_date_, $inventory_account, $project_id, 0, $comment, $amount);
        }
            
            
        if($error > 0){ // if fails inner loop then continue with outer loop
            cancel_transaction();
            continue;
        }
        
        $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 update source transaction #$trans_no.";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
        }
        
        add_fi_batch_transaction($batch_no, $tran_type, $trans_no, $tran_date);
        
        commit_transaction();
    }
    
    return array("success"=>true, "message"=>"");
}

?>