Commit a157469c authored by gwendal's avatar gwendal
Browse files

do not invoice service with no chargeable item (fix #181)

parent d1bd2143
......@@ -243,6 +243,12 @@ class Service < ActiveRecord::Base
return records if self.first_invoicing_started_on.nil?
return records if self.service_sale_templates.empty?
# Ensure at least one service_sale_templates is chargeable
if service_sale_templates.where(chargeable: true).empty?
errors.add(:base, :no_chargeable_item)
return records
# Ensure that there is no open invoice for this service
if is_there_an_open_invoice_for_this_service?
errors.add(:base, :already_an_open_invoice)
......@@ -256,6 +262,7 @@ class Service < ActiveRecord::Base
return records
# For services not yet invoiced, set the last_invoicing_end_at to yesterday
# in order to bill them.
if last_invoicing_end_at.nil?
......@@ -383,6 +383,7 @@ fr:
already_a_pending_payment: "Il y a déjà un paiement en attente pour ce service"
already_an_open_invoice: "Il y a déjà une facture en brouillon pour ce service"
last_invoicing_end_at_after_end_of_month_chargeable: "La fin de facturation de ce service est postérieur au mois de facturation en cours"
no_chargeable_item: "Le service n'a pas d'item facturable"
<<: *service_errors
......@@ -610,6 +610,18 @@ RSpec.describe Service, type: :model do
it 'should not render anything if there is no chargeable item' do
@service = FactoryBot.create(
member: @member,
first_invoicing_started_on: - 5.month,
last_invoicing_end_at: Date.yesterday - 2.month,
@service.service_sale_templates.update_all(chargeable: false)
records = @service.generate_invoicing
expect(records).to be_empty
# it 'should use deposit account if possible' do
# @service = FactoryBot.create(
# :vpn_service,
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment