<?php


function fi_asset_quantity_adjustments($batch_no, $cutoff_date_, $timeout_at, $batch_size=1){
    global $systypes_array;
    
    $trans_type = ST_ASSET_QUANTITY_ADJUSTMENT;
    
    $cutoff_date = date2sql($cutoff_date_);
    $default_adjustment_account = get_sys_prefs_parameter_value("default_adj_act");
    
    if(!(strlen($default_adjustment_account) > 2)){
        $message = $systypes_array[$trans_type]." - Invalid Default Adjustment Account";
        write_fi_log($batch_no, $message);
        return false;
    }
    
    $asset_qty_adj_header = get_fi_asset_quantity_adjustment_headers($cutoff_date, $batch_size);
        while($header = db_fetch($asset_qty_adj_header)){
            
            if($timeout_at < time()){
                return array("success"=>false, "message"=>get_fi_integration_timeout_message());
            }
            
            $trans_no = $header['id'];
            $project_id = $header['project_id'];
            $tran_date = $header['tran_date'];
            $tran_date_ = sql2date($tran_date);
            
            $cgfit_msg = can_generate_fi_transaction($trans_type, $trans_no, $tran_date);
            if($cgfit_msg['success'] !== true){ 
                return $cgfit_msg;
            }
            
            begin_global_transaction();
            
            $asset_qty_adj_details = get_fi_asset_quantity_adjustment_details($trans_no);
                if(!(db_num_rows($asset_qty_adj_details) > 0)){
                    cancel_transaction();
                    $message = $systypes_array[$trans_type]." #$trans_no - No Details found.";
                    write_fi_log($batch_no, $message);
                    return array("success"=>false, "message"=>$message);
                }
                

                while($row = db_fetch($asset_qty_adj_details)){
                    $asset_id = $row['asset_id'];
                    $detail_line_id = $row['detail_line_id'];

                    if(!(abs($row['quantity']) > 0)){
                            cancel_transaction();
                            $message = $systypes_array[$trans_type]." #$trans_no - Detail id: $detail_line_id - Invalid Asset quantity - Field: 'quantity'";
                            write_fi_log($batch_no, $message);
                            return array("success"=>false, "message"=>$message);
                    }

                    if(!(strlen($row['asset_account']) > 2)){
                            cancel_transaction();
                            $message = $systypes_array[$trans_type]." #$trans_no - Detail id: $detail_line_id - Invalid Asset Account - Field: 'asset_account'";
                            write_fi_log($batch_no, $message);
                            return array("success"=>false, "message"=>$message);
                    }else{
                        $asset_account = $row['asset_account'];
                    }
                    
                    if(!(strlen($row['asset_adjustment_account']) > 2)){
                            cancel_transaction();
                            $message = $systypes_array[$trans_type]." #$trans_no - Detail id: $detail_line_id - Invalid Asset Adjustment Account - Field: 'asset_adjustment_account'";
                            write_fi_log($batch_no, $message);
                            return array("success"=>false, "message"=>$message);
                    }else{
                        $asset_adjustment_account = $row['asset_adjustment_account'];
                    }

                    $comment = get_asset_formatted_name($asset_id);
                    $currency_code = get_company_pref('curr_default');

                    $amount = $row['quantity'] * $row['asset_value'];
                    if($header['is_deduction'] == 1){
                         $amount = $amount * -1;
                    } 

                    add_fi_trans($batch_no, $trans_type, $trans_no, $detail_line_id, $tran_date_, $asset_account, $project_id, "", $comment, $amount, $currency_code);
                    add_fi_trans($batch_no, $trans_type, $trans_no, $detail_line_id, $tran_date_, $asset_adjustment_account, $project_id, "", $comment, -$amount, $currency_code);
                    
                }

                $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);
                }
                
                add_fi_batch_transaction($batch_no, $trans_type, $trans_no, $tran_date);

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

