# Customers

A customer (a "party") is who you invoice. Push them before you invoice them, under your own id.

## Create or update

`POST /v1/parties` takes an array. Each party is keyed on your `external_ref`: send the same one again and it is updated, never duplicated.

```json
[{
  "external_ref": "CUST-1001",
  "name": "Northwind Trading (Pty) Ltd",
  "party_type": "ORGANISATION",
  "vat_number": "4123456789",
  "company_reg": "2015/123456/07",
  "email": "accounts@northwind.example",
  "phone": "+27 11 555 0100",
  "address": {"street_no": "12", "street": "Main Road", "suburb": "Marshalltown", "city": "Johannesburg",
              "postal_code": "2001", "country_code": "ZA"}
}]
```

The address is sent whole or not at all: it replaces the stored one.

**Null leaves it alone, an empty string clears it.** For every optional field, `null` (or leaving it out) keeps what is stored, and `""` clears it. So a sync that does not know a VAT number cannot wipe one somebody typed into the office.

A changed name is kept as a rename: old documents keep the name they were issued under, and searches still find the customer by the old name.

## Branches

A chain customer with branches: push the head office, then each branch with `parent_external_ref` set to the head office's id. Invoices go to the branch; the statement can be sent per branch or for the whole group.

## A company exists once

Two customers with the same VAT number are the same company. Before creating one, search for it.

## Finding a customer

`GET /v1/parties/search?q=...` searches names, former names, VAT and company numbers, your ids, our account codes, branch numbers, e-mail addresses and address text. Each hit says what it matched on (`matched_via`), so you can show why it appeared.

## Customers missing details

`GET /v1/parties/incomplete` lists customers whose VAT number or address is missing, the ones you invoice most first. A tax invoice above the legal threshold must carry the customer's VAT number and address, so this is the list worth asking about.

## Credit on the account

`GET /v1/customers/{externalRef}/credit` gives what the customer can spend on the next order, how much of it they asked to have refunded, and the documents behind the number. See [Payments and allocations](06-payments-and-allocations.md).
