Followthesun_Crm360 - Version 0.1.0

Version Notes

-

Download this release

Release Info

Developer Followthesun
Extension Followthesun_Crm360
Version 0.1.0
Comparing to
See all releases


Version 0.1.0

app/code/community/Followthesun/Crm360/Block/Adminhtml/Log.php ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Followthesun_Crm360_Block_Adminhtml_Log extends Mage_Adminhtml_Block_Widget_Grid_Container
3
+ {
4
+ public function __construct()
5
+ {
6
+ $this->_controller = 'adminhtml_log';
7
+ $this->_blockGroup = 'crm360';
8
+ $this->_headerText = Mage::helper('crm360')->__('Crm360 / Logs');
9
+ parent::__construct();
10
+ $this->_removeButton('add');
11
+ }
12
+ }
app/code/community/Followthesun/Crm360/Block/Adminhtml/Log/Grid.php ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Followthesun_Crm360_Block_Adminhtml_Log_Grid extends Mage_Adminhtml_Block_Widget_Grid
4
+ {
5
+ public function __construct()
6
+ {
7
+ parent::__construct();
8
+ $this->setId('LogGrid');
9
+ $this->setDefaultSort('log_id');
10
+ $this->setDefaultDir('DESC');
11
+ $this->setSaveParametersInSession(true);
12
+ }
13
+
14
+ protected function _prepareCollection()
15
+ {
16
+ $collection = Mage::getModel('crm360/log')->getCollection();
17
+ $this->setCollection($collection);
18
+ return parent::_prepareCollection();
19
+ }
20
+
21
+ protected function _prepareColumns()
22
+ {
23
+ $this->addColumn('log_id', array(
24
+ 'header' => Mage::helper('crm360')->__('ID'),
25
+ 'align' =>'right',
26
+ 'width' => '50px',
27
+ 'index' => 'log_id',
28
+ ));
29
+
30
+ $this->addColumn('type', array(
31
+ 'header' => Mage::helper('crm360')->__('Type'),
32
+ 'index' => 'type',
33
+ 'width' => '100px',
34
+ ));
35
+ $this->addColumn('status', array(
36
+ 'header' => Mage::helper('crm360')->__('Statut'),
37
+ 'index' => 'status',
38
+ ));
39
+
40
+ $this->addColumn('message', array(
41
+ 'header' => Mage::helper('crm360')->__('Message'),
42
+ 'index' => 'message',
43
+ ));
44
+
45
+ $this->addColumn('created_at', array(
46
+ 'header' => Mage::helper('crm360')->__('Date de création'),
47
+ 'index' => 'created_at',
48
+ 'width' => '150px',
49
+ ));
50
+
51
+ $this->addColumn('updated_at', array(
52
+ 'header' => Mage::helper('crm360')->__('Data de modification'),
53
+ 'index' => 'updated_at',
54
+ 'width' => '150px',
55
+ ));
56
+
57
+ return parent::_prepareColumns();
58
+ }
59
+
60
+ protected function _prepareMassaction()
61
+ {
62
+ $this->setMassactionIdField('log_id');
63
+ $this->getMassactionBlock()->setFormFieldName('log');
64
+
65
+ $this->getMassactionBlock()->addItem('delete', array(
66
+ 'label' => Mage::helper('crm360')->__('Delete'),
67
+ 'url' => $this->getUrl('*/*/massDelete'),
68
+ 'confirm' => Mage::helper('crm360')->__('Etes-vous sûr ?')
69
+ ));
70
+ return $this;
71
+ }
72
+
73
+ public function getRowUrl($row)
74
+ {
75
+ return '';
76
+ }
77
+
78
+ }
app/code/community/Followthesun/Crm360/Helper/Data.php ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Followthesun_Crm360_Helper_Data extends Mage_Core_Helper_Abstract {
4
+
5
+ /*
6
+ * Check if login/password is correct
7
+ * return Boolean
8
+ */
9
+ public function identificationCheck($login, $password) {
10
+ $config_login = Mage::getStoreConfig('crm360_config/identification/login');
11
+ $config_password = Mage::getStoreConfig('crm360_config/identification/password');
12
+ if ($config_login === $login && $config_password === $password) {
13
+ return true;
14
+ }
15
+ return false;
16
+ }
17
+
18
+ /*
19
+ * Return tmp dir
20
+ */
21
+ public function getTmpDir() {
22
+ $path = Mage::getBaseDir('media') . DS . 'crm360' . DS;
23
+ if(!is_dir($path)) {
24
+ mkdir($path,0777);
25
+ }
26
+ return $path;
27
+ }
28
+
29
+ /*
30
+ * Return customer last billing address use OR default billing address OR false
31
+ */
32
+ public function getCustommerAddress($_customer) {
33
+
34
+ /* search last order */
35
+ $orders = Mage::getResourceModel('sales/order_collection')
36
+ ->addFieldToSelect('*')
37
+ ->addFieldToFilter('customer_id', $_customer->getId())
38
+ ->addAttributeToSort('created_at', 'DESC')
39
+ ->setPageSize(1);
40
+ $lastOrder = $orders->getFirstItem();
41
+ if($lastOrder->getId()) {
42
+ return Mage::getModel('sales/order_address')->load($lastOrder->getBillingAddressId());
43
+ }
44
+ elseif($defaultBillingAddress = $_customer->getDefaultBillingAddress())
45
+ {
46
+ return $defaultBillingAddress;
47
+ }
48
+ return false;
49
+ }
50
+
51
+ /*
52
+ * return Array with country code (ISO2, ISO3)
53
+ */
54
+ public function prepareArrayCountries() {
55
+ $coll = Mage::getModel('directory/country')->getCollection();
56
+ $array = "";
57
+ foreach($coll as $country) {
58
+ $array[$country['country_id']]['iso2_code'] = $country->getData('iso2_code');
59
+ $array[$country['country_id']]['iso3_code'] = $country->getData('iso3_code');
60
+ }
61
+ return $array;
62
+ }
63
+
64
+ /*
65
+ * return format price
66
+ * ex: 15,55 => 15.55
67
+ * 9 => 9.00
68
+ */
69
+ public function formatPrice($value) {
70
+ $value = Mage::getModel('directory/currency')->format(
71
+ $value,
72
+ array('display'=>Zend_Currency::NO_SYMBOL,'precision'=>2),
73
+ false
74
+ );
75
+ return str_replace(',','.',$value);
76
+ }
77
+ }
app/code/community/Followthesun/Crm360/Model/Log.php ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Followthesun_Crm360_Model_Log extends Mage_Core_Model_Abstract
4
+ {
5
+ CONST STATUS_PENDING = 'En attente';
6
+ CONST STATUS_PROCESSING = 'En cours de traitement';
7
+ CONST STATUS_SUCCESS = 'Terminé avec succès';
8
+ CONST STATUS_FAILED = 'Erreur lors du traitement';
9
+
10
+ public function _construct()
11
+ {
12
+ parent::_construct();
13
+ $this->_init('crm360/log');
14
+ }
15
+ }
app/code/community/Followthesun/Crm360/Model/Mysql4/Log.php ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Followthesun_Crm360_Model_Mysql4_Log extends Mage_Core_Model_Mysql4_Abstract
4
+ {
5
+ public function _construct()
6
+ {
7
+ // Note that the faq_id refers to the key field in your database table.
8
+ $this->_init('crm360/log', 'log_id');
9
+ }
10
+ }
app/code/community/Followthesun/Crm360/Model/Mysql4/Log/Collection.php ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Followthesun_Crm360_Model_Mysql4_Log_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
4
+ {
5
+ public function _construct()
6
+ {
7
+ parent::_construct();
8
+ $this->_init('crm360/log');
9
+ }
10
+ }
app/code/community/Followthesun/Crm360/controllers/Adminhtml/LogController.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Followthesun_Crm360_Adminhtml_LogController extends Mage_Adminhtml_Controller_action {
4
+
5
+ protected function _initAction() {
6
+
7
+ $this->loadLayout()
8
+ ->_setActiveMenu('crm360/logs');
9
+ return $this;
10
+ }
11
+
12
+ public function indexAction() {
13
+ $this->_initAction()
14
+ ->renderLayout();
15
+ }
16
+
17
+ public function massDeleteAction() {
18
+ $logIds = $this->getRequest()->getParam('log');
19
+ if (!is_array($logIds)) {
20
+ Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)'));
21
+ } else {
22
+ try {
23
+ foreach ($logIds as $logId) {
24
+ $log = Mage::getModel('crm360/log')->load($logId);
25
+ $log->delete();
26
+ }
27
+ Mage::getSingleton('adminhtml/session')->addSuccess(
28
+ Mage::helper('adminhtml')->__(
29
+ 'Total of %d record(s) were successfully deleted', count($logIds)
30
+ )
31
+ );
32
+ } catch (Exception $e) {
33
+ Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
34
+ }
35
+ }
36
+ $this->_redirect('*/*/index');
37
+ }
38
+ }
app/code/community/Followthesun/Crm360/controllers/IndexController.php ADDED
@@ -0,0 +1,441 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Followthesun_Crm360_IndexController extends Mage_Core_Controller_Front_Action {
4
+
5
+ var $timestamp;
6
+ var $ftp_url;
7
+ var $ftp_login;
8
+ var $ftp_password;
9
+ var $ftp_path;
10
+ var $tmp_dir;
11
+ var $errors;
12
+
13
+ /**
14
+ * Predispatch: update tmp_dir AND check identification
15
+ *
16
+ * @return Mage_Core_Controller_Front_Action
17
+ */
18
+ public function preDispatch() {
19
+ parent::preDispatch();
20
+
21
+ $helper = Mage::helper('crm360');
22
+ $this->tmp_dir = $helper->getTmpDir();
23
+
24
+ $params = $this->getRequest()->getParams();
25
+ $login = $params['login'];
26
+ $password = $params['password'];
27
+ if (!$helper->identificationCheck($login, $password)) { /* identification failed */
28
+ $action = Mage::app()->getRequest()->getActionName();
29
+ if ($action === 'export') {
30
+ $logModel = Mage::getModel('crm360/log');
31
+ $logModel->setCreatedAt(date('Y-m-d H:i:s'))
32
+ ->setStatus($logModel::STATUS_FAILED)
33
+ ->setMessage($this->__('Erreur de login ou mot de passe'))
34
+ ->save();
35
+ }
36
+ $result['response']['error'] = true;
37
+ $result['response']['message'] = $this->__('Erreur de login ou de mot de passe');
38
+
39
+ $this->getResponse()
40
+ ->clearHeaders()
41
+ ->setHeader('Content-Type', 'application/json')
42
+ ->setBody(json_encode($result));
43
+
44
+ $this->setFlag('', 'no-dispatch', true);
45
+ }
46
+ }
47
+
48
+ public function exportAction() {
49
+
50
+ $params = $this->getRequest()->getParams();
51
+ $this->type = $params['type'];
52
+
53
+ $result = $this->checkParams($params);
54
+ if(empty($result) || (is_array($result) && !isset($result['error']))) {
55
+ $logModel = Mage::getModel('crm360/log');
56
+ $logModel->setCreatedAt(date('Y-m-d H:i:s'))
57
+ ->setType($this->type)
58
+ ->setStatus($logModel::STATUS_PENDING)
59
+ ->save();
60
+ $params['id'] = $logModel->getId();
61
+
62
+ /* launch Export => erreur commande wget not found */
63
+ $wget_path = Mage::getStoreConfig('crm360_config/config/wget_path');
64
+ $return = shell_exec($wget_path.' -O /dev/null "' . Mage::getUrl('*/*/launchExport', array('_query'=>$params)) . '" &');
65
+
66
+ $result['response']['id'] = $logModel->getId();
67
+ $result['response']['status'] = $logModel->getStatus();
68
+ }
69
+ $this->getResponse()
70
+ ->clearHeaders()
71
+ ->setHeader('Content-Type', 'application/json')
72
+ ->setBody(json_encode($result));
73
+ }
74
+
75
+ /*
76
+ *
77
+ */
78
+ public function launchExportAction() {
79
+
80
+ $params = $this->getRequest()->getParams();
81
+ $id = $params['id'];
82
+ $this->timestamp = (isset($params['timestamp'])) ? $params['timestamp'] : null;
83
+ $this->type = $params['type'];
84
+ $this->ftp_url = $params['ftp_url'];
85
+ $this->ftp_login = $params['ftp_login'];
86
+ $this->ftp_password = $params['ftp_password'];
87
+ $this->ftp_path = $params['ftp_path'];
88
+
89
+ $logModel = Mage::getModel('crm360/log')->load($id);
90
+
91
+ /* prévention du max_execution_time */
92
+ register_shutdown_function(array($this, 'handleFatalError'),$logModel);
93
+
94
+ if (!$logModel || !$logModel->getId()) {
95
+ $this->error[] = $this->__('La demande numéro %s n\'existe pas', $id);
96
+ } elseif ($logModel->getStatus() !== $logModel::STATUS_PENDING) {
97
+ $this->error[] = $this->__('La demande numéro %s est déjà en cours de traitement ou terminée', $id);
98
+ } else {
99
+ $logModel->setStatus($logModel::STATUS_PROCESSING)->save();
100
+ $pathFiles = '';
101
+ switch ($this->type) {
102
+ case 'customers' :
103
+ $pathFiles = $this->_prepareCustomersFile($logModel->getId());
104
+ break;
105
+ case 'orders' :
106
+ $pathFiles = $this->_prepareOrdersFile($logModel->getId());
107
+ break;
108
+ case 'orderDetails' :
109
+ $pathFiles = $this->_prepareOrderDetailsFile($logModel->getId());
110
+ break;
111
+ case 'products' :
112
+ $pathFiles = $this->_prepareProductsFile($logModel->getId());
113
+ break;
114
+ case 'categories' :
115
+ $pathFiles = $this->_prepareCategoriesFile($logModel->getId());
116
+ break;
117
+ default :
118
+ $this->error[] = $this->__('Le type d\'export "'.$this->type.'" n\'existe pas.');
119
+ break;
120
+ }
121
+ }
122
+
123
+ if (is_array($this->error)) {
124
+ $logModel->setStatus($logModel::STATUS_FAILED)
125
+ ->setMessage(implode("\n", $this->error));
126
+ } elseif ($pathFiles) {
127
+ if(!is_array($pathFiles)) $pathFiles = array($pathFiles);
128
+ $this->ftpCopy($pathFiles);
129
+ foreach($pathFiles as $pathFile)
130
+ {
131
+ unlink($pathFile);
132
+ }
133
+ $logModel->setStatus($logModel::STATUS_SUCCESS);
134
+ }
135
+ $logModel->setUpdatedAt(date('Y-m-d H:i:s'));
136
+ $logModel->save();
137
+ }
138
+
139
+ /**
140
+ * Méthode appelée à la détection d'une fatal error.
141
+ */
142
+ public function handleFatalError($logModel)
143
+ {
144
+ $lastError = error_get_last();
145
+ if(isset($lastError['type']) && $lastError['type'] === E_ERROR)
146
+ {
147
+ //var_dump($logModel);
148
+ $logModel->setStatus($logModel::STATUS_FAILED)
149
+ ->setUpdatedAt(date('Y-m-d H:i:s'))
150
+ ->setMessage($lastError['message'])
151
+ ->save();
152
+ }
153
+ }
154
+
155
+ protected function checkParams($params) {
156
+ $result = '';
157
+ if (!isset($params['type']) || !isset($params['ftp_url']) || !isset($params['ftp_login']) || !isset($params['ftp_password']) || !isset($params['ftp_path'])) {
158
+ $result['error'] = "Erreur de paramètre";
159
+ } else {
160
+ $type = $params['type'];
161
+ if(!in_array($type, array('customers','orders','orderDetails','products','categories'))) {
162
+ $result['error'] = $this->__('Le type d\'export "'.$this->type.'" n\'existe pas.');
163
+ } else {
164
+ $conn_id = ftp_connect($params['ftp_url']);
165
+
166
+ try {
167
+ // login with username and password
168
+ $login_result = ftp_login($conn_id, $params['ftp_login'], $params['ftp_password']);
169
+ if (!$login_result) {
170
+ $result['error'] = $this->__('Erreur lors de la connexion FTP');
171
+ }
172
+ } catch(Exception $e) {
173
+ }
174
+ }
175
+ }
176
+ return $result;
177
+ }
178
+
179
+ /*
180
+ * Create csv file with customers info
181
+ * return path file
182
+ */
183
+ protected function _prepareCustomersFile($logModelId) {
184
+ $pathFile = $this->tmp_dir . 'Contact-' . date('Y-m-d') .'-'.$logModelId.'.csv';
185
+ $helper = Mage::helper('crm360');
186
+
187
+ try {
188
+
189
+ /* prepare array with country code */
190
+ $arrayCountries = $helper->prepareArrayCountries();
191
+
192
+ $fp = fopen($pathFile, 'w');
193
+ $coll = Mage::getModel('customer/customer')->getCollection();
194
+ $coll->addAttributeToSelect(array('firstname', 'lastname', 'email', 'dob', 'is_subscribed'));
195
+ if ($this->timestamp !== null) {
196
+ $coll->addAttributeToFilter('updated_at', array('from' => $this->timestamp));
197
+ }
198
+ foreach ($coll as $customer) {
199
+ $customerBillingAddress = $helper->getCustommerAddress($customer);
200
+ $data = array(
201
+ $customer->getId(),
202
+ $customer->getPrefix(),
203
+ $customer->getFirstname(),
204
+ $customer->getLastname(),
205
+ ($customerBillingAddress && $customerBillingAddress->getId()) ? $customerBillingAddress->getStreet(1) : '',
206
+ ($customerBillingAddress && $customerBillingAddress->getId()) ? $customerBillingAddress->getStreet(2) : '',
207
+ ($customerBillingAddress && $customerBillingAddress->getId()) ? $customerBillingAddress->getData('postcode') : '',
208
+ ($customerBillingAddress && $customerBillingAddress->getId()) ? $customerBillingAddress->getData('city') : '',
209
+ ($customerBillingAddress && $customerBillingAddress->getId()) ? $customerBillingAddress->getRegion() : '',
210
+ ($customerBillingAddress && $customerBillingAddress->getId() && isset($arrayCountries[$customerBillingAddress->getData('country_id')])) ? $arrayCountries[$customerBillingAddress->getData('country_id')]['iso3_code'] : '',
211
+ $customer->getEmail(),
212
+ (int)$customer->getIsSubscribed(),
213
+ '',
214
+ ($customerBillingAddress && $customerBillingAddress->getId()) ? $customerBillingAddress->getData('telephone') : '',
215
+ $customer->getDob() ? date('d/m/Y', strtotime($customer->getDob())) : '',
216
+ ($customerBillingAddress && $customerBillingAddress->getId()) ? $customerBillingAddress->getCompany() : '',
217
+ '',
218
+ date('d/m/Y H:i:s', strtotime($customer->getUpdatedAt())),
219
+ '',
220
+ '',
221
+ ($customerBillingAddress && $customerBillingAddress->getId() && isset($arrayCountries[$customerBillingAddress->getData('country_id')])) ? $arrayCountries[$customerBillingAddress->getData('country_id')]['iso3_code'] : ''
222
+ );
223
+ //fputcsv($fp, $data, '|');
224
+ fputs($fp,implode($data, '|').PHP_EOL);
225
+ }
226
+ fclose($fp);
227
+ return $pathFile;
228
+ } catch (Exception $e) {
229
+ $this->error[] = $e->getMessage();
230
+ return false;
231
+ }
232
+ }
233
+
234
+ /*
235
+ * Create csv file with orders info
236
+ * return Array path file header and detail
237
+ */
238
+ protected function _prepareOrdersFile($logModelId) {
239
+ try {
240
+ $helper = Mage::helper('crm360');
241
+ $time = date('Y-m-d');
242
+
243
+ $pathFileHeader = $this->tmp_dir . 'PurchaseHeader-' . $time .'-'.$logModelId.'.csv';
244
+ $fpHeader = fopen($pathFileHeader, 'w');
245
+
246
+ $coll = Mage::getModel('sales/order')->getCollection();
247
+ if ($this->timestamp !== null) {
248
+ $coll->addAttributeToFilter('updated_at', array('from' => $this->timestamp));
249
+ }
250
+ foreach ($coll as $order) {
251
+ /* Order Header */
252
+ $dataHeader = array(
253
+ $order->getCustomerId(),
254
+ date('d/m/Y H:i:s', strtotime($order->getUpdatedAt())),
255
+ $order->getStoreId(),
256
+ $order->getIncrementId(),
257
+ $order->getBaseCurrencyCode(),
258
+ $helper->formatPrice($order->getGrandTotal()),
259
+ $helper->formatPrice($order->getGrandTotal()-$order->getTaxAmount()),
260
+ $helper->formatPrice($order->getDiscountAmount()),
261
+ $order->getStatus()
262
+ );
263
+ //fputcsv($fpHeader, $dataHeader, '|');
264
+ fputs($fpHeader,implode($dataHeader, '|').PHP_EOL);
265
+
266
+ }
267
+ fclose($fpHeader);
268
+
269
+ return array($pathFileHeader);
270
+ } catch (Exception $e) {
271
+ $this->error[] = $e->getMessage();
272
+ return false;
273
+ }
274
+ }
275
+
276
+ /*
277
+ * Create csv file with orders info
278
+ * return Array path file header and detail
279
+ */
280
+ protected function _prepareOrderDetailsFile($logModelId) {
281
+ try {
282
+ $helper = Mage::helper('crm360');
283
+ $time = date('Y-m-d');
284
+
285
+ $pathFileDetail = $this->tmp_dir . 'PurchaseDetail-' . $time .'-'.$logModelId.'.csv';
286
+ $fpDetail = fopen($pathFileDetail, 'w');
287
+
288
+ $coll = Mage::getModel('sales/order')->getCollection();
289
+ if ($this->timestamp !== null) {
290
+ $coll->addAttributeToFilter('updated_at', array('from' => $this->timestamp));
291
+ }
292
+ foreach ($coll as $order) {
293
+ /* Order Detail */
294
+ $items = $order->getAllItems();
295
+ foreach ($items as $item) {
296
+ $dataDetail = array(
297
+ date('d/m/Y H:i:s', strtotime($order->getUpdatedAt())),
298
+ $order->getStoreId(),
299
+ $order->getIncrementId(),
300
+ $item->getSku(),
301
+ $helper->formatPrice($item->getRowTotalInclTax()),
302
+ $helper->formatPrice($item->getRowTotal()),
303
+ $helper->formatPrice($item->getDiscountAmount()),
304
+ $item->getQtyOrdered()
305
+ );
306
+ //fputcsv($fpDetail, $dataDetail, '|');
307
+ fputs($fpDetail,implode($dataDetail, '|').PHP_EOL);
308
+ }
309
+ }
310
+ fclose($fpDetail);
311
+
312
+ return array($pathFileDetail);
313
+ } catch (Exception $e) {
314
+ $this->error[] = $e->getMessage();
315
+ return false;
316
+ }
317
+ }
318
+
319
+ /*
320
+ * Create csv file with products info
321
+ * return path file
322
+ */
323
+ protected function _prepareProductsFile($logModelId) {
324
+ $pathFile = $this->tmp_dir . 'PurchaseGood-' . date('Y-m-d') .'-'.$logModelId.'.csv';
325
+
326
+ try {
327
+ $fp = fopen($pathFile, 'w');
328
+ $coll = Mage::getModel('catalog/product')->getCollection();
329
+ $coll->addAttributeToSelect(array('sku','name', 'price'))
330
+ ->addFinalPrice();
331
+ if ($this->timestamp !== null) {
332
+ $coll->addAttributeToFilter('updated_at', array('from' => $this->timestamp));
333
+ }
334
+ foreach ($coll as $product) {
335
+ $data = array(
336
+ $product->getSku(),
337
+ $product->getName(),
338
+ implode(',', $product->getCategoryIds())
339
+ );
340
+ //fputcsv($fp, $data, '|');
341
+ fputs($fp,implode($data, '|').PHP_EOL);
342
+ }
343
+ fclose($fp);
344
+ return $pathFile;
345
+ } catch (Exception $e) {
346
+ $this->error[] = $e->getMessage();
347
+ return false;
348
+ }
349
+ }
350
+
351
+ /*
352
+ * Create csv file with categories info
353
+ * return path file
354
+ */
355
+ protected function _prepareCategoriesFile($logModelId) {
356
+ $pathFile = $this->tmp_dir . 'PurchaseGoodCategory-' . date('Y-m-d').'-'.$logModelId.'.csv';
357
+
358
+ try {
359
+ $fp = fopen($pathFile, 'w');
360
+ $coll = Mage::getModel('catalog/category')->getCollection(); //->addFieldToFilter('parent_id',array('neq'=>'0'))
361
+ $coll->addAttributeToSelect(array('name'));
362
+ if ($this->timestamp !== null) {
363
+ $coll->addAttributeToFilter('updated_at', array('from' => $this->timestamp));
364
+ }
365
+ foreach ($coll as $category) {
366
+ $data = array(
367
+ $category->getId(),
368
+ $category->getParentId(),
369
+ $category->getName(),
370
+ );
371
+ //fputcsv($fp, $data, '|');
372
+ fputs($fp,implode($data, '|').PHP_EOL);
373
+ }
374
+ fclose($fp);
375
+ return $pathFile;
376
+ } catch (Exception $e) {
377
+ $this->error[] = $e->getMessage();
378
+ return false;
379
+ }
380
+ }
381
+
382
+ /*
383
+ * copy file in FTP
384
+ */
385
+ protected function ftpCopy($pathFiles) {
386
+ // set up basic connection
387
+ $conn_id = ftp_connect($this->ftp_url);
388
+
389
+ try {
390
+ // login with username and password
391
+ $login_result = ftp_login($conn_id, $this->ftp_login, $this->ftp_password);
392
+ if (!$login_result) {
393
+ $this->error[] = $this->__('Erreur lors de la connexion FTP');
394
+ return false;
395
+ }
396
+ foreach($pathFiles as $pathFile) {
397
+ // upload a file
398
+ $remote_file = $this->ftp_path . basename($pathFile);
399
+ if (ftp_put($conn_id, $remote_file, $pathFile, FTP_ASCII)) {
400
+
401
+ } else {
402
+ $this->error[] = $this->__("Une erreur est survenue lors du transfère du fichier");
403
+ }
404
+ }
405
+ // close the connection
406
+ ftp_close($conn_id);
407
+ } catch (Exception $e) {
408
+ $this->error[] = $e->getMessage();
409
+ return false;
410
+ }
411
+ }
412
+
413
+ /*
414
+ * Return status of task
415
+ */
416
+ public function statusAction() {
417
+ $params = $this->getRequest()->getParams();
418
+ $result = '';
419
+ if (!isset($params['id'])) {
420
+ $result['error'] = "Erreur de paramètre";
421
+ } else {
422
+ $id = $params['id'];
423
+ $logModel = Mage::getModel('crm360/log')->load($id);
424
+ if ($logModel && $logModel->getId()) {
425
+ $result['response']['status'] = $logModel->getStatus();
426
+ /* if status failed, add message to response */
427
+ if ($logModel->getStatus() == $logModel::STATUS_FAILED) {
428
+ $result['response']['message'] = $logModel->getMessage();
429
+ }
430
+ } else {
431
+ $result['response']['error'] = true;
432
+ $result['response']['message'] = $this->__('La demande numéro %s n\'existe pas', $id);
433
+ }
434
+ }
435
+ $this->getResponse()
436
+ ->clearHeaders()
437
+ ->setHeader('Content-Type', 'application/json')
438
+ ->setBody(json_encode($result));
439
+ }
440
+
441
+ }
app/code/community/Followthesun/Crm360/etc/config.xml ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Followthesun_Crm360>
5
+ <version>0.1.0</version>
6
+ </Followthesun_Crm360>
7
+ </modules>
8
+
9
+ <frontend>
10
+ <routers>
11
+ <crm360>
12
+ <use>standard</use>
13
+ <args>
14
+ <module>Followthesun_Crm360</module>
15
+ <frontName>crm360</frontName>
16
+ </args>
17
+ </crm360>
18
+ </routers>
19
+ <layout>
20
+ <updates>
21
+ <followthesun_crm360>
22
+ <file>followthesun_crm360.xml</file>
23
+ </followthesun_crm360>
24
+ </updates>
25
+ </layout>
26
+ </frontend>
27
+
28
+ <admin>
29
+ <routers>
30
+ <crm360>
31
+ <use>admin</use>
32
+ <args>
33
+ <module>Followthesun_Crm360</module>
34
+ <frontName>crm360</frontName>
35
+ </args>
36
+ </crm360>
37
+ </routers>
38
+ </admin>
39
+ <adminhtml>
40
+ <layout>
41
+ <updates>
42
+ <followthesun_crm360>
43
+ <file>followthesun_crm360.xml</file>
44
+ </followthesun_crm360>
45
+ </updates>
46
+ </layout>
47
+ <menu>
48
+ <crm360>
49
+ <title>CRM 360</title>
50
+ <sort_order>100</sort_order>
51
+ <children>
52
+ <logs>
53
+ <title>Logs</title>
54
+ <action>crm360/adminhtml_log</action>
55
+ </logs>
56
+ <config>
57
+ <title>Configuration</title>
58
+ <action>adminhtml/system_config/edit/section/crm360_config/</action>
59
+ </config>
60
+ </children>
61
+ </crm360>
62
+ </menu>
63
+ <acl>
64
+ <resources>
65
+ <admin>
66
+ <children>
67
+ <crm360>
68
+ <children>
69
+ <logs translate="title">
70
+ <title>Logs</title>
71
+ <sort_order>1</sort_order>
72
+ </logs>
73
+ </children>
74
+ </crm360>
75
+ <system>
76
+ <children>
77
+ <config>
78
+ <children>
79
+ <crm360_config translate="title" module="crm360">
80
+ <title>CRM 360 Configuration</title>
81
+ <sort_order>100</sort_order>
82
+ </crm360_config>
83
+ </children>
84
+ </config>
85
+ </children>
86
+ </system>
87
+ </children>
88
+ </admin>
89
+ </resources>
90
+ </acl>
91
+ </adminhtml>
92
+
93
+ <global>
94
+ <blocks>
95
+ <crm360>
96
+ <class>Followthesun_Crm360_Block</class>
97
+ </crm360>
98
+ </blocks>
99
+ <helpers>
100
+ <crm360>
101
+ <class>Followthesun_Crm360_Helper</class>
102
+ </crm360>
103
+ </helpers>
104
+ <models>
105
+ <crm360>
106
+ <class>Followthesun_Crm360_Model</class>
107
+ <resourceModel>crm360_mysql4</resourceModel>
108
+ </crm360>
109
+ <crm360_mysql4>
110
+ <class>Followthesun_Crm360_Model_Mysql4</class>
111
+ <entities>
112
+ <log>
113
+ <table>crm360_logs</table>
114
+ </log>
115
+ </entities>
116
+ </crm360_mysql4>
117
+ </models>
118
+ <resources>
119
+ <crm360_setup>
120
+ <setup>
121
+ <module>Followthesun_Crm360</module>
122
+ </setup>
123
+ <connection>
124
+ <use>core_setup</use>
125
+ </connection>
126
+ </crm360_setup>
127
+ <crm360_write>
128
+ <connection>
129
+ <use>core_write</use>
130
+ </connection>
131
+ </crm360_write>
132
+ <crm360_read>
133
+ <connection>
134
+ <use>core_read</use>
135
+ </connection>
136
+ </crm360_read>
137
+ </resources>
138
+ </global>
139
+
140
+ <!-- Valeurs par défaut -->
141
+ <default>
142
+ <crm360_config>
143
+ <config>
144
+ <wget_path>/usr/bin/wget</wget_path>
145
+ </config>
146
+ </crm360_config>
147
+ </default>
148
+
149
+ </config>
app/code/community/Followthesun/Crm360/etc/system.xml ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <tabs>
4
+ <crm360 translate="label" module="crm360">
5
+ <label>CRM 360</label>
6
+ <sort_order>200</sort_order>
7
+ </crm360>
8
+ </tabs>
9
+ <sections>
10
+ <crm360_config>
11
+ <label>Configuration</label>
12
+ <tab>crm360</tab>
13
+ <frontend_type>text</frontend_type>
14
+ <sort_order>2000</sort_order>
15
+ <show_in_default>1</show_in_default>
16
+ <show_in_website>1</show_in_website>
17
+ <show_in_store>1</show_in_store>
18
+ <groups>
19
+ <identification translate="label">
20
+ <label>Identification</label>
21
+ <frontend_type>text</frontend_type>
22
+ <sort_order>1</sort_order>
23
+ <show_in_default>1</show_in_default>
24
+ <show_in_website>1</show_in_website>
25
+ <show_in_store>1</show_in_store>
26
+ <fields>
27
+ <login translate="label">
28
+ <label>Login</label>
29
+ <frontend_type>text</frontend_type>
30
+ <sort_order>1</sort_order>
31
+ <show_in_default>1</show_in_default>
32
+ <show_in_website>1</show_in_website>
33
+ <show_in_store>1</show_in_store>
34
+ </login>
35
+ <password translate="label">
36
+ <label>Password</label>
37
+ <frontend_type>text</frontend_type>
38
+ <sort_order>2</sort_order>
39
+ <show_in_default>1</show_in_default>
40
+ <show_in_website>1</show_in_website>
41
+ <show_in_store>1</show_in_store>
42
+ </password>
43
+ </fields>
44
+ </identification>
45
+ <config translate="label">
46
+ <label>Configuration</label>
47
+ <frontend_type>text</frontend_type>
48
+ <sort_order>1</sort_order>
49
+ <show_in_default>1</show_in_default>
50
+ <show_in_website>1</show_in_website>
51
+ <show_in_store>1</show_in_store>
52
+ <fields>
53
+ <wget_path translate="label comment">
54
+ <label>Commande wget</label>
55
+ <comment>Chemin vers la commande wget</comment>
56
+ <frontend_type>text</frontend_type>
57
+ <sort_order>1</sort_order>
58
+ <show_in_default>1</show_in_default>
59
+ <show_in_website>0</show_in_website>
60
+ <show_in_store>0</show_in_store>
61
+ </wget_path>
62
+ </fields>
63
+ </config>
64
+ </groups>
65
+ </crm360_config>
66
+ </sections>
67
+ </config>
app/code/community/Followthesun/Crm360/sql/crm360_setup/mysql4-install-0.1.0.php ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ $installer = $this;
4
+
5
+ $installer->startSetup();
6
+
7
+ $installer->run("
8
+
9
+ DROP TABLE IF EXISTS {$this->getTable('crm360/log')};
10
+ CREATE TABLE {$this->getTable('crm360/log')} (
11
+ `log_id` int(11) unsigned NOT NULL auto_increment,
12
+ `type` varchar(255) NULL,
13
+ `message` text NULL,
14
+ `status` varchar(50) NULL,
15
+ `created_at` datetime NULL,
16
+ `updated_at` datetime NULL,
17
+ PRIMARY KEY (`log_id`)
18
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
19
+ ");
20
+
21
+ $installer->endSetup();
app/design/adminhtml/default/default/layout/followthesun_crm360.xml ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <layout>
3
+ <crm360_adminhtml_log_index>
4
+ <reference name="head">
5
+ <action method="setTitle" translate="title"><title>Log</title></action>
6
+ </reference>
7
+ <reference name="content">
8
+ <block type="crm360/adminhtml_log" name="crm360_log" />
9
+ </reference>
10
+ </crm360_adminhtml_log_index>
11
+ </layout>
app/etc/modules/Followthesun_Crm360.xml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Followthesun_Crm360>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ </Followthesun_Crm360>
8
+ </modules>
9
+ </config>
package.xml ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Followthesun_Crm360</name>
4
+ <version>0.1.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://opensource.org/licenses/osl-3.0.php">Open Software License (OSL)</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Rassemblez toutes vos donn&#xE9;es clients/achats/produits dans votre r&#xE9;f&#xE9;rentiel client unique CRM360&#xB0; !</summary>
10
+ <description>Redonnez de l&#x2019;agilit&#xE9; &#xE0; votre Relation Client.&lt;br /&gt;&lt;br /&gt;&#xD;
11
+ &#xD;
12
+ CRM360&#xB0;, &#xE9;dit&#xE9; par la soci&#xE9;t&#xE9; follow the sun, est la solution CRM Marketing BtoC la plus &#xE9;conomique, qui permet de conna&#xEE;tre et dialoguer avec chacun de ses clients, en moins de huit jours, pour fid&#xE9;liser et d&#xE9;velopper ses ventes.&lt;br /&gt;&lt;br /&gt;&#xD;
13
+ &#xD;
14
+ V&#xE9;ritable solution de travail collaborative entre les agences Marketing et les marques, cette solution SaaS propose :&#xD;
15
+ &lt;ul&gt;&#xD;
16
+ &lt;li&gt;- une vision &#xE0; 360&#xB0; de chacun de ses clients&lt;/li&gt;&#xD;
17
+ &lt;li&gt;- la gestion int&#xE9;gr&#xE9;e et automatis&#xE9;e de la communication multicanal&lt;/li&gt;&#xD;
18
+ &lt;li&gt;- un acc&#xE8;s dynamique aux donn&#xE9;es&lt;/li&gt;&#xD;
19
+ &lt;li&gt;- une gestion des droits adapt&#xE9;e aux structures d&#xE9;centralis&#xE9;es&lt;/li&gt;&#xD;
20
+ &lt;/ul&gt;&#xD;
21
+ Nos points forts : &#xD;
22
+ &lt;ul&gt;&#xD;
23
+ &lt;li&gt;- un &#xAB; traitement qualit&#xE9; &#xBB; int&#xE9;gr&#xE9; des donn&#xE9;es&lt;/li&gt;&#xD;
24
+ &lt;li&gt;- une capacit&#xE9; de ciblages multicrit&#xE8;res simple et puissante&lt;/li&gt;&#xD;
25
+ &lt;li&gt;- une architecture ouverte pour des fonctionnalit&#xE9;s compl&#xE9;mentaires&lt;/li&gt;&#xD;
26
+ &lt;li&gt;- une solution adapt&#xE9;e aux organisations Internationales&lt;/li&gt;&#xD;
27
+ &lt;/ul&gt;&#xD;
28
+ Disposez de votre propre base de donn&#xE9;es Marketing &#xAB; all inclusive &#xBB;. &lt;br /&gt;&lt;br /&gt;&#xD;
29
+ &#xD;
30
+ Pour en savoir plus, rendez-vous sur http://crm360.fts-services.com</description>
31
+ <notes>-</notes>
32
+ <authors><author><name>Followthesun</name><user>Followthesun</user><email>support.magento@fts-services.com</email></author></authors>
33
+ <date>2014-01-28</date>
34
+ <time>13:30:40</time>
35
+ <contents><target name="magecommunity"><dir name="Followthesun"><dir name="Crm360"><dir name="Block"><dir name="Adminhtml"><dir name="Log"><file name="Grid.php" hash="4e7bbd40c3b5bfdd214fb5b9c9e2ba93"/></dir><file name="Log.php" hash="735456f8665be8ccb491c4db2ddd65e9"/></dir></dir><dir name="Helper"><file name="Data.php" hash="d0f298a2f7dc08e581dd4325afc97ceb"/></dir><dir name="Model"><file name="Log.php" hash="86cb866b5c324c0ad83ff5c2a5c96492"/><dir name="Mysql4"><dir name="Log"><file name="Collection.php" hash="6d8dff8a190e4aebb7bbe470f70d5e9f"/></dir><file name="Log.php" hash="1848a798c760a87133671ebe4e26ff6b"/></dir></dir><dir name="controllers"><dir name="Adminhtml"><file name="LogController.php" hash="c107355484f115e681208f74a7e05be6"/></dir><file name="IndexController.php" hash="25b466455fb871f50ebed032b884af3e"/></dir><dir name="etc"><file name="config.xml" hash="b11677837d4af13da851ac7bad55a021"/><file name="system.xml" hash="b1d9b17e8c347e334f30b99e57c2ee75"/></dir><dir name="sql"><dir name="crm360_setup"><file name="mysql4-install-0.1.0.php" hash="5da8a1c638dc921e01c79b15dffeac75"/></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Followthesun_Crm360.xml" hash="a08a9efc4777fe67b23322bb7cd0ce05"/></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="layout"><file name="followthesun_crm360.xml" hash="df2f2d310f3290e7c59bf601f5805f2d"/></dir></dir></dir></dir></target></contents>
36
+ <compatible/>
37
+ <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
38
+ </package>