File: Entity.sql

package info (click to toggle)
ledgersmb 1.6.33%2Bds-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,000 kB
  • sloc: perl: 52,612; sql: 43,562; xml: 36,194; javascript: 2,428; sh: 1,099; makefile: 361; pascal: 25
file content (136 lines) | stat: -rw-r--r-- 3,549 bytes parent folder | download | duplicates (3)
1
2
3
4
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

set client_min_messages = 'warning';


--

-- Copyright (C) 2011 LedgerSMB Core Team.  Licensed under the GNU General
-- Public License v 2 or at your option any later version.

-- Docstrings already added to this file.

BEGIN;

CREATE OR REPLACE FUNCTION entity_save(
    in_entity_id int, in_name text, in_entity_class INT
) RETURNS INT AS $$

    DECLARE
        e entity;
        e_id int;

    BEGIN

        select * into e from entity where id = in_entity_id;

        update
            entity
        SET
            name = in_name,
            entity_class = in_entity_class
        WHERE
            id = in_entity_id;
        IF NOT FOUND THEN
            -- do the insert magic.
            e_id = nextval('entity_id_seq');
            insert into entity (id, name, entity_class) values
                (e_id,
                in_name,
                in_entity_class
                );
            return e_id;
        END IF;
        return in_entity_id;

    END;

$$ language 'plpgsql';

COMMENT ON FUNCTION entity_save(
    in_entity_id int, in_name text, in_entity_class INT
)  IS
$$ Currently unused.  Left in because it is believed it may be helpful.

This saves an entity, with the control code being the next available via the
defaults table.$$;

CREATE OR REPLACE FUNCTION entity__list_classes ()
RETURNS SETOF entity_class AS $$
DECLARE out_row entity_class;
BEGIN
        FOR out_row IN
                SELECT * FROM entity_class
                WHERE active and pg_has_role(SESSION_USER,
                                   lsmb__role_prefix()
                                   || 'contact_class_'
                                   || lower(regexp_replace(class, '( |\-)', '_')),
                                   'USAGE')
                ORDER BY id
        LOOP
                RETURN NEXT out_row;
        END LOOP;
END;
$$ LANGUAGE PLPGSQL;

COMMENT ON FUNCTION entity__list_classes () IS
$$ Returns a list of entity classes, ordered by assigned ids$$;

CREATE OR REPLACE FUNCTION entity__get (
    in_entity_id int
) RETURNS setof entity AS $$
    SELECT * FROM entity WHERE id = in_entity_id;
$$ language sql;

COMMENT ON FUNCTION entity__get (
    in_entity_id int
) IS
$$ Returns a set of (only one) entity record with the entity id.$$;


CREATE OR REPLACE FUNCTION eca__get_entity (
    in_credit_id int
) RETURNS setof entity AS $$

    SELECT entity.*
      FROM entity_credit_account
      JOIN entity ON entity_credit_account.entity_id = entity.id
     WHERE entity_credit_account.id = in_credit_id;

$$ language sql;

COMMENT ON FUNCTION eca__get_entity (
    in_credit_id int
)  IS
$$ Returns a set of (only one) entity to which the entity credit account is
attached.$$;

CREATE OR REPLACE FUNCTION entity__get_bank_account(in_id int)
RETURNS entity_bank_account
LANGUAGE SQL AS $$
SELECT * FROM  entity_bank_account WHERE id = $1;
$$;

CREATE OR REPLACE FUNCTION entity__delete_bank_account
(in_entity_id int, in_id int)
RETURNS bool AS
$$
BEGIN

UPDATE entity_credit_account SET bank_account = NULL
 WHERE entity_id = in_entity_id AND bank_account = in_id;

DELETE FROM entity_bank_account
 WHERE id = in_id AND entity_id = in_entity_id;

RETURN FOUND;

END;
$$ LANGUAGE plpgsql;

COMMENT ON FUNCTION entity__delete_bank_account(in_entity_id int, in_id int) IS
$$ Deletes the bank account identitied by in_id if it is attached to the entity
identified by entity_id.  Returns true if a record is deleted, false if not.$$;

update defaults set value = 'yes' where setting_key = 'module_load_ok';

COMMIT;