Commit c73a84e2851b15dd5209fa1b84ec849e716c8da7

Authored by Rodrigo Souto
1 parent f9156029

Free delivery price for shopping_cart

plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb
@@ -8,6 +8,7 @@ class ShoppingCartPluginMyprofileController < MyProfileController @@ -8,6 +8,7 @@ class ShoppingCartPluginMyprofileController < MyProfileController
8 params[:settings][:enabled] = params[:settings][:enabled] == '1' 8 params[:settings][:enabled] = params[:settings][:enabled] == '1'
9 params[:settings][:delivery] = params[:settings][:delivery] == '1' 9 params[:settings][:delivery] = params[:settings][:delivery] == '1'
10 params[:settings][:delivery_price] = params[:settings][:delivery_price].to_d 10 params[:settings][:delivery_price] = params[:settings][:delivery_price].to_d
  11 + params[:settings][:free_delivery_price] = params[:settings][:free_delivery_price].to_d
11 end 12 end
12 13
13 @settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin, params[:settings]) 14 @settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin, params[:settings])
plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb
@@ -85,6 +85,7 @@ class ShoppingCartPluginProfileController < ProfileController @@ -85,6 +85,7 @@ class ShoppingCartPluginProfileController < ProfileController
85 85
86 def buy 86 def buy
87 @environment = profile.environment 87 @environment = profile.environment
  88 + @settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin)
88 render :layout => false 89 render :layout => false
89 end 90 end
90 91
plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb
@@ -8,22 +8,26 @@ module ShoppingCartPlugin::CartHelper @@ -8,22 +8,26 @@ module ShoppingCartPlugin::CartHelper
8 end 8 end
9 9
10 def get_price(product, environment, quantity=1) 10 def get_price(product, environment, quantity=1)
11 - float_to_currency_cart(sell_price(product)*quantity, environment) 11 + float_to_currency_cart(price_with_quantity(product,quantity), environment)
12 end 12 end
13 13
14 - def get_total(items, environment)  
15 - float_to_currency_cart(items.map { |id, quantity| sell_price(Product.find(id)) * quantity}.sum, environment) 14 + def price_with_quantity(product, quantity=1)
  15 + quantity = 1 if !quantity.kind_of?(Numeric)
  16 + sell_price(product)*quantity
  17 + end
  18 +
  19 + def get_total(items)
  20 + items.map { |id, quantity| price_with_quantity(Product.find(id),quantity)}.sum
  21 + end
  22 +
  23 + def get_total_on_currency(items, environment)
  24 + float_to_currency_cart(get_total(items), environment)
16 end 25 end
17 26
18 def items_table(items, profile, by_mail = false) 27 def items_table(items, profile, by_mail = false)
19 environment = profile.environment 28 environment = profile.environment
20 settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin) 29 settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin)
21 items = items.to_a 30 items = items.to_a
22 - if settings.delivery  
23 - delivery = Product.new(:name => _('Delivery'), :price => settings.delivery_price)  
24 - delivery.save(false)  
25 - items << [delivery.id, 1]  
26 - end  
27 31
28 quantity_opts = { :class => 'cart-table-quantity' } 32 quantity_opts = { :class => 'cart-table-quantity' }
29 quantity_opts.merge!({:align => 'center'}) if by_mail 33 quantity_opts.merge!({:align => 'center'}) if by_mail
@@ -31,6 +35,16 @@ module ShoppingCartPlugin::CartHelper @@ -31,6 +35,16 @@ module ShoppingCartPlugin::CartHelper
31 price_opts.merge!({:align => 'right'}) if by_mail 35 price_opts.merge!({:align => 'right'}) if by_mail
32 items.sort! {|a, b| Product.find(a.first).name <=> Product.find(b.first).name} 36 items.sort! {|a, b| Product.find(a.first).name <=> Product.find(b.first).name}
33 37
  38 + if settings.delivery
  39 + if settings.free_delivery_price && get_total(items) >= settings.free_delivery_price
  40 + delivery = Product.new(:name => _('Free delivery'), :price => 0)
  41 + else
  42 + delivery = Product.new(:name => _('Delivery'), :price => settings.delivery_price)
  43 + end
  44 + delivery.save(false)
  45 + items << [delivery.id, '']
  46 + end
  47 +
