1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magefix\Fixture\Builder; |
4
|
|
|
|
5
|
|
|
use Mage; |
6
|
|
|
use Mage_Customer_Model_Group; |
7
|
|
|
use Magefix\Exceptions\UndefinedAttributes; |
8
|
|
|
use Magefix\Exceptions\UndefinedQuoteAddresses; |
9
|
|
|
use Magefix\Exceptions\UndefinedQuoteProducts; |
10
|
|
|
use Magefix\Exceptions\UnknownQuoteAddressType; |
11
|
|
|
use Magefix\Fixture\Builder\Helper\Checkout; |
12
|
|
|
use Magefix\Fixture\Builder\Helper\QuoteCustomer; |
13
|
|
|
use Magefix\Fixture\Builder\Helper\ShippingAddress; |
14
|
|
|
use Magefix\Fixture\Builder\Helper\ShippingMethod; |
15
|
|
|
use Magefix\Fixture\Factory\Builder; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class SalesOrder |
19
|
|
|
* |
20
|
|
|
* @package Magefix\Fixture\Builder |
21
|
|
|
* @author Carlo Tasca <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class SalesOrder extends AbstractBuilder |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $_quoteProducts = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @throws UndefinedAttributes |
32
|
|
|
* @throws UnknownQuoteAddressType |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
public function build() |
36
|
|
|
{ |
37
|
|
|
$defaultData = $this->_getMageModelData() ? $this->_getMageModelData() : []; |
38
|
|
|
$fixtureData = $this->_getFixtureAttributes(); |
39
|
|
|
$mergedData = array_merge($defaultData, $fixtureData); |
40
|
|
|
|
41
|
|
|
$this->_getMageModel()->setData($mergedData); |
42
|
|
|
$this->_buildQuoteProductFixtures(); |
43
|
|
|
$this->_addProductsToQuote(); |
44
|
|
|
$this->_setCheckoutMethod(); |
45
|
|
|
$this->_addAddressesToQuote(); |
46
|
|
|
$this->_setShippingMethod(); |
47
|
|
|
$this->_setPaymentMethod(); |
48
|
|
|
|
49
|
|
|
return $this->_saveFixture(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @throws UndefinedQuoteProducts |
54
|
|
|
* @throws \Magefix\Fixture\Factory\UndefinedFixtureModel |
55
|
|
|
* |
56
|
|
|
*/ |
57
|
|
|
protected function _buildQuoteProductFixtures() |
58
|
|
|
{ |
59
|
|
|
$this->_throwUndefinedQuoteProductsException(); |
60
|
|
|
$this->_quoteProducts = $this->_buildManySimple('quote_products'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
protected function _addProductsToQuote() |
65
|
|
|
{ |
66
|
|
|
foreach ($this->_quoteProducts as $product) { |
67
|
|
|
$this->_getMageModel()->addProduct($product); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @throws UndefinedQuoteAddresses |
73
|
|
|
* @throws UnknownQuoteAddressType |
74
|
|
|
* |
75
|
|
|
*/ |
76
|
|
|
protected function _addAddressesToQuote() |
77
|
|
|
{ |
78
|
|
|
$this->_throwUndefinedQuoteAddressesException(); |
79
|
|
|
|
80
|
|
|
$addresses = new ShippingAddress($this, $this->_getMageModel(), $this->_data); |
81
|
|
|
$addresses->addToQuote(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function _setShippingMethod() |
85
|
|
|
{ |
86
|
|
|
$this->_validateShippingMethodData(); |
87
|
|
|
|
88
|
|
|
$shippingData = $this->_processFixtureAttributes($this->_data['fixture']['shipping_method']); |
89
|
|
|
|
90
|
|
|
$shippingMethod = new ShippingMethod($this->_getMageModel(), $shippingData); |
91
|
|
|
$shippingMethod->addShippingDataToQuote(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @throws UndefinedAttributes |
96
|
|
|
* |
97
|
|
|
*/ |
98
|
|
|
protected function _validateShippingMethodData() |
99
|
|
|
{ |
100
|
|
|
$this->_throwUndefinedAttributesException( |
101
|
|
|
isset($this->_data['fixture']['shipping_method']['carrier']), |
102
|
|
|
'Sales Order Fixture: Shipping carrier has not been defined. Check fixture yml.' |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$this->_throwUndefinedAttributesException( |
106
|
|
|
isset($this->_data['fixture']['shipping_method']['method']), |
107
|
|
|
'Sales Order Fixture: Shipping method has not been defined. Check fixture yml.' |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$this->_throwUndefinedAttributesException( |
111
|
|
|
isset($this->_data['fixture']['shipping_method']['free_shipping']), |
112
|
|
|
'Sales Order Fixture: Free shipping has not been defined. Check fixture yml.' |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
$this->_throwUndefinedAttributesException( |
116
|
|
|
isset($this->_data['fixture']['shipping_method']['collect_totals']), |
117
|
|
|
'Sales Order Fixture: Collect totals has not been defined. Check fixture yml.' |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$this->_throwUndefinedAttributesException( |
121
|
|
|
isset($this->_data['fixture']['shipping_method']['collect_shipping_rates']), |
122
|
|
|
'Sales Order Fixture: Collect shipping rates has not been defined. Check fixture yml.' |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function _setCheckoutMethod() |
127
|
|
|
{ |
128
|
|
|
$this->_validateCheckoutMethod(); |
129
|
|
|
$checkoutMethodData = $this->_processFixtureAttributes($this->_data['fixture']['checkout']); |
130
|
|
|
$customerData = isset($this->_data['fixture']['checkout']['customer']) ? $this->_processFixtureAttributes( |
131
|
|
|
$this->_data['fixture']['checkout']['customer'] |
132
|
|
|
) : []; |
133
|
|
|
$this->_getMageModel()->setCheckoutMethod($checkoutMethodData['method']); |
134
|
|
|
$this->_setupCheckoutMethodGuest($checkoutMethodData); |
135
|
|
|
$this->_setupCheckoutMethodRegister($checkoutMethodData, $customerData); |
136
|
|
|
$this->_setupCheckoutMethodCustomer($checkoutMethodData, $customerData); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param array $checkoutMethodData |
141
|
|
|
* |
142
|
|
|
*/ |
143
|
|
|
protected function _setupCheckoutMethodGuest(array $checkoutMethodData) |
144
|
|
|
{ |
145
|
|
|
if (Checkout::isGuestCheckout($checkoutMethodData)) { |
146
|
|
|
$customer = new QuoteCustomer($this, $this->_getMageModel(), $checkoutMethodData); |
147
|
|
|
$customer->setupMethodGuest(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param array $customerRegisterData |
153
|
|
|
* @param array $customerData |
154
|
|
|
*/ |
155
|
|
|
protected function _setupCheckoutMethodRegister(array $customerRegisterData, array $customerData) |
156
|
|
|
{ |
157
|
|
|
if (Checkout::isRegisterCheckout($customerRegisterData)) { |
158
|
|
|
$customer = new QuoteCustomer($this, $this->_getMageModel(), $customerData); |
159
|
|
|
$customer->setupMethodRegister(); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param array $customerRegisterData |
165
|
|
|
* @param array $customerData |
166
|
|
|
*/ |
167
|
|
|
protected function _setupCheckoutMethodCustomer(array $customerRegisterData, array $customerData) |
168
|
|
|
{ |
169
|
|
|
if (Checkout::isCustomerCheckout($customerRegisterData)) { |
170
|
|
|
$customers = Builder::buildMany(Builder::CUSTOMER_FIXTURE_TYPE, $this, [$customerData], $this->getHook()); |
171
|
|
|
if (count($customers)) { |
172
|
|
|
$this->getMageModel()->setCustomer($customers[0]); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
protected function _setPaymentMethod() |
178
|
|
|
{ |
179
|
|
|
$this->_validatePaymentMethod(); |
180
|
|
|
$paymentMethodData = $this->_processFixtureAttributes($this->_data['fixture']['payment']); |
181
|
|
|
$this->_importPaymentData($paymentMethodData); |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
protected function _importPaymentData(array $data) |
186
|
|
|
{ |
187
|
|
|
$this->_getMageModel()->getPayment()->importData(['method' => $data['method']]); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return mixed |
192
|
|
|
* @throws Exception |
193
|
|
|
* |
194
|
|
|
*/ |
195
|
|
|
protected function _saveFixture() |
196
|
|
|
{ |
197
|
|
|
$this->_getMageModel()->save(); |
198
|
|
|
$order = Checkout::quoteServiceSubmitAll($this->_getMageModel()); |
199
|
|
|
|
200
|
|
|
return $order->getId(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @throws UndefinedQuoteProducts |
205
|
|
|
* |
206
|
|
|
*/ |
207
|
|
|
protected function _validatePaymentMethod() |
208
|
|
|
{ |
209
|
|
|
if (!isset($this->_data['fixture']['payment']['method'])) { |
210
|
|
|
throw new UndefinedQuoteProducts( |
211
|
|
|
'Sales Order Fixture: Payment method has not been defined. Check fixture yml.' |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @throws UndefinedAttributes |
218
|
|
|
* |
219
|
|
|
*/ |
220
|
|
|
protected function _validateCheckoutMethod() |
221
|
|
|
{ |
222
|
|
|
$this->_throwUndefinedAttributesException( |
223
|
|
|
isset($this->_data['fixture']['checkout']['method']), |
224
|
|
|
'Sales Order Fixture: Checkout method has not been defined. Check fixture yml.' |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @throws UndefinedBundleProducts |
230
|
|
|
*/ |
231
|
|
|
protected function _throwUndefinedQuoteProductsException() |
232
|
|
|
{ |
233
|
|
|
if (!isset($this->_data['fixture']['quote_products']['products'])) { |
234
|
|
|
throw new UndefinedQuoteProducts( |
235
|
|
|
'Sales Order Fixture: Quote products have not been defined. Check fixture yml.' |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @throws UndefinedQuoteAddresses |
242
|
|
|
* |
243
|
|
|
*/ |
244
|
|
|
protected function _throwUndefinedQuoteAddressesException() |
245
|
|
|
{ |
246
|
|
|
if (!isset($this->_data['fixture']['addresses'])) { |
247
|
|
|
throw new UndefinedQuoteAddresses( |
248
|
|
|
'Sales Order Fixture: Quote addresses have not been defined. Check fixture yml.' |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|