diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 8441b2d596..bf80b56fa9 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -159,6 +159,7 @@ Spree::Admin::ReportsController.class_eval do def sales_tax prepare_date_params params @distributors = Enterprise.is_distributor.managed_by(spree_current_user) + @report_type = params[:report_type] @report = OpenFoodNetwork::SalesTaxReport.new spree_current_user, params diff --git a/app/views/spree/admin/reports/sales_tax.html.haml b/app/views/spree/admin/reports/sales_tax.html.haml index 9a5cb9e533..25029636fd 100644 --- a/app/views/spree/admin/reports/sales_tax.html.haml +++ b/app/views/spree/admin/reports/sales_tax.html.haml @@ -5,9 +5,15 @@ .four.columns.alpha = label_tag nil, t(:report_distributor) = f.collection_select(:distributor_id_eq, @distributors, :id, :name, {:include_blank => 'All'}, {:class => "select2 fullwidth"}) + = label_tag nil, t(:report_customers_type) + %br + = select_tag(:report_type, options_for_select([[t(:report_tax_rates),:tax_rates],[t(:report_tax_types),:tax_types]], @report_type)) + %br + %br = check_box_tag :csv = label_tag :csv, t(:report_customers_csv) %br + %br = button t(:search) %br diff --git a/config/locales/en.yml b/config/locales/en.yml index 9cfbc6691e..a883ed948f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1343,6 +1343,8 @@ Please follow the instructions there to make your enterprise visible on the Open report_order_cycle: "Order Cycle: " report_entreprises: "Enterprises: " report_users: "Users: " + report_tax_rates: "Tax rates" + report_tax_types: "Tax types" initial_invoice_number: "Initial invoice number:" invoice_date: "Invoice date:" due_date: "Due date:" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 772dbe5695..ff74ed2780 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -1225,6 +1225,8 @@ fr: report_order_cycle: "Cycle de vente:" report_entreprises: "Entreprises:" report_users: "Managers:" + report_tax_rates: "Taxes par taux" + report_tax_types: "Taxes par type" initial_invoice_number: "N° de facture initial:" invoice_date: "Date de facture:" due_date: "Date d'échéance:" diff --git a/lib/open_food_network/sales_tax_report.rb b/lib/open_food_network/sales_tax_report.rb index 0d41b24ec1..779e870ddc 100644 --- a/lib/open_food_network/sales_tax_report.rb +++ b/lib/open_food_network/sales_tax_report.rb @@ -9,9 +9,16 @@ module OpenFoodNetwork end def header - ["Order number", "Date", "Items", "Items total (#{currency_symbol})", "Taxable Items Total (#{currency_symbol})", - "Sales Tax (#{currency_symbol})", "Delivery Charge (#{currency_symbol})", "Tax on Delivery (#{currency_symbol})", "Tax on Fees (#{currency_symbol})", - "Total Tax (#{currency_symbol})", "Customer", "Distributor"] + case params[:report_type] + when "tax_types" + ["Order number", "Date", "Items", "Items total (#{currency_symbol})", "Taxable Items Total (#{currency_symbol})", + "Sales Tax (#{currency_symbol})", "Delivery Charge (#{currency_symbol})", "Tax on Delivery (#{currency_symbol})", "Tax on Fees (#{currency_symbol})", + "Total Tax (#{currency_symbol})", "Customer", "Distributor"] + else + ["Order number", "Total excl. VAT"] + + relevant_rates.map { |rate| "%s (%.1f%%)" % [rate.name, rate.amount.to_f * 100] } + + ["Total VAT", "Total incl. VAT"] + end end def search @@ -24,19 +31,39 @@ module OpenFoodNetwork end def table - orders.map do |order| - totals = totals_of order.line_items - shipping_cost = shipping_cost_for order + case params[:report_type] + when "tax_types" + orders.map do |order| + totals = totals_of order.line_items + shipping_cost = shipping_cost_for order - [order.number, order.created_at, totals[:items], totals[:items_total], - totals[:taxable_total], totals[:sales_tax], shipping_cost, order.shipping_tax, order.enterprise_fee_tax, order.total_tax, - order.bill_address.full_name, order.distributor.andand.name] + [order.number, order.created_at, totals[:items], totals[:items_total], + totals[:taxable_total], totals[:sales_tax], shipping_cost, order.shipping_tax, order.enterprise_fee_tax, order.total_tax, + order.bill_address.full_name, order.distributor.andand.name] + end + else + orders.map do |order| + [order.number, order.total - order.total_tax] + + relevant_rates.map { |rate| order.tax_adjustment_totals.fetch(rate, 0) } + + [order.total_tax, order.display_total] + end end + end private + def relevant_rates + queries = [ search.result.joins(:line_items => {:adjustments => :tax_rate}).select('spree_tax_rates.*').uniq, + search.result.joins(:adjustments => :tax_rate).select('spree_tax_rates.*').uniq ] + queries.map do |query| + ActiveRecord::Base.connection.select_all(query) + end.sum.map do + |tax_rate| Spree::TaxRate.new(tax_rate, without_protection: true) + end + end + def totals_of(line_items) totals = {items: 0, items_total: 0.0, taxable_total: 0.0, sales_tax: 0.0}