View: dbo.Etl_Helper_Ledger_Entry_Ar_Payment_Application_Credit_Memo_Ar_Dr_View
View definition
`CREATE VIEW dbo.Etl_Helper_Ledger_Entry_Ar_Payment_Application_Credit_Memo_Ar_Dr_View AS SELECT -- Unique key arpav.gl_transaction_KEY, ledger_entry_type_KEY = 23 /* AR Payment Application Credit Memo AR Dr */, -- PayloadLedger entry payload j.client_KEY, glt.journal_KEY, glt.gl_period_KEY, gl_account_KEY = COALESCE(arp.unearned_revenue__gl_account_KEY, cm_art.gl_account_KEY), gl_accounting_method_KEY = CASE WHEN arp.ar_payment_type_KEY IS NULL -- Credit Memo OR arp.ar_payment_type_KEY = 553 /* Write off */ THEN 1 /* Accrual */ ELSE NULL /* Both accrual and cash basis */ END, gl_balance_effect_KEY = 1 /* Debit */, gl_transaction_type_KEY = 570 /*AR payment application*/, transaction_status_KEY = 1 /*Live*/, gl_transaction_date = arpav.application_date, gl_transaction_amount = arpav.paid_amount, gl_transaction_description = arpav.description, gl_transaction_detail_description = arpav.description, gl_transaction_reference_number = arpav.reference_number, gl_transaction_reference_number_sortable = arpav.reference_number_sortable, -- Standard supplemental information gl_transaction_source_expression_KEY = CAST(1 AS TINYINT), -- GLTransactionSourceExpression.ARPaymentApplication gl_transaction_source_KEY = 8 /*ARPaymentApplication*/, is_activity_journal_entry = CAST(0 AS BIT), journal_entry_type_KEY = 101 /*Journal entry*/, source_was_a_distribution = CAST(1 AS BIT), sequence_number = 0, workpaper_reference = N'' COLLATE Latin1_General_CI_AS, distribution_workpaper_reference = N'' COLLATE Latin1_General_CI_AS, -- Custom supplemental information arp.customer_KEY FROM dbo.AR_Payment_Application_View AS arpav INNER JOIN dbo.GL_Transaction AS glt ON glt.gl_transaction_KEY = arpav.gl_transaction_KEY LEFT JOIN dbo.AR_Payment AS arp ON arpav.payment__gl_transaction_KEY = arp.gl_transaction_KEY LEFT JOIN dbo.AR_Transaction AS cm_art ON arpav.payment__gl_transaction_KEY = cm_art.gl_transaction_KEY INNER JOIN dbo.Journal AS j ON j.journal_KEY = glt.journal_KEY WHERE arpav.was_applied_at_payment_creation = 0 -- Only create an unearned revenue entry if there is actually unearned revenue! OR cm_art.gl_transaction_KEY IS NOT NULL -- Credit memos create a debit to their AR account. ;`