When to use this endpoint

Use Customer_stockExportTotals from the CRM1 customer Stock Used tab when a user selects Export Totals.

Do not assume this CSV is a serialization of Customer_stockTotalsGet; the two endpoints use different job and form-stock predicates.

Authentication and permissions

The request must authenticate with signed API-key fields or a logged-in CRM session. The handler performs no additional page or role-permission check.

The lookup and any write use the authenticated tenant; an ID from another tenant cannot be addressed through this call.

Parameters

ParameterTypeRequiredDescription
idInteger stringYesPositive customer ID. The endpoint does not verify that the customer exists.

Example request

$csv = FM_api('Customer_stockExportTotals', [
	'id' => '1088',
]);
file_put_contents('northwind-stock-totals.csv', $csv);

Example response

"Stock Item Name",Quantity
"Filter cartridge",5
"Cable 10m",2.5

The response is text/csv. Its attachment filename is <current customer name>_customer_stock_totals.csv; a nonexistent positive ID produces a blank name prefix and a header-only file.

Errors and unusual responses

ResponseCondition
{"error":"not logged in"}No authenticated tenant context is available. Invalid signed credentials can be rejected by the dispatcher before the handler runs.
{"error":"missing or invalid parameter: id"}id is absent, zero, negative, or non-numeric.
Header-only CSVNo qualifying main stock exists or the positive customer ID does not exist.

Legacy application errors may be returned with HTTP 200. Check the decoded response for error, not only the HTTP status.

Behaviour and edge cases

  • Jobs must be non-deleted, have non-zero completion state, target the customer directly (customer_type=0), and have non-NULL main stock text containing id. Unlike the totals JSON endpoints, meta.uses_stock is not required.
  • Rows are queried by completion date descending, but quantities are aggregated before output and no explicit stock-name order is applied.
  • Quantities are summed by stock ID and then keyed by current stock name. Distinct IDs with the same current name overwrite one another in the final map.
  • Malformed stock_used or entries without expected id/amt members can produce legacy warnings rather than a structured JSON error.
  • The endpoint performs no explicit export permission check. CRM1 controls visibility of its export buttons.

Side effects

  • Writes /tmp/<tenant ID>/tmp/customer_stock_totals.csv, creating the tenant directory with mode 0777 when needed, and streams that file.
  • The fixed per-tenant filename is overwritten and left on disk, so concurrent exports in one tenant can interfere.

See it in Fieldmotion

CRM1

Open a customer’s Stock Used tab and select Export Totals. CRM1 submits a form to Customer_stockExportTotals. The export controls are shown only when permission bit 256 is not set; the handler itself does not repeat that permission check.

CRM2

A source search found no CRM2 caller for this CSV endpoint. CRM2 exports row data through Customer_stockGetExportData after selecting IDs with its newer stock table endpoint.

Related endpoints