Blame view

app/Models/Banking/Account.php 2.43 KB
b7c7a5f6   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  <?php
  
  namespace App\Models\Banking;
  
  use App\Models\Model;
  use Sofa\Eloquence\Eloquence;
  
  class Account extends Model
  {
      use Eloquence;
  
      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'];
  
      /**
       * Searchable rules.
       *
       * @var array
       */
      protected $searchableColumns = [
          'name'         => 10,
          'number'       => 10,
          'bank_name'    => 10,
          'bank_phone'   => 5,
          'bank_address' => 2,
      ];
  
      public function currency()
      {
          return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
      }
  
      public function invoice_payments()
      {
          return $this->hasMany('App\Models\Income\InvoicePayment');
      }
  
      public function revenues()
      {
          return $this->hasMany('App\Models\Income\Revenue');
      }
  
      public function bill_payments()
      {
          return $this->hasMany('App\Models\Expense\BillPayment');
      }
  
      public function payments()
      {
          return $this->hasMany('App\Models\Expense\Payment');
      }
  
      /**
       * 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 invoices
          foreach ($this->invoice_payments as $item) {
              $total += $item->amount;
          }
  
          // Sum revenues
          foreach ($this->revenues as $item) {
              $total += $item->amount;
          }
  
          // Subtract bills
          foreach ($this->bill_payments as $item) {
              $total -= $item->amount;
          }
  
          // Subtract payments
          foreach ($this->payments as $item) {
              $total -= $item->amount;
          }
  
          return $total;
      }
  }