1: <?php
2: /*
3:
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * or OpenGPL v3 license (GNU Public License V3.0)
9: * that is bundled with this package in the file LICENSE.txt.
10: * It is also available through the world-wide-web at this URL:
11: * http://opensource.org/licenses/osl-3.0.php
12: * or
13: * http://www.gnu.org/licenses/gpl-3.0.txt
14: * If you did not receive a copy of the license and are unable to
15: * obtain it through the world-wide-web, please send an email
16: * to info@e-abi.ee so we can send you a copy immediately.
17: *
18: * DISCLAIMER
19: *
20: * Do not edit or add to this file if you wish to upgrade this module to newer
21: * versions in the future.
22: *
23: * @category Eabi
24: * @package Eabi_Dpd
25: * @copyright Copyright (c) 2014 Aktsiamaailm LLC (http://en.e-abi.ee/)
26: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
27: * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU Public License V3.0
28: * @author Matis Halmann
29: *
30:
31: */
32:
33: /**
34: * <p>Represents one entry from eabi_postoffice table.</p>
35: * <p>Represents one parcel terminal, which can be selected as preferred shipping location for end user.</p>
36: * <p>Structure:</p>
37: * <ul>
38: <li><b>id</b> - unique auto incement id for entry</li>
39: <li><b>remote_module_id</b> - refers to eabi_carriermodule.id</li>
40: <li><b>remote_module_name</b> - Magento carrier code, also refers to eabi_carriermodule.carrier_code</li>
41: <li><b>remote_place_id</b> - Parcel terminal numeric ID, which is provided by remote server.</li>
42: <li><b>remote_servicing_place_id</b> - not used</li>
43: <li><b>name</b> - human readable remote parcel terminal short name</li>
44: <li><b>city</b> - city where remote parcel terminal is located (optional)</li>
45: <li><b>county</b> - region or county where remote parcel terminal is located (optional)</li>
46: <li><b>zip_code</b> - zip code for the remote parcel terminal (optional)</li>
47: <li><b>country</b> - ISO-3166 country code for the remote parcel terminal</li>
48: <li><b>description</b> - Extra human readable information can be entered here like address, opening times, etc...</li>
49: <li><b>group_id</b> - unique id that represents city/county combination group. Module auto generates this id for you.</li>
50: <li><b>group_name</b> - merged city/county if any of those is set. Otherwise left empty</li>
51: <li><b>group_sort</b> - higher the number, the more important group is and thus parcel terminal belonging to more important group is displayed before the others.</li>
52: <li><b>local_carrier_id</b> - not used</li>
53: <li><b>created_time</b> - time, when this parcel terminal entry was created</li>
54: <li><b>update_time</b> - time, when this parcel terminal was last updated</li>
55: <li><b>cached_attributes</b> - not used, but can store data in serialized form</li>
56: </ul>
57: * @author matishalmann
58: */
59: class Eabi_Postoffice_Model_Office extends Mage_Core_Model_Abstract {
60: //put your code here
61:
62: public function _construct() {
63: parent::_construct();
64: $this->_init('eabi_postoffice/office');
65:
66: }
67:
68: public function fromOfficeElement(array $officeElement, $remoteModuleId = false) {
69: $remoteModuleId = (int)$remoteModuleId;
70: if ($remoteModuleId > 0) {
71: $remoteModule = Mage::getModel('eabi_postoffice/carriermodule')->load($remoteModuleId);
72: if (!is_object($remoteModule) || $remoteModule->getId() <= 0) {
73: throw new Exception('Carrier module could not be detected for this Office model');
74: }
75: $this->setData('remote_module_id', $remoteModule->getId());
76: $this->setData('remote_module_name', $remoteModule->getCarrierCode());
77: } else {
78: if ($this->getData('remote_module_id') == '' || $this->getData('remote_module_name') == '' ) {
79: throw new Exception('Remote module ID and Remote Module Name have to be defined');
80: }
81: }
82:
83: //start setting the data
84: //mandatory
85: $this->setData('remote_place_id', $officeElement['place_id']);
86: $this->setData('name', $officeElement['name']);
87:
88: if (isset($officeElement['servicing_place_id'])) {
89: $this->setData('remote_servicing_place_id', $officeElement['servicing_place_id']);
90: }
91: if (isset($officeElement['city'])) {
92: $this->setData('city', $officeElement['city']);
93: }
94: if (isset($officeElement['county'])) {
95: $this->setData('county', $officeElement['county']);
96: }
97: if (isset($officeElement['zip'])) {
98: $this->setData('zip_code', $officeElement['zip']);
99: }
100: if (isset($officeElement['country'])) {
101: $this->setData('country', $officeElement['country']);
102: }
103: if (isset($officeElement['description'])) {
104: $this->setData('description', $officeElement['description']);
105: }
106: if (isset($officeElement['group_id']) && isset($officeElement['group_name'])) {
107: $this->setData('group_id', $officeElement['group_id']);
108: $this->setData('group_name', $officeElement['group_name']);
109: if (isset($officeElement['group_sort'])) {
110: $this->setData('group_sort', $officeElement['group_sort']);
111: }
112: }
113:
114: if (isset($officeElement['extra']) && is_array($officeElement['extra'])) {
115: $this->setData('cached_attributes', serialize($officeElement['extra']));
116: }
117:
118:
119: return $this;
120:
121: }
122:
123: public function loadByCodeAndRemoteId($code, $remoteId) {
124: return $this->getCollection()
125: ->addFieldToFilter('remote_module_name', $code)
126: ->addFieldToFilter('remote_place_id', $remoteId)
127: ->getFirstItem();
128: }
129:
130:
131: protected function _beforeSave() {
132: if (method_exists($this, 'isObjectNew') && $this->isObjectNew()) {
133: $this->setCreatedTime(self::$_date->get('yyyy-MM-dd HH:mm:ss'));
134: $this->setUpdateTime(self::$_date->get('yyyy-MM-dd HH:mm:ss'));
135:
136: } else {
137: $this->setUpdateTime(self::$_date->get('yyyy-MM-dd HH:mm:ss'));
138: }
139: if ($this->getCreatedTime() == '') {
140: $this->setCreatedTime(self::$_date->get('yyyy-MM-dd HH:mm:ss'));
141: }
142: return parent::_beforeSave();
143: }
144:
145: public static function eabi_init() {
146: self::$_date = new Zend_Date(time(), Zend_Date::TIMESTAMP);
147: }
148:
149: protected static $_date;
150:
151: }
152: Eabi_Postoffice_Model_Office::eabi_init();
153: