Standard   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 456
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 103
c 3
b 0
f 0
dl 0
loc 456
rs 10
wmc 29

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getDescription() 0 3 1
B run() 0 42 7
A getStockItems() 0 11 2
A location() 0 18 1
A importStocks() 0 34 6
A max() 0 19 1
A backup() 0 28 1
A import() 0 28 3
A skip() 0 18 1
A update() 0 46 5
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2025
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Stock\Import\Csv;
12
13
14
/**
15
 * Job controller for CSV stock imports
16
 *
17
 * @package Controller
18
 * @subpackage Jobs
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Jobs\Base
22
	implements \Aimeos\Controller\Jobs\Iface
23
{
24
	/** controller/jobs/stock/import/csv/name
25
	 * Class name of the used stock suggestions scheduler controller implementation
26
	 *
27
	 * Each default job controller can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the controller factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Controller\Jobs\Stock\Import\Csv\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Stock\Import\Csv\Mycsv
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/stock/import/csv/name = Mycsv
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyCsv"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2019.04
55
	 */
56
57
	/** controller/jobs/stock/import/csv/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the stock import CSV job controller
59
	 *
60
	 * Decorators extend the functionality of a class by adding new aspects
61
	 * (e.g. log what is currently done), executing the methods of the underlying
62
	 * class only in certain conditions (e.g. only for logged in users) or
63
	 * modify what is returned to the caller.
64
	 *
65
	 * This option allows you to remove a decorator added via
66
	 * "controller/jobs/common/decorators/default" before they are wrapped
67
	 * around the job controller.
68
	 *
69
	 *  controller/jobs/stock/import/csv/decorators/excludes = array( 'decorator1' )
70
	 *
71
	 * This would remove the decorator named "decorator1" from the list of
72
	 * common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via
73
	 * "controller/jobs/common/decorators/default" to the job controller.
74
	 *
75
	 * @param array List of decorator names
76
	 * @since 2019.04
77
	 * @see controller/jobs/common/decorators/default
78
	 * @see controller/jobs/stock/import/csv/decorators/global
79
	 * @see controller/jobs/stock/import/csv/decorators/local
80
	 */
81
82
	/** controller/jobs/stock/import/csv/decorators/global
83
	 * Adds a list of globally available decorators only to the stock import CSV job controller
84
	 *
85
	 * Decorators extend the functionality of a class by adding new aspects
86
	 * (e.g. log what is currently done), executing the methods of the underlying
87
	 * class only in certain conditions (e.g. only for logged in users) or
88
	 * modify what is returned to the caller.
89
	 *
90
	 * This option allows you to wrap global decorators
91
	 * ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller.
92
	 *
93
	 *  controller/jobs/stock/import/csv/decorators/global = array( 'decorator1' )
94
	 *
95
	 * This would add the decorator named "decorator1" defined by
96
	 * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to the job controller.
97
	 *
98
	 * @param array List of decorator names
99
	 * @since 2019.04
100
	 * @see controller/jobs/common/decorators/default
101
	 * @see controller/jobs/stock/import/csv/decorators/excludes
102
	 * @see controller/jobs/stock/import/csv/decorators/local
103
	 */
104
105
	/** controller/jobs/stock/import/csv/decorators/local
106
	 * Adds a list of local decorators only to the stock import CSV job controller
107
	 *
108
	 * Decorators extend the functionality of a class by adding new aspects
109
	 * (e.g. log what is currently done), executing the methods of the underlying
110
	 * class only in certain conditions (e.g. only for logged in users) or
111
	 * modify what is returned to the caller.
112
	 *
113
	 * This option allows you to wrap local decorators
114
	 * ("\Aimeos\Controller\Jobs\Stock\Import\Csv\Decorator\*") around the job
115
	 * controller.
116
	 *
117
	 *  controller/jobs/stock/import/csv/decorators/local = array( 'decorator2' )
118
	 *
119
	 * This would add the decorator named "decorator2" defined by
120
	 * "\Aimeos\Controller\Jobs\Stock\Import\Csv\Decorator\Decorator2"
121
	 * only to the job controller.
122
	 *
123
	 * @param array List of decorator names
124
	 * @since 2019.04
125
	 * @see controller/jobs/common/decorators/default
126
	 * @see controller/jobs/stock/import/csv/decorators/excludes
127
	 * @see controller/jobs/stock/import/csv/decorators/global
128
	 */
129
130
131
	use \Aimeos\Controller\Jobs\Common\Types;
132
133
134
	/**
135
	 * Returns the localized name of the job.
136
	 *
137
	 * @return string Name of the job
138
	 */
139
	public function getName() : string
140
	{
141
		return $this->context()->translate( 'controller/jobs', 'Stock import CSV' );
142
	}
143
144
145
	/**
146
	 * Returns the localized description of the job.
147
	 *
148
	 * @return string Description of the job
149
	 */
150
	public function getDescription() : string
151
	{
152
		return $this->context()->translate( 'controller/jobs', 'Imports new and updates existing stocks from CSV files' );
153
	}
154
155
156
	/**
157
	 * Executes the job
158
	 *
159
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
160
	 */
161
	public function run()
162
	{
163
		$context = $this->context();
164
		$logger = $context->logger();
165
		$process = $context->process();
166
167
		try
168
		{
169
			$fs = $context->fs( 'fs-import' );
170
			$site = $context->locale()->getSiteItem()->getCode();
171
			$location = $this->location() . '/' . $site;
172
173
			if( $fs->isDir( $location ) === false ) {
174
				return;
175
			}
176
177
			$logger->info( sprintf( 'Started stock import from "%1$s"', $location ), 'import/csv/stock' );
178
179
			$fcn = function( \Aimeos\MShop\ContextIface $context, string $path ) {
180
				$this->import( $context, $path );
181
			};
182
183
			foreach( map( $fs->scan( $location ) )->sort() as $filename )
184
			{
185
				$path = $location . '/' . $filename;
186
187
				if( $filename[0] === '.' || $fs instanceof \Aimeos\Base\Filesystem\DirIface && $fs->isDir( $path ) ) {
188
					continue;
189
				}
190
191
				$process->start( $fcn, [$context, $path] );
192
			}
193
194
			$process->wait();
195
196
			$logger->info( sprintf( 'Finished stock import from "%1$s"', $location ), 'import/csv/stock' );
197
		}
198
		catch( \Exception $e )
199
		{
200
			$logger->error( 'Stock import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/csv/stock' );
201
			$this->mail( 'Stock CSV import error', $e->getMessage() . "\n" . $e->getTraceAsString() );
202
			throw $e;
203
		}
204
	}
205
206
207
	/**
208
	 * Returns the directory for storing imported files
209
	 *
210
	 * @return string Directory for storing imported files
211
	 */
212
	protected function backup() : string
213
	{
214
		/** controller/jobs/stock/import/csv/backup
215
		 * Name of the backup for sucessfully imported files
216
		 *
217
		 * After a CSV file was imported successfully, you can move it to another
218
		 * location, so it won't be imported again and isn't overwritten by the
219
		 * next file that is stored at the same location in the file system.
220
		 *
221
		 * You should use an absolute path to be sure but can be relative path
222
		 * if you absolutely know from where the job will be executed from. The
223
		 * name of the new backup location can contain placeholders understood
224
		 * by the PHP DateTime::format() method (with percent signs prefix) to
225
		 * create dynamic paths, e.g. "backup/%Y-%m-%d" which would create
226
		 * "backup/2000-01-01". For more information about the date() placeholders,
227
		 * please have a look  into the PHP documentation of the
228
		 * {@link https://www.php.net/manual/en/datetime.format.php format() method}.
229
		 *
230
		 * **Note:** If no backup name is configured, the file will be removed!
231
		 *
232
		 * @param integer Name of the backup file, optionally with date/time placeholders
233
		 * @since 2019.04
234
		 * @see controller/jobs/stock/import/csv/location
235
		 * @see controller/jobs/stock/import/csv/max-size
236
		 * @see controller/jobs/stock/import/csv/skip-lines
237
		 */
238
		$backup = $this->context()->config()->get( 'controller/jobs/stock/import/csv/backup' );
239
		return \Aimeos\Base\Str::strtime( (string) $backup );
240
	}
