|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2021 |
|
6
|
|
|
* @package Controller |
|
7
|
|
|
* @subpackage Common |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor\Stock; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Product stock processor for CSV imports |
|
16
|
|
|
* |
|
17
|
|
|
* @package Controller |
|
18
|
|
|
* @subpackage Common |
|
19
|
|
|
*/ |
|
20
|
|
|
class Standard |
|
21
|
|
|
extends \Aimeos\Controller\Common\Product\Import\Csv\Processor\Base |
|
22
|
|
|
implements \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface |
|
23
|
|
|
{ |
|
24
|
|
|
/** controller/common/product/import/csv/processor/stock/name |
|
25
|
|
|
* Name of the stock processor implementation |
|
26
|
|
|
* |
|
27
|
|
|
* Use "Myname" if your class is named "\Aimeos\Controller\Common\Product\Import\Csv\Processor\Stock\Myname". |
|
28
|
|
|
* The name is case-sensitive and you should avoid camel case names like "MyName". |
|
29
|
|
|
* |
|
30
|
|
|
* @param string Last part of the processor class name |
|
31
|
|
|
* @since 2015.10 |
|
32
|
|
|
* @category Developer |
|
33
|
|
|
*/ |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Saves the product stock related data to the storage |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items |
|
40
|
|
|
* @param array $data List of CSV fields with position as key and data as value |
|
41
|
|
|
* @return array List of data which hasn't been imported |
|
42
|
|
|
*/ |
|
43
|
|
|
public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data ) : array |
|
44
|
|
|
{ |
|
45
|
|
|
$manager = \Aimeos\MShop::create( $this->getContext(), 'stock' ); |
|
46
|
|
|
$manager->begin(); |
|
47
|
|
|
|
|
48
|
|
|
try |
|
49
|
|
|
{ |
|
50
|
|
|
$stock = 0; |
|
51
|
|
|
$map = $this->getMappedChunk( $data, $this->getMapping() ); |
|
52
|
|
|
$items = $manager->search( $manager->filter()->add( ['stock.productid' => $product->getId()] ) ); |
|
53
|
|
|
|
|
54
|
|
|
foreach( $map as $pos => $list ) |
|
55
|
|
|
{ |
|
56
|
|
|
if( !array_key_exists( 'stock.stocklevel', $list ) ) { |
|
57
|
|
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$list['stock.productid'] = $product->getId(); |
|
61
|
|
|
$list['stock.dateback'] = $this->getValue( $list, 'stock.dateback' ); |
|
62
|
|
|
$list['stock.stocklevel'] = $this->getValue( $list, 'stock.stocklevel' ); |
|
63
|
|
|
$list['stock.type'] = $this->getValue( $list, 'stock.type', 'default' ); |
|
64
|
|
|
|
|
65
|
|
|
$this->addType( 'stock/type', 'product', $list['stock.type'] ); |
|
66
|
|
|
|
|
67
|
|
|
if( ( $item = $items->pop() ) === null ) { |
|
68
|
|
|
$item = $manager->create(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$manager->save( $item->fromArray( $list ), false ); |
|
72
|
|
|
|
|
73
|
|
|
if( $item->getStockLevel() === null || $item->getStockLevel() > 0 ) { |
|
74
|
|
|
$stock = 1; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$manager->delete( $items->toArray() ); |
|
79
|
|
|
$product->inStock( $stock ); |
|
80
|
|
|
|
|
81
|
|
|
$data = $this->getObject()->process( $product, $data ); |
|
82
|
|
|
|
|
83
|
|
|
$manager->commit(); |
|
84
|
|
|
} |
|
85
|
|
|
catch( \Exception $e ) |
|
86
|
|
|
{ |
|
87
|
|
|
$manager->rollback(); |
|
88
|
|
|
throw $e; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $data; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|