diff --git a/app/assets/javascripts/darkswarm/controllers/checkout/checkout_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/checkout/checkout_controller.js.coffee index 81a852bd36..da5cb974e4 100644 --- a/app/assets/javascripts/darkswarm/controllers/checkout/checkout_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/checkout/checkout_controller.js.coffee @@ -14,6 +14,14 @@ Darkswarm.controller "CheckoutCtrl", ($scope, storage, Checkout, CurrentUser, Cu storeName: "#{prefix}_sameasbilling" defaultValue: 'YES' + storage.bind $scope, "Checkout.default_bill_address", + storeName: "#{prefix}_defaultasbilladdress" + defaultValue: 'NO' + + storage.bind $scope, "Checkout.default_ship_address", + storeName: "#{prefix}_defaultasshipaddress" + defaultValue: 'NO' + $scope.order = Checkout.order # Ordering is important $scope.secrets = Checkout.secrets diff --git a/app/assets/javascripts/darkswarm/services/checkout.js.coffee b/app/assets/javascripts/darkswarm/services/checkout.js.coffee index 2336de3382..3b6a6f53af 100644 --- a/app/assets/javascripts/darkswarm/services/checkout.js.coffee +++ b/app/assets/javascripts/darkswarm/services/checkout.js.coffee @@ -4,6 +4,9 @@ Darkswarm.factory 'Checkout', (CurrentOrder, ShippingMethods, PaymentMethods, $h secrets: {} order: CurrentOrder.order ship_address_same_as_billing: 'YES' + default_bill_address: 'NO' + default_ship_address: 'NO' + submit: -> Loading.message = t 'submitting_order' @@ -19,7 +22,10 @@ Darkswarm.factory 'Checkout', (CurrentOrder, ShippingMethods, PaymentMethods, $h # Rails wants our Spree::Address data to be provided with _attributes preprocess: -> - munged_order = {} + munged_order = + default_bill_address: @default_bill_address + default_ship_address: @default_ship_address + for name, value of @order # Clone all data from the order JSON object switch name when "bill_address" diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index c6d1291477..47d68b5e53 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -50,6 +50,8 @@ class CheckoutController < Spree::CheckoutController else update_failed end + + set_default_address_for_user else update_failed end @@ -58,6 +60,12 @@ class CheckoutController < Spree::CheckoutController private + def set_default_address_for_user + spree_current_user.set_bill_address(@order.bill_address.clone) if params[:order][:default_bill_address] == 'YES' + spree_current_user.set_ship_address(@order.ship_address.clone) if params[:order][:default_ship_address] == 'YES' + end + + def check_order_for_phantom_fees phantom_fees = @order.adjustments.joins('LEFT OUTER JOIN spree_line_items ON spree_line_items.id = spree_adjustments.source_id'). where("originator_type = 'EnterpriseFee' AND source_type = 'Spree::LineItem' AND spree_line_items.id IS NULL") diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index 562b0d10ca..8e8f134202 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -76,6 +76,16 @@ Spree.user_class.class_eval do data_array.sort! { |a, b| b.distributed_orders.length <=> a.distributed_orders.length } end + def set_bill_address(address) + self.bill_address = address + self.save + end + + def set_ship_address(address) + self.ship_address = address + self.save + end + private def limit_owned_enterprises diff --git a/app/views/checkout/_billing.html.haml b/app/views/checkout/_billing.html.haml index 7ea4d57da1..9e24c986b9 100644 --- a/app/views/checkout/_billing.html.haml +++ b/app/views/checkout/_billing.html.haml @@ -12,7 +12,13 @@ %accordion-group{"is-open" => "accordion.billing", "ng-class" => "{valid: billing.$valid, open: accordion.billing}"} = render 'checkout/accordion_heading' - + + - if spree_current_user + .small-12.columns + %label + %input{type: :checkbox, "ng-model" => "Checkout.default_bill_address", "ng-true-value" => "'YES'", "ng-false-value" => "'NO'"} + = t :checkout_default_bill_address + = f.fields_for :bill_address, @order.bill_address do |ba| .row .small-12.columns diff --git a/app/views/checkout/_shipping.html.haml b/app/views/checkout/_shipping.html.haml index 97722d49a5..453922ca14 100644 --- a/app/views/checkout/_shipping.html.haml +++ b/app/views/checkout/_shipping.html.haml @@ -13,7 +13,7 @@ "ng-class" => "{valid: shipping.$valid, open: accordion.shipping}"} = render 'checkout/accordion_heading' - .small-12.columns + .small-12.columns.medium-6.columns.large-6.columns %label{"ng-repeat" => "method in ShippingMethods.shipping_methods"} %input{type: :radio, required: true, @@ -33,6 +33,11 @@ %input{type: :checkbox, "ng-model" => "Checkout.ship_address_same_as_billing", "ng-true-value" => "'YES'", "ng-false-value" => "'NO'"} = t :checkout_address_same + - if spree_current_user + %label + %input{type: :checkbox, "ng-model" => "Checkout.default_ship_address", "ng-true-value" => "'YES'", "ng-false-value" => "'NO'"} + = t :checkout_default_ship_address + .small-12.columns.medium-6.columns.large-6.columns #distributor_address.panel{"ng-show" => "Checkout.shippingMethod().description"} %span{ style: "white-space: pre-wrap;" }{{ Checkout.shippingMethod().description }} diff --git a/config/locales/en.yml b/config/locales/en.yml index 20cc39e0d8..e9e737d664 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -380,7 +380,9 @@ en: checkout_as_guest: "Checkout as guest" checkout_details: "Your details" checkout_billing: "Billing info" + checkout_default_bill_address: "Save as default billing address" checkout_shipping: Shipping info + checkout_default_ship_address: "Save as default shipping address" checkout_method_free: Free checkout_address_same: Shipping address same as billing address? checkout_ready_for: "Ready for:" diff --git a/spec/features/consumer/shopping/checkout_spec.rb b/spec/features/consumer/shopping/checkout_spec.rb index 6055cb47d1..b399fe7762 100644 --- a/spec/features/consumer/shopping/checkout_spec.rb +++ b/spec/features/consumer/shopping/checkout_spec.rb @@ -61,6 +61,50 @@ feature "As a consumer I want to check out my cart", js: true do page.should have_content "An item in your cart has become unavailable" end end + context 'login in as user' do + let(:user) { create(:user) } + + before do + quick_login_as(user) + visit checkout_path + + toggle_shipping + choose sm1.name + toggle_payment + choose pm1.name + toggle_details + within "#details" do + fill_in "First Name", with: "Will" + fill_in "Last Name", with: "Marshall" + fill_in "Email", with: "test@test.com" + fill_in "Phone", with: "0468363090" + end + toggle_billing + check "Save as default billing address" + within "#billing" do + fill_in "City", with: "Melbourne" + fill_in "Postcode", with: "3066" + fill_in "Address", with: "123 Your Face" + select "Australia", from: "Country" + select "Victoria", from: "State" + end + + toggle_shipping + check "Shipping address same as billing address?" + check "Save as default shipping address" + end + + it "sets user's default billing address and shipping address" do + user.bill_address.should be_nil + user.ship_address.should be_nil + + place_order + page.should have_content "Your order has been processed successfully" + + user.reload.bill_address.address1.should eq '123 Your Face' + user.reload.ship_address.address1.should eq '123 Your Face' + end + end context "on the checkout page" do before do @@ -73,6 +117,11 @@ feature "As a consumer I want to check out my cart", js: true do page.should have_content distributor.name end + it 'does not show the save as defalut address checkbox' do + page.should_not have_content "Save as default billing address" + page.should_not have_content "Save as default shipping address" + end + it "shows a breakdown of the order price" do toggle_shipping choose sm2.name @@ -244,6 +293,10 @@ feature "As a consumer I want to check out my cart", js: true do page.should have_content "Your order has been processed successfully" end + it 'sets default billing and shipping address after the order processed successfully' do + + end + it "takes us to the cart page with an error when a product becomes out of stock just before we purchase", js: true do Spree::Config.set allow_backorders: false variant.on_hand = 0 diff --git a/spec/javascripts/unit/darkswarm/controllers/checkout/checkout_controller_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers/checkout/checkout_controller_spec.js.coffee index 170d85ad88..b2c36b000a 100644 --- a/spec/javascripts/unit/darkswarm/controllers/checkout/checkout_controller_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/controllers/checkout/checkout_controller_spec.js.coffee @@ -59,10 +59,13 @@ describe "CheckoutCtrl", -> describe "Local storage", -> it "binds to localStorage when given a scope", -> prefix = "order_#{scope.order.id}#{CurrentUser.id or ""}#{CurrentHubMock.hub.id}" - console.log prefix + field = scope.fieldsToBind[0] expect(storage.bind).toHaveBeenCalledWith(scope, "Checkout.order.#{field}", {storeName: "#{prefix}_#{field}"}) expect(storage.bind).toHaveBeenCalledWith(scope, "Checkout.ship_address_same_as_billing", {storeName: "#{prefix}_sameasbilling", defaultValue: 'YES'}) + expect(storage.bind).toHaveBeenCalledWith(scope, "Checkout.default_bill_address", {storeName: "#{prefix}_defaultasbilladdress", defaultValue: 'NO'}) + expect(storage.bind).toHaveBeenCalledWith(scope, "Checkout.default_ship_address", {storeName: "#{prefix}_defaultasshipaddress", defaultValue: 'NO'}) + it "it can retrieve data from localstorage", -> prefix = "order_#{scope.order.id}#{CurrentUser.id or ""}#{CurrentHubMock.hub.id}" diff --git a/spec/javascripts/unit/darkswarm/services/checkout_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/checkout_spec.js.coffee index 4d10592e91..03b7e96b16 100644 --- a/spec/javascripts/unit/darkswarm/services/checkout_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/checkout_spec.js.coffee @@ -131,6 +131,16 @@ describe 'Checkout service', -> Checkout.ship_address_same_as_billing = 'YES' expect(Checkout.preprocess().ship_address_attributes).toEqual(orderData.bill_address) + it "munges the default as billing address and shipping address", -> + expect(Checkout.preprocess().default_bill_address).toEqual('NO') + expect(Checkout.preprocess().default_ship_address).toEqual('NO') + + Checkout.default_bill_address = 'YES' + Checkout.default_ship_address = 'YES' + + expect(Checkout.preprocess().default_bill_address).toEqual('YES') + expect(Checkout.preprocess().default_ship_address).toEqual('YES') + it "creates attributes for card fields", -> source_attributes = Checkout.preprocess().payments_attributes[0].source_attributes expect(source_attributes).toBeDefined()