Commit 0c6f57f3 authored by gwendal's avatar gwendal
Browse files

hotfix: credit_note is now functionnal (+fix spec)

parent 6ec581ec
......@@ -202,7 +202,56 @@ class Invoice < InvoicingLedgerItem
end
public
def send_email
if open_or_pending?
errors.add(:base, :cannot_send_invoice_by_mail_if_invoice_is_not_closed)
return false
end
if self.recipient.send_invoice_by_mail
MemberMailer.invoice_email(self.recipient, self).deliver_now
else
true
end
end
def send_email_ca
raise if open_or_pending?
TreasrurerMailer.invoice_email(self.recipient, self).deliver_now
end
# self.send_email_to_treasurer_after_generate_invoices(invoice_count, invoice_records, error_count, error_records)
def self.send_email_to_treasurer_after_generate_invoices(invoice_count, invoice_records, error_count, error_records)
TreasurerMailer.generated_invoices(
invoice_count, invoice_records, error_count, error_records
).deliver_now
end
def set_invoice_as_payed_if_payment_is_enough
return if status != "closed"
if is_attached_payment_enough?
self.payed = true
else
self.payed = false
end
return true
end
# Get the remaining amount to sold this invoice (positive value)
def get_remaining_amount
ledger_ids = [self.id]
payment_ids = self.payment_ids
ledger_ids << payment_ids
summary = InvoicingLedgerItem.where(id: ledger_ids)
.where(currency: currency)
.account_summaries(self.recipient, :with_status => ["open", "closed", "pending", "cleared"])
return self.total_amount if summary.empty? or not (summary[0].has_key? self.currency.to_sym)
remaining_amount = summary[0][self.currency.to_sym].balance
return remaining_amount
end
def generate_deposit_payment
return if type == "MemberCreditNote"
return if status != "closed"
# TODO: Should not be a negative payment but rather an invoice...
records = []
......@@ -269,59 +318,13 @@ class Invoice < InvoicingLedgerItem
set_invoice_as_payed_if_payment_is_enough
end
def send_email
if open_or_pending?
errors.add(:base, :cannot_send_invoice_by_mail_if_invoice_is_not_closed)
return false
end
if self.recipient.send_invoice_by_mail
MemberMailer.invoice_email(self.recipient, self).deliver_now
else
true
end
end
def send_email_ca
raise if open_or_pending?
TreasrurerMailer.invoice_email(self.recipient, self).deliver_now
end
# self.send_email_to_treasurer_after_generate_invoices(invoice_count, invoice_records, error_count, error_records)
def self.send_email_to_treasurer_after_generate_invoices(invoice_count, invoice_records, error_count, error_records)
TreasurerMailer.generated_invoices(
invoice_count, invoice_records, error_count, error_records
).deliver_now
end
def set_invoice_as_payed_if_payment_is_enough
return if status != "closed"
if is_attached_payment_enough?
self.payed = true
else
self.payed = false
end
return true
end
# Get the remaining amount to sold this invoice (positive value)
def get_remaining_amount
ledger_ids = [self.id]
payment_ids = self.payment_ids
ledger_ids << payment_ids
summary = InvoicingLedgerItem.where(id: ledger_ids)
.where(currency: currency)
.account_summaries(self.recipient, :with_status => ["open", "closed", "pending", "cleared"])
return self.total_amount if summary.empty? or not (summary[0].has_key? self.currency.to_sym)
remaining_amount = - summary[0][self.currency.to_sym].balance
return remaining_amount
end
def generate_payment
return true if is_attached_payment_enough?
return if open_or_pending?
remaining_amount = get_remaining_amount
if remaining_amount != total_amount
if remaining_amount.abs() != total_amount.abs()
errors.add(:base, :invoice_has_already_some_payment_so_i_dont_know_how_to_attribute_remaining_payment)
return
end
......@@ -361,7 +364,7 @@ class Invoice < InvoicingLedgerItem
deposit: false,
payment_method: self.payment_method,
invoice_ids: [self.id],
total_amount: remaining_amount,
# total_amount: remaining_amount,
subscription: self.subscription
)
records << payment
......
......@@ -103,7 +103,7 @@ class SendMail
Utils::Logging::TASKS_LOGGER.debug "* Would send reminder for #{object.to_s}"
else
Utils::Logging::TASKS_LOGGER.debug "* Send reminder for #{object.to_s}"
amount = invoices.reduce(0) {|sum, invoice| sum + invoice.get_remaining_amount}
amount = invoices.reduce(0) {|sum, invoice| sum + invoice.get_remaining_amount.abs()}
if object.kind_of? Service
if object.monthly_recurring_charge * service_balance_ratio <= amount
MemberMailer.remind_unpaid_service(object, invoices).deliver_now
......
......@@ -49,7 +49,7 @@ describe "send_mail:notify_unpaid" do
invoices << FactoryBot.create(:member_invoice_with_service_sale,
issue_date: Date.today - 3.month,
recipient: member)
invoices.last.status = "closed"
invoices.last.prepare_to_close
invoices.last.save
end
......
......@@ -47,7 +47,11 @@ Rspec.describe ServiceSale, type: :model do
describe :to_invoicing_export do
it 'should be exportable' do
@tax_rate = FactoryBot.create(:tax_rate)
if Settings.ledger_item.taxes
@tax_rate = FactoryBot.create(:tax_rate)
else
@tax_rate = nil
end
@sale = FactoryBot.create(
:service_sale,
ledger_item: @invoice,
......
......@@ -546,8 +546,8 @@ RSpec.describe Service, type: :model do
it 'should return the link to the help if defined in config' do
expected = {
dsl_service: "example.org/dsl_help_page",
radio_service: nil,
vpn_service: nil,
radio_service: "example.org/radio_help_page",
vpn_service: "example.org/vpn_help_page",
}
all.each do |type|
service = FactoryBot.build(type, member: @member)
......
Markdown is supported
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