Skip to content

Commit

Permalink
Database update required
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdrake committed Sep 4, 2023
1 parent 43e2153 commit 044b6bf
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
64 changes: 62 additions & 2 deletions src/Console/LaravelCrmUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
use Illuminate\Support\Composer;
use VentureDrake\LaravelCrm\Models\Delivery;
use VentureDrake\LaravelCrm\Models\Invoice;
use VentureDrake\LaravelCrm\Models\InvoiceLine;
use VentureDrake\LaravelCrm\Models\Order;
use VentureDrake\LaravelCrm\Models\OrderProduct;
use VentureDrake\LaravelCrm\Models\Person;
use VentureDrake\LaravelCrm\Models\Quote;
use VentureDrake\LaravelCrm\Models\QuoteProduct;
use VentureDrake\LaravelCrm\Models\Setting;
use VentureDrake\LaravelCrm\Services\SettingService;

class LaravelCrmUpdate extends Command
Expand Down Expand Up @@ -187,12 +191,68 @@ public function handle()
]);
}

$this->info('Updating Laravel CRM quote numbers complete');

$this->settingService->set('db_update_0194', 1);
$this->info('Updating Laravel CRM delivery numbers complete');
}

if($this->settingService->get('db_update_0199')->value == 0) {
$this->info('Updating Laravel CRM tax amounts...');

foreach (QuoteProduct::whereNull('tax_amount')->get() as $quoteProduct) {
$this->info('Updating Laravel CRM quote product tax #'.$quoteProduct->id);

if($quoteProduct->product && $quoteProduct->product->taxRate) {
$taxRate = $quoteProduct->product->taxRate->rate;
} elseif($quoteProduct->product && $quoteProduct->product->tax_rate) {
$taxRate = $quoteProduct->product->tax_rate;
} else {
$taxRate = Setting::where('name', 'tax_rate')->first()->value ?? 0;
}

$quoteProduct->update([
'tax_rate' => $taxRate,
'tax_amount' => $quoteProduct->amount * ($taxRate / 100)
]);
}

foreach (OrderProduct::whereNull('tax_amount')->get() as $orderProduct) {
$this->info('Updating Laravel CRM order product tax #'.$orderProduct->id);

if($orderProduct->product && $orderProduct->product->taxRate) {
$taxRate = $orderProduct->product->taxRate->rate;
} elseif($orderProduct->product && $orderProduct->product->tax_rate) {
$taxRate = $orderProduct->product->tax_rate;
} else {
$taxRate = Setting::where('name', 'tax_rate')->first()->value ?? 0;
}

$orderProduct->update([
'tax_rate' => $taxRate,
'tax_amount' => $orderProduct->amount * ($taxRate / 100)
]);
}

foreach (InvoiceLine::whereNull('tax_amount')->get() as $invoiceLine) {
$this->info('Updating Laravel CRM invoice line tax #'.$invoiceLine->id);

if($invoiceLine->product && $invoiceLine->product->taxRate) {
$taxRate = $invoiceLine->product->taxRate->rate;
} elseif($invoiceLine->product && $invoiceLine->product->tax_rate) {
$taxRate = $invoiceLine->product->tax_rate;
} else {
$taxRate = Setting::where('name', 'tax_rate')->first()->value ?? 0;
}

$invoiceLine->update([
'tax_rate' => $taxRate,
'tax_amount' => ($invoiceLine->amount * ($taxRate / 100)) / 100
]);
}

$this->settingService->set('db_update_0199', 1);
$this->info('Updating Laravel CRM tax amounts complete');
}

$this->info('Laravel CRM is now updated.');
}
}
9 changes: 9 additions & 0 deletions src/Http/Middleware/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ public function handle($request, Closure $next)
]);
}

if((int) Str::replace('.', '', config('laravel-crm.version')) >= 199) {
Setting::firstOrCreate([
'global' => 1,
'name' => 'db_update_0199',
], [
'value' => 0,
]);
}

$installIdSetting = Setting::where([
'name' => 'install_id',
])->first();
Expand Down
6 changes: 6 additions & 0 deletions src/Http/Middleware/SystemCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public function handle($request, Closure $next)
}
}

if($setting = \VentureDrake\LaravelCrm\Models\Setting::where('name', 'db_update_0199')->first()) {
if($setting->value == 0) {
$dbUpdateRequired = true;
}
}

if($dbUpdateRequired) {
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();
}
Expand Down

0 comments on commit 044b6bf

Please sign in to comment.