Commit a2369d8964da1c06beaab3c9c344cc41b4ab8707

Authored by Rodrigo Souto
1 parent c73a84e2

Adding delivery options for shopping cart

plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb
... ... @@ -7,8 +7,8 @@ class ShoppingCartPluginMyprofileController < MyProfileController
7 7 if params[:settings]
8 8 params[:settings][:enabled] = params[:settings][:enabled] == '1'
9 9 params[:settings][:delivery] = params[:settings][:delivery] == '1'
10   - params[:settings][:delivery_price] = params[:settings][:delivery_price].to_d
11 10 params[:settings][:free_delivery_price] = params[:settings][:free_delivery_price].to_d
  11 + params[:settings][:delivery_options] = treat_delivery_options(params[:settings][:delivery_options])
12 12 end
13 13  
14 14 @settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin, params[:settings])
... ... @@ -54,4 +54,16 @@ class ShoppingCartPluginMyprofileController < MyProfileController
54 54 order.save!
55 55 redirect_to :action => 'reports', :from => params[:context_from], :to => params[:context_to], :filter_status => params[:context_status]
56 56 end
  57 +
  58 + private
  59 +
  60 + def treat_delivery_options(params)
  61 + result = {}
  62 + params[:options].size.times do |counter|
  63 + if params[:options][counter].present? && params[:prices][counter].present?
  64 + result[params[:options][counter]] = params[:prices][counter]
  65 + end
  66 + end
  67 + result
  68 + end
57 69 end
... ...
plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb
... ... @@ -92,8 +92,8 @@ class ShoppingCartPluginProfileController < ProfileController
92 92 def send_request
93 93 register_order(params[:customer], session[:cart][:items])
94 94 begin
95   - ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, session[:cart][:items])
96   - ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, session[:cart][:items])
  95 + ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, session[:cart][:items], params[:delivery_option])
  96 + ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, session[:cart][:items], params[:delivery_option])
97 97 render :text => {
98 98 :ok => true,
99 99 :message => _('Request sent successfully. Check your email.'),
... ... @@ -152,6 +152,24 @@ class ShoppingCartPluginProfileController < ProfileController
152 152 end
153 153 end
154 154  
  155 + def update_delivery_option
  156 + settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin)
  157 + delivery_price = settings.delivery_options[params[:delivery_option]]
  158 + delivery = Product.new(:name => params[:delivery_option], :price => delivery_price)
  159 + delivery.save(false)
  160 + items = session[:cart][:items].clone
  161 + items[delivery.id] = 1
  162 + total_price = get_total_on_currency(items, environment)
  163 + delivery.destroy
  164 + render :text => {
  165 + :ok => true,
  166 + :delivery_price => float_to_currency_cart(delivery_price, environment),
  167 + :total_price => total_price,
  168 + :message => _('Delivery option updated.'),
  169 + :error => {:code => 0}
  170 + }.to_json
  171 + end
  172 +
155 173 private
156 174  
157 175 def validate_same_enterprise
... ...
plugins/shopping_cart/lib/shopping_cart_plugin.rb
... ... @@ -3,24 +3,30 @@ require_dependency 'shopping_cart_plugin/ext/person'
3 3  
4 4 class ShoppingCartPlugin < Noosfero::Plugin
5 5  
6   - def self.plugin_name
  6 + class << self
  7 + def plugin_name
7 8 "Shopping Basket"
8   - end
  9 + end
9 10  
10   - def self.plugin_description
11   - _("A shopping basket feature for enterprises")
12   - end
  11 + def plugin_description
  12 + _("A shopping basket feature for enterprises")
  13 + end
13 14  
14   - def self.enabled_default_setting
15   - true
16   - end
  15 + def enabled_default_setting
  16 + true
  17 + end
17 18  
18   - def self.delivery_default_setting
19   - false
20   - end
  19 + def delivery_default_setting
  20 + false
  21 + end
21 22  
22   - def self.delivery_price_default_setting
23   - 0
  23 + def delivery_price_default_setting
  24 + 0
  25 + end
  26 +
  27 + def delivery_options_default_setting
  28 + {}
  29 + end
24 30 end
25 31  
26 32 def add_to_cart_button(item, enterprise = context.profile)
... ...
plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb
... ... @@ -24,7 +24,7 @@ module ShoppingCartPlugin::CartHelper
24 24 float_to_currency_cart(get_total(items), environment)
25 25 end
26 26  
27   - def items_table(items, profile, by_mail = false)
  27 + def items_table(items, profile, delivery_option = nil, by_mail = false)
28 28 environment = profile.environment
29 29 settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin)
30 30 items = items.to_a
... ... @@ -39,7 +39,7 @@ module ShoppingCartPlugin::CartHelper
39 39 if settings.free_delivery_price && get_total(items) >= settings.free_delivery_price
40 40 delivery = Product.new(:name => _('Free delivery'), :price => 0)
41 41 else
42   - delivery = Product.new(:name => _('Delivery'), :price => settings.delivery_price)
  42 + delivery = Product.new(:name => delivery_option, :price => settings.delivery_options[delivery_option])
43 43 end
44 44 delivery.save(false)
45 45 items << [delivery.id, '']
... ... @@ -49,17 +49,23 @@ module ShoppingCartPlugin::CartHelper
49 49 border="'+(by_mail ? '1' : '0')+'"
50 50 style="'+(by_mail ? 'border-collapse:collapse' : '')+'">' +
51 51 content_tag('tr',
52   - content_tag('th', _('Item name')) +
53   - content_tag('th', by_mail ? '&nbsp;#&nbsp;' : '#') +
54   - content_tag('th', _('Price'))
  52 + content_tag('th', _('Item name')) +
  53 + content_tag('th', by_mail ? '&nbsp;#&nbsp;' : '#') +
  54 + content_tag('th', _('Price'))
55 55 ) +
56 56 items.map do |id, quantity|
57 57 product = Product.find(id)
  58 + name_opts = {}
  59 + is_delivery = quantity.kind_of?(String)
  60 + if is_delivery
  61 + price_opts.merge!({:id => 'delivery-price'})
  62 + name_opts.merge!({:id => 'delivery-name'})
  63 + end
58 64 content_tag('tr',
59   - content_tag('td', product.name) +
60   - content_tag('td', quantity, quantity_opts ) +
61   - content_tag('td', get_price(product, environment, quantity), price_opts )
62   - )
  65 + content_tag('td', product.name, name_opts) +
  66 + content_tag('td', quantity, quantity_opts ) +
  67 + content_tag('td', get_price(product, environment, quantity), price_opts)
  68 + )
63 69 end.join("\n")
64 70  
65 71 total = get_total_on_currency(items, environment)
... ... @@ -72,6 +78,12 @@ module ShoppingCartPlugin::CartHelper
72 78 end
73 79  
74 80 def float_to_currency_cart(value, environment)
75   - number_to_currency(value, :unit => environment.currency_unit, :separator => environment.currency_separator, :delimiter => environment.currency_delimiter, :precision => 2, :format => "%u %n")
  81 + number_to_currency(value, :unit => environment.currency_unit, :separator => environment.currency_separator, :delimiter => environment.currency_delimiter, :precision => 2, :format => "%u%n")
  82 + end
  83 +
  84 + def select_delivery_options(options, environment)
  85 + options.map do |option, price|
  86 + ["#{option} (#{float_to_currency_cart(price, environment)})", option]
  87 + end
76 88 end
77 89 end
... ...
plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb
1 1 class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase
2 2  
3   - def customer_notification(customer, supplier, items)
  3 + def customer_notification(customer, supplier, items, delivery_option)
4 4 domain = supplier.hostname || supplier.environment.default_hostname
5 5 recipients customer[:email]
6 6 from 'no-reply@' + domain
... ... @@ -10,10 +10,11 @@ class ShoppingCartPlugin::Mailer &lt; Noosfero::Plugin::MailerBase
10 10 body :customer => customer,
11 11 :supplier => supplier,
12 12 :items => items,
13   - :environment => supplier.environment
  13 + :environment => supplier.environment,
  14 + :delivery_option => delivery_option
14 15 end
15 16  
16   - def supplier_notification(customer, supplier, items)
  17 + def supplier_notification(customer, supplier, items, delivery_option)
