Warning: chmod(): Operation not permitted in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8
[0] in function chmod in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8
[1] in function include_once in /var/www/hopeinstoughton_www/wp-settings.php on line 560
[2] in function require_once in /var/www/hopeinstoughton_www/wp-config.php on line 85
[3] in function require_once in /var/www/hopeinstoughton_www/wp-load.php on line 50
[4] in function require_once in /var/www/hopeinstoughton_www/wp-blog-header.php on line 13
[5] in function require in /var/www/hopeinstoughton_www/index.php on line 17
403WebShell
403Webshell
Server IP : 94.177.8.99  /  Your IP : 216.73.217.165
Web Server : Apache/2.4.58 (Ubuntu)
System : Linux aries 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64
User : www-data ( 33)
PHP Version : 8.1.34
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : OFF
Directory :  /var/www/theyoungdesigners_com/modules/storeDiscount/model/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/theyoungdesigners_com/modules/storeDiscount/model/storediscount_model.php
<?php

class StoreDiscount_model extends Database_Model {

	public function __construct() {
		parent::__construct();
	}
	
	private $type = NULL;
	private $id = NULL;
	private $method;
	private $changed;
	private $values;
	private $sd_customer;
	private $sd_catalog;
	private $sd_coupon;
	private $model_order;

	public function ModelOrder() {
		if (func_num_args()) {
			$this->model_order = func_get_arg(0);
			return $this;
		}
		return $this->model_order;
	}

	public function Type() {					// 'C' = category, 'U' = user/customer, 'N' = coupon
		if (func_num_args()) {
			$this->type = func_get_arg(0);
			if (!is_null($this->type) && !is_null($this->id)) {
				$this->_Load();
			}
			return $this;
		}
		return $this->type;
	}
	public function Id() {
		if (func_num_args()) {
			$this->id = func_get_arg(0);
			if (!is_null($this->type) && !is_null($this->id)) {
				$this->_Load();
			}
			return $this;
		}
		return $this->id;
	}
	
	public function Method() {					// 'P' = percentage discount, 'A' = fixed amount
		if (func_num_args()) {
			$this->method = func_get_arg(0);
			$this->changed['sd_method'] = $this->method;
			return $this;
		}
		return $this->method;
	}

	public function Coupon() {			// for type 'N'
		if (func_num_args()) {
			$this->sd_coupon = func_get_arg(0);
			$this->changed['sd_coupon'] = $this->sd_coupon;
			return $this;
		}
		return $this->sd_coupon;
	}

	public function Customer() {				// for type 'N'
		if (func_num_args()) {
			$this->sd_customer = func_get_arg(0);
			if (empty($this->sd_customer)) $this->sd_customer = '';
			$this->changed['sd_customer'] = $this->sd_customer;
			return $this;
		}
		return $this->sd_customer;
	}

	public function ProductGroup() {			// for type 'N'
		if (func_num_args()) {
			$this->sd_catalog = intval(func_get_arg(0));
			$this->changed['sd_catalog'] = $this->sd_catalog;
			return $this;
		}
		return $this->sd_catalog;
	}

	
	public function Values() {
		if (func_num_args()) {
			$this->values = func_get_arg(0);
			$this->changed['sd_info'] = json_encode($this->values,  JSON_UNESCAPED_SLASHES);
			return $this;
		}
		if (is_null($this->values) || !count($this->values)) {
			return [ ['qty' => '', 'val' => '' ] ];
		}
		return $this->values;
	}

	protected function _Load() {
		$DATA = $this->Query('select * from `store_discount`')
					->Where('sd_type=%s', $this->type)
					->Where('sd_key=%d', $this->id)
					->First();
		$this->LoadFromDataset($DATA, $DATA->sd_type, $DATA->sd_key);
	}
	
	public function LoadCoupon($coupon) {
		$DATA = $this->Query('select * from `store_discount`')
					->Where('sd_type=%s', 'N')
					->Where('sd_coupon=%s', $coupon)
					->First();
		$this->LoadFromDataset($DATA, $DATA->sd_type, $DATA->sd_key);
	}
	
