1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\SalesBundle\ImportExport\Strategy; |
4
|
|
|
|
5
|
|
|
use Oro\Bundle\AddressBundle\Entity\Address; |
6
|
|
|
use Oro\Bundle\ImportExportBundle\Strategy\Import\ConfigurableAddOrReplaceStrategy; |
7
|
|
|
|
8
|
|
|
use OroCRM\Bundle\SalesBundle\Entity\B2bCustomer; |
9
|
|
|
|
10
|
|
|
class B2bConfigurableAddOrReplaceStrategy extends ConfigurableAddOrReplaceStrategy |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Save state about exists Billing Address value in entity or not. |
14
|
|
|
* Value is updated in function beforeProcessEntity and used |
15
|
|
|
* in function afterProcessEntity |
16
|
|
|
* |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
protected $isBillingAddress = true; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Save state about exists Shipping Address value in entity or not. |
23
|
|
|
* Value is updated in function beforeProcessEntity and used |
24
|
|
|
* in function afterProcessEntity |
25
|
|
|
* |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected $isShippingAddress = true; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected function beforeProcessEntity($entity) |
34
|
|
|
{ |
35
|
|
|
/** @var B2bCustomer $entity */ |
36
|
|
|
$entity = parent::beforeProcessEntity($entity); |
37
|
|
|
$this->checkEmptyAddresses($entity); |
38
|
|
|
|
39
|
|
|
return $entity; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
protected function afterProcessEntity($entity) |
46
|
|
|
{ |
47
|
|
|
/** @var B2bCustomer $entity */ |
48
|
|
|
$entity = parent::afterProcessEntity($entity); |
49
|
|
|
$this->clearEmptyAddresses($entity); |
50
|
|
|
|
51
|
|
|
$this->guessRegion($entity->getBillingAddress()); |
52
|
|
|
$this->guessRegion($entity->getShippingAddress()); |
53
|
|
|
|
54
|
|
|
return $entity; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param B2bCustomer $entity |
59
|
|
|
*/ |
60
|
|
|
protected function checkEmptyAddresses(B2bCustomer $entity) |
61
|
|
|
{ |
62
|
|
|
if (!$entity->getBillingAddress()) { |
63
|
|
|
$this->isBillingAddress = false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!$entity->getShippingAddress()) { |
67
|
|
|
$this->isShippingAddress = false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param B2bCustomer $entity |
73
|
|
|
*/ |
74
|
|
|
protected function clearEmptyAddresses(B2bCustomer $entity) |
75
|
|
|
{ |
76
|
|
|
if (!$this->isBillingAddress) { |
77
|
|
|
$entity->setBillingAddress(null); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!$this->isShippingAddress) { |
81
|
|
|
$entity->setShippingAddress(null); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param Address $address |
87
|
|
|
*/ |
88
|
|
|
protected function guessRegion($address) |
89
|
|
|
{ |
90
|
|
|
if ($address |
91
|
|
|
&& $address->getCountry() && $address->getRegionText() |
92
|
|
|
&& !$address->getRegion() |
93
|
|
|
) { |
94
|
|
|
$region = $this->databaseHelper->findOneBy( |
95
|
|
|
'Oro\Bundle\AddressBundle\Entity\Region', |
96
|
|
|
[ |
97
|
|
|
'country' => $address->getCountry(), |
98
|
|
|
'name' => $address->getRegionText() |
99
|
|
|
] |
100
|
|
|
); |
101
|
|
|
if ($region) { |
102
|
|
|
$address->setRegion($region); |
103
|
|
|
$address->setRegionText(null); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|