34 table = '<table id="cart-items-table" cellpadding="2" cellspacing="0" 48 table = '<table id="cart-items-table" cellpadding="2" cellspacing="0"
35 border="'+(by_mail ? '1' : '0')+'" 49 border="'+(by_mail ? '1' : '0')+'"
36 style="'+(by_mail ? 'border-collapse:collapse' : '')+'">' + 50 style="'+(by_mail ? 'border-collapse:collapse' : '')+'">' +
@@ -48,7 +62,7 @@ module ShoppingCartPlugin::CartHelper @@ -48,7 +62,7 @@ module ShoppingCartPlugin::CartHelper
48 ) 62 )
49 end.join("\n") 63 end.join("\n")
50 64
51 - total = get_total(items, environment) 65 + total = get_total_on_currency(items, environment)
52 delivery.destroy if settings.delivery 66 delivery.destroy if settings.delivery
53 67
54 table + 68 table +
plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb
@@ -3,7 +3,12 @@ @@ -3,7 +3,12 @@
3 <% form_for(:settings, @settings, :url => {:action => 'edit'}, :html => {:method => 'post'}) do |f| %> 3 <% form_for(:settings, @settings, :url => {:action => 'edit'}, :html => {:method => 'post'}) do |f| %>
4 <%= labelled_form_field(_('Enabled?'), f.check_box(:enabled)) %> 4 <%= labelled_form_field(_('Enabled?'), f.check_box(:enabled)) %>
5 <%= labelled_form_field(_('Delivery?'), f.check_box(:delivery)) %> 5 <%= labelled_form_field(_('Delivery?'), f.check_box(:delivery)) %>
6 - <%= labelled_form_field(_('Delivery price:'), f.text_field(:delivery_price)) %> 6 + <% display_delivery_options = @settings.delivery ? 'auto' : 'none' %>
  7 + <fieldset id='delivery_options' style="display: <%= display_delivery_options %>"><legend><%=_('Delivery Options')%></legend>
  8 + <%= labelled_form_field(_('Price:'), f.text_field(:delivery_price)) %>
  9 + <%= labelled_form_field(_('Free delivery price:'), f.text_field(:free_delivery_price)) %>
  10 + <%= content_tag('small', _('Empty stands for no free delivery price.')) %>
  11 + </fieldset>
7 <br style='clear: both'/> 12 <br style='clear: both'/>
8 <br style='clear: both'/> 13 <br style='clear: both'/>
9 <div> 14 <div>
@@ -11,3 +16,9 @@ @@ -11,3 +16,9 @@
11 <%= button :back, _('Back to control panel'), :controller => 'profile_editor' %> 16 <%= button :back, _('Back to control panel'), :controller => 'profile_editor' %>
12 </div> 17 </div>
13 <% end%> 18 <% end%>
  19 +
  20 +<script>
  21 + jQuery('#settings_delivery').click(function(){
  22 + jQuery('#delivery_options').toggle('fast');
  23 + });
  24 +</script>
plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb
@@ -7,11 +7,13 @@ @@ -7,11 +7,13 @@
7 <%= labelled_form_field('* ' + _("Email"), f.text_field(:email, :class => 'required email') ) %> 7 <%= labelled_form_field('* ' + _("Email"), f.text_field(:email, :class => 'required email') ) %>
8 <%= labelled_form_field('* ' + _("Contact phone"), f.text_field(:contact_phone, :class => 'required') ) %> 8 <%= labelled_form_field('* ' + _("Contact phone"), f.text_field(:contact_phone, :class => 'required') ) %>
9 </div> 9 </div>
10 - <fieldset><legend><%=_('Delivery Address')%></legend>  
11 - <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %>  
12 - <%= labelled_form_field( _("City"), f.text_field(:city)) %>  
13 - <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %>  
14 - </fieldset> 10 + <% if @settings.delivery %>
  11 + <fieldset><legend><%=_('Delivery Address')%></legend>
  12 + <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %>
  13 + <%= labelled_form_field( _("City"), f.text_field(:city)) %>
  14 + <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %>
  15 + </fieldset>
  16 + <% end %>
15 <div id="cart-form-actions"> 17 <div id="cart-form-actions">
16 <%= submit_button(:send, _('Send buy request')) %> 18 <%= submit_button(:send, _('Send buy request')) %>
17 </div> 19 </div>