2023-01-05 12:09:16 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# module definition
|
|
|
|
module Invoices; end
|
|
|
|
|
|
|
|
# Build a label for the given invoice item
|
|
|
|
class Invoices::ItemLabelService
|
|
|
|
class << self
|
|
|
|
# @param invoice [Invoice]
|
|
|
|
# @param item [InvoiceItem]
|
|
|
|
# @return [String]
|
|
|
|
def build(invoice, item)
|
|
|
|
details = invoice.is_a?(Avoir) ? "#{I18n.t('invoices.cancellation')} - " : ''
|
|
|
|
|
|
|
|
if item.object_type == Subscription.name
|
|
|
|
"#{details}#{build_subscription_label(invoice, item)}"
|
|
|
|
elsif item.object_type == Reservation.name
|
|
|
|
"#{details}#{build_reservation_label(invoice, item)}"
|
|
|
|
else
|
|
|
|
"#{details}#{item.description}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# @param invoice [Invoice]
|
|
|
|
# @param item [InvoiceItem]
|
|
|
|
# @return [String]
|
|
|
|
def build_subscription_label(invoice, item)
|
|
|
|
subscription = item.object
|
|
|
|
label = if invoice.main_item&.object_type == 'OfferDay'
|
|
|
|
I18n.t('invoices.subscription_extended_for_free_from_START_to_END',
|
2023-02-24 17:26:55 +01:00
|
|
|
**{ START: I18n.l(invoice.main_item&.object&.start_at&.to_date),
|
|
|
|
END: I18n.l(invoice.main_item&.object&.end_at&.to_date) })
|
2023-01-05 12:09:16 +01:00
|
|
|
else
|
|
|
|
subscription_end_at = subscription.expiration_date
|
|
|
|
subscription_start_at = subscription_end_at - subscription.plan.duration
|
|
|
|
I18n.t('invoices.subscription_NAME_from_START_to_END',
|
2023-02-24 17:26:55 +01:00
|
|
|
**{ NAME: item.description,
|
|
|
|
START: I18n.l(subscription_start_at.to_date),
|
|
|
|
END: I18n.l(subscription_end_at.to_date) })
|
2023-01-05 12:09:16 +01:00
|
|
|
end
|
|
|
|
unless invoice.payment_schedule_item.nil?
|
|
|
|
dues = invoice.payment_schedule_item.payment_schedule.payment_schedule_items.order(:due_date)
|
|
|
|
label += "\n #{I18n.t('invoices.from_payment_schedule',
|
2023-02-24 17:26:55 +01:00
|
|
|
**{ NUMBER: dues.index(invoice.payment_schedule_item) + 1,
|
|
|
|
TOTAL: dues.count,
|
|
|
|
DATE: I18n.l(invoice.payment_schedule_item.due_date.to_date),
|
|
|
|
SCHEDULE: invoice.payment_schedule_item.payment_schedule.reference })}"
|
2023-01-05 12:09:16 +01:00
|
|
|
end
|
|
|
|
label
|
|
|
|
end
|
|
|
|
|
|
|
|
# @param invoice [Invoice]
|
|
|
|
# @param item [InvoiceItem]
|
|
|
|
# @return [String]
|
|
|
|
def build_reservation_label(invoice, item)
|
|
|
|
case invoice.main_item&.object.try(:reservable_type)
|
|
|
|
### Machine reservation
|
|
|
|
when 'Machine'
|
2023-02-24 17:26:55 +01:00
|
|
|
I18n.t('invoices.machine_reservation_DESCRIPTION', **{ DESCRIPTION: item.description })
|
2023-01-05 12:09:16 +01:00
|
|
|
when 'Space'
|
2023-02-24 17:26:55 +01:00
|
|
|
I18n.t('invoices.space_reservation_DESCRIPTION', **{ DESCRIPTION: item.description })
|
2023-01-05 12:09:16 +01:00
|
|
|
### Training reservation
|
|
|
|
when 'Training'
|
2023-02-24 17:26:55 +01:00
|
|
|
I18n.t('invoices.training_reservation_DESCRIPTION', **{ DESCRIPTION: item.description })
|
2023-01-05 12:09:16 +01:00
|
|
|
### events reservation
|
|
|
|
when 'Event'
|
|
|
|
build_event_reservation_label(invoice, item)
|
|
|
|
else
|
|
|
|
item.description
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# @param invoice [Invoice]
|
|
|
|
# @param item [InvoiceItem]
|
|
|
|
# @return [String]
|
|
|
|
def build_event_reservation_label(invoice, item)
|
2023-02-24 17:26:55 +01:00
|
|
|
label = I18n.t('invoices.event_reservation_DESCRIPTION', **{ DESCRIPTION: item.description })
|
2023-01-05 12:09:16 +01:00
|
|
|
# details of the number of tickets
|
|
|
|
if invoice.main_item&.object&.nb_reserve_places&.positive?
|
2023-02-24 17:26:55 +01:00
|
|
|
label += "\n #{I18n.t('invoices.full_price_ticket', **{ count: invoice.main_item&.object&.nb_reserve_places })}"
|
2023-01-05 12:09:16 +01:00
|
|
|
end
|
|
|
|
invoice.main_item&.object&.tickets&.each do |t|
|
|
|
|
label += "\n #{I18n.t('invoices.other_rate_ticket',
|
2023-02-24 17:26:55 +01:00
|
|
|
**{ count: t.booked,
|
|
|
|
NAME: t.event_price_category.price_category.name })}"
|
2023-01-05 12:09:16 +01:00
|
|
|
end
|
|
|
|
label
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|