<?php


function calculate_asset_depreciations($asset_run_id)
{
    //get current value, depreciation rate for all assets                          
        
        $asset_depreciation_run = get_asset_depreciation_run_details($asset_run_id);
        
        if(!($asset_depreciation_run['no_of_months'] > 0)){
            display_error('"No Of Months" is not a positive value. Can calculate depreciation');
            return;
        }
        
        update_asset_depreciation_run_status($asset_run_id, "C"); // Calculating
    
        delete_asset_depreciation_run_asset_run_details($asset_run_id); //delete all assets if we enter the same asset_run_id
        
        $asset_value = get_asset_current_value(); // after the delete, get the values from assets & asset categories table for calculating the depreciation
         
        $j = 0;
         //while loop// for every asset
        while($myrow = db_fetch($asset_value))
        {        
        // calclate depreciation
           $purchased_value = $myrow['purchase_value'] ;
           $depreciation_rate = $myrow['depreciation_rate'];
           $depreciation_value = ($depreciation_rate /12 * $asset_depreciation_run['no_of_months'])/100 * $purchased_value ;
           $asset_id = $myrow['asset_id'];
           $starting_value = $myrow['current_value'];
           
           $depreciation_type = 'P';
           $asset_account = $myrow['asset_account'];
           $depreciation_account = $myrow['depreciation_account'];
           $accumulated_account = $myrow['accumulated_account'];
                   
             
            insert_calculation_value_to_asset_depreciation_run_details($asset_run_id, $asset_id, $starting_value,
                                                          $depreciation_type, $depreciation_rate, 
                                                          $depreciation_value, $asset_account, $depreciation_account,
                                                          $accumulated_account);
     
                 
//            display_notification("Asset Run Details has been added");
              
            $j++;          
        }
        
         $depreciation_summary = calculate_depreciation_summary($asset_run_id); //start of the calculate depreciation summary
         
         $j = 0;
            
        while($row = db_fetch($depreciation_summary))
        {
            $depreciation_value = $row['dv'];
            $depreciation_account = $row['dc'];
            
            insert_depreciation_summary($depreciation_value, $depreciation_account, $asset_run_id); //insert depreciation summary            
            
        }        
        
        $asset_account_summary = calculate_asset_account_summary($asset_run_id); // calculate asset account summary
         
        $j = 0;
        
        while($asset_row = db_fetch($asset_account_summary))
        {
            $depreciation_value = $asset_row['dv'];
            $asset_account = $asset_row['ac'];
            
            insert_asset_account_summary($depreciation_value, $asset_account, $asset_run_id );
            
        }
          
        update_asset_depreciation_run_status($asset_run_id, "L"); // Calculated        
        
}

   


?>























