This repository has been archived on 2024-12-01. You can view files and clone it, but cannot push or open issues or pull requests.
redmine_rodax_crm/app/models/contract.rb
2012-01-03 19:11:27 +00:00

121 lines
4.6 KiB
Ruby

class Contract < ActiveRecord::Base
unloadable
belongs_to :project
belongs_to :author, :class_name => "User", :foreign_key => "author_id"
belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
belongs_to :category, :class_name => 'ContractCategory', :foreign_key => 'category_id'
belongs_to :contact
belongs_to :status, :class_name => "ContractStatus", :foreign_key => "status_id"
has_many :contracts, :class_name => "contract", :foreign_key => "reference_id"
has_many :notes, :as => :source, :class_name => 'ContractNote', :dependent => :delete_all, :order => "created_on DESC"
has_many :contract_processes, :dependent => :delete_all
has_and_belongs_to_many :related_contacts, :class_name => 'Contact', :order => "#{Contact.table_name}.last_name, #{Contact.table_name}.first_name", :uniq => true
named_scope :visible, lambda {|*args| { :include => :project,
:conditions => Project.allowed_to_condition(args.first || User.current, :view_contracts)} }
named_scope :deletable, lambda {|*args| { :include => :project,
:conditions => Project.allowed_to_condition(args.first || User.current, :delete_contracts) }}
named_scope :live_search, lambda {|search| {:conditions => ["(#{Contract.table_name}.name LIKE ?)", "%#{search}%"] }}
named_scope :open, :include => :status, :conditions => ["#{ContractStatus.table_name}.is_closed = ?", false]
acts_as_customizable
acts_as_viewable
acts_as_watchable
acts_as_attachable :view_permission => :view_contracts,
:delete_permission => :edit_contracts
acts_as_event :datetime => :created_on,
:url => Proc.new {|o| {:controller => 'contracts', :action => 'show', :id => o}},
:type => 'icon-report',
:title => Proc.new {|o| o.name },
:description => Proc.new {|o| [o.price, o.contact ? o.contact.name : nil, o.background].join(' ').strip }
acts_as_searchable :columns => ["#{table_name}.name",
"#{table_name}.background"],
:include => [:project],
# sort by id so that limited eager loading doesn't break with postgresql
:order_column => "#{table_name}.id"
validates_presence_of :name
validates_numericality_of :price, :allow_nil => true
after_save :create_contract_process
include ActionView::Helpers::NumberHelper
include ::ContractsHelper
def after_initialize
if new_record?
# set default values for new records only
self.status ||= ContractStatus.default
end
end
def avatar
end
def full_name
result = ''
result << self.contact.name + ": " unless self.contact.blank?
result << self.name
end
def all_contacts
@all_contacts ||= ([self.contact] + self.related_contacts ).uniq
end
def self.available_users(prj=nil)
cond = "(1=1)"
cond << " AND #{Contract.table_name}.project_id = #{prj.id}" if prj
User.active.find(:all, :select => "DISTINCT #{User.table_name}.*", :joins => "JOIN #{Contract.table_name} ON #{Contract.table_name}.assigned_to_id = #{User.table_name}.id", :conditions => cond, :order => "#{User.table_name}.lastname, #{User.table_name}.firstname")
end
def init_contract_process(author)
@current_contract_process ||= ContractProcess.new(:contract => self, :author => (author || User.current))
@contract_status_before_change = self.new_record? ? nil : self.status_id
updated_on_will_change!
@current_contract_process
end
def create_contract_process
if @current_contract_process && !(@contract_status_before_change == self.status_id)
@current_contract_process.old_value = @contract_status_before_change
@current_contract_process.value = self.status_id
@current_contract_process.save
# reset current journal
init_contract_process @current_contract_process.author
end
end
def visible?(usr=nil)
(usr || User.current).allowed_to?(:view_contracts, self.project)
end
def editable?(usr=nil)
(usr || User.current).allowed_to?(:edit_contracts, self.project)
end
def destroyable?(usr=nil)
(usr || User.current).allowed_to?(:delete_contracts, self.project)
end
def info
result = ""
result = self.status.name if self.status
result = result + " - " + contract_price(self) if !self.price.blank?
result
end
end