Blame view

app/Models/Income/InvoicePayment.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
  <?php
  
  namespace App\Models\Income;
  
  use App\Models\Model;
  use App\Traits\Currencies;
  use App\Traits\DateTime;
  use App\Traits\Media;
  use Date;
  
  class InvoicePayment extends Model
  {
      use Currencies, DateTime, Media;
  
      protected $table = 'invoice_payments';
  
      protected $dates = ['deleted_at', 'paid_at'];
  
      /**
       * Attributes that should be mass-assignable.
       *
       * @var array
       */
      protected $fillable = ['company_id', 'invoice_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'description', 'payment_method', 'reference'];
  
      public function account()
      {
          return $this->belongsTo('App\Models\Banking\Account');
      }
  
      public function currency()
      {
          return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
      }
  
      public function invoice()
      {
          return $this->belongsTo('App\Models\Income\Invoice');
      }
  
      public function item()
      {
          return $this->belongsTo('App\Models\Common\Item');
      }
  
      public function tax()
      {
          return $this->belongsTo('App\Models\Setting\Tax');
      }
  
      public function scopeLatest($query)
      {
          return $query->orderBy('paid_at', 'desc');
      }
  
      /**
       * Convert amount to double.
       *
       * @param  string  $value
       * @return void
       */
      public function setAmountAttribute($value)
      {
          $this->attributes['amount'] = (double) $value;
      }
  
      /**
       * Convert currency rate to double.
       *
       * @param  string  $value
       * @return void
       */
      public function setCurrencyRateAttribute($value)
      {
          $this->attributes['currency_rate'] = (double) $value;
      }
  
      /**
       * Scope paid invoice.
       *
       * @param \Illuminate\Database\Eloquent\Builder $query
       * @return \Illuminate\Database\Eloquent\Builder
       */
      public function scopePaid($query)
      {
          return $query->sum('amount');
      }
  
      /**
       * Get the current balance.
       *
       * @return string
       */
      public function getAttachmentAttribute($value)
      {
          if (!empty($value) && !$this->hasMedia('attachment')) {
              return $value;
          } elseif (!$this->hasMedia('attachment')) {
              return false;
          }
  
          return $this->getMedia('attachment')->last();
      }
  
      public function getDivideConvertedAmount($format = false)
      {
          return $this->divide($this->amount, $this->currency_code, $this->currency_rate, $format);
      }
  }