241
242
243
	/**
244
	 * Imports the CSV file given by its path
245
	 *
246
	 * @param \Aimeos\MShop\ContextIface $context Context object
247
	 * @param string $path Relative path to the CSV file in the file system
248
	 */
249
	protected function import( \Aimeos\MShop\ContextIface $context, string $path )
250
	{
251
		$context = $this->context();
252
		$logger = $context->logger();
253
254
		$skiplines = $this->skip();
255
		$fs = $context->fs( 'fs-import' );
256
257
		$logger->info( sprintf( 'Started stock import from file "%1$s"', $path ), 'import/csv/stock' );
258
259
		$fh = $fs->reads( $path );
260
261
		for( $i = 0; $i < $skiplines; $i++ ) {
262
			fgetcsv( $fh, null, ',', '"', '' );
263
		}
264
265
		$this->importStocks( $fh );
266
267
		fclose( $fh );
268
		$this->saveTypes();
269
270
		if( !empty( $backup = $this->backup() ) ) {
271
			$fs->move( $path, $backup );
272
		} else {
273
			$fs->rm( $path );
274
		}
275
276
		$logger->info( sprintf( 'Finished stock import from file "%1$s"', $path ), 'import/csv/stock' );
277
	}
278
279
280
	/**
281
	 * Returns the stock items for the given product IDs and stock types
282
	 *
283
	 * @param array $ids List of product IDs
284
	 * @param array $types List of stock types
285
	 * @return array Multi-dimensional array of code/type/item map
286
	 */
287
	protected function getStockItems( array $ids, array $types ) : array
288
	{
289
		$manager = \Aimeos\MShop::create( $this->context(), 'stock' );
290
		$search = $manager->filter()->add( ['stock.productid' => $ids, 'stock.type' => $types] )->slice( 0, 10000 );
291
292
		$map = [];
293
		foreach( $manager->search( $search ) as $item ) {
294
			$map[$item->getProductId()][$item->getType()] = $item;
295
		}
296
297
		return $map;
298
	}
299
300
301
	/**
302
	 * Imports the CSV data and creates new stocks or updates existing ones
303
	 *
304
	 * @param resource $fhandle File handle for the CSV file to import
305
	 * @return int Number of imported stocks
306
	 */
307
	protected function importStocks( $fhandle ) : int
308
	{
309
		$total = 0;
310
311
		do
312
		{
313
			$count = 0;
314
			$max = $this->max();
315
			$codes = $data = $types = [];
316
317
			while( ( $row = fgetcsv( $fhandle, null, ',', '"', '' ) ) !== false && $count < $max )
318
			{
319
				if( $row[0] === '' ) {
320
					continue;
321
				}
322
323
				$type = $this->val( $row, 2, 'default' );
324
				$types[$type] = null;
325
				$codes[] = $row[0];
326
				$row[2] = $type;
327
				$data[] = $row;
328
329
				$count++;
330
			}
331
332
			if( !empty( $data ) ) {
333
				$this->update( $data, $codes, array_keys( $types ) );
334
			}
335
336
			$total += $count;
337
		}
338
		while( $count > 0 );
339
340
		return $total;
341
	}
342
343
344
	/**
345
	 * Returns the path to the directory with the CSV file
346
	 *
347
	 * @return string Path to the directory with the CSV file
348
	 */
349
	protected function location() : string
350
	{
351
		/** controller/jobs/stock/import/csv/location
352
		 * Directory where the CSV files are stored which should be imported
353
		 *
354
		 * It's the relative path inside the "fs-import" virtual file system
355
		 * configuration. The default location of the "fs-import" file system is:
356
		 *
357
		 * * Laravel: ./storage/import/
358
		 * * TYPO3: /uploads/tx_aimeos/.secure/import/
359
		 *
360
		 * @param string Relative path to the CSV files
361
		 * @since 2019.04
362
		 * @see controller/jobs/stock/import/csv/backup
363
		 * @see controller/jobs/stock/import/csv/max-size
364
		 * @see controller/jobs/stock/import/csv/skip-lines
365
		 */
366
		return (string) $this->context()->config()->get( 'controller/jobs/stock/import/csv/location', 'stock' );
367
	}
