diff --git a/doc/API/API-Docs.md b/doc/API/API-Docs.md
index 2c3c62847..3a7db5085 100644
--- a/doc/API/API-Docs.md
+++ b/doc/API/API-Docs.md
@@ -34,6 +34,9 @@
- [`edit_rule`](#api-route-19)
- [`inventory`](#api-inventory)
- [`get_inventory`](#api-route-20)
+ - [`bills`](#api-bills)
+ - [`list_bills`](#api-route-22)
+ - [`get_bill`](#api-route-23)
Describes the API structure.
# `Structure` [`top`](#top)
@@ -812,3 +815,118 @@ Output:
]
}
```
+
+## `Bills` [`top`](#top)
+
+### Function: `list_bills` [`top`](#top)
+
+Retrieve the list of bills currently in the system.
+
+Route: /api/v0/bills
+
+Input:
+
+Example:
+```curl
+curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/bills
+```
+
+Output:
+```text
+{
+ "status": "ok",
+ "err-msg": "",
+ "count": 1,
+ "bills": [
+ {
+ "bill_id": "1",
+ "bill_name": "Router bills",
+ "bill_type": "cdr",
+ "bill_cdr": "10000000",
+ "bill_day": "1",
+ "bill_quota": "0",
+ "rate_95th_in": "0",
+ "rate_95th_out": "0",
+ "rate_95th": "0",
+ "dir_95th": "in",
+ "total_data": "0",
+ "total_data_in": "0",
+ "total_data_out": "0",
+ "rate_average_in": "0",
+ "rate_average_out": "0",
+ "rate_average": "0",
+ "bill_last_calc": "2015-07-02 17:01:26",
+ "bill_custid": "Router",
+ "bill_ref": "Router",
+ "bill_notes": "Bill me",
+ "bill_autoadded": "0",
+ "ports_total": "0",
+ "allowed": "10Mbps",
+ "used": "0bps",
+ "percent": 0,
+ "overuse": "-"
+ }
+ ]
+}
+```
+
+### Function: `get_bill` [`top`](#top)
+
+Retrieve a specific bill
+
+Route: /api/v0/bills/:id
+ /api/v0/bills?ref=:ref
+ /api/v0/bills?custid=:custid
+
+ - id is the specific bill id
+ - ref is the billing reference
+ - custid is the customer reference
+
+Input:
+
+Example:
+```curl
+curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/bills/1
+curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/bills?ref=:customerref
+curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/bills?custid=:custid
+```
+
+Output:
+```text
+{
+ "status": "ok",
+ "err-msg": "",
+ "count": 1,
+ "bills": [
+ {
+ "bill_id": "1",
+ "bill_name": "Router bills",
+ "bill_type": "cdr",
+ "bill_cdr": "10000000",
+ "bill_day": "1",
+ "bill_quota": "0",
+ "rate_95th_in": "0",
+ "rate_95th_out": "0",
+ "rate_95th": "0",
+ "dir_95th": "in",
+ "total_data": "0",
+ "total_data_in": "0",
+ "total_data_out": "0",
+ "rate_average_in": "0",
+ "rate_average_out": "0",
+ "rate_average": "0",
+ "bill_last_calc": "2015-07-02 17:01:26",
+ "bill_custid": "Router",
+ "bill_ref": "Router",
+ "bill_notes": "Bill me",
+ "bill_autoadded": "0",
+ "ports_total": "0",
+ "allowed": "10Mbps",
+ "used": "0bps",
+ "percent": 0,
+ "overuse": "-"
+ }
+ ]
+}
+```
+
diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php
index 4f20c9b8b..8208a0c41 100644
--- a/html/includes/api_functions.inc.php
+++ b/html/includes/api_functions.inc.php
@@ -550,49 +550,6 @@ function get_port_graphs() {
}
-
-function list_bills() {
- global $config;
- $app = \Slim\Slim::getInstance();
- $router = $app->router()->getCurrentRoute()->getParams();
- $bill_id = $router['bill_id'];
- if (isset($_GET['custid'])) {
- $sql = '`bill_custid` = ?';
- $param = array($_GET['custid']);
- }
- else if (isset($_GET['ref'])) {
- $sql = '`bill_ref` = ?';
- $param = array($_GET['ref']);
- }
- else if (is_numeric($bill_id)) {
- $sql = '`bill_id` = ?';
- $param = array($bill_id);
- }
-
- else {
- $sql = '';
- $param = array();
- }
-
- if (count($param) >= 1) {
- $sql = "WHERE $sql";
- }
-
- $bills = dbFetchRows("SELECT * FROM `bills` $sql", $param);
- $total_bills = count($bills);
- $output = array(
- 'status' => 'ok',
- 'err-msg' => '',
- 'count' => $total_bills,
- 'bills' => $bills,
- );
- $app->response->setStatus('200');
- $app->response->headers->set('Content-Type', 'application/json');
- echo _json_encode($output);
-
-}
-
-
function list_alert_rules() {
global $config;
$app = \Slim\Slim::getInstance();
@@ -886,3 +843,76 @@ function list_oxidized() {
echo _json_encode($devices);
}
+
+function list_bills() {
+ global $config;
+ $app = \Slim\Slim::getInstance();
+ $status = 'ok';
+ $err_msg = '';
+ $message = '';
+ $code = 200;
+ $bills = array();
+ $bill_id = mres($router['bill_id']);
+ $bill_ref = mres($_GET['ref']);
+ $bill_custid = mres($_GET['custid']);
+ if (!empty($bill_custid)) {
+ $sql = '`bill_custid` = ?';
+ $param = array($bill_custid);
+ }
+ elseif (!empty($bill_ref)) {
+ $sql = '`bill_ref` = ?';
+ $param = array($bill_ref);
+ }
+ elseif (is_numeric($bill_id)) {
+ $sql = '`bill_id` = ?';
+ $param = array($bill_id);
+ }
+ else {
+ $sql = '';
+ $param = array();
+ }
+
+ if (count($param) >= 1) {
+ $sql = "WHERE $sql";
+ }
+
+ foreach (dbFetchRows("SELECT `bills`.*,COUNT(port_id) AS `ports_total` FROM `bills` LEFT JOIN `bill_ports` ON `bill_ports`.`bill_id`=`bills`.`bill_id` $sql GROUP BY `bill_name`,`bill_ref` ORDER BY `bill_name`",$param) as $bill) {
+ $day_data = getDates($bill['bill_day']);
+ $ports_total = $bill['ports_total'];
+ $datefrom = $day_data['0'];
+ $dateto = $day_data['1'];
+ $rate_data = $bill;
+ $rate_95th = $rate_data['rate_95th'];
+ $dir_95th = $rate_data['dir_95th'];
+ $total_data = $rate_data['total_data'];
+ $rate_average = $rate_data['rate_average'];
+
+ if ($bill['bill_type'] == "cdr") {
+ $type = "CDR";
+ $allowed = format_si($bill['bill_cdr'])."bps";
+ $used = format_si($rate_data['rate_95th'])."bps";
+ $percent = round(($rate_data['rate_95th'] / $bill['bill_cdr']) * 100,2);
+ $background = get_percentage_colours($percent);
+ $overuse = $rate_data['rate_95th'] - $bill['bill_cdr'];
+ $overuse = (($overuse <= 0) ? "-" : format_si($overuse));
+ }
+ elseif ($bill['bill_type'] == "quota") {
+ $type = "Quota";
+ $allowed = format_bytes_billing($bill['bill_quota']);
+ $used = format_bytes_billing($rate_data['total_data']);
+ $percent = round(($rate_data['total_data'] / ($bill['bill_quota'])) * 100,2);
+ $background = get_percentage_colours($percent);
+ $overuse = $rate_data['total_data'] - $bill['bill_quota'];
+ $overuse = (($overuse <= 0) ? "-" : format_bytes_billing($overus));
+ }
+ $bill['allowed'] = $allowed;
+ $bill['used'] = $used;
+ $bill['percent'] = $percent;
+ $bill['overuse'] = $overuse;
+ $bills[] = $bill;
+ }
+ $count = count($bills);
+ $output = array("status" => $status, "err-msg" => $err_msg, "count" => $count, "bills" => $bills);
+ $app->response->headers->set('Content-Type', 'application/json');
+ echo _json_encode($output);
+}