From b4b3e55195c98e8919f5b90a646c81585f0a18ea Mon Sep 17 00:00:00 2001
From: Robert Anderson <randerson@lbsd.net>
Date: Tue, 30 Jun 2009 09:35:27 +0000
Subject: [PATCH] Added WiSP User Topups

---
 webgui/ajax.php                               |  64 ++++++++
 .../include/ajax/functions/WiSPUserTopups.php | 145 ++++++++++++++++++
 webgui/index.html                             |   2 +-
 webgui/js/app/windows/WiSPUserTopups.js       |  51 +++---
 webgui/js/app/windows/WiSPUsers.js            |  28 ++++
 5 files changed, 264 insertions(+), 26 deletions(-)
 create mode 100644 webgui/include/ajax/functions/WiSPUserTopups.php

diff --git a/webgui/ajax.php b/webgui/ajax.php
index 7fb563be..0aed8d94 100644
--- a/webgui/ajax.php
+++ b/webgui/ajax.php
@@ -19,6 +19,7 @@
 	include_once("include/ajax/functions/WiSPLocations.php");
 	include_once("include/ajax/functions/WiSPLocationMembers.php");
 	include_once("include/ajax/functions/WiSPUserLogs.php");
+	include_once("include/ajax/functions/WiSPUserTopups.php");
 
 	include_once("include/radiuscodes.php");
 
