Company.php
6.19 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
namespace App\Models\Common;
use Auth;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kyslik\ColumnSortable\Sortable;
use App\Traits\Media;
class Company extends Eloquent
{
use Filterable, SoftDeletes, Sortable, Media;
protected $table = 'companies';
protected $dates = ['deleted_at'];
protected $fillable = ['domain', 'enabled'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['name', 'domain', 'email', 'enabled', 'created_at'];
public function accounts()
{
return $this->hasMany('App\Models\Banking\Account');
}
public function bill_histories()
{
return $this->hasMany('App\Models\Expense\BillHistory');
}
public function bill_items()
{
return $this->hasMany('App\Models\Expense\BillItem');
}
public function bill_payments()
{
return $this->hasMany('App\Models\Expense\BillPayment');
}
public function bill_statuses()
{
return $this->hasMany('App\Models\Expense\BillStatus');
}
public function bills()
{
return $this->hasMany('App\Models\Expense\Bill');
}
public function categories()
{
return $this->hasMany('App\Models\Setting\Category');
}
public function currencies()
{
return $this->hasMany('App\Models\Setting\Currency');
}
public function customers()
{
return $this->hasMany('App\Models\Income\Customer');
}
public function invoice_histories()
{
return $this->hasMany('App\Models\Income\InvoiceHistory');
}
public function invoice_items()
{
return $this->hasMany('App\Models\Income\InvoiceItem');
}
public function invoice_payments()
{
return $this->hasMany('App\Models\Income\InvoicePayment');
}
public function invoice_statuses()
{
return $this->hasMany('App\Models\Income\InvoiceStatus');
}
public function invoices()
{
return $this->hasMany('App\Models\Income\Invoice');
}
public function items()
{
return $this->hasMany('App\Models\Common\Item');
}
public function payments()
{
return $this->hasMany('App\Models\Expense\Payment');
}
public function recurring()
{
return $this->hasMany('App\Models\Common\Recurring');
}
public function revenues()
{
return $this->hasMany('App\Models\Income\Revenue');
}
public function settings()
{
return $this->hasMany('App\Models\Setting\Setting');
}
public function taxes()
{
return $this->hasMany('App\Models\Setting\Tax');
}
public function transfers()
{
return $this->hasMany('App\Models\Banking\Transfer');
}
public function users()
{
return $this->morphedByMany('App\Models\Auth\User', 'user', 'user_companies', 'company_id', 'user_id');
}
public function vendors()
{
return $this->hasMany('App\Models\Expense\Vendor');
}
public function setSettings()
{
$settings = $this->settings;
foreach ($settings as $setting) {
list($group, $key) = explode('.', $setting->getAttribute('key'));
// Load only general settings
if ($group != 'general') {
continue;
}
$value = $setting->getAttribute('value');
if (($key == 'company_logo') && empty($value)) {
$value = 'public/img/company.png';
}
$this->setAttribute($key, $value);
}
// Set default default company logo if empty
if ($this->getAttribute('company_logo') == '') {
$this->setAttribute('company_logo', 'public/img/company.png');
}
}
/**
* Define the filter provider globally.
*
* @return ModelFilter
*/
public function modelFilter()
{
list($folder, $file) = explode('/', \Route::current()->uri());
if (empty($folder) || empty($file)) {
return $this->provideFilter();
}
$class = '\App\Filters\\' . ucfirst($folder) .'\\' . ucfirst($file);
return $this->provideFilter($class);
}
/**
* Scope to get all rows filtered, sorted and paginated.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $sort
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCollect($query, $sort = 'name')
{
$request = request();
$input = $request->input();
$limit = $request->get('limit', setting('general.list_limit', '25'));
return Auth::user()->companies()->filter($input)->sortable($sort)->paginate($limit);
}
/**
* Scope to only include companies of a given enabled value.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeEnabled($query, $value = 1)
{
return $query->where('enabled', $value);
}
/**
* Sort by company name
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $direction
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function nameSortable($query, $direction)
{
return $query->join('settings', 'companies.id', '=', 'settings.company_id')
->where('key', 'general.company_name')
->orderBy('value', $direction)
->select('companies.*');
}
/**
* Sort by company email
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $direction
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function emailSortable($query, $direction)
{
return $query->join('settings', 'companies.id', '=', 'settings.company_id')
->where('key', 'general.company_email')
->orderBy('value', $direction)
->select('companies.*');
}
/**
* Get the current balance.
*
* @return string
*/
public function getCompanyLogoAttribute()
{
return $this->getMedia('company_logo')->last();
}
}