17 18 domain = supplier.environment.default_hostname
18 19 recipients supplier.contact_email
19 20 from 'no-reply@' + domain
... ... @@ -23,6 +24,7 @@ class ShoppingCartPlugin::Mailer &lt; Noosfero::Plugin::MailerBase
23 24 body :customer => customer,
24 25 :supplier => supplier,
25 26 :items => items,
26   - :environment => supplier.environment
  27 + :environment => supplier.environment,
  28 + :delivery_option => delivery_option
27 29 end
28 30 end
... ...
plugins/shopping_cart/public/buy.js 0 → 100644
... ... @@ -0,0 +1,30 @@
  1 +jQuery(document).ready(function(){
  2 + jQuery("#cart-request-form").validate({
  3 + submitHandler: function(form) {
  4 + jQuery(form).find('input.submit').attr('disabled', true);
  5 + jQuery('#cboxLoadingOverlay').show().addClass('loading');
  6 + jQuery('#cboxLoadingGraphic').show().addClass('loading');
  7 + }
  8 + });
  9 +});
  10 +
  11 +jQuery('#delivery_option').change(function(){
  12 + jQuery('#cboxLoadingGraphic').show();
  13 + me = this;
  14 + enterprise = jQuery(me).attr('data-profile-identifier');
  15 + option = jQuery(me).val();
  16 + jQuery.ajax({
  17 + url: '/profile/'+ enterprise +'/plugin/shopping_cart/update_delivery_option',
  18 + dataType: "json",
  19 + data: 'delivery_option='+option,
  20 + success: function(data, st, ajax) {
  21 + jQuery('#delivery-price').text(data.delivery_price);
  22 + jQuery('.cart-table-total-value').text(data.total_price);
  23 + jQuery('#delivery-name').text(option);
  24 + jQuery('#cboxLoadingGraphic').hide();
  25 + },
  26 + error: function(ajax, st, errorThrown) {
  27 + alert('Update delivery option - HTTP '+st+': '+errorThrown);
  28 + },
  29 + });
  30 +});
... ...
plugins/shopping_cart/public/edit.js 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +jQuery('#settings_delivery').click(function(){
  2 + jQuery('#delivery_settings').toggle('fast');
  3 +});
  4 +
  5 +jQuery('#add-new-option').click(function(){
  6 + new_option = jQuery('#empty-option').clone();
  7 + new_option.removeAttr('id');
  8 + jQuery('#add-new-option-row').before(new_option);
  9 + new_option.show();
  10 + return false;
  11 +});
  12 +
  13 +jQuery('.remove-option').live('click', function(){
  14 + jQuery(this).closest('tr').remove();
  15 + return false;
  16 +});
... ...
plugins/shopping_cart/views/shopping_cart_plugin/mailer/customer_notification.html.erb
... ... @@ -35,7 +35,7 @@
35 35 </ul>
36 36  
37 37 <p><%=_('Here are the products you bought:')%></p>
38   - <%= items_table(@items, @supplier, true) %>
  38 + <%= items_table(@items, @supplier, @delivery_option, true) %>
39 39  
40 40 <p>
41 41 --<br/>
... ...
plugins/shopping_cart/views/shopping_cart_plugin/mailer/supplier_notification.html.erb
... ... @@ -33,7 +33,7 @@
33 33 </ul>
34 34  
35 35 <p><%=_('And here are the items bought by this customer:')%></p>
36   - <%= items_table(@items, @supplier, true) %>
  36 + <%= items_table(@items, @supplier, @delivery_option, true) %>
