OXID eShop CE  4.10.0
 All Classes Namespaces Files Functions Variables Pages
oxvoucherserie.php
Go to the documentation of this file.
1 <?php
2 
8 class oxVoucherSerie extends oxBase
9 {
10 
16  protected $_oGroups = null;
17 
21  protected $_sClassName = 'oxvoucherserie';
22 
26  public function __construct()
27  {
29  $this->init('oxvoucherseries');
30  }
31 
39  public function delete($sOxId = null)
40  {
41  if (!$sOxId) {
42  $sOxId = $this->getId();
43  }
44 
45 
46  $this->unsetDiscountRelations();
47  $this->unsetUserGroups();
48  $this->deleteVoucherList();
49 
50  return parent::delete($sOxId);
51  }
52 
58  public function setUserGroups()
59  {
60  if ($this->_oGroups === null) {
61  $this->_oGroups = oxNew('oxlist');
62  $this->_oGroups->init('oxgroups');
63  $sViewName = getViewName("oxgroups");
64  $sSelect = "select gr.* from {$sViewName} as gr, oxobject2group as o2g where
65  o2g.oxobjectid = " . oxDb::getDb()->quote($this->getId()) . " and gr.oxid = o2g.oxgroupsid ";
66  $this->_oGroups->selectString($sSelect);
67  }
68 
69  return $this->_oGroups;
70  }
71 
75  public function unsetUserGroups()
76  {
77  $oDb = oxDb::getDb();
78  $sDelete = 'delete from oxobject2group where oxobjectid = ' . $oDb->quote($this->getId());
79  $oDb->execute($sDelete);
80  }
81 
85  public function unsetDiscountRelations()
86  {
87  $oDb = oxDb::getDb();
88  $sDelete = 'delete from oxobject2discount where oxobject2discount.oxdiscountid = ' . $oDb->quote($this->getId());
89  $oDb->execute($sDelete);
90  }
91 
97  public function getVoucherList()
98  {
99  $oVoucherList = oxNew('oxvoucherlist');
100  $sSelect = 'select * from oxvouchers where oxvoucherserieid = ' . oxDb::getDb()->quote($this->getId());
101  $oVoucherList->selectString($sSelect);
102 
103  return $oVoucherList;
104  }
105 
109  public function deleteVoucherList()
110  {
111  $oDb = oxDb::getDb();
112  $sDelete = 'delete from oxvouchers where oxvoucherserieid = ' . $oDb->quote($this->getId());
113  $oDb->execute($sDelete);
114  }
115 
121  public function countVouchers()
122  {
123  $aStatus = array();
124 
125  $oDb = oxDb::getDb();
126  $sQuery = 'select count(*) as total from oxvouchers where oxvoucherserieid = ' . $oDb->quote($this->getId());
127  $aStatus['total'] = $oDb->getOne($sQuery);
128 
129  $sQuery = 'select count(*) as used from oxvouchers where oxvoucherserieid = ' . $oDb->quote($this->getId()) . ' and ((oxorderid is not NULL and oxorderid != "") or (oxdateused is not NULL and oxdateused != 0))';
130  $aStatus['used'] = $oDb->getOne($sQuery);
131 
132  $aStatus['available'] = $aStatus['total'] - $aStatus['used'];
133 
134  return $aStatus;
135  }
136 
144  public function getVoucherStatusByDatetime($sNow = null)
145  {
146  //return content
147  $iActive = 1;
148  $iInactive = 0;
149 
150  $oUtilsDate = oxRegistry::get("oxUtilsDate");
151  //current object datetime
152  $sBeginDate = $this->oxvoucherseries__oxbegindate->value;
153  $sEndDate = $this->oxvoucherseries__oxenddate->value;
154 
155  //If nothing pass, use current server time
156  if ($sNow == null) {
157  $sNow = date('Y-m-d H:i:s', $oUtilsDate->getTime());
158  }
159 
160  //Check for active status.
161  if (($sBeginDate == '0000-00-00 00:00:00' && $sEndDate == '0000-00-00 00:00:00') || //If both dates are empty => treat it as always active
162  ($sBeginDate == '0000-00-00 00:00:00' && $sNow <= $sEndDate) || //check for end date without start date
163  ($sBeginDate <= $sNow && $sEndDate == '0000-00-00 00:00:00') || //check for start date without end date
164  ($sBeginDate <= $sNow && $sNow <= $sEndDate)
165  ) { //check for both start date and end date.
166  return $iActive;
167  }
168 
169  //If active status code was reached, return as inactive
170  return $iInactive;
171  }
172 }