Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Models\Income;
  4.  
  5. use App\Models\Model;
  6. use Bkwld\Cloner\Cloneable;
  7. use App\Traits\Currencies;
  8. use Illuminate\Notifications\Notifiable;
  9. use Sofa\Eloquence\Eloquence;
  10.  
  11. class Customer extends Model
  12. {
  13. use Cloneable, Currencies, Eloquence, Notifiable;
  14.  
  15. protected $table = 'customers';
  16.  
  17. /**
  18. * Attributes that should be mass-assignable.
  19. *
  20. * @var array
  21. */
  22. protected $fillable = ['company_id', 'user_id', 'name', 'email', 'tax_number', 'phone', 'address', 'website', 'currency_code', 'reference', 'enabled'];
  23.  
  24. /**
  25. * Sortable columns.
  26. *
  27. * @var array
  28. */
  29. public $sortable = ['name', 'email', 'phone', 'enabled'];
  30.  
  31. /**
  32. * Searchable rules.
  33. *
  34. * @var array
  35. */
  36. protected $searchableColumns = [
  37. 'name' => 10,
  38. 'email' => 5,
  39. 'phone' => 2,
  40. 'website' => 2,
  41. 'address' => 1,
  42. ];
  43.  
  44. public function invoices()
  45. {
  46. return $this->hasMany('App\Models\Income\Invoice');
  47. }
  48.  
  49. public function revenues()
  50. {
  51. return $this->hasMany('App\Models\Income\Revenue');
  52. }
  53.  
  54. public function currency()
  55. {
  56. return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
  57. }
  58.  
  59. public function user()
  60. {
  61. return $this->belongsTo('App\Models\Auth\User', 'user_id', 'id');
  62. }
  63.  
  64. public function onCloning($src, $child = null)
  65. {
  66. $this->user_id = null;
  67. }
  68.  
  69. public function getUnpaidAttribute()
  70. {
  71. $amount = 0;
  72.  
  73. $invoices = $this->invoices()->accrued()->notPaid()->get();
  74.  
  75. foreach ($invoices as $invoice) {
  76. $invoice_amount = $invoice->amount - $invoice->paid;
  77.  
  78. $amount += $this->dynamicConvert(setting('general.default_currency'), $invoice_amount, $invoice->currency_code, $invoice->currency_rate, false);
  79. }
  80.  
  81. return $amount;
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement