Commit 3f3550d3fd6947eba4cd4e14bd165331b15c10bd

Authored by Rodrigo Souto
1 parent b99c27c5

[shopping-cart plugin] Delivery and some fixes

plugins/shopping_cart/controllers/shopping_cart_plugin_myprofile_controller.rb
@@ -4,8 +4,7 @@ class ShoppingCartPluginMyprofileController < MyProfileController @@ -4,8 +4,7 @@ class ShoppingCartPluginMyprofileController < MyProfileController
4 def edit 4 def edit
5 if request.post? 5 if request.post?
6 begin 6 begin
7 - profile.shopping_cart = params[:shopping_cart] == '1' ? true : false  
8 - profile.save! 7 + profile.update_attributes!(params[:profile_attr])
9 session[:notice] = _('Option updated successfully.') 8 session[:notice] = _('Option updated successfully.')
10 rescue Exception => exception 9 rescue Exception => exception
11 session[:notice] = _('Option wasn\'t updated successfully.') 10 session[:notice] = _('Option wasn\'t updated successfully.')
plugins/shopping_cart/db/migrate/20110513112133_add_shopping_cart_delivery_to_profile.rb 0 → 100644
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  1 +class AddShoppingCartDeliveryToProfile < ActiveRecord::Migration
  2 +
  3 + def self.up
  4 + add_column :profiles, :shopping_cart_delivery, :boolean, :default => false
  5 + add_column :profiles, :shopping_cart_delivery_price, :decimal, :default => 0
  6 + end
  7 +
  8 + def self.down
  9 + remove_column :profiles, :shopping_cart_delivery
  10 + remove_column :profiles, :shopping_cart_delivery_price
  11 + end
  12 +end
plugins/shopping_cart/lib/shopping_cart_plugin/cart_helper.rb
@@ -15,8 +15,20 @@ module ShoppingCartPlugin::CartHelper @@ -15,8 +15,20 @@ module ShoppingCartPlugin::CartHelper
15 float_to_currency_cart(items.map { |id, quantity| sell_price(Product.find(id)) * quantity}.sum, environment) 15 float_to_currency_cart(items.map { |id, quantity| sell_price(Product.find(id)) * quantity}.sum, environment)
16 end 16 end
17 17
18 - def items_table(items, environment, by_mail = false)  
19 - '<table id="cart-items-table" cellpadding="2" cellspacing="0" 18 + def items_table(items, profile, by_mail = false)
  19 + environment = profile.environment
  20 + items = items.to_a
  21 + if profile.shopping_cart_delivery
  22 + delivery = Product.create!(:name => _('Delivery'), :price => profile.shopping_cart_delivery_price, :product_category => ProductCategory.last)
  23 + items << [delivery.id, 1]
  24 + end
  25 +
  26 + quantity_opts = { :class => 'cart-table-quantity' }
  27 + quantity_opts.merge!({:align => 'center'}) if by_mail
  28 + price_opts = {:class => 'cart-table-price'}
  29 + price_opts.merge!({:align => 'right'}) if by_mail
  30 +
  31 + table = '<table id="cart-items-table" cellpadding="2" cellspacing="0"
20 border="'+(by_mail ? '1' : '0')+'" 32 border="'+(by_mail ? '1' : '0')+'"
21 style="'+(by_mail ? 'border-collapse:collapse' : '')+'">' + 33 style="'+(by_mail ? 'border-collapse:collapse' : '')+'">' +
22 content_tag('tr', 34 content_tag('tr',
@@ -26,18 +38,19 @@ module ShoppingCartPlugin::CartHelper @@ -26,18 +38,19 @@ module ShoppingCartPlugin::CartHelper
26 ) + 38 ) +
27 items.map do |id, quantity| 39 items.map do |id, quantity|
28 product = Product.find(id) 40 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', 41 content_tag('tr',
34 content_tag('td', product.name) + 42 content_tag('td', product.name) +
35 content_tag('td', quantity, quantity_opts ) + 43 content_tag('td', quantity, quantity_opts ) +
36 content_tag('td', get_price(product, environment), price_opts ) 44 content_tag('td', get_price(product, environment), price_opts )
37 ) 45 )
38 - end.join("\n") + 46 + end.join("\n")
  47 +
  48 + total = get_total(items, environment)
  49 + delivery.destroy if profile.shopping_cart_delivery
  50 +
  51 + table +
