コード例 #1
0
 /**
  * Retrieve actual history records that have unused points, i.e. points_delta-points_used > 0
  * Update points_used field for non-used points
  *
  * @param Enterprise_Reward_Model_Reward_History $history
  * @param int $required Points total that required
  * @return Enterprise_Reward_Model_Mysql4_Reward_History
  */
 public function useAvailablePoints($history, $required)
 {
     $required = (int) abs($required);
     if (!$required) {
         return $this;
     }
     $adapter = $this->_getWriteAdapter();
     try {
         $adapter->beginTransaction();
         $select = $adapter->select()->from(array('history' => $this->getMainTable()), array('history_id', 'points_delta', 'points_used'))->where('reward_id = :reward_id')->where('website_id = :website_id')->where('is_expired=0')->where('points_delta - points_used > 0')->order('history_id')->forUpdate(true);
         $bind = array(':reward_id' => $history->getRewardId(), ':website_id' => $history->getWebsiteId());
         $stmt = $adapter->query($select, $bind);
         $updateSqlValues = array();
         $data = array();
         while ($row = $stmt->fetch()) {
             if ($required <= 0) {
                 break;
             }
             $rowAvailable = $row['points_delta'] - $row['points_used'];
             $pointsUsed = min($required, $rowAvailable);
             $required -= $pointsUsed;
             $newPointsUsed = $pointsUsed + $row['points_used'];
             $data[] = array('history_id' => $row['history_id'], 'points_used' => $newPointsUsed);
         }
         if (count($data) > 0) {
             $adapter->insertOnDuplicate($this->getMainTable(), $data, array('history_id', 'points_used'));
         }
         $adapter->commit();
     } catch (Exception $e) {
         $adapter->rollback();
         throw $e;
     }
     return $this;
 }