# Full company data export

SELF-2-3 (#192). A company admin downloads everything the company keeps in CropPath as one dated ZIP archive of CSV
and JSON files: to keep a copy, to answer an auditor, or to move to another system. This page describes the archive.
Each archive's `manifest.json` lists its files and, for each CSV, its columns.

## How to get one

In the console: **Settings → Download all data** (`/admin/settings#export`). Over the API (a signed-in session; an
API token or API client is refused):

| Step | Request | Notes |
|---|---|---|
| Ask | `POST /v1/company/exports` (no body) → 202 | Needs a password confirmed within the last 10 minutes. One export per company at a time (409 `export_in_progress`); at most 3 requests in any 24 hours (429 `too_many_exports`, with `Retry-After`). Audited `export.company.request`. |
| Wait | `GET /v1/company/exports` | `status` goes `queued` → `running` → `ready` (or `failed`). The background job builds one archive at a time, usually within minutes. Expired and failed requests are deleted from this list 90 days after they were asked for; the activity log keeps their audit rows. |
| Download | `GET /v1/company/exports/{id}/download` → `application/zip` | Needs a recent password confirmation too. Valid for 24 hours after the archive is ready (`expires_at`), then 410 `export_expired` and the file is deleted. Another company's export answers 404. Audited `export.company` with the archive's size and SHA-256. |

Who may: company admins (`tenant_admin`) and CropPath staff (`platform_admin`) acting for the company. Auditors and
operators may not: the archive holds the whole company, members included. Check the download against the `sha256`
the list shows.

## Archive layout

The file is named `croppath-export-<company>-<request time, UTC, YYYYMMDDTHHMMSSZ>.zip`. Inside, in this order:

| File | Content |
|---|---|
| `profile.json` | `company`: the registration (`id`, `name`, `source`, `created_at`); `profile`: the company profile (SELF-1-5: legal and trading name, address, district, province, country, tax number, contact name, phone and email, website, `household_business`, `public_full_address` and `public_legal_name` as true or false (#208), `logo_sha256`, `updated_at`, `updated_by`), or null when none was saved. |
| `logo.png` or `logo.jpg` | The company logo, when the profile has one. |
| `facilities.csv` | Packing houses and other facilities. |
| `growing_areas.csv` | Growing areas (orchards) of the company. |
| `growers.csv` | Growers (farmers) and their consent. |
| `devices.csv` | Registered devices (scales, knockers, loggers, harvest app installs). |
| `device_placements.csv` | Where CropPath recorded each device as installed, version by version: growing area, install point, commissioning record (installer, photo hash, GPS fix and install time) and removals. |
| `calibrations.csv` | Calibration records of the company's devices (method, reference values, coefficients, performer, accreditation, certificate hash, validity), oldest first; a correction is a new record naming the one it supersedes, and both are listed. |
| `lots.csv` | Lots, by their sequence number. |
| `product_gtins.csv` | The GTIN the company registered for each trade item (product, variety, grade) (#161). |
| `events.csv` | Device events (signed envelopes) as received, oldest first, with their payload. |
| `cartons.csv` | Cartons and their labels. |
| `pallets.csv` | Pallets, each identified by its SSCC (#161). |
| `pallet_cartons.csv` | Which carton is on which pallet (#161). |
| `shipments.csv` | Shipments, their temperature logger check and consignee. |
| `shipment_lots.csv` | Which lots went on which shipment, with the seal number. |
| `lab_reports.csv` | Signed lab reports of the company's lots, with the signed document. |
| `lab_results.csv` | One row per result of those reports. |
| `agronomy_sheets.csv` | The company's agronomic input sheets (#351): drafts, signed, superseded and revoked versions of each series, with their body. |
| `agronomy_sheet_attestations.csv` | One row per signature of those sheets: who signed (the legal name and organisation recorded when they signed), the statement agreed to, the signed hash and when. |
| `control_zones.csv` | The zones of the company's irrigation controllers and their installation facts (#352). |
| `control_rules.csv` | The company's irrigation rules: kind, controller and zones, the signed sheet each reads, windows, approval and state (#352). |
| `control_programs.csv` | The irrigation programs the control engine composed, with their params, provenance and decisions (#352). |
| `control_gate_decisions.csv` | The sensor-health gate's decision for each threshold zone of those programs (#352). |
| `control_step_runs.csv` | The water each controller reported delivered to a zone, one row per reading (#352). |
| `et0_daily.csv` | The reference ET0 of the weather stations' local days, every stored version (#353). |
| `control_water_balance.csv` | The root-zone water balance of each zone and local day (#353). |
| `alarms.csv` | The company's irrigation alarms (#361): each condition occurrence with its state, who acknowledged or shelved it and why. |
| `alarm_events.csv` | One row per change of state of those alarms (their history). |
| `members.csv` | Everyone who belongs to the company, with role and state. |
| `activity_log.csv` | The company's activity log (who did what and when). |
| `manifest.json` | `format` (`croppath-company-export/1`), `company`, `export_id`, `generated_at`, `time_zone` (`UTC`), `encoding` (`UTF-8`), and `files`: each file's `name` and `format`, and for a CSV its `rows` and `columns`. |

## Columns

Every CSV starts with a header row of these names, in this order.

- `facilities.csv`: code, name, slug, gacc_phc_code, address, district, province, lines (JSON array), source,
  approved_at, proposed_at, proposed_by, created_at, updated_at, gln (the facility's GS1 Global Location Number,
  #161).
- `growing_areas.csv`: code, name, gacc_puc_code, crop, variety, area_ha, yield_kg_per_ha, trees, planted_year,
  commune, district, province, polygon (GeoJSON text as stored), grower_id, source, approved_at, created_at,
  updated_at, registry_changed_at, gln (#161).

  Addresses (#273). Since 1 July 2025 Viet Nam has two levels of administrative units, the province and the commune
  (xã, phường or đặc khu), and no district. A facility, growing area or company profile whose address was chosen from
  the official list holds the official names: `province` the province's name, `commune` (growing areas) the commune's
  name, and `district` is empty (null in `profile.json`). A record not chosen from the list yet keeps the text it was
  saved with, its district included, and a company profile in another country keeps its own region and locality in
  `province` and `district`. The columns and their order are unchanged; the official codes, and the commune of a
  facility or company profile, are not in this archive yet.
- `growers.csv`: id, name, cooperative, public_consent, consent_at, consent_date, consent_scope, created_at,
  updated_at.
- `devices.csv`: device_id, display_name, profile, facility, site, status, vendor_model, serial, approval,
  approval_reason, approval_at, approval_by, registered_by, created_at.
- `device_placements.csv`: device_id, version (1, 2, ... per device), mobility (`fixed` or `mobile`), key_protection
  (`hardware` or `software`, as CropPath was told at commissioning), gnss, growing_area, lat, lon, accuracy_m (the
  install point, WGS84, accuracy in metres), fix_at (when the install point's GPS fix was taken), installed_at,
  installer, photo_sha256 (the installer's photo, an attachment of the company), depth_cm, sensor_serial, note,
  removed_at (set on a removal version: when the device was taken out), recorded_at. Who at CropPath recorded a
  version is not included.
- `calibrations.csv`: id, device_id, sensor_serial (the probe's serial, empty when not given), capability (empty: every
  capability of the device), calibrated_at, method (`ph_buffers`, `ec_reference`, `lab_correlation`, `factory` or
  `other`), reference_values and coefficients (JSON as recorded), performer, accreditation (`iso17025_lab`,
  `in_house` or `manufacturer`), certificate_sha256 (the certificate, an attachment of the company), valid_until,
  supersedes (the id of the record this one corrects), created_at. Who recorded a calibration is not included.
- `lots.csv`: seq, code, public_id, facility, product, variety, growing_area, harvest_date, harvest_event_id,
  destination, gtin, status, storage_band, fruit_count, packed_at, packed_season, packed_cartons, packed_fruits,
  packed_kg, created_at, updated_at, closed_at.
- `product_gtins.csv`: id, product, variety and grade (empty: any), gtin (14 digits), allocated (whether CropPath
  allocated it from its GS1 company prefix), created_by, created_at.
- `events.csv`: id, received_at, device_id, site, facility, seq, kind, event_type, event_time (as the device signed
  it), event_ts, status, reasons (JSON), lot, growing_area, payload (the signed envelope, JSON), location_glns (the
  GLNs of the event's facility and growing area when it was received, JSON, #161; empty when neither had one).
- `cartons.csv`: code, lot, carton_no, public_id, gs1_serial, net_mass_kg, contents_sha256, labels_printed_at,
  created_by, created_at, updated_at.
- `pallets.csv`: sscc (18 digits), facility, created_by, created_at, updated_at.
- `pallet_cartons.csv`: carton (a `code` of cartons.csv), pallet (an `sscc` of pallets.csv), lot, added_at.
- `shipments.csv`: code, facility, status, container, border_gate, destination, setpoint_c, tolerance_c,
  logger_device_id, logger_interval_s, logger_check, logger_detail (JSON), consignee_name, consignee_address,
  consignee_country, consignee_gln, consignee_registration_id, created_by, created_at, updated_at, departed_by,
  departed_at, sscc (the container load's SSCC, #161).
- `shipment_lots.csv`: shipment, lot, seal_no, sealed_at, cartons, added_by, added_at.
- `lab_reports.csv`: id, lot, lab, report_no, market, kid, signed_at, passed, report_sha256, submitted_by,
  received_at, document (the signed report, JSON).
- `lab_results.csv`: id, report_id (an `id` of lab_reports.csv), k, analyte, outcome, value, unit, lod, method,
  limit_kind, limit_value, limit_source, passed.
- `agronomy_sheets.csv` (#351): id, series_key (`<crop>:<variety>:<province>`, `*` for every variety or province),
  crop, variety, province_code, version (1, 2, ... per series), status (`draft`, `signed`, `superseded` or
  `revoked`), valid_from, valid_to (local dates, YYYY-MM-DD), body (the `agritrace.agronomy-sheet/1` body as its
  canonical JSON, RFC 8785, exactly as stored), body_sha256 (the SHA-256 of the sheet's signed document, which covers
  the series, version and validity too, not the body alone), created_at, updated_at, signed_at, superseded_at,
  revoked_at. Who created, edited or revoked a sheet and why it was revoked are not included.
- `agronomy_sheet_attestations.csv` (#351): id, sheet_id (an `id` of agronomy_sheets.csv), legal_name and
  organisation (as the agronomist registry held them when the sheet was signed), statement_version,
  statement_text_sha256 (the SHA-256 of the statement's text the agronomist agreed to), body_sha256 (the hash that
  was signed), signed_at, second_factor_at (when the signing session last proved a second factor). The session's
  hash and the request id of a signature are not included.
- `control_zones.csv` (#352): id, device_id, zone (1 to 16), growing_area, name, irrigated_area_m2, nominal_flow_lpm,
  efficiency, max_daily_l, probes (JSON), weather_station (JSON), controller_utc_offset, field_capacity_pct and
  wilting_point_pct (the soil's volumetric water content at field capacity and at the wilting point, #353; empty when
  not entered), created_at, updated_at.
- `control_rules.csv` (#352): id, kind, name, device_id, zones (JSON), sheet_id (an `id` of agronomy_sheets.csv),
  windows (JSON), fallback_rule_id, approval, eval_interval_s, min_interval_s, peer_tolerance_pct, min_probes,
  enabled, state, state_reason, created_at, updated_at, enabled_by (`user-<account id>`, never an email), enabled_at,
  next_run_at and last_run_at (when the engine composes the rule's controller next, and last did), resumed_command_id
  (the id of the person's own set_program command an admin resumed the rules over; a command id, not a person).
- `control_programs.csv` (#352): id, device_id, local_date, status, cause, params (the set_program params, JSON),
  provenance (JSON: the rules, the sheets cited by id, version and SHA-256, the zones' decisions), command_id,
  program_version, proposed_at, expires_at, decided_by (`user-<account id>`; empty for the engine's own decisions),
  decided_at, decision_note, dispatched_at, error, withdrawn_by (`user-<account id>`, #367: who withdrew the program
  from the controller), withdrawn_at, withdraw_reason. A controller's hold (`control_holds`, #367) is not exported:
  it is in force only until its end and then deleted; the withdrawal that set it is in the program row above and in
  the activity log (`device.program.withdraw`, and `device.program.hold_release` for an early release).
- `control_gate_decisions.csv` (#352): id, device_id, zone, rule_id (an `id` of control_rules.csv), program_id (an `id`
  of control_programs.csv; empty for a decision of no program), decided_at, verdict (`healthy`, `fallback` or
  `blocked`), reason, detail (JSON: what the gate read and found). No person is named.
- `control_step_runs.csv` (#352): envelope_id and reading_ix (the reading of the controller's upload it came from),
  device_id, zone, obs_time (the controller's time of the reading), received_at, litres, qc_flag, trust_tier. No
  person is named.
- `et0_daily.csv` (#353): id, device_id (the weather station), local_date, seq (the version of the day), wind_height_m,
  elevation_m, status (`ok`, `needs_krs` or `refused`), reason, et0_mm (empty unless `ok`), inputs (JSON: each input
  with its method, coverage and whether it was estimated), trace (JSON: the intermediate values), computed_at.
- `control_water_balance.csv` (#353): the latest version of each zone, rule and day only: id, device_id, zone,
  local_date, rule_id, seq (that version's number), source (`measured` or `ledger`), dr_mm
  (the root-zone depletion at the end of the day), taw_mm, raw_mm, detail (JSON: the crop's use, effective rain, net
  irrigation and percolation of the day), computed_at.
- `alarms.csv` (#361): id, kind (the alarm catalog's kind, in the alarm philosophy), catalog_version, priority
  (`urgent`, `warning` or `notice`), subject_type (`device`, `zone`, `area`, `rule` or `program`), subject_key,
  device_id, zone, growing_area, facility, raised_at, last_active_at, cleared_at, acked_at, acked_by, ack_note,
  shelved_until, shelved_by, shelve_reason (a reason code), shelve_note, closed_at,
  occurrences, detail (JSON facts: command ids, reasons, measured litres; never coordinates), source_alert_id. A
  person (acked_by, shelved_by) is the account id `user-<id>` of members.csv's user_id, never an email.
- `alarm_events.csv` (#361): id, alarm_id (an `id` of alarms.csv), at, event (`raised`, `reraised`, `cleared`,
  `acked`, `shelved`, `unshelved`, `shelve_expired` or `closed`), actor (`user-<id>`, or `system:alarms` for the
  evaluator), reason (a shelving's reason code; empty otherwise: a person's notes are on the alarm only). The
  request id of an event is not included.
- `members.csv`: membership_id, user_id, email, display_name, role, facilities (JSON array, empty for every
  facility), status (the membership in this company), account_status, two_factor (whether two-step sign-in is on),
  notice_accepted_at, created_at, disabled_at, disabled_by.
- `activity_log.csv`: id, time, actor, action, entity, entity_id, before_hash, details (the row's JSON summary: the
  HTTP status and what changed), request_id.

## Values

- Encoding: UTF-8 without a byte order mark; CSV with comma separators, `"` quoting where needed, and CRLF line
  ends (RFC 4180). To open a CSV in Excel with Vietnamese text intact, use Data → From Text/CSV and pick UTF-8.
- Times: every `*_at` column, `time`, `received_at` and `event_ts` is UTC in ISO 8601 with milliseconds, for example
  `2026-09-30T01:00:00.000Z` (08:00 in Vietnam). `event_time`, `harvest_date`, `consent_date` and `packed_season`
  are text exactly as recorded.
- Numbers as stored: masses in kg, temperatures in °C, lab values in the unit the lab signed. A decimal is written
  with a dot (`1234.5`, `18000.0`).
- Yes/no columns (`public_consent`, `passed`, `two_factor`, `gnss`) are `true` or `false`.
- An empty cell is a value that was never recorded.
- Formula guard: a text cell that starts with `=`, `+`, `-`, `@`, a tab or a carriage return gets a leading `'`, as
  in every other CropPath export, so a spreadsheet never runs it as a formula. Remove that one `'` to get the stored
  text back.
- Rows are in key order (facilities, growing areas, devices, cartons and shipments by code; device placements by
  device and version; lots by sequence; events by receive time; everything else by id).

## What is never in the archive

Password hashes, two-step sign-in secrets and backup codes, API tokens and their hashes, API client secrets, webhook
signing secrets, private keys of facilities or labs, session data (also the session hash and request id a signature
of an agronomic input sheet records, and the request id of an alarm's event), and the hashed IP addresses of the
activity log.
Another company's records never appear, even where a record of the company refers to it.

## Storage and limits

The archive is built in the background, one export at a time, and kept for 24 hours after it is ready; then it is
deleted (an export interrupted by a restart is marked failed after an hour, its files deleted). The request itself (who
asked, when, the archive's size and SHA-256) stays in the list and the activity log. An archive is never copied into
CropPath's backups.