37 37  
38 38 <p>
39 39 --<br/>
... ...
plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb
... ... @@ -3,9 +3,36 @@
3 3 <% form_for(:settings, @settings, :url => {:action => 'edit'}, :html => {:method => 'post'}) do |f| %>
4 4 <%= labelled_form_field(_('Enabled?'), f.check_box(:enabled)) %>
5 5 <%= labelled_form_field(_('Delivery?'), f.check_box(:delivery)) %>
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)) %>
  6 + <% display_delivery_settings = @settings.delivery ? 'auto' : 'none' %>
  7 + <fieldset id='delivery_settings' style="display: <%= display_delivery_settings %>"><legend><%=_('Delivery')%></legend>
  8 + <table>
  9 + <tr>
  10 + <th><%= _('Option') %></th>
  11 + <th><%= _('Price') %></th>
  12 + <th>&nbsp;</th>
  13 + </tr>
  14 + <% @settings.delivery_options.each do |option, price| %>
  15 + <tr>
  16 + <td><%= text_field_tag('settings[delivery_options][options][]', option, :style => 'width: 100%') %></td>
  17 + <td><%= text_field_tag('settings[delivery_options][prices][]', price, :style => 'width: 100%') %></td>
  18 + <td><%= button_without_text(:close, _('Remove option'), '', :class => 'remove-option') %></td>
  19 + </tr>
  20 + <% end %>
  21 + <tr>
  22 + <td><%= text_field_tag('settings[delivery_options][options][]', nil, :style => 'width: 100%') %></td>
  23 + <td><%= text_field_tag('settings[delivery_options][prices][]', nil, :style => 'width: 100%') %></td>
  24 + <td><%= button_without_text(:close, _('Remove option'), '', :class => 'remove-option') %></td>
  25 + </tr>
  26 + <tr id='add-new-option-row'>
  27 + <td colspan='3' style='background-color: #EEE; text-align: center'><%= link_to(_('ADD NEW OPTION'), '', :id => 'add-new-option') %></td>
  28 + </tr>
  29 + <tr id="empty-option" style='display: none'>
  30 + <td><%= text_field_tag('settings[delivery_options][options][]', nil, :style => 'width: 100%') %></td>
  31 + <td><%= text_field_tag('settings[delivery_options][prices][]', nil, :style => 'width: 100%') %></td>
  32 + <td><%= button_without_text(:close, _('Remove option'), '', :class => 'remove-option') %></td>
  33 + </tr>
  34 + </table>
  35 +
9 36 <%= labelled_form_field(_('Free delivery price:'), f.text_field(:free_delivery_price)) %>
10 37 <%= content_tag('small', _('Empty stands for no free delivery price.')) %>
11 38 </fieldset>
... ... @@ -17,8 +44,4 @@
17 44 </div>
18 45 <% end%>
19 46  
20   -<script>
21   - jQuery('#settings_delivery').click(function(){
22   - jQuery('#delivery_options').toggle('fast');
23   - });
24   -</script>
  47 +<%= javascript_include_tag '../plugins/shopping_cart/edit' %>
... ...
plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb
... ... @@ -6,6 +6,7 @@
6 6 <%= labelled_form_field('* ' + _("Name"), f.text_field(:name, :class => 'required') ) %>
7 7 <%= labelled_form_field('* ' + _("Email"), f.text_field(:email, :class => 'required email') ) %>
8 8 <%= labelled_form_field('* ' + _("Contact phone"), f.text_field(:contact_phone, :class => 'required') ) %>
  9 + <%= labelled_form_field(_('Delivery option'), select_tag(:delivery_option, options_for_select(select_delivery_options(@settings.delivery_options, environment)), 'data-profile-identifier' => profile.identifier)) if @settings.delivery && @settings.free_delivery_price && get_total(session[:cart][:items]) < @settings.free_delivery_price %>
9 10 </div>
10 11 <% if @settings.delivery %>
11 12 <fieldset><legend><%=_('Delivery Address')%></legend>
... ... @@ -18,20 +19,8 @@
18 19 <%= submit_button(:send, _('Send buy request')) %>
19 20 </div>
20 21 <% end %>
21   - <%= items_table(session[:cart][:items], profile) %>
  22 + <%= items_table(session[:cart][:items], profile, @settings.delivery_options.first.first) %>
22 23 <%= link_to '', '#', :onclick => "Cart.colorbox_close(this);", :class => 'cart-box-close icon-cancel' %>
23 24 </div>
24 25  
25   -<script type="text/javascript">
26   -//<![CDATA[
27   - jQuery(document).ready(function(){
28   - jQuery("#cart-request-form").validate({
29   - submitHandler: function(form) {
30   - jQuery(form).find('input.submit').attr('disabled', true);
31   - jQuery('#cboxLoadingOverlay').show().addClass('loading');
32   - jQuery('#cboxLoadingGraphic').show().addClass('loading');
33   - }
34   - });
35   - });
36   -//]]>
37   -</script>
  26 +<%= javascript_include_tag '../plugins/shopping_cart/buy' %>
... ...