From 8fabfb85d31e97312cadf503791a3742e92ce3df Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Tue, 1 Nov 2011 23:50:18 -0300 Subject: [PATCH] [bsc-contract] Controllers --- plugins/bsc/controllers/bsc_plugin_environment_controller.rb | 2 ++ plugins/bsc/controllers/bsc_plugin_myprofile_controller.rb | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/bsc/test/functional/bsc_plugin_myprofile_controller_test.rb | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 329 insertions(+), 1 deletion(-) diff --git a/plugins/bsc/controllers/bsc_plugin_environment_controller.rb b/plugins/bsc/controllers/bsc_plugin_environment_controller.rb index 9b870ef..a52b484 100644 --- a/plugins/bsc/controllers/bsc_plugin_environment_controller.rb +++ b/plugins/bsc/controllers/bsc_plugin_environment_controller.rb @@ -1,3 +1,5 @@ +include BscPlugin::BscHelper + class BscPluginEnvironmentController < AdminController def new diff --git a/plugins/bsc/controllers/bsc_plugin_myprofile_controller.rb b/plugins/bsc/controllers/bsc_plugin_myprofile_controller.rb index 3954e8d..e095900 100644 --- a/plugins/bsc/controllers/bsc_plugin_myprofile_controller.rb +++ b/plugins/bsc/controllers/bsc_plugin_myprofile_controller.rb @@ -1,3 +1,5 @@ +include BscPlugin::BscHelper + class BscPluginMyprofileController < MyProfileController def manage_associated_enterprises @@ -85,4 +87,129 @@ class BscPluginMyprofileController < MyProfileController end end + def manage_contracts + self.class.no_design_blocks + @sorting = params[:sorting] || 'created_at asc' + sorted_by = @sorting.split(' ').first + sort_direction = @sorting.split(' ').last + @status = params[:status] || BscPlugin::Contract::Status.types.map { |s| s.to_s } + @contracts = profile.contracts. + status(@status). + sorted_by(sorted_by, sort_direction). + paginate(:per_page => contracts_per_page, :page => params[:page]) + end + + def new_contract + if !request.post? + @contract = BscPlugin::Contract.new + else + @contract = BscPlugin::Contract.new(params[:contract]) + @contract.bsc = profile + sales = params[:sales] ? params[:sales].map {|key, value| value} : [] + sales.reject! {|sale| sale[:product_id].blank?} + + if @contract.save! + enterprises_ids = params[:enterprises] || '' + enterprises_ids.split(',').each { |id| @contract.enterprises << Enterprise.find(id) } + @failed_sales = @contract.save_sales(sales) + + if @failed_sales.blank? + session[:notice] = _('Contract created.') + redirect_to :action => 'manage_contracts' + else + session[:notice] = _('Contract created but some products could not be added.') + redirect_to :action => 'edit_contract', :contract_id => @contract.id + end + end + end + end + + def view_contract + begin + @contract = BscPlugin::Contract.find(params[:contract_id]) + rescue + session[:notice] = _('Contract doesn\'t exists! Maybe it was already removed.') + redirect_to :action => 'manage_contracts' + end + end + + def edit_contract + begin + @contract = BscPlugin::Contract.find(params[:contract_id]) + rescue + session[:notice] = _('Could not edit such contract.') + redirect_to :action => 'manage_contracts' + end + if request.post? && @contract.update_attributes(params[:contract]) + + # updating associated enterprises + enterprises_ids = params[:enterprises] || '' + enterprises = [Enterprise.find(enterprises_ids.split(','))].flatten + to_remove = @contract.enterprises - enterprises + to_add = enterprises - @contract.enterprises + to_remove.each { |enterprise| @contract.enterprises.delete(enterprise)} + to_add.each { |enterprise| @contract.enterprises << enterprise } + + # updating sales + sales = params[:sales] ? params[:sales].map {|key, value| value} : [] + sales.reject! {|sale| sale[:product_id].blank?} + products = [Product.find(sales.map { |sale| sale[:product_id] })].flatten + to_remove = @contract.products - products + to_keep = sales.select { |sale| @contract.products.include?(Product.find(sale[:product_id])) } + + to_keep.each do |sale_attrs| + sale = @contract.sales.find_by_product_id(sale_attrs[:product_id]) + sale.update_attributes!(sale_attrs) + sales.delete(sale_attrs) + end + + to_remove.each { |product| @contract.sales.find_by_product_id(product.id).destroy } + @failed_sales = @contract.save_sales(sales) + + if @failed_sales.blank? + session[:notice] = _('Contract edited.') + redirect_to :action => 'manage_contracts' + else + session[:notice] = _('Contract edited but some products could not be added.') + redirect_to :action => 'edit_contract', :contract_id => @contract.id + end + end + end + + def destroy_contract + begin + contract = BscPlugin::Contract.find(params[:contract_id]) + contract.destroy + session[:notice] = _('Contract removed.') + rescue + session[:notice] = _('Contract could not be removed. Sorry! ^^') + end + redirect_to :action => 'manage_contracts' + end + + def search_contract_enterprises + render :text => profile.enterprises.find(:all, :conditions => ["(LOWER(name) LIKE ? OR LOWER(identifier) LIKE ?)", "%#{params[:enterprises]}%", "%#{params[:enterprises]}%"]). + map {|enterprise| {:id => enterprise.id, :name => enterprise.short_name(60)} }. + to_json + end + + def search_sale_product + query = params[:sales].map {|key, value| value}[0][:product_id] + enterprises = (params[:enterprises] || []).split(',') + enterprises = enterprises.blank? ? '' : enterprises + added_products = (params[:added_products] || []).split(',') + added_products = added_products.blank? ? '' : added_products + render :text => Product.find(:all, :conditions => ["LOWER(name) LIKE ? AND enterprise_id IN (?) AND id NOT IN (?)", "%#{query}%", enterprises, added_products]). + map {|product| { :id => product.id, + :name => short_text(product_display_name(product), 60), + :sale_id => params[:sale_id], + :product_price => product.price || 0 }}. + to_json + end + + private + + def contracts_per_page + 15 + end end diff --git a/plugins/bsc/test/functional/bsc_plugin_myprofile_controller_test.rb b/plugins/bsc/test/functional/bsc_plugin_myprofile_controller_test.rb index 5264a1a..aa7440f 100644 --- a/plugins/bsc/test/functional/bsc_plugin_myprofile_controller_test.rb +++ b/plugins/bsc/test/functional/bsc_plugin_myprofile_controller_test.rb @@ -40,7 +40,7 @@ class BscPluginMyprofileControllerTest < Test::Unit::TestCase e6 = Enterprise.create!(:name => 'Bla', :identifier => 'sample-enterprise-6') get :search_enterprise, :profile => bsc.identifier, :q => 'sampl' - + assert_match /#{e1.name}/, @response.body assert_match /#{e2.name}/, @response.body assert_no_match /#{e3.name}/, @response.body @@ -108,5 +108,204 @@ class BscPluginMyprofileControllerTest < Test::Unit::TestCase assert_equal enterprise.bsc, bsc end + should 'fecth contracts filtered by status' do + contract0 = BscPlugin::Contract.create!(:bsc => bsc, :status => 0, :client_name => 'Marvin') + contract1 = BscPlugin::Contract.create!(:bsc => bsc, :status => 1, :client_name => 'Marvin') + contract2 = BscPlugin::Contract.create!(:bsc => bsc, :status => 2, :client_name => 'Marvin') + contract3 = BscPlugin::Contract.create!(:bsc => bsc, :status => 3, :client_name => 'Marvin') + + get :manage_contracts, :profile => bsc.identifier, :status => ['1', '3'] + + assert_not_includes assigns(:contracts), contract0 + assert_includes assigns(:contracts), contract1 + assert_not_includes assigns(:contracts), contract2 + assert_includes assigns(:contracts), contract3 + end + + should 'manage contracts should have all status marked by default' do + get :manage_contracts, :profile => bsc.identifier + assert_equal assigns(:status), BscPlugin::Contract::Status.types.map { |s| s.to_s } + end + + should 'fetch contracts sorted accordingly' do + contract0 = BscPlugin::Contract.create!(:bsc => bsc, :created_at => 1.day.ago, :client_name => 'Eva') + contract1 = BscPlugin::Contract.create!(:bsc => bsc, :created_at => 2.day.ago, :client_name => 'Adam') + contract2 = BscPlugin::Contract.create!(:bsc => bsc, :created_at => 3.day.ago, :client_name => 'Marvin') + + by_date = [contract2, contract1, contract0] + by_name = [contract1, contract0, contract2] + + get :manage_contracts, :profile => bsc.identifier, :sorting => 'created_at asc' + assert_equal by_date, assigns(:contracts) + + get :manage_contracts, :profile => bsc.identifier, :sorting => 'created_at desc' + assert_equal by_date.reverse, assigns(:contracts) + + get :manage_contracts, :profile => bsc.identifier, :sorting => 'client_name asc' + assert_equal by_name, assigns(:contracts) + + get :manage_contracts, :profile => bsc.identifier, :sorting => 'client_name desc' + assert_equal by_name.reverse, assigns(:contracts) + end + + should 'limit the contracts to defined per page' do + BscPlugin::Contract.create!(:bsc => bsc, :client_name => 'Marvin') + BscPlugin::Contract.create!(:bsc => bsc, :client_name => 'Marvin') + + @controller.stubs(:contracts_per_page).returns(1) + + get :manage_contracts, :profile => bsc.identifier + + assert_equal 1, assigns(:contracts).count + end + + should 'destroy contract' do + contract = BscPlugin::Contract.create!(:bsc => bsc, :client_name => 'Marvin') + + assert_difference BscPlugin::Contract, :count, -1 do + get :destroy_contract, :profile => bsc.identifier, :contract_id => contract.id + end + + assert_raise ActiveRecord::RecordNotFound do + BscPlugin::Contract.find(contract.id) + end + end + + should 'not crash if trying to destroy a contract that does not exists' do + assert_nothing_raised do + get :destroy_contract, :profile => bsc.identifier, :contract_id => -1 + end + assert_redirected_to :action => 'manage_contracts' + end + + should 'not crash if trying to edit a contract that does not exists' do + assert_nothing_raised do + get :edit_contract, :profile => bsc.identifier, :contract_id => -1 + end + assert_redirected_to :action => 'manage_contracts' + end + + should 'create contract associating the enterprises' do + enterprise1 = fast_create(Enterprise) + enterprise2 = fast_create(Enterprise) + + post :new_contract, :profile => bsc.identifier, :enterprises => "#{enterprise1.id},#{enterprise2.id}", :contract => {:bsc => bsc, :client_name => 'Marvin'} + + bsc.reload + contract = bsc.contracts.last + + assert_includes contract.enterprises, enterprise1 + assert_includes contract.enterprises, enterprise2 + end + + should 'edit contract adding or removing enterprises accordingly' do + enterprise1 = fast_create(Enterprise) + enterprise2 = fast_create(Enterprise) + enterprise3 = fast_create(Enterprise) + contract = BscPlugin::Contract.create!(:bsc => bsc, :client_name => 'Marvin') + contract.enterprises << enterprise1 + contract.enterprises << enterprise2 + + post :edit_contract, :profile => bsc.identifier, :contract_id => contract.id, :enterprises => "#{enterprise2.id},#{enterprise3.id}", :contract => {:bsc => bsc} + contract.reload + + assert_not_includes contract.enterprises, enterprise1 + assert_includes contract.enterprises, enterprise2 + assert_includes contract.enterprises, enterprise3 + end + + should 'not crash if there is no enterprises on create' do + assert_nothing_raised do + post :new_contract, :profile => bsc.identifier, :contract => {:bsc => bsc, :client_name => 'Marvin'} + end + end + + should 'create contract with associated sales' do + product1 = fast_create(Product, :price => 2.50) + product2 = fast_create(Product) + sale1 = {:product_id => product1.id, :quantity => 2} + sale2 = {:product_id => product2.id, :quantity => 5, :price => 3.50} + sales = {1 => sale1, 2 => sale2} + + post :new_contract, :profile => bsc.identifier, :sales => sales, :contract => {:bsc => bsc, :client_name => 'Marvin'} + + bsc.reload + contract = bsc.contracts.last + + assert_includes contract.products, product1 + assert_includes contract.products, product2 + + assert_equal sale1[:quantity], contract.sales.find_by_product_id(sale1[:product_id]).quantity + assert_equal sale2[:quantity], contract.sales.find_by_product_id(sale2[:product_id]).quantity + assert_equal sale2[:price], contract.sales.find_by_product_id(sale2[:product_id]).price + end + + should 'edit contract adding or removing sales accordingly' do + product1 = fast_create(Product) + product2 = fast_create(Product) + product3 = fast_create(Product) + contract = BscPlugin::Contract.create!(:bsc => bsc, :client_name => 'Marvin') + BscPlugin::Sale.create!(:product => product1, :contract => contract, :quantity => 1) + BscPlugin::Sale.create!(:product => product2, :contract => contract, :quantity => 1) + sales = {1 => {:product_id => product2.id, :quantity => 1}, 2 => {:product_id => product3.id, :quantity => 1}} + + post :edit_contract, :profile => bsc.identifier, :contract_id => contract.id, :sales => sales , :contract => {:bsc => bsc} + contract.reload + + assert_not_includes contract.products, product1 + assert_includes contract.products, product2 + assert_includes contract.products, product3 + end + + should 'edit sales informations' do + product1 = fast_create(Product) + product2 = fast_create(Product) + contract = BscPlugin::Contract.create!(:bsc => bsc, :client_name => 'Marvin') + sale1 = BscPlugin::Sale.create!(:product => product1, :contract => contract, :quantity => 1, :price => 1.0) + sale2 = BscPlugin::Sale.create!(:product => product2, :contract => contract, :quantity => 2, :price => 2.0) + sale2.save! + sales = {1 => {:product_id => product1.id, :quantity => 3, :price => 5.0}, 2 => {:product_id => product2.id, :quantity => 4, :price => 10.0}} + + post :edit_contract, :profile => bsc.identifier, :contract_id => contract.id, :sales => sales , :contract => {:bsc => bsc} + + sale1.reload + sale2.reload + + assert_equal 3, sale1.quantity + assert_equal 5.0, sale1.price + assert_equal 4, sale2.quantity + assert_equal 10.0, sale2.price + end + + should 'redirect to edit contract if some sale could not be created' do + product = fast_create(Product) + # sale without quantity + sales = {1 => {:product_id => product.id, :price => 1.50}} + + post :new_contract, :profile => bsc.identifier, :sales => sales, :contract => {:bsc => bsc, :client_name => 'Marvin'} + + bsc.reload + contract = bsc.contracts.last + + assert_redirected_to :action => 'edit_contract', :contract_id => contract.id + end + + should 'search for products from the invoved enterprises' do + # product1 fits + # product2 doesn't fits because its in added_products + # product3 doesn't fits because its enterprise is in enterprises + enterprise1 = fast_create(Enterprise) + enterprise2 = fast_create(Enterprise) + enterprise3 = fast_create(Enterprise) + product1 = fast_create(Product, :enterprise_id => enterprise1.id, :name => 'Black Bycicle') + product2 = fast_create(Product, :enterprise_id => enterprise2.id, :name => 'Black Guitar') + product3 = fast_create(Product, :enterprise_id => enterprise3.id, :name => 'Black Notebook') + + get :search_sale_product, :profile => bsc.identifier, :enterprises => [enterprise1.id,enterprise2.id].join(','), :added_products => [product2.id].join(','),:sales => {1 => {:product_id => 'black'}} + + assert_match /#{product1.name}/, @response.body + assert_no_match /#{product2.name}/, @response.body + assert_no_match /#{product3.name}/, @response.body + end end -- libgit2 0.21.2