diff --git a/plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb b/plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb index 96ea956..60b9567 100644 --- a/plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb +++ b/plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb @@ -7,8 +7,8 @@ class ShoppingCartPluginMyprofileController < MyProfileController if params[:settings] params[:settings][:enabled] = params[:settings][:enabled] == '1' params[:settings][:delivery] = params[:settings][:delivery] == '1' - params[:settings][:delivery_price] = params[:settings][:delivery_price].to_d params[:settings][:free_delivery_price] = params[:settings][:free_delivery_price].to_d + params[:settings][:delivery_options] = treat_delivery_options(params[:settings][:delivery_options]) end @settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin, params[:settings]) @@ -54,4 +54,16 @@ class ShoppingCartPluginMyprofileController < MyProfileController order.save! redirect_to :action => 'reports', :from => params[:context_from], :to => params[:context_to], :filter_status => params[:context_status] end + + private + + def treat_delivery_options(params) + result = {} + params[:options].size.times do |counter| + if params[:options][counter].present? && params[:prices][counter].present? + result[params[:options][counter]] = params[:prices][counter] + end + end + result + end end diff --git a/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb b/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb index b8f94c1..7c69b90 100644 --- a/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb +++ b/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb @@ -92,8 +92,8 @@ class ShoppingCartPluginProfileController < ProfileController def send_request register_order(params[:customer], session[:cart][:items]) begin - ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, session[:cart][:items]) - ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, session[:cart][:items]) + ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, session[:cart][:items], params[:delivery_option]) + ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, session[:cart][:items], params[:delivery_option]) render :text => { :ok => true, :message => _('Request sent successfully. Check your email.'), @@ -152,6 +152,24 @@ class ShoppingCartPluginProfileController < ProfileController end end + def update_delivery_option + settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin) + delivery_price = settings.delivery_options[params[:delivery_option]] + delivery = Product.new(:name => params[:delivery_option], :price => delivery_price) + delivery.save(false) + items = session[:cart][:items].clone + items[delivery.id] = 1 + total_price = get_total_on_currency(items, environment) + delivery.destroy + render :text => { + :ok => true, + :delivery_price => float_to_currency_cart(delivery_price, environment), + :total_price => total_price, + :message => _('Delivery option updated.'), + :error => {:code => 0} + }.to_json + end + private def validate_same_enterprise diff --git a/plugins/shopping_cart/lib/shopping_cart_plugin.rb b/plugins/shopping_cart/lib/shopping_cart_plugin.rb index 2ddda29..4e5e1ca 100644 --- a/plugins/shopping_cart/lib/shopping_cart_plugin.rb +++ b/plugins/shopping_cart/lib/shopping_cart_plugin.rb @@ -3,24 +3,30 @@ require_dependency 'shopping_cart_plugin/ext/person' class ShoppingCartPlugin < Noosfero::Plugin - def self.plugin_name + class << self + def plugin_name "Shopping Basket" - end + end - def self.plugin_description - _("A shopping basket feature for enterprises") - end + def plugin_description + _("A shopping basket feature for enterprises") + end - def self.enabled_default_setting - true - end + def enabled_default_setting + true + end - def self.delivery_default_setting - false - end + def delivery_default_setting + false + end - def self.delivery_price_default_setting - 0 + def delivery_price_default_setting + 0 + end + + def delivery_options_default_setting + {} + end end def add_to_cart_button(item, enterprise = context.profile) diff --git a/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb b/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb index aacbf8b..fa8ada7 100644 --- a/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb +++ b/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb @@ -24,7 +24,7 @@ module ShoppingCartPlugin::CartHelper float_to_currency_cart(get_total(items), environment) end - def items_table(items, profile, by_mail = false) + def items_table(items, profile, delivery_option = nil, by_mail = false) environment = profile.environment settings = Noosfero::Plugin::Settings.new(profile, ShoppingCartPlugin) items = items.to_a @@ -39,7 +39,7 @@ module ShoppingCartPlugin::CartHelper if settings.free_delivery_price && get_total(items) >= settings.free_delivery_price delivery = Product.new(:name => _('Free delivery'), :price => 0) else - delivery = Product.new(:name => _('Delivery'), :price => settings.delivery_price) + delivery = Product.new(:name => delivery_option, :price => settings.delivery_options[delivery_option]) end delivery.save(false) items << [delivery.id, ''] @@ -49,17 +49,23 @@ module ShoppingCartPlugin::CartHelper border="'+(by_mail ? '1' : '0')+'" style="'+(by_mail ? 'border-collapse:collapse' : '')+'">' + content_tag('tr', - content_tag('th', _('Item name')) + - content_tag('th', by_mail ? ' # ' : '#') + - content_tag('th', _('Price')) + content_tag('th', _('Item name')) + + content_tag('th', by_mail ? ' # ' : '#') + + content_tag('th', _('Price')) ) + items.map do |id, quantity| product = Product.find(id) + name_opts = {} + is_delivery = quantity.kind_of?(String) + if is_delivery + price_opts.merge!({:id => 'delivery-price'}) + name_opts.merge!({:id => 'delivery-name'}) + end content_tag('tr', - content_tag('td', product.name) + - content_tag('td', quantity, quantity_opts ) + - content_tag('td', get_price(product, environment, quantity), price_opts ) - ) + content_tag('td', product.name, name_opts) + + content_tag('td', quantity, quantity_opts ) + + content_tag('td', get_price(product, environment, quantity), price_opts) + ) end.join("\n") total = get_total_on_currency(items, environment) @@ -72,6 +78,12 @@ module ShoppingCartPlugin::CartHelper end def float_to_currency_cart(value, environment) - number_to_currency(value, :unit => environment.currency_unit, :separator => environment.currency_separator, :delimiter => environment.currency_delimiter, :precision => 2, :format => "%u %n") + number_to_currency(value, :unit => environment.currency_unit, :separator => environment.currency_separator, :delimiter => environment.currency_delimiter, :precision => 2, :format => "%u%n") + end + + def select_delivery_options(options, environment) + options.map do |option, price| + ["#{option} (#{float_to_currency_cart(price, environment)})", option] + end end end diff --git a/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb b/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb index 6a064f6..d89ec1a 100644 --- a/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb +++ b/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb @@ -1,6 +1,6 @@ class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase - def customer_notification(customer, supplier, items) + def customer_notification(customer, supplier, items, delivery_option) domain = supplier.hostname || supplier.environment.default_hostname recipients customer[:email] from 'no-reply@' + domain @@ -10,10 +10,11 @@ class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase body :customer => customer, :supplier => supplier, :items => items, - :environment => supplier.environment + :environment => supplier.environment, + :delivery_option => delivery_option end - def supplier_notification(customer, supplier, items) + def supplier_notification(customer, supplier, items, delivery_option) domain = supplier.environment.default_hostname recipients supplier.contact_email from 'no-reply@' + domain @@ -23,6 +24,7 @@ class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase body :customer => customer, :supplier => supplier, :items => items, - :environment => supplier.environment + :environment => supplier.environment, + :delivery_option => delivery_option end end diff --git a/plugins/shopping_cart/public/buy.js b/plugins/shopping_cart/public/buy.js new file mode 100644 index 0000000..3fca959 --- /dev/null +++ b/plugins/shopping_cart/public/buy.js @@ -0,0 +1,30 @@ +jQuery(document).ready(function(){ + jQuery("#cart-request-form").validate({ + submitHandler: function(form) { + jQuery(form).find('input.submit').attr('disabled', true); + jQuery('#cboxLoadingOverlay').show().addClass('loading'); + jQuery('#cboxLoadingGraphic').show().addClass('loading'); + } + }); +}); + +jQuery('#delivery_option').change(function(){ + jQuery('#cboxLoadingGraphic').show(); + me = this; + enterprise = jQuery(me).attr('data-profile-identifier'); + option = jQuery(me).val(); + jQuery.ajax({ + url: '/profile/'+ enterprise +'/plugin/shopping_cart/update_delivery_option', + dataType: "json", + data: 'delivery_option='+option, + success: function(data, st, ajax) { + jQuery('#delivery-price').text(data.delivery_price); + jQuery('.cart-table-total-value').text(data.total_price); + jQuery('#delivery-name').text(option); + jQuery('#cboxLoadingGraphic').hide(); + }, + error: function(ajax, st, errorThrown) { + alert('Update delivery option - HTTP '+st+': '+errorThrown); + }, + }); +}); diff --git a/plugins/shopping_cart/public/edit.js b/plugins/shopping_cart/public/edit.js new file mode 100644 index 0000000..2d051ad --- /dev/null +++ b/plugins/shopping_cart/public/edit.js @@ -0,0 +1,16 @@ +jQuery('#settings_delivery').click(function(){ + jQuery('#delivery_settings').toggle('fast'); +}); + +jQuery('#add-new-option').click(function(){ + new_option = jQuery('#empty-option').clone(); + new_option.removeAttr('id'); + jQuery('#add-new-option-row').before(new_option); + new_option.show(); + return false; +}); + +jQuery('.remove-option').live('click', function(){ + jQuery(this).closest('tr').remove(); + return false; +}); diff --git a/plugins/shopping_cart/views/shopping_cart_plugin/mailer/customer_notification.html.erb b/plugins/shopping_cart/views/shopping_cart_plugin/mailer/customer_notification.html.erb index 6afb388..9f96d63 100644 --- a/plugins/shopping_cart/views/shopping_cart_plugin/mailer/customer_notification.html.erb +++ b/plugins/shopping_cart/views/shopping_cart_plugin/mailer/customer_notification.html.erb @@ -35,7 +35,7 @@
<%=_('Here are the products you bought:')%>
- <%= items_table(@items, @supplier, true) %> + <%= items_table(@items, @supplier, @delivery_option, true) %>
--
diff --git a/plugins/shopping_cart/views/shopping_cart_plugin/mailer/supplier_notification.html.erb b/plugins/shopping_cart/views/shopping_cart_plugin/mailer/supplier_notification.html.erb
index 7f791ae..61d5e2a 100644
--- a/plugins/shopping_cart/views/shopping_cart_plugin/mailer/supplier_notification.html.erb
+++ b/plugins/shopping_cart/views/shopping_cart_plugin/mailer/supplier_notification.html.erb
@@ -33,7 +33,7 @@
<%=_('And here are the items bought by this customer:')%>
- <%= items_table(@items, @supplier, true) %> + <%= items_table(@items, @supplier, @delivery_option, true) %>
--
diff --git a/plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb b/plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb
index 6b409dc..c65ef9a 100644
--- a/plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb
+++ b/plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb
@@ -3,9 +3,36 @@
<% form_for(:settings, @settings, :url => {:action => 'edit'}, :html => {:method => 'post'}) do |f| %>
<%= labelled_form_field(_('Enabled?'), f.check_box(:enabled)) %>
<%= labelled_form_field(_('Delivery?'), f.check_box(:delivery)) %>
- <% display_delivery_options = @settings.delivery ? 'auto' : 'none' %>
-