# Project Report Generation Script Documentation

## Overview

This CLI PHP script generates stage-wise reports for projects that are
approved for specific stages. It is optimized for processing large
datasets efficiently using batch processing.

------------------------------------------------------------------------

## How It Works

1.  Accepts CLI arguments (`user_id`, `stage_id`)
2.  Validates inputs
3.  Maps stage to report type(s)
4.  Fetches approved projects from database
5.  Processes records in batches
6.  Generates reports using dynamic classes
7.  Logs success/failure
8.  Outputs summary

------------------------------------------------------------------------

## Command Usage

``` bash
php cli/generate_all_project_initiation_reports.php --user_id=1 --stage_id=project_stage_0
```

### Server Usage
``` bash
sudo -u www-data /usr/bin/php cli/generate_all_project_initiation_reports.php --user_id=1 --stage_id=project_stage_0
```

------------------------------------------------------------------------

## Parameters

### Required

-   `--user_id`\
    User executing the script

-   `--stage_id`\
    Stage identifier

### Allowed Stage Values

-   project_stage_0
-   project_stage_1
-   project_stage_2
-   project_stage_3
-   project_stage_4

------------------------------------------------------------------------

### Optional

-   `--is_update_regeneration_date=Yes|No`
-   `--report_generation_date="YYYY-MM-DD HH:MM:SS"`

------------------------------------------------------------------------

## Internal Flow

### Fetch Projects

``` sql
SELECT DISTINCT project_id
FROM tbl_project_stages
WHERE stage_id = ?
AND stage_status = 'stage_status_4'
```

------------------------------------------------------------------------

### Batch Processing

``` sql
WHERE project_id > last_id
ORDER BY project_id ASC
LIMIT 500
```

------------------------------------------------------------------------

### Report Execution

``` php
$report = new ReportClass($jsonObject);
$result = $report->generate();
```

------------------------------------------------------------------------

## Performance Features

-   Cursor-based pagination
-   Constant memory usage
-   Retry mechanism
-   Logging with timestamps
-   Fail-safe processing

------------------------------------------------------------------------

## Exit Codes

-   `0` → Success
-   `1` → Input/Setup error
-   `2` → Completed with failures

------------------------------------------------------------------------

## Summary

This script is production-ready, scalable, and optimized for handling
millions of records efficiently.
