1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/app/models/avoir.rb

58 lines
1.9 KiB
Ruby
Raw Normal View History

2019-01-09 16:28:23 +01:00
# frozen_string_literal: true
# Avoir is a special type of Invoice, which it inherits. It is used to
# refund an user, based on a previous invoice, or to credit an user's wallet.
2016-03-23 18:39:41 +01:00
class Avoir < Invoice
belongs_to :invoice
2019-01-09 16:28:23 +01:00
validates :avoir_mode, inclusion: { in: %w[stripe cheque transfer none cash wallet] }
2016-03-23 18:39:41 +01:00
attr_accessor :invoice_items_ids
def generate_reference
pattern = Setting.find_by(name: 'invoice_reference').value
2016-03-23 18:39:41 +01:00
# invoice number per day (dd..dd)
reference = pattern.gsub(/d+(?![^\[]*\])/) do |match|
pad_and_truncate(number_of_invoices('day'), match.to_s.length)
end
# invoice number per month (mm..mm)
reference.gsub!(/m+(?![^\[]*\])/) do |match|
pad_and_truncate(number_of_invoices('month'), match.to_s.length)
end
# invoice number per year (yy..yy)
reference.gsub!(/y+(?![^\[]*\])/) do |match|
pad_and_truncate(number_of_invoices('year'), match.to_s.length)
end
# full year (YYYY)
reference.gsub!(/YYYY(?![^\[]*\])/, created_at.strftime('%Y'))
2016-03-23 18:39:41 +01:00
# year without century (YY)
reference.gsub!(/YY(?![^\[]*\])/, created_at.strftime('%y'))
2016-03-23 18:39:41 +01:00
# abbreviated month name (MMM)
reference.gsub!(/MMM(?![^\[]*\])/, created_at.strftime('%^b'))
2016-03-23 18:39:41 +01:00
# month of the year, zero-padded (MM)
reference.gsub!(/MM(?![^\[]*\])/, created_at.strftime('%m'))
2016-03-23 18:39:41 +01:00
# month of the year, non zero-padded (M)
reference.gsub!(/M(?![^\[]*\])/, created_at.strftime('%-m'))
2016-03-23 18:39:41 +01:00
# day of the month, zero-padded (DD)
reference.gsub!(/DD(?![^\[]*\])/, created_at.strftime('%d'))
2016-03-23 18:39:41 +01:00
# day of the month, non zero-padded (DD)
reference.gsub!(/DD(?![^\[]*\])/, created_at.strftime('%-d'))
2016-03-23 18:39:41 +01:00
# information about refund/avoir (R[text])
reference.gsub!(/R\[([^\]]+)\]/, '\1')
# remove information about online selling (X[text])
reference.gsub!(/X\[([^\]]+)\]/, ''.to_s)
self.reference = reference
end
def expire_subscription
user.subscription.expire(Time.now)
end
end