|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\SalesBundle\Migrations\Schema\v1_27; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Portability\Connection; |
|
6
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
7
|
|
|
use Doctrine\DBAL\Types\Type; |
|
8
|
|
|
|
|
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
|
11
|
|
|
|
|
12
|
|
|
use Oro\Bundle\MigrationBundle\Migration\OrderedMigrationInterface; |
|
13
|
|
|
use Oro\Bundle\MigrationBundle\Migration\Migration; |
|
14
|
|
|
use Oro\Bundle\MigrationBundle\Migration\ParametrizedSqlMigrationQuery; |
|
15
|
|
|
use Oro\Bundle\MigrationBundle\Migration\QueryBag; |
|
16
|
|
|
|
|
17
|
|
|
use Oro\Bundle\CurrencyBundle\DependencyInjection\Configuration; |
|
18
|
|
|
|
|
19
|
|
|
class UpdateOpportunityCurrency implements |
|
20
|
|
|
Migration, |
|
21
|
|
|
ContainerAwareInterface, |
|
22
|
|
|
OrderedMigrationInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use ContainerAwareTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getOrder() |
|
30
|
|
|
{ |
|
31
|
|
|
return 2; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function up(Schema $schema, QueryBag $queries) |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var Connection $connection */ |
|
40
|
|
|
$connection = $this->container->get('doctrine')->getConnection(); |
|
41
|
|
|
$currencies = $connection->fetchAll(' |
|
42
|
|
|
SELECT |
|
43
|
|
|
oro_organization.id as organization_id, |
|
44
|
|
|
oro_config_value.text_value |
|
45
|
|
|
FROM |
|
46
|
|
|
oro_config |
|
47
|
|
|
INNER JOIN |
|
48
|
|
|
oro_organization |
|
49
|
|
|
ON |
|
50
|
|
|
oro_organization.id = oro_config.record_id |
|
51
|
|
|
INNER JOIN |
|
52
|
|
|
oro_config_value |
|
53
|
|
|
ON |
|
54
|
|
|
oro_config.id = oro_config_value.config_id |
|
55
|
|
|
WHERE |
|
56
|
|
|
oro_config_value.name = \'default_currency\' |
|
57
|
|
|
'); |
|
58
|
|
|
|
|
59
|
|
|
$currencies = array_column($currencies, 'text_value', 'organization_id'); |
|
60
|
|
|
|
|
61
|
|
|
$defaultCurrency = $connection->fetchColumn(' |
|
62
|
|
|
SELECT |
|
63
|
|
|
oro_config_value.text_value |
|
64
|
|
|
FROM |
|
65
|
|
|
oro_config_value |
|
66
|
|
|
INNER JOIN |
|
67
|
|
|
oro_config |
|
68
|
|
|
ON |
|
69
|
|
|
oro_config_value.config_id = oro_config.id |
|
70
|
|
|
WHERE |
|
71
|
|
|
oro_config.entity = ? |
|
72
|
|
|
AND |
|
73
|
|
|
oro_config_value.name = \'default_currency\' |
|
74
|
|
|
', ['app'], 0); |
|
75
|
|
|
|
|
76
|
|
|
if (!$defaultCurrency) { |
|
77
|
|
|
$defaultCurrency = Configuration::DEFAULT_CURRENCY; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->updateOpportunityTable($queries, $currencies, $defaultCurrency); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param QueryBag $queries |
|
85
|
|
|
* @param array $currencies |
|
86
|
|
|
* @param string $defaultCurrency |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function updateOpportunityTable(QueryBag $queries, array $currencies, $defaultCurrency) |
|
89
|
|
|
{ |
|
90
|
|
|
|
|
91
|
|
|
$query = 'UPDATE orocrm_sales_opportunity |
|
92
|
|
|
SET budget_amount_currency = :currency, close_revenue_currency = :currency'; |
|
93
|
|
|
|
|
94
|
|
|
if (!empty($currencies)) { |
|
95
|
|
|
$query .= sprintf('WHERE organization_id |
|
96
|
|
|
NOT IN (%s)', implode(array_keys($currencies), ',')); |
|
97
|
|
|
} |
|
98
|
|
|
$migrationQuery = new ParametrizedSqlMigrationQuery(); |
|
99
|
|
|
|
|
100
|
|
|
$migrationQuery->addSql( |
|
101
|
|
|
$query, |
|
102
|
|
|
['currency' => $defaultCurrency], |
|
103
|
|
|
['currency' => Type::STRING] |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
|
|
$queries->addPostQuery($migrationQuery); |
|
107
|
|
|
|
|
108
|
|
|
foreach ($currencies as $id => $currency) { |
|
109
|
|
|
$query = 'UPDATE orocrm_sales_opportunity |
|
110
|
|
|
SET budget_amount_currency = :currency, close_revenue_currency = :currency |
|
111
|
|
|
WHERE organization_id = :organization_id'; |
|
112
|
|
|
|
|
113
|
|
|
$migrationQuery = new ParametrizedSqlMigrationQuery(); |
|
114
|
|
|
|
|
115
|
|
|
$migrationQuery->addSql( |
|
116
|
|
|
$query, |
|
117
|
|
|
['currency' => $currency, 'organization_id' => $id], |
|
118
|
|
|
['currency' => Type::STRING, 'organization_id' => Type::INTEGER] |
|
119
|
|
|
); |
|
120
|
|
|
$queries->addPostQuery($migrationQuery); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|