class ContractsController < ApplicationController unloadable PRICE_TYPE_PULLDOWN = [l(:label_price_fixed_bid), l(:label_price_per_hour)] before_filter :find_contract, :only => [:show, :edit, :update, :destroy] before_filter :find_project_by_project_id, :only => [:new, :create] before_filter :bulk_find_contracts, :only => [:bulk_update, :bulk_edit, :bulk_destroy, :context_menu] before_filter :authorize, :except => [:index] before_filter :find_optional_project, :only => [:index] # before_filter :find_contracts, :only => :index before_filter :update_contract_from_params, :only => [:edit, :update] before_filter :build_new_contract_from_params, :only => [:new, :create] before_filter :find_contract_attachments, :only => :show helper :attachments helper :contacts helper :notes helper :timelog helper :watchers helper :custom_fields include WatchersHelper include ContractsHelper def new @contract = Contract.new @contract.contact = Contact.find(params[:contact_id]) if params[:contact_id] @contract.assigned_to = User.current end def create @contract = Contract.new(params[:contract]) # @contract.contacts = [Contact.find(params[:contacts])] @contract.project = @project @contract.author = User.current @contract.init_contract_process(User.current) if @contract.save flash[:notice] = l(:notice_successful_create) redirect_to :action => "show", :id => @contract else render :action => "new" end end def update @contract.init_contract_process(User.current) if @contract.update_attributes(params[:contract]) # @contract.contacts = [Contact.find(params[:contacts])] if params[:contacts] flash[:notice] = l(:notice_successful_update) respond_to do |format| format.html { redirect_to :action => "show", :id => @contract } format.xml { } end else respond_to do |format| format.html { render :action => "edit"} end end end def edit respond_to do |format| format.html { } format.xml { } end end def index retrieve_contracts_query params[:status_id] = "o" unless params.has_key?(:status_id) find_contracts respond_to do |format| format.html{ request.xhr? ? render( :partial => "list", :layout => false, :locals => {:contracts => @contracts}) : last_notes } format.xml { render :xml => @contracts} format.json { render :text => @contracts.to_json, :layout => false } end end def show @note = ContractNote.new respond_to do |format| format.html { @contract.viewed } format.xml { } end end def destroy if @contract.destroy flash[:notice] = l(:notice_successful_delete) else flash[:error] = l(:notice_unsuccessful_save) end redirect_to :action => "index", :project_id => params[:project_id] end def context_menu @contract = @contracts.first if (@contracts.size == 1) @can = {:edit => User.current.allowed_to?(:edit_contracts, @projects), :delete => User.current.allowed_to?(:delete_contracts, @projects) } # @back = back_url render :layout => false end def bulk_destroy @contracts.each do |contract| begin contract.reload.destroy rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists # nothing to do, issue was already deleted (eg. by a parent) end end respond_to do |format| format.html { redirect_back_or_default(:action => 'index', :project_id => @project) } format.api { head :ok } end end def bulk_edit @available_statuses = @projects.map(&:contract_statuses).inject{|memo,w| memo & w} @available_categories = @projects.map(&:contract_categories).inject{|memo,w| memo & w} @assignables = @projects.map(&:assignable_users).inject{|memo,a| memo & a} end def bulk_update unsaved_contract_ids = [] @contracts.each do |contract| contract.reload contract.init_contract_process(User.current) unless contract.update_attributes(parse_params_for_bulk_contract_attributes(params)) # Keep unsaved issue ids to display them in flash error unsaved_contract_ids << contract.id end if params[:note] && !params[:note][:content].blank? note = ContractNote.new(params[:note]) note.author = User.current contract.notes << note end end set_flash_from_bulk_issue_save(@contracts, unsaved_contract_ids) redirect_back_or_default({:controller => 'contracts', :action => 'index', :project_id => @project}) end private def last_notes(count=5) # TODO: ????????? ???????? ???? ? ???????? ??? ? ?????? acts-as-noteble scope = ContractNote.scoped({}) scope = scope.scoped(:conditions => ["#{Contract.table_name}.project_id = ?", @project.id]) if @project @last_notes = scope.visible.find(:all, :limit => count, :order => "#{ContractNote.table_name}.created_on DESC") end def build_new_contract_from_params end def update_contract_from_params end def find_contract_attachments @contract_attachments = Attachment.find(:all, :conditions => { :container_type => "Note", :container_id => @contract.notes.map(&:id)}, :order => "created_on DESC") end def find_contracts(pages=true) retrieve_date_range(params[:period].to_s) scope = Contract.scoped({}) scope = scope.scoped(:conditions => ["#{Contract.table_name}.project_id = ?", @project.id]) if @project scope = scope.scoped(:conditions => ["#{Contract.table_name}.status_id = ?", params[:status_id]]) if (!params[:status_id].blank? && params[:status_id] != "o") scope = scope.scoped(:conditions => ["#{Contract.table_name}.category_id = ?", params[:category_id]]) if !params[:category_id].blank? scope = scope.scoped(:conditions => ["#{Contract.table_name}.assigned_to_id = ?", params[:assigned_to_id]]) if !params[:assigned_to_id].blank? scope = scope.scoped(:conditions => ["#{Contract.table_name}.created_on BETWEEN ? AND ?", @from, @to]) if (@from && @to) params[:search].split(' ').collect{ |search_string| scope = scope.live_search(search_string) } if !params[:search].blank? scope = scope.visible scope = scope.scoped(:include => :status, :conditions => ["#{ContractStatus.table_name}.is_closed = ?", false]) if (params[:status_id] == "o") scope = scope.scoped(:order => :status_id) @contracts_count = scope.count if pages @contracts_sum = scope.sum(:price, :group => :currency) page_size = params[:page_size].blank? ? 20 : params[:page_size].to_i @contracts_pages = Paginator.new(self, @contracts_count, page_size, params[:page]) @offset = @contracts_pages.current.offset @limit = @contracts_pages.items_per_page scope = scope.scoped :limit => @limit, :offset => @offset @contracts = scope fake_name = @contracts.first.price if @contracts.length > 0 #without this patch paging does not work end scope end # Filter for bulk issue operations def bulk_find_contracts @contracts = Contract.find_all_by_id(params[:id] || params[:ids], :include => :project) raise ActiveRecord::RecordNotFound if @contracts.empty? if @contracts.detect {|contract| !contract.visible?} deny_access return end @projects = @contracts.collect(&:project).compact.uniq @project = @projects.first if @projects.size == 1 rescue ActiveRecord::RecordNotFound render_404 end def find_contract @contract = Contract.find(params[:id], :include => [:project, :status, :category]) @project ||= @contract.project # if !(params[:project_id] == @project.identifier) # params[:project_id] = @project.identifier # redirect_to params # end rescue ActiveRecord::RecordNotFound render_404 end def parse_params_for_bulk_contract_attributes(params) attributes = (params[:contract] || {}).reject {|k,v| v.blank?} attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] attributes end end