| 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 |  |  |     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 |  |  |             if ($category == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 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); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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')->where('category_id', $category)->orderBy($sorting_field, $sorting_direction); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if ($category == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             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); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             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); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  |