# Transaction

> Base URL: `https://pos.pages.fm/api/v1`. Authenticate with the `api_key` query parameter.

## List of transaction

`GET /shops/{SHOP_ID}/transactions`

Get list of transactions

### Parameters

| Name | In | Type | Required | Description |
|---|---|---|---|---|
| `SHOP_ID` | path | integer | yes | Shop ID |
| `page` | query | integer |  | Page number |
| `page_size` | query | integer |  | Page size |
| `status` | query | integer |  | Status Values: `-1` All status, `0` New, `1` Effective, `2` Canceled, `3` Deleted. |
| `startDateTime` | query | integer |  | Start time |
| `endDateTime` | query | integer |  | End time |

### Responses

- `200` Success (object)

```json
{
  "success": true,
  "data": [
    {
      "id": "5b6bbae4-56b4-4189-a84d-d2ecfecead3e",
      "display_id": "KH6815",
      "name": "Giao dịch công nợ #1692 trả nhà cung cấp",
      "note": "trả nhà cung cấp",
      "amount": -70000,
      "status": 1,
      "is_locked": false,
      "is_canceled": false,
      "is_accounting": false,
      "payment_debts_to_suppliers": false,
      "payments": [
        "cash"
      ],
      "tag_ids": [
        "193a-e877-359a-b340-01d8-4d2e-2050",
        "55f6-111c-2fd5-38e8-5b9d-009f-d3b1"
      ],
      "supplier_products_id": 293,
      "advertising_cost_id": null,
      "shop_id": 20,
      "creator_id": "d6e6a545-f102-439e-9943-8ac8369f9735",
      "creator": {
        "id": "d6e6a545-f102-439e-9943-8ac8369f9735",
        "name": "vudzung12",
        "fb_id": "1286585024855632",
        "avatar_url": null
      },
      "images": [
        "https://content.pancake.vn/user-content-dev2.pancake.vn/2022/7/7/eb376f0bf90f47d25396d49e4d547700f182981b.jpg"
      ],
      "inserted_at": "2023-09-23T05:00:20Z",
      "time_incurred": "2023-09-23T05:00:20Z",
      "table_name": "debts",
      "orderId": null
    }
  ]
}
```

## Create transaction

`POST /shops/{SHOP_ID}/transactions`

### Parameters

| Name | In | Type | Required | Description |
|---|---|---|---|---|
| `SHOP_ID` | path | integer | yes | Shop ID |

### Request body

Request body containing transaction information to create

- `transaction` (object, required): 
  - `name` (string, required): Transaction name
  - `note` (string): Note
  - `amount` (integer): Amount of money
  - `is_locked` (boolean): Locked
  - `tag_ids` (string[]): Transaction tags
  - `payments` (string[]): Payment method
  - `supplier_products_id` (integer): Supplier products ID
  - `is_accounting` (boolean): Financial accounting
  - `time_incurred` (string (date-time)): Time incurred
  - `payment_debts_to_suppliers` (boolean): Paying off debts to suppliers
  - `images` (string (uri)[]): Image links
  - `orderId` (string): Order ID

```json
{
  "transaction": {
    "name": "Thanh toán tiền hàng",
    "note": "thanh toán tiền hàng",
    "amount": -1000000,
    "is_locked": false,
    "tag_ids": [
      "193a-e877-359a-b340-01d8-4d2e-2050"
    ],
    "payments": [
      "transfer_money"
    ],
    "supplier_products_id": 293,
    "is_accounting": true,
    "time_incurred": "2023-09-23T08:41:54.404Z",
    "payment_debts_to_suppliers": true,
    "images": [
      "https://content.pancake.vn/user-content-dev2.pancake.vn/2022/7/7/eb376f0bf90f47d25396d49e4d547700f182981b.jpg"
    ],
    "orderId": "19785"
  }
}
```

### Responses

- `200` Success (object)

```json
{
  "success": true
}
```

## Create advertising cost

`POST /shops/{SHOP_ID}/adv_costs`

