1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

fix create invoice for each due date

This commit is contained in:
Sylvain 2021-01-25 14:37:07 +01:00
parent 26636254bd
commit 01a0612a4b
8 changed files with 41 additions and 17 deletions

View File

@ -9,6 +9,7 @@ class PaymentSchedule < PaymentDocument
belongs_to :wallet_transaction
belongs_to :coupon
belongs_to :invoicing_profile
belongs_to :statistic_profile
belongs_to :operator_profile, foreign_key: :operator_profile_id, class_name: 'InvoicingProfile'
belongs_to :subscription, foreign_type: 'Subscription', foreign_key: 'scheduled_id'
@ -19,6 +20,7 @@ class PaymentSchedule < PaymentDocument
before_create :add_environment
after_create :update_reference, :chain_record
after_commit :generate_and_send_document, on: [:create], if: :persisted?
after_commit :generate_initial_invoice, on: [:create], if: :persisted?
def file
dir = "payment_schedules/#{invoicing_profile.id}"
@ -74,4 +76,8 @@ class PaymentSchedule < PaymentDocument
end
PaymentScheduleWorker.perform_async(id)
end
def generate_initial_invoice
PaymentScheduleItemWorker.perform_async
end
end

View File

@ -11,6 +11,6 @@ class PaymentScheduleItem < Footprintable
end
def self.columns_out_of_footprint
%w[invoice_id]
%w[invoice_id stp_invoice_id]
end
end

View File

@ -62,6 +62,7 @@ class PaymentScheduleService
ps.stp_setup_intent_id = setup_intent_id
ps.operator_profile = operator.invoicing_profile
ps.invoicing_profile = user.invoicing_profile
ps.statistic_profile = user.statistic_profile
ps.payment_schedule_items = items
items.each do |item|
item.payment_schedule = ps
@ -78,7 +79,7 @@ class PaymentScheduleService
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,
statistic_profile: payment_schedule_item.payment_schedule.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
@ -97,7 +98,7 @@ class PaymentScheduleService
# save the results
invoice.save
payment_schedule_item.update_attributes(invoice_id: invoice.id)
payment_schedule_item.update_attributes(invoice_id: invoice.id, stp_invoice_id: stp_invoice.id)
end
private
@ -108,16 +109,14 @@ class PaymentScheduleService
##
def complete_first_invoice(payment_schedule_item, invoice)
# sub-prices for the subscription and the reservation
details = {}
if payment_schedule_item.payment_schedule.scheduled_type == Subscription.name
details[:subscription] = payment_schedule_item.details['recurring'] + payment_schedule_item.details['adjustment']
else
details[:reservation] = payment_schedule_item.details['other_items']
end
details = {
subscription: payment_schedule_item.details['recurring'] + payment_schedule_item.details['adjustment']
}
# the subscription and reservation items
subscription = Subscription.find(payment_schedule_item.details['subscription_id'])
if payment_schedule_item.payment_schedule.scheduled_type == Reservation.name
details[:reservation] = payment_schedule_item.details['other_items']
reservation = payment_schedule_item.payment_schedule.scheduled
end
@ -157,7 +156,7 @@ class PaymentScheduleService
def generate_reservation_item(invoice, reservation, payment_details)
raise TypeError unless [Space, Machine, Training].include? reservation.reservable.class
description = reservation.reservable.name
description = "#{reservation.reservable.name}\n"
reservation.slots.each do |slot|
description += " #{I18n.l slot.start_at, format: :long} - #{I18n.l slot.end_at, format: :hour_minute}\n"
end

View File

@ -6,7 +6,7 @@ class PaymentScheduleItemWorker
include Sidekiq::Worker
def perform
PaymentScheduleItem.where(due_date: [DateTime.current.at_beginning_of_day, DateTime.current.end_of_day], state: 'new').each do |psi|
PaymentScheduleItem.where(state: 'new').where('due_date < ?', DateTime.current.end_of_day).each do |psi|
# the following depends on the payment method (stripe/check)
if psi.payment_schedule.payment_method == 'stripe'
### Stripe

View File

