<?php

function fi_asset_depreciation($batch_no, $cutoff_date_){
    global $systypes_array;
    $cutoff_date = date2sql($cutoff_date_);
    $trans_type = ST_ASSET_DEPRECIATION;
    $asset_depreciations = get_fi_asset_depreciation($cutoff_date);

    while($a_dep = db_fetch($asset_depreciations)){
        if($a_dep['depreciation_value'] == 0){
            continue;
        }
        $trans_no = $a_dep['trans_no'];
        $detail_line_id = $a_dep['detail_line_id'];
        
        if(strlen($a_dep['asset_account']) > 0){
            if($a_dep['asset_account'] == '-1' || $a_dep['asset_account'] == '0'){
                $message = $systypes_array[$trans_type]." - #$trans_no Detail id: $detail_line_id - Invalid Asset Account - Field: 'asset_account'";
                write_fi_log($batch_no, $message);
                continue;
            }
        }else{
            $message = $systypes_array[$trans_type]." - #$trans_no Detail id: $detail_line_id - Invalid Asset Account - Field: 'asset_account'";
            write_fi_log($batch_no, $message);
            continue;
        }
        
        if(strlen($a_dep['depreciation_account']) > 0){
            if($a_dep['depreciation_account'] == '-1' || $a_dep['depreciation_account'] == '0'){
                $message = $systypes_array[$trans_type]." - #$trans_no Detail id: $detail_line_id - Invalid Depreciation Account - Field: 'depreciation_account'";
                write_fi_log($batch_no, $message);
                continue;
            }
        }else{
            $message = $systypes_array[$trans_type]." - #$trans_no Detail id: $detail_line_id - Invalid Depreciation Account - Field: 'depreciation_account'";
            write_fi_log($batch_no, $message);
            continue;
        }

        $cgfit_msg = can_generate_fi_transaction($trans_type, $trans_no, $a_dep['tran_date']);
        if($cgfit_msg['success'] !== true){ 
            return $cgfit_msg;
        }
        
        $tran_date_ = sql2date($a_dep['tran_date']);
        $comment = $a_dep['asset_code']." - ".$a_dep['description']." - (".$a_dep['asset_id'].")";
        $currency_code = get_company_pref('curr_default');

        begin_global_transaction();
            add_fi_trans($batch_no, $trans_type, $trans_no, $tran_date_, $a_dep['asset_account'], "", "", $comment, -$a_dep['depreciation_value'], $detail_line_id, 
                         $currency_code);
            add_fi_trans($batch_no, $trans_type, $trans_no, $tran_date_, $a_dep['depreciation_account'], "", "", $comment, $a_dep['depreciation_value'], $detail_line_id, 
                         $currency_code);
            $num_affercted_row = update_source_transaction_fi_batch_no($trans_type, $detail_line_id, $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);
        }else{
            commit_transaction();
        }
    }
}

?>