|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2018 |
|
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
|
|
|
private $types = []; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Initializes the object |
|
40
|
|
|
* |
|
41
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
|
42
|
|
|
* @param array $mapping Associative list of field position in CSV as key and domain item key as value |
|
43
|
|
|
* @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object Decorated processor |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping, |
|
46
|
|
|
\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null ) |
|
47
|
|
|
{ |
|
48
|
|
|
parent::__construct( $context, $mapping, $object ); |
|
49
|
|
|
|
|
50
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, 'stock/type' ); |
|
51
|
|
|
$search = $manager->createSearch()->setSlice( 0, 0x7fffffff ); |
|
52
|
|
|
|
|
53
|
|
|
foreach( $manager->searchItems( $search ) as $item ) { |
|
54
|
|
|
$this->types[$item->getCode()] = $item->getCode(); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Saves the product stock related data to the storage |
|
61
|
|
|
* |
|
62
|
|
|
* @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items |
|
63
|
|
|
* @param array $data List of CSV fields with position as key and data as value |
|
64
|
|
|
* @return array List of data which hasn't been imported |
|
65
|
|
|
*/ |
|
66
|
|
|
public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data ) |
|
67
|
|
|
{ |
|
68
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'stock' ); |
|
69
|
|
|
$manager->begin(); |
|
70
|
|
|
|
|
71
|
|
|
try |
|
72
|
|
|
{ |
|
73
|
|
|
$map = $this->getMappedChunk( $data, $this->getMapping() ); |
|
74
|
|
|
$items = $this->getStockItems( $product->getCode() ); |
|
75
|
|
|
|
|
76
|
|
|
foreach( $map as $pos => $list ) |
|
77
|
|
|
{ |
|
78
|
|
|
if( !array_key_exists( 'stock.stocklevel', $list ) ) { |
|
79
|
|
|
continue; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$list['stock.productcode'] = $product->getCode(); |
|
83
|
|
|
$list['stock.type'] = trim( isset( $list['stock.type'] ) ? $list['stock.type'] : 'default' ); |
|
84
|
|
|
|
|
85
|
|
|
if( !in_array( $list['stock.type'], $this->types ) ) |
|
86
|
|
|
{ |
|
87
|
|
|
$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $list['stock.type'], 'stock' ); |
|
88
|
|
|
throw new \Aimeos\Controller\Common\Exception( $msg ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if( isset( $list['stock.dateback'] ) && trim( $list['stock.dateback'] ) === '' ) { |
|
92
|
|
|
$list['stock.dateback'] = null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if( trim( $list['stock.stocklevel'] ) === '' ) { |
|
96
|
|
|
$list['stock.stocklevel'] = null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if( ( $item = array_pop( $items ) ) === null ) { |
|
100
|
|
|
$item = $manager->createItem(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$item->fromArray( $list ); |
|
104
|
|
|
$manager->saveItem( $item, false ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$manager->deleteItems( array_keys( $items ) ); |
|
108
|
|
|
|
|
109
|
|
|
$data = $this->getObject()->process( $product, $data ); |
|
110
|
|
|
|
|
111
|
|
|
$manager->commit(); |
|
112
|
|
|
} |
|
113
|
|
|
catch( \Exception $e ) |
|
114
|
|
|
{ |
|
115
|
|
|
$manager->rollback(); |
|
116
|
|
|
throw $e; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $data; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns the stock items for the given product code |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $code Unique product code |
|
127
|
|
|
* @return \Aimeos\MShop\Stock\Item\Iface[] Associative list of stock items |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function getStockItems( $code ) |
|
130
|
|
|
{ |
|
131
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'stock' ); |
|
132
|
|
|
|
|
133
|
|
|
$search = $manager->createSearch(); |
|
134
|
|
|
$search->setConditions( $search->compare( '==', 'stock.productcode', $code ) ); |
|
135
|
|
|
|
|
136
|
|
|
return $manager->searchItems( $search ); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|