# Rebuild quarter KPIs + EDM batch import

This guide documents two Artisan commands:

- `lnc:rebuild_quarters_kpi` (class `App\Console\Commands\RebuildAllQuarters`)
- `lnc:import-edm` (class `App\Console\Commands\import_edm`)

## `lnc:rebuild_quarters_kpi` (RebuildAllQuarters)

### What it does

- **Always runs the EDM batch import first** by directly calling `import_edm->handle()` at the beginning of the command.
- Recalculates **Quarter KPI** for companies using `App\Services\CalcQuarterService::calc1CompanyQuarter(...)`.
- Shows a CLI progress bar and writes job logs to `storage/logs/job_logs.log`.

### Command signature

```bash
php artisan lnc:rebuild_quarters_kpi {--begin-date=} {--last-updates} {--only-cagr}
```

### Options

- **`--last-updates`**: Rebuild only for companies whose non-quarter KPIs were updated in the **last 24 hours**.
  - Internally sets `beginDate = now() - 24h`.
- **`--begin-date=DD/MM/YYYY`**: Rebuild only for companies whose non-quarter KPIs were updated on/after the given date.
  - Expected format is **`d/m/Y`** (example: `21/04/2026`).
- **`--only-cagr`**: Passed through to `CalcQuarterService::calc1CompanyQuarter(...)` to restrict calculations to CAGR-related metrics (implementation detail lives in `CalcQuarterService`).

### How it selects companies (when filtering)

If `--last-updates` or `--begin-date` is provided, the command selects companies having at least one KPI such that:

- `kpis.updated_at::date >= :beginDate`
- `kpis.report_period <> 'QUARTER'`

If no date option is provided, it processes **all companies** (chunked by 100).

### Examples

Rebuild all companies:

```bash
php artisan lnc:rebuild_quarters_kpi
```

Rebuild only companies updated in the last 24 hours:

```bash
php artisan lnc:rebuild_quarters_kpi --last-updates
```

Rebuild only companies updated since a specific date:

```bash
php artisan lnc:rebuild_quarters_kpi --begin-date=21/04/2026
```

Rebuild only CAGR (same filters apply):

```bash
php artisan lnc:rebuild_quarters_kpi --only-cagr
php artisan lnc:rebuild_quarters_kpi --last-updates --only-cagr
php artisan lnc:rebuild_quarters_kpi --begin-date=21/04/2026 --only-cagr
```

### Outputs / logs

- **Console**: progress bar + final `Done !`
- **Logs**: `storage/logs/job_logs.log` (logger name: `lnc:rebuild_quarters_kpi`)

## `lnc:import-edm` (import_edm)

### What it does

- Batch-imports EDM tables from CSV files located in `storage/app/edm_batch`.
- For each recognized CSV file, calls:
  - `App\Http\Controllers\API\EdmController::analyse_csv($table, $csvContent, true)`
    - The last argument is `true` to convert content to **UTF-8**.
- **Deletes each CSV file after processing** (`unlink(...)`).
- Writes logs to `storage/logs/job_logs.log` (logger name: `ImportEDM`).

### Command signature

```bash
php artisan lnc:import-edm
```

### Input folder (required)

The command requires the folder below to exist:

- `storage/app/edm_batch`

If it does not exist, the command prints:

- `the folder "storage/app/edm_batch" does't exists!`

and exits with code `2`.

### Supported CSV filenames (table names)

Only CSV filenames matching the following table names are imported:

- `edm_funds.csv`
- `edm_agents.csv`
- `edm_instruments.csv`
- `edm_deal_link.csv`
- `edm_deals.csv`

Any other `*.csv` file is skipped with:

- `File name not recognized. file skipped -> <filename>`

### Typical workflow

1. Put one or more CSV files into `storage/app/edm_batch/` with one of the supported filenames above.
2. Run:

```bash
php artisan lnc:import-edm
```

3. Check `storage/logs/job_logs.log` for the per-table start/end entries.

### Important notes

- **Destructive**: imported CSV files are deleted after processing.
- **Order**: files are processed in the filesystem iteration order (DirectoryIterator); do not rely on a specific import ordering unless you control the contents accordingly.