	public function LoadFromDataset($DATA, $sd_type = '', $sd_key = 0) {
		if ($sd_type != '') {
			$this->type	= $sd_type;
			$this->id	= $sd_key;
		}
		$this->Method($DATA->sd_method)
			 ->Values(json_decode($DATA->sd_info, TRUE))
			 ->Coupon($DATA->sd_coupon)
			 ->Customer($DATA->sd_customer)
			 ->ProductGroup($DATA->sd_catalog);
		$this->changed = [];
	}
	
	public function AsHandlebars($full = TRUE) {
		$data = [
			'method'	=> empty($this->method) ? 'P' : $this->method,
			'entry'		=> $this->Values(),
		];
		if ($full) {
			$data['authdisc']	= hook_execute('nonce.create', FALSE, 'disc', "{$this->type}:{$this->id}");
		}
		if ($this->type == 'N') {
			$data = array_merge($data, $data['entry']);
			$data['sd_coupon'] = $this->sd_coupon;
			$data['sd_customer'] = $this->sd_customer;
			$data['sd_catalog'] = $this->sd_catalog;
			unset($data['entry']);
		}
		return $data;
	}
	
	public function Update() {
		if (count($this->changed)) {
			$this->Tablename('store_discount')
				 ->InsertOrUpdate(['sd_type' => $this->type, 'sd_key' => $this->id], $this->changed);
			$this->DbErr();
			$this->changed = [];
		}
	}

	private function _getCategoryDiscount($item, $cat) {
		$discount = NULL; 
		$quantity = $item['count'];
		foreach($cat['values'] as $entry) {
			if ($quantity >= $entry['qty']) {
				$discount = $entry;
				$discount['method'] = $cat['method'];
			}
		}
		return $discount;
	}
	
	private function _applyDiscount(&$item, $discount) {
		$item['original'] = $item['price'];			// original non-discounted price
		if ($discount['method'] == 'A') {
			$item['price'] -= $discount['val'];
			if ($item['price'] < 0) $item['price'] = 0;
			$item['discount_applied'] = $discount['val'];
		} elseif ($discount['method'] == 'P') {
			$amount = $item['price'] * ($discount['val'] / 100);		// this is the amount to reduce
			$item['price'] -= $amount;
			$item['discount_applied'] = $amount;
			$item['price'] = round($item['price'], 2);
		}
	}

	public function applyCategoryDiscounts($cart, $categories) {
		$rows = $this->Query('select * from `store_discount`')
					->Where('sd_type=%s', 'C')
					->WhereIn('sd_key', array_keys($categories))
					->Get();
		foreach($rows as $row) {
			$category = $row->sd_key;
			$categories[$category] = [
				'method' => $row->sd_method,
				'values' => json_decode($row->sd_info, TRUE),
			];
		}
		foreach($cart as &$item) {
			$category = $item['category'];
			if (isset($categories[$category]) && !isset($item['discount_applied'])) {
				$discount = $this->_getCategoryDiscount($item, $categories[$category]);
				if (!is_null($discount)) {
					$this->_applyDiscount($item, $discount);
				}
			}
		}
		return $cart;
	}
		
	
	// adjust full price
	public function AdjustItem(&$item, $product) {
		$entry = [
			'price' => $product['price']['full'],
			'count' => $item['count'],
		];
		$cat = [ 'values' => $this->values, 'method' => $this->method ];
		$discount = $this->_getCategoryDiscount($entry, $cat);
		if (!is_null($discount)) {
			$this->_applyDiscount($entry, $discount);
			$item['price'] = $entry['price'];
		}
	}

