Class: VoucherForm::Apply
- Inherits:
-
Object
- Object
- VoucherForm::Apply
- Includes:
- DefaultErrorContainer, MoneyRails::ActionViewExtension
- Defined in:
- app/forms/voucher_form/apply.rb
Overview
typed: ignore frozen_string_literal: true
Instance Attribute Summary collapse
-
#actor ⇒ Object
readonly
Returns the value of attribute actor.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#data ⇒ Object
Returns the value of attribute data.
-
#param ⇒ Object
readonly
Returns the value of attribute param.
-
#vouchers ⇒ Object
Returns the value of attribute vouchers.
Instance Method Summary collapse
- #calculate ⇒ Object
-
#initialize(param, actor, current_user = nil) ⇒ Apply
constructor
but what we need is admin checking.
- #validate ⇒ Object
- #vouchers_amount ⇒ Object
- #vouchers_amount_v2 ⇒ Object
Methods included from DefaultErrorContainer
#error, #error_message_simple, #merge_errors
Constructor Details
#initialize(param, actor, current_user = nil) ⇒ Apply
but what we need is admin checking
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/forms/voucher_form/apply.rb', line 42 def initialize(param, actor, current_user = nil) @param = param.to_h.with_indifferent_access @country_code = @param[:country_code].presence || restaurant.country&.alpha3 @country_code = @country_code&.upcase @data = [] Array.wrap(param[:voucher_code]).each do |voucher_code| @data.push( voucher_code: voucher_code, voucher: find_voucher(voucher_code), ) end @actor = actor.to_sym @current_user = current_user raise 'Invalid actor' unless %i[admin restaurant_staff owner user].include?(@actor) end |
Instance Attribute Details
#actor ⇒ Object (readonly)
Returns the value of attribute actor.
8 9 10 |
# File 'app/forms/voucher_form/apply.rb', line 8 def actor @actor end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
8 9 10 |
# File 'app/forms/voucher_form/apply.rb', line 8 def country_code @country_code end |
#data ⇒ Object
Returns the value of attribute data.
9 10 11 |
# File 'app/forms/voucher_form/apply.rb', line 9 def data @data end |
#param ⇒ Object (readonly)
Returns the value of attribute param.
8 9 10 |
# File 'app/forms/voucher_form/apply.rb', line 8 def param @param end |
#vouchers ⇒ Object
Returns the value of attribute vouchers.
9 10 11 |
# File 'app/forms/voucher_form/apply.rb', line 9 def vouchers @vouchers end |
Instance Method Details
#calculate ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'app/forms/voucher_form/apply.rb', line 185 def calculate return {} if vouchers.blank? @calculate ||= begin support_dynamic_pricing_feature = true date_param = if param[:date].present? if param[:date].is_a?(String) param[:date].to_date else param[:date] end else HH_LOGGER.debug('Date is not present in voucher calculation', caller: caller) support_dynamic_pricing_feature = false Date.current_date end calc = HhPackage::ReservationPackages::ChargeCalculator.new calc.use_dynamic_pricing = support_dynamic_pricing_feature calc.set_delivery_pricing_tiers(restaurant.delivery_pricing_tiers) calc.assign_voucher(vouchers) calc.set_user(param[:user_id]) calc.accept_refund = accept_refund? calc.calculate( package_bought, adult_param, kids_param, distance_to_restaurant_param, restaurant, date_param, add_on_bought ) end end |
#validate ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'app/forms/voucher_form/apply.rb', line 58 def validate errors.clear voucher_counts = Hash.new(0) vouchers.each { |v| voucher_counts[v.voucher_code.to_s] += 1 } duplicate_code = voucher_counts.select { |_, count| count > 1 }.keys if duplicate_code.present? errors.add(:base, I18n.t('views.voucher.duplicate', voucher_code: duplicate_code.first)) return false end if marketing_vouchers.count > 1 errors.add(:base, I18n.t('views.voucher.invalid_multiple_marketing', voucher_code: marketing_vouchers.first.voucher_code)) return false end if invalid_combination? vouchers_code = uncombinable_marketing_vouchers.map(&:voucher_code).to_sentence errors.add(:base, I18n.t('views.voucher.valid_combine', voucher_code: vouchers_code)) return false end data.each do |d| voucher = d[:voucher] if voucher_referral?(d[:voucher_code]) referral_voucher = VoucherForm::Referral.new( user_id: user&.id, code: d[:voucher_code], temporary: temporary?, country_code: country_code, ) unless referral_voucher.valid? errors.add(:base, referral_voucher.) return false end end if voucher_redemption?(d[:voucher_code]) redeem_voucher = VoucherForm::Reward.new( user_id: user.id, redeemed_points: redeemed_points, temporary: temporary?, campaign_redeemed_amount: campaign_redeemed_amount, country_code: country_code, restaurant: restaurant, ) unless redeem_voucher.valid_redeem_points? errors.add(:base, redeem_voucher.) return false end end if voucher_app_first?(d[:voucher_code]) app_first_voucher = VoucherForm::AppFirst.new(user_id: user&.id, temporary: temporary?, client_type: param[:client_type], country: restaurant.country) unless app_first_voucher.valid? errors.add(:base, app_first_voucher.) return false end end if voucher.blank? # if voucher not found, we will try to find it by code # then validate it based on voucher currency with current restaurant currency fetch_voucher = Voucher.find_by(voucher_code: d[:voucher_code]) if fetch_voucher.present? && fetch_voucher.amount.currency.to_s != restaurant&.country&.currency_code_upcase errors.add(:base, I18n.t("views.voucher.valid_country_#{fetch_voucher.country_code&.downcase}", voucher_code: fetch_voucher.voucher_code)) return false end if !voucher_redemption?(d[:voucher_code]) && !voucher_app_first?(d[:voucher_code]) errors.add(:base, I18n.t('views.voucher.not_found', voucher_code: d[:voucher_code])) next end next end unless voucher_usable_on_website?(voucher) errors.add(:base, I18n.t('views.voucher.not_usable_on_website')) return false end if voucher.person? # this discount type only valid for ayce invalid_packs = package_bought.reject { |p| p[:package].instance_of?(HhPackage::Package::Ayce) } if invalid_packs.present? errors.add(:base, I18n.t('views.voucher.invalid_person_level')) return false end end if multi_delivery_discount_voucher errors.add(:base, 'Vouchers for discounted shipping costs cannot be multiple') return false end single_voucher_param = param.tap do |h| h[:voucher] = d[:voucher] end if adaptive_points_ratio_id.present? voucher.adaptive_points_ratio_id = adaptive_points_ratio_id end form = VoucherForm::Validation.new(single_voucher_param, actor) unless form.validate merge_errors(form.errors) next end end errors.blank? end |
#vouchers_amount ⇒ Object
217 218 219 |
# File 'app/forms/voucher_form/apply.rb', line 217 def vouchers_amount @vouchers_amount ||= (vouchers.present? ? vouchers.sum(&:amount).amount.to_i : 0) end |
#vouchers_amount_v2 ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 |
# File 'app/forms/voucher_form/apply.rb', line 221 def vouchers_amount_v2 @vouchers_amount_v2 ||= begin va_v2 = (vouchers.present? ? vouchers.sum(&:amount).amount : 0) formatted_va_v2 = if va_v2 % 1 == 0 va_v2.to_i else va_v2.to_f.round(2) end formatted_va_v2 end end |