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: class Eabi_Postoffice_Adminhtml_PostofficeController extends Mage_Adminhtml_Controller_Action {
40:
41: protected function _initAction() {
42: return $this;
43: }
44:
45: protected function _construct() {
46:
47: }
48:
49: 50: 51: 52: 53:
54: public function rebuildAction() {
55: $result = $this->_rebuild();
56: echo json_encode($result);
57: die();
58: }
59:
60: protected function _rebuild() {
61: $carrierCode = $this->getRequest()->getParam('carrier_code', '');
62: if ($carrierCode == '') {
63: return array('error' => Mage::helper('eabi_postoffice')->__('Invalid Carrier code'));
64: }
65: $carrierModule = Mage::getModel('eabi_postoffice/carriermodule')->load($carrierCode, 'carrier_code');
66: if (!is_object($carrierModule) ||$carrierModule->getId() <= 0) {
67: return array('error' => Mage::helper('eabi_postoffice')->__('Invalid Carrier code'));
68: }
69: try {
70: $carrierModule->updateCarrierData(true);
71: } catch (Exception $e) {
72: return array('error' => $e->getMessage());
73: }
74: return array('success' => true);
75:
76: }
77:
78: 79: 80: 81: 82: 83:
84: public function addresscardpdfAction() {
85: $orderId = (int)$this->getRequest()->getParam('order_id', 0);
86: if ($orderId <= 0) {
87: return;
88: }
89: $order = Mage::getModel('sales/order')->load($orderId);
90: if (!$order || $order->getId() <= 0) {
91: return;
92: }
93: $incrementId = $order->getIncrementId();
94: $res = Mage::helper('eabi_postoffice')->getBarcodePdf($incrementId);
95: if ($res !== false) {
96: header('Content-Type: application/pdf');
97: header('Content-Disposition: attachment; filename="addresscard-' . $incrementId . '.pdf"');
98: echo $res;
99: } else {
100: echo 'No barcode available';
101: }
102: }
103:
104: 105: 106: 107: 108: 109: 110:
111: public function autosendAction() {
112: $result = $this->_autoSend();
113: echo json_encode($result);
114: die();
115: }
116:
117: protected function _autoSend() {
118:
119: $orderId = (int)$this->getRequest()->getParam('order_id', 0);
120: if ($orderId <= 0) {
121: return array('error' => Mage::helper('eabi_postoffice')->__('Invalid Order ID'));
122: }
123: $order = Mage::getModel('sales/order')->load($orderId);
124: if (!$order || $order->getId() <= 0) {
125: return array('error' => Mage::helper('eabi_postoffice')->__('Invalid Order ID'));
126: }
127:
128:
129: $shippingMethod = $order->getShippingMethod();
130: $paymentMethod = $order->getPayment();
131:
132:
133: $shippingCarrierCode = substr($shippingMethod, 0, strpos($shippingMethod, '_'));
134: $shippingMethodModel = Mage::getModel('shipping/shipping')->getCarrierByCode($shippingCarrierCode, $order->getStoreId());
135:
136: if (!($shippingMethodModel instanceof Eabi_Postoffice_Model_Carrier_Abstract)){
137: return array('error' => Mage::helper('eabi_postoffice')->__('This carrier is not subclass of Eabi_Postoffice_Model_Carrier_Abstract'));
138: }
139: $shippingMethodModel->setStoreId($order->getStoreId());
140:
141:
142: if (!$shippingMethodModel->isAutoSendAvailable()) {
143: return array('error' => Mage::helper('eabi_postoffice')->__('Automatic data sending is not available for the selected carrier'));
144: }
145:
146: if (!$this->_getOfficeHelper()->canSendData($order)) {
147: return array('error' => Mage::helper('eabi_postoffice')->__('This order has not yet been fully paid'));
148: }
149:
150:
151: if (($order->isCanceled() || $order->getIsVirtual())) {
152: return array('error' => Mage::helper('eabi_postoffice')->__('This order cannot be shipped'));
153: }
154:
155:
156:
157: Mage::helper('eabi_postoffice')->sendManualOrderData($order->getIncrementId(), $shippingMethodModel->getConfigData('senddata_event'));
158:
159:
160:
161: return array('success' => true);
162:
163: }
164:
165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175:
176: public function officeAction() {
177: try {
178: if ($this->getRequest()->isPost()) {
179: $this->getResponse()->setHeader('Content-type', 'application/json');
180: $jsonResponse = array(
181: 'html' => false,
182: 'comment' => false,
183: );
184:
185: $post = $this->getRequest()->getPost();
186: $storeId = (int)$this->getRequest()->getParam('store_id', 0);
187: if ($storeId <= 0) {
188: throw new Exception('Store ID must be supplied');
189: }
190: $url = $this->getUrl('eabi_postoffice/adminhtml_postoffice/office', array('store_id' => $storeId, '_secure' => true));
191: $addressId = $post['address_id'];
192: $carrierCode = $post['carrier_code'];
193: $carrierId = $post['carrier_id'];
194: $divId = $post['div_id'];
195: $groupId = isset($post['group_id']) ? ((int) $post['group_id']) : 0;
196: $placeId = isset($post['place_id']) ? ((int) $post['place_id']) : 0;
197: $shippingModel = Mage::getModel('shipping/shipping')->getCarrierByCode($carrierCode, $storeId);
198:
199:
200: $shippingModel->setStoreId($storeId);
201:
202: if (!$shippingModel->isAjaxInsertAllowed($addressId)) {
203: throw new Exception('Invalid Shipping method');
204: }
205: if (!($shippingModel instanceof Eabi_Postoffice_Model_Carrier_Abstract)) {
206: throw new Exception('Invalid Shipping model');
207: }
208:
209: if ($placeId > 0) {
210: $place = $shippingModel->getTerminal($placeId);
211: if ($place) {
212: $shippingModel->setOfficeToSession($addressId, $place);
213: $jsonResponse['html'] = true;
214: $terminalComment = $shippingModel->getTerminalComment($place);
215: $isError = $terminalComment->getIsError();
216: if ($isError) {
217: $jsonResponse['html'] = false;
218: $jsonResponse['comment'] = $terminalComment->getComment();
219: }
220:
221:
222: $this->getResponse()->setBody(json_encode($jsonResponse));
223: return;
224: } else {
225: $jsonResponse['html'] = false;
226: $this->getResponse()->setBody(json_encode($jsonResponse));
227: return;
228: }
229: }
230:
231: $groups = $shippingModel->getGroups($addressId);
232: $html = '';
233:
234: if ($groups) {
235: $groupSelectWidth = (int)$shippingModel->getConfigData('group_width');
236: $style = '';
237: if ($groupSelectWidth > 0) {
238: $style = ' style="width:'.$groupSelectWidth.'px"';
239: }
240: $html .= '<select onclick="return false;" '.$style.' name="' . $carrierCode . '_select_group" onchange="new Ajax.Request(\'' . $url . '\',{method:\'post\',parameters:{carrier_id:\'' . $carrierId . '\',carrier_code:\'' . $carrierCode . '\',div_id:\'' . $divId . '\',address_id:\'' . $addressId . '\',group_id: $(this).getValue()},onSuccess:function(a){$(\'' . $divId . '\').update(a.responseJSON.html)}});">';
241: $html .= '<option value="">';
242: $html .= htmlspecialchars(Mage::helper('eabi_postoffice')->__('-- select --'));
243: $html .= '</option>';
244:
245: foreach ($groups as $group) {
246: $html .= '<option value="' . $group->getGroupId() . '"';
247: if ($groupId > 0 && $groupId == $group->getGroupId()) {
248: $html .= ' selected="selected"';
249: }
250: $html .= '>';
251: $html .= $shippingModel->getGroupTitle($group);
252: $html .= '</option>';
253: }
254: $html .= '</select>';
255: }
256:
257:
258: if ($groupId > 0 || $groups === false) {
259: $terminals = array();
260: if ($groups !== false) {
261: $terminals = $shippingModel->getTerminals($groupId, $addressId);
262: } else {
263: $terminals = $shippingModel->getTerminals(null, $addressId);
264: }
265: $officeSelectWidth = (int)$shippingModel->getConfigData('office_width');
266: $style = '';
267: if ($officeSelectWidth > 0) {
268: $style = ' style="width:'.$officeSelectWidth.'px"';
269: }
270: $html .= '<select onclick="return false;" '.$style.' name="' . $carrierCode . '_select_office" onchange="var sel = $(this); new Ajax.Request(\'' . $url . '\',{method:\'post\',parameters:{carrier_id:\'' . $carrierId . '\',carrier_code:\'' . $carrierCode . '\',div_id:\'' . $divId . '\',address_id:\'' . $addressId . '\',place_id: sel.getValue()},onSuccess:function(a){ if (a.responseJSON.html === true) { if (typeof a.responseJSON.comment === \'string\') { sel.next().update(a.responseJSON.comment); sel.next().removeClassName(\'eabi_postoffice_comment_error\'); sel.next().show(); $$(\'.eabi_carrier\').each(function() { $(this).addClassName(\'over\'); });} else { sel.next().hide(); } $(\'' . $carrierId . '\').writeAttribute(\'value\', \'' . $carrierCode . '_' . $carrierCode . '_\' + sel.getValue()); $(\'' . $carrierId . '\').click(); } else if (a.responseJSON.html === false) { if (typeof a.responseJSON.comment === \'string\') { sel.next().update(a.responseJSON.comment); sel.next().addClassName(\'eabi_postoffice_comment_error\'); sel.next().show(); } else { sel.next().hide(); } } else { sel.next().hide(); }}});">';
271: $html .= '<option value="">';
272: $html .= htmlspecialchars(Mage::helper('eabi_postoffice')->__('-- select --'));
273: $html .= '</option>';
274:
275: $optionsHtml = '';
276: $previousGroup = false;
277: $optGroupHtml = '';
278: $groupCount = 0;
279:
280: foreach ($terminals as $terminal) {
281: if ($shippingModel->getGroupTitle($terminal) != $previousGroup && !$shippingModel->getConfigData('disable_group_titles')) {
282: if ($previousGroup != false) {
283: $optionsHtml .= '</optgroup>';
284: $optionsHtml .= '<optgroup label="'.$shippingModel->getGroupTitle($terminal).'">';
285: } else {
286: $optGroupHtml .= '<optgroup label="'.$shippingModel->getGroupTitle($terminal).'">';
287: }
288: $groupCount++;
289: }
290: $optionsHtml .= '<option value="' . $terminal->getRemotePlaceId() . '"';
291: if (false) {
292: $optionsHtml .= ' selected="selected"';
293: }
294: $optionsHtml .= '>';
295: $optionsHtml .= $shippingModel->getTerminalTitle($terminal);
296: $optionsHtml .= '</option>';
297:
298: $previousGroup = $shippingModel->getGroupTitle($terminal);
299: }
300: if ($groupCount > 1) {
301: $html .= $optGroupHtml . $optionsHtml . '</optgroup>';
302: } else {
303: $html .= $optionsHtml;
304: }
305:
306: $html .= '</select>';
307:
308:
309: }
310: $html .= '<span class="eabi_postoffice_comment" style="display:none;"></span>';
311:
312:
313: $jsonResponse['html'] = $html;
314: $this->getResponse()->setBody(json_encode($jsonResponse));
315: } else {
316: throw new Exception('Invalid request method');
317: }
318: } catch (Exception $e) {
319: $this->getResponse()->setHeader('HTTP/1.1', '500 Internal error');
320: $this->getResponse()->setHeader('Status', '500 Internal error');
321: throw $e;
322: }
323: return;
324: }
325:
326:
327: 328: 329: 330:
331: public function removeAction() {
332: $result = array('status' => 'failed');
333: if ($this->getRequest()->isPost() && $this->getRequest()->getPost('remove') == 'true') {
334: $dirName = Mage::getBaseDir('code').'/local/Eabi/Postoffice';
335: if (is_dir($dirName) && file_exists($dirName.'/etc/config.xml')) {
336: $directory = new Varien_Io_File();
337: $deleteResult = $directory->rmdir($dirName, true);
338: if ($deleteResult) {
339: $result['status'] = 'success';
340: }
341: }
342:
343: }
344: $this->getResponse()->setRawHeader('Content-type: application/json');
345: $this->getResponse()->setBody(json_encode($result));
346: return;
347: }
348:
349: 350: 351: 352:
353: protected function _getEabi() {
354: return Mage::helper('eabi');
355: }
356:
357: 358: 359: 360:
361: protected function _getOfficeHelper() {
362: return Mage::helper('eabi_postoffice');
363: }
364:
365:
366: }
367:
368: