Class: EventDispatchers::InvoiceSentViaEdiEventDispatcher

Inherits:
BaseEventDispatcher show all
Defined in:
app/notifications/event_dispatchers/invoice_sent_via_edi_event_dispatcher.rb

Instance Attribute Summary

Attributes inherited from BaseEventDispatcher

#company_id

Instance Method Summary collapse

Methods inherited from BaseEventDispatcher

#initialize

Constructor Details

This class inherits a constructor from EventDispatchers::BaseEventDispatcher

Instance Method Details

#dispatchObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/notifications/event_dispatchers/invoice_sent_via_edi_event_dispatcher.rb', line 5

def dispatch
  # 1- find all invoices which have 0 reference events and status 2
  sql = <<-SQL
    SELECT vei.edi_invoice_id, vei.edi_partner_id FROM VW_EDI_INVOICES vei
    LEFT JOIN REFERENCE_EVENTS re ON re.reference_id = vei.edi_invoice_id
      AND re.reference_type = 'EdiInvoice'
    WHERE vei.company_id = ?
      AND vei.status = '02'
    GROUP BY vei.edi_invoice_id, vei.edi_partner_id
    HAVING COUNT(re.reference_event_id) = 0
  SQL

  EdiInvoice.find_by_sql([sql, company_id]).each do |invoice|
    ReferenceEvent.create!(
      slug: ReferenceEvent::Slugs::INVOICE_SENT_VIA_EDI,
      level: ReferenceEvent::Levels::INFO,
      company_id: company_id,
      reference_type: 'EdiInvoice',
      reference_id: invoice.edi_invoice_id,
      data: {
        edi_partner_id: invoice.edi_partner_id
      },
      end_at: nil
    ).tap do |reference_event|
      reference_event.update!(
        last_verified_at: Time.current
      )
    end
  end
end