-- View: public.vw_utility_account_move

-- DROP VIEW public.vw_utility_account_move;

CREATE OR REPLACE VIEW public.vw_utility_account_move AS
 SELECT am.id AS "intAccountMoveId",
    am.date AS "dtmDate",
    am.post_date AS "dtmPostDate",
    COALESCE(am.post_number, ''::character varying) AS "strPostNumber",
    COALESCE(am.description, ''::character varying) AS "strDescription",
    am.journal AS "intJournalId",
    am.number AS "strJournalNb",
    am.state AS "strState",
    COALESCE(split_part(am.origin::text, ','::text, 1), ''::text) AS "strOrigin",
    COALESCE(split_part(am.origin::text, ','::text, 2)::integer, 0) AS "intOriginId",
    aml.lot AS "intLotId",
    COALESCE(aml.reconciliation, 0) AS "intReconciliation",
    aml.debit AS "dblDebit",
    aml.credit AS "dblCredit",
    aml.maturity_date AS "dtmMaturityDate",
    COALESCE(split_part(aml.origin::text, ','::text, 1), ''::text) AS "strLineOrigin",
    COALESCE(split_part(aml.origin::text, ','::text, 2)::integer, 0) AS "intLineOriginId",
    acc.name AS "strAccount",
    aj.code AS "strAccountJournalCode",
    aj.name AS "strAccountJournal",
    aj.type AS "strAccountJournalType",
    COALESCE(cc.name, ''::character varying) AS "strSecondCurrency",
    COALESCE(aml.amount_second_currency, 0::numeric) AS "dblAmountSecondCurrency",
    aml.debit - aml.credit AS "dblAmount"
   FROM account_move am
     JOIN account_move_line aml ON am.id = aml.move
     JOIN account_account acc ON aml.account = acc.id
     JOIN account_journal aj ON am.journal = aj.id
     LEFT JOIN currency_currency cc ON aml.second_currency = cc.id;

ALTER TABLE public.vw_utility_account_move
    OWNER TO postgres;
