2019-06-03 16:00:09 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# user's virtual wallet which can be credited by an admin
|
|
|
|
# all subsequent user's transactions will charge the wallet, as the default payment mean, if the wallet amount > 0
|
|
|
|
# if the wallet amount is not sufficient, a secondary payment mean will be requested (card or cash, depending on the login context)
|
2020-03-25 10:16:47 +01:00
|
|
|
class Wallet < ApplicationRecord
|
2016-07-05 12:30:52 +02:00
|
|
|
include AmountConcern
|
|
|
|
|
2019-06-03 16:00:09 +02:00
|
|
|
belongs_to :invoicing_profile
|
2016-07-05 12:30:52 +02:00
|
|
|
has_many :wallet_transactions, dependent: :destroy
|
2016-07-04 19:20:10 +02:00
|
|
|
|
2019-06-03 16:00:09 +02:00
|
|
|
validates :invoicing_profile, presence: true
|
2016-07-05 11:32:06 +02:00
|
|
|
|
2022-10-11 11:43:32 +02:00
|
|
|
delegate :user, to: :invoicing_profile
|
|
|
|
|
2016-07-04 19:20:10 +02:00
|
|
|
def credit(amount)
|
2019-06-03 16:00:09 +02:00
|
|
|
if amount.is_a?(Numeric) && amount >= 0
|
2022-12-12 16:25:29 +01:00
|
|
|
self.amount = (BigDecimal(self.amount.to_s) + BigDecimal(amount.to_s)).to_f
|
2016-07-05 10:38:39 +02:00
|
|
|
return save
|
|
|
|
end
|
|
|
|
false
|
2016-07-04 19:20:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def debit(amount)
|
2019-06-03 16:00:09 +02:00
|
|
|
if amount.is_a?(Numeric) && amount >= 0
|
2022-12-12 16:25:29 +01:00
|
|
|
self.amount = (BigDecimal(self.amount.to_s) - BigDecimal(amount.to_s)).to_f
|
2016-07-05 10:38:39 +02:00
|
|
|
return save
|
|
|
|
end
|
|
|
|
false
|
2016-07-04 19:20:10 +02:00
|
|
|
end
|
2016-07-18 18:16:54 +02:00
|
|
|
end
|