oxvoucherserie.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxVoucherSerie extends oxBase
00009 {
00010 
00016     protected $_oGroups = null;
00017 
00021     protected $_sClassName = 'oxvoucherserie';
00022 
00026     public function __construct()
00027     {
00028         parent::__construct();
00029         $this->init('oxvoucherseries');
00030     }
00031 
00039     public function delete($sOxId = null)
00040     {
00041         if (!$sOxId) {
00042             $sOxId = $this->getId();
00043         }
00044 
00045 
00046         $this->unsetDiscountRelations();
00047         $this->unsetUserGroups();
00048         $this->deleteVoucherList();
00049 
00050         return parent::delete($sOxId);
00051     }
00052 
00058     public function setUserGroups()
00059     {
00060         if ($this->_oGroups === null) {
00061             $this->_oGroups = oxNew('oxlist');
00062             $this->_oGroups->init('oxgroups');
00063             $sViewName = getViewName("oxgroups");
00064             $sSelect = "select gr.* from {$sViewName} as gr, oxobject2group as o2g where
00065                          o2g.oxobjectid = " . oxDb::getDb()->quote($this->getId()) . " and gr.oxid = o2g.oxgroupsid ";
00066             $this->_oGroups->selectString($sSelect);
00067         }
00068 
00069         return $this->_oGroups;
00070     }
00071 
00075     public function unsetUserGroups()
00076     {
00077         $oDb = oxDb::getDb();
00078         $sDelete = 'delete from oxobject2group where oxobjectid = ' . $oDb->quote($this->getId());
00079         $oDb->execute($sDelete);
00080     }
00081 
00085     public function unsetDiscountRelations()
00086     {
00087         $oDb = oxDb::getDb();
00088         $sDelete = 'delete from oxobject2discount where oxobject2discount.oxdiscountid = ' . $oDb->quote($this->getId());
00089         $oDb->execute($sDelete);
00090     }
00091 
00097     public function getVoucherList()
00098     {
00099         $oVoucherList = oxNew('oxvoucherlist');
00100         $sSelect = 'select * from oxvouchers where oxvoucherserieid = ' . oxDb::getDb()->quote($this->getId());
00101         $oVoucherList->selectString($sSelect);
00102 
00103         return $oVoucherList;
00104     }
00105 
00109     public function deleteVoucherList()
00110     {
00111         $oDb = oxDb::getDb();
00112         $sDelete = 'delete from oxvouchers where oxvoucherserieid = ' . $oDb->quote($this->getId());
00113         $oDb->execute($sDelete);
00114     }
00115 
00121     public function countVouchers()
00122     {
00123         $aStatus = array();
00124 
00125         $oDb = oxDb::getDb();
00126         $sQuery = 'select count(*) as total from oxvouchers where oxvoucherserieid = ' . $oDb->quote($this->getId());
00127         $aStatus['total'] = $oDb->getOne($sQuery);
00128 
00129         $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))';
00130         $aStatus['used'] = $oDb->getOne($sQuery);
00131 
00132         $aStatus['available'] = $aStatus['total'] - $aStatus['used'];
00133 
00134         return $aStatus;
00135     }
00136 
00144     public function getVoucherStatusByDatetime($sNow = null)
00145     {
00146         //return content
00147         $iActive = 1;
00148         $iInactive = 0;
00149 
00150         $oUtilsDate = oxRegistry::get("oxUtilsDate");
00151         //current object datetime
00152         $sBeginDate = $this->oxvoucherseries__oxbegindate->value;
00153         $sEndDate = $this->oxvoucherseries__oxenddate->value;
00154 
00155         //If nothing pass, use current server time
00156         if ($sNow == null) {
00157             $sNow = date('Y-m-d H:i:s', $oUtilsDate->getTime());
00158         }
00159 
00160         //Check for active status.
00161         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
00162             ($sBeginDate == '0000-00-00 00:00:00' && $sNow <= $sEndDate) || //check for end date without start date
00163             ($sBeginDate <= $sNow && $sEndDate == '0000-00-00 00:00:00') || //check for start date without end date
00164             ($sBeginDate <= $sNow && $sNow <= $sEndDate)
00165         ) { //check for both start date and end date.
00166             return $iActive;
00167         }
00168 
00169         //If active status code was reached, return as inactive
00170         return $iInactive;
00171     }
00172 }