Expense::scopeIndexQuery()   C
last analyzed

Complexity

Conditions 7
Paths 16

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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