@ -48,7 +48,7 @@ version_check:
queue: system
payment_schedule_item:
cron: "0 5 * * *" # every day at 5:00 AM
cron: "0 23 * * *" # every day at 11pm
class: 'PaymentScheduleItemWorker'
queue: default

View File

@ -17,6 +17,7 @@ class CreatePaymentSchedules < ActiveRecord::Migration[5.2]
t.string :footprint
t.string :environment
t.belongs_to :invoicing_profile, foreign_key: true
t.belongs_to :statistic_profile, foreign_key: true
t.references :operator_profile, foreign_key: { to_table: 'invoicing_profiles' }
t.timestamps

View File

@ -8,6 +8,7 @@ class CreatePaymentScheduleItems < ActiveRecord::Migration[5.2]
t.datetime :due_date
t.string :state, default: 'new'
t.jsonb :details, default: '{}'
t.string :stp_invoice_id
t.belongs_to :payment_schedule, foreign_key: true
t.belongs_to :invoice, foreign_key: true
t.string :footprint

View File

@ -78,11 +78,11 @@ CREATE FUNCTION public.fill_search_vector_for_project() RETURNS trigger
select string_agg(description, ' ') as content into step_description from project_steps where project_id = new.id;
new.search_vector :=
setweight(to_tsvector('pg_catalog.simple', unaccent(coalesce(new.name, ''))), 'A') ||
setweight(to_tsvector('pg_catalog.simple', unaccent(coalesce(new.tags, ''))), 'B') ||
setweight(to_tsvector('pg_catalog.simple', unaccent(coalesce(new.description, ''))), 'D') ||
setweight(to_tsvector('pg_catalog.simple', unaccent(coalesce(step_title.title, ''))), 'C') ||
setweight(to_tsvector('pg_catalog.simple', unaccent(coalesce(step_description.content, ''))), 'D');
setweight(to_tsvector('pg_catalog.french', unaccent(coalesce(new.name, ''))), 'A') ||
setweight(to_tsvector('pg_catalog.french', unaccent(coalesce(new.tags, ''))), 'B') ||
setweight(to_tsvector('pg_catalog.french', unaccent(coalesce(new.description, ''))), 'D') ||
setweight(to_tsvector('pg_catalog.french', unaccent(coalesce(step_title.title, ''))), 'C') ||
setweight(to_tsvector('pg_catalog.french', unaccent(coalesce(step_description.content, ''))), 'D');
return new;
end
@ -1472,6 +1472,7 @@ CREATE TABLE public.payment_schedule_items (
due_date timestamp without time zone,
state character varying DEFAULT 'new'::character varying,
details jsonb DEFAULT '"{}"'::jsonb,
stp_invoice_id character varying,
payment_schedule_id bigint,
invoice_id bigint,
footprint character varying,
@ -1518,6 +1519,7 @@ CREATE TABLE public.payment_schedules (
footprint character varying,
environment character varying,
invoicing_profile_id bigint,
statistic_profile_id bigint,
operator_profile_id bigint,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
@ -4574,6 +4576,13 @@ CREATE INDEX index_payment_schedules_on_operator_profile_id ON public.payment_sc
CREATE INDEX index_payment_schedules_on_scheduled_type_and_scheduled_id ON public.payment_schedules USING btree (scheduled_type, scheduled_id);
--
-- Name: index_payment_schedules_on_statistic_profile_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_payment_schedules_on_statistic_profile_id ON public.payment_schedules USING btree (statistic_profile_id);
--
-- Name: index_payment_schedules_on_wallet_transaction_id; Type: INDEX; Schema: public; Owner: -
--
@ -5233,6 +5242,14 @@ ALTER TABLE ONLY public.exports
ADD CONSTRAINT fk_rails_26b155474a FOREIGN KEY (user_id) REFERENCES public.users(id);
--
-- Name: payment_schedules fk_rails_27cdd051f7; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.payment_schedules
ADD CONSTRAINT fk_rails_27cdd051f7 FOREIGN KEY (statistic_profile_id) REFERENCES public.statistic_profiles(id);
--
-- Name: statistic_profiles fk_rails_2c8874d1a1; Type: FK CONSTRAINT; Schema: public; Owner: -
--