{
  "key": "vertical-ai-construction-change-order-prepayments-v1",
  "name": "Construction AI \u2014 Change-order prepayments",
  "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": "cases",
                "template": true,
                "children": [
                  {
                    "key": "held"
                  }
                ]
              },
              {
                "key": "refunds",
                "template": true,
                "children": [
                  {
                    "key": "pending"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "key": "income",
        "type": "income",
        "children": [
          {
            "key": "cases",
            "template": true
          }
        ]
      }
    ]
  },
  "ledgerEntries": {
    "types": [
      {
        "type": "change_order_prepayments_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": "change_order_prepayments_reserve",
        "description": "Reserve customer prepayment for an authorized case",
        "lines": [
          {
            "key": "line_1",
            "account": {
              "path": "liabilities/sources:{{source_id}}/available"
            },
            "amount": "-{{amount}}"
          },
          {
            "key": "line_2",
            "account": {
              "path": "liabilities/sources:{{source_id}}/cases:{{case_id}}/held"
            },
            "amount": "{{amount}}"
          }
        ],
        "conditions": [
          {
            "account": {
              "path": "liabilities/sources:{{source_id}}/available"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          }
        ]
      },
      {
        "type": "change_order_prepayments_release",
        "description": "Release an unused case hold to available customer funds",
        "lines": [
          {
            "key": "line_1",
            "account": {
              "path": "liabilities/sources:{{source_id}}/cases:{{case_id}}/held"
            },
            "amount": "-{{amount}}"
          },
          {
            "key": "line_2",
            "account": {
              "path": "liabilities/sources:{{source_id}}/available"
            },
            "amount": "{{amount}}"
          }
        ],
        "conditions": [
          {
            "account": {
              "path": "liabilities/sources:{{source_id}}/cases:{{case_id}}/held"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          }
        ]
      },
      {
        "type": "change_order_prepayments_complete",
        "description": "Allocate an accepted delivered service amount to operational earned fees",
        "lines": [
          {
            "key": "line_1",
            "account": {
              "path": "liabilities/sources:{{source_id}}/cases:{{case_id}}/held"
            },
            "amount": "-{{amount}}"
          },
          {
            "key": "line_2",
            "account": {
              "path": "income/cases:{{case_id}}"
            },
            "amount": "{{amount}}"
          }
        ],
        "conditions": [
          {
            "account": {
              "path": "liabilities/sources:{{source_id}}/cases:{{case_id}}/held"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          }
        ]
      },
      {
        "type": "change_order_prepayments_credit_service",
        "description": "Credit an approved service amount back to the customer",
        "lines": [
          {
            "key": "line_1",
            "account": {
              "path": "income/cases:{{case_id}}"
            },
            "amount": "-{{amount}}"
          },
          {
            "key": "line_2",
            "account": {
              "path": "liabilities/sources:{{source_id}}/available"
            },
            "amount": "{{amount}}"
          }
        ],
        "conditions": [
          {
            "account": {
              "path": "income/cases:{{case_id}}"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          }
        ]
      },
      {
        "type": "change_order_prepayments_reserve_refund",
        "description": "Reserve a customer refund before calling the payment provider",
        "lines": [
          {
            "key": "line_1",
            "account": {
              "path": "liabilities/sources:{{source_id}}/available"
            },
            "amount": "-{{amount}}"
          },
          {
            "key": "line_2",
            "account": {
              "path": "liabilities/sources:{{source_id}}/refunds:{{refund_id}}/pending"
            },
            "amount": "{{amount}}"
          }
        ],
        "conditions": [
          {
            "account": {
              "path": "liabilities/sources:{{source_id}}/available"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          }
        ]
      },
      {
        "type": "change_order_prepayments_refund_settled",
        "description": "Record a refund only after the payment provider confirms it settled",
        "lines": [
          {
            "key": "line_1",
            "account": {
              "path": "liabilities/sources:{{source_id}}/refunds:{{refund_id}}/pending"
            },
            "amount": "-{{amount}}"
          },
          {
            "key": "line_2",
            "account": {
              "path": "assets/confirmed-cash"
            },
            "amount": "-{{amount}}"
          }
        ],
        "conditions": [
          {
            "account": {
              "path": "liabilities/sources:{{source_id}}/refunds:{{refund_id}}/pending"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          },
          {
            "account": {
              "path": "assets/confirmed-cash"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          }
        ]
      },
      {
        "type": "change_order_prepayments_refund_failed",
        "description": "Release the refund reservation only after definitive failure",
        "lines": [
          {
            "key": "line_1",
            "account": {
              "path": "liabilities/sources:{{source_id}}/refunds:{{refund_id}}/pending"
            },
            "amount": "-{{amount}}"
          },
          {
            "key": "line_2",
            "account": {
              "path": "liabilities/sources:{{source_id}}/available"
            },
            "amount": "{{amount}}"
          }
        ],
        "conditions": [
          {
            "account": {
              "path": "liabilities/sources:{{source_id}}/refunds:{{refund_id}}/pending"
            },
            "postcondition": {
              "totalBalance": {
                "gte": "0"
              }
            }
          }
        ]
      }
    ]
  },
  "scenes": [
    {
      "name": "Change-order prepayments \u2014 successful path and confirmed partial failures",
      "events": [
        {
          "eventType": "entry",
          "entry": {
            "type": "change_order_prepayments_fund",
            "typeVersion": 1,
            "parameters": {
              "source_id": "1",
              "amount": "1000"
            }
          }
        },
        {
          "eventType": "entry",
          "entry": {
            "type": "change_order_prepayments_reserve",
            "typeVersion": 1,
            "parameters": {
              "source_id": "1",
              "case_id": "1",
              "amount": "700"
            }
          }
        },
        {
          "eventType": "entry",
          "entry": {
            "type": "change_order_prepayments_complete",
            "typeVersion": 1,
            "parameters": {
              "source_id": "1",
              "case_id": "1",
              "amount": "500"
            }
          }
        },
        {
          "eventType": "entry",
          "entry": {
            "type": "change_order_prepayments_credit_service",
            "typeVersion": 1,
            "parameters": {
              "source_id": "1",
              "case_id": "1",
              "amount": "100"
            }
          }
        },
        {
          "eventType": "entry",
          "entry": {
            "type": "change_order_prepayments_release",
            "typeVersion": 1,
            "parameters": {
              "source_id": "1",
              "case_id": "1",
              "amount": "200"
            }
          }
        },
        {
          "eventType": "entry",
          "entry": {
            "type": "change_order_prepayments_reserve_refund",
            "typeVersion": 1,
            "parameters": {
              "source_id": "1",
              "refund_id": "1",
              "amount": "200"
            }
          }
        },
        {
          "eventType": "entry",
          "entry": {
            "type": "change_order_prepayments_refund_failed",
            "typeVersion": 1,
            "parameters": {
              "source_id": "1",
              "refund_id": "1",
              "amount": "50"
            }
          }
        },
        {
          "eventType": "entry",
          "entry": {
            "type": "change_order_prepayments_refund_settled",
            "typeVersion": 1,
            "parameters": {
              "source_id": "1",
              "refund_id": "1",
              "amount": "150"
            }
          }
        }
      ]
    }
  ]
}
