From 2fe25c6219dbbb42243c7a888c1b9383fa46f64f Mon Sep 17 00:00:00 2001 From: Pierre de Lacroix Date: Tue, 14 Mar 2017 23:59:09 +0100 Subject: [PATCH] Allow both invoice layouts to coexist Add a new "Invoice" page in the admin backend with a checkbox to toggle between layouts --- .../admin/invoice_settings_controller.rb | 13 ++++ .../admin/orders_controller_decorator.rb | 6 +- .../spree/app_configuration_decorator.rb | 3 + .../add_invoice_settings.html.haml.deface | 4 ++ .../admin/invoice_settings/edit.html.haml | 14 ++++ .../admin/orders/_invoice_table.html.haml | 44 +++++------- .../admin/orders/_invoice_table2.html.haml | 58 +++++++++++++++ .../spree/admin/orders/invoice.html.haml | 57 +++++---------- .../spree/admin/orders/invoice2.html.haml | 71 +++++++++++++++++++ config/locales/en.yml | 6 ++ config/locales/fr.yml | 6 ++ config/routes.rb | 2 + 12 files changed, 218 insertions(+), 66 deletions(-) create mode 100644 app/controllers/admin/invoice_settings_controller.rb create mode 100644 app/overrides/spree/admin/shared/_configuration_menu/add_invoice_settings.html.haml.deface create mode 100644 app/views/admin/invoice_settings/edit.html.haml create mode 100644 app/views/spree/admin/orders/_invoice_table2.html.haml create mode 100644 app/views/spree/admin/orders/invoice2.html.haml diff --git a/app/controllers/admin/invoice_settings_controller.rb b/app/controllers/admin/invoice_settings_controller.rb new file mode 100644 index 0000000000..ac65c9860e --- /dev/null +++ b/app/controllers/admin/invoice_settings_controller.rb @@ -0,0 +1,13 @@ +class Admin::InvoiceSettingsController < Spree::Admin::BaseController + + def update + Spree::Config.set(params[:preferences]) + + respond_to do |format| + format.html { + redirect_to main_app.edit_admin_invoice_settings_path + } + end + end + +end diff --git a/app/controllers/spree/admin/orders_controller_decorator.rb b/app/controllers/spree/admin/orders_controller_decorator.rb index b5a2bb4f95..e24f89eb27 100644 --- a/app/controllers/spree/admin/orders_controller_decorator.rb +++ b/app/controllers/spree/admin/orders_controller_decorator.rb @@ -77,7 +77,8 @@ Spree::Admin::OrdersController.class_eval do end def invoice - pdf = render_to_string pdf: "invoice-#{@order.number}.pdf", template: "spree/admin/orders/invoice", formats: [:html], encoding: "UTF-8" + template = if Spree::Config.invoice_style2? then "spree/admin/orders/invoice2" else "spree/admin/orders/invoice" end + pdf = render_to_string pdf: "invoice-#{@order.number}.pdf", template: template, formats: [:html], encoding: "UTF-8" Spree::OrderMailer.invoice_email(@order.id, pdf).deliver flash[:success] = t(:invoice_email_sent) @@ -85,7 +86,8 @@ Spree::Admin::OrdersController.class_eval do end def print - render pdf: "invoice-#{@order.number}", template: "spree/admin/orders/invoice", encoding: "UTF-8" + template = if Spree::Config.invoice_style2? then "spree/admin/orders/invoice2" else "spree/admin/orders/invoice" end + render pdf: "invoice-#{@order.number}", template: template, encoding: "UTF-8" end def update_distribution_charge diff --git a/app/models/spree/app_configuration_decorator.rb b/app/models/spree/app_configuration_decorator.rb index ef1478e595..cbddf2e5ed 100644 --- a/app/models/spree/app_configuration_decorator.rb +++ b/app/models/spree/app_configuration_decorator.rb @@ -32,4 +32,7 @@ Spree::AppConfiguration.class_eval do # External services preference :bugherd_api_key, :string, default: nil + + # Invoice layout + preference :invoice_style2?, :boolean, default: false end diff --git a/app/overrides/spree/admin/shared/_configuration_menu/add_invoice_settings.html.haml.deface b/app/overrides/spree/admin/shared/_configuration_menu/add_invoice_settings.html.haml.deface new file mode 100644 index 0000000000..f65ed9eeea --- /dev/null +++ b/app/overrides/spree/admin/shared/_configuration_menu/add_invoice_settings.html.haml.deface @@ -0,0 +1,4 @@ +// insert_bottom "[data-hook='admin_configurations_sidebar_menu']" + +%li + = link_to t('admin.invoice_settings.edit.invoice_settings'), main_app.edit_admin_invoice_settings_path diff --git a/app/views/admin/invoice_settings/edit.html.haml b/app/views/admin/invoice_settings/edit.html.haml new file mode 100644 index 0000000000..a740e03a08 --- /dev/null +++ b/app/views/admin/invoice_settings/edit.html.haml @@ -0,0 +1,14 @@ += render :partial => 'spree/admin/shared/configuration_menu' + +- content_for :page_title do + = t('.invoice_settings') + += form_tag main_app.admin_invoice_settings_path, :method => :put do + + .field.align-center + = hidden_field_tag 'preferences[invoice_style2?]', '0' + = check_box_tag 'preferences[invoice_style2?]', '1', Spree::Config[:invoice_style2?] + = label_tag nil, t('.invoice_style2?') + + .form-buttons{"data-hook" => "buttons"} + = button t(:update), 'icon-refresh' diff --git a/app/views/spree/admin/orders/_invoice_table.html.haml b/app/views/spree/admin/orders/_invoice_table.html.haml index 0941904d8b..4bd9917eb0 100644 --- a/app/views/spree/admin/orders/_invoice_table.html.haml +++ b/app/views/spree/admin/orders/_invoice_table.html.haml @@ -2,16 +2,13 @@ %thead %tr %th{:align => "left"} - %h5= t(:invoice_column_item) + %h4= t(:invoice_column_item) %th{:align => "right", :width => "15%"} - %h5= t(:invoice_column_qty) + %h4= t(:invoice_column_qty) %th{:align => "right", :width => "15%"} - %h5= @order.has_taxes_included ? t(:invoice_column_unit_price_with_taxes) : t(:invoice_column_unit_price_without_taxes) + %h4= @order.total_tax > 0 ? t(:invoice_column_tax) : "" %th{:align => "right", :width => "15%"} - %h5= @order.has_taxes_included ? t(:invoice_column_price_with_taxes) : t(:invoice_column_price_without_taxes) - - if @order.total_tax > 0 - %th{:align => "right", :width => "15%"} - %h5= t(:invoice_column_tax_rate) + %h4= t(:invoice_column_price) %tbody - @order.line_items.sort_by{ |li| li.product.name }.each do |item| %tr @@ -20,39 +17,34 @@ %td{:align => "right"} = item.quantity %td{:align => "right"} - = item.single_display_amount_with_adjustments + = item.included_tax > 0 ? item.display_included_tax : "" %td{:align => "right"} = item.display_amount_with_adjustments - - if @order.total_tax > 0 - %td{:align => "right"} - = display_line_item_tax_rates(item) - 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.included_tax > 0 ? adjustment.display_included_tax : "" %td{:align => "right"} = adjustment.display_amount - - if @order.total_tax > 0 - %td{:align => "right"} - = display_adjustment_tax_rates(adjustment) %tfoot %tr - %td{:align => "right", :colspan => "3"} - %strong= @order.has_taxes_included ? t(:total_incl_tax) : t(:total_excl_tax) %td{:align => "right", :colspan => "2"} - %strong= @order.has_taxes_included ? @order.display_total : display_checkout_total_less_tax(@order) - - display_checkout_taxes_hash(@order).each do |tax_rate, tax_value| - %tr - %td{:align => "right", :colspan => "3"} - = t(:tax_total, rate: tax_rate) - %td{:align => "right", :colspan => "2"} - = tax_value + %strong GST Total: + %td{:align => "right", :colspan => "2"} + %strong= display_checkout_tax_total(@order) %tr - %td{:align => "right", :colspan => "3"} - = @order.has_taxes_included ? t(:total_excl_tax) : t(:total_incl_tax) %td{:align => "right", :colspan => "2"} - = @order.has_taxes_included ? display_checkout_total_less_tax(@order) : @order.display_total + %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_table2.html.haml b/app/views/spree/admin/orders/_invoice_table2.html.haml new file mode 100644 index 0000000000..0941904d8b --- /dev/null +++ b/app/views/spree/admin/orders/_invoice_table2.html.haml @@ -0,0 +1,58 @@ +%table.order-summary{:width => "100%"} + %thead + %tr + %th{:align => "left"} + %h5= t(:invoice_column_item) + %th{:align => "right", :width => "15%"} + %h5= t(:invoice_column_qty) + %th{:align => "right", :width => "15%"} + %h5= @order.has_taxes_included ? t(:invoice_column_unit_price_with_taxes) : t(:invoice_column_unit_price_without_taxes) + %th{:align => "right", :width => "15%"} + %h5= @order.has_taxes_included ? t(:invoice_column_price_with_taxes) : t(:invoice_column_price_without_taxes) + - if @order.total_tax > 0 + %th{:align => "right", :width => "15%"} + %h5= t(:invoice_column_tax_rate) + %tbody + - @order.line_items.sort_by{ |li| li.product.name }.each do |item| + %tr + %td + = render 'spree/shared/line_item_name', line_item: item + %td{:align => "right"} + = item.quantity + %td{:align => "right"} + = item.single_display_amount_with_adjustments + %td{:align => "right"} + = item.display_amount_with_adjustments + - if @order.total_tax > 0 + %td{:align => "right"} + = display_line_item_tax_rates(item) + - 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"} + %td{:align => "right"} + %td{:align => "right"} + = adjustment.display_amount + - if @order.total_tax > 0 + %td{:align => "right"} + = display_adjustment_tax_rates(adjustment) + %tfoot + %tr + %td{:align => "right", :colspan => "3"} + %strong= @order.has_taxes_included ? t(:total_incl_tax) : t(:total_excl_tax) + %td{:align => "right", :colspan => "2"} + %strong= @order.has_taxes_included ? @order.display_total : display_checkout_total_less_tax(@order) + - display_checkout_taxes_hash(@order).each do |tax_rate, tax_value| + %tr + %td{:align => "right", :colspan => "3"} + = t(:tax_total, rate: tax_rate) + %td{:align => "right", :colspan => "2"} + = tax_value + %tr + %td{:align => "right", :colspan => "3"} + = @order.has_taxes_included ? t(:total_excl_tax) : t(:total_incl_tax) + %td{:align => "right", :colspan => "2"} + = @order.has_taxes_included ? display_checkout_total_less_tax(@order) : @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 a7356309e2..9e8aa8270f 100644 --- a/app/views/spree/admin/orders/invoice.html.haml +++ b/app/views/spree/admin/orders/invoice.html.haml @@ -2,55 +2,40 @@ %table{:width => "100%"} %tbody + %tr{ valign: "top" } + %td{ :align => "left", colspan: 3 } + %h6= "Issued on: #{Time.zone.now.strftime("%F")}" %tr{ valign: "top" } %td{ :align => "left" } %h4 - = t(:tax_invoice) - - if @order.distributor.display_invoice_logo? && @order.distributor.logo.present? - %td{ :align => "right", rowspan: 2 } - = wicked_pdf_image_tag @order.distributor.logo(:small), width: 150, height: 150 + = "TAX INVOICE: " + = "#{@order.number}" + %td{width: "10%" } +   + %td{ :align => "right" } + %h4= @order.order_cycle.andand.name %tr{ valign: "top" } %td{ :align => "left" } - %strong= @order.distributor.name - %br - = @order.distributor.address.address_part1 - %br - = @order.distributor.address.address_part2 - %br - = @order.distributor.email_address - - if @order.distributor.phone.present? - %br - = @order.distributor.phone + %strong= "From: #{@order.distributor.name}" - if @order.distributor.abn.present? %br - = "#{t :abn} #{@order.distributor.abn}" - - if @order.distributor.acn.present? - %br - = "#{t :acn} #{@order.distributor.acn}" - %tr{ valign: "top" } - %td{ :align => "left", colspan: 2 }   - %tr{ valign: "top" } - %td{ :align => "left" } + = "ABN: #{@order.distributor.abn}" %br - = t :invoice_issued_on - = l Time.zone.now.to_date + = @order.distributor.address.full_address %br - = t :date_of_transaction - = l @order.completed_at.to_date - %br - = t :order_number - = @order.number + = @order.distributor.email + %td{width: "10%" } +   %td{ :align => "right" } - = t :invoice_billing_address - %br - %strong= @order.ship_address.full_name + %strong= "To: #{@order.ship_address.full_name}" - if @order.customer.code.present? %br = "Code: #{@order.customer.code}" %br - = @order.ship_address.address_part1 + = @order.ship_address.full_address %br - = @order.ship_address.address_part2 + = "#{@order.customer.email}," + = "#{@order.bill_address.phone}" = render 'spree/admin/orders/invoice_table' @@ -63,8 +48,4 @@ %p   -- if @order.distributor.invoice_text.present? - %p - = @order.distributor.invoice_text - = render 'spree/order_mailer/payment' diff --git a/app/views/spree/admin/orders/invoice2.html.haml b/app/views/spree/admin/orders/invoice2.html.haml new file mode 100644 index 0000000000..89f28ab308 --- /dev/null +++ b/app/views/spree/admin/orders/invoice2.html.haml @@ -0,0 +1,71 @@ += wicked_pdf_stylesheet_link_tag "mail/all" + + +%table{:width => "100%"} + %tbody + %tr{ valign: "top" } + %td{ :align => "left" } + %h4 + = t :tax_invoice + - if @order.distributor.display_invoice_logo? && @order.distributor.logo.present? + %td{ :align => "right", rowspan: 2 } + = wicked_pdf_image_tag @order.distributor.logo(:small), width: 150, height: 150 + %tr{ valign: "top" } + %td{ :align => "left" } + %strong= @order.distributor.name + %br + = @order.distributor.address.address_part1 + %br + = @order.distributor.address.address_part2 + %br + = @order.distributor.email_address + - if @order.distributor.phone.present? + %br + = @order.distributor.phone + - if @order.distributor.abn.present? + %br + = "#{t :abn} #{@order.distributor.abn}" + - if @order.distributor.acn.present? + %br + = "#{t :acn} #{@order.distributor.acn}" + %tr{ valign: "top" } + %td{ :align => "left", colspan: 2 }   + %tr{ valign: "top" } + %td{ :align => "left" } + %br + = t :invoice_issued_on + = l Time.zone.now.to_date + %br + = t :date_of_transaction + = l @order.completed_at.to_date + %br + = t :order_number + = @order.number + %td{ :align => "right" } + = t :invoice_billing_address + %br + %strong= @order.ship_address.full_name + - if @order.customer.code.present? + %br + = "Code: #{@order.customer.code}" + %br + = @order.ship_address.address_part1 + %br + = @order.ship_address.address_part2 + += render 'spree/admin/orders/invoice_table2' + +- if @order.special_instructions.present? + %p.callout + %strong + = t :customer_instructions + %p + %em= @order.special_instructions + %p +   + +- if @order.distributor.invoice_text.present? + %p + = @order.distributor.invoice_text + += render 'spree/order_mailer/payment' diff --git a/config/locales/en.yml b/config/locales/en.yml index d2fe26e105..1031187aa7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -527,9 +527,15 @@ en: require_customer_login: "This shop is for customers only." require_login_html: "Please %{login} if you have an account already. Otherwise, %{register} to become a customer." require_customer_html: "Please %{contact} %{enterprise} to become a customer." + invoice_settings: + edit: + invoice_settings: Invoice + invoice_style2?: Use the alternative invoice model that includes total tax breakdown per rate and tax rate info per item (not yet suitable for countries displaying prices excluding tax) # Printable Invoice Columns invoice_billing_address: "Billing address:" + invoice_column_tax: "GST" + invoice_column_price: "Price" invoice_column_item: "Item" invoice_column_qty: "Qty" invoice_column_unit_price_with_taxes: "Unit price (Incl. tax)" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index b30dc6a818..6a5f04a021 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -464,6 +464,10 @@ fr: shared: user_guide_link: user_guide: Guide l'utilisateur + invoice_settings: + edit: + invoice_settings: Apparence de la facture + invoice_style2?: Utiliser le modèle de facture alternaltif qui comprend une séparation des taxes par taux et une indication du taux appliqué par item (pas encore adapté pour les pays affichant des prix hors taxe) home: hubs: @@ -484,6 +488,8 @@ fr: require_customer_html: "Veuillez %{contact} %{enterprise} pour devenir membre." invoice_billing_address: "Adresse de facturation :" + invoice_column_tax: "TVA" + invoice_column_price: "Prix" invoice_column_item: "Produit" invoice_column_qty: "Qté" invoice_column_unit_price_with_taxes: "Prix unitaire (TTC)" diff --git a/config/routes.rb b/config/routes.rb index 607e6308a2..ed344f1288 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -145,6 +145,8 @@ Openfoodnetwork::Application.routes.draw do resources :column_preferences, only: [], format: :json do put :bulk_update, on: :collection end + + resource :invoice_settings, only: [:edit, :update] end namespace :api do