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 55842fd..617eda0 100644 --- a/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb +++ b/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb @@ -1,4 +1,4 @@ -include ActionView::Helpers::NumberHelper +include ShoppingCartPlugin::CartHelper class ShoppingCartPluginProfileController < ProfileController append_view_path File.join(File.dirname(__FILE__) + '/../views') @@ -88,19 +88,20 @@ class ShoppingCartPluginProfileController < ProfileController def send_request begin - #implement send email here + ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, session[:cart][:items]) + ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, session[:cart][:items]) render :text => { :ok => true, :message => _('Request sent successfully. Check your email.'), :error => {:code => 0} }.to_json - rescue + rescue Exception => exception render :text => { :ok => false, :message => _('Your request failed.'), :error => { :code => 6, - :message => _('Send request failed.') + :message => exception } }.to_json end @@ -180,10 +181,5 @@ class ShoppingCartPluginProfileController < ProfileController true end - def get_price(product) - float_to_currency( product.discount ? product.price_with_discount : product.price ) - end - - end diff --git a/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb b/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb new file mode 100644 index 0000000..2f759be --- /dev/null +++ b/plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb @@ -0,0 +1,44 @@ +module ShoppingCartPlugin::CartHelper + + include ActionView::Helpers::NumberHelper + + def sell_price(product) + return 0 if product.price.nil? + product.discount ? product.price_with_discount : product.price + end + + def get_price(product) + float_to_currency(sell_price(product)) + end + + def get_total(items) + float_to_currency(items.map { |id, quantity| sell_price(Product.find(id)) * quantity}.sum) + end + + def items_table(items, by_mail = false) + '' + + content_tag('tr', + content_tag('th', _('Item name')) + + content_tag('th', by_mail ? ' # ' : '#') + + content_tag('th', _('Price')) + ) + + items.map do |id, quantity| + product = Product.find(id) + quantity_opts = { :class => 'cart-table-quantity' } + quantity_opts.merge!({:align => 'center'}) if by_mail + price_opts = {:class => 'cart-table-price'} + price_opts.merge!({:align => 'right'}) if by_mail + content_tag('tr', + content_tag('td', product.name) + + content_tag('td', quantity, quantity_opts ) + + content_tag('td', get_price(product), price_opts ) + ) + end.join("\n") + + content_tag('th', _('Total:'), :colspan => 2, :class => 'cart-table-total-label') + + content_tag('th', get_total(items), :class => 'cart-table-total-value') + + '
' + end + +end diff --git a/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb b/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb new file mode 100644 index 0000000..140f6c7 --- /dev/null +++ b/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb @@ -0,0 +1,28 @@ +include ApplicationHelper + +class ShoppingCartPlugin::Mailer < ActionMailer::Base + + prepend_view_path(ShoppingCartPlugin.root_path+'/views') + + def customer_notification(customer, supplier, items) + recipients customer[:email] + from supplier.contact_email + subject _("[%s] Your buy request was performed successfully.") % supplier[:name] + content_type 'text/html' + body :customer => customer, + :supplier => supplier, + :items => items, + :environment => supplier.environment + end + + def supplier_notification(customer, supplier, items) + recipients supplier.contact_email + from customer[:email] + subject _("[%s] You have a new buy request from %s.") % [supplier.environment.name, customer[:name]] + content_type 'text/html' + body :customer => customer, + :supplier => supplier, + :items => items, + :environment => supplier.environment + end +end diff --git a/plugins/shopping_cart/public/cart.js b/plugins/shopping_cart/public/cart.js index 91828bb..0fd02ec 100644 --- a/plugins/shopping_cart/public/cart.js +++ b/plugins/shopping_cart/public/cart.js @@ -241,25 +241,17 @@ function Cart(config) { }); } - Cart.send_request = function(button) { - var params = {}; - params['name'] = $('#name', button.parentNode).val(); - params['email'] = $('#email', button.parentNode).val(); - params['contact_phone'] = $('#contact_phone', button.parentNode).val(); - params['address'] = $('#address', button.parentNode).val(); - Cart.instance.send_request(params); + Cart.send_request = function(form) { + Cart.instance.send_request($(form).serialize()); + return false; } - Cart.prototype.send_request = function(p) { - params = "?"; - for( var attribute in p ) { - var value = p[attribute]; - params += attribute+'='+value+'&'; - } - params = params.substring(0, params.length-1); + Cart.prototype.send_request = function(params) { var me = this; $.ajax({ - url: '/profile/'+ me.enterprise +'/plugins/shopping_cart/send_request'+params, + type: 'POST', + url: '/profile/'+ me.enterprise +'/plugins/shopping_cart/send_request', + data: params, dataType: 'json', success: function(data, status, ajax){ if ( !data.ok ) display_notice(data.error.message); 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 new file mode 100644 index 0000000..73d3419 --- /dev/null +++ b/plugins/shopping_cart/views/shopping_cart_plugin/mailer/customer_notification.html.erb @@ -0,0 +1,24 @@ +<% environment = @environment %> +

