* @version 1.0 * @since 20 Aug 2014 17:20:09 */ require_once 'models/Model.inc.php'; class Payment extends Model { // The database fields used by this model. public static $fields = array( 'account_id' => '', 'payment_date' => '', 'amount' => '', 'status' => '', 'type' => '', 'stripe_charge_id' => '', 'added_datetime' => '', 'added_by_user_id' => '', 'modified_datetime' => '', 'modified_by_user_id' => '', ); /* * * * @access public * @param * @return * @author Quinn Comendant * @version 1.0 * @since 16 Nov 2014 17:45:40 */ static public function insert($frm) { global $auth, $cache; $db =& DB::getInstance(); $app =& App::getInstance(); // Remove any stale cached list data. $cache->delete('payment list'); // Create the record under the user's account, unless an account_id was provided. $account_id = isset($frm['account_id']) && is_numeric($frm['account_id']) ? $frm['account_id'] : $auth->get('account_id'); // Insert record data. $app->logMsg(sprintf('%s with data %s', __METHOD__, getDump($frm)), LOG_DEBUG, __FILE__, __LINE__); $db->query(" INSERT INTO payment_tbl ( account_id, payment_date, amount, status, type, stripe_charge_id, added_datetime, added_by_user_id, modified_datetime, modified_by_user_id ) VALUES ( '" . $db->escapeString($account_id) . "', '" . $db->escapeString(strToSQLDate($frm['payment_date'])) . "', '" . $db->escapeString($frm['amount']) . "', '" . $db->escapeString($frm['status']) . "', '" . $db->escapeString($frm['type']) . "', '" . $db->escapeString($frm['stripe_charge_id']) . "', NOW(), '" . $db->escapeString($auth->get('user_id')) . "', NOW(), '" . $db->escapeString($auth->get('user_id')) . "' ) "); $last_insert_id = mysql_insert_id($db->getDBH()); // Create version. $version = Version::getInstance($auth); $version->create('payment_tbl', 'payment_id', $last_insert_id, $frm['payment_id']); return $last_insert_id; } /* * * * @access public * @param * @return * @author Quinn Comendant * @version 1.0 * @since 16 Nov 2014 17:45:40 */ static public function update($frm) { global $auth, $lock, $cache; $db =& DB::getInstance(); $app =& App::getInstance(); $lock->select('payment_tbl', 'payment_id', $frm['payment_id']); if ($lock->isLocked() && !$lock->isMine()) { $lock->dieErrorPage(); } // Remove any stale cached list data. $cache->delete('payment list'); // Create the record under the user's account, unless an account_id was provided. $account_id = isset($frm['account_id']) && is_numeric($frm['account_id']) ? $frm['account_id'] : $auth->get('account_id'); // Update record data. $app->logMsg(sprintf('%s with data %s', __METHOD__, getDump($frm)), LOG_DEBUG, __FILE__, __LINE__); $db->query(" UPDATE payment_tbl SET account_id = '" . $db->escapeString($account_id) . "', payment_date = '" . $db->escapeString(strToSQLDate($frm['payment_date'])) . "', amount = '" . $db->escapeString($frm['amount']) . "', status = '" . $db->escapeString($frm['status']) . "', type = '" . $db->escapeString($frm['type']) . "', stripe_charge_id = '" . $db->escapeString($frm['stripe_charge_id']) . "', modified_datetime = NOW(), modified_by_user_id = '" . $db->escapeString($auth->get('user_id')) . "' WHERE payment_id = '" . $db->escapeString($frm['payment_id']) . "' "); // Create version. $version = Version::getInstance($auth); $version->create('payment_tbl', 'payment_id', $frm['payment_id'], $frm['payment_id']); // Unlock record. $lock->remove(); } /* * * * @access public * @param * @return * @author Quinn Comendant * @version 1.0 * @since 16 Nov 2014 17:45:40 */ static public function delete($id) { global $lock, $cache, $locally_carried_queries; $db =& DB::getInstance(); $app =& App::getInstance(); $lock->select('payment_tbl', 'payment_id', $id); if ($lock->isLocked() && !$lock->isMine()) { $lock->dieErrorPage(); } // Remove any stale cached list data. $cache->delete('payment list'); // Delete the record. $db->query("DELETE FROM payment_tbl WHERE payment_id = '" . $db->escapeString($id) . "'"); // Unlock record. $lock->remove(); } /* * * * @access public * @param * @return * @author Quinn Comendant * @version 1.0 * @since 16 Nov 2014 17:45:40 */ static public function getPaginatedList($where_clause='') { global $page, $so, $cache; $db =& DB::getInstance(); $app =& App::getInstance(); // Build search query if available. if (getFormData('q', false)) { $qry_words = preg_split('/[^\w]/', getFormData('q')); for ($i=0; $iescapeString($qry_words[$i]) . "%' OR payment_tbl.amount LIKE '%" . $db->escapeString($qry_words[$i]) . "%' OR payment_tbl.stripe_charge_id LIKE '%" . $db->escapeString($qry_words[$i]) . "%' ) "; } } if (getFormData('filter_account_id', false)) { // Limit by filter. $where_clause .= (empty($where_clause) ? 'WHERE' : ' AND') . " payment_tbl.account_id = '" . $db->escapeString(getFormData('filter_account_id')) . "'"; } // Count the total number of records so we can do something about the page numbers. $qid = $db->query(" SELECT COUNT(*) FROM payment_tbl $where_clause "); list($num_results) = mysql_fetch_row($qid); // Set page numbers now we know (needed for next step). $page->setTotalItems($num_results); $page->calculate(); // Final SQL, with sort and page limiters. $sql = " SELECT payment_tbl.*, a1.username AS added_by_username, a2.username AS modified_by_username FROM payment_tbl LEFT JOIN user_tbl a1 ON (payment_tbl.added_by_user_id = a1.user_id) LEFT JOIN user_tbl a2 ON (payment_tbl.modified_by_user_id = a2.user_id) $where_clause " . $so->getSortOrderSQL() . " " . $page->getLimitSQL() . " "; // Use a cache hash to determine if the result-set has changed. // A unique key for this query, with the total_items in case db records // were added since the last cache. This identifies a unique set of // cached data, but we must refer to the list that is cached by a more // generic name. so that we can flush the cache (if records updated) // without knowing the hash. $cache_hash = md5($sql . '|' . $page->total_items); $tmp_prefs = new Prefs('session', array('storagetype' => 'session')); if ($tmp_prefs->get('cache_hash') != $cache_hash) { $cache->delete('payment list'); $tmp_prefs->set('cache_hash', $cache_hash); } // First try to return from the cache. if ($cache->exists('payment list')) { return $cache->get('payment list'); } // The list was not cached, so issue the real query. $qid = $db->query($sql); $list = array(); while ($row = mysql_fetch_assoc($qid)) { $list[] = $row; } // Save this list into the cache. if (isset($list) && !empty($list)) { $cache->set('payment list', $list); } return $list; } /* * Return array of all Payments of an account. By default includes only Paid payment_status. * * @access public * @param * @return * @author Quinn Comendant * @version 1.0 * @since 14 Dec 2011 20:07:16 */ // static public function getListByAccount($account_id=null) // { // $db =& DB::getInstance(); // global $cfg; // // $qid = $db->query(" // SELECT // payment_tbl.*, // account_tbl.account_id, // account_tbl.organization, // user_tbl.email, // user_tbl.phone, // user_tbl.first_name, // user_tbl.last_name // FROM payment_tbl // LEFT JOIN account_tbl ON (payment_tbl.account_id = account_tbl.account_id) // LEFT JOIN user_tbl ON (account_tbl.account_id = user_tbl.account_id) // WHERE user_tbl.user_type = 'primary' // AND account_tbl.account_id = '" . $db->escapeString($account_id) . "' // ORDER BY payment_date DESC // "); // $payments = array(); // while ($p = mysql_fetch_assoc($qid)) { // $payments[] = $p; // } // return $payments; // } }