Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Generate Gift Card based on CSV

CSV file => https://docs.google.com/spreadsheets/d/1KNUAW33DRgI7cwg66-OwAAxzcvlOLXMj7xTgjn3L4wY/edit#gid=319968689

user_not_founds = []
expiry_date = Date.parse('2 October 2023')
today = Date.today
payment_types = PaymentType.all
valid = []

ActiveRecord::Base.transaction do
  CSV.foreach('/tmp/compensation.csv', headers: true) do |row|
    name = row['Comp Method'].to_s

    next unless name.include?('GF')

    email = row['email']

    user = User.find_by email: email

    if user.blank?
      user_not_founds.push email
      next
    end

    amount = name.gsub('GF', '').to_i
    voucher = Voucher.new({
                            'user_id' => user.id,
                            'voucher_type' => 'specific_customer',
                            'name' => name,
                            'amount_cents' => amount * 100,
                            'amount_currency' => 'THB',
                            'max_usage' => 1,
                            'expiry_date' => expiry_date,
                            'quota' => 1,
                            'payment_type' => nil,
                            'prefix_number' => nil,
                            'expiry_type' => 'range',
                            'expiry_range_by' => 'created_at',
                            'start_date' => today,
                            'end_date' => expiry_date,
                            'subsidized_by' => 'hungryhub',
                            'apply_for' => 'package',
                            'usage_type' => 'one_time',
                            'discount_type' => 'amount',
                            'active' => true,
                            'sun_active' => true,
                            'mon_active' => true,
                            'tue_active' => true,
                            'wed_active' => true,
                            'thu_active' => true,
                            'fri_active' => true,
                            'sat_active' => true,
                            'for_web' => true,
                            'for_ios' => true,
                            'for_android' => true,
                            'non_refundable' => true,
                            'require_full_prepayment' => true,
                            'amount_cap_cents' => 0,
                            'amount_cap_currency' => 'THB',
                            'voucher_code' => Voucher.generate_voucher_code,
                          })
    if voucher.valid?
      voucher.save!
      payment_types.each do |pt|
        voucher.payment_types << pt
      end
      valid.push voucher
    end
  end
end

File.open('/tmp/valid.csv', 'w') do |f|
  header = Voucher.last.attributes.keys
  header.push('email')
  data = [header.to_csv]
  valid.map do |v|
    body = v.attributes.values
    body.push v.user.email
    data.push(body.to_csv)
  end

  f.write data.join('')
  f.close
end

File.open('/tmp/invalid.csv', 'w') do |f|
  data = []
  data.push("In quota? (Y/N),Comp Method,Comp Status,Comp Date,Transaction ID,Voucher Purchase Date,Customer Name,Quota,Voucher Package Name,Total Voucher Value,Partner Name,Payment Status,Voucher Active Status,GB Pay - ignore,Payment Type,Payment Gateway,Paid at Date,Paid Time,phone,email\n")
  invalid.map { |v| data.push(v.to_h.values.to_csv) }
  f.write data.join('')
  f.close
end