1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-30 19:52:20 +01:00

WIP: generate invoices for each payment schedule item

This commit is contained in:
Sylvain 2021-01-20 17:00:23 +01:00
parent 8adb569891
commit ecdec70755
3 changed files with 88 additions and 8 deletions

View File

@ -68,4 +68,68 @@ class PaymentScheduleService
end
ps
end
def generate_invoice(payment_schedule_item, stp_invoice = nil)
invoice = Invoice.new(
invoiced: payment_schedule_item.payment_schedule.scheduled,
invoicing_profile: payment_schedule_item.payment_schedule.invoicing_profile,
statistic_profile: payment_schedule_item.payment_schedule.user.statistic_profile,
operator_profile_id: payment_schedule_item.payment_schedule.operator_profile_id,
stp_payment_intent_id: stp_invoice&.payment_intent,
payment_method: stp_invoice ? 'stripe' : nil
)
generate_invoice_items(invoice, payment_schedule_item, reservation: reservation, subscription: subscription)
InvoicesService.set_total_and_coupon(invoice, user, payment_details[:coupon])
invoice
end
private
def generate_invoice_items(invoice, payment_details, reservation: nil, subscription: nil)
if reservation
case reservation.reservable
# === Event reservation ===
when Event
InvoicesService.generate_event_item(invoice, reservation, payment_details)
# === Space|Machine|Training reservation ===
else
InvoicesService.generate_generic_item(invoice, reservation, payment_details)
end
end
return unless subscription || reservation&.plan_id
subscription = reservation.generate_subscription if !subscription && reservation.plan_id
InvoicesService.generate_subscription_item(invoice, subscription, payment_details)
end
def generate_reservation_item(invoice, reservation, payment_details)
raise TypeError unless [Space, Machine, Training].include? reservation.reservable.class
reservation.slots.each do |slot|
description = reservation.reservable.name +
" #{I18n.l slot.start_at, format: :long} - #{I18n.l slot.end_at, format: :hour_minute}"
price_slot = payment_details[:elements][:slots].detect { |p_slot| p_slot[:start_at].to_time.in_time_zone == slot[:start_at] }
invoice.invoice_items.push InvoiceItem.new(
amount: price_slot[:price],
description: description
)
end
end
##
# Generate an InvoiceItem for the given subscription and save it in invoice.invoice_items.
# This method must be called only with a valid subscription
##
def self.generate_subscription_item(invoice, subscription, payment_details)
raise TypeError unless subscription
invoice.invoice_items.push InvoiceItem.new(
amount: payment_details[:elements][:plan],
description: subscription.plan.name,
subscription_id: subscription.id
)
end
end

View File

@ -9,16 +9,27 @@ class PaymentScheduleItemWorker
PaymentScheduleItem.where(due_date: [DateTime.current.at_beginning_of_day, DateTime.current.end_of_day], state: 'new').each do |psi|
# the following depends on the payment method (stripe/check)
if psi.payment_schedule.payment_method == 'stripe'
# TODO, if stripe:
# - verify the payment was successful
# - if not, alert the admins
# - if succeeded, generate the invoice
stripe_key = Setting.get('stripe_secret_key')
stp_suscription = Stripe::Subscription.retrieve(psi.payment_schedule.stp_subscription_id, api_key: stripe_key)
stp_invoice = Stripe::Invoice.retrieve(stp_suscription.latest_invoice, api_key: stripe_key)
if stp_invoice.status == 'paid'
PaymentScheduleService.new.generate_invoice(psi, stp_invoice)
psi.update_attributes(state: 'paid')
else
NotificationCenter.call type: 'notify_admin_payment_schedule_failed', # TODO
receiver: User.admins_and_managers,
attached_object: psi
NotificationCenter.call type: 'notify_member_payment_schedule_failed', # TODO
receiver: psi.payment_schedule.user,
attached_object: psi
psi.update_attributes(state: 'pending')
end
else
# TODO, if check:
# - alert the admins and the user that it is time to bank the check
# - generate the invoice
NotificationCenter.call type: 'notify_admin_payment_schedule_check', # TODO
receiver: User.admins_and_managers,
attached_object: psi
psi.update_attributes(state: 'pending')
end
# TODO, finally, in any cases, update the psi.state field according to the new status
end
end
end

View File

@ -47,4 +47,9 @@ version_check:
class: 'VersionCheckWorker'
queue: system
payment_schedule_item:
cron: "0 5 * * *" # every day at 5:00 AM
class: 'PaymentScheduleItemWorker'
queue: default
<%= PluginRegistry.insert_code('yml.schedule') %>