<?php

function fi_stock_asset_transfers($batch_no, $cutoff_date_, $timeout_at, $batch_size=1){
    global $systypes_array;
    $cutoff_date = date2sql($cutoff_date_);
    $trans_type = ST_STOCK_ASSET_TRANSFER;
    
    $h_result = get_fi_stock_asset_transfer_headers($cutoff_date, $batch_size);

    while($h = db_fetch($h_result)){
        
        if($timeout_at < time()){
            return array("success"=>false, "message"=>get_fi_integration_timeout_message());
        }
        
        $trans_no = $h['trans_no'];
        $tran_date = $h['tran_date'];
        
        $cgfit_msg = can_generate_fi_transaction($trans_type, $trans_no, $tran_date);
        if($cgfit_msg['success'] !== true){ 
            return $cgfit_msg;
        }
        
        $stock_asset_transfer = get_fi_stock_asset_transfer_details($trans_no);

        begin_global_transaction();
        
        while($sat = db_fetch($stock_asset_transfer)){

            $project_id = $sat['asset_project_id'];
            
            if(!(strlen($sat['asset_account']) > 2)){
                    $message = $systypes_array[$trans_type]." #$trans_no - Invalid Asset Account - Field: 'asset_account'";
                    write_fi_log($batch_no, $message);
                    return array("success"=>false, "message"=>$message);
            }else{
                $asset_account = $sat['asset_account'];
            }
        
            if(!(strlen($sat['inventory_account']) > 2)){
                    $message = $systypes_array[$trans_type]." #$trans_no - Invalid Inventory Account - Field: 'inventory_account'";
                    write_fi_log($batch_no, $message);
                    return array("success"=>false, "message"=>$message);
            }else{
                $inventory_account = $sat['inventory_account'];
            }

            $asset_total_amount = $sat['asset_quantity'] * $sat['asset_value'];
            $inventory_total_amount = $sat['stock_item_quantity'] * $sat['stock_item_cost'];
            $detail_line_id = $sat['id'];
            
            $tran_date_ = sql2date($tran_date);
            $comment = "Stock: ".$sat['stock_id']." - ".$sat['stock_description'].PHP_EOL;
            $comment .= "Asset: ".$sat['asset_code']." - ".$sat['asset_description']." - (".$sat['asset_id'].")";

            
            add_fi_trans($batch_no, $trans_type, $trans_no, $detail_line_id, $tran_date_, $asset_account, $project_id, "", $comment, $asset_total_amount);
            add_fi_trans($batch_no, $trans_type, $trans_no, $detail_line_id, $tran_date_, $inventory_account, "", "", $comment, -$inventory_total_amount); 
        }
        
        $num_affercted_row = update_source_transaction_fi_batch_no($trans_type, $trans_no, $batch_no);
        if(!($num_affercted_row == 1)){
            cancel_transaction();
            $message = $systypes_array[$trans_type]." - Could not process #$trans_no.";
            write_fi_log($batch_no, $message);
            return array("success"=>false, "message"=>$message);
        }else{
            add_fi_batch_transaction($batch_no, $trans_type, $trans_no, $tran_date);
            commit_transaction();
        }
        
    }
    
    return array("success"=>true, "message"=>"");
}

