{
  "key": "vertical-ai-dental-dental-lab-invoice-payouts-v1",
  "name": "Dental AI \u2014 Dental lab invoice payouts",
  "chartOfAccounts": {
    "defaultCurrency": {
      "code": "USD"
    },
    "defaultCurrencyMode": "single",
    "defaultConsistencyConfig": {
      "totalBalanceUpdates": "strong"
    },
    "accounts": [
      {
        "key": "assets",
        "type": "asset",
        "children": [
          {
            "key": "confirmed-cash"
          }
        ]
      },
      {
        "key": "liabilities",
        "type": "liability",
        "children": [
          {
            "key": "sources",
            "template": true,
            "children": [
              {
                "key": "available"
              }
            ]
          },
          {
            "key": "payees",
            "template": true,
            "children": [
              {
                "key": "cases",
                "template": true,
                "children": [
                  {
                    "key": "approved"
                  },
                  {
                    "key": "payouts",
                    "template": true,
                    "children": [
                      {
                        "key": "pending"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "ledgerEntries": {
    "types": [
      {
        "type": "dental_lab_invoice_payouts_fund",
        "description": "Record a confirmed incoming payment; does not execute a payment",
        "lines": [
          {
            "key": "line_1",
            "account": {
              "path": "assets/confirmed-cash"
            },
            "amount": "{{amount}}"
          },
          {
            "key": "line_2",
            "account": {
              "path": "liabilities/sources:{{source_id}}/available"
            },
            "amount": "{{amount}}"
          }
        ]
      },
      {
        "type": "dental_lab_invoice_payouts_approve",
        "description": "Allocate prefunded money to an approved case payable",
        "lines": [
          {
            "key": "line_1",
            "account": {
              "path": "liabilities/sources:{{source_id}}/available"
            },
            "amount": "-{{amount}}"
          },
          {
            "key": "line_2",
            "account": {
              "path": "liabilities/payees:{{payee_id}}/cases:{{case_id}}/approved"
            },
            "amount": "{{amount}}"
          }
        ],
        "conditions": [
          {
            "account": {
              "path": "liabilities/sources:{{source_id}}/available"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          }
        ]
      },
      {
        "type": "dental_lab_invoice_payouts_cancel_approval",
        "description": "Return an unpaid approved amount to the funded pool",
        "lines": [
          {
            "key": "line_1",
            "account": {
              "path": "liabilities/payees:{{payee_id}}/cases:{{case_id}}/approved"
            },
            "amount": "-{{amount}}"
          },
          {
            "key": "line_2",
            "account": {
              "path": "liabilities/sources:{{source_id}}/available"
            },
            "amount": "{{amount}}"
          }
        ],
        "conditions": [
          {
            "account": {
              "path": "liabilities/payees:{{payee_id}}/cases:{{case_id}}/approved"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          }
        ]
      },
      {
        "type": "dental_lab_invoice_payouts_reserve_payout",
        "description": "Reserve an approved payable before calling the payout provider",
        "lines": [
          {
            "key": "line_1",
            "account": {
              "path": "liabilities/payees:{{payee_id}}/cases:{{case_id}}/approved"
            },
            "amount": "-{{amount}}"
          },
          {
            "key": "line_2",
            "account": {
              "path": "liabilities/payees:{{payee_id}}/cases:{{case_id}}/payouts:{{payout_id}}/pending"
            },
            "amount": "{{amount}}"
          }
        ],
        "conditions": [
          {
            "account": {
              "path": "liabilities/payees:{{payee_id}}/cases:{{case_id}}/approved"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          }
        ]
      },
      {
        "type": "dental_lab_invoice_payouts_payout_settled",
        "description": "Record confirmed external payout settlement",
        "lines": [
          {
            "key": "line_1",
            "account": {
              "path": "liabilities/payees:{{payee_id}}/cases:{{case_id}}/payouts:{{payout_id}}/pending"
            },
            "amount": "-{{amount}}"
          },
          {
            "key": "line_2",
            "account": {
              "path": "assets/confirmed-cash"
            },
            "amount": "-{{amount}}"
          }
        ],
        "conditions": [
          {
            "account": {
              "path": "liabilities/payees:{{payee_id}}/cases:{{case_id}}/payouts:{{payout_id}}/pending"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          },
          {
            "account": {
              "path": "assets/confirmed-cash"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          }
        ]
      },
      {
        "type": "dental_lab_invoice_payouts_payout_failed",
        "description": "Release reservation only after definitive external payout failure",
        "lines": [
          {
            "key": "line_1",
            "account": {
              "path": "liabilities/payees:{{payee_id}}/cases:{{case_id}}/payouts:{{payout_id}}/pending"
            },
            "amount": "-{{amount}}"
          },
          {
            "key": "line_2",
            "account": {
              "path": "liabilities/payees:{{payee_id}}/cases:{{case_id}}/approved"
            },
            "amount": "{{amount}}"
          }
        ],
        "conditions": [
          {
            "account": {
              "path": "liabilities/payees:{{payee_id}}/cases:{{case_id}}/payouts:{{payout_id}}/pending"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          }
        ]
      }
    ]
  },
  "scenes": [
    {
      "name": "Dental lab invoice payouts \u2014 successful path and confirmed partial failures",
      "events": [
        {
          "eventType": "entry",
          "entry": {
            "type": "dental_lab_invoice_payouts_fund",
            "typeVersion": 1,
            "parameters": {
              "source_id": "1",
              "amount": "1000"
            }
          }
        },
        {
          "eventType": "entry",
          "entry": {
            "type": "dental_lab_invoice_payouts_approve",
            "typeVersion": 1,
            "parameters": {
              "source_id": "1",
              "case_id": "1",
              "payee_id": "1",
              "amount": "700"
            }
          }
        },
        {
          "eventType": "entry",
          "entry": {
            "type": "dental_lab_invoice_payouts_cancel_approval",
            "typeVersion": 1,
            "parameters": {
              "source_id": "1",
              "case_id": "1",
              "payee_id": "1",
              "amount": "100"
            }
          }
        },
        {
          "eventType": "entry",
          "entry": {
            "type": "dental_lab_invoice_payouts_reserve_payout",
            "typeVersion": 1,
            "parameters": {
              "case_id": "1",
              "payee_id": "1",
              "payout_id": "1",
              "amount": "400"
            }
          }
        },
        {
          "eventType": "entry",
          "entry": {
            "type": "dental_lab_invoice_payouts_payout_failed",
            "typeVersion": 1,
            "parameters": {
              "case_id": "1",
              "payee_id": "1",
              "payout_id": "1",
              "amount": "100"
            }
          }
        },
        {
          "eventType": "entry",
          "entry": {
            "type": "dental_lab_invoice_payouts_payout_settled",
            "typeVersion": 1,
            "parameters": {
              "case_id": "1",
              "payee_id": "1",
              "payout_id": "1",
              "amount": "300"
            }
          }
        }
      ]
    }
  ]
}
