Class: Api::Partner::V1::BillsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/partner/v1/bills_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#default_restaurant, #identity_cache_memoization, #render_unauthorize_action, #restaurants, #set_options

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Methods included from ResponseCacheConcern

#my_response_cache

Instance Method Details

#email_owner_managerObject



25
26
27
# File 'app/controllers/api/partner/v1/bills_controller.rb', line 25

def email_owner_manager
  render json: manager_emails(restaurants)
end

#exportObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/api/partner/v1/bills_controller.rb', line 29

def export
  permitted_parameters = params.require(:reservation).permit(:month, :year, :type, emails: [])

  emails = (permitted_parameters[:emails].presence || []).to_set.add(current_staff.email).to_a

  messages = []

  if permitted_parameters[:month].blank? || permitted_parameters[:year].blank?
    messages << I18n.t('partner.errors.bills.export.month_year_blank')
  end

  emails.each do |email|
    unless ::EmailValidator.valid?(email)
      messages << I18n.t('partner.errors.bills.export.e_mail_invalid',
                         email: email)
    end
  end

  if permitted_parameters[:month] > Time.current.month && permitted_parameters[:year] >= Time.current.year
    messages << I18n.t('partner.errors.bills.export.month_year_less_than_current')
  end

  return render json: { status: 'failed', message: messages } if messages.present?

  start_date = DateTime.new(permitted_parameters[:year], permitted_parameters[:month]).beginning_of_month
  end_date = start_date.end_of_month
  data_type = permitted_parameters.require(:type)

  restaurants.each do |restaurant|
    NotificationWorkers::ReservationReport.fix_queue_perform_async(
      :restaurant_managers,
      {
        emails: emails,
        restaurant_id: restaurant.id,
        start_date: start_date,
        end_date: end_date,
        data_type: data_type,
        date_type: :date,
        report_type: :billing,
      },
    )
  end
  render json: { status: 'success', message: I18n.t('partner.errors.bills.export.send_email_billing') }
end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/api/partner/v1/bills_controller.rb', line 8

def index
  date = params[:date].presence || Time.current
  return if validate_date_within_one_year(date)

  result = Rails.cache.fetch(
    "billing_cache_#{restaurants&.cache_key}:#{params[:date]}", expires_in: 1.day
  ) do
    reservations = Reservation.joins(:restaurant).
      where(restaurant_id: restaurants.ids).
      reached_goal_scope.
      exclude_cancel_scope.exclude_temporary
    PartnerService::Bills::CalculateService.new(reservations,
                                                date.to_date).calculate
  end
  render json: { data: result, success: true }
end