Passed
Push — master ( be1785...591f13 )
by Ajit
07:10
created

Expense::category()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App;
4
5
use Carbon\Carbon;
6
use Sofa\Eloquence\Eloquence;
7
use Illuminate\Database\Eloquent\Model;
8
9
class Expense extends Model
10
{
11
    //Eloquence Search mapping
12
    use Eloquence;
13
    use createdByUser, updatedByUser;
14
15
    protected $table = 'trn_expenses';
16
17
    protected $fillable = [
18
        'name',
19
        'category_id',
20
        'amount',
21
        'due_date',
22
        'repeat',
23
        'note',
24
        'paid',
25
        'created_by',
26
        'updated_by',
27
    ];
28
29
    protected $searchableColumns = [
30
        'name' => 20,
31
        'amount' => 10,
32
    ];
33
34
    protected $dates = ['created_at', 'updated_at', 'due_date'];
35
36
    public function category()
37
    {
38
        return $this->belongsTo('App\ExpenseCategory', 'category_id');
39
    }
40
41
    public function scopeDueAlerts($query)
42
    {
43
        return $query->where('paid', '!=', \constPaymentStatus::Paid)->where('due_date', '>=', Carbon::today());
44
    }
45
46
    public function scopeOutstandingAlerts($query)
47
    {
48
        return $query->where('paid', '!=', \constPaymentStatus::Paid)->where('due_date', '<', Carbon::today());
49
    }
50
51
    public function scopeIndexQuery($query, $category, $sorting_field, $sorting_direction, $drp_start, $drp_end)
52
    {
53
        $sorting_field = ($sorting_field != null ? $sorting_field : 'created_at');
54
        $sorting_direction = ($sorting_direction != null ? $sorting_direction : 'desc');
55
56
        if ($drp_start == null or $drp_end == null) {
57
            if ($category == 0) {
58
                return $query->leftJoin('mst_expenses_categories', 'trn_expenses.category_id', '=', 'mst_expenses_categories.id')->select('trn_expenses.*', 'mst_expenses_categories.name as category_name')->orderBy($sorting_field, $sorting_direction);
59
            } else {
60
                return $query->leftJoin('mst_expenses_categories', 'trn_expenses.category_id', '=', 'mst_expenses_categories.id')->select('trn_expenses.*', 'mst_expenses_categories.name as category_name')->where('category_id', $category)->orderBy($sorting_field, $sorting_direction);
61
            }
62
        }
63
64
        if ($category == 0) {
65
            return $query->leftJoin('mst_expenses_categories', 'trn_expenses.category_id', '=', 'mst_expenses_categories.id')->select('trn_expenses.*', 'mst_expenses_categories.name as category_name')->whereBetween('trn_expenses.created_at', [
66
                $drp_start,
67
                $drp_end,
68
            ])->orderBy($sorting_field, $sorting_direction);
69
        } else {
70
            return $query->leftJoin('mst_expenses_categories', 'trn_expenses.category_id', '=', 'mst_expenses_categories.id')->select('trn_expenses.*', 'mst_expenses_categories.name as category_name')->where('category_id', $category)->whereBetween('trn_expenses.created_at', [
71
                $drp_start,
72
                $drp_end,
73
            ])->orderBy($sorting_field, $sorting_direction);
74
        }
75
    }
76
}
77