Class: ExportCreditCardService

Inherits:
BaseOperationService show all
Defined in:
app/services/export_credit_card_service.rb

Overview

Generate Excel file contains Credit Card and Promptpay data

Instance Attribute Summary collapse

Attributes inherited from BaseOperationService

#outcome

Instance Method Summary collapse

Methods inherited from BaseOperationService

#success?

Constructor Details

#initialize(start_date, end_date, restaurant_id = nil) ⇒ ExportCreditCardService

Returns a new instance of ExportCreditCardService.



17
18
19
20
21
22
23
24
# File 'app/services/export_credit_card_service.rb', line 17

def initialize(start_date, end_date, restaurant_id = nil)
  I18n.locale = :en
  self.start_date = start_date
  self.end_date = end_date
  self.restaurant_id = restaurant_id
  self.credit_cards = collections
  @filepath = Rails.root.join('tmp', 'reports', "Creditcard-reports-#{Time.current_time.to_i}.xls").to_s
end

Instance Attribute Details

#credit_cardsObject

Returns the value of attribute credit_cards.



6
7
8
# File 'app/services/export_credit_card_service.rb', line 6

def credit_cards
  @credit_cards
end

#end_dateObject

Returns the value of attribute end_date.



7
8
9
# File 'app/services/export_credit_card_service.rb', line 7

def end_date
  @end_date
end

#restaurant_idObject

Returns the value of attribute restaurant_id.



6
7
8
# File 'app/services/export_credit_card_service.rb', line 6

def restaurant_id
  @restaurant_id
end

#start_dateObject

Returns the value of attribute start_date.



7
8
9
# File 'app/services/export_credit_card_service.rb', line 7

def start_date
  @start_date
end

Instance Method Details

#collectionsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/export_credit_card_service.rb', line 33

def collections
  query_customer = 'externals_omise_charges.customer_id IS NOT NULL
OR externals_omise_charges.reservation_id IS NOT NULL'
  query_creation = 'externals_omise_charges.paid_at >= ? AND
externals_omise_charges.paid_at <=?'
  @credit_cards = ::Externals::Omise::Charge.includes(:reservation,
                                                      customer: {
                                                        reservation: [restaurant: :translations]
                                                      })
                                            .where(query_customer)
                                            .where(query_creation, start_date, end_date)
  if restaurant_id.present?
    @credit_cards = @credit_cards.joins(:reservation).where(reservations: { restaurant_id: restaurant_id })
  end
  @credit_cards
end

#downloadObject



26
27
28
29
30
31
# File 'app/services/export_credit_card_service.rb', line 26

def download
  generate_credit_card_report
  self.attachment = store_to_attachment
  delete_report(attachment)
  download_link
end