This repository was archived by the owner on Apr 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathUpgradeData.php
122 lines (112 loc) · 5.1 KB
/
UpgradeData.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
<?php
/**
* @category ClassyLlama
* @copyright Copyright (c) 2017 Classy Llama Studios, LLC
*/
namespace ClassyLlama\AvaTax\Setup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
class UpgradeData implements UpgradeDataInterface
{
/**
* Define connection name to connect to 'sales' database on split database install; falls back to default for a
* conventional install
* @var string
*/
private static $connectionName = 'sales';
/**
* Upgrade scripts
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
$setup->startSetup();
if (version_compare($context->getVersion(), '1.0.0', '<' )) {
// Only copy data and drop columns from "sales_invoice" if the columns exist
$tableName = $setup->getTable('sales_invoice');
if (
$setup->getConnection(self::$connectionName)
->tableColumnExists($tableName, 'avatax_is_unbalanced')
&& $setup->getConnection(self::$connectionName)
->tableColumnExists($tableName, 'base_avatax_tax_amount')
) {
// Copy any existing AvaTax data from core Invoice table into new AvaTax Invoice table
$select = $setup->getConnection(self::$connectionName)->select()
->from(
$setup->getTable('sales_invoice'),
[
'entity_id',
'avatax_is_unbalanced',
'base_avatax_tax_amount'
])
->where('base_avatax_tax_amount IS NOT NULL OR avatax_is_unbalanced IS NOT NULL');
$select = $setup->getConnection(self::$connectionName)->insertFromSelect(
$select,
$setup->getTable('avatax_sales_invoice'),
[
'parent_id',
'is_unbalanced',
'base_avatax_tax_amount'
]
);
$setup->getConnection(self::$connectionName)->query($select);
// Drop "avatax_is_unbalanced" column from "sales_invoice" table
$setup->getConnection(self::$connectionName)
->dropColumn(
$setup->getTable('sales_invoice'),
'avatax_is_unbalanced'
);
// Drop "base_avatax_tax_amount" column from "sales_invoice" table
$setup->getConnection(self::$connectionName)
->dropColumn(
$setup->getTable('sales_invoice'),
'base_avatax_tax_amount'
);
}
// Only copy data and drop columns from "sales_creditmemo" if the columns exist
$tableName = $setup->getTable('sales_creditmemo');
if (
$setup->getConnection(self::$connectionName)
->tableColumnExists($tableName, 'avatax_is_unbalanced')
&& $setup->getConnection(self::$connectionName)
->tableColumnExists($tableName, 'base_avatax_tax_amount')
) {
// Copy any existing AvaTax data from core Credit Memo table into new AvaTax Credit Memo table
$select = $setup->getConnection(self::$connectionName)->select()
->from(
$setup->getTable('sales_creditmemo'),
[
'entity_id',
'avatax_is_unbalanced',
'base_avatax_tax_amount'
])
->where('base_avatax_tax_amount IS NOT NULL OR avatax_is_unbalanced IS NOT NULL');
$select = $setup->getConnection(self::$connectionName)->insertFromSelect(
$select,
$setup->getTable('avatax_sales_creditmemo'),
[
'parent_id',
'is_unbalanced',
'base_avatax_tax_amount'
]
);
$setup->getConnection(self::$connectionName)->query($select);
// Drop "avatax_is_unbalanced" column from "sales_creditmemo" table
$setup->getConnection(self::$connectionName)
->dropColumn(
$setup->getTable('sales_creditmemo'),
'avatax_is_unbalanced'
);
// Drop "base_avatax_tax_amount" column from "sales_creditmemo" table
$setup->getConnection(self::$connectionName)
->dropColumn(
$setup->getTable('sales_creditmemo'),
'base_avatax_tax_amount'
);
}
}
$setup->endSetup();
}
}