forked from myjavawork/bb
/
Proxy.php
242 lines (188 loc) · 7.15 KB
/
Proxy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/*
* @copyright Copyright (c) 2013 by ESS-UA.
*/
class Ess_M2ePro_Model_Amazon_Order_Proxy extends Ess_M2ePro_Model_Order_Proxy
{
/** @var $order Ess_M2ePro_Model_Amazon_Order */
protected $order = NULL;
// ########################################
/**
* @param Ess_M2ePro_Model_Order_Item_Proxy[] $items
* @return Ess_M2ePro_Model_Order_Item_Proxy[]
* @throws Exception
*/
protected function mergeItems(array $items)
{
foreach ($items as $key => $item) {
if ($item->getPrice() <= 0) {
unset($items[$key]);
}
}
if (count($items) == 0) {
throw new Exception('Every item in order has zero price.');
}
return parent::mergeItems($items);
}
// ########################################
public function getBillingAddressData()
{
if ($this->order->getAmazonAccount()->isMagentoOrdersBillingAddressSameAsShipping()) {
return parent::getBillingAddressData();
}
if ($this->order->getShippingAddress()->hasSameBuyerAndRecipient()) {
return parent::getBillingAddressData();
}
$customerNameParts = $this->getNameParts($this->order->getBuyerName());
return array(
'firstname' => $customerNameParts['firstname'],
'lastname' => $customerNameParts['lastname'],
'country_id' => '',
'region' => '',
'region_id' => '',
'city' => 'The Amazon does not supply the complete billing buyer information.',
'postcode' => '',
'street' => array(),
'company' => ''
);
}
public function shouldIgnoreBillingAddressValidation()
{
if ($this->order->getAmazonAccount()->isMagentoOrdersBillingAddressSameAsShipping()) {
return false;
}
if ($this->order->getShippingAddress()->hasSameBuyerAndRecipient()) {
return false;
}
return true;
}
// ########################################
public function getCheckoutMethod()
{
if ($this->order->getAmazonAccount()->isMagentoOrdersCustomerPredefined() ||
$this->order->getAmazonAccount()->isMagentoOrdersCustomerNew()) {
return self::CHECKOUT_REGISTER;
}
return self::CHECKOUT_GUEST;
}
public function getBuyerEmail()
{
return $this->order->getBuyerEmail();
}
public function getCustomer()
{
$customer = Mage::getModel('customer/customer');
if ($this->order->getAmazonAccount()->isMagentoOrdersCustomerPredefined()) {
$customer->load($this->order->getAmazonAccount()->getMagentoOrdersCustomerId());
if (is_null($customer->getId())) {
throw new Exception('Customer with ID specified in Amazon account settings does not exist.');
}
}
if ($this->order->getAmazonAccount()->isMagentoOrdersCustomerNew()) {
$customerInfo = $this->getAddressData();
$customer->setWebsiteId($this->order->getAmazonAccount()->getMagentoOrdersCustomerNewWebsiteId());
$customer->loadByEmail($customerInfo['email']);
if (!is_null($customer->getId())) {
return $customer;
}
$customerInfo['website_id'] = $this->order->getAmazonAccount()->getMagentoOrdersCustomerNewWebsiteId();
$customerInfo['group_id'] = $this->order->getAmazonAccount()->getMagentoOrdersCustomerNewGroupId();
// $customerInfo['is_subscribed'] = $this->order->getAmazonAccount()->isMagentoOrdersCustomerNewSubscribed();
/** @var $customerBuilder Ess_M2ePro_Model_Magento_Customer */
$customerBuilder = Mage::getModel('M2ePro/Magento_Customer')->setData($customerInfo);
$customerBuilder->buildCustomer();
$customer = $customerBuilder->getCustomer();
// if ($this->order->getAmazonAccount()->isMagentoOrdersCustomerNewNotifyWhenCreated()) {
// $customer->sendNewAccountEmail();
// }
}
return $customer;
}
public function getCurrency()
{
return $this->order->getCurrency();
}
public function getPaymentData()
{
$paymentData = array(
'method' => Mage::getSingleton('M2ePro/Magento_Payment')->getCode(),
'component_mode' => Ess_M2ePro_Helper_Component_Amazon::NICK,
'payment_method' => '',
'channel_order_id' => $this->order->getAmazonOrderId(),
'channel_final_fee' => 0,
'transactions' => array()
);
return $paymentData;
}
public function getShippingData()
{
return array(
'shipping_method' => $this->order->getShippingService(),
'shipping_price' => $this->getBaseShippingPrice(),
'carrier_title' => Mage::helper('M2ePro')->__('Amazon Shipping')
);
}
protected function getShippingPrice()
{
$price = $this->order->getShippingPrice();
if ($this->isTaxModeNone() && !$this->isShippingPriceIncludesTax()) {
$taxAmount = Mage::getSingleton('tax/calculation')
->calcTaxAmount($price, $this->getTaxRate(), false, false);
$price += $taxAmount;
}
return $price;
}
// ########################################
public function getTaxRate()
{
$paidAmount = $this->order->getPaidAmount();
$taxAmount = $this->order->getTaxAmount();
if ($paidAmount == 0 || $taxAmount == 0) {
return 0;
}
if ($paidAmount == $taxAmount) {
// impossible in real world, but this check is required to prevent possible division by zero
return 0;
}
return round(100 / (($paidAmount - $taxAmount) / $taxAmount), 4);
}
public function hasVat()
{
return $this->order->getTaxAmount() == 0;
}
public function hasTax()
{
return $this->order->getTaxAmount() != 0;
}
public function isShippingPriceIncludesTax()
{
return $this->order->getTaxAmount() == 0;
}
public function isTaxModeNone()
{
return $this->order->getAmazonAccount()->isMagentoOrdersTaxModeNone();
}
public function isTaxModeMagento()
{
return $this->order->getAmazonAccount()->isMagentoOrdersTaxModeMagento();
}
public function isTaxModeChannel()
{
return $this->order->getAmazonAccount()->isMagentoOrdersTaxModeChannel();
}
public function getChannelComments()
{
$comments = array();
if ($this->order->getDiscountAmount() != 0) {
$discount = Mage::getSingleton('M2ePro/Currency')
->formatPrice($this->getCurrency(), $this->order->getDiscountAmount());
$comment = Mage::helper('M2ePro')->__(
'%s promotion discount amount were subtracted from the Total Amount.', $discount
);
$comment .= '<br />';
$comments[] = $comment;
}
return $comments;
}
// ########################################
}