00001 <?php
00002 class Mage_Weee_Model_Mysql4_Tax extends Mage_Core_Model_Mysql4_Abstract {
00003 protected function _construct()
00004 {
00005 $this->_init('weee/tax', 'value_id');
00006 }
00007
00008 public function fetchOne($select)
00009 {
00010 return $this->_getReadAdapter()->fetchOne($select);
00011 }
00012
00013 public function fetchCol($select)
00014 {
00015 return $this->_getReadAdapter()->fetchCol($select);
00016 }
00017
00018 public function updateDiscountPercents()
00019 {
00020 $this->_getWriteAdapter()->delete($this->getTable('weee/discount'));
00021 $now = strtotime(now());
00022
00023 $select = $this->_getReadAdapter()->select();
00024 $select->from(array('data'=>$this->getTable('catalogrule/rule_product')))
00025 ->where('(from_time <= ? OR from_time = 0)', $now)
00026 ->where('(to_time >= ? OR to_time = 0)', $now)
00027 ->order(array('data.website_id', 'data.customer_group_id', 'data.product_id', 'data.sort_order'));
00028
00029 $data = $this->_getReadAdapter()->fetchAll($select);
00030 $productData = array();
00031 $stops = array();
00032 foreach ($data as $row) {
00033 $key = "{$row['product_id']}-{$row['website_id']}-{$row['customer_group_id']}";
00034 if (isset($stops[$key]) && $stops[$key]) {
00035 continue;
00036 }
00037
00038 if ($row['action_operator'] == 'by_percent') {
00039 if (isset($productData[$key])) {
00040 $productData[$key]['value'] += $productData[$key]['value']/100*$row['action_amount'];
00041 } else {
00042 $productData[$key] = array(
00043 'entity_id' => $row['product_id'],
00044 'customer_group_id' => $row['customer_group_id'],
00045 'website_id' => $row['website_id'],
00046 'value' => 100-max(0, min(100, $row['action_amount'])),
00047 );
00048 }
00049 }
00050
00051 if ($row['action_stop']) {
00052 $stops[$key] = true;
00053 }
00054 }
00055
00056 foreach ($productData as $product) {
00057 $this->_getWriteAdapter()->insert($this->getTable('weee/discount'), $product);
00058 }
00059 }
00060
00061 public function getProductDiscountPercent($product, $website, $group)
00062 {
00063 $select = $this->_getReadAdapter()->select();
00064 $select->from($this->getTable('weee/discount'), 'value')
00065 ->where('website_id = ?', $website)
00066 ->where('entity_id = ?', $product)
00067 ->where('customer_group_id = ?', $group);
00068
00069 return $this->_getReadAdapter()->fetchOne($select);
00070 }
00071 }