	public function CustomFields($rep) {
		
		$rep->pagesize(10);
		$rep->AutoWidth(FALSE);

		$col = $rep->AddColumn();
		$col->name('sd_coupon')
			->caption('Coupon')
			->Width(88)
			->render(function($value, $item) {
				$safevalue = htmlspecialchars($value);
				$auth = hook_execute('nonce.create', FALSE, 'coupon', $item['id'], 600);
				return "<a href=\"/admin/store/coupon?auth={$auth}\">$safevalue</a>";
			});


		$col = $rep->AddColumn();
		$col->name('sc_caption')
			->width(200)
			->caption('Product Group');
			
		$col = $rep->AddColumn();
		$col->name('name')
			->caption('Customer');

		$col = $rep->AddColumn();
		$col->name('active')
			->caption('Active Date')
			->width(220)
			->render(function($value, $item) {
				return [
					'disp' => trim("{$item['dt_start']} - {$item['dt_end']}"),
					'val'  => $item['dt_start'] != '' ? strtotime($item['dt_start'] . ' Z') : 0,
				];
			});

		$col = $rep->AddColumn();
		$col->name('minimum')
			->caption('Min.')
			->className('right')
			->width(88);

		$col = $rep->AddColumn();
		$col->name('amount')
			->caption('Value')
			->width(88)
			->className('right')
			->render(function($value, $item) {
				return [
					'disp'	=> $item['sd_method'] = 'P' ? "{$value}%" : "\${$value}",
					'val'	=> $value
				];
			});
//		$col = $rep->AddColumn();
//		$col->name('sold')
//			->caption('Sold');

		$col = $rep->AddColumn();
		$col->name('id')
			->width(0);
		$col = $rep->AddColumn();
		$col->name('sd_method')
			->width(0);
			

	}

	public function getData($sql) {
		$sql = <<<'BLOCK'
select 
  d.*, 
  sc_caption,
  concat(name_first,' ',name_last) as name
from `store_discount`  as d
  left join `store_category` as c 	on sc_id=sd_catalog
  left join `users` as u 			on user_id=sd_customer
where sd_type="N"
BLOCK;
		$data2 = [];
		$data = $this->Query($sql)
					->Get();
		foreach($data as $row) {
			$payload = json_decode($row->sd_info, TRUE);
			$payload['id'] = $row->sd_key;
			$payload['method'] = $row->sd_method;
			$payload['name'] = $row->name;
			$payload['sd_coupon'] = $row->sd_coupon;
			$payload['sd_customer'] = $row->sd_customer;
			$payload['sd_catalog'] = $row->sd_catalog;
			$payload['sc_caption'] = $row->sc_caption;
			$data2[] = $payload;
		} 
		return $data2;
	}

	// `id` is crc32()
	public function CouponList($rep) {
		$data2 = [];
		if (is_null($rep)) {
			return $this->getData();
		
		} else {
			$rep->onData([$this, 'getData']);
			$data2 = $rep->Execute();
			return ['data' => $data2];
		}
		
	}
	
	public function updateCoupon($post) {
		$coupon = strtoupper(trim($post['sd_coupon']));
		if (empty($this->id)) {
			$this->id = crc32($coupon);
		}
		$payload = [
			'dt_start'	=> trim($post['dt_start']),
			'dt_end'	=> trim($post['dt_end']),
			'amount'	=> floatval($post['amount']),
			'minimum'	=> floatval($post['minimum']),
		];
		$post['sd_catalog']	= trim($post['sd_catalog']);
		$post['sd_customer'] = preg_match('~\[(\d+)\]~', trim($post['sd_customer']), $match) ? $match[1] : '';
		$data = [
			'sd_coupon'		=> $coupon,
			'sd_catalog'	=> empty($post['sd_catalog']) ? 0 : $post['sd_catalog'],
			'sd_customer'	=> empty($post['sd_customer']) ? 0 : $post['sd_customer'],
			'sd_method'		=> $post['method'],
			'sd_info'		=> json_encode($payload, JSON_UNESCAPED_SLASHES),
		];
		$this->TableName('store_discount')
			->InsertOrUpdate([ 'sd_type' => 'N', 'sd_key' => $this->id ], $data);
		$this->DbErr();
		return TRUE;
	}
	
	protected function _loadOrder() {
		// load the order
		$order_id = $_SESSION['order'] ?? FALSE;
		$data = NULL;
		if (is_object($this->model_order) && $order_id) {
			$this->model_order->OrderId($order_id);
			$data = $this->model_order->AsHandleBars(TRUE);
		}
		return [ $order_id, $data ];
	}
	
	protected function getSelectionTotal($items) {
		$total = 0;
		foreach($items as $item) {
			if ($this->sd_catalog == $item['category'] || !$this->sd_catalog) {
				$total += $item['sell'] * $item['quantity'];
			}
		}
		return $total;
	}
	
