{
  "key": "dental-appointment-deposits-v1",
  "name": "Dental payment processing: collect appointment deposits",
  "chartOfAccounts": {
    "defaultCurrency": {
      "code": "USD"
    },
    "defaultCurrencyMode": "single",
    "defaultConsistencyConfig": {
      "totalBalanceUpdates": "strong"
    },
    "accounts": [
      {
        "key": "assets",
        "type": "asset",
        "children": [
          {
            "key": "payments",
            "payment": {
              "enabled": true
            }
          }
        ]
      },
      {
        "key": "liabilities",
        "type": "liability",
        "children": [
          {
            "key": "deposits",
            "template": true,
            "children": [
              {
                "key": "available"
              }
            ]
          }
        ]
      },
      {
        "key": "income",
        "type": "income",
        "children": [
          {
            "key": "dental-services",
            "template": true
          }
        ]
      },
      {
        "key": "expenses",
        "type": "expense",
        "children": [
          {
            "key": "payment-fees"
          }
        ]
      }
    ]
  },
  "payments": {
    "types": [
      {
        "type": "dental_deposit",
        "typeVersion": 1,
        "status": "active",
        "payment": {
          "amount": "{{amount}}",
          "direction": "payin"
        },
        "accounting": {
          "settled": {
            "description": "Card receipt for dental appointment {{appointment_id}}",
            "lines": [
              {
                "key": "settlement",
                "account": {
                  "path": "assets/payments"
                },
                "amount": "{{settled_amount}}",
                "system": "payment_settlement_line"
              },
              {
                "key": "fee",
                "account": {
                  "path": "assets/payments"
                },
                "amount": "-{{fragment_fee_amount}}",
                "system": "payment_fee_line"
              },
              {
                "key": "receipt",
                "account": {
                  "path": "liabilities/deposits:{{appointment_id}}/available"
                },
                "amount": "{{settled_amount}}"
              },
              {
                "key": "fee-expense",
                "account": {
                  "path": "expenses/payment-fees"
                },
                "amount": "{{fragment_fee_amount}}"
              }
            ]
          }
        }
      }
    ]
  },
  "ledgerEntries": {
    "types": [
      {
        "type": "dental_deposit_apply",
        "description": "Apply funding to completed dental treatment {{appointment_id}}",
        "lines": [
          {
            "key": "source",
            "account": {
              "path": "liabilities/deposits:{{appointment_id}}/available"
            },
            "amount": "-{{amount}}"
          },
          {
            "key": "destination",
            "account": {
              "path": "income/dental-services:{{appointment_id}}"
            },
            "amount": "{{amount}}"
          }
        ],
        "conditions": [
          {
            "account": {
              "path": "liabilities/deposits:{{appointment_id}}/available"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          }
        ]
      }
    ]
  }
}