@@ -163,6 +164,69 @@
 
 	switch ($function) {
 
+		# WiSPUserTopups.js functions
+		case "getWiSPUserTopups":
+
+			$res = getWiSPUserTopups($soapParams);
+			$rawData = $res[0]; $numResults = $res[1];
+
+			$res = new json_response;
+			$res->setID('ID');
+			$res->addField('ID','int');
+			$res->addField('Timestamp','date');
+			$res->addField('Type','int');
+			$res->addField('Value','int');
+			$res->addField('ValidFrom','string');
+			$res->addField('ValidTo','string');
+			$res->parseArray($rawData);
+			$res->setDatasetSize($numResults);
+
+			echo json_encode($res->export());
+
+			break;
+	
+		case "createWiSPUserTopup":
+
+			$res = createWiSPUserTopup($soapParams);
+			if (isset($res)) {
+				ajaxException($res);
+			}
+
+			break;
+
+		case "updateWiSPUserTopup":
+
+			$res = updateWiSPUserTopup($soapParams);
+			if (isset($res)) {
+				ajaxException($res);
+			}
+
+			break;
+
+		case "getWiSPUserTopup":
+			$rawData = getWiSPUserTopup($soapParams);
+
+			$res = new json_response;
+			$res->setID('ID');
+			$res->addField('ID','int');
+			$res->addField('Type','int');
+			$res->addField('Value','int');
+			$res->addField('ValidFrom','date');
+			$res->addField('ValidTo','date');
+			$res->parseHash($rawData);
+
+			echo json_encode($res->export());
+			break;
+
+		case "removeWiSPUserTopup":
+
+			$res = removeWiSPUserTopup($soapParams);
+			if (isset($res)) {
+				ajaxException($res);
+			}
+
+			break;
+
 		# AdminGroupMembers.js functions
 		case "getAdminGroupMembers":
 
diff --git a/webgui/include/ajax/functions/WiSPUserTopups.php b/webgui/include/ajax/functions/WiSPUserTopups.php
new file mode 100644
index 00000000..b006ba81
--- /dev/null
+++ b/webgui/include/ajax/functions/WiSPUserTopups.php
@@ -0,0 +1,145 @@
+<?php
+
+include_once("include/db.php");
+
+
+# Add new topup
+function createWiSPUserTopup($params) {
+	global $db;
+
+	$timestamp = date('Y-m-d H:i:s');
+	$res = DBDo("INSERT INTO topups (UserID,Timestamp,Type,Value,ValidFrom,ValidTo) VALUES (?,?,?,?,?,?)",
+			array($params[0]['UserID'],$timestamp,$params[0]['Type'],$params[0]['Value'],$params[0]['ValidFrom'],
+					$params[0]['ValidTo'])
+	);
+
+	if (!is_numeric($res)) {
+		return $res;
+	}
+
+	return NULL;
+}
+
+# Edit topup
+function updateWiSPUserTopup($params) {
+	global $db;
+
+	$res = DBDo("UPDATE topups SET Value = ?, Type = ?, ValidFrom = ?, ValidTo = ? WHERE ID = ?",
+				array($params[0]['Value'],
+				$params[0]['Type'],
+				$params[0]['ValidFrom'],
+				$params[0]['ValidTo'],
+				$params[0]['ID'])
+	);
+
+	if (!is_numeric($res)) {
+		return $res;
+	}
+
+	return NULL;
+}
+
+# Delete user topup
+function removeWiSPUserTopup($params) {
+	global $db;
+
+	$res = DBDo("DELETE FROM topups WHERE ID = ?",array($params[0]));
+	if (!is_numeric($res)) {
+		return $res;
+	}
+
+	return NULL;
+}
+ 
+# Return specific topup row
+function getWiSPUserTopup($params) {
+	global $db;
+
+	$res = DBSelect("SELECT ID, Type, Value, ValidFrom, ValidTo FROM topups WHERE ID = ?",array($params[0]));
+	if (!is_object($res)) {
+		return $res;
+	}
+
+	$resultArray = array();
+
+	$row = $res->fetchObject();
+
+	$resultArray['ID'] = $row->id;
+	$resultArray['Type'] = $row->type;
+	$resultArray['Value'] = $row->value;
+
+	# Convert to ISO format
+	$date = new DateTime($row->validfrom);
+	$value = $date->format("Y-m-d");
+	$resultArray['ValidFrom'] = $value;
+
+	# Convert to ISO format
+	$date = new DateTime($row->validto);
+	$value = $date->format("Y-m-d");
+	$resultArray['ValidTo'] = $value;
+
+	return $resultArray;
+}
+
+# Return list of topups
+function getWiSPUserTopups($params) {
+	global $db;
+
+	# Filters and sorts are the same here
+	$filtersorts = array(
+		'ID' => 'topups.ID',
+		'Type' => 'topups.Type',
+		'Value' => 'topups.Value',
+		'ValidFrom' => 'topups.ValidFrom',
+		'ValidTo' => 'topups.ValidTo'
+	);
+
+	$res = DBSelectSearch("
+			SELECT 
+				ID, Timestamp, Type, Value, ValidFrom, ValidTo 
+			FROM 
+				topups 
+			WHERE 
+				Depleted = 0
+			AND
+				UserID = ".DBQuote($params[0]['UserID'])."
+			ORDER BY
+				Timestamp
+			DESC
+		",$params[1],$filtersorts,$filtersorts);
+
+	$sth = $res[0]; $numResults = $res[1];
+	# If STH is blank, return the error back to whoever requested the data
+	if (!isset($sth)) {
+		return $res;
+	}
+
+	$resultArray = array();
+
+	# loop through rows
+	while ($row = $sth->fetchObject()) {
+		$item = array();
+
+		$item['ID'] = $row->id;
+		$item['Timestamp'] = $row->timestamp;
+		$item['Type'] = $row->type;
+		$item['Value'] = $row->value;
+
+		# Convert to ISO format
+		$date = new DateTime($row->validfrom);
+		$value = $date->format("Y-m-d");
+		$item['ValidFrom'] = $value;
+
+		# Convert to ISO format
+		$date = new DateTime($row->validto);
+		$value = $date->format("Y-m-d");
+		$item['ValidTo'] = $value;
+
+		# push this row onto array
+		array_push($resultArray,$item);
+	}
+
+	return array($resultArray,$numResults);
+}
+
+# vim: ts=4
diff --git a/webgui/index.html b/webgui/index.html
index ecd65971..02b05fe7 100644
--- a/webgui/index.html
+++ b/webgui/index.html
@@ -71,7 +71,7 @@
 	<script type="text/javascript">document.getElementById('loading-msg').innerHTML = 'Windows...';</script>
 	<script type="text/javascript" src="js/app/windows/WiSPUsers.js"></script>
 	<script type="text/javascript" src="js/app/windows/WiSPUserLogs.js"></script>
-
+	<script type="text/javascript" src="js/app/windows/WiSPUserTopups.js"></script>
 	<script type="text/javascript" src="js/app/windows/WiSPLocations.js"></script>
 	<script type="text/javascript" src="js/app/windows/WiSPLocationMembers.js"></script>
 
diff --git a/webgui/js/app/windows/WiSPUserTopups.js b/webgui/js/app/windows/WiSPUserTopups.js
index 1ae25579..d7151424 100644
--- a/webgui/js/app/windows/WiSPUserTopups.js
+++ b/webgui/js/app/windows/WiSPUserTopups.js
@@ -21,7 +21,7 @@ function showWiSPUserTopupsWindow(userID) {
 					tooltip:'Add topup',
 					iconCls:'add',
 					handler: function() {
-						showWiSPUserTopupEditWindow(userID,0);
+						showWiSPUserTopupAddEditWindow(userID,0);
 					}
 				}, 
 				'-', 
@@ -34,7 +34,7 @@ function showWiSPUserTopupsWindow(userID) {
 						// Check if we have selected item
 						if (selectedItem) {
 							// If so display window
-							showWiSPUserTopupEditWindow(userID,selectedItem.data.ID);
+							showWiSPUserTopupAddEditWindow(userID,selectedItem.data.ID);
 						} else {
 							wispUserTopupsWindow.getEl().mask();
 
@@ -91,9 +91,14 @@ function showWiSPUserTopupsWindow(userID) {
 					dataIndex: 'ID'
 				},
 				{
-					header: "Bandwidth",
+					header: "Type",
 					sortable: true,
-					dataIndex: 'Bandwidth'
+					dataIndex: 'Type'
+				},
+				{
+					header: "Value",
+					sortable: true,
+					dataIndex: 'Value'
 				},
 				{
 					header: "Timestamp",
@@ -110,12 +115,7 @@ function showWiSPUserTopupsWindow(userID) {
 					header: "ValidTo",
 					sortable: true,
 					dataIndex: 'ValidTo'
-				},
-				{
-					header: "AgentRef",
-					sortable: true,
-					dataIndex: 'AgentRef'
-				}
+				}			
 			]),
 		},
 		// Store config
@@ -134,11 +134,10 @@ function showWiSPUserTopupsWindow(userID) {
 		{
 			filters: [
 				{type: 'numeric',  dataIndex: 'ID'},
-				{type: 'numeric',  dataIndex: 'Bandwidth'},
 				{type: 'date',  dataIndex: 'Timestamp'},
+				{type: 'numeric',  dataIndex: 'Value'},
 				{type: 'date',  dataIndex: 'ValidFrom'},
-				{type: 'date',  dataIndex: 'ValidTo'},
-				{type: 'string',  dataIndex: 'AgentRef'}
+				{type: 'date',  dataIndex: 'ValidTo'}
 			]
 		}
 	);
@@ -148,7 +147,7 @@ function showWiSPUserTopupsWindow(userID) {
 
 
 // Display edit/add form
-function showWiSPUserTopupEditWindow(userID,topupID) {
+function showWiSPUserTopupAddEditWindow(userID,topupID) {
 	var today = new Date();
 	var firstOfMonth = today.getFirstDateOfMonth();
 	var firstOfNext = today.getLastDateOfMonth().add(Date.DAY, 1);
@@ -161,8 +160,8 @@ function showWiSPUserTopupEditWindow(userID,topupID) {
 			ID: topupID,
 			SOAPFunction: 'updateWiSPUserTopup',
 			SOAPParams: 
-				'0:ID,0:Bandwidth,'+
-				'0:Timestamp,0:ValidFrom,0:ValidTo,0:AgentRef'
+				'0:ID,0:Value,0:Type,'+
+				'0:ValidFrom,0:ValidTo'
 		};
 	// We doing an Add
 	} else {
@@ -170,8 +169,8 @@ function showWiSPUserTopupEditWindow(userID,topupID) {
 			UserID: userID,
 			SOAPFunction: 'createWiSPUserTopup',
 			SOAPParams: 
-				'0:UserID,0:Bandwidth,'+
-				'0:Timestamp,0:ValidFrom,0:ValidTo,0:AgentRef'
+				'0:UserID,0:Value,0:Type,'+
+				'0:ValidFrom,0:ValidTo'
 		};
 	}
 
@@ -198,8 +197,15 @@ function showWiSPUserTopupEditWindow(userID,topupID) {
 			items: [
 				{
 					xtype: 'numberfield',
-					fieldLabel: 'Bandwidth',
-					name: 'Bandwidth',
+					fieldLabel: 'Type',
+					name: 'Type',
+					minValue: 1,
+					allowBlank: false
+				},
+				{
+					xtype: 'numberfield',
+					fieldLabel: 'Value',
+					name: 'Value',
 					minValue: 1,
 					allowBlank: false
 				},
@@ -212,7 +218,6 @@ function showWiSPUserTopupEditWindow(userID,topupID) {
 					value: firstOfMonth,
 					format: 'Y-m-d',
 					endDateField: 'ValidTo'
-
 				},
 				{
 					xtype: 'datefield',
@@ -223,10 +228,6 @@ function showWiSPUserTopupEditWindow(userID,topupID) {
 					value: firstOfNext,
 					format: 'Y-m-d',
 					startDateField: 'ValidFrom'
-				},
-				{
-					fieldLabel: 'AgentRef',
-					name: 'AgentRef'
 				}
 			],
 		},
diff --git a/webgui/js/app/windows/WiSPUsers.js b/webgui/js/app/windows/WiSPUsers.js
index 4fb02adb..5f3365f3 100644
--- a/webgui/js/app/windows/WiSPUsers.js
+++ b/webgui/js/app/windows/WiSPUsers.js
@@ -95,6 +95,34 @@ function showWiSPUserWindow() {
 						} else {
 							WiSPUserWindow.getEl().mask();
 
+							// Display error
+							Ext.Msg.show({
+								title: "Nothing selected",
+								msg: "No user selected",
+								icon: Ext.MessageBox.ERROR,
+								buttons: Ext.Msg.CANCEL,
+								modal: false,
+								fn: function() {
+									WiSPUserWindow.getEl().unmask();
+								}
+							});
+						}
+					}
+				},
+				'-',
+				{
+					text:'Topups',
+					tooltip:'User topups',
+					iconCls:'logs',
+					handler: function() {
+						var selectedItem = WiSPUserWindow.getComponent('gridpanel').getSelectionModel().getSelected();
+						// Check if we have selected item
+						if (selectedItem) {
+							// If so display window
+							showWiSPUserTopupsWindow(selectedItem.data.ID);
+						} else {
+							WiSPUserWindow.getEl().mask();
+
 							// Display error
 							Ext.Msg.show({
 								title: "Nothing selected",
-- 
GitLab