Passed
Push — master ( 326a3e...330c3c )
by Aimeos
09:07
created

Standard::update()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 24
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 39
rs 9.2248
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2022
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\Common\Common\Import\Traits;
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
		$location = $this->location();
168
		$fs = $context->fs( 'fs-import' );
169
170
		if( $fs->isDir( $location ) === false ) {
171
			return;
172
		}
173
174
		try
175
		{
176
			$logger->info( sprintf( 'Started stock import from "%1$s"', $location ), 'import/csv/stock' );
177
178
			$fcn = function( \Aimeos\MShop\ContextIface $context, string $path ) {
179
				$this->import( $context, $path );
180
			};
181
182
			foreach( map( $fs->scan( $location ) )->sort() as $filename )
183
			{
184
				$path = $location . '/' . $filename;
185
186
				if( $fs instanceof \Aimeos\Base\Filesystem\DirIface && $fs->isDir( $path ) ) {
187
					continue;
188
				}
189
190
				$process->start( $fcn, [$context, $path] );
191
			}
192
193
			$process->wait();
194
195
			$logger->info( sprintf( 'Finished stock import from "%1$s"', $location ), 'import/csv/stock' );
196
		}
197
		catch( \Exception $e )
198
		{
199
			$logger->error( 'Stock import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/csv/stock' );
200
			$this->mail( 'Stock CSV import error', $e->getMessage() . "\n" . $e->getTraceAsString() );
201
			throw $e;
202
		}
203
	}
204
205
206
	/**
207
	 * Returns the directory for storing imported files
208
	 *
209
	 * @return string Directory for storing imported files
210
	 */
211
	protected function backup() : string
212
	{
213
		/** controller/jobs/stock/import/csv/backup
214
		 * Name of the backup for sucessfully imported files
215
		 *
216
		 * After a CSV file was imported successfully, you can move it to another
217
		 * location, so it won't be imported again and isn't overwritten by the
218
		 * next file that is stored at the same location in the file system.
219
		 *
220
		 * You should use an absolute path to be sure but can be relative path
221
		 * if you absolutely know from where the job will be executed from. The
222
		 * name of the new backup location can contain placeholders understood
223
		 * by the PHP DateTime::format() method (with percent signs prefix) to
224
		 * create dynamic paths, e.g. "backup/%Y-%m-%d" which would create
225
		 * "backup/2000-01-01". For more information about the date() placeholders,
226
		 * please have a look  into the PHP documentation of the
227
		 * {@link https://www.php.net/manual/en/datetime.format.php format() method}.
228
		 *
229
		 * **Note:** If no backup name is configured, the file will be removed!
230
		 *
231
		 * @param integer Name of the backup file, optionally with date/time placeholders
232
		 * @since 2019.04
233
		 * @see controller/jobs/stock/import/csv/location
234
		 * @see controller/jobs/stock/import/csv/max-size
235
		 * @see controller/jobs/stock/import/csv/skip-lines
236
		 */
237
		$backup = $this->context()->config()->get( 'controller/jobs/stock/import/csv/backup' );
238
		return \Aimeos\Base\Str::strtime( (string) $backup );
239
	}
240
241
242
	/**
243
	 * Imports the CSV file given by its path
244
	 *
245
	 * @param \Aimeos\MShop\ContextIface $context Context object
246
	 * @param string $path Relative path to the CSV file in the file system
247
	 */
248
	protected function import( \Aimeos\MShop\ContextIface $context, string $path )
249
	{
250
		$context = $this->context();
251
		$logger = $context->logger();
252
253
		$skiplines = $this->skip();
254
		$fs = $context->fs( 'fs-import' );
255
256
		$logger->info( sprintf( 'Started stock import from file "%1$s"', $path ), 'import/csv/stock' );
257
258
		$fh = $fs->reads( $path );
259
260
		for( $i = 0; $i < $skiplines; $i++ ) {
261
			fgetcsv( $fh );
262
		}
263
264
		$this->importStocks( $fh );
265
266
		fclose( $fh );
267
		$this->saveTypes();
268
269
		if( !empty( $backup = $this->backup() ) ) {
270
			$fs->move( $path, $backup );
271
		} else {
272
			$fs->rm( $path );
273
		}
274
275
		$logger->info( sprintf( 'Finished stock import from file "%1$s"', $path ), 'import/csv/stock' );
276
	}
277
278
279
	/**
280
	 * Returns the stock items for the given product IDs and stock types
281
	 *
282
	 * @param array $ids List of product IDs
283
	 * @param array $types List of stock types
284
	 * @return array Multi-dimensional array of code/type/item map
285
	 */
286
	protected function getStockItems( array $ids, array $types ) : array
287
	{
288
		$manager = \Aimeos\MShop::create( $this->context(), 'stock' );
289
		$search = $manager->filter()->add( ['stock.productid' => $ids, 'stock.type' => $types] )->slice( 0, 10000 );
290
291
		$map = [];
292
		foreach( $manager->search( $search ) as $item ) {
293
			$map[$item->getProductId()][$item->getType()] = $item;
294
		}
295
296
		return $map;
297
	}
298
299
300
	/**
301
	 * Imports the CSV data and creates new stocks or updates existing ones
302
	 *
303
	 * @param resource $fhandle File handle for the CSV file to import
304
	 * @return int Number of imported stocks
305
	 */
306
	protected function importStocks( $fhandle ) : int
307
	{
308
		$total = 0;
309
310
		do
311
		{
312
			$count = 0;
313
			$max = $this->max();
314
			$codes = $data = $types = [];
315
316
			while( ( $row = fgetcsv( $fhandle ) ) !== false && $count < $max )
317
			{
318
				if( $row[0] === '' ) {
319
					continue;
320
				}
321
322
				$type = $this->val( $row, 2, 'default' );
323
				$types[$type] = null;
324
				$codes[] = $row[0];
325
				$row[2] = $type;
326
				$data[] = $row;
327
328
				$count++;
329
			}
330
331
			if( !empty( $data ) ) {
332
				$this->update( $data, $codes, array_keys( $types ) );
333
			}
334
335
			$total += $count;
336
		}
337
		while( $count > 0 );
338
339
		return $total;
340
	}
341
342
343
	/**
344
	 * Returns the path to the directory with the CSV file
345
	 *
346
	 * @return string Path to the directory with the CSV file
347
	 */
348
	protected function location() : string
349
	{
350
		/** controller/jobs/stock/import/csv/location
351
		 * File or directory where the content is stored which should be imported
352
		 *
353
		 * You need to configure the CSV file or directory with the CSV files that
354
		 * should be imported. It should be an absolute path to be sure but can be
355
		 * relative path if you absolutely know from where the job will be executed
356
		 * from.
357
		 *
358
		 * @param string Relative path to the CSV files
359
		 * @since 2019.04
360
		 * @see controller/jobs/stock/import/csv/backup
361
		 * @see controller/jobs/stock/import/csv/max-size
362
		 * @see controller/jobs/stock/import/csv/skip-lines
363
		 */
364
		return (string) $this->context()->config()->get( 'controller/jobs/stock/import/csv/location', 'stock' );
365
	}
366
367
368
	/**
369
	 * Returns the maximum number of CSV rows to import at once
370
	 *
371
	 * @return int Maximum number of CSV rows to import at once
372
	 */
373
	protected function max() : int
374
	{
375
		/** controller/jobs/stock/import/csv/max-size
376
		 * Maximum number of CSV rows to import at once
377
		 *
378
		 * It's more efficient to read and import more than one row at a time
379
		 * to speed up the import. Usually, the bigger the chunk that is imported
380
		 * at once, the less time the importer will need. The downside is that
381
		 * the amount of memory required by the import process will increase as
382
		 * well. Therefore, it's a trade-off between memory consumption and
383
		 * import speed.
384
		 *
385
		 * @param integer Number of rows
386
		 * @since 2019.04
387
		 * @see controller/jobs/stock/import/csv/backup
388
		 * @see controller/jobs/stock/import/csv/location
389
		 * @see controller/jobs/stock/import/csv/skip-lines
390
		 */
391
		return (int) $this->context()->config()->get( 'controller/jobs/stock/import/csv/max-size', 1000 );
392
	}
393
394
395
	/**
396
	 * Returns the number of rows skipped in front of each CSV files
397
	 *
398
	 * @return int Number of rows skipped in front of each CSV files
399
	 */
400
	protected function skip() : int
401
	{
402
		/** controller/jobs/stock/import/csv/skip-lines
403
		 * Number of rows skipped in front of each CSV files
404
		 *
405
		 * Some CSV files contain header information describing the content of
406
		 * the column values. These data is for informational purpose only and
407
		 * can't be imported into the database. Using this option, you can
408
		 * define the number of lines that should be left out before the import
409
		 * begins.
410
		 *
411
		 * @param integer Number of rows
412
		 * @since 2019.04
413
		 * @see controller/jobs/stock/import/csv/backup
414
		 * @see controller/jobs/stock/import/csv/location
415
		 * @see controller/jobs/stock/import/csv/max-size
416
		 */
417
		return (int) $this->context()->config()->get( 'controller/jobs/stock/import/csv/skip-lines', 0 );
418
	}
419
420
421
	/**
422
	 * Updates the stock items
423
	 *
424
	 * @param array $data List of stock entries
425
	 * @param array $codes List of product codes the stock items are associated to
426
	 * @param array $types List of stock types which should be updated
427
	 */
428
	protected function update( array $data, array $codes, array $types )
429
	{
430
		$context = $this->context();
431
		$manager = \Aimeos\MShop::create( $context, 'stock' );
432
		$prodManager = \Aimeos\MShop::create( $context, 'product' );
433
434
		$filter = $prodManager->filter()->add( ['product.code' => $codes] )->slice( 0, count( $codes ) );
435
		$products = $prodManager->search( $filter );
436
		$prodMap = $products->col( null, 'product.code' );
437
438
		$map = $this->getStockItems( $products->keys()->all(), $types );
439
		$items = [];
440
441
		foreach( $data as $entry )
442
		{
443
			$code = $entry[0];
444
			$type = $entry[2];
445
446
			if( ( $product = $prodMap->get( $code ) ) === null ) {
447
				continue;
448
			}
449
450
			$item = $map[$product->getId()][$type] ?? $manager->create();
451
452
			$items[] = $item->setProductId( $product->getId() )->setType( $type )
453
				->setStocklevel( $this->val( $entry, 1 ) )
454
				->setDateBack( $this->val( $entry, 3 ) );
455
456
			if( $item->getStockLevel() === null || $item->getStockLevel() > 0 ) {
457
				$product->setInStock( 1 );
458
			}
459
460
			$this->addType( 'stock/type', 'product', $type );
461
			unset( $map[$code][$type] );
462
		}
463
464
		$prodManager->save( $products );
465
		$manager->save( $items );
466
		unset( $items );
467
	}
468
}
469