Browse code

Initial commit

Benjamin Roth authored on07/09/2015 15:02:25
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,133 @@
1
+<?php
2
+
3
+/**
4
+ * eSales Media oxVoucher for Contao Open Source CMS
5
+ *
6
+ * Copyright (c) 2015 eSales Media
7
+ *
8
+ * @author  Benjamin Roth [benjamin@esales-media.de]
9
+ * @license proprietary
10
+ */
11
+
12
+namespace eSM_oxVoucher;
13
+
14
+class TicketHandler extends Voucher
15
+{
16
+
17
+	/**
18
+	 * Oxid Database Object
19
+	 * @var \Database|null
20
+	 */
21
+	protected $oxDB = null;
22
+
23
+	/**
24
+	 * ticket database result array
25
+	 * @var Array
26
+	 */
27
+	protected $arrTicket = array();
28
+
29
+	protected $blnIsVariant = null;
30
+
31
+	/**
32
+	 * Import Oxid database instance
33
+	 */
34
+	public function __construct($strOxId)
35
+	{
36
+		$this->oxDB = Voucher::getOxDbInstance();
37
+
38
+		$this->load($strOxId);
39
+
40
+		parent::__construct();
41
+	}
42
+
43
+	public function __get($name)
44
+	{
45
+		if ($this->arrTicket[strtoupper($name)])
46
+		{
47
+			return $this->arrTicket[strtoupper($name)];
48
+		}
49
+		return null;
50
+	}
51
+
52
+	public function __set($name, $value)
53
+	{
54
+		if ($this->arrTicket[strtoupper($name)])
55
+		{
56
+			$this->arrTicket[strtoupper($name)] = $value;
57
+		}
58
+	}
59
+
60
+	public function getData()
61
+	{
62
+		return $this->arrTicket;
63
+	}
64
+
65
+	protected function load($strOxId)
66
+	{
67
+		$objSQL = $this->oxDB->prepare("SELECT a.OXID, a.OXPARENTID, a.OXTITLE, pa.OXTITLE AS 'OXPARENTTITLE', pa.OXSHORTDESC AS 'OXPARENTSHORTDESC', a.OXSHORTDESC, a.OXACTIVE, UNIX_TIMESTAMP(a.OXACTIVEFROM) AS 'OXACTIVEFROM', UNIX_TIMESTAMP(a.OXACTIVETO) AS 'OXACTIVETO', UNIX_TIMESTAMP(a.OXESSELLUNTIL) AS 'OXESSELLUNTIL', a.OXPRICE, a.OXSTOCK, a.OXVARSTOCK, a.OXVARNAME, a.OXVARSELECT FROM oxv_oxarticles_de a LEFT JOIN oxv_oxarticles_de AS pa ON pa.OXID = a.OXPARENTID AND a.OXPARENTID != ''  WHERE a.OXID = ?");
68
+
69
+		$objResult = $objSQL->execute($strOxId);
70
+
71
+		if ($objResult->numRows)
72
+		{
73
+			$this->arrTicket = $objResult->row();
74
+		}
75
+	}
76
+
77
+	protected function refresh()
78
+	{
79
+		$this->load($this->oxid);
80
+	}
81
+
82
+
83
+	public function isVariant()
84
+	{
85
+		if (is_null($this->blnIsVariant))
86
+		{
87
+			$this->blnIsVariant = (bool) ( $this->oxparentid ? $this->oxparentid : false );
88
+		}
89
+
90
+		return $this->blnIsVariant;
91
+	}
92
+
93
+	public function isBuyable()
94
+	{
95
+		if ($this->isActiveTime() && $this->oxactive && $this->oxstock > 0)
96
+		{
97
+			return true;
98
+		}
99
+		return false;
100
+	}
101
+
102
+	public function isActiveTime()
103
+	{
104
+		$Today = new \Date();
105
+		if ((is_null($this->oxactivefrom) || $this->oxactivefrom <= $Today->tstamp) && (is_null($this->oxactiveto) || $this->oxactiveto >= $Today->tstamp))
106
+		{
107
+			return true;
108
+		}
109
+		return false;
110
+	}
111
+
112
+
113
+	public function reduceStock($iVal)
114
+	{
115
+		$iVal = (int) $iVal;
116
+		if ($iVal <= $this->oxstock && $iVal > 0)
117
+		{
118
+			$objSQL = $this->oxDB->prepare("UPDATE oxarticles SET OXSTOCK = OXSTOCK - $iVal WHERE OXID = ?");
119
+			$objSQL->execute($this->oxid);
120
+
121
+			if ($this->isVariant())
122
+			{
123
+				$objSQL = $this->oxDB->prepare("UPDATE oxarticles p SET p.OXVARSTOCK = (SELECT SUM(v.OXSTOCK) FROM oxarticles v WHERE OXPARENTID = ?), p.OXSOLDAMOUNT = p.OXSOLDAMOUNT + $iVal WHERE p.OXID = ? AND p.OXPARENTID = ''");
124
+				$objSQL->execute($this->oxparentid,$this->oxparentid);
125
+			} else {
126
+				$objSQL = $this->oxDB->prepare("UPDATE oxarticles SET OXSOLDAMOUNT = OXSOLDAMOUNT + $iVal WHERE OXID = ?");
127
+				$objSQL->execute($this->oxid);
128
+			}
129
+
130
+			$this->refresh();
131
+		}
132
+	}
133
+}
0 134
\ No newline at end of file