diff --git a/test/data/33_informal_establishments.csv b/test/data/33_informal_establishments.csv new file mode 100644 index 000000000..82553937e --- /dev/null +++ b/test/data/33_informal_establishments.csv @@ -0,0 +1,3 @@ +valid_from,valid_to,tax_ident,stat_ident,name,physical_region_code,physical_country_iso_2,primary_activity_category_code,employees,turnover,data_source_code +2024-01-01,infinity,82212760144,3000,THE INFORMAL NILE PEARL WATER,225613,UG,4752,1,1200,nlr +2024-01-01,infinity,82812760140,3001,THE EQUATOR GLOBE SOLUTIONS,225602,UG,5610,2,4400,nlr diff --git a/test/expected/33_informal_formal_connections.out b/test/expected/33_informal_formal_connections.out new file mode 100644 index 000000000..2745b2578 --- /dev/null +++ b/test/expected/33_informal_formal_connections.out @@ -0,0 +1,1748 @@ +BEGIN; +\echo "Setting up Statbus using the web provided examples" +"Setting up Statbus using the web provided examples" +\echo "User selected the Activity Category Standard" +"User selected the Activity Category Standard" +INSERT INTO settings(activity_category_standard_id,only_one_setting) +SELECT id, true FROM activity_category_standard WHERE code = 'isic_v4' +ON CONFLICT (only_one_setting) +DO UPDATE SET + activity_category_standard_id =(SELECT id FROM activity_category_standard WHERE code = 'isic_v4') + WHERE settings.id = EXCLUDED.id; +; +\echo "User uploads the sample activity categories" +"User uploads the sample activity categories" +\copy public.activity_category_available_custom(path,name) FROM 'app/public/demo/activity_custom_isic_demo.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +\echo "User uploads the sample regions" +"User uploads the sample regions" +\copy public.region_upload(path, name) FROM 'app/public/demo/regions_demo.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +\echo "User uploads the sample legal forms" +"User uploads the sample legal forms" +\copy public.legal_form_custom_only(code,name) FROM 'app/public/demo/legal_forms_demo.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +\echo "User uploads the sample sectors" +"User uploads the sample sectors" +\copy public.sector_custom_only(path,name,description) FROM 'app/public/demo/sectors_demo.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 0 | 0 +(1 row) + +\echo "User uploads legal units" +"User uploads legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,stat_ident,name,birth_date,physical_region_code,physical_country_iso_2,primary_activity_category_code,legal_form_code,sector_code,employees,turnover,data_source_code) FROM 'test/data/31_legal_units.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +\echo "User uploads formal establishments" +"User uploads formal establishments" +\copy public.import_establishment_era_for_legal_unit(valid_from,valid_to,tax_ident,stat_ident,name,physical_region_code,physical_country_iso_2,primary_activity_category_code,employees,turnover,legal_unit_tax_ident,data_source_code) FROM 'test/data/31_formal_establishments.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +\echo "User uploads informal establishments" +"User uploads informal establishments" +\copy public.import_establishment_era_without_legal_unit(valid_from,valid_to,tax_ident,stat_ident,name,physical_region_code,physical_country_iso_2,primary_activity_category_code,employees,turnover,data_source_code) FROM 'test/data/33_informal_establishments.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SAVEPOINT after_loading_units; +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 4 | 2 | 4 +(1 row) + +\echo "Refreshing materialized views" +"Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + view_name +--------------------------- + statistical_unit + activity_category_used + region_used + sector_used + data_source_used + legal_form_used + country_used + statistical_unit_facet + statistical_history + statistical_history_facet +(10 rows) + +SELECT unit_type, name, external_idents +FROM statistical_unit ORDER BY unit_type,name; + unit_type | name | external_idents +---------------+-------------------------------+---------------------------------------------------- + establishment | EQUATOR GLOBE SOLUTIONS | {"tax_ident": "92812760140", "stat_ident": "2001"} + establishment | NILE PEARL WATER | {"tax_ident": "92212760144", "stat_ident": "2000"} + establishment | THE EQUATOR GLOBE SOLUTIONS | {"tax_ident": "82812760140", "stat_ident": "3001"} + establishment | THE INFORMAL NILE PEARL WATER | {"tax_ident": "82212760144", "stat_ident": "3000"} + legal_unit | EQUATOR GLOBE SOLUTIONS | {"tax_ident": "2812760140", "stat_ident": "1001"} + legal_unit | NILE PEARL WATER | {"tax_ident": "2212760144", "stat_ident": "1000"} + enterprise | EQUATOR GLOBE SOLUTIONS | {"tax_ident": "2812760140", "stat_ident": "1001"} + enterprise | NILE PEARL WATER | {"tax_ident": "2212760144", "stat_ident": "1000"} + enterprise | THE EQUATOR GLOBE SOLUTIONS | {"tax_ident": "82812760140", "stat_ident": "3001"} + enterprise | THE INFORMAL NILE PEARL WATER | {"tax_ident": "82212760144", "stat_ident": "3000"} +(10 rows) + +\echo "Test statistical_unit_hierarchy - for Nile Pearl Water" +"Test statistical_unit_hierarchy - for Nile Pearl Water" +WITH selected_enterprise AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '1000' + AND unit_type = 'enterprise' + LIMIT 1 +) +SELECT jsonb_pretty( + public.remove_ephemeral_data_from_hierarchy( + public.statistical_unit_hierarchy( + 'enterprise', + (SELECT unit_id FROM selected_enterprise), + '2024-01-01'::DATE + ) + ) + ) AS statistical_unit_hierarchy; + statistical_unit_hierarchy +----------------------------------------------------------------------------------------------------------------------------------- + { + + "enterprise": { + + "notes": null, + + "active": true, + + "legal_unit": [ + + { + + "name": "NILE PEARL WATER", + + "notes": null, + + "active": true, + + "sector": { + + "code": "6100", + + "name": "Central government", + + "path": "DOMESTIC.C.6100", + + "label": "DOMESTICC6100", + + "active": true, + + "custom": true, + + "description": "Central government" + + }, + + "activity": [ + + { + + "type": "primary", + + "valid_to": "infinity", + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "activity_category": { + + "code": "4752", + + "name": "Retail sale of hardware, paints and glass in specialized stores", + + "path": "G.47.5.2", + + "label": "G4752", + + "level": 4, + + "active": true, + + "custom": false, + + "description": null, + + "activity_category_standard": { + + "code": "isic_v4", + + "name": "ISIC 4", + + "obsolete": false, + + "description": "ISIC Version 4", + + "code_pattern": "digits" + + } + + } + + } + + ], + + "location": [ + + { + + "type": "physical", + + "region": { + + "code": "225613", + + "name": "Entebbe", + + "path": "2.256.13", + + "label": "225613", + + "level": 3, + + "center_altitude": null, + + "center_latitude": null, + + "center_longitude": null + + }, + + "country": { + + "name": "Uganda", + + "iso_2": "UG", + + "iso_3": "UGA", + + "active": true, + + "custom": false, + + "iso_num": "800" + + }, + + "altitude": null, + + "latitude": null, + + "valid_to": "infinity", + + "longitude": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "postal_code": null, + + "valid_after": "2023-12-31", + + "postal_place": null, + + "address_part1": null, + + "address_part2": null, + + "address_part3": null + + } + + ], + + "valid_to": "infinity", + + "birth_date": "2016-01-10", + + "death_date": null, + + "legal_form": { + + "code": "4", + + "name": "Cooperative", + + "active": true, + + "custom": true + + }, + + "reorg_date": null, + + "short_name": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "web_address": null, + + "edit_comment": "Batch import", + + "telephone_no": null, + + "email_address": null, + + "establishment": [ + + { + + "name": "NILE PEARL WATER", + + "notes": null, + + "active": true, + + "activity": [ + + { + + "type": "primary", + + "valid_to": "infinity", + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "activity_category": { + + "code": "4752", + + "name": "Retail sale of hardware, paints and glass in specialized stores", + + "path": "G.47.5.2", + + "label": "G4752", + + "level": 4, + + "active": true, + + "custom": false, + + "description": null, + + "activity_category_standard": { + + "code": "isic_v4", + + "name": "ISIC 4", + + "obsolete": false, + + "description": "ISIC Version 4", + + "code_pattern": "digits" + + } + + } + + } + + ], + + "location": [ + + { + + "type": "physical", + + "region": { + + "code": "225613", + + "name": "Entebbe", + + "path": "2.256.13", + + "label": "225613", + + "level": 3, + + "center_altitude": null, + + "center_latitude": null, + + "center_longitude": null + + }, + + "country": { + + "name": "Uganda", + + "iso_2": "UG", + + "iso_3": "UGA", + + "active": true, + + "custom": false, + + "iso_num": "800" + + }, + + "altitude": null, + + "latitude": null, + + "valid_to": "infinity", + + "longitude": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "postal_code": null, + + "valid_after": "2023-12-31", + + "postal_place": null, + + "address_part1": null, + + "address_part2": null, + + "address_part3": null + + } + + ], + + "valid_to": "infinity", + + "birth_date": null, + + "death_date": null, + + "reorg_date": null, + + "short_name": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "web_address": null, + + "edit_comment": "Batch import", + + "telephone_no": null, + + "email_address": null, + + "invalid_codes": null, + + "stat_for_unit": [ + + { + + "valid_to": "infinity", + + "value_int": 0, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "employees", + + "name": "Number of people employed", + + "type": "int", + + "archived": false, + + "priority": 1, + + "frequency": "yearly", + + "description": "The number of people receiving an official salary with government reporting."+ + } + + }, + + { + + "valid_to": "infinity", + + "value_int": 0, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "turnover", + + "name": "Turnover", + + "type": "int", + + "archived": false, + + "priority": 2, + + "frequency": "yearly", + + "description": "The amount (EUR)" + + } + + } + + ], + + "free_econ_zone": null, + + "external_idents": { + + "tax_ident": "92212760144", + + "stat_ident": "2000" + + }, + + "parent_org_link": null, + + "reorg_references": null, + + "primary_for_legal_unit": true + + } + + ], + + "invalid_codes": null, + + "stat_for_unit": [ + + { + + "valid_to": "infinity", + + "value_int": 2, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "employees", + + "name": "Number of people employed", + + "type": "int", + + "archived": false, + + "priority": 1, + + "frequency": "yearly", + + "description": "The number of people receiving an official salary with government reporting." + + } + + }, + + { + + "valid_to": "infinity", + + "value_int": 9000000, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "turnover", + + "name": "Turnover", + + "type": "int", + + "archived": false, + + "priority": 2, + + "frequency": "yearly", + + "description": "The amount (EUR)" + + } + + } + + ], + + "free_econ_zone": null, + + "external_idents": { + + "tax_ident": "2212760144", + + "stat_ident": "1000" + + }, + + "parent_org_link": null, + + "reorg_references": null, + + "primary_for_enterprise": true + + } + + ], + + "short_name": null, + + "edit_comment": "Batch import" + + } + + } +(1 row) + +\echo "Test statistical_unit_hierarchy - for The Equator Globe Solutions" +"Test statistical_unit_hierarchy - for The Equator Globe Solutions" +WITH selected_enterprise AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '3001' + AND unit_type = 'enterprise' + LIMIT 1 +) +SELECT jsonb_pretty( + public.remove_ephemeral_data_from_hierarchy( + public.statistical_unit_hierarchy( + 'enterprise', + (SELECT unit_id FROM selected_enterprise), + '2024-01-01'::DATE + ) + ) + ) AS statistical_unit_hierarchy; + statistical_unit_hierarchy +--------------------------------------------------------------------------------------------------------------------------- + { + + "enterprise": { + + "notes": null, + + "active": true, + + "short_name": null, + + "edit_comment": "Batch import", + + "establishment": [ + + { + + "name": "THE EQUATOR GLOBE SOLUTIONS", + + "notes": null, + + "active": true, + + "activity": [ + + { + + "type": "primary", + + "valid_to": "infinity", + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "activity_category": { + + "code": "5610", + + "name": "Restaurants and mobile food service activities", + + "path": "I.56.1.0", + + "label": "I5610", + + "level": 4, + + "active": true, + + "custom": false, + + "description": null, + + "activity_category_standard": { + + "code": "isic_v4", + + "name": "ISIC 4", + + "obsolete": false, + + "description": "ISIC Version 4", + + "code_pattern": "digits" + + } + + } + + } + + ], + + "location": [ + + { + + "type": "physical", + + "region": { + + "code": "225602", + + "name": "Kampala", + + "path": "2.256.02", + + "label": "225602", + + "level": 3, + + "center_altitude": null, + + "center_latitude": null, + + "center_longitude": null + + }, + + "country": { + + "name": "Uganda", + + "iso_2": "UG", + + "iso_3": "UGA", + + "active": true, + + "custom": false, + + "iso_num": "800" + + }, + + "altitude": null, + + "latitude": null, + + "valid_to": "infinity", + + "longitude": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "postal_code": null, + + "valid_after": "2023-12-31", + + "postal_place": null, + + "address_part1": null, + + "address_part2": null, + + "address_part3": null + + } + + ], + + "valid_to": "infinity", + + "birth_date": null, + + "death_date": null, + + "reorg_date": null, + + "short_name": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "web_address": null, + + "edit_comment": "Batch import", + + "telephone_no": null, + + "email_address": null, + + "invalid_codes": null, + + "stat_for_unit": [ + + { + + "valid_to": "infinity", + + "value_int": 2, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "employees", + + "name": "Number of people employed", + + "type": "int", + + "archived": false, + + "priority": 1, + + "frequency": "yearly", + + "description": "The number of people receiving an official salary with government reporting."+ + } + + }, + + { + + "valid_to": "infinity", + + "value_int": 4400, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "turnover", + + "name": "Turnover", + + "type": "int", + + "archived": false, + + "priority": 2, + + "frequency": "yearly", + + "description": "The amount (EUR)" + + } + + } + + ], + + "free_econ_zone": null, + + "external_idents": { + + "tax_ident": "82812760140", + + "stat_ident": "3001" + + }, + + "parent_org_link": null, + + "reorg_references": null, + + "primary_for_legal_unit": null + + } + + ] + + } + + } +(1 row) + +SELECT count(*) FROM public.enterprise; + count +------- + 4 +(1 row) + +\echo "Connect - Nile Pearl Water - to The Equator Globe Solutions" +"Connect - Nile Pearl Water - to The Equator Globe Solutions" +WITH equator_enterprise AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '3001' + AND unit_type = 'enterprise' + LIMIT 1 +), nile_legal_unit AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '1000' + AND unit_type = 'legal_unit' + LIMIT 1 +) +SELECT + public.remove_ephemeral_data_from_hierarchy( + connect_legal_unit_to_enterprise(nile_legal_unit.unit_id, equator_enterprise.unit_id) + ) +FROM equator_enterprise + , nile_legal_unit; + remove_ephemeral_data_from_hierarchy +-------------------------------------- + {} +(1 row) + +\echo "Refreshing materialized views" +"Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + view_name +--------------------------- + statistical_unit + activity_category_used + region_used + sector_used + data_source_used + legal_form_used + country_used + statistical_unit_facet + statistical_history + statistical_history_facet +(10 rows) + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 4 | 2 | 3 +(1 row) + +SELECT unit_type, name, external_idents +FROM statistical_unit ORDER BY unit_type,name; + unit_type | name | external_idents +---------------+-------------------------------+---------------------------------------------------- + establishment | EQUATOR GLOBE SOLUTIONS | {"tax_ident": "92812760140", "stat_ident": "2001"} + establishment | NILE PEARL WATER | {"tax_ident": "92212760144", "stat_ident": "2000"} + establishment | THE EQUATOR GLOBE SOLUTIONS | {"tax_ident": "82812760140", "stat_ident": "3001"} + establishment | THE INFORMAL NILE PEARL WATER | {"tax_ident": "82212760144", "stat_ident": "3000"} + legal_unit | EQUATOR GLOBE SOLUTIONS | {"tax_ident": "2812760140", "stat_ident": "1001"} + legal_unit | NILE PEARL WATER | {"tax_ident": "2212760144", "stat_ident": "1000"} + enterprise | EQUATOR GLOBE SOLUTIONS | {"tax_ident": "2812760140", "stat_ident": "1001"} + enterprise | THE EQUATOR GLOBE SOLUTIONS | {"tax_ident": "82812760140", "stat_ident": "3001"} + enterprise | THE INFORMAL NILE PEARL WATER | {"tax_ident": "82212760144", "stat_ident": "3000"} +(9 rows) + +\echo "Test statistical_unit_hierarchy after" +"Test statistical_unit_hierarchy after" +\echo "Test statistical_unit_hierarchy - for Nile Pearl Water (enterprise) - contains nothing" +"Test statistical_unit_hierarchy - for Nile Pearl Water (enterprise) - contains nothing" +WITH selected_enterprise AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '1000' + AND unit_type = 'enterprise' + LIMIT 1 +) +SELECT jsonb_pretty( + public.remove_ephemeral_data_from_hierarchy( + public.statistical_unit_hierarchy( + 'enterprise', + (SELECT unit_id FROM selected_enterprise), + '2024-01-01'::DATE + ) + ) + ) AS statistical_unit_hierarchy; + statistical_unit_hierarchy +---------------------------- + { + + } +(1 row) + +\echo "Test statistical_unit_hierarchy - for Nile Pearl Water (legal unit)" +"Test statistical_unit_hierarchy - for Nile Pearl Water (legal unit)" +WITH selected_legal_unit AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '1000' + AND unit_type = 'legal_unit' + LIMIT 1 +) +SELECT jsonb_pretty( + public.remove_ephemeral_data_from_hierarchy( + public.statistical_unit_hierarchy( + 'legal_unit', + (SELECT unit_id FROM selected_legal_unit), + '2024-01-01'::DATE + ) + ) + ) AS statistical_unit_hierarchy; + statistical_unit_hierarchy +----------------------------------------------------------------------------------------------------------------------------------- + { + + "enterprise": { + + "notes": null, + + "active": true, + + "legal_unit": [ + + { + + "name": "NILE PEARL WATER", + + "notes": null, + + "active": true, + + "sector": { + + "code": "6100", + + "name": "Central government", + + "path": "DOMESTIC.C.6100", + + "label": "DOMESTICC6100", + + "active": true, + + "custom": true, + + "description": "Central government" + + }, + + "activity": [ + + { + + "type": "primary", + + "valid_to": "infinity", + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "activity_category": { + + "code": "4752", + + "name": "Retail sale of hardware, paints and glass in specialized stores", + + "path": "G.47.5.2", + + "label": "G4752", + + "level": 4, + + "active": true, + + "custom": false, + + "description": null, + + "activity_category_standard": { + + "code": "isic_v4", + + "name": "ISIC 4", + + "obsolete": false, + + "description": "ISIC Version 4", + + "code_pattern": "digits" + + } + + } + + } + + ], + + "location": [ + + { + + "type": "physical", + + "region": { + + "code": "225613", + + "name": "Entebbe", + + "path": "2.256.13", + + "label": "225613", + + "level": 3, + + "center_altitude": null, + + "center_latitude": null, + + "center_longitude": null + + }, + + "country": { + + "name": "Uganda", + + "iso_2": "UG", + + "iso_3": "UGA", + + "active": true, + + "custom": false, + + "iso_num": "800" + + }, + + "altitude": null, + + "latitude": null, + + "valid_to": "infinity", + + "longitude": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "postal_code": null, + + "valid_after": "2023-12-31", + + "postal_place": null, + + "address_part1": null, + + "address_part2": null, + + "address_part3": null + + } + + ], + + "valid_to": "infinity", + + "birth_date": "2016-01-10", + + "death_date": null, + + "legal_form": { + + "code": "4", + + "name": "Cooperative", + + "active": true, + + "custom": true + + }, + + "reorg_date": null, + + "short_name": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "web_address": null, + + "edit_comment": "Batch import", + + "telephone_no": null, + + "email_address": null, + + "establishment": [ + + { + + "name": "NILE PEARL WATER", + + "notes": null, + + "active": true, + + "activity": [ + + { + + "type": "primary", + + "valid_to": "infinity", + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "activity_category": { + + "code": "4752", + + "name": "Retail sale of hardware, paints and glass in specialized stores", + + "path": "G.47.5.2", + + "label": "G4752", + + "level": 4, + + "active": true, + + "custom": false, + + "description": null, + + "activity_category_standard": { + + "code": "isic_v4", + + "name": "ISIC 4", + + "obsolete": false, + + "description": "ISIC Version 4", + + "code_pattern": "digits" + + } + + } + + } + + ], + + "location": [ + + { + + "type": "physical", + + "region": { + + "code": "225613", + + "name": "Entebbe", + + "path": "2.256.13", + + "label": "225613", + + "level": 3, + + "center_altitude": null, + + "center_latitude": null, + + "center_longitude": null + + }, + + "country": { + + "name": "Uganda", + + "iso_2": "UG", + + "iso_3": "UGA", + + "active": true, + + "custom": false, + + "iso_num": "800" + + }, + + "altitude": null, + + "latitude": null, + + "valid_to": "infinity", + + "longitude": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "postal_code": null, + + "valid_after": "2023-12-31", + + "postal_place": null, + + "address_part1": null, + + "address_part2": null, + + "address_part3": null + + } + + ], + + "valid_to": "infinity", + + "birth_date": null, + + "death_date": null, + + "reorg_date": null, + + "short_name": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "web_address": null, + + "edit_comment": "Batch import", + + "telephone_no": null, + + "email_address": null, + + "invalid_codes": null, + + "stat_for_unit": [ + + { + + "valid_to": "infinity", + + "value_int": 0, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "employees", + + "name": "Number of people employed", + + "type": "int", + + "archived": false, + + "priority": 1, + + "frequency": "yearly", + + "description": "The number of people receiving an official salary with government reporting."+ + } + + }, + + { + + "valid_to": "infinity", + + "value_int": 0, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "turnover", + + "name": "Turnover", + + "type": "int", + + "archived": false, + + "priority": 2, + + "frequency": "yearly", + + "description": "The amount (EUR)" + + } + + } + + ], + + "free_econ_zone": null, + + "external_idents": { + + "tax_ident": "92212760144", + + "stat_ident": "2000" + + }, + + "parent_org_link": null, + + "reorg_references": null, + + "primary_for_legal_unit": true + + } + + ], + + "invalid_codes": null, + + "stat_for_unit": [ + + { + + "valid_to": "infinity", + + "value_int": 2, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "employees", + + "name": "Number of people employed", + + "type": "int", + + "archived": false, + + "priority": 1, + + "frequency": "yearly", + + "description": "The number of people receiving an official salary with government reporting." + + } + + }, + + { + + "valid_to": "infinity", + + "value_int": 9000000, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "turnover", + + "name": "Turnover", + + "type": "int", + + "archived": false, + + "priority": 2, + + "frequency": "yearly", + + "description": "The amount (EUR)" + + } + + } + + ], + + "free_econ_zone": null, + + "external_idents": { + + "tax_ident": "2212760144", + + "stat_ident": "1000" + + }, + + "parent_org_link": null, + + "reorg_references": null, + + "primary_for_enterprise": true + + } + + ], + + "short_name": null, + + "edit_comment": "Batch import", + + "establishment": [ + + { + + "name": "THE EQUATOR GLOBE SOLUTIONS", + + "notes": null, + + "active": true, + + "activity": [ + + { + + "type": "primary", + + "valid_to": "infinity", + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "activity_category": { + + "code": "5610", + + "name": "Restaurants and mobile food service activities", + + "path": "I.56.1.0", + + "label": "I5610", + + "level": 4, + + "active": true, + + "custom": false, + + "description": null, + + "activity_category_standard": { + + "code": "isic_v4", + + "name": "ISIC 4", + + "obsolete": false, + + "description": "ISIC Version 4", + + "code_pattern": "digits" + + } + + } + + } + + ], + + "location": [ + + { + + "type": "physical", + + "region": { + + "code": "225602", + + "name": "Kampala", + + "path": "2.256.02", + + "label": "225602", + + "level": 3, + + "center_altitude": null, + + "center_latitude": null, + + "center_longitude": null + + }, + + "country": { + + "name": "Uganda", + + "iso_2": "UG", + + "iso_3": "UGA", + + "active": true, + + "custom": false, + + "iso_num": "800" + + }, + + "altitude": null, + + "latitude": null, + + "valid_to": "infinity", + + "longitude": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "postal_code": null, + + "valid_after": "2023-12-31", + + "postal_place": null, + + "address_part1": null, + + "address_part2": null, + + "address_part3": null + + } + + ], + + "valid_to": "infinity", + + "birth_date": null, + + "death_date": null, + + "reorg_date": null, + + "short_name": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "web_address": null, + + "edit_comment": "Batch import", + + "telephone_no": null, + + "email_address": null, + + "invalid_codes": null, + + "stat_for_unit": [ + + { + + "valid_to": "infinity", + + "value_int": 2, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "employees", + + "name": "Number of people employed", + + "type": "int", + + "archived": false, + + "priority": 1, + + "frequency": "yearly", + + "description": "The number of people receiving an official salary with government reporting." + + } + + }, + + { + + "valid_to": "infinity", + + "value_int": 4400, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "turnover", + + "name": "Turnover", + + "type": "int", + + "archived": false, + + "priority": 2, + + "frequency": "yearly", + + "description": "The amount (EUR)" + + } + + } + + ], + + "free_econ_zone": null, + + "external_idents": { + + "tax_ident": "82812760140", + + "stat_ident": "3001" + + }, + + "parent_org_link": null, + + "reorg_references": null, + + "primary_for_legal_unit": null + + } + + ] + + } + + } +(1 row) + +\echo "Test statistical_unit_hierarchy - for The Equator Globe Solutions (informal establishment)" +"Test statistical_unit_hierarchy - for The Equator Globe Solutions (informal establishment)" +WITH selected_establishment AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '3001' + AND unit_type = 'establishment' + LIMIT 1 +) +SELECT jsonb_pretty( + public.remove_ephemeral_data_from_hierarchy( + public.statistical_unit_hierarchy( + 'establishment', + (SELECT unit_id FROM selected_establishment), + '2024-01-01'::DATE + ) + ) + ) AS statistical_unit_hierarchy; + statistical_unit_hierarchy +----------------------------------------------------------------------------------------------------------------------------------- + { + + "enterprise": { + + "notes": null, + + "active": true, + + "legal_unit": [ + + { + + "name": "NILE PEARL WATER", + + "notes": null, + + "active": true, + + "sector": { + + "code": "6100", + + "name": "Central government", + + "path": "DOMESTIC.C.6100", + + "label": "DOMESTICC6100", + + "active": true, + + "custom": true, + + "description": "Central government" + + }, + + "activity": [ + + { + + "type": "primary", + + "valid_to": "infinity", + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "activity_category": { + + "code": "4752", + + "name": "Retail sale of hardware, paints and glass in specialized stores", + + "path": "G.47.5.2", + + "label": "G4752", + + "level": 4, + + "active": true, + + "custom": false, + + "description": null, + + "activity_category_standard": { + + "code": "isic_v4", + + "name": "ISIC 4", + + "obsolete": false, + + "description": "ISIC Version 4", + + "code_pattern": "digits" + + } + + } + + } + + ], + + "location": [ + + { + + "type": "physical", + + "region": { + + "code": "225613", + + "name": "Entebbe", + + "path": "2.256.13", + + "label": "225613", + + "level": 3, + + "center_altitude": null, + + "center_latitude": null, + + "center_longitude": null + + }, + + "country": { + + "name": "Uganda", + + "iso_2": "UG", + + "iso_3": "UGA", + + "active": true, + + "custom": false, + + "iso_num": "800" + + }, + + "altitude": null, + + "latitude": null, + + "valid_to": "infinity", + + "longitude": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "postal_code": null, + + "valid_after": "2023-12-31", + + "postal_place": null, + + "address_part1": null, + + "address_part2": null, + + "address_part3": null + + } + + ], + + "valid_to": "infinity", + + "birth_date": "2016-01-10", + + "death_date": null, + + "legal_form": { + + "code": "4", + + "name": "Cooperative", + + "active": true, + + "custom": true + + }, + + "reorg_date": null, + + "short_name": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "web_address": null, + + "edit_comment": "Batch import", + + "telephone_no": null, + + "email_address": null, + + "establishment": [ + + { + + "name": "NILE PEARL WATER", + + "notes": null, + + "active": true, + + "activity": [ + + { + + "type": "primary", + + "valid_to": "infinity", + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "activity_category": { + + "code": "4752", + + "name": "Retail sale of hardware, paints and glass in specialized stores", + + "path": "G.47.5.2", + + "label": "G4752", + + "level": 4, + + "active": true, + + "custom": false, + + "description": null, + + "activity_category_standard": { + + "code": "isic_v4", + + "name": "ISIC 4", + + "obsolete": false, + + "description": "ISIC Version 4", + + "code_pattern": "digits" + + } + + } + + } + + ], + + "location": [ + + { + + "type": "physical", + + "region": { + + "code": "225613", + + "name": "Entebbe", + + "path": "2.256.13", + + "label": "225613", + + "level": 3, + + "center_altitude": null, + + "center_latitude": null, + + "center_longitude": null + + }, + + "country": { + + "name": "Uganda", + + "iso_2": "UG", + + "iso_3": "UGA", + + "active": true, + + "custom": false, + + "iso_num": "800" + + }, + + "altitude": null, + + "latitude": null, + + "valid_to": "infinity", + + "longitude": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "postal_code": null, + + "valid_after": "2023-12-31", + + "postal_place": null, + + "address_part1": null, + + "address_part2": null, + + "address_part3": null + + } + + ], + + "valid_to": "infinity", + + "birth_date": null, + + "death_date": null, + + "reorg_date": null, + + "short_name": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "web_address": null, + + "edit_comment": "Batch import", + + "telephone_no": null, + + "email_address": null, + + "invalid_codes": null, + + "stat_for_unit": [ + + { + + "valid_to": "infinity", + + "value_int": 0, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "employees", + + "name": "Number of people employed", + + "type": "int", + + "archived": false, + + "priority": 1, + + "frequency": "yearly", + + "description": "The number of people receiving an official salary with government reporting."+ + } + + }, + + { + + "valid_to": "infinity", + + "value_int": 0, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "turnover", + + "name": "Turnover", + + "type": "int", + + "archived": false, + + "priority": 2, + + "frequency": "yearly", + + "description": "The amount (EUR)" + + } + + } + + ], + + "free_econ_zone": null, + + "external_idents": { + + "tax_ident": "92212760144", + + "stat_ident": "2000" + + }, + + "parent_org_link": null, + + "reorg_references": null, + + "primary_for_legal_unit": true + + } + + ], + + "invalid_codes": null, + + "stat_for_unit": [ + + { + + "valid_to": "infinity", + + "value_int": 2, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "employees", + + "name": "Number of people employed", + + "type": "int", + + "archived": false, + + "priority": 1, + + "frequency": "yearly", + + "description": "The number of people receiving an official salary with government reporting." + + } + + }, + + { + + "valid_to": "infinity", + + "value_int": 9000000, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "turnover", + + "name": "Turnover", + + "type": "int", + + "archived": false, + + "priority": 2, + + "frequency": "yearly", + + "description": "The amount (EUR)" + + } + + } + + ], + + "free_econ_zone": null, + + "external_idents": { + + "tax_ident": "2212760144", + + "stat_ident": "1000" + + }, + + "parent_org_link": null, + + "reorg_references": null, + + "primary_for_enterprise": true + + } + + ], + + "short_name": null, + + "edit_comment": "Batch import", + + "establishment": [ + + { + + "name": "THE EQUATOR GLOBE SOLUTIONS", + + "notes": null, + + "active": true, + + "activity": [ + + { + + "type": "primary", + + "valid_to": "infinity", + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "activity_category": { + + "code": "5610", + + "name": "Restaurants and mobile food service activities", + + "path": "I.56.1.0", + + "label": "I5610", + + "level": 4, + + "active": true, + + "custom": false, + + "description": null, + + "activity_category_standard": { + + "code": "isic_v4", + + "name": "ISIC 4", + + "obsolete": false, + + "description": "ISIC Version 4", + + "code_pattern": "digits" + + } + + } + + } + + ], + + "location": [ + + { + + "type": "physical", + + "region": { + + "code": "225602", + + "name": "Kampala", + + "path": "2.256.02", + + "label": "225602", + + "level": 3, + + "center_altitude": null, + + "center_latitude": null, + + "center_longitude": null + + }, + + "country": { + + "name": "Uganda", + + "iso_2": "UG", + + "iso_3": "UGA", + + "active": true, + + "custom": false, + + "iso_num": "800" + + }, + + "altitude": null, + + "latitude": null, + + "valid_to": "infinity", + + "longitude": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "postal_code": null, + + "valid_after": "2023-12-31", + + "postal_place": null, + + "address_part1": null, + + "address_part2": null, + + "address_part3": null + + } + + ], + + "valid_to": "infinity", + + "birth_date": null, + + "death_date": null, + + "reorg_date": null, + + "short_name": null, + + "valid_from": "2024-01-01", + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "web_address": null, + + "edit_comment": "Batch import", + + "telephone_no": null, + + "email_address": null, + + "invalid_codes": null, + + "stat_for_unit": [ + + { + + "valid_to": "infinity", + + "value_int": 2, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "employees", + + "name": "Number of people employed", + + "type": "int", + + "archived": false, + + "priority": 1, + + "frequency": "yearly", + + "description": "The number of people receiving an official salary with government reporting." + + } + + }, + + { + + "valid_to": "infinity", + + "value_int": 4400, + + "valid_from": "2024-01-01", + + "value_bool": null, + + "data_source": { + + "code": "nlr", + + "name": "National Legal Business Registry", + + "active": true, + + "custom": false + + }, + + "valid_after": "2023-12-31", + + "value_float": null, + + "value_string": null, + + "stat_definition": { + + "code": "turnover", + + "name": "Turnover", + + "type": "int", + + "archived": false, + + "priority": 2, + + "frequency": "yearly", + + "description": "The amount (EUR)" + + } + + } + + ], + + "free_econ_zone": null, + + "external_idents": { + + "tax_ident": "82812760140", + + "stat_ident": "3001" + + }, + + "parent_org_link": null, + + "reorg_references": null, + + "primary_for_legal_unit": null + + } + + ] + + } + + } +(1 row) + +\echo "Test statistical_unit_hierarchy - for The Equator Globe Solutions (enterprise)" +"Test statistical_unit_hierarchy - for The Equator Globe Solutions (enterprise)" +WITH selected_enterprise AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '3001' + AND unit_type = 'enterprise' + LIMIT 1 +) +SELECT jsonb_pretty( + public.remove_ephemeral_data_from_hierarchy( + public.statistical_unit_hierarchy( + 'enterprise', + (SELECT unit_id FROM selected_enterprise), + '2024-01-01'::DATE + ) + ) + ) AS statistical_unit_hierarchy; + diff --git a/test/sql/33_informal_formal_connections.sql b/test/sql/33_informal_formal_connections.sql new file mode 100644 index 000000000..e49002654 --- /dev/null +++ b/test/sql/33_informal_formal_connections.sql @@ -0,0 +1,193 @@ +BEGIN; +\echo "Setting up Statbus using the web provided examples" +\echo "User selected the Activity Category Standard" +INSERT INTO settings(activity_category_standard_id,only_one_setting) +SELECT id, true FROM activity_category_standard WHERE code = 'isic_v4' +ON CONFLICT (only_one_setting) +DO UPDATE SET + activity_category_standard_id =(SELECT id FROM activity_category_standard WHERE code = 'isic_v4') + WHERE settings.id = EXCLUDED.id; +; + +\echo "User uploads the sample activity categories" +\copy public.activity_category_available_custom(path,name) FROM 'app/public/demo/activity_custom_isic_demo.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +\echo "User uploads the sample regions" +\copy public.region_upload(path, name) FROM 'app/public/demo/regions_demo.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +\echo "User uploads the sample legal forms" +\copy public.legal_form_custom_only(code,name) FROM 'app/public/demo/legal_forms_demo.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +\echo "User uploads the sample sectors" +\copy public.sector_custom_only(path,name,description) FROM 'app/public/demo/sectors_demo.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + + + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + + +\echo "User uploads legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,stat_ident,name,birth_date,physical_region_code,physical_country_iso_2,primary_activity_category_code,legal_form_code,sector_code,employees,turnover,data_source_code) FROM 'test/data/31_legal_units.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +\echo "User uploads formal establishments" +\copy public.import_establishment_era_for_legal_unit(valid_from,valid_to,tax_ident,stat_ident,name,physical_region_code,physical_country_iso_2,primary_activity_category_code,employees,turnover,legal_unit_tax_ident,data_source_code) FROM 'test/data/31_formal_establishments.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +\echo "User uploads informal establishments" +\copy public.import_establishment_era_without_legal_unit(valid_from,valid_to,tax_ident,stat_ident,name,physical_region_code,physical_country_iso_2,primary_activity_category_code,employees,turnover,data_source_code) FROM 'test/data/33_informal_establishments.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +SAVEPOINT after_loading_units; + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +SELECT unit_type, name, external_idents +FROM statistical_unit ORDER BY unit_type,name; + + +\echo "Test statistical_unit_hierarchy - for Nile Pearl Water" +WITH selected_enterprise AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '1000' + AND unit_type = 'enterprise' + LIMIT 1 +) +SELECT jsonb_pretty( + public.remove_ephemeral_data_from_hierarchy( + public.statistical_unit_hierarchy( + 'enterprise', + (SELECT unit_id FROM selected_enterprise), + '2024-01-01'::DATE + ) + ) + ) AS statistical_unit_hierarchy; + + +\echo "Test statistical_unit_hierarchy - for The Equator Globe Solutions" +WITH selected_enterprise AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '3001' + AND unit_type = 'enterprise' + LIMIT 1 +) +SELECT jsonb_pretty( + public.remove_ephemeral_data_from_hierarchy( + public.statistical_unit_hierarchy( + 'enterprise', + (SELECT unit_id FROM selected_enterprise), + '2024-01-01'::DATE + ) + ) + ) AS statistical_unit_hierarchy; + +SELECT count(*) FROM public.enterprise; + + +\echo "Connect - Nile Pearl Water - to The Equator Globe Solutions" +WITH equator_enterprise AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '3001' + AND unit_type = 'enterprise' + LIMIT 1 +), nile_legal_unit AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '1000' + AND unit_type = 'legal_unit' + LIMIT 1 +) +SELECT + public.remove_ephemeral_data_from_hierarchy( + connect_legal_unit_to_enterprise(nile_legal_unit.unit_id, equator_enterprise.unit_id) + ) +FROM equator_enterprise + , nile_legal_unit; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +SELECT unit_type, name, external_idents +FROM statistical_unit ORDER BY unit_type,name; + +\echo "Test statistical_unit_hierarchy after" + +\echo "Test statistical_unit_hierarchy - for Nile Pearl Water (enterprise) - contains nothing" +WITH selected_enterprise AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '1000' + AND unit_type = 'enterprise' + LIMIT 1 +) +SELECT jsonb_pretty( + public.remove_ephemeral_data_from_hierarchy( + public.statistical_unit_hierarchy( + 'enterprise', + (SELECT unit_id FROM selected_enterprise), + '2024-01-01'::DATE + ) + ) + ) AS statistical_unit_hierarchy; + +\echo "Test statistical_unit_hierarchy - for Nile Pearl Water (legal unit)" +WITH selected_legal_unit AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '1000' + AND unit_type = 'legal_unit' + LIMIT 1 +) +SELECT jsonb_pretty( + public.remove_ephemeral_data_from_hierarchy( + public.statistical_unit_hierarchy( + 'legal_unit', + (SELECT unit_id FROM selected_legal_unit), + '2024-01-01'::DATE + ) + ) + ) AS statistical_unit_hierarchy; + +\echo "Test statistical_unit_hierarchy - for The Equator Globe Solutions (informal establishment)" +WITH selected_establishment AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '3001' + AND unit_type = 'establishment' + LIMIT 1 +) +SELECT jsonb_pretty( + public.remove_ephemeral_data_from_hierarchy( + public.statistical_unit_hierarchy( + 'establishment', + (SELECT unit_id FROM selected_establishment), + '2024-01-01'::DATE + ) + ) + ) AS statistical_unit_hierarchy; + +\echo "Test statistical_unit_hierarchy - for The Equator Globe Solutions (enterprise)" +WITH selected_enterprise AS ( + SELECT unit_id FROM public.statistical_unit + WHERE external_idents ->> 'stat_ident' = '3001' + AND unit_type = 'enterprise' + LIMIT 1 +) +SELECT jsonb_pretty( + public.remove_ephemeral_data_from_hierarchy( + public.statistical_unit_hierarchy( + 'enterprise', + (SELECT unit_id FROM selected_enterprise), + '2024-01-01'::DATE + ) + ) + ) AS statistical_unit_hierarchy; + +ROLLBACK; \ No newline at end of file