Class: VoucherTransaction

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

Overview

Schema Information

Table name: voucher_transactions

id                   :bigint           not null, primary key
active               :boolean          default(TRUE)
cancelled_by         :string(191)
payment_type         :string(191)
total_price_cents    :integer          default(0), not null
total_price_currency :string(191)      default("THB"), not null
created_at           :datetime         not null
updated_at           :datetime         not null
guest_id             :bigint
user_id              :bigint

Indexes

index_voucher_transactions_on_created_at  (created_at)
index_voucher_transactions_on_guest_id    (guest_id)
index_voucher_transactions_on_user_id     (user_id)

Instance Method Summary collapse

Methods inherited from ApplicationRecord

sync_carrierwave_url

Instance Method Details

#firebase_tracking_keyString

Returns the Firebase tracking key for this voucher transaction Used for real-time status updates in Firebase Realtime Database

Returns:

  • (String)

    Firebase path for this transaction



125
126
127
# File 'app/models/voucher_transaction.rb', line 125

def firebase_tracking_key
  "voucher_transaction/#{id}"
end

#mark_vouchers_as_active!Object

Raises:

  • (ActiveRecord::RecordInvalid)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/voucher_transaction.rb', line 55

def mark_vouchers_as_active!
  return false if vouchers.blank?

  success = false
  ActiveRecord::Base.transaction do
    vouchers.each { |rv| rv.update!(active: true) }
    success = true
  end
  raise ActiveRecord::RecordInvalid unless success

  true
end


129
130
131
132
133
# File 'app/models/voucher_transaction.rb', line 129

def paid_charges
  return @paid_charges if defined?(@paid_charges)

  @paid_charges = charges.success_scope
end

#payment_failed_urlObject



117
118
119
120
# File 'app/models/voucher_transaction.rb', line 117

def payment_failed_url
  url = AdminSetting.web_v2_host
  "#{url}/restaurants/hungryhub-voucher/payment-failed?type=voucher"
end

#payment_gatewayObject



68
69
70
# File 'app/models/voucher_transaction.rb', line 68

def payment_gateway
  'Hungry Hub'
end

#payment_statusObject



81
82
83
# File 'app/models/voucher_transaction.rb', line 81

def payment_status
  status_as_symbol.to_s.humanize
end

#payment_success_urlObject



112
113
114
115
# File 'app/models/voucher_transaction.rb', line 112

def payment_success_url
  url = AdminSetting.web_v2_host
  "#{url}/restaurants/hungryhub-voucher/payment-success/#{encrypted_id}?type=voucher"
end

#qr_code_for_paymentObject



76
77
78
79
# File 'app/models/voucher_transaction.rb', line 76

def qr_code_for_payment
  charge = charges.present? && charges.pending_scope.promptpay_source.last
  charge.source.qr_code.url if charge
end

#qr_code_for_payment_expired_atObject



72
73
74
# File 'app/models/voucher_transaction.rb', line 72

def qr_code_for_payment_expired_at
  created_at + AdminSetting.prompt_pay_count_down_in_minute.to_i.minute
end

#qrcode_data_urlObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/voucher_transaction.rb', line 95

def qrcode_data_url
  qrcode = RQRCode::QRCode.new(id.to_s)
  png = qrcode.as_png(
    bit_depth: 1,
    border_modules: 4,
    color_mode: ChunkyPNG::COLOR_GRAYSCALE,
    color: 'black',
    file: nil,
    fill: 'white',
    module_px_size: 6,
    resize_exactly_to: false,
    resize_gte_to: false,
    size: 500,
  )
  png.to_data_url
end

#set_total_priceObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/voucher_transaction.rb', line 39

def set_total_price
  return if vouchers.blank?

  # Determine currency from vouchers (they should all have the same currency)
  # Use amount_currency attribute directly instead of Money object's currency
  # because vouchers might not be persisted yet when this runs
  voucher_currency = vouchers.first&.amount_currency ||
    vouchers.first&.amount&.currency ||
    Country::THAI_CURRENCY_CODE
  self.total_price_currency = voucher_currency.to_s

  # Sum up the amounts in cents
  total_cents = vouchers.sum { |v| v.amount_cents || 0 }
  self.total_price_cents = total_cents
end

#status_as_symbolObject



85
86
87
88
89
90
91
92
93
# File 'app/models/voucher_transaction.rb', line 85

def status_as_symbol
  @status_as_symbol ||= if !active? && paid_charges.blank?
                          :cancelled
                        elsif paid_charges.present?
                          :paid
                        else
                          :waiting_for_payment
                        end
end