From aa4798d35c8ba3e516c209574d022a0491e318b4 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Fri, 14 Jul 2023 23:51:40 +0100 Subject: [PATCH] add adjustables to the list of objects to serialize when creating an invoice --- .../invoice/data_presenter/adjustable.rb | 21 +++++++++++++++++++ .../invoice/data_presenter/adjustment.rb | 3 ++- .../invoice/adjustable_serializer.rb | 18 ++++++++++++++++ .../invoice/adjustment_serializer.rb | 1 + .../admin/orders/_invoice_table3.html.haml | 4 ++-- 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 app/models/invoice/data_presenter/adjustable.rb create mode 100644 app/serializers/invoice/adjustable_serializer.rb diff --git a/app/models/invoice/data_presenter/adjustable.rb b/app/models/invoice/data_presenter/adjustable.rb new file mode 100644 index 0000000000..299732d77f --- /dev/null +++ b/app/models/invoice/data_presenter/adjustable.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: false + +class Invoice + class DataPresenter + class Adjustable < Invoice::DataPresenter::Base + attributes :id, :type, :currency, :included_tax_total, :additional_tax_total, :amount + + def display_taxes(display_zero: false) + if included_tax_total.positive? + amount = Spree::Money.new(included_tax_total, currency: currency) + I18n.t(:tax_amount_included, amount: amount) + elsif additional_tax_total.positive? + Spree::Money.new(additional_tax_total, currency: currency) + elsif display_zero + Spree::Money.new(0.00, currency: currency) + end + end + + end + end +end \ No newline at end of file diff --git a/app/models/invoice/data_presenter/adjustment.rb b/app/models/invoice/data_presenter/adjustment.rb index ad5db4bded..8ec4ad414c 100644 --- a/app/models/invoice/data_presenter/adjustment.rb +++ b/app/models/invoice/data_presenter/adjustment.rb @@ -7,7 +7,8 @@ class Invoice attributes :additional_tax_total, :adjustable_type, :amount, :currency, :included_tax_total, :label array_attribute :tax_rates, class_name: 'TaxRate' - attributes_with_presenter :originator, class_name: :AdjustmentOriginator + attributes_with_presenter :originator, class_name: 'AdjustmentOriginator' + attributes_with_presenter :adjustable invoice_generation_attributes :additional_tax_total, :adjustable_type, :amount, :included_tax_total invoice_update_attributes :label diff --git a/app/serializers/invoice/adjustable_serializer.rb b/app/serializers/invoice/adjustable_serializer.rb new file mode 100644 index 0000000000..fd9d5c255a --- /dev/null +++ b/app/serializers/invoice/adjustable_serializer.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: false + +class Invoice + class AdjustableSerializer < ActiveModel::Serializer + attributes :id, :type, :currency, :included_tax_total, :additional_tax_total, :amount + def type + object.class.name + end + + [:currency, :included_tax_total, :additional_tax_total, :amount].each do |method| + define_method method do + return nil unless object.respond_to?(method) + + object.public_send(method).to_f + end + end + end +end diff --git a/app/serializers/invoice/adjustment_serializer.rb b/app/serializers/invoice/adjustment_serializer.rb index dd5f8efe6b..daf46294cf 100644 --- a/app/serializers/invoice/adjustment_serializer.rb +++ b/app/serializers/invoice/adjustment_serializer.rb @@ -5,6 +5,7 @@ class Invoice attributes :adjustable_type, :label, :included_tax_total, :additional_tax_total, :amount, :currency has_one :originator, serializer: Invoice::AdjustmentOriginatorSerializer + has_one :adjustable, serializer: Invoice::AdjustableSerializer has_many :tax_rates, serializer: Invoice::TaxRateSerializer def tax_rates diff --git a/app/views/spree/admin/orders/_invoice_table3.html.haml b/app/views/spree/admin/orders/_invoice_table3.html.haml index 77dfc3f292..299539d964 100644 --- a/app/views/spree/admin/orders/_invoice_table3.html.haml +++ b/app/views/spree/admin/orders/_invoice_table3.html.haml @@ -25,14 +25,14 @@ = item.display_amount_with_adjustments - @order.checkout_adjustments(exclude: [:line_item]).reverse_each do |adjustment| - - taxable = adjustment#.adjustable_type == "Spree::Shipment" ? adjustment.adjustable : adjustment + - taxable = adjustment.adjustable_type == "Spree::Shipment" ? adjustment.adjustable : adjustment %tr %td %strong= "#{raw(adjustment.label)}" %td{:align => "right"} 1 %td{:align => "right"} - = adjustment.display_taxes + = taxable.display_taxes %td{:align => "right"} = adjustment.display_amount %tfoot