<%= _('Hi %s!') % @customer[:name] %>

+

+<%= _('This is a notification e-mail about your buy request on %s.') % link_to(@supplier.name, @supplier.url) %> +<%= _('The supplier already received your buy request and may contact you for confirmation.') %> +<%= _('Please check if your information below is correct:') %> +

+ +

<%=_('Here are the products you bought:')%>

+<%= items_table(@items, true) %> +

+<%=_('Thanks for buying with us!')%> +
+<%= link_to @supplier.name, @supplier.url %> +

+<%= _('A service of %s.') % environment.name %> 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 new file mode 100644 index 0000000..e76b167 --- /dev/null +++ b/plugins/shopping_cart/views/shopping_cart_plugin/mailer/supplier_notification.html.erb @@ -0,0 +1,22 @@ +<% environment = @environment %> +

<%= _('Hi %s!') % @supplier.name %>

+

+<%= _('This is a buy request made by %s.') % @customer[:name] %> +<%= _('Below follows the customer informations:') %> +

+ +

<%=_('And here are the items bought by this customer:')%>

+<%= items_table(@items, true) %> +

+<%=_('If there are any problems with this email contact the admin of %s.') % environment.name %> +

+<%= _('A service of %s.') % environment.name %> diff --git a/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb b/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb index 1d906fc..8024ba6 100644 --- a/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb +++ b/plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb @@ -1,9 +1,24 @@ <% person = user.nil? ? Person.new : user %> -<% form_tag :action => 'send_request' do %> - <%= labelled_form_field( _("Full name"), text_field_tag(:name, person.name) ) %> - <%= labelled_form_field( _("Email"), text_field_tag(:email, person.email) ) %> - <%= labelled_form_field( _("Phone number"), text_field_tag(:contact_phone, person.contact_phone) ) %> - <%= labelled_form_field( _("Delivery address"), text_field_tag(:address, person.address) ) %> -

- -<% end %> +
+ <% form_for(:customer, person, :url => {:action => 'send_request'}, + :html => {:onsubmit => "return Cart.send_request(this)", :id => 'cart-request-form' }) do |f| %> +
+ <%= labelled_form_field( _("Full name"), f.text_field(:name) ) %> + <%= labelled_form_field( _("Email"), f.text_field(:email) ) %> + <%= labelled_form_field( _("Phone number"), f.text_field(:contact_phone) ) %> +
+
<%=_('Delivery Address')%> + <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %> + <%= labelled_form_field( _("City"), f.text_field(:city)) %> + <%= labelled_form_field( _("State"), f.text_field(:state)) %> + <%= select_country(_('Country'), :customer, :country, {:class => 'type-select'}, {:selected => person.country}) %> + <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %> +
+
+ <%= tag 'input', :class => 'button with-text icon-send', + :value => _('Send buy request'), + :type => 'submit'%> +
+ <% end %> + <%= items_table(session[:cart][:items]) %> +
-- libgit2 0.21.2