368
369
370
	/**
371
	 * Returns the maximum number of CSV rows to import at once
372
	 *
373
	 * @return int Maximum number of CSV rows to import at once
374
	 */
375
	protected function max() : int
376
	{
377
		/** controller/jobs/stock/import/csv/max-size
378
		 * Maximum number of CSV rows to import at once
379
		 *
380
		 * It's more efficient to read and import more than one row at a time
381
		 * to speed up the import. Usually, the bigger the chunk that is imported
382
		 * at once, the less time the importer will need. The downside is that
383
		 * the amount of memory required by the import process will increase as
384
		 * well. Therefore, it's a trade-off between memory consumption and
385
		 * import speed.
386
		 *
387
		 * @param integer Number of rows
388
		 * @since 2019.04
389
		 * @see controller/jobs/stock/import/csv/backup
390
		 * @see controller/jobs/stock/import/csv/location
391
		 * @see controller/jobs/stock/import/csv/skip-lines
392
		 */
393
		return (int) $this->context()->config()->get( 'controller/jobs/stock/import/csv/max-size', 1000 );
394
	}
395
396
397
	/**
398
	 * Returns the number of rows skipped in front of each CSV files
399
	 *
400
	 * @return int Number of rows skipped in front of each CSV files
401
	 */
402
	protected function skip() : int
403
	{
404
		/** controller/jobs/stock/import/csv/skip-lines
405
		 * Number of rows skipped in front of each CSV files
406
		 *
407
		 * Some CSV files contain header information describing the content of
408
		 * the column values. These data is for informational purpose only and
409
		 * can't be imported into the database. Using this option, you can
410
		 * define the number of lines that should be left out before the import
411
		 * begins.
412
		 *
413
		 * @param integer Number of rows
414
		 * @since 2019.04
415
		 * @see controller/jobs/stock/import/csv/backup
416
		 * @see controller/jobs/stock/import/csv/location
417
		 * @see controller/jobs/stock/import/csv/max-size
418
		 */
419
		return (int) $this->context()->config()->get( 'controller/jobs/stock/import/csv/skip-lines', 0 );
420
	}
421
422
423
	/**
424
	 * Updates the stock items
425
	 *
426
	 * @param array $data List of stock entries
427
	 * @param array $codes List of product codes the stock items are associated to
428
	 * @param array $types List of stock types which should be updated
429
	 */
430
	protected function update( array $data, array $codes, array $types )
431
	{
432
		$context = $this->context();
433
		$manager = \Aimeos\MShop::create( $context, 'stock' );
434
		$prodManager = \Aimeos\MShop::create( $context, 'product' );
435
436
		$filter = $prodManager->filter()->add( ['product.code' => $codes] )->slice( 0, count( $codes ) );
437
		$products = $prodManager->search( $filter );
438
		$prodMap = $products->col( null, 'product.code' );
439
440
		$map = $this->getStockItems( $products->keys()->all(), $types );
441
		$items = [];
442
443
		$prodManager->begin();
444
445
		foreach( $data as $entry )
446
		{
447
			$code = $entry[0];
448
			$type = $entry[2];
449
450
			if( ( $product = $prodMap->get( $code ) ) === null ) {
451
				continue;
452
			}
453
454
			$item = $map[$product->getId()][$type] ?? $manager->create();
455
456
			$items[] = $item->setProductId( $product->getId() )->setType( $type )
457
				->setStocklevel( $this->val( $entry, 1 ) )
458
				->setDateBack( $this->val( $entry, 3 ) )
459
				->setTimeframe( $this->val( $entry, 4, '' ) );
460
461
			if( $item->getStockLevel() === null || $item->getStockLevel() > 0 ) {
462
				$prodManager->stock( $product->getId(), 1 );
463
			}
464
465
			$this->addType( 'stock/type', 'product', $type );
466
			unset( $map[$code][$type] );
467
		}
468
469
		$manager->begin();
470
		$manager->save( $items );
471
		$manager->commit();
472
473
		$prodManager->commit();
474
475
		unset( $items );
476
	}
477
}
478