# -*- coding: utf-8 -*-
class CategoryController < ApplicationController
  	
  # Авторизация (файл: controllers/application.rb)
  before_filter :isAuthorization

  # Права доступа (файл: controllers/application.rb)
  around_filter :isAccessSuper

  # Конструктор
  before_filter :construct

  def construct
    if(session[:isAdmin]=='true')
      $department = Check.isText(params[:d])
    else
      $department = Check.isText(params[:d])
#      $department = Check.isText(cookies[:sys_dep])
    end
  end


  
  def index
    if(session[:isAdmin]=='true')
    else
      if(session[:isSuperUser]=='true')
        redirect_to :action => "edit"
      else
        redirect_to :action => "error", :code=>1
      end
    end
  end


  def edit
    if $department && $department != ''
      @result = Category.find_by_sql("SELECT * FROM category WHERE department='"+$department.to_s+"' ORDER BY sort ASC")
    elsif session[:isAdmin]
      @result = Category.find_by_sql("SELECT * FROM category ORDER BY department, sort")
    end
  end



	def addcategory
	  @title=''
	  @price=''
          @disp_wage = ''
          @bonus=''
          @description = ''
          @show = ''
          @master_percent = ''
	  @sort = 500

		begin
			CategoryDB.find(params[:id].to_i)
		rescue
                  @category = Category.new
			print 'Куда полез?'
		else
                  # get data work
                  
                  @category = Category.find params[:id]
                  @id = @category.id
                  @title = @category.name
                  @price = @category.price.to_s
                  @disp_wage = @category.disp_wage.to_s
                  @show = @category.show
                  @bonus = @category.bonus
                  @master_percent = @category.master_percent
                  @sort = @category.sort
                  @description = @category.description
		end
		
	end

	def savecategory
		if(params[:id].to_i>0)
			id = Check.intval(params[:id]).to_i
			sv_work = CategoryDB.find(id)
		else
			sv_work = CategoryDB.new
		end
		# get
          sv_work.name = Check.isText(params[:title])
          sv_work.price = Check.isText(params[:price])
          sv_work.disp_wage = Check.isText(params[:disp_wage])
          sv_work.bonus = Check.isText(params[:bonus])
          sv_work.master_percent = Check.isText(params[:master_percent])
          sv_work.show = params[:show] || false
          sv_work.description = Check.isText(params[:description])
          sv_work.sort = Check.isText(params[:sort])
          sv_work.department = Check.isText(params[:department])
          sv_work.income_id = params[:category][:income_id]
          sv_work.login = params[:login]
          sv_work.loginroot = params[:loginroot]
          sv_work.inst = params[:inst]
          sv_work.process_id = params[:process_id]
          sv_work.demounting = params[:demounting]
          sv_work.change = params[:change]
          sv_work.mat_inst = params[:mat_inst]
          sv_work.mat_sale = params[:mat_sale]
          sv_work.mat_rent = params[:mat_rent]
          sv_work.mat_rent_rate = params[:mat_rent_rate]          
          sv_work.quest_bonus = params[:quest_bonus]          
          sv_work.save
          redir = (params[:d] && params[:d] != '') ? Check.isText(params[:d]) : ''

		redirect_to :action => "edit", :d=>redir
	end
	
	def delcategory
		if(params[:id].to_i>0)
			id = params[:id].to_i
			begin
				CategoryDB.find(id)
			rescue	
				print 'Такого идентификатора - НЕТ.'
			else 	
				# delete
                          cat = Category.find params[:id]
                          if Report.find(:all, :conditions =>"category = #{cat.id}").size == 0
                            cat.destroy
                          else
                            cat.update_attributes(:show => 0)
                          end

				# redirect
				redirect_to :action => "index"
			end
		end
	end

        def update_value
          @category = Category.find(params[:id])
          if @category.update_attributes(params[:attr_name].to_sym => params[:attr_val].to_bool)
            render(:nothing => true)
          else 
            render(:text => "Error")
          end
        end





end



