<?php
/**
* J2T RewardsPoint2
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@j2t-design.com so we can send you a copy immediately.
*
* @category Magento extension
* @package RewardsPoint2
* @copyright Copyright (c) 2009 J2T DESIGN. (http://www.j2t-design.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Rewardpoints_Model_Account extends Mage_Core_Model_Abstract {
protected $customerId = -1;
protected $storeId = -1;
protected $pointsCurrent = NULL;
protected $pointsSpent = NULL;
protected $rewardpointsAccountId = NULL;
public function saveCheckedOrder($orderId, $customerId, $storeId, $pointsCurrent, $referral = null, $no_check = false){
$this->customerId = $customerId;
$this->storeId = $storeId;
$this->pointsCurrent = $pointsCurrent;
$this->save($orderId, $no_check, $referral);
}
public function save($orderId = null, $no_check = false, $referral = null) {
if (!$no_check){
$connection = Mage::getSingleton('core/resource')
->getConnection('rewardpoints_read');
$select = $connection->select()
->from(Mage::getSingleton('core/resource')->getTableName('rewardpoints_account'), array('COUNT(rewardpoints_account_id) AS nb_rewards'))
->where(Mage::getSingleton('core/resource')->getTableName('rewardpoints_account').'.customer_id=?', $this->customerId)
->where(Mage::getSingleton('core/resource')->getTableName('rewardpoints_account').".order_id=?", $orderId)
->where(Mage::getSingleton('core/resource')->getTableName('rewardpoints_account').".store_id=?", $this->storeId)
->group(Mage::getSingleton('core/resource')->getTableName('rewardpoints_account').'.customer_id');
$data = $connection->fetchRow($select);
} else {
$data['nb_rewards'] = 0;
}
if($data['nb_rewards'] == 0){
$connection = Mage::getSingleton('core/resource')
->getConnection('rewardpoints_write');
$connection->beginTransaction();
$fields = array();
$fields['customer_id'] = $this->customerId;
$fields['store_id'] = $this->storeId;
$fields['points_current'] = $this->pointsCurrent;
//$fields['points_received'] = $this->pointsReceived;
if ($referral != null){
$fields['rewardpoints_referral_id'] = $referral;
}
$fields['points_spent'] = $this->pointsSpent;
$fields['order_id'] = $orderId;
try {
if (!is_null($this->rewardpointsAccountId)) {
$where = $connection->quoteInto('customer_id=?', $fields['customer_id']);
$connection->update(Mage::getSingleton('core/resource')->getTableName('rewardpoints_account'), $fields, $where);
} else {
$fields['date_start'] = date('Y-m-d');
$extra_days = Mage::getStoreConfig('rewardpoints/default/points_duration', Mage::app()->getStore()->getId());
if ($extra_days){
$fields['date_end'] = date("Y-m-d", mktime(date("H"), date("i"), date("s"), date("n"), date("j") + $extra_days, date("Y")));
}
$connection->insert(Mage::getSingleton('core/resource')->getTableName('rewardpoints_account'), $fields);
$this->rewardpointsAccountId = $connection->lastInsertId(Mage::getSingleton('core/resource')->getTableName('rewardpoints_account'));
}
$connection->commit();
}
catch (Exception $e) {
$connection->rollBack();
throw $e;
}
}
return $this;
}
public function load($id, $field=null) {
if ($field === null) {
$field = 'customer_id';
}
$this->customerId = $id;
$this->storeId = Mage::app()->getStore()->getId();
return $this;
}
//add and subtract points methods
public function addPoints($p) {
$this->pointsCurrent += $p;
}
public function subtractPoints($p) {
$this->pointsSpent = $p;
}
/**
* Setters
*
* @param unknown_type $value
*/
public function setRewardpointsAccountId($value){
$this->rewardpointsAccountId = $value;
}
public function setCustomerId($value){
$this->cutomerId = $value;
}
public function setStoreId($value){
$this->storeId = $value;
}
public function setPointsCurrent($value){
$this->pointsCurrent = $value;
}
public function setPointsSpent($value){
$this->pointsSpent = $value;
}
/**
* Getters
*
* @return unknown
*/
public function getPointsCurrent(){
$total = $this->getPointsReceived() - $this->getPointsSpent();
if ($total > 0){
return $total;
} else {
return 0;
}
}
public function getPointsReceived(){
$order_states = array("processing","complete");
$connection = Mage::getSingleton('core/resource')
->getConnection('rewardpoints_read');
//echo Mage::getSingleton('core/resource')->getTableName('rewardpoints_account').' test ';
//die;
$select = $connection->select()
/*->from('rewardpoints_account', array('SUM(points_current) AS nb_credit'))*/
->from(Mage::getSingleton('core/resource')->getTableName('rewardpoints_account'),array(new Zend_Db_Expr('SUM('.Mage::getSingleton('core/resource')->getTableName('rewardpoints_account').'.points_current) AS nb_credit'),new Zend_Db_Expr('SUM('.Mage::getSingleton('core/resource')->getTableName('rewardpoints_account').'.points_spent)')))
->where(" (".Mage::getSingleton('core/resource')->getTableName('rewardpoints_account').".order_id = '".Rewardpoints_Model_Stats::TYPE_POINTS_ADMIN."' or ".Mage::getSingleton('core/resource')->getTableName('rewardpoints_account').".order_id = '".Rewardpoints_Model_Stats::TYPE_POINTS_REGISTRATION."'
or ".Ma
- 1
- 2
- 3
前往页