1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
33:
34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47:
48: class Eabi_DpdEE_Block_Order_Courier extends Mage_Core_Block_Template {
49: protected $_dateFormat = 'yyyy-MM-dd';
50: protected $_timeFormat = 'HHmm';
51: protected $_timeFormatNice = 'HH:mm';
52:
53: protected $_availableDates;
54: protected $_apiResult;
55:
56: public function _construct() {
57: $this->setTemplate('eabi_dpdee/order/courier.phtml');
58: parent::_construct();
59: }
60:
61: 62: 63: 64:
65: public function getHtmlId() {
66: return 'eabi_carrier_order_courier_box';
67: }
68:
69:
70:
71:
72:
73: 74: 75: 76:
77: public function getAvailableDates() {
78: if ($this->_availableDates) {
79: return $this->_availableDates;
80: }
81: $showOnlyDates = false;
82: $datesResult = $this->_getDpdHelper()->getApi($this->getStoreId(), $this->getCode())->getCourierCollectionTimes();
83: $this->_apiResult = $datesResult;
84: $dateObj = null;
85:
86: $resultString = '';
87:
88: $resultString .= '<select name="Po_Date" id="Po_date" onchange="updateDpdEeTimes(this);">';
89: $resultString .= '<option value="-">'.$this->_getDpdHelper()->__(' - select pickup date - ').'</option>';
90:
91: foreach ($datesResult['Po_date'] as $dateString) {
92: if (is_null($dateObj)) {
93: $dateObj = new Zend_Date($dateString, $this->_dateFormat);
94: }
95: $dateObj->set($dateString, $this->_dateFormat);
96: if ($dateObj->isToday() && !$showOnlyDates) {
97:
98: $resultString .= $this->_getDateInputLabel($this->_getDpdHelper()->__('Today'), $dateString);
99: } else if ($dateObj->isTomorrow() && !$showOnlyDates) {
100:
101: $resultString .= $this->_getDateInputLabel($this->_getDpdHelper()->__('Tomorrow'), $dateString);
102: } else {
103:
104: $resultString .= $this->_getDateInputLabel($dateString, $dateString);
105: }
106: }
107: $resultString .= '</select>';
108:
109:
110: $this->_availableDates = $resultString;
111: return $this->_availableDates;
112: }
113:
114:
115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126:
127: public function getAvailabeTimes() {
128: if (!$this->_apiResult) {
129: $this->getAvailableDates();
130: }
131: $availableTimes = array();
132: $timeDateObject = null;
133:
134: $availableTimes["-"] = $this->_getDpdHelper()->__('Select pickup date first');
135: foreach ($this->_apiResult['Po_date'] as $key => $dateString) {
136: $timeFrom = $this->_normalizeTime($this->_apiResult['Po_time_from'][$key]);
137: $timeTo = $this->_normalizeTime($this->_apiResult['Po_time_til'][$key]);
138: $timeWindow = $this->_normalizeTime($this->_apiResult['Minimal_time_window'][$key]);
139: $availableTimes[$dateString] = $this->_getTimeSelectMenu($timeFrom, $timeTo, $timeWindow);
140: }
141: return json_encode($availableTimes);
142: }
143:
144: 145: 146: 147: 148: 149:
150: protected function _normalizeTime($input) {
151: if (strlen($input) === 1) {
152: $input = '0'.$input .'00';
153: }
154: if (strlen($input) === 2) {
155: $input = $input .'00';
156: }
157: if (strlen($input) === 3) {
158: $input = '0'.$input;
159: }
160: $date = new Zend_Date($input, $this->_timeFormat);
161: $date->setTimezone('Etc/GMT+0');
162: return $date;
163: }
164:
165:
166: 167: 168: 169: 170: 171: 172: 173: 174:
175: protected function (Zend_Date $timeFrom, Zend_Date $timeTo, Zend_Date $timeWindow) {
176: $position = $timeFrom->get('H');
177: $result = '<select name="Po_Time" id="Po_Time">';
178: do {
179: $endTime = clone $timeFrom;
180: $endTime->add($timeWindow);
181: if ($timeTo->isEarlier($endTime)) {
182: $timeDisplay = htmlspecialchars(ltrim($timeFrom->get($this->_timeFormat), '0') . '-' . ltrim($timeTo->get($this->_timeFormat), '0'));
183: $timeDisplayNice = htmlspecialchars($timeFrom->get($this->_timeFormatNice) . '-' . $timeTo->get($this->_timeFormatNice));
184: } else {
185: $timeDisplay = htmlspecialchars(ltrim($timeFrom->get($this->_timeFormat), '0') . '-' . ltrim($endTime->get($this->_timeFormat), '0'));
186: $timeDisplayNice = htmlspecialchars($timeFrom->get($this->_timeFormatNice) . '-' . $endTime->get($this->_timeFormatNice));
187: }
188: $result .= '<option value="' . $timeDisplay . '">' . $timeDisplayNice . '</option>';
189: $timeFrom->setTimestamp($timeFrom->getTimestamp() + 60 * 60);
190: } while ($timeFrom->get(Zend_Date::HOUR_SHORT) + $timeWindow->get(Zend_Date::HOUR_SHORT) <= $timeTo->get(Zend_Date::HOUR_SHORT));
191: $result .= '</select>';
192: return $result;
193: }
194:
195: protected function _getDateInputLabel($label, $value) {
196: return '<option value="'. htmlspecialchars($value).'" >'. htmlspecialchars($label).'</option>';
197: }
198:
199: 200: 201: 202:
203: public function getCode() {
204: if (!$this->getData('code')) {
205: return 'eabidpdee';
206: }
207: return $this->getData('code');
208: }
209:
210: 211: 212: 213:
214: public function getEnvelopeQty() {
215: return '0';
216: }
217:
218:
219: 220: 221: 222:
223: public function getParcelQty() {
224: return '1';
225:
226: }
227:
228:
229: 230: 231: 232:
233: public function getPalletQty() {
234: return '0';
235:
236: }
237:
238: 239: 240: 241:
242: protected function _getDpdHelper() {
243: return Mage::helper('eabi_dpdee');
244: }
245:
246:
247: }
248: