Blame view

app/Models/Income/InvoiceTotal.php 1.72 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
  <?php
  
  namespace App\Models\Income;
  
  use App\Models\Model;
  use App\Models\Setting\Tax;
  use App\Traits\DateTime;
  
  class InvoiceTotal extends Model
  {
      use DateTime;
  
      protected $table = 'invoice_totals';
  
      /**
       * The accessors to append to the model's array form.
       *
       * @var array
       */
      protected $appends = ['title'];
  
      /**
       * Attributes that should be mass-assignable.
       *
       * @var array
       */
      protected $fillable = ['company_id', 'invoice_id', 'code', 'name', 'amount', 'sort_order'];
  
      public function invoice()
      {
          return $this->belongsTo('App\Models\Income\Invoice');
      }
  
      /**
       * Convert amount to double.
       *
       * @param  string  $value
       * @return void
       */
      public function setAmountAttribute($value)
      {
          $this->attributes['amount'] = (double) $value;
      }
  
      /**
       * Get the formatted name.
       *
       * @return string
       */
      public function getTitleAttribute()
      {
          $title = $this->name;
  
          $percent = 0;
  
          switch ($this->code) {
              case 'discount':
                  $title = trans($title);
                  $percent = $this->invoice->discount;
  
                  break;
              case 'tax':
                  $rate = Tax::where('name', $title)->value('rate');
  
                  if (!empty($rate)) {
                      $percent = $rate;
                  }
  
                  break;
          }
  
          if (!empty($percent)) {
              $title .= ' (';
  
              if (setting('general.percent_position', 'after') == 'after') {
                  $title .= $percent . '%';
              } else {
                  $title .= '%' . $percent;
              }
  
              $title .= ')';
          }
  
          return $title;
      }
  }