diff --git a/app/controllers/spree/admin/orders_controller_decorator.rb b/app/controllers/spree/admin/orders_controller_decorator.rb index 7749950854..c35d3f1977 100644 --- a/app/controllers/spree/admin/orders_controller_decorator.rb +++ b/app/controllers/spree/admin/orders_controller_decorator.rb @@ -2,6 +2,7 @@ require 'open_food_network/spree_api_key_loader' Spree::Admin::OrdersController.class_eval do include OpenFoodNetwork::SpreeApiKeyLoader + helper CheckoutHelper before_filter :load_spree_api_key, :only => :bulk_management # We need to add expections for collection actions other than :index here diff --git a/app/helpers/checkout_helper.rb b/app/helpers/checkout_helper.rb index c1139b63f9..080db9c0e8 100644 --- a/app/helpers/checkout_helper.rb +++ b/app/helpers/checkout_helper.rb @@ -38,6 +38,10 @@ module CheckoutHelper Spree::Money.new order.total_tax, currency: order.currency end + def display_checkout_total_less_tax(order) + Spree::Money.new order.total - order.total_tax, currency: order.currency + end + def checkout_state_options(source_address) if source_address == :billing address = @order.billing_address diff --git a/app/models/spree/adjustment_decorator.rb b/app/models/spree/adjustment_decorator.rb index d27b5d760b..766d5fc97a 100644 --- a/app/models/spree/adjustment_decorator.rb +++ b/app/models/spree/adjustment_decorator.rb @@ -25,5 +25,9 @@ module Spree def has_tax? included_tax > 0 end + + def display_included_tax + Spree::Money.new(included_tax, { :currency => currency }) + end end end diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 689cabcc6c..5dc7a1bf53 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -37,6 +37,10 @@ Spree::LineItem.class_eval do adjustments.included_tax.any? end + def included_tax + adjustments.included_tax.sum(&:included_tax) + end + def price_with_adjustments # EnterpriseFee#create_locked_adjustment applies adjustments on line items to their parent order, # so line_item.adjustments returns an empty array @@ -57,4 +61,8 @@ Spree::LineItem.class_eval do def display_amount_with_adjustments Spree::Money.new(amount_with_adjustments, { :currency => currency }) end + + def display_included_tax + Spree::Money.new(included_tax, { :currency => currency }) + end end diff --git a/app/views/spree/admin/orders/_invoice_table.html.haml b/app/views/spree/admin/orders/_invoice_table.html.haml new file mode 100644 index 0000000000..9a7ebf14ce --- /dev/null +++ b/app/views/spree/admin/orders/_invoice_table.html.haml @@ -0,0 +1,57 @@ +%table.order-summary{:width => "100%"} + %thead + %tr + %th{:align => "left"} + %h4 Item + %th{:align => "right", :width => "15%"} + %h4 Qty + %th{:align => "right", :width => "15%"} + %h4 GST + %th{:align => "right", :width => "15%"} + %h4 Price + %tbody + - @order.line_items.each do |item| + %tr + %td + - if item.variant.product.name == item.variant.name_to_display + %strong= "#{raw(item.variant.product.name)}" + - else + %strong + %span= "#{raw(item.variant.product.name)}" + %span= "- " + "#{raw(item.variant.name_to_display)}" + - if item.variant.options_text + = "(" + "#{raw(item.variant.options_text)}" + ")" + %td{:align => "right"} + = item.quantity + %td{:align => "right"} + = item.display_included_tax + %td{:align => "right"} + = item.display_amount_with_adjustments + - checkout_adjustments_for(@order, exclude: [:line_item]).reject{ |a| a.amount == 0 }.reverse_each do |adjustment| + %tr + %td + %strong= "#{raw(adjustment.label)}" + %td{:align => "right"} + 1 + %td{:align => "right"} + = adjustment.display_included_tax + %td{:align => "right"} + = adjustment.display_amount + %tfoot + %tr + %td{:align => "right", :colspan => "2"} + %strong GST Total: + %td{:align => "right", :colspan => "2"} + %strong= display_checkout_tax_total(@order) + %tr + %td{:align => "right", :colspan => "2"} + %strong Total (Excl. GST): + %td{:align => "right", :colspan => "2"} + %strong= display_checkout_total_less_tax(@order) + %tr + %td{:align => "right", :colspan => "2"} + %strong Total (Incl. GST): + %td{:align => "right", :colspan => "2"} + %strong= @order.display_total +%p +   diff --git a/app/views/spree/admin/orders/invoice.html.haml b/app/views/spree/admin/orders/invoice.html.haml index db94bb060d..7c4cd37b2a 100644 --- a/app/views/spree/admin/orders/invoice.html.haml +++ b/app/views/spree/admin/orders/invoice.html.haml @@ -3,13 +3,39 @@ %table{:width => "100%"} %tbody - %tr + %tr{ valign: "top" } %td{ :align => "left" } - = "Order ##{@order.number}" + %h4 + - if @order.total_tax > 0 + = "TAX" + = "INVOICE: " + = "#{@order.number}" + %td{width: "10%" } +   %td{ :align => "right" } - - if @order.total_tax > 0 - = "TAX" - = "INVOICE" + %h4= Time.zone.now.strftime("%F") + %tr{ valign: "top" } + %td{ :align => "left" } + %strong= "From: #{@order.distributor.name}" + - if @order.distributor.abn.present? + %br + = "ABN: #{@order.distributor.abn}" + %br + = @order.distributor.address.full_address + %br + = @order.distributor.email + %td{width: "10%" } +   + %td{ :align => "right" } + %strong= "To: #{@order.ship_address.full_name}" + - if @order.customer.code.present? + %br + = "Code: #{@order.customer.code}" + %br + = @order.ship_address.full_address + %br + = "#{@order.customer.email}," + = "#{@order.bill_address.phone}" -= render 'spree/order_mailer/order_summary' += render 'spree/admin/orders/invoice_table' = render 'spree/order_mailer/payment' diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index f9c0e97f20..96ea64f838 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -64,18 +64,30 @@ module Spree end end - describe "checking if a line item has tax included" do + describe "tax" do let(:li_no_tax) { create(:line_item) } let(:li_tax) { create(:line_item) } let(:tax_rate) { create(:tax_rate, calculator: Spree::Calculator::DefaultTax.new) } let!(:adjustment) { create(:adjustment, adjustable: li_tax, originator: tax_rate, label: "TR", amount: 123, included_tax: 10.00) } - it "returns true when it does" do - li_tax.should have_tax + context "checking if a line item has tax included" do + it "returns true when it does" do + expect(li_tax).to have_tax + end + + it "returns false otherwise" do + expect(li_no_tax).to_not have_tax + end end - it "returns false otherwise" do - li_no_tax.should_not have_tax + context "calculating the amount of included tax" do + it "returns the included tax when present" do + expect(li_tax.included_tax).to eq 10.00 + end + + it "returns 0.00 otherwise" do + expect(li_no_tax.included_tax).to eq 0.00 + end end end end