Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/reports/database/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/reports/database/operational_statistics_tables.sql

-- =====================================================
-- OPERATIONAL STATISTICS DATABASE TABLES
-- =====================================================
-- Creates 6 tables for Operational Statistics module
-- Following the structure of Demography 1 module
-- =====================================================

-- Tab 1: Assembly & Worship
CREATE TABLE IF NOT EXISTS assembly_worship_data (
    id INT AUTO_INCREMENT PRIMARY KEY,
    area_id INT NOT NULL,
    district_id INT NOT NULL,
    assembly_id INT NOT NULL,
    
    -- Assembly Statistics
    new_assemblies_opened INT DEFAULT 0,
    assemblies_closed INT DEFAULT 0,
    
    -- Home Cells
    opened_home_cells INT DEFAULT 0,
    closed_home_cells INT DEFAULT 0,
    home_cell_meetings_held INT DEFAULT 0,
    home_cell_male_attendance INT DEFAULT 0,
    home_cell_female_attendance INT DEFAULT 0,
    
    -- Study Groups
    opened_study_groups INT DEFAULT 0,
    closed_study_groups INT DEFAULT 0,
    study_group_leaders INT DEFAULT 0,
    study_group_meetings_held INT DEFAULT 0,
    study_group_male_attendance INT DEFAULT 0,
    study_group_female_attendance INT DEFAULT 0,
    
    -- Bible Readings
    public_bible_readings INT DEFAULT 0,
    
    -- Auto-calculated totals
    total_home_cell_attendance INT GENERATED ALWAYS AS (home_cell_male_attendance + home_cell_female_attendance) STORED,
    total_study_group_attendance INT GENERATED ALWAYS AS (study_group_male_attendance + study_group_female_attendance) STORED,
    
    -- Audit fields
    created_by INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by INT NULL,
    updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
    
    -- Constraints
    FOREIGN KEY (area_id) REFERENCES areas(id) ON DELETE CASCADE,
    FOREIGN KEY (district_id) REFERENCES districts(id) ON DELETE CASCADE,
    FOREIGN KEY (assembly_id) REFERENCES assemblies(id) ON DELETE CASCADE,
    FOREIGN KEY (created_by) REFERENCES users(id),
    FOREIGN KEY (updated_by) REFERENCES users(id),
    UNIQUE KEY unique_assembly_worship (assembly_id),
    INDEX idx_area (area_id),
    INDEX idx_district (district_id),
    INDEX idx_assembly (assembly_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Tab 2: Outreach & Souls
CREATE TABLE IF NOT EXISTS outreach_souls_data (
    id INT AUTO_INCREMENT PRIMARY KEY,
    area_id INT NOT NULL,
    district_id INT NOT NULL,
    assembly_id INT NOT NULL,
    
    -- Outreach Programs
    total_outreach_programs INT DEFAULT 0,
    
    -- Souls Won
    adult_souls_non_cop INT DEFAULT 0,
    gospel_sunday_souls INT DEFAULT 0,
    children_souls INT DEFAULT 0,
    other_cop_souls INT DEFAULT 0,
    hum_souls INT DEFAULT 0,
    mpwd_souls INT DEFAULT 0,
    chaplaincy_souls INT DEFAULT 0,
    som_souls INT DEFAULT 0,
    digital_space_souls INT DEFAULT 0,
    
    -- Backsliders
    backsliders_won_back INT DEFAULT 0,
    backsliders_being_followed INT DEFAULT 0,
    
    -- Auto-calculated total
    total_souls_won INT GENERATED ALWAYS AS (
        adult_souls_non_cop + gospel_sunday_souls + children_souls + 
        other_cop_souls + hum_souls + mpwd_souls + chaplaincy_souls + 
        som_souls + digital_space_souls
    ) STORED,
    
    -- Audit fields
    created_by INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by INT NULL,
    updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
    
    -- Constraints
    FOREIGN KEY (area_id) REFERENCES areas(id) ON DELETE CASCADE,
    FOREIGN KEY (district_id) REFERENCES districts(id) ON DELETE CASCADE,
    FOREIGN KEY (assembly_id) REFERENCES assemblies(id) ON DELETE CASCADE,
    FOREIGN KEY (created_by) REFERENCES users(id),
    FOREIGN KEY (updated_by) REFERENCES users(id),
    UNIQUE KEY unique_assembly_outreach (assembly_id),
    INDEX idx_area (area_id),
    INDEX idx_district (district_id),
    INDEX idx_assembly (assembly_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Tab 3: Baptism & Events
CREATE TABLE IF NOT EXISTS baptism_events_data (
    id INT AUTO_INCREMENT PRIMARY KEY,
    area_id INT NOT NULL,
    district_id INT NOT NULL,
    assembly_id INT NOT NULL,
    
    -- Water Baptism
    children_baptised_13_male INT DEFAULT 0,
    children_baptised_13_female INT DEFAULT 0,
    teens_baptised_13_19_male INT DEFAULT 0,
    teens_baptised_13_19_female INT DEFAULT 0,
    young_adults_baptised_20_35_male INT DEFAULT 0,
    young_adults_baptised_20_35_female INT DEFAULT 0,
    adults_baptised_above_35_male INT DEFAULT 0,
    adults_baptised_above_35_female INT DEFAULT 0,
    
    -- Holy Spirit Baptism
    new_converts_baptised_holy_spirit INT DEFAULT 0,
    old_members_baptised_holy_spirit INT DEFAULT 0,
    
    -- Births & Dedications
    male_births INT DEFAULT 0,
    female_births INT DEFAULT 0,
    male_children_dedicated INT DEFAULT 0,
    female_children_dedicated INT DEFAULT 0,
    
    -- Deaths
    male_children_deaths INT DEFAULT 0,
    female_children_deaths INT DEFAULT 0,
    male_teens_deaths_13_19 INT DEFAULT 0,
    female_teens_deaths_13_19 INT DEFAULT 0,
    male_young_adults_deaths_20_35 INT DEFAULT 0,
    female_young_adults_deaths_20_35 INT DEFAULT 0,
    male_adults_deaths_above_35 INT DEFAULT 0,
    female_adults_deaths_above_35 INT DEFAULT 0,
    
    -- Auto-calculated totals
    total_water_baptised INT GENERATED ALWAYS AS (
        children_baptised_13_male + children_baptised_13_female +
        teens_baptised_13_19_male + teens_baptised_13_19_female +
        young_adults_baptised_20_35_male + young_adults_baptised_20_35_female +
        adults_baptised_above_35_male + adults_baptised_above_35_female
    ) STORED,
    total_holy_spirit_baptised INT GENERATED ALWAYS AS (
        new_converts_baptised_holy_spirit + old_members_baptised_holy_spirit
    ) STORED,
    total_births INT GENERATED ALWAYS AS (male_births + female_births) STORED,
    total_dedications INT GENERATED ALWAYS AS (male_children_dedicated + female_children_dedicated) STORED,
    total_deaths INT GENERATED ALWAYS AS (
        male_children_deaths + female_children_deaths +
        male_teens_deaths_13_19 + female_teens_deaths_13_19 +
        male_young_adults_deaths_20_35 + female_young_adults_deaths_20_35 +
        male_adults_deaths_above_35 + female_adults_deaths_above_35
    ) STORED,
    
    -- Audit fields
    created_by INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by INT NULL,
    updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
    
    -- Constraints
    FOREIGN KEY (area_id) REFERENCES areas(id) ON DELETE CASCADE,
    FOREIGN KEY (district_id) REFERENCES districts(id) ON DELETE CASCADE,
    FOREIGN KEY (assembly_id) REFERENCES assemblies(id) ON DELETE CASCADE,
    FOREIGN KEY (created_by) REFERENCES users(id),
    FOREIGN KEY (updated_by) REFERENCES users(id),
    UNIQUE KEY unique_assembly_baptism (assembly_id),
    INDEX idx_area (area_id),
    INDEX idx_district (district_id),
    INDEX idx_assembly (assembly_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Tab 4: Worship & Attendance
CREATE TABLE IF NOT EXISTS worship_attendance_data (
    id INT AUTO_INCREMENT PRIMARY KEY,
    area_id INT NOT NULL,
    district_id INT NOT NULL,
    assembly_id INT NOT NULL,
    
    -- Sunday Services
    sunday_attendance INT DEFAULT 0,
    communion_sunday_attendance INT DEFAULT 0,
    communion_participants INT DEFAULT 0,
    
    -- New Convert Classes
    new_convert_classes INT DEFAULT 0,
    new_convert_class_attendees INT DEFAULT 0,
    new_converts_retained INT DEFAULT 0,
    presiding_visits_new_convert_class INT DEFAULT 0,
    
    -- Mid-week Services
    midweek_church_teachings INT DEFAULT 0,
    midweek_church_attendance INT DEFAULT 0,
    weekly_friday_meetings INT DEFAULT 0,
    weekly_friday_attendance INT DEFAULT 0,
    
    -- Special Services
    annual_thematic_teachings INT DEFAULT 0,
    holy_ghost_prayer_sessions INT DEFAULT 0,
    marriage_teachings INT DEFAULT 0,
    blessed_marriages INT DEFAULT 0,
    intergenerational_services INT DEFAULT 0,
    
    -- Audit fields
    created_by INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by INT NULL,
    updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
    
    -- Constraints
    FOREIGN KEY (area_id) REFERENCES areas(id) ON DELETE CASCADE,
    FOREIGN KEY (district_id) REFERENCES districts(id) ON DELETE CASCADE,
    FOREIGN KEY (assembly_id) REFERENCES assemblies(id) ON DELETE CASCADE,
    FOREIGN KEY (created_by) REFERENCES users(id),
    FOREIGN KEY (updated_by) REFERENCES users(id),
    UNIQUE KEY unique_assembly_worship_attendance (assembly_id),
    INDEX idx_area (area_id),
    INDEX idx_district (district_id),
    INDEX idx_assembly (assembly_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Tab 5: Social Interventions
CREATE TABLE IF NOT EXISTS social_interventions_data (
    id INT AUTO_INCREMENT PRIMARY KEY,
    area_id INT NOT NULL,
    district_id INT NOT NULL,
    assembly_id INT NOT NULL,
    
    -- Intervention Types
    tertiary_sponsorship INT DEFAULT 0,
    pre_tertiary_sponsorship INT DEFAULT 0,
    health_support INT DEFAULT 0,
    apprenticeship_support INT DEFAULT 0,
    community_transformation INT DEFAULT 0,
    environmental_care INT DEFAULT 0,
    other_interventions INT DEFAULT 0,
    
    -- Beneficiaries
    male_beneficiaries INT DEFAULT 0,
    female_beneficiaries INT DEFAULT 0,
    community_benefit INT DEFAULT 0,
    
    -- Financial
    amount_spent_human_development DECIMAL(15,2) DEFAULT 0.00,
    amount_spent_non_human DECIMAL(15,2) DEFAULT 0.00,
    
    -- Auto-calculated totals
    total_interventions INT GENERATED ALWAYS AS (
        tertiary_sponsorship + pre_tertiary_sponsorship + health_support +
        apprenticeship_support + community_transformation + environmental_care +
        other_interventions
    ) STORED,
    total_beneficiaries INT GENERATED ALWAYS AS (
        male_beneficiaries + female_beneficiaries + community_benefit
    ) STORED,
    total_amount_spent DECIMAL(15,2) GENERATED ALWAYS AS (
        amount_spent_human_development + amount_spent_non_human
    ) STORED,
    
    -- Audit fields
    created_by INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by INT NULL,
    updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
    
    -- Constraints
    FOREIGN KEY (area_id) REFERENCES areas(id) ON DELETE CASCADE,
    FOREIGN KEY (district_id) REFERENCES districts(id) ON DELETE CASCADE,
    FOREIGN KEY (assembly_id) REFERENCES assemblies(id) ON DELETE CASCADE,
    FOREIGN KEY (created_by) REFERENCES users(id),
    FOREIGN KEY (updated_by) REFERENCES users(id),
    UNIQUE KEY unique_assembly_social (assembly_id),
    INDEX idx_area (area_id),
    INDEX idx_district (district_id),
    INDEX idx_assembly (assembly_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Tab 6: Net Tithes & Missions Offering
CREATE TABLE IF NOT EXISTS tithes_missions_data (
    id INT AUTO_INCREMENT PRIMARY KEY,
    area_id INT NOT NULL,
    district_id INT NOT NULL,
    assembly_id INT NOT NULL,
    
    -- Financial Data
    monthly_net_tithes DECIMAL(15,2) DEFAULT 0.00,
    monthly_missions_offering DECIMAL(15,2) DEFAULT 0.00,
    
    -- Auto-calculated total
    total_monthly_income DECIMAL(15,2) GENERATED ALWAYS AS (
        monthly_net_tithes + monthly_missions_offering
    ) STORED,
    
    -- Audit fields
    created_by INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by INT NULL,
    updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
    
    -- Constraints
    FOREIGN KEY (area_id) REFERENCES areas(id) ON DELETE CASCADE,
    FOREIGN KEY (district_id) REFERENCES districts(id) ON DELETE CASCADE,
    FOREIGN KEY (assembly_id) REFERENCES assemblies(id) ON DELETE CASCADE,
    FOREIGN KEY (created_by) REFERENCES users(id),
    FOREIGN KEY (updated_by) REFERENCES users(id),
    UNIQUE KEY unique_assembly_tithes (assembly_id),
    INDEX idx_area (area_id),
    INDEX idx_district (district_id),
    INDEX idx_assembly (assembly_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists