<?php
/**********************************************************************
***********************************************************************/

function get_asset_depreciation_run_summary($asset_run_id)
{
    $sql = "SELECT ard.id,
                   a.asset_id,
                   a.asset_code,
                   a.description,
                   a.current_value,       
                   ard.processed_by,
                   ard.depreciation_value
                 
            FROM ".TB_PREF."asset_depreciation_run_details ard,
                 ".TB_PREF."asset_master a
                   
            WHERE  a.asset_id = ard.asset_id  
                    AND ard.asset_run_id=".db_escape($asset_run_id);                    

    return db_query($sql, "Asset run records could not be retrieved");    
}

  
function get_calculated_asset_depreciation_details($asset_run_id)
{
    $sql = "SELECT ard.*,
                    a.current_value, 
                    a.purchase_value 
                   
            FROM ".TB_PREF."asset_master a,
                 ".TB_PREF."asset_depreciation_run_details ard       

            WHERE a.asset_id = ard.asset_id 
                    AND processed_by = 0 
                    AND ard.asset_run_id=".db_escape($asset_run_id);                    
    
   return db_query($sql, "");     
}

function update_asset_current_value($asset_id, $new_value, $asset_run_id)
{
    $sql = "UPDATE ".TB_PREF."asset_master 
                        SET current_value=".db_escape($new_value).",
                            last_depreciation_run=".db_escape($asset_run_id)."
                        WHERE asset_id = ".db_escape($asset_id);
    
           db_query($sql, "Could not Update approve asset real value");
           return db_num_affected_rows();   
           
}

function update_asset_depreciation_detail_processed($id)
{
    $user_id = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
        
    $sql = "UPDATE ".TB_PREF."asset_depreciation_run_details 
            SET processed_by =".db_escape($user_id).",
                processed_on =".db_escape($datetime)."
            WHERE id = ".db_escape($id)." 
                AND processed_by = 0 ";
    
    db_query($sql, "update_asset_depreciation_detail_processed");
    return db_num_affected_rows();  
}

function add_processed_asset_depreciation_detail($asset_run_id, $asset_id, $purchased_value, $starting_value,
                                                          $depreciation_type, $depreciation_rate, 
                                                          $depreciation_value, $asset_account, $depreciation_account,
                                                          $accumulated_account)
{
    $user_id = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');

    $sql = "INSERT INTO ".TB_PREF."processed_asset_depreciation_details 
                              (asset_run_id, asset_id, purchased_value, starting_value,
                               depreciation_type, depreciation_rate, depreciation_value,
                               asset_account, depreciation_account, accumulated_account,
                               processed_by, processed_on)
           
           VALUES(".db_escape($asset_run_id).",
                  ".db_escape($asset_id).",  
                  ".db_escape($purchased_value).",
                  ".db_escape($starting_value).",  
                  ".db_escape($depreciation_type).",
                  ".db_escape($depreciation_rate).",
                  ".db_escape($depreciation_value).",
                  ".db_escape($asset_account).",
                  ".db_escape($depreciation_account).",
                  ".db_escape($accumulated_account).",
                  ".db_escape($user_id).",
                  ".db_escape($datetime).")";
                      
     db_query($sql, "add_processed_asset_depreciation_detail");
     
}

?>