95 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			95 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Models\Banking;
 | |
| 
 | |
| use App\Abstracts\Model;
 | |
| use App\Traits\Transactions;
 | |
| 
 | |
| class Account extends Model
 | |
| {
 | |
|     use Transactions;
 | |
| 
 | |
|     protected $table = 'accounts';
 | |
| 
 | |
|     /**
 | |
|      * The accessors to append to the model's array form.
 | |
|      *
 | |
|      * @var array
 | |
|      */
 | |
|     protected $appends = ['balance'];
 | |
| 
 | |
|     /**
 | |
|      * Attributes that should be mass-assignable.
 | |
|      *
 | |
|      * @var array
 | |
|      */
 | |
|     protected $fillable = ['company_id', 'name', 'number', 'currency_code', 'opening_balance', 'bank_name', 'bank_phone', 'bank_address', 'enabled'];
 | |
| 
 | |
|     /**
 | |
|      * Sortable columns.
 | |
|      *
 | |
|      * @var array
 | |
|      */
 | |
|     public $sortable = ['name', 'number', 'opening_balance', 'enabled'];
 | |
| 
 | |
|     public function currency()
 | |
|     {
 | |
|         return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
 | |
|     }
 | |
| 
 | |
|     public function expense_transactions()
 | |
|     {
 | |
|         return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
 | |
|     }
 | |
| 
 | |
|     public function income_transactions()
 | |
|     {
 | |
|         return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
 | |
|     }
 | |
| 
 | |
|     public function transactions()
 | |
|     {
 | |
|         return $this->hasMany('App\Models\Banking\Transaction');
 | |
|     }
 | |
| 
 | |
|     public function scopeName($query, $name)
 | |
|     {
 | |
|         return $query->where('name', '=', $name);
 | |
|     }
 | |
| 
 | |
|     public function scopeNumber($query, $number)
 | |
|     {
 | |
|         return $query->where('number', '=', $number);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Convert opening balance to double.
 | |
|      *
 | |
|      * @param  string  $value
 | |
|      * @return void
 | |
|      */
 | |
|     public function setOpeningBalanceAttribute($value)
 | |
|     {
 | |
|         $this->attributes['opening_balance'] = (double) $value;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Get the current balance.
 | |
|      *
 | |
|      * @return string
 | |
|      */
 | |
|     public function getBalanceAttribute()
 | |
|     {
 | |
|         // Opening Balance
 | |
|         $total = $this->opening_balance;
 | |
| 
 | |
|         // Sum Incomes
 | |
|         $total += $this->income_transactions->sum('amount');
 | |
| 
 | |
|         // Subtract Expenses
 | |
|         $total -= $this->expense_transactions->sum('amount');
 | |
| 
 | |
|         return $total;
 | |
|     }
 | |
| }
 |