Commit 18549df8840c8be785e26d2d8f2939047398fdc6

Authored by Rodrigo Souto
1 parent 30acff8d

Email request

* Features:
    - Plugin helper.
    - Plugin mailer.
    - Template for supplier and customer email.
    - Upgrades to form.
  * Fixes:
    - Passing request form through ajax serialize instead of hardcoded.
plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb
1 -include ActionView::Helpers::NumberHelper 1 +include ShoppingCartPlugin::CartHelper
2 2
3 class ShoppingCartPluginProfileController < ProfileController 3 class ShoppingCartPluginProfileController < ProfileController
4 append_view_path File.join(File.dirname(__FILE__) + '/../views') 4 append_view_path File.join(File.dirname(__FILE__) + '/../views')
@@ -88,19 +88,20 @@ class ShoppingCartPluginProfileController &lt; ProfileController @@ -88,19 +88,20 @@ class ShoppingCartPluginProfileController &lt; ProfileController
88 88
89 def send_request 89 def send_request
90 begin 90 begin
91 - #implement send email here 91 + ShoppingCartPlugin::Mailer.deliver_customer_notification(params[:customer], profile, session[:cart][:items])
  92 + ShoppingCartPlugin::Mailer.deliver_supplier_notification(params[:customer], profile, session[:cart][:items])
92 render :text => { 93 render :text => {
93 :ok => true, 94 :ok => true,
94 :message => _('Request sent successfully. Check your email.'), 95 :message => _('Request sent successfully. Check your email.'),
95 :error => {:code => 0} 96 :error => {:code => 0}
96 }.to_json 97 }.to_json
97 - rescue 98 + rescue Exception => exception
98 render :text => { 99 render :text => {
99 :ok => false, 100 :ok => false,
100 :message => _('Your request failed.'), 101 :message => _('Your request failed.'),
101 :error => { 102 :error => {
102 :code => 6, 103 :code => 6,
103 - :message => _('Send request failed.') 104 + :message => exception
104 } 105 }
105 }.to_json 106 }.to_json
106 end 107 end
@@ -180,10 +181,5 @@ class ShoppingCartPluginProfileController &lt; ProfileController @@ -180,10 +181,5 @@ class ShoppingCartPluginProfileController &lt; ProfileController
180 true 181 true
181 end 182 end
182 183
183 - def get_price(product)  
184 - float_to_currency( product.discount ? product.price_with_discount : product.price )  
185 - end  
186 -  
187 -  
188 end 184 end
189 185
plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb 0 → 100644
@@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
  1 +module ShoppingCartPlugin::CartHelper
  2 +
  3 + include ActionView::Helpers::NumberHelper
  4 +
  5 + def sell_price(product)
  6 + return 0 if product.price.nil?
  7 + product.discount ? product.price_with_discount : product.price
  8 + end
  9 +
  10 + def get_price(product)
  11 + float_to_currency(sell_price(product))
  12 + end
  13 +
  14 + def get_total(items)
  15 + float_to_currency(items.map { |id, quantity| sell_price(Product.find(id)) * quantity}.sum)
  16 + end
  17 +
  18 + def items_table(items, by_mail = false)
  19 + '<table id="cart-items-table" cellpadding="2" cellspacing="0"
  20 + border="'+(by_mail ? '1' : '0')+'"
  21 + style="'+(by_mail ? 'border-collapse:collapse' : '')+'">' +
  22 + content_tag('tr',
  23 + content_tag('th', _('Item name')) +
  24 + content_tag('th', by_mail ? '&nbsp;#&nbsp;' : '#') +
  25 + content_tag('th', _('Price'))
  26 + ) +
  27 + items.map do |id, quantity|
  28 + product = Product.find(id)
  29 + quantity_opts = { :class => 'cart-table-quantity' }
  30 + quantity_opts.merge!({:align => 'center'}) if by_mail
  31 + price_opts = {:class => 'cart-table-price'}
  32 + price_opts.merge!({:align => 'right'}) if by_mail
  33 + content_tag('tr',
  34 + content_tag('td', product.name) +
  35 + content_tag('td', quantity, quantity_opts ) +
  36 + content_tag('td', get_price(product), price_opts )
  37 + )
  38 + end.join("\n") +
  39 + content_tag('th', _('Total:'), :colspan => 2, :class => 'cart-table-total-label') +
  40 + content_tag('th', get_total(items), :class => 'cart-table-total-value') +
  41 + '</table>'
  42 + end
  43 +
  44 +end
plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb 0 → 100644
@@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
  1 +include ApplicationHelper
  2 +
  3 +class ShoppingCartPlugin::Mailer < ActionMailer::Base
  4 +
  5 + prepend_view_path(ShoppingCartPlugin.root_path+'/views')
  6 +
  7 + def customer_notification(customer, supplier, items)
  8 + recipients customer[:email]
  9 + from supplier.contact_email
  10 + subject _("[%s] Your buy request was performed successfully.") % supplier[:name]
  11 + content_type 'text/html'
  12 + body :customer => customer,
  13 + :supplier => supplier,
  14 + :items => items,
  15 + :environment => supplier.environment
  16 + end
  17 +
  18 + def supplier_notification(customer, supplier, items)
  19 + recipients supplier.contact_email
  20 + from customer[:email]
  21 + subject _("[%s] You have a new buy request from %s.") % [supplier.environment.name, customer[:name]]
  22 + content_type 'text/html'
  23 + body :customer => customer,
  24 + :supplier => supplier,
  25 + :items => items,
  26 + :environment => supplier.environment
  27 + end
  28 +end
plugins/shopping_cart/public/cart.js
@@ -241,25 +241,17 @@ function Cart(config) { @@ -241,25 +241,17 @@ function Cart(config) {
241 }); 241 });
242 } 242 }
243 243
244 - Cart.send_request = function(button) {  
245 - var params = {};  
246 - params['name'] = $('#name', button.parentNode).val();  
247 - params['email'] = $('#email', button.parentNode).val();  
248 - params['contact_phone'] = $('#contact_phone', button.parentNode).val();  
249 - params['address'] = $('#address', button.parentNode).val();  
250 - Cart.instance.send_request(params); 244 + Cart.send_request = function(form) {
  245 + Cart.instance.send_request($(form).serialize());
  246 + return false;
251 } 247 }
252 248
253 - Cart.prototype.send_request = function(p) {  
254 - params = "?";  
255 - for( var attribute in p ) {  
256 - var value = p[attribute];  
257 - params += attribute+'='+value+'&';  
258 - }  
259 - params = params.substring(0, params.length-1); 249 + Cart.prototype.send_request = function(params) {
260 var me = this; 250 var me = this;
261 $.ajax({ 251 $.ajax({
262 - url: '/profile/'+ me.enterprise +'/plugins/shopping_cart/send_request'+params, 252 + type: 'POST',
  253 + url: '/profile/'+ me.enterprise +'/plugins/shopping_cart/send_request',
  254 + data: params,
263 dataType: 'json', 255 dataType: 'json',
264 success: function(data, status, ajax){ 256 success: function(data, status, ajax){
265 if ( !data.ok ) display_notice(data.error.message); 257 if ( !data.ok ) display_notice(data.error.message);
plugins/shopping_cart/views/shopping_cart_plugin/mailer/customer_notification.html.erb 0 → 100644
@@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
  1 +<% environment = @environment %>
  2 +<h4><%= _('Hi %s!') % @customer[:name] %></h4>
  3 +<p>
  4 +<%= _('This is a notification e-mail about your buy request on %s.') % link_to(@supplier.name, @supplier.url) %>
  5 +<%= _('The supplier already received your buy request and may contact you for confirmation.') %>
  6 +<%= _('Please check if your information below is correct:') %>
  7 +</p>
  8 +<ul>
  9 + <li><b><%= _('Full name') %>: </b><%= @customer[:name] %></li>
  10 + <li><b><%= _('Phone number') %>: </b><%= @customer[:contact_phone] %></li>
  11 + <li><b><%= _('Address') %>: </b><%= @customer[:address] %>
  12 + <br \>
  13 + <%= @customer[:city]+'-'+@customer[:state]+'-'+@customer[:country] %>
  14 + <br \>
  15 + <%= @customer[:zip_code] %></li>
  16 +</ul>
  17 +<p><%=_('Here are the products you bought:')%></p>
  18 +<%= items_table(@items, true) %>
  19 +<p>
  20 +<%=_('Thanks for buying with us!')%>
  21 +<br/>
  22 +<%= link_to @supplier.name, @supplier.url %>
  23 +</p>
  24 +<small style="color: #888"><%= _('A service of %s.') % environment.name %></small>
plugins/shopping_cart/views/shopping_cart_plugin/mailer/supplier_notification.html.erb 0 → 100644
@@ -0,0 +1,22 @@ @@ -0,0 +1,22 @@
  1 +<% environment = @environment %>
  2 +<h4><%= _('Hi %s!') % @supplier.name %></h4>
  3 +<p>
  4 +<%= _('This is a buy request made by %s.') % @customer[:name] %>
  5 +<%= _('Below follows the customer informations:') %>
  6 +</p>
  7 +<ul>
  8 + <li><b><%= _('Full name') %>: </b><%= @customer[:name] %></li>
  9 + <li><b><%= _('Email') %>: </b><%= @customer[:email] %></li>
  10 + <li><b><%= _('Phone number') %>: </b><%= @customer[:contact_phone] %></li>
  11 + <li><b><%= _('Address') %>: </b><%= @customer[:address] %>
  12 + <br \>
  13 + <%= @customer[:city]+'-'+@customer[:state]+'-'+@customer[:country] %>
  14 + <br \>
  15 + <%= @customer[:zip_code] %></li>
  16 +</ul>
  17 +<p><%=_('And here are the items bought by this customer:')%></p>
  18 +<%= items_table(@items, true) %>
  19 +<p>
  20 +<%=_('If there are any problems with this email contact the admin of %s.') % environment.name %>
  21 +</p>
  22 +<small style="color: #888"><%= _('A service of %s.') % environment.name %></small>
plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb
1 <% person = user.nil? ? Person.new : user %> 1 <% person = user.nil? ? Person.new : user %>
2 -<% form_tag :action => 'send_request' do %>  
3 - <%= labelled_form_field( _("Full name"), text_field_tag(:name, person.name) ) %>  
4 - <%= labelled_form_field( _("Email"), text_field_tag(:email, person.email) ) %>  
5 - <%= labelled_form_field( _("Phone number"), text_field_tag(:contact_phone, person.contact_phone) ) %>  
6 - <%= labelled_form_field( _("Delivery address"), text_field_tag(:address, person.address) ) %>  
7 - <br style='clear: both'/><br style='clear: both'/>  
8 - <input class="button with-text icon-send" value="<%=_('Send')%>" type="submit" onclick="Cart.send_request(this); return false">  
9 -<% end %> 2 +<div id='cart-request-box'>
  3 + <% form_for(:customer, person, :url => {:action => 'send_request'},
  4 + :html => {:onsubmit => "return Cart.send_request(this)", :id => 'cart-request-form' }) do |f| %>
  5 + <div id="cart-form-main">
  6 + <%= labelled_form_field( _("Full name"), f.text_field(:name) ) %>
  7 + <%= labelled_form_field( _("Email"), f.text_field(:email) ) %>
  8 + <%= labelled_form_field( _("Phone number"), f.text_field(:contact_phone) ) %>
  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( _("State"), f.text_field(:state)) %>
  14 + <%= select_country(_('Country'), :customer, :country, {:class => 'type-select'}, {:selected => person.country}) %>
  15 + <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %>
  16 + </fieldset>
  17 + <div id="cart-form-actions">
  18 + <%= tag 'input', :class => 'button with-text icon-send',
  19 + :value => _('Send buy request'),
  20 + :type => 'submit'%>
  21 + </div>
  22 + <% end %>
  23 + <%= items_table(session[:cart][:items]) %>
  24 +</div>