	public function ValidateCoupon($coupon) {
		// load the order
		[ $order_id, $data ] = $this->_loadOrder();
		if (!$order_id) {
			return [
				'result' => -9997,
				'field' => [
					'so_coupon' => [
						'message' => 'Please visit checkout page before setting coupon code',
					],
				],
			];
		}

		// load the coupon
		$this->LoadCoupon($coupon);
		if ($coupon != '') {
			
			if (!($this->Type() == 'N')) {					// invalid coupon code
				return [
					'result' => -9997,
					'field' => [
						'so_coupon' => [
							'message' => 'Unknown coupon code',
						],
					],
				];
			}
			// coupon expired?
			$date_ok = TRUE;
			$date = $this->values['dt_start'] ?? '';
			if ($date != '') {
				$dt = strtotime($date . ' Z');
				if (time() < $dt) $date_ok = FALSE;
			}
			$date = $this->values['dt_end'] ?? '';
			if ($date != '') {
				$dt = strtotime($date . ' Z') + 86399;
				if (time() > $dt) $date_ok = FALSE;
			}
			if (!$date_ok) {
				return [
					'result' => -9997,
					'field' => [
						'so_coupon' => [
							'message' => 'This coupon has expired',
						],
					],
				];
			}
			
			// make sure the coupon conditions are met: minimum and category
			$total = $this->getSelectionTotal($data['cart']);
			
			if (!$total) {
				return [
					'result' => -9997,
					'field' => [
						'so_coupon' => [
							'message' => 'This coupon cannot be applied to any items in your cart',
						],
					],
				];
			}
			$minimum = $this->values['minimum'] ?? 0;
			if ($total < $minimum) {
				return [
					'result' => -9997,
					'field' => [
						'so_coupon' => [
							'message' => 'Minimum order value has not been reached for this coupon',
						],
					],
				];
			}
		}

		// add coupon (or empty coupon) to order 
		$xcoupon = $coupon ? $this->Id() : 0;
		$this->TableName('store_order')
			->InsertOrUpdate([
				'so_order' 		=> $order_id,
			],[
				'so_coupon' 	=> $xcoupon,
			]
		);
		
		// clear the order so that it will be reloaded 
		if (is_object($this->model_order)) {
			$this->model_order
				->Clear()
				->OrderId($order_id)
				->Recalculate();
		}

		// close popup and redisplay pricing
		return [
			'result' => 0,
			'order_id' => $order_id,
			'field' => [
				'so_coupon' => [
					'message' => '',
				],
			],
		];
	}
	
	public function asPopup() {
		// load the order
		$order_id = $_SESSION['order'] ?? FALSE;
		if ($order_id) {
			$sql = <<<'BLOCK'
select sd_coupon from `store_order`
  left join `store_discount` on so_coupon=sd_key and sd_type='N'
BLOCK;
			$row = $this->Query($sql)
						->Where('so_order=%d', $order_id)
						->First();
			$value = $row->sd_coupon ?? '';
		} else {
			$value = '';
		}
		
		return [
			'popup' => [
				'title'		=> 'Enter Coupon',
				'template'	=> 'storediscount_popup',
				'modal'		=> TRUE,
				'width'		=> 400,
				'so_coupon' => $value,
				'actions'	=> [
					[
						'data-action' 	=> 'cancel',
						'text'			=> 'Close',
					],					[
						'data-action' 	=> 'submit',
						'text'			=> 'Apply Coupon',
					],

				],
			],
		];
	}
	
	public function RecalculateOrder($order_object, $coupon_id) {
	
		$cart = $order_object->AsHandleBars_Items();
		if ($coupon_id) {
			$this->Type('N')
					->Id($coupon_id);
			$total = $this->getSelectionTotal($cart);
			$minimum = $this->values['minimum'] ?? 0;
			if ($total >= $minimum) {

				// calculate the discount amount
				$saving = $this->Method() == 'P' ? $total * ($this->values['amount'] / 100) : $this->values['amount'];
				if ($saving > $total) $saving = $total;
				
				// add it to the result
				$order_object->Extras('coupon',  [ 
					'desc'	=> 'Coupon "' . $this->Coupon() . '"',  
					'amt'	=> -$saving, 
					'tax'	=> 0 ,
					'coupon'=> $this->AsHandlebars(FALSE),		// this is the coupon at time of purchase.
				]);
				return;
			}
		}
		$order_object->Extras('coupon', NULL);
	}

}

Youez - 2016 - github.com/yon3zu
LinuXploit