39 content_tag('th', _('Total:'), :colspan => 2, :class => 'cart-table-total-label') + 52 content_tag('th', _('Total:'), :colspan => 2, :class => 'cart-table-total-label') +
40 - content_tag('th', get_total(items, environment), :class => 'cart-table-total-value') + 53 + content_tag('th', total, :class => 'cart-table-total-value') +
41 '</table>' 54 '</table>'
42 end 55 end
43 56
plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb
@@ -20,7 +20,7 @@ class ShoppingCartPluginMyprofileControllerTest &lt; Test::Unit::TestCase @@ -20,7 +20,7 @@ class ShoppingCartPluginMyprofileControllerTest &lt; Test::Unit::TestCase
20 should 'be able to enable shopping cart' do 20 should 'be able to enable shopping cart' do
21 enterprise.shopping_cart = false 21 enterprise.shopping_cart = false
22 enterprise.save 22 enterprise.save
23 - post :edit, :profile => enterprise.identifier, :shopping_cart => '1' 23 + post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart => '1'}
24 enterprise.reload 24 enterprise.reload
25 25
26 assert enterprise.shopping_cart 26 assert enterprise.shopping_cart
@@ -29,9 +29,34 @@ class ShoppingCartPluginMyprofileControllerTest &lt; Test::Unit::TestCase @@ -29,9 +29,34 @@ class ShoppingCartPluginMyprofileControllerTest &lt; Test::Unit::TestCase
29 should 'be able to disable shopping cart' do 29 should 'be able to disable shopping cart' do
30 enterprise.shopping_cart = true 30 enterprise.shopping_cart = true
31 enterprise.save 31 enterprise.save
32 - post :edit, :profile => enterprise.identifier, :shopping_cart => '0' 32 + post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart => '0'}
33 enterprise.reload 33 enterprise.reload
34 34
35 assert !enterprise.shopping_cart 35 assert !enterprise.shopping_cart
36 end 36 end
  37 +
  38 + should 'be able to enable shopping cart delivery' do
  39 + enterprise.shopping_cart_delivery = false
  40 + enterprise.save
  41 + post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart_delivery => '1'}
  42 + enterprise.reload
  43 +
  44 + assert enterprise.shopping_cart_delivery
  45 + end
  46 +
  47 + should 'be able to disable shopping cart delivery' do
  48 + enterprise.shopping_cart_delivery = true
  49 + enterprise.save
  50 + post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart_delivery => '0'}
  51 + enterprise.reload
  52 +
  53 + assert !enterprise.shopping_cart_delivery
  54 + end
  55 +
  56 + should 'be able to choose the delivery price' do
  57 + price = 4.35
  58 + post :edit, :profile => enterprise.identifier, :profile_attr => {:shopping_cart_delivery_price => price}
  59 + enterprise.reload
  60 + assert enterprise.shopping_cart_delivery_price == price
  61 + end
37 end 62 end
plugins/shopping_cart/views/shopping_cart_plugin/mailer/customer_notification.html.erb
@@ -21,7 +21,7 @@ @@ -21,7 +21,7 @@
21 </ul> 21 </ul>
22 22
23 <p><%=_('Here are the products you bought:')%></p> 23 <p><%=_('Here are the products you bought:')%></p>
24 - <%= items_table(@items, @environment, true) %> 24 + <%= items_table(@items, @supplier, true) %>
25 25
26 <p> 26 <p>
27 --<br/> 27 --<br/>
plugins/shopping_cart/views/shopping_cart_plugin/mailer/supplier_notification.html.erb
@@ -21,7 +21,7 @@ @@ -21,7 +21,7 @@
21 </ul> 21 </ul>
22 22
23 <p><%=_('And here are the items bought by this customer:')%></p> 23 <p><%=_('And here are the items bought by this customer:')%></p>
24 - <%= items_table(@items, @environment, true) %> 24 + <%= items_table(@items, @supplier, true) %>
25 25
26 <p> 26 <p>
27 --<br/> 27 --<br/>
plugins/shopping_cart/views/shopping_cart_plugin_myprofile/edit.html.erb
1 <h1> <%= _('Cart options') %> </h1> 1 <h1> <%= _('Cart options') %> </h1>
2 2
3 -<%# form_for :profile, profile, :url => {:action => 'edit'} do %>  
4 -<% form_tag :action => 'edit' do %>  
5 - <%= labelled_check_box(_('Enabled?'), :shopping_cart, '1', profile.shopping_cart) %> 3 +<% form_for(:profile_attr, profile, :url => {:action => 'edit'}, :html => {:method => 'post'}) do |f| %>
  4 +<%# form_tag :action => 'edit' do %>
  5 + <%= labelled_form_field(_('Enabled?'), f.check_box(:shopping_cart)) %>
  6 + <%= labelled_form_field(_('Delivery?'), f.check_box(:shopping_cart_delivery)) %>
  7 + <%= labelled_form_field(_('Delivery price:'), f.text_field(:shopping_cart_delivery_price)) %>
6 <br style='clear: both'/> 8 <br style='clear: both'/>
7 <br style='clear: both'/> 9 <br style='clear: both'/>
8 <div> 10 <div>
plugins/shopping_cart/views/shopping_cart_plugin_profile/buy.html.erb
@@ -3,22 +3,21 @@ @@ -3,22 +3,21 @@
3 <% form_for(:customer, person, :url => {:action => 'send_request'}, 3 <% form_for(:customer, person, :url => {:action => 'send_request'},
4 :html => {:onsubmit => "return Cart.send_request(this)", :id => 'cart-request-form' }) do |f| %> 4 :html => {:onsubmit => "return Cart.send_request(this)", :id => 'cart-request-form' }) do |f| %>
5 <div id="cart-form-main"> 5 <div id="cart-form-main">
6 - <%= labelled_form_field('* ' + _("Full name"), f.text_field(:name, :class => 'required') ) %> 6 + <%= labelled_form_field('* ' + _("Name"), f.text_field(:name, :class => 'required') ) %>
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('* ' + _("Phone number"), 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> 10 <fieldset><legend><%=_('Delivery Address')%></legend>
11 <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %> 11 <%= labelled_form_field(_('Address (street and number)'), f.text_field(:address)) %>
12 <%= labelled_form_field( _("City"), f.text_field(:city)) %> 12 <%= labelled_form_field( _("City"), f.text_field(:city)) %>
13 <%= labelled_form_field( _("State"), f.text_field(:state)) %> 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)) %> 14 <%= labelled_form_field(_('ZIP code'), f.text_field(:zip_code)) %>
16 </fieldset> 15 </fieldset>
17 <div id="cart-form-actions"> 16 <div id="cart-form-actions">
18 <%= submit_button(:send, _('Send buy request')) %> 17 <%= submit_button(:send, _('Send buy request')) %>
19 </div> 18 </div>
20 <% end %> 19 <% end %>
21 - <%= items_table(session[:cart][:items], @environment) %> 20 + <%= items_table(session[:cart][:items], profile) %>
22 </div> 21 </div>
23 22
24 <script type="text/javascript"> 23 <script type="text/javascript">