From 1613355c04667c227fbd8c3f411682d58974d959 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 16 Apr 2015 12:16:45 +0200 Subject: [PATCH] [UPDATE] Store coin auto payout in coin_addresses table per coin --- include/classes/coin_address.class.php | 34 ++++++++++++++++++---- include/classes/user.class.php | 15 +++++----- include/version.inc.php | 2 +- sql/000_base_structure.sql | 2 +- upgrade/definitions/1.0.0_to_1.0.1.inc.php | 32 ++++++++++++++++++++ 5 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 upgrade/definitions/1.0.0_to_1.0.1.inc.php diff --git a/include/classes/coin_address.class.php b/include/classes/coin_address.class.php index 1e9bbb08..3396a54a 100644 --- a/include/classes/coin_address.class.php +++ b/include/classes/coin_address.class.php @@ -27,6 +27,29 @@ class CoinAddress extends Base { return $this->sqlError(); } + /** + * Fetch users Auto Payout Threshold for a currency + * @param UserID int UserID + * @return mixed Float value for threshold, false on error + **/ + public function getAPThreshold($userID, $currency=NULL) { + if ($currency === NULL) $currency = $this->config['currency']; + $this->debug->append("STA " . __METHOD__, 4); + $stmt = $this->mysqli->prepare(" + SELECT ap_threshold + FROM " . $this->getTableName() . " + WHERE account_id = ? AND currency = ? + "); + if ( $this->checkStmt($stmt) && $stmt->bind_param('is', $userID, $currency) && $stmt->execute() && $result = $stmt->get_result()) { + if ($result->num_rows == 1) { + return $result->fetch_object()->ap_threshold; + } + } + $this->debug->append("Unable to fetch users auto payout threshold for " . $currency); + return $this->sqlError(); + } + + /** * Check if a coin address is already set * @param address string Coin Address to check for @@ -76,23 +99,24 @@ class CoinAddress extends Base { * Update a coin address record for a user and a currency * @param userID int Account ID * @param address string Coin Address + * @param ap_threshold float Threshold for auto payouts for this currency * @param currency string Currency short handle, defaults to config option * @return bool true or false **/ - public function update($userID, $address, $currency=NULL) { + public function update($userID, $address, $ap_threshold, $currency=NULL) { if ($currency === NULL) $currency = $this->config['currency']; if ($address != $this->getCoinAddress($userID) && $this->existsCoinAddress($address)) { $this->setErrorMessage('Unable to update coin address, address already exists'); return false; } if ($this->getCoinAddress($userID) != NULL) { - $stmt = $this->mysqli->prepare("UPDATE " . $this->getTableName() . " SET coin_address = ? WHERE account_id = ? AND currency = ?"); - if ( $this->checkStmt($stmt) && $stmt->bind_param('sis', $address, $userID, $currency) && $stmt->execute()) { + $stmt = $this->mysqli->prepare("UPDATE " . $this->getTableName() . " SET coin_address = ?, ap_threshold = ? WHERE account_id = ? AND currency = ?"); + if ( $this->checkStmt($stmt) && $stmt->bind_param('sdis', $address, $ap_threshold, $userID, $currency) && $stmt->execute()) { return true; } } else { - $stmt = $this->mysqli->prepare("INSERT INTO " . $this->getTableName() . " (coin_address, account_id, currency) VALUES (?, ?, ?)"); - if ( $this->checkStmt($stmt) && $stmt->bind_param('sis', $address, $userID, $currency) && $stmt->execute()) { + $stmt = $this->mysqli->prepare("INSERT INTO " . $this->getTableName() . " (coin_address, ap_threshold, account_id, currency) VALUES (?, ?, ?, ?)"); + if ( $this->checkStmt($stmt) && $stmt->bind_param('sdis', $address, $ap_threshold, $userID, $currency) && $stmt->execute()) { return true; } } diff --git a/include/classes/user.class.php b/include/classes/user.class.php index 91172e90..69dc0794 100644 --- a/include/classes/user.class.php +++ b/include/classes/user.class.php @@ -340,11 +340,11 @@ class User extends Base { $this->debug->append("STA " . __METHOD__, 4); $stmt = $this->mysqli->prepare(" SELECT - a.id, a.username, ca.coin_address AS coin_address, a.ap_threshold + a.id, a.username, ca.coin_address AS coin_address, ca.ap_threshold FROM " . $this->getTableName() . " AS a LEFT JOIN " . $this->coin_address->getTableName() . " AS ca ON a.id = ca.account_id - WHERE ap_threshold > 0 AND ca.currency = ? + WHERE ca.ap_threshold > 0 AND ca.currency = ? AND ca.coin_address IS NOT NULL "); if ( $this->checkStmt($stmt) && $stmt->bind_param('s', $this->config['currency']) && $stmt->execute() && $result = $stmt->get_result()) { @@ -544,12 +544,12 @@ class User extends Base { if ($email == 'hidden' || $email == NULL) $email = $this->getUserEmailById($userID); // We passed all validation checks so update the account - $stmt = $this->mysqli->prepare("UPDATE $this->table SET ap_threshold = ?, donate_percent = ?, email = ?, timezone = ?, is_anonymous = ? WHERE id = ?"); - if ($this->checkStmt($stmt) && $stmt->bind_param('ddssii', $threshold, $donate, $email, $timezone, $is_anonymous, $userID) && $stmt->execute()) { + $stmt = $this->mysqli->prepare("UPDATE $this->table SET donate_percent = ?, email = ?, timezone = ?, is_anonymous = ? WHERE id = ?"); + if ($this->checkStmt($stmt) && $stmt->bind_param('dssii', $donate, $email, $timezone, $is_anonymous, $userID) && $stmt->execute()) { $this->log->log("info", $this->getUserName($userID)." updated their account details"); - // Update coin address too + // Update coin address and ap_threshold if coin_address is set if ($address) { - if ($this->coin_address->update($userID, $address)) { + if ($this->coin_address->update($userID, $address, $threshold)) { return true; } } else { @@ -698,12 +698,13 @@ class User extends Base { $stmt = $this->mysqli->prepare(" SELECT id AS id, username, pin, api_key, is_admin, is_anonymous, email, timezone, no_fees, - IFNULL(donate_percent, '0') as donate_percent, ap_threshold + IFNULL(donate_percent, '0') as donate_percent FROM " . $this->getTableName() . " WHERE id = ? LIMIT 0,1"); if ($this->checkStmt($stmt) && $stmt->bind_param('i', $userID) && $stmt->execute() && $result = $stmt->get_result()) { $aData = $result->fetch_assoc(); $aData['coin_address'] = $this->coin_address->getCoinAddress($userID); + $aData['ap_threshold'] = $this->coin_address->getAPThreshold($userID); $stmt->close(); return $aData; } diff --git a/include/version.inc.php b/include/version.inc.php index dba0ed55..c91dc7fc 100644 --- a/include/version.inc.php +++ b/include/version.inc.php @@ -2,7 +2,7 @@ $defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; define('MPOS_VERSION', '1.0.2'); -define('DB_VERSION', '1.0.0'); +define('DB_VERSION', '1.0.1'); define('CONFIG_VERSION', '1.0.0'); define('HASH_VERSION', 1); diff --git a/sql/000_base_structure.sql b/sql/000_base_structure.sql index 826e6f2b..1996379b 100644 --- a/sql/000_base_structure.sql +++ b/sql/000_base_structure.sql @@ -27,7 +27,6 @@ CREATE TABLE IF NOT EXISTS `accounts` ( `api_key` varchar(255) DEFAULT NULL, `token` varchar(65) DEFAULT NULL, `donate_percent` float DEFAULT '0', - `ap_threshold` float DEFAULT '0', PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`), UNIQUE KEY `email` (`email`) @@ -56,6 +55,7 @@ CREATE TABLE IF NOT EXISTS `coin_addresses` ( `account_id` int(11) NOT NULL, `currency` varchar(5) NOT NULL, `coin_address` varchar(255) NOT NULL, + `ap_threshold` float DEFAULT '0', PRIMARY KEY (`id`), UNIQUE KEY `coin_address` (`coin_address`), KEY `account_id` (`account_id`) diff --git a/upgrade/definitions/1.0.0_to_1.0.1.inc.php b/upgrade/definitions/1.0.0_to_1.0.1.inc.php new file mode 100644 index 00000000..f616e890 --- /dev/null +++ b/upgrade/definitions/1.0.0_to_1.0.1.inc.php @@ -0,0 +1,32 @@ +getValue('DB_VERSION'); // Our actual version installed + + // Upgrade specific variables + $aSql[] = "ALTER TABLE `" . $coin_address->getTableName() . "` ADD ap_threshold float DEFAULT '0'"; + $aSql[] = "UPDATE " . $coin_address->getTableName() . " AS ca LEFT JOIN " . $user->getTableName() . " AS a ON a.id = ca.account_id SET ca.coin_address = a.coin_address"; + $aSql[] = "ALTER TABLE `" . $user->getTableName() . "` DROP `ap_threshold`"; + $aSql[] = "UPDATE " . $setting->getTableName() . " SET value = '1.0.1' WHERE name = 'DB_VERSION'"; + + if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) { + // Run the upgrade + echo '- Starting database migration to version ' . $db_version_new . PHP_EOL; + foreach ($aSql as $sql) { + echo '- Preparing: ' . $sql . PHP_EOL; + $stmt = $mysqli->prepare($sql); + if ($stmt && $stmt->execute()) { + echo '- success' . PHP_EOL; + } else { + echo '- failed: ' . $mysqli->error . PHP_EOL; + exit(1); + } + } + } +} +?>