1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\OrdersAdmin\Tasks; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DB; |
6
|
|
|
use SilverStripe\Control\Director; |
7
|
|
|
use SilverStripe\Dev\MigrationTask; |
8
|
|
|
use SilverStripe\ORM\DatabaseAdmin; |
9
|
|
|
use SilverStripe\Control\Controller; |
10
|
|
|
use SilverCommerce\GeoZones\Model\Zone; |
11
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
12
|
|
|
use SilverStripe\Subsites\Model\Subsite; |
|
|
|
|
13
|
|
|
use SilverCommerce\OrdersAdmin\Model\Invoice; |
14
|
|
|
use SilverCommerce\OrdersAdmin\Model\Estimate; |
15
|
|
|
use SilverCommerce\OrdersAdmin\Model\LineItem; |
16
|
|
|
use SilverCommerce\OrdersAdmin\Model\LineItemCustomisation; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Task to handle migrating orders/items to newer versions |
20
|
|
|
*/ |
21
|
|
|
class OrdersMigrationTask extends MigrationTask |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Should this task be invoked automatically via dev/build? |
26
|
|
|
* |
27
|
|
|
* @config |
28
|
|
|
* |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private static $run_during_dev_build = true; |
|
|
|
|
32
|
|
|
|
33
|
|
|
private static $segment = 'OrdersMigrationTask'; |
|
|
|
|
34
|
|
|
|
35
|
|
|
protected $description = "Upgrade Orders/Items"; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Run this task |
39
|
|
|
* |
40
|
|
|
* @param HTTPRequest $request The current request |
|
|
|
|
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function run($request) |
45
|
|
|
{ |
46
|
|
|
if ($request->getVar('direction') == 'down') { |
47
|
|
|
$this->down(); |
48
|
|
|
} else { |
49
|
|
|
$this->up(); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function up() |
57
|
|
|
{ |
58
|
|
|
// Migrate estimate/invoice numbers to new ref field |
59
|
|
|
$this->log('- Migrating estimate/invoice numbers'); |
60
|
|
|
$total = 0; |
|
|
|
|
61
|
|
|
|
62
|
|
|
if (class_exists(Subsite::class)) { |
63
|
|
|
$items = Subsite::get_from_all_subsites(Estimate::class); |
64
|
|
|
} else { |
65
|
|
|
$items = Estimate::get(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$count = false; |
69
|
|
|
if ($items) { |
70
|
|
|
$this->log('- '.$items->count().' items to convert.'); |
71
|
|
|
$count = $items->count(); |
72
|
|
|
} |
73
|
|
|
$i = 0; |
74
|
|
|
|
75
|
|
|
foreach ($items as $item) { |
76
|
|
|
if ($item->Number !== null) { |
77
|
|
|
$config = SiteConfig::current_site_config(); |
78
|
|
|
$inv_prefix = $config->InvoiceNumberPrefix; |
79
|
|
|
$est_prefix = $config->EstimateNumberPrefix; |
80
|
|
|
$number = $item->Number; |
81
|
|
|
|
82
|
|
|
// Strip off current prefix and convert to a ref |
83
|
|
|
if ($item instanceof Invoice) { |
84
|
|
|
$ref = str_replace($inv_prefix . "-", "", $number); |
85
|
|
|
$ref = str_replace('-', '', $ref); |
86
|
|
|
$item->Ref = (int)$ref; |
|
|
|
|
87
|
|
|
$item->Prefix = $inv_prefix; |
|
|
|
|
88
|
|
|
} else { |
89
|
|
|
$ref = str_replace($est_prefix . "-", "", $number); |
90
|
|
|
$ref = str_replace('-', '', $ref); |
91
|
|
|
$item->Ref = (int)$ref; |
92
|
|
|
$item->Prefix = $est_prefix; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$item->Number = null; |
|
|
|
|
96
|
|
|
$item->write(); |
97
|
|
|
} |
98
|
|
|
$i++; |
99
|
|
|
$this->log('- '.$i.'/'.$count.' items migrated.', true); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
unset($items); |
103
|
|
|
|
104
|
|
|
// Change price/tax on line items to use new fields from extension |
105
|
|
|
$items = LineItem::get(); |
106
|
|
|
$count = $items->count(); |
107
|
|
|
$this->log("Migrating {$count} Line Items"); |
108
|
|
|
$i = 0; |
109
|
|
|
|
110
|
|
|
foreach ($items as $item) { |
111
|
|
|
$write = false; |
112
|
|
|
|
113
|
|
|
if ((int)$item->Price && (int)$item->BasePrice == 0) { |
114
|
|
|
$item->BasePrice = $item->Price; |
115
|
|
|
$write = true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($item->TaxID != 0 && $item->TaxRateID == 0) { |
119
|
|
|
$item->TaxRateID = $item->TaxID; |
120
|
|
|
$write = true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($write) { |
124
|
|
|
$item->write(); |
125
|
|
|
$i++; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
unset($items); |
130
|
|
|
|
131
|
|
|
$this->log("Migrated {$i} Line Item Customisations"); |
132
|
|
|
|
133
|
|
|
// Change price/tax on line items to use new fields from extension |
134
|
|
|
$items = LineItemCustomisation::get(); |
135
|
|
|
$count = $items->count(); |
136
|
|
|
$this->log("Migrating {$count} Line Items"); |
137
|
|
|
$i = 0; |
138
|
|
|
|
139
|
|
|
foreach ($items as $item) { |
140
|
|
|
$write = false; |
141
|
|
|
|
142
|
|
|
if ((int)$item->Price && (int)$item->BasePrice == 0) { |
143
|
|
|
$item->BasePrice = $item->Price; |
144
|
|
|
$write = true; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($write) { |
148
|
|
|
$item->write(); |
149
|
|
|
$i++; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
unset($items); |
154
|
|
|
|
155
|
|
|
$this->log("Migrated {$i} Line Items"); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function down() |
162
|
|
|
{ |
163
|
|
|
$zones = Zone::get(); |
|
|
|
|
164
|
|
|
|
165
|
|
|
$this->log('No Downgrade Required'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $text |
170
|
|
|
*/ |
171
|
|
|
protected function log($text) |
172
|
|
|
{ |
173
|
|
|
if (Controller::curr() instanceof DatabaseAdmin) { |
174
|
|
|
DB::alteration_message($text, 'obsolete'); |
175
|
|
|
} elseif (Director::is_cli()) { |
176
|
|
|
echo $text . "\n"; |
177
|
|
|
} else { |
178
|
|
|
echo $text . "<br/>"; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths