> For the complete documentation index, see [llms.txt](https://docs.kick.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kick.co/ai/cli/cli-use-case-library/close.md).

# Close

Kick CLI scripts for closing a period: export profit and loss JSON for every entity, compare periods, and archive trial balances.

Use these scripts to export statements while you close a period. All of them only read from Kick.

<details>

<summary><strong>Generate P&#x26;L JSON for every entity</strong> (read)</summary>

To archive profit and loss data for each entity in a workspace, list each entity's Cash ledger with `kick workspaces context`, then export JSON per entity. The report basis comes from the ledger, so these are cash-basis reports. The CLI does not emit PDF; save JSON and convert externally if you need PDF.

**The Script**

```bash
#!/bin/bash
# monthly-pl-reports.sh

WORKSPACE_ID="<workspace-id>"
START_DATE="2026-01-01"
END_DATE="2026-01-31"
OUTPUT_DIR="./reports/${START_DATE}-to-${END_DATE}"

mkdir -p "$OUTPUT_DIR"

kick workspaces context "$WORKSPACE_ID" --output json | \
  jq -r '.ledgers[] | select(.basis == "cash") | "\(.entityId)\t\(.id)\t\(.entityName)"' | \
while IFS=$'\t' read -r entity_id ledger_id entity_name; do
  safe_name=${entity_name// /-}

  echo "Generating P&L for $entity_name (entity $entity_id)..."

  kick --workspace "$WORKSPACE_ID" reports profit-loss \
    --entity "$entity_id" \
    --ledger-id "$ledger_id" \
    --start-date "$START_DATE" \
    --end-date "$END_DATE" \
    --cycle month \
    --output json \
    > "$OUTPUT_DIR/pl-${safe_name}-${START_DATE}.json"

  echo "Saved $OUTPUT_DIR/pl-${safe_name}-${START_DATE}.json"
done

echo "All reports saved in $OUTPUT_DIR"
```

**How to Customize**

* Change `START_DATE` and `END_DATE` to the period you are closing
* Add `--comparisons previous-period` to include a comparison column in the report output
* For accrual-basis reports, change `select(.basis == "cash")` to `select(.basis == "accruals")`. Entities without an Accrual ledger are skipped
* Filter entities in the `jq` step if you only need active clients
* `kick workspaces context` covers up to 50 business entities per workspace

**What It Outputs**

* One JSON file per entity in `./reports/<start>-to-<end>/`
* Filename format: `pl-Entity-Name-2026-01-01.json`

</details>

<details>

<summary><strong>Compare month-over-month with built-in comparisons</strong> (read)</summary>

To see current-period profit and loss beside the previous period for one entity, use the report comparison flags instead of running two separate exports.

**The Script**

```bash
#!/bin/bash
# mom-pl-comparison.sh

WORKSPACE_ID="<workspace-id>"
ENTITY_ID=123
START_DATE="2026-02-01"
END_DATE="2026-02-28"

ledger_id=$(kick --workspace "$WORKSPACE_ID" accounting ledgers get --entity-id "$ENTITY_ID" --output json | jq -r '.ledgers[] | select(.basis == "cash") | .id')

kick --workspace "$WORKSPACE_ID" reports profit-loss \
  --entity "$ENTITY_ID" \
  --ledger-id "$ledger_id" \
  --start-date "$START_DATE" \
  --end-date "$END_DATE" \
  --cycle month \
  --comparisons previous-period \
  --output json | jq .
```

**How to Customize**

* Change `ENTITY_ID` and the date range
* Add `--comparison-diffs absolute` or `--comparison-diffs percentage` to control how differences display
* Run for multiple entities by looping over the ledgers from `kick workspaces context`, as in the P\&L script above

**What It Outputs**

* Formatted JSON profit and loss with a previous-period comparison column
* Pipe through `jq` to extract the rows you need for a spreadsheet

</details>

<details>

<summary><strong>Archive trial balance before statements</strong> (read)</summary>

To snapshot trial balance rows before you prepare statements, export JSON for one entity.

**The Script**

```bash
#!/bin/bash
# trial-balance-export.sh

WORKSPACE_ID="<workspace-id>"
ENTITY_ID=123
START_DATE="2026-01-01"
END_DATE="2026-01-31"
OUTPUT_FILE="trial-balance-${ENTITY_ID}-${END_DATE}.json"

ledger_id=$(kick --workspace "$WORKSPACE_ID" accounting ledgers get --entity-id "$ENTITY_ID" --output json | jq -r '.ledgers[] | select(.basis == "cash") | .id')

kick --workspace "$WORKSPACE_ID" reports trial-balance \
  --entity "$ENTITY_ID" \
  --ledger-id "$ledger_id" \
  --start-date "$START_DATE" \
  --end-date "$END_DATE" \
  --cycle month \
  --limit 250 \
  --output json \
  > "$OUTPUT_FILE"

if [ "$(jq '.hasMore' "$OUTPUT_FILE")" = "true" ]; then
  echo "Saved the first 250 rows only. Get the rest with --cursor $(jq -r '.nextCursor' "$OUTPUT_FILE")" >&2
  exit 1
fi

echo "Saved $OUTPUT_FILE"
```

**How to Customize**

* Add `--fields` to limit columns when the full row set is too large
* The trial balance returns 100 rows per call by default and at most 250. If it has more than 250 rows, the script stops and prints the `--cursor` value for the next call

**What It Outputs**

* A JSON file with trial balance rows for the entity and period

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.kick.co/ai/cli/cli-use-case-library/close.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
