-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrvNetImport.php
338 lines (311 loc) · 11.9 KB
/
CrvNetImport.php
1
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/**
* Created by PhpStorm.
* User: Jorge
* Date: 02/11/2018
* Time: 12:21
*/
if (!defined('_PS_VERSION_')) {
exit;
}
include_once dirname(__FILE__) . '/classes/CrvNetImportClass.php';
class CrvNetImport extends Module
{
/**
* Names of single preferences of module settings.
*/
const PREF_FEED_ZIP = 'PREF_FEED_ZIP';
const PREF_FEED_SUPPLIER = 'PREF_FEED_SUPPLIER';
const PREF_FEED_STOCK = 'PREF_FEED_STOCK';
const PREF_FEED_IMAGES = 'PREF_FEED_IMAGES';
const PREF_FEED_DUPLICATE = 'PREF_FEED_DUPLICATE';
const PREF_FEED_IMPORT_IMAGES = 'PREF_FEED_IMPORT_IMAGES';
const TOTAL = 'PREF_FEED_TOTAL';
const ACTUAL = 'PREF_FEED_ACTUAL';
const TEXT = 'PREF_FEED_TEXT';
/**
* Default values for module settings.
*/
const DEFAULT_FEED_URL = '';
private $import;
public function ajaxPercentageImport()
{
if (ob_get_length() > 0) {
ob_end_clean();
}
$totalItemExport = Configuration::get(self::TOTAL);
$totalItemExported = Configuration::get(self::ACTUAL);
$porcentaje = (float)round($totalItemExported * 100 / $totalItemExport, 2);
die(json_encode(array(
'percent' => (is_float($porcentaje) ? $porcentaje: 0),
'text' => Configuration::get(self::TEXT),
'totalItemExport' => $totalItemExport,
'totalItemExported' => $totalItemExported,
)));
}
/**
* @since 1.0.0
* @see ModuleCore::__construct()
*/
public function __construct()
{
$this->name = 'CrvNetImport';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'Jorge Márquez';
$this->need_instance = 1;
$this->bootstrap = true;
$this->_directory = dirname(__FILE__);
parent::__construct();
$this->displayName = $this->l('CrvNet Import XML');
$this->description = $this->l('Module for importing XML with products from CrvNET 5.10.4.2');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
}
/**
* @since 1.0.0
* @return boolean
* @see ModuleCore::install()
*/
public function install()
{
if (!parent::install()) {
return false;
}
Configuration::updateValue(self::PREF_FEED_ZIP,0);
Configuration::updateValue(self::PREF_FEED_SUPPLIER,0);
Configuration::updateValue(self::PREF_FEED_STOCK,0);
Configuration::updateValue(self::PREF_FEED_IMAGES,0);
Configuration::updateValue(self::PREF_FEED_DUPLICATE,0);
Configuration::updateValue(self::PREF_FEED_IMPORT_IMAGES,0);
Configuration::updateValue(self::TOTAL,0);
Configuration::updateValue(self::ACTUAL,0);
Configuration::updateValue(self::TEXT,"");
return true;
}
/**
* @since 1.0.0
* @return boolean
* @see ModuleCore::uninstall()
*/
public function uninstall()
{
if (!parent::uninstall()) {
return false;
}
Configuration::deleteByName(self::PREF_FEED_ZIP);
Configuration::deleteByName(self::PREF_FEED_SUPPLIER);
Configuration::deleteByName(self::PREF_FEED_STOCK);
Configuration::deleteByName(self::PREF_FEED_IMAGES);
Configuration::deleteByName(self::PREF_FEED_DUPLICATE);
Configuration::deleteByName(self::PREF_FEED_IMPORT_IMAGES);
Configuration::deleteByName(self::TOTAL);
Configuration::deleteByName(self::ACTUAL);
Configuration::deleteByName(self::TEXT);
return true;
}
/**
* @since 1.0.0
*/
public function getContent()
{
$valor = Tools::getValue('submitCrvNetImport');
if ($valor == "1"
|| $valor == "2"
|| $valor == "3"
|| $valor == "4"
|| $valor == "5")
{
if (file_exists(_PS_DOWNLOAD_DIR_."ExportXML.zip")) {
$borrardirectoriozip = Tools::getValue(self::PREF_FEED_ZIP);
$id_supplier = Tools::getValue(self::PREF_FEED_SUPPLIER);
$deleteimages = Tools::getValue(self::PREF_FEED_IMAGES);
$incrementstock = Tools::getValue(self::PREF_FEED_STOCK);
$modificarcoincidentes = Tools::getValue(self::PREF_FEED_DUPLICATE);
$importimages = Tools::getValue(self::PREF_FEED_IMPORT_IMAGES);
if ($id_supplier != 0){
try {
//import process
$this->import = new CrvNetImportClass(_PS_DOWNLOAD_DIR_."ExportXML/", $id_supplier, $deleteimages, $incrementstock, $modificarcoincidentes, $importimages, $borrardirectoriozip);
$this->import->importSteps($valor);
} catch (Exception $e) {
die(json_encode(array(
'error' => 1,
'text' => $this->l('Error to import XML:'.$e->getMessage().'\n'.$e->getTraceAsString()),
)));
}
}else{
die(json_encode(array(
'error' => 1,
'text' => 'Proveedor no válido, seleccione uno.',
)));
}
} else {
die(json_encode(array(
'error' => 1,
'text' => 'Fichero XML no válido.',
)));
}
}
if (Tools::getValue('submitCrvNetImport') == "6") {
if (ob_get_length() > 0) {
ob_end_clean();
}
$this->ajaxPercentageImport();
}
return $this->displayForm();
}
/**
* @since 1.0.0
*/
public function displayForm()
{
/**
* @var integer $default_lang
*/
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
/**
* @var HelperForm $helper
*/
$helper = new HelperForm();
$suppliers = Supplier::getSuppliers(false, null, true);
$suppliersList = [];
if ($suppliers && count($suppliers)) {
foreach ($suppliers as $supplier) {
$suppliersList[] = array("id_supplier" => $supplier['id_supplier'], "name" => $supplier['name']);
}
}
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cog',
),
'input' => array(
array(
'type' => 'switch',
'label' => $this->l('¿Borrar directorio de importación ZIP?'),
'name' => self::PREF_FEED_ZIP,
'class' => 't',
'required' => true,
'is_bool' => true,
'values' => array(
array(
'id' => 'zip',
'value' => 0
),
array(
'id' => 'zip',
'value' => 1
)
)
),
array(
'type' => 'select',
'label' => $this->l('Proveedor'),
'desc' => $this->l('Seleccione proveedor de las piezas'),
'required' => true,
'name' => self::PREF_FEED_SUPPLIER,
'options' => array(
'query' => $suppliersList,
'id' => 'id_supplier',
'name' => 'name',
'default' => array(
'value' => '0',
'label' => $this->l('-Elige un proveedor-')
)
)
),
array(
'type' => 'switch',
'label' => $this->l('¿Modificar productos que coincidan con su código de referencia de proveedor?'),
'name' => self::PREF_FEED_DUPLICATE,
'class' => 't',
'required' => true,
'is_bool' => true,
'values' => array(
array(
'id' => 'image',
'value' => 0
),
array(
'id' => 'image',
'value' => 1
)
)
),
array(
'type' => 'switch',
'label' => $this->l('Cuando un producto ya exista al importarlo, ¿Establecer el stock a 1?'),
'name' => self::PREF_FEED_STOCK,
'class' => 't',
'required' => true,
'is_bool' => true,
'values' => array(
array(
'id' => 'stock',
'value' => 0,
//'label' => $this->l('Select to update all stock imported to increment in 1')
),
array(
'id' => 'stock',
'value' => 1,
//'label' => $this->l('Select to update all stock imported to 1')
)
)
),
array(
'type' => 'switch',
'label' => $this->l('¿Desea importar las imágenes?'),
'name' => self::PREF_FEED_IMPORT_IMAGES,
'class' => 't',
'required' => true,
'is_bool' => true,
'values' => array(
array(
'id' => 'image',
'value' => 0,
),
array(
'id' => 'image',
'value' => 1,
)
)
),
array(
'type' => 'switch',
'label' => $this->l('Cuando un producto ya exista al importarlo, ¿Desea mantener las imágenes?'),
'name' => self::PREF_FEED_IMAGES,
'class' => 't',
'required' => true,
'is_bool' => true,
'values' => array(
array(
'id' => 'image',
'value' => 0,
//'label' => $this->l('Si las borra')
),
array(
'id' => 'image',
'value' => 1,
//'label' => $this->l('No las borra')
)
)
)
)
);
// Main properties
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
$helper->title = $this->displayName;
$helper->submit_action = 'submit'.$this->name;
// Language
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
$this->context->controller->addCSS($this->_path .
'views/css/datamaster.admin.css', 'all');
// Current values
$helper->fields_value[self::PREF_FEED_IMPORT_IMAGES] = 1;
return $helper->generateForm($fields_form);
}
}