1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2015 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
*/
|
8
|
|
|
class OfferIterator extends CDataProviderIterator
|
9
|
|
|
{
|
10
|
|
|
/**
|
11
|
|
|
* @var RetailCrmProductList $productList
|
12
|
|
|
*/
|
13
|
|
|
public $productList;
|
14
|
|
|
|
15
|
|
|
public $buildCategoryCallback;
|
16
|
|
|
|
17
|
|
|
public function current()
|
18
|
|
|
{
|
19
|
|
|
/**
|
20
|
|
|
* @var Product $product
|
21
|
|
|
*/
|
22
|
|
|
$product = parent::current();
|
23
|
|
|
|
24
|
|
|
$offers = array();
|
25
|
|
|
|
26
|
|
|
if( $basketParameter = $product->getBasketParameter(true) )
|
27
|
|
|
{
|
28
|
|
|
foreach($basketParameter->getParameters() as $parameter)
|
29
|
|
|
{
|
30
|
|
|
$offers[] = $this->buildOfferByParameter($product, $parameter);
|
31
|
|
|
}
|
32
|
|
|
}
|
33
|
|
|
else
|
34
|
|
|
{
|
35
|
|
|
$offers = array($this->getOffer($product));
|
36
|
|
|
}
|
37
|
|
|
|
38
|
|
|
$product->detachBehaviors();
|
39
|
|
|
|
40
|
|
|
return $offers;
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
protected function getOffer(Product $product)
|
44
|
|
|
{
|
45
|
|
|
$offer = $this->buildOffer($product);
|
46
|
|
|
|
47
|
|
|
$product->detachBehaviors();
|
48
|
|
|
|
49
|
|
|
return $offer;
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
protected function buildOffer(Product $product)
|
53
|
|
|
{
|
54
|
|
|
$parent = $product;
|
55
|
|
|
|
56
|
|
|
$offer = array(
|
57
|
|
|
'id' => $product->id,
|
58
|
|
|
'xmlId' => $product->id,
|
59
|
|
|
'productId' => $parent->id,
|
60
|
|
|
'price' => round($product->getPrice()),
|
61
|
|
|
'url' => $this->getUrl($product),
|
62
|
|
|
'categories' => $this->buildCategory($parent),
|
63
|
|
|
'name' => XmlHelper::escape($product->getHeader()),
|
64
|
|
|
'productName' =>XmlHelper::escape($product->getHeader()),
|
65
|
|
|
'picture' => $product->image ? Yii::app()->request->hostInfo.$product->image : '',
|
66
|
|
|
'vendor' => isset($parent->category) ? $parent->category->name : '',
|
67
|
|
|
'quantity' => $product->dump == 1 ? 1000 : 0,
|
68
|
|
|
);
|
69
|
|
|
|
70
|
|
|
$offer['params'] = $this->getParameters($product, $product->getParameters());
|
71
|
|
|
|
72
|
|
|
return $offer;
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
protected function buildOfferByParameter(Product $product, ProductParameter $parameter)
|
76
|
|
|
{
|
77
|
|
|
$id = $product->id.'_'.$parameter->id;
|
78
|
|
|
$offer = array(
|
79
|
|
|
'id' => $id,
|
80
|
|
|
'xmlId' => $id,
|
81
|
|
|
'productId' => $product->id,
|
82
|
|
|
'price' => round(PriceHelper::isNotEmpty($parameter->price) ? $parameter->price : $product->price),
|
83
|
|
|
'url' => $this->getUrl($product),
|
84
|
|
|
'categories' => $this->buildCategory($product),
|
85
|
|
|
'name' => XmlHelper::escape($product->getHeader()." (".$parameter->parameterName->name.": ".$parameter->variant->name.")"),
|
86
|
|
|
'productName' =>XmlHelper::escape($product->getHeader()),
|
87
|
|
|
'picture' => $product->image ? Yii::app()->request->hostInfo.$product->image : '',
|
88
|
|
|
'vendor' => isset($product->category) ? $product->category->name : '',
|
89
|
|
|
'quantity' => $product->dump == 1 ? 1000 : 0,
|
90
|
|
|
);
|
91
|
|
|
|
92
|
|
|
$offer['params'] = $this->getParameters($product, $product->getProductOneParameters());
|
93
|
|
|
|
94
|
|
|
return $offer;
|
95
|
|
|
}
|
96
|
|
|
|
97
|
|
|
/**
|
98
|
|
|
* @param Product $product
|
99
|
|
|
* @param ProductParameterName[] $productParameters
|
100
|
|
|
*
|
101
|
|
|
* @return array
|
102
|
|
|
*/
|
103
|
|
|
protected function getParameters(Product $product, $productParameters)
|
104
|
|
|
{
|
105
|
|
|
$parameters = array();
|
106
|
|
|
|
107
|
|
|
if( !empty($product->articul) )
|
108
|
|
|
{
|
109
|
|
|
$parameters['article'] = array(
|
110
|
|
|
'code' => 'article',
|
111
|
|
|
'name' => 'Ариткул',
|
112
|
|
|
'value' => $product->articul
|
113
|
|
|
);
|
114
|
|
|
}
|
115
|
|
|
|
116
|
|
|
if( !empty($product->offer_id) )
|
117
|
|
|
{
|
118
|
|
|
$parameters['offer_id'] = array(
|
119
|
|
|
'code' => 'offer_id',
|
120
|
|
|
'name' => 'OfferId',
|
121
|
|
|
'value' => $product->offer_id
|
122
|
|
|
);
|
123
|
|
|
}
|
124
|
|
|
|
125
|
|
|
foreach($productParameters as $parameter)
|
126
|
|
|
{
|
127
|
|
|
$code = 'param_id_'.$parameter->id;
|
128
|
|
|
$parameters[$code] = array(
|
129
|
|
|
'code' => $code,
|
130
|
|
|
'name' => $parameter->name,
|
131
|
|
|
'value' => $parameter->value
|
132
|
|
|
);
|
133
|
|
|
}
|
134
|
|
|
|
135
|
|
|
return $parameters;
|
136
|
|
|
}
|
137
|
|
|
|
138
|
|
|
public function getPictureRetailCrm($product, $pictures)
|
139
|
|
|
{
|
140
|
|
|
$picture = Arr::get($pictures, $product->id);
|
141
|
|
|
|
142
|
|
|
if( !$picture )
|
143
|
|
|
{
|
144
|
|
|
if( $product->parent > 0 )
|
145
|
|
|
$picture = Arr::get($pictures, $product->parent);
|
146
|
|
|
}
|
147
|
|
|
|
148
|
|
|
return $picture;
|
149
|
|
|
}
|
150
|
|
|
|
151
|
|
|
protected function buildCategory(Product $product)
|
152
|
|
|
{
|
153
|
|
|
$categoryIds = call_user_func($this->buildCategoryCallback, $product);
|
154
|
|
|
|
155
|
|
|
return $categoryIds;
|
156
|
|
|
}
|
157
|
|
|
|
158
|
|
|
/**
|
159
|
|
|
* @param Product $product
|
160
|
|
|
*
|
161
|
|
|
* @return bool
|
162
|
|
|
*/
|
163
|
|
|
protected function getUrl(Product $product)
|
164
|
|
|
{
|
165
|
|
|
return $product->getUrl(true);
|
166
|
|
|
}
|
167
|
|
|
} |