### Parameters

| Name | In | Type | Required | Description |
|---|---|---|---|---|
| `SHOP_ID` | path | integer | yes | Shop ID |

### Request body

Request body containing advertising cost information to create

- `params` (object, required): 
  - `cost_date` (string (date-time), required): Cost date
  - `transaction` (object, required): 
  - `items` (object[], required): Advertising items

```json
{
  "params": {
    "cost_date": "2023-09-07 22:00:00",
    "transaction": {
      "name": "Chi phí quảng cáo",
      "is_locked": false,
      "note": "example",
      "status": "0",
      "time_incurred": "2023-09-07 15:00:00",
      "tags": [
        "Thẻ 1",
        "Thẻ 2"
      ]
    },
    "items": [
      {
        "cost": 10000,
        "tax": 5,
        "warehouse_id": "IS",
        "is_livestream": true,
        "product": "1",
        "pke_mkter": "test@gmail.com",
        "source": {
          "key": "-3",
          "account": "3"
        },
        "is_wholesale": true,
        "post_ids": [
          "127967746288487_545680607828820"
        ]
      }
    ]
  }
}
```

### Responses

- `200` Success (object)

```json
{
  "success": true
}
```

## Create or update daily advertising cost by ad account

`POST /shops/{SHOP_ID}/adv_costs/upsert`

One advertising cost per (ad_account_id, day of cost_date in shop time). Creates it when missing, otherwise updates it in place: items are matched by (advertising_id, product); matched items only change the fields sent, new items are added, items not sent are kept (send cost 0 to clear one). Total and the linked expense amount are recalculated from all saved items as Σ cost × (1 + tax/100). When updating, the transaction object is ignored (name, note, tags, status, time incurred stay unchanged). Not written when the cost is deleted or its expense transaction is canceled, deleted, locked or transferred to supplier debt. Requires the permission to update income/expense transactions. ad_account_id must not start with act_ (reserved for costs created by Meta Ads Manager).

### Parameters

| Name | In | Type | Required | Description |
|---|---|---|---|---|
| `SHOP_ID` | path | integer | yes | Shop ID |

### Request body

Advertising cost of one ad account for one day

- `params` (object, required): 
  - `ad_account_id` (string, required): Ad account ID on the partner side, part of the cost key. Must not start with act_
  - `cost_date` (string, required): Cost date in shop time (YYYY-MM-DD HH:MM:SS or YYYY-MM-DD). Only the day is used to find the cost
  - `transaction` (object): Expense transaction, only used when the cost is created. is_locked is ignored
  - `items` (object[], required): Advertising items. (advertising_id, product) must be unique in the request

```json
{
  "params": {
    "ad_account_id": "123-456-7890",
    "cost_date": "2026-05-10 00:00:00",
    "transaction": {
      "name": "Chi phí Google PMax 10/05",
      "status": "1"
    },
    "items": [
      {
        "advertising_id": "21345678901",
        "product": "SP001",
        "cost": 150000,
        "ads_source": "Google",
        "advertising_account": "Webcake Ads",
        "utm": {
          "utm_source": "google",
          "utm_medium": "cpc",
          "utm_campaign": "pmax_tet"
        }
      },
      {
        "advertising_id": "21345678901",
        "product": "SP002",
        "cost": 0
      }
    ]
  }
}
```

### Responses

- `200` Success (object)
- `409` Another request is writing the same advertising cost, retry later (ErrorResponse)
- `422` Invalid data, or the advertising cost of that day was canceled, deleted, locked or transferred to supplier debt (ErrorResponse)

```json
{
  "success": true
}
```

## Transaction history

`GET /shops/{SHOP_ID}/payment_accounts/get_payment_histories`

Get payment transaction history

### Parameters

| Name | In | Type | Required | Description |
|---|---|---|---|---|
| `SHOP_ID` | path | integer | yes | Shop ID |

### Responses

- `200` Success (object)

```json
{
  "success": true,
  "data": []
}
```
