Skip to content

Instantly share code, notes, and snippets.

@Antiarchitect
Created April 15, 2010 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Antiarchitect/366697 to your computer and use it in GitHub Desktop.
Save Antiarchitect/366697 to your computer and use it in GitHub Desktop.
class Dcp < Program
...
def after_initialize
ids = Array.new
unless (all_subprograms = subprograms.all(:select => 'id')).nil?
all_subprograms.each do |subprogram|
ids << subprogram.id
end
end
ids << self.id
@all_events = Event.all(:select => 'id', :conditions => {:dcp_id => ids})
super
end
end
class Event < ActiveRecord::Base
...
has_many :actual_financings do
def current(year = Time.now.year.to_int)
last(:order => 'date', :conditions => ['YEAR(date) = ?', year])
end
end
has_many :planned_financings do
def current(year = Time.now.year.to_int)
last(:order => 'date', :conditions => ['YEAR(date) = ?', year])
end
end
has_many :flow_financings do
def current(year = Time.now.year.to_int)
last(:order => 'date', :conditions => ['YEAR(date) = ?', year])
end
end
has_many :planned_results
has_many :achieved_results
def after_initialize
unless (actual_financing = actual_financings.current).nil?
@actual_federal_financing = actual_financing.federal_budget
@actual_current_regional_financing = actual_financing.current_regional_budget
@actual_old_regional_financing = actual_financing.old_regional_budget
@actual_local_financing = actual_financing.local_budget
@actual_extrabudgetary = actual_financing.extrabudgetary
end
unless (flow_financing = flow_financings.current).nil?
@flow_federal_financing = flow_financing.federal_budget
@flow_current_regional_financing = flow_financing.current_regional_budget
@flow_old_regional_financing = flow_financing.old_regional_budget
@flow_local_financing = flow_financing.local_budget
@flow_extrabudgetary = flow_financing.extrabudgetary
end
unless (planned_financing = planned_financings.current).nil?
@planned_federal_financing = planned_financing.federal_budget
@planned_current_regional_financing = planned_financing.current_regional_budget
@planned_old_regional_financing = planned_financing.old_regional_budget
@planned_local_financing = planned_financing.local_budget
@planned_extrabudgetary = planned_financing.extrabudgetary
end
end
include VirtualFinancing
end
class Program < ActiveRecord::Base
...
def after_initialize
unless (@all_events).nil?
@actual_federal_financing = @all_events.sum(&:actual_federal_financing)
@actual_current_regional_financing = @all_events.sum(&:actual_current_regional_financing)
@actual_old_regional_financing = @all_events.sum(&:actual_old_regional_financing)
@actual_local_financing = @all_events.sum(&:actual_local_financing)
@actual_extrabudgetary = @all_events.sum(&:actual_extrabudgetary)
@flow_federal_financing = @all_events.sum(&:flow_federal_financing)
@flow_current_regional_financing = @all_events.sum(&:flow_current_regional_financing)
@flow_old_regional_financing = @all_events.sum(&:flow_old_regional_financing)
@flow_local_financing = @all_events.sum(&:flow_local_financing)
@flow_extrabudgetary = @all_events.sum(&:flow_extrabudgetary)
@planned_federal_financing = @all_events.sum(&:planned_federal_financing)
@planned_current_regional_financing = @all_events.sum(&:planned_current_regional_financing)
@planned_old_regional_financing = @all_events.sum(&:planned_old_regional_financing)
@planned_local_financing = @all_events.sum(&:planned_local_financing)
@planned_extrabudgetary = @all_events.sum(&:planned_extrabudgetary)
end
end
include VirtualFinancing
end
class ReportsController < ApplicationController
layout nil
layout 'application', :except => :create
def new
end
def create
@name = params[:name]
@programs_type = params[:programs_type]
@year = params[:year]
case @programs_type
when 'fcp'
@programs = Fcp.all
@unit = ', млн. рублей'
when 'dcp'
@programs = Dcp.all
@unit = ', тыс. рублей'
end
render :report
end
end
module VirtualFinancing
def self.included(base)
base.class_eval do
def actual_federal_financing
@actual_federal_financing ||= 0
end
def actual_current_regional_financing
@actual_current_regional_financing ||= 0
end
def actual_old_regional_financing
@actual_old_regional_financing ||= 0
end
def actual_regional_financing
actual_current_regional_financing + actual_old_regional_financing
end
def actual_local_financing
@actual_local_financing ||= 0
end
def actual_extrabudgetary
@actual_extrabudgetary ||= 0
end
def flow_federal_financing
@flow_federal_financing ||= 0
end
def flow_current_regional_financing
@flow_current_regional_financing ||= 0
end
def flow_old_regional_financing
@flow_old_regional_financing ||= 0
end
def flow_regional_financing
flow_current_regional_financing + flow_old_regional_financing
end
def flow_local_financing
@flow_local_financing ||= 0
end
def flow_extrabudgetary
@flow_extrabudgetary ||= 0
end
def planned_federal_financing
@planned_federal_financing ||= 0
end
def planned_current_regional_financing
@planned_current_regional_financing ||= 0
end
def planned_old_regional_financing
@planned_old_regional_financing ||= 0
end
def planned_regional_financing
planned_current_regional_financing + planned_old_regional_financing
end
def planned_local_financing
@planned_local_financing ||= 0
end
def planned_extrabudgetary
@planned_extrabudgetary ||= 0
end
def total_actual_financing
actual_federal_financing +
actual_regional_financing +
actual_local_financing +
actual_extrabudgetary
end
def total_flow_financing
flow_federal_financing +
flow_regional_financing +
flow_local_financing +
flow_extrabudgetary
end
def total_planned_financing
planned_federal_financing +
planned_regional_financing +
planned_local_financing +
planned_extrabudgetary
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment