Class: Returns::SendSecondReminder

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/returns/send_second_reminder.rb

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SendSecondReminder

Returns a new instance of SendSecondReminder.



9
10
11
# File 'app/services/returns/send_second_reminder.rb', line 9

def initialize(options={})
  @renderer = PdfRenderer.new
end

Instance Attribute Details

#rendererObject (readonly)

Returns the value of attribute renderer.



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

def renderer
  @renderer
end

Instance Method Details

#process(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/returns/send_second_reminder.rb', line 13

def process(options = {})
  processed_rmas = []
  invoices = Invoice.joins(order: [:rma, deliveries: [:payments]]).where("invoices.state = ? and payments.category = ? and invoices.document_date = ? and rmas.state = ?", 'unpaid', Payment::ADV_REPL, 20.days.ago.to_date, 'awaiting_return')

  invoices.each do |i|
    rma = i.order.rma
    next if rma.skip_reminders

    keep_as_draft = rma.transmission_email.empty? and rma.transmission_fax.empty?
    comm = CommunicationBuilder.new(resource: i.order.rma, merge_options: {invoice: i, rma: i.order.rma}, template_system_code: 'RMA_REMINDER_2').create(keep_as_draft: keep_as_draft)
    if keep_as_draft == true
      # couldn't send as no email/fax listed on the RMA
      InternalMailer.unable_to_deliver_rma_reminder(rma, comm).deliver_later
    end
    at = ActivityType.find(ActivityTypeConstants::RMAOUT)
    unless rma.activities.open_activities.where(activity_type_id: ActivityTypeConstants::RMAOUT).any?
      Activity.create(lock_target_datetime: true,
                      activity_type_id: ActivityTypeConstants::RMAOUT,
                      target_datetime: 1.working.days.from_now,
                      assigned_resource: at.determine_assigned_resource(i.customer),
                      party: i.customer,
                      resource: rma,
                      notes: "20 days Follow up on #{rma.reference_number}.")
    end

    processed_rmas << i.order.rma.rma_number
  end

  Result.new({rmas: processed_rmas})
end