shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

reward.php (2607B)


      1 <?php
      2 class ModelExtensionTotalReward extends Model {
      3 	public function getTotal($total) {
      4 		if (isset($this->session->data['reward'])) {
      5 			$this->load->language('extension/total/reward', 'reward');
      6 
      7 			$points = $this->customer->getRewardPoints();
      8 
      9 			if ($this->session->data['reward'] <= $points) {
     10 				$discount_total = 0;
     11 
     12 				$points_total = 0;
     13 
     14 				foreach ($this->cart->getProducts() as $product) {
     15 					if ($product['points']) {
     16 						$points_total += $product['points'];
     17 					}
     18 				}
     19 
     20 				$points = min($points, $points_total);
     21 
     22 				foreach ($this->cart->getProducts() as $product) {
     23 					$discount = 0;
     24 
     25 					if ($product['points']) {
     26 						$discount = $product['total'] * ($this->session->data['reward'] / $points_total);
     27 
     28 						if ($product['tax_class_id']) {
     29 							$tax_rates = $this->tax->getRates($product['total'] - ($product['total'] - $discount), $product['tax_class_id']);
     30 
     31 							foreach ($tax_rates as $tax_rate) {
     32 								if ($tax_rate['type'] == 'P') {
     33 									$total['taxes'][$tax_rate['tax_rate_id']] -= $tax_rate['amount'];
     34 								}
     35 							}
     36 						}
     37 					}
     38 
     39 					$discount_total += $discount;
     40 				}
     41 
     42 				$total['totals'][] = array(
     43 					'code'       => 'reward',
     44 					'title'      => sprintf($this->language->get('reward')->get('text_reward'), $this->session->data['reward']),
     45 					'value'      => -$discount_total,
     46 					'sort_order' => $this->config->get('total_reward_sort_order')
     47 				);
     48 
     49 				$total['total'] -= $discount_total;
     50 			}
     51 		}
     52 	}
     53 
     54 	public function confirm($order_info, $order_total) {
     55 		$this->load->language('extension/total/reward');
     56 
     57 		$points = 0;
     58 
     59 		$start = strpos($order_total['title'], '(') + 1;
     60 		$end = strrpos($order_total['title'], ')');
     61 
     62 		if ($start && $end) {
     63 			$points = substr($order_total['title'], $start, $end - $start);
     64 		}
     65 
     66 		$this->load->model('account/customer');
     67 
     68 		if ($this->model_account_customer->getRewardTotal($order_info['customer_id']) >= $points) {
     69 			$this->db->query("INSERT INTO " . DB_PREFIX . "customer_reward SET customer_id = '" . (int)$order_info['customer_id'] . "', order_id = '" . (int)$order_info['order_id'] . "', description = '" . $this->db->escape(sprintf($this->language->get('text_order_id'), (int)$order_info['order_id'])) . "', points = '" . (float)-$points . "', date_added = NOW()");
     70 		} else {
     71 			return $this->config->get('config_fraud_status_id');
     72 		}
     73 	}
     74 
     75 	public function unconfirm($order_id) {
     76 		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_reward WHERE order_id = '" . (int)$order_id . "' AND points < 0");
     77 	}
     78 }