# Fat Free CRM
# Copyright (C) 2008-2011 by Michael Dvorkin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------------------------------

# == Schema Information
# Schema version: 1
#
# Table name: service_requests
#
#  id              :integer(4)      not null, primary key
#  user_id         :integer(4)
#  assigned_to     :integer(4)
#  completed_by    :integer(4)
#  name            :string(255)     default(""), not null
#  asset_id        :integer(4)
#  asset_type      :string(255)
#  priority        :string(32)
#  status          :string(32)
#  description     :text
#  reported_at     :datetime
#  due_at          :datetime
#  completed_at    :datetime
#  deleted_at      :datetime
#  created_at      :datetime
#  updated_at      :datetime
#
class ServiceRequest < ActiveRecord::Base
  belongs_to  :user
  belongs_to  :account
  belongs_to  :assignee, :class_name => "User", :foreign_key => :assigned_to
  belongs_to  :completor, :class_name => "User", :foreign_key => :completed_by
  has_one     :account_service_request, :dependent => :destroy
  has_one     :account, :through => :account_service_request
  has_many    :activities, :as => :subject, :order => 'created_at DESC'
  has_many    :emails, :as => :mediator

  simple_column_search :name, :match => :middle, :escape => lambda { |query| query.strip }

  named_scope :with_priority, lambda { |priority| { :conditions => ["priority LIKE ?", priority] } }
  named_scope :only_priorities, lambda { |filters| { :conditions => [ "priority IN (?)", filters ] } }
  named_scope :only_statuses, lambda { |filters| { :conditions => [ "status IN (?)", filters ] } }
  named_scope :with_status, lambda { |status| { :conditions => ["status = ?", status] } }  
  
  uses_user_permissions
  acts_as_commentable
  is_paranoid
  exportable

  validates_presence_of :name, :message => :missing_service_request_name
  validates_presence_of :description, :message => :missing_service_request_description  

  def status_as_slug
    self.status.downcase.gsub(/\s/, '_')
  end
  
  def status_in_words
    self.status
  end
 
  def update_with_account_and_permissions(params)
    account = Account.create_or_select_for(self, params[:account], params[:users])
    self.account_service_request = AccountServiceRequest.new(:account => account, :service_request => self) unless account.id.blank?
    self.update_with_permissions(params[:service_request], params[:users])
  end

  def save_with_account_and_permissions(params)
    account = Account.create_or_select_for(self, params[:account], params[:users])
    self.account_service_request = AccountServiceRequest.new(:account => account, :service_request => self) unless account.id.blank?
    #self.contacts << Contact.find(params[:contact]) unless params[:contact].blank?
    self.save_with_permissions(params[:users])
  end
end

