Skip to content

Commit 044b6bf

Browse files
committed
Database update required
1 parent 43e2153 commit 044b6bf

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

src/Console/LaravelCrmUpdate.php

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
use Illuminate\Support\Composer;
77
use VentureDrake\LaravelCrm\Models\Delivery;
88
use VentureDrake\LaravelCrm\Models\Invoice;
9+
use VentureDrake\LaravelCrm\Models\InvoiceLine;
910
use VentureDrake\LaravelCrm\Models\Order;
11+
use VentureDrake\LaravelCrm\Models\OrderProduct;
1012
use VentureDrake\LaravelCrm\Models\Person;
1113
use VentureDrake\LaravelCrm\Models\Quote;
14+
use VentureDrake\LaravelCrm\Models\QuoteProduct;
15+
use VentureDrake\LaravelCrm\Models\Setting;
1216
use VentureDrake\LaravelCrm\Services\SettingService;
1317

1418
class LaravelCrmUpdate extends Command
@@ -187,12 +191,68 @@ public function handle()
187191
]);
188192
}
189193

190-
$this->info('Updating Laravel CRM quote numbers complete');
191-
192194
$this->settingService->set('db_update_0194', 1);
193195
$this->info('Updating Laravel CRM delivery numbers complete');
194196
}
195197

198+
if($this->settingService->get('db_update_0199')->value == 0) {
199+
$this->info('Updating Laravel CRM tax amounts...');
200+
201+
foreach (QuoteProduct::whereNull('tax_amount')->get() as $quoteProduct) {
202+
$this->info('Updating Laravel CRM quote product tax #'.$quoteProduct->id);
203+
204+
if($quoteProduct->product && $quoteProduct->product->taxRate) {
205+
$taxRate = $quoteProduct->product->taxRate->rate;
206+
} elseif($quoteProduct->product && $quoteProduct->product->tax_rate) {
207+
$taxRate = $quoteProduct->product->tax_rate;
208+
} else {
209+
$taxRate = Setting::where('name', 'tax_rate')->first()->value ?? 0;
210+
}
211+
212+
$quoteProduct->update([
213+
'tax_rate' => $taxRate,
214+
'tax_amount' => $quoteProduct->amount * ($taxRate / 100)
215+
]);
216+
}
217+
218+
foreach (OrderProduct::whereNull('tax_amount')->get() as $orderProduct) {
219+
$this->info('Updating Laravel CRM order product tax #'.$orderProduct->id);
220+
221+
if($orderProduct->product && $orderProduct->product->taxRate) {
222+
$taxRate = $orderProduct->product->taxRate->rate;
223+
} elseif($orderProduct->product && $orderProduct->product->tax_rate) {
224+
$taxRate = $orderProduct->product->tax_rate;
225+
} else {
226+
$taxRate = Setting::where('name', 'tax_rate')->first()->value ?? 0;
227+
}
228+
229+
$orderProduct->update([
230+
'tax_rate' => $taxRate,
231+
'tax_amount' => $orderProduct->amount * ($taxRate / 100)
232+
]);
233+
}
234+
235+
foreach (InvoiceLine::whereNull('tax_amount')->get() as $invoiceLine) {
236+
$this->info('Updating Laravel CRM invoice line tax #'.$invoiceLine->id);
237+
238+
if($invoiceLine->product && $invoiceLine->product->taxRate) {
239+
$taxRate = $invoiceLine->product->taxRate->rate;
240+
} elseif($invoiceLine->product && $invoiceLine->product->tax_rate) {
241+
$taxRate = $invoiceLine->product->tax_rate;
242+
} else {
243+
$taxRate = Setting::where('name', 'tax_rate')->first()->value ?? 0;
244+
}
245+
246+
$invoiceLine->update([
247+
'tax_rate' => $taxRate,
248+
'tax_amount' => ($invoiceLine->amount * ($taxRate / 100)) / 100
249+
]);
250+
}
251+
252+
$this->settingService->set('db_update_0199', 1);
253+
$this->info('Updating Laravel CRM tax amounts complete');
254+
}
255+
196256
$this->info('Laravel CRM is now updated.');
197257
}
198258
}

src/Http/Middleware/Settings.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ public function handle($request, Closure $next)
189189
]);
190190
}
191191

192+
if((int) Str::replace('.', '', config('laravel-crm.version')) >= 199) {
193+
Setting::firstOrCreate([
194+
'global' => 1,
195+
'name' => 'db_update_0199',
196+
], [
197+
'value' => 0,
198+
]);
199+
}
200+
192201
$installIdSetting = Setting::where([
193202
'name' => 'install_id',
194203
])->first();

src/Http/Middleware/SystemCheck.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public function handle($request, Closure $next)
7070
}
7171
}
7272

73+
if($setting = \VentureDrake\LaravelCrm\Models\Setting::where('name', 'db_update_0199')->first()) {
74+
if($setting->value == 0) {
75+
$dbUpdateRequired = true;
76+
}
77+
}
78+
7379
if($dbUpdateRequired) {
7480
flash('Your Laravel CRM software version requires some database updates to function correctly. Please <a href="https://github.com/venturedrake/laravel-crm#upgrading-from--02">update database</a>')->info()->important();
7581
}

0 commit comments

Comments
 (0)