Class: VoucherDeductible

Inherits:
ApplicationRecord show all
Defined in:
app/models/voucher_deductible.rb

Overview

Schema Information

Table name: voucher_deductibles

id                    :bigint           not null, primary key
usage_amount_cents    :integer          default(0), not null
usage_amount_currency :string(191)      default("THB"), not null
created_at            :datetime         not null
updated_at            :datetime         not null
reservation_id        :bigint
user_id               :bigint
voucher_id            :bigint

Indexes

index_voucher_deductibles_on_reservation_id  (reservation_id)
index_voucher_deductibles_on_user_id         (user_id)
index_voucher_deductibles_on_voucher_id      (voucher_id)

Class Method Summary collapse

Methods inherited from ApplicationRecord

sync_carrierwave_url

Class Method Details

.amount_balance(voucher_id) ⇒ Object



26
27
28
29
30
31
32
33
# File 'app/models/voucher_deductible.rb', line 26

def self.amount_balance(voucher_id)
  voucher = Voucher.find voucher_id
  vd = VoucherDeductible.where(voucher: voucher)
  voucher_amount = voucher.amount.amount.to_i
  return voucher_amount if vd.blank?

  [voucher_amount - vd.sum(&:usage_amount).amount.to_i, 0].max
end