Transaction.php

Go to the documentation of this file.
00001 <?php
00003 // {{{ license
00004 
00005 // +----------------------------------------------------------------------+
00006 // | FastFrame Application Framework                                      |
00007 // +----------------------------------------------------------------------+
00008 // | Copyright (c) 2002-2006 The Codejanitor Transaction                        |
00009 // +----------------------------------------------------------------------+
00010 // | This source file is subject to the GNU Lesser Public License (LGPL), |
00011 // | that is bundled with this package in the file LICENSE, and is        |
00012 // | available at through the world-wide-web at                           |
00013 // | http://www.fsf.org/copyleft/lesser.html                              |
00014 // | If you did not receive a copy of the LGPL and are unable to          |
00015 // | obtain it through the world-wide-web, you can get it by writing the  |
00016 // | Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
00017 // | MA 02111-1307, USA.                                                  |
00018 // +----------------------------------------------------------------------+
00019 // | Authors: Jason Rust <jrust@codejanitor.com>                          |
00020 // | Authors: Greg Gilbert <ggilbert@codejanitor.com>                     |
00021 // +----------------------------------------------------------------------+
00022 
00023 // }}}
00024 // {{{ requires
00025 
00026 require_once FASTFRAME_ROOT . 'lib/FastFrame/DataAccess.php';
00027 require_once dirname(__FILE__) . '/Loan.php';
00028 
00029 // }}}
00030 // {{{ class FF_DataAccess_Transaction 
00031 
00042 // }}}
00043 class FF_DataAccess_Transaction_mysql extends FF_DataAccess {
00044     // {{{ properties
00045 
00050     var $itemsTable;
00051 
00056     var $groupsTable;
00057 
00062     var $loansTable;
00063 
00064     // }}}
00065     // {{{ constructor
00066 
00073     function FF_DataAccess_Transaction_mysql()
00074     {
00075         FF_DataAccess::FF_DataAccess();
00076         $this->table = $this->o_registry->getConfigParam('data/transaction_items_table');
00077         $this->itemsTable = $this->o_registry->getConfigParam('data/items_table');
00078         $this->groupsTable = $this->o_registry->getConfigParam('data/groups_table');
00079         $this->loansTable = $this->o_registry->getConfigParam('data/loans_table');
00080     }
00081 
00082     // }}}
00083     // {{{ removeItemInTransaction()
00084 
00096     function removeItemInTransaction($in_transactionId, $in_itemId, $in_limit, $in_isGroup = false)
00097     {
00098         $o_result = new FF_Result();
00099         $s_stmt = $this->o_data->prepare("DELETE FROM $this->table 
00100                                           WHERE transaction_id=? AND item_id=? AND is_group=? LIMIT $in_limit");
00101 
00102         $a_data = array($in_transactionId, $in_itemId, $in_isGroup);
00103         if (DB::isError($result = $this->o_data->execute($s_stmt, $a_data))) {
00104             $o_result->addMessage($result->getMessage());
00105             $o_result->setSuccess(false);
00106         }
00107 
00108         return $o_result;
00109     }
00110 
00111     // }}}
00112     // {{{ returnItemsInTransaction()
00113 
00123     function &returnItemsInTransaction($in_items)
00124     {
00125         require_once dirname(__FILE__) . '/../../Model/Log.php';
00126         $o_log =& new FF_Model_Log();
00127         $o_result = new FF_Result();
00128         $s_itemList = '';
00129         $a_items = array();
00130         $in_items = $this->_matchItemsToTransaction($in_items);
00131         // Time needs to be exactly same for both log and return date so we can match them up.
00132         $s_timestamp = time();
00133         foreach ($in_items as $a_data) {
00134             $s_itemList .= sprintf('(item_id = %s AND transaction_id = %s) OR ',
00135                     $this->o_data->quoteSmart($a_data['item_id']),
00136                     $this->o_data->quoteSmart($a_data['transaction_id']));
00137 
00138             $a_items[] = $a_data['item_id'];
00139             $o_log->newLogEntry('ITEM_RETURNED', $a_data['item_id'], $this->table, $s_timestamp, $a_data['transaction_id']);
00140         }
00141 
00142         $s_itemList .= '0=1';
00143         $s_query = "UPDATE $this->table SET return_date = ? WHERE $s_itemList"; 
00144         if (DB::isError($result = $this->o_data->query($s_query, array($this->timestampToISODate($s_timestamp))))) {
00145             $o_result->addMessage($result->getMessage());
00146             $o_result->setSuccess(false);
00147         }
00148 
00149         require_once dirname(__FILE__) . '/../../Model/Item.php';
00150         $o_itemDataAccess =& FF_DataAccess::factory('Item');
00151         $tmp_result =& $o_itemDataAccess->updateStatusOfItems($a_items, ITEM_STATUS_AVAILABLE);
00152         if (!$tmp_result->isSuccess()) {
00153             $o_result->addMessage($tmp_result->getMessages());
00154             $o_result->setSuccess(false);
00155         }
00156 
00157         return $o_result;
00158     }
00159 
00160     // }}}
00161     // {{{ getBorrowersFromReturnItems()
00162 
00171     function getBorrowersFromReturnItems($in_items)
00172     {
00173         $s_itemList = '';
00174         $a_items = array();
00175         foreach ($in_items as $a_data) {
00176             if (!is_null($a_data['itemId'])) {
00177                 $s_itemList .= 'item_id=' . $this->o_data->quoteSmart($a_data['itemId']) . " OR \n";
00178                 $a_items[] = $a_data['itemId'];
00179             }
00180         }
00181 
00182         $s_itemList .= '0=1';
00183         // NOTE: Assuming items were returned in the last 120 seconds,
00184         // so basically returnItemsInTransaction() and this method should
00185         // be called side by side.
00186         $s_query = "SELECT t2.borrower_id, COUNT(t1.item_id) 
00187                     FROM $this->table AS t1
00188                     INNER JOIN $this->loansTable AS t2 ON t1.transaction_id = t2.id
00189                     WHERE t1.return_date + 0 >= NOW() - 120 AND t1.is_group = 0 
00190                     AND t2.type = ? AND ($s_itemList)
00191                     GROUP BY t2.borrower_id";
00192 
00193         return $this->o_data->getAssoc($s_query, false, LOANS_TYPE_LOAN);
00194     }
00195 
00196     // }}}
00197     // {{{ getReturnItemsForBorrower()
00198 
00208     function getReturnItemsForBorrower($in_borrowerId, $in_timestamp)
00209     {
00210         // :NOTE: Assuming items were returned within 120 seconds of the timestamp,
00211         settype($in_timestamp, 'int');
00212         $s_query = "SELECT t.item_id, UNIX_TIMESTAMP(t.return_date) AS return_date, i.name, i.barcode
00213                     FROM $this->table AS t
00214                     INNER JOIN $this->loansTable AS l ON t.transaction_id=l.id
00215                     INNER JOIN $this->itemsTable AS i ON t.item_id=i.id
00216                     WHERE l.borrower_id=? AND UNIX_TIMESTAMP(t.return_date) >= $in_timestamp - 120 AND t.is_group=0
00217                     ORDER BY i.name";
00218 
00219         return $this->o_data->getAll($s_query, array($in_borrowerId));
00220     }
00221 
00222     // }}}
00223     //{{{ getItemIdsByTransactionId()
00224 
00234     function getItemIdsByTransactionId($in_transactionId, $in_onlyNonReturned = false)
00235     {
00236         $s_query = "SELECT item_id, is_group FROM $this->table WHERE transaction_id=?";
00237         if ($in_onlyNonReturned) {
00238             $s_query .= ' AND return_date=0';
00239         }
00240 
00241         return $this->o_data->getAssoc($s_query, false, $in_transactionId);
00242     }
00243     
00244     // }}}
00245     //{{{ getItemsByTransactionId()
00246 
00256     function getItemsByTransactionId($in_transactionId, $in_excludeReturned = false)
00257     {
00258         $s_excludeReturned = '';
00259         if ($in_excludeReturned) {
00260             $s_excludeReturned = ' AND return_date = 0';
00261         }
00262 
00263         $s_query = "SELECT t.transaction_id, t.item_id, 
00264                     UNIX_TIMESTAMP(t.return_date) AS return_date, 
00265                     IF(t.is_group = 1, 1, 0) AS is_group, 
00266                     IF(i.name IS NULL, g.name, i.name) AS name,
00267                     IF(t.is_group = 1, g.is_kit, 0) AS is_kit,
00268                     IF(i.description IS NULL, g.description, i.description) AS description,
00269                     i.barcode, i.status
00270                     FROM $this->table AS t
00271                     LEFT JOIN $this->itemsTable AS i ON t.item_id = i.id
00272                     LEFT JOIN $this->groupsTable AS g ON t.item_id = g.id
00273                     WHERE t.transaction_id = ? $s_excludeReturned
00274                     ORDER BY t.return_date, i.name, g.name";
00275 
00276         if (DB::isError($result = $this->o_data->getAll($s_query, array($in_transactionId)))) {
00277             return array();
00278         }
00279         else {
00280             return $result;
00281         }
00282     }
00283     
00284     // }}}
00285     //{{{ getItems()
00286 
00296     function getItems($in_items)
00297     {
00298         $s_itemList = '';
00299         $s_groupList = '';
00300         $a_groupCount = array();
00301         foreach ($in_items as $a_item) {
00302             if (!is_null($a_item['itemId'])) {
00303                 if ($a_item['isGroup']) {
00304                     if (!isset($a_groupCount[$a_item['itemId']])) {
00305                         $a_groupCount[$a_item['itemId']] = array(0, false);
00306                     }
00307 
00308                     $a_groupCount[$a_item['itemId']][0]++;
00309                     $s_groupList .= sprintf('id = %s OR ', $this->o_data->quoteSmart($a_item['itemId']));
00310                 }
00311                 else {
00312                     $s_itemList .= sprintf('id = %s OR ', $this->o_data->quoteSmart($a_item['itemId']));
00313                 }
00314             }
00315         }
00316 
00317         $s_itemList .= '0=1';
00318         $s_groupList .= '0=1';
00319         $s_query = "SELECT id AS item_id, name, barcode, status FROM $this->itemsTable WHERE $s_itemList";
00320         $a_items = $this->o_data->getAll($s_query);
00321         foreach (array_keys($a_items) as $s_key) {
00322             $a_items[$s_key]['transaction_id'] = null;
00323             $a_items[$s_key]['return_date'] = 0; 
00324             $a_items[$s_key]['is_kit'] = false; 
00325             $a_items[$s_key]['is_group'] = false;
00326             $a_items[$s_key]['notSaved'] = true;
00327         }
00328 
00329         $s_query = "SELECT id AS item_id, name, is_kit FROM $this->groupsTable WHERE $s_groupList";
00330         $tmp_items = $this->o_data->getAll($s_query);
00331         foreach (array_keys($tmp_items) as $s_key) {
00332             $tmp_items[$s_key]['transaction_id'] = null;
00333             $tmp_items[$s_key]['return_date'] = 0; 
00334             $tmp_items[$s_key]['is_group'] = true;
00335             $tmp_items[$s_key]['notSaved'] = true;
00336             $a_groupCount[$tmp_items[$s_key]['item_id']][1] = $s_key;
00337         }
00338         
00339         // Because the above query for groups will only return 1
00340         // instance of a group even if X requests for the same group were
00341         // made we need to add the requested number of groups back in
00342         foreach ($a_groupCount as $s_id => $a_vals) {
00343             if ($a_vals[0] > 1 && $a_vals[1] !== false) {
00344                 for ($i = 1; $i < $a_vals[0]; $i++) {
00345                     $tmp_items[] = $tmp_items[$a_vals[1]];
00346                 }
00347             }
00348         }
00349 
00350         $a_items = array_merge($a_items, $tmp_items);
00351         return $a_items;
00352     }
00353     
00354     // }}}
00355     // {{{ updateItemReturnDates()
00356 
00367     function &updateItemReturnDates($in_items, $in_id, $in_timestamp)
00368     {
00369         $o_result = new FF_Result();
00370         $s_itemList = '';
00371         foreach ($in_items as $s_itemId) {
00372             $s_itemList .= 'item_id = ' . $this->o_data->quoteSmart($s_itemId) . " OR \n";
00373         }
00374 
00375         $s_itemList .= '0=1';
00376         $s_stmt = $this->o_data->prepare("UPDATE $this->table SET return_date = ? 
00377                                           WHERE transaction_id = ? AND ($s_itemList)");
00378 
00379         $a_data = array($this->timestampToISODate($in_timestamp), $in_id);
00380         if (DB::isError($result = $this->o_data->execute($s_stmt, $a_data))) {
00381             $o_result->addMessage($result->getMessage());
00382             $o_result->setSuccess(false);
00383         }
00384 
00385         return $o_result;
00386     }
00387 
00388     // }}}
00389     // {{{ filterOutReturnedItems()
00390 
00401     function filterOutReturnedItems($in_items, $in_transactionId)
00402     {
00403         $s_itemList = '';
00404         foreach ($in_items as $s_itemId) {
00405             $s_itemList .= 'item_id = ' . $this->o_data->quoteSmart($s_itemId) . " OR \n";
00406         }
00407 
00408         $s_itemList .= '0=1';
00409         $s_query = "SELECT item_id FROM $this->table WHERE transaction_id = ? AND return_date = 0 AND ($s_itemList)";
00410         return (array) $this->o_data->getCol($s_query, 0, $in_transactionId);
00411     }
00412 
00413     // }}}
00414     // {{{ getMostRecentBorrower()
00415 
00424     function getMostRecentBorrower($in_id)
00425     {
00426         $s_query = "SELECT l.borrower_id FROM $this->table AS t 
00427                     INNER JOIN $this->loansTable AS l ON t.transaction_id = l.id
00428                     WHERE t.item_id = ? 
00429                     ORDER BY l.due_date DESC LIMIT 1";
00430 
00431         return $this->o_data->getOne($s_query, $in_id);
00432 
00433     }
00434 
00435     // }}}
00436     // {{{ _matchItemsToTransaction()
00437 
00447     function _matchItemsToTransaction($in_items)
00448     {
00449         $s_itemList = '';
00450         foreach ($in_items as $a_data) {
00451             if (!is_null($a_data['itemId'])) {
00452                 $s_itemList .= sprintf('t1.item_id = %s OR ', $this->o_data->quoteSmart($a_data['itemId']));
00453             }
00454         }
00455 
00456         $s_itemList .= '0=1';
00457         $s_query = "SELECT t1.item_id, t1.transaction_id
00458                     FROM $this->table AS t1 
00459                     INNER JOIN $this->loansTable AS t2 ON t1.transaction_id = t2.id
00460                     WHERE t1.return_date = 0 AND t1.is_group = 0 AND t2.type = ? AND ($s_itemList)";
00461         
00462         return $this->o_data->getAll($s_query, array(LOANS_TYPE_LOAN));
00463     }
00464 
00465     // }}}
00466 }
00467 ?>

Generated on Fri Jun 23 11:38:18 2006 for FastFrame by  doxygen 1.4.4