class CreatePeriods < ActiveRecord::Migration

  def self.up
    create_table :periods do |t|
      t.date :month
      t.string :department
      t.boolean :closed
    end

    create_table :payrolls do |t|
      t.integer :employee_id, :period_id, :zp_type_id
      t.boolean :finn_closed, :default => false     
      t.decimal :sum,  :precision => 10, :scale => 2
      t.string :description
      t.text :done_ids
    end

    pers = (Date.new(2008, 01)..Date.new(2015, 12)).select {|d| d.day == 1}
  
    for month in pers do
      if month >= '01.01.2015'.to_date
        cl = false
      else
        cl = true
      end
      for dep in ['mod', 'corp', 'soft', 'ams', 'welder', 'private', 'alp'] do
        Period.create(:month => month, :department => dep, :closed => cl)
      end
    end
    
    empls = Employee.find [79494, 4810, 494312, 5532]
    for empl in empls do
      accs = empl.accesses.select{|i| i.access_superuser == 1}
        for acc in accs do
        acc.update_attributes(:access_admin => 1)
      end
    end

  end 

  def self.down
    drop_table :periods
    drop_table :payrolls
  end

end
