# BillingRouter

[Git Source](https://github.com/EntreDevelopers-Lab-Inc/Mezz-Companies/blob/f7a3e84e3dd5bb33c4bd7f77283983f9e8ba20b2/src/billing-router/BillingRouter.sol)

**Inherits:** Initializable, ContextUpgradeable, HubOwnableUUPSUpgradeable, ERC165Upgradeable, IBillingRouter

**Author:** Daniel Yamagata & Jerry Qi

A contract for sending and paying invoices

*This contract uses ContextUpgradeable to allow for future versions to use a gas relayer if desired*

## State Variables

### BillingRouterStorageLocation

```solidity
bytes32 private constant BillingRouterStorageLocation =
    0x37f3f3ea6bd3134a12f955ddfd4147e5790b22caaa1c2898646a2bd9d6b60b00;
```

## Functions

### \_getBillingRouterStorage

```solidity
function _getBillingRouterStorage() internal pure returns (BillingRouterStorage storage $);
```

### onlyVendor

*Reverts if the caller is not the vendor of 'invoiceId' or if the invoice does not exist*

```solidity
modifier onlyVendor(bytes32 invoiceId);
```

### constructor

```solidity
constructor(address _mezzHub) HubOwnableUUPSUpgradeable(_mezzHub);
```

### sendInvoice

Sends an invoice to a billable party. Returns the invoice ID

*Anyone can pay the invoice on behalf of the billable party*

```solidity
function sendInvoice(
    address billableParty,
    address denominationAsset,
    uint256 amount,
    uint256 dueDate,
    bytes32 categoryId,
    string memory description,
    string memory documentName,
    string memory documentUri
) external virtual pausable returns (bytes32);
```

**Parameters**

| Name                | Type      | Description                                                                                                                                                                          |
| ------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `billableParty`     | `address` | The address of the billable party                                                                                                                                                    |
| `denominationAsset` | `address` | The address of the denomination asset used for the invoice                                                                                                                           |
| `amount`            | `uint256` | The amount of the denomination asset to be paid                                                                                                                                      |
| `dueDate`           | `uint256` | The due date of the invoice. Must be in the future                                                                                                                                   |
| `categoryId`        | `bytes32` | The category ID of the invoice. Should be the keccak256 hash of the category name. For example, the category ID of "Transportation Services" is keccak256("Transportation Services") |
| `description`       | `string`  | The description of the invoice                                                                                                                                                       |
| `documentName`      | `string`  | The name of the document associated with the invoice                                                                                                                                 |
| `documentUri`       | `string`  | The URI of the document associated with the invoice                                                                                                                                  |

**Returns**

| Name     | Type      | Description           |
| -------- | --------- | --------------------- |
| `<none>` | `bytes32` | The ID of the invoice |

### payInvoice

Pays the invoice with 'invoiceId'

*The amount paid to the vendor may be different from the amount specified in the invoice. This is due to some ERC20s having fees built into the transfer function of ERC20s.*

```solidity
function payInvoice(bytes32 invoiceId) external virtual freezable returns (uint256);
```

**Parameters**

| Name        | Type      | Description                      |
| ----------- | --------- | -------------------------------- |
| `invoiceId` | `bytes32` | The ID of the invoice to be paid |

**Returns**

| Name     | Type      | Description                                             |
| -------- | --------- | ------------------------------------------------------- |
| `<none>` | `uint256` | The amount of the denomination asset paid to the vendor |

### cancelInvoice

Cancels the invoice with 'invoiceId'

*Can only be called by the invoice's vendor*

```solidity
function cancelInvoice(bytes32 invoiceId) external virtual onlyVendor(invoiceId) pausable;
```

**Parameters**

| Name        | Type      | Description                           |
| ----------- | --------- | ------------------------------------- |
| `invoiceId` | `bytes32` | The ID of the invoice to be cancelled |

### updateInvoiceDocument

Updates the document related to the invoice with 'invoiceId' in the Document Registry

*Can only be called by the invoice's vendor*

```solidity
function updateInvoiceDocument(bytes32 invoiceId, string memory updatedDocumentName, string memory updatedDocumentUri)
    external
    virtual
    onlyVendor(invoiceId)
    returns (uint256);
```

**Parameters**

| Name                  | Type      | Description                                           |
| --------------------- | --------- | ----------------------------------------------------- |
| `invoiceId`           | `bytes32` | The ID of the invoice whose document is to be updated |
| `updatedDocumentName` | `string`  | The updated name of the document                      |
| `updatedDocumentUri`  | `string`  | The updated URI of the document                       |

**Returns**

| Name     | Type      | Description                     |
| -------- | --------- | ------------------------------- |
| `<none>` | `uint256` | The new version of the document |

### hashInvoice

Returns the ID of an invoice given the invoice's parameters

*The description hash can be queried via event emission*

```solidity
function hashInvoice(
    address vendor,
    address billableParty,
    address denominationAsset,
    uint256 amount,
    uint256 dueDate,
    bytes32 categoryId,
    bytes32 descriptionHash
) public pure returns (bytes32);
```

### isInvoicePaid

Returns true if the invoice with 'invoiceId' has been paid, false otherwise

```solidity
function isInvoicePaid(bytes32 invoiceId) external view returns (bool);
```

### isInvoicePastDue

Returns true if the invoice with 'invoiceId' is past due, false otherwise

```solidity
function isInvoicePastDue(bytes32 invoiceId) external view returns (bool);
```

### getInvoiceById

Returns the invoice with 'invoiceId' as a DataTypes.Invoice struct

```solidity
function getInvoiceById(bytes32 invoiceId) external view returns (DataTypes.Invoice memory);
```

### \_validateDenominationAsset

*Reverts if 'denominationAsset' is invalid*

```solidity
function _validateDenominationAsset(address denominationAsset) internal view virtual;
```

### \_validateCallerIsVendor

```solidity
function _validateCallerIsVendor(bytes32 invoiceId) internal view virtual;
```

### \_validateInvoiceExists

```solidity
function _validateInvoiceExists(bytes32 invoiceId) internal view virtual;
```

### supportsInterface

*ERC165 support*

```solidity
function supportsInterface(bytes4 interfaceId)
    public
    view
    virtual
    override(ERC165Upgradeable, IERC165)
    returns (bool);
```

## Structs

### BillingRouterStorage

```solidity
struct BillingRouterStorage {
    mapping(bytes32 => DataTypes.Invoice) _invoiceById;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mezzanine.xyz/smart-contracts/source-code/billingrouter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
