Module: MailingsHelper

Defined in:
app/helpers/mailings_helper.rb

Overview

== Schema Information

Table name: mailings

id :integer not null, primary key
state :string(255)
description :text
date_completed :datetime
creator_id :integer
updater_id :integer
label_file_name :string(255)
label_content_type :string(255)
label_file_size :integer
cover_letter_file_name :string(255)
cover_letter_type :string(255)
cover_file_size :integer
created_at :datetime
updated_at :datetime
job_id :string(255)
catalog_id :integer

Instance Method Summary collapse

Instance Method Details

#mailing_activities_can_complete?(mailing = @mailing) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/helpers/mailings_helper.rb', line 42

def mailing_activities_can_complete?(mailing = @mailing)
  !mailing.complete? and !mailing.has_active_background_job? and !mailing.ready_to_complete?
end

#mailing_activity_commands(mail_activity = @mail_activity, options = {}) ⇒ Array<String>

Command links for a mail activity row (view/process, print postage,
complete, manually complete, remove).

Parameters:

  • mail_activity (MailActivity) (defaults to: @mail_activity)

    the activity to build commands for

  • options (Hash) (defaults to: {})

    display options

Options Hash (options):

  • :hide_show_link (Boolean)

    omit the "View/Process" link

Returns:

  • (Array<String>)

    HTML-safe command links



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
# File 'app/helpers/mailings_helper.rb', line 80

def mailing_activity_commands(mail_activity = @mail_activity, options = {})
  commands = []
  with_options(class: 'btn btn-outline-primary btn-block') do |defaults|
    unless options[:hide_show_link]
      show_link_title = fa_icon('chevron-right', text: mail_activity.complete? ? "View" : "Process")
      commands << defaults.link_to(show_link_title,
                    mail_activity_path(mail_activity),
                    class: 'btn btn-outline-primary btn-block',
                    data: { controller: 'ui-blocker' })
    end
    if mail_activity.ready_to_print_postage_label?
      commands << defaults.link_to(fa_icon('print', text: "Print Postage"),
                    upload_path(mail_activity.all_labels_pdf),
                    target: '_print_postage')
    end
    if mail_activity.can_complete_mail_activity?
      commands << defaults.link_to(fa_icon('check', text: "Complete", title: "Mark as completed"),
                    complete_mail_activity_path(mail_activity),
                    data: { turbo_method: :put, turbo_confirm: "Are you sure you want to mark this mail activity as complete? You can't undo this!" })
    end

    if mail_activity.can_manually_complete_mail_activity?
      commands << defaults.link_to(fa_icon('check', text: "Manually Complete", title: "Mark as manually complete"),
                    manually_complete_mail_activity_path(mail_activity),
                    data: { turbo_method: :put, turbo_confirm: "Are you sure you want to mark this mail activity as manually complete? You can't undo this!" })
    end
    if mail_activity.ok_to_delete?
      commands << defaults.link_to(fa_icon('trash', text: "Remove", title: "Remove Activity From Mailing"),
                    mail_activity_path(mail_activity),
                    data: { turbo_method: :delete, turbo_confirm: "Are you sure you want to remove this mail activity from the mailing?" })
    end
  end
  commands
end

#mailing_activity_icon_for_state(state) ⇒ Object



46
47
48
# File 'app/helpers/mailings_helper.rb', line 46

def mailing_activity_icon_for_state(state)
  { open: 'circle', packaged: 'dropbox', postage_rated: 'envelope', postage_labeled: 'envelope', complete: 'check' }[state.to_sym]
end

#mailing_activity_postage_commands(mail_activity = @mail_activity) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/helpers/mailings_helper.rb', line 60

def mailing_activity_postage_commands(mail_activity = @mail_activity)
  opts = []
  btn_class = 'btn btn-outline-primary'
  if mail_activity.ready_to_label?
    opts << blocking_submit_button(label: "Generate Postage Label(s)")
  elsif mail_activity.postage_labeled?
    opts << link_to("Void Postage Label(s)", void_postal_labels_mail_activity_path(mail_activity), class: btn_class, data: { controller: 'ui-blocker', turbo_method: :put, turbo_confirm: "Are you sure you want to void the postage label(s)?" })
  end
  if mail_activity.ready_to_rate?
    opts << link_to(mail_activity.postage_rated? ? "Refresh Postage Rates" : "Get Postage Rates", find_postal_rates_mail_activity_path(@mail_activity), class: btn_class, data: { controller: 'ui-blocker', turbo_method: :put })
  end
  opts
end

#mailing_activity_postage_rate_for_select(mail_activity = @mail_activity) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'app/helpers/mailings_helper.rb', line 50

def mailing_activity_postage_rate_for_select(mail_activity = @mail_activity)
  mail_activity.postage_rates.map do |pr|
    composite_code = pr['service_code'] # e.g. "UPS::03" or legacy "Priority"
    service_name = pr['service_name'].presence || composite_code
    package_info = pr['hw_package_type'].present? ? " (#{pr['hw_package_type'].underscore.tr('_', ' ')})" : ''
    label = "#{service_name}#{package_info}: #{number_to_currency(pr['total_charges'].to_f)}"
    [label, composite_code]
  end
end

#mailings_command_options(mailing = nil, link_options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/mailings_helper.rb', line 24

def mailings_command_options(mailing = nil, link_options = {})
  mailing ||= @mailing
  opts = []
  with_options(**link_options) do |lo|
    if mailing.ready_to_complete?
      opts << lo.link_to(fa_icon('print', text: 'Print Postage Labels'), print_all_postage_labels_mailing_path(mailing), target: '_postage_labels') if mailing.mail_activities.any?(&:ready_to_print_labels?)
    end
    opts << lo.link_to(fa_icon('table', text: "Download Mailing Manifest"), mailing_path(mailing, format: :csv))
    if mailing_activities_can_complete?(mailing)
      opts << lo.link_to(fa_icon('check', text: "Mark All Mail Activities As Complete"), manually_complete_all_mail_activities_mailing_path(mailing),
data: { turbo_method: :put, turbo_confirm: "This will mark all the open mail activities as complete, you cannot UNDO, continue?" })
    end
    opts << lo.link_to("Completion pending", job_path(mailing.job_id)) if mailing.has_active_background_job?
    opts << lo.link_to(fa_icon('trash', text: "Delete Mailing"), mailing_path(mailing), data: { turbo_method: :delete, turbo_confirm: "Deleting this mailing will not delete the activities, continue?" }) if mailing.ok_to_delete?
  end
  opts
end