mirror of
				https://github.com/crater-invoice/crater.git
				synced 2025-10-31 13:41:09 -04:00 
			
		
		
		
	* Create PHP CS Fixer config and add to CI workflow * Run php cs fixer on project * Add newline at end of file * Update to use PHP CS Fixer v3 * Run v3 config on project * Run seperate config in CI
		
			
				
	
	
		
			80 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			80 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace Crater\Models;
 | |
| 
 | |
| use Carbon\Carbon;
 | |
| use Illuminate\Database\Eloquent\Factories\HasFactory;
 | |
| use Illuminate\Database\Eloquent\Model;
 | |
| 
 | |
| class ExpenseCategory extends Model
 | |
| {
 | |
|     use HasFactory;
 | |
| 
 | |
|     protected $fillable = ['name', 'company_id', 'description'];
 | |
| 
 | |
|     /**
 | |
|      * The accessors to append to the model's array form.
 | |
|      *
 | |
|      * @var array
 | |
|      */
 | |
|     protected $appends = ['amount', 'formattedCreatedAt'];
 | |
| 
 | |
|     public function expenses()
 | |
|     {
 | |
|         return $this->hasMany(Expense::class);
 | |
|     }
 | |
| 
 | |
|     public function getFormattedCreatedAtAttribute($value)
 | |
|     {
 | |
|         $dateFormat = CompanySetting::getSetting('carbon_date_format', $this->company_id);
 | |
| 
 | |
|         return Carbon::parse($this->created_at)->format($dateFormat);
 | |
|     }
 | |
| 
 | |
|     public function getAmountAttribute()
 | |
|     {
 | |
|         return $this->expenses()->sum('amount');
 | |
|     }
 | |
| 
 | |
|     public function scopeWhereCompany($query, $company_id)
 | |
|     {
 | |
|         $query->where('company_id', $company_id);
 | |
|     }
 | |
| 
 | |
|     public function scopeWhereCategory($query, $category_id)
 | |
|     {
 | |
|         $query->orWhere('id', $category_id);
 | |
|     }
 | |
| 
 | |
|     public function scopeWhereSearch($query, $search)
 | |
|     {
 | |
|         $query->where('name', 'LIKE', '%'.$search.'%');
 | |
|     }
 | |
| 
 | |
|     public function scopeApplyFilters($query, array $filters)
 | |
|     {
 | |
|         $filters = collect($filters);
 | |
| 
 | |
|         if ($filters->get('category_id')) {
 | |
|             $query->whereCategory($filters->get('category_id'));
 | |
|         }
 | |
| 
 | |
|         if ($filters->get('company_id')) {
 | |
|             $query->whereCompany($filters->get('company_id'));
 | |
|         }
 | |
| 
 | |
|         if ($filters->get('search')) {
 | |
|             $query->whereSearch($filters->get('search'));
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public function scopePaginateData($query, $limit)
 | |
|     {
 | |
|         if ($limit == 'all') {
 | |
|             return collect(['data' => $query->get()]);
 | |
|         }
 | |
| 
 | |
|         return $query->paginate($limit);
 | |
|     }
 | |
| }
 |