diff options
-rw-r--r-- | sql/modules/Company.sql | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/sql/modules/Company.sql b/sql/modules/Company.sql index f493586f..b7f5e640 100644 --- a/sql/modules/Company.sql +++ b/sql/modules/Company.sql @@ -235,8 +235,23 @@ CREATE OR REPLACE FUNCTION company_save ( in_entity_id int, in_sic_code text ) RETURNS INT AS $$ DECLARE t_entity_id INT; + t_company_id INT; BEGIN IF in_entity_id IS NULL THEN + IF in_id IS NULL THEN + SELECT id INTO t_company_id FROM company + WHERE legal_name = in_name AND + (tax_id = in_tax_id OR + (tax_id IS NULL AND in_tax_id IS NULL)); + END IF; + IF t_company_id IS NOT NULL THEN + SELECT entity_id INTO t_entity_id FROM company + WHERE id = t_company_id; + END IF; + ELSE + t_entity_id := in_entity_id; + END IF; + IF in_entity_id IS NULL THEN INSERT INTO entity (name, entity_class) VALUES (in_name, in_entity_class); t_entity_id := currval('entity_id_seq'); |