Version120.php
3.57 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace App\Listeners\Updates;
use App\Events\UpdateFinished;
use App\Models\Auth\Role;
use App\Models\Auth\Permission;
use App\Models\Common\Company;
use App\Models\Expense\Bill;
use App\Models\Income\Invoice;
use App\Models\Setting\Category;
use DB;
use Schema;
use Artisan;
class Version120 extends Listener
{
const ALIAS = 'core';
const VERSION = '1.2.0';
/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle(UpdateFinished $event)
{
// Check if should listen
if (!$this->check($event)) {
return;
}
$this->updatePermissions();
// Update database
Artisan::call('migrate', ['--force' => true]);
$this->updateInvoicesAndBills();
$this->changeQuantityColumn();
}
protected function updatePermissions()
{
$permissions = [];
// Create tax summary permission
$permissions[] = Permission::firstOrCreate([
'name' => 'read-reports-tax-summary',
'display_name' => 'Read Reports Tax Summary',
'description' => 'Read Reports Tax Summary',
]);
// Create profit loss permission
$permissions[] = Permission::firstOrCreate([
'name' => 'read-reports-profit-loss',
'display_name' => 'Read Reports Profit Loss',
'description' => 'Read Reports Profit Loss',
]);
// Attach permission to roles
$roles = Role::all();
foreach ($roles as $role) {
$allowed = ['admin', 'manager'];
if (!in_array($role->name, $allowed)) {
continue;
}
foreach ($permissions as $permission) {
$role->attachPermission($permission);
}
}
}
protected function updateInvoicesAndBills()
{
$companies = Company::all();
foreach ($companies as $company) {
// Invoices
$invoice_category = Category::create([
'company_id' => $company->id,
'name' => trans_choice('general.invoices', 2),
'type' => 'income',
'color' => '#00c0ef',
'enabled' => '1'
]);
foreach ($company->invoices as $invoice) {
$invoice->category_id = $invoice_category->id;
$invoice->save();
}
// Bills
$bill_category = Category::create([
'company_id' => $company->id,
'name' => trans_choice('general.bills', 2),
'type' => 'expense',
'color' => '#dd4b39',
'enabled' => '1'
]);
foreach ($company->bills as $bill) {
$bill->category_id = $bill_category->id;
$bill->save();
}
}
}
protected function changeQuantityColumn()
{
$connection = env('DB_CONNECTION');
if ($connection == 'mysql') {
$tables = [
env('DB_PREFIX') . 'invoice_items',
env('DB_PREFIX') . 'bill_items'
];
foreach ($tables as $table) {
DB::statement("ALTER TABLE `$table` MODIFY `quantity` DOUBLE(7,2) NOT NULL");
}
} else {
Schema::table('invoice_items', function ($table) {
$table->decimal('quantity', 7, 2)->change();
});
Schema::table('bill_items', function ($table) {
$table->decimal('quantity', 7, 2)->change();
});
}
}
}