add adjustables to the list of objects to serialize when creating an invoice

This commit is contained in:
Mohamed ABDELLANI
2023-07-14 23:51:40 +01:00
parent fc519da83b
commit aa4798d35c
5 changed files with 44 additions and 3 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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