Record.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Record extends Model
  6. {
  7. use HasFactory;
  8. protected $fillable = [
  9. 'member_id',
  10. 'supplier_id',
  11. // 'causal_id',
  12. 'payment_method_id',
  13. 'date',
  14. 'data_pagamento',
  15. //'month',
  16. //'year',
  17. 'type',
  18. //'amount',
  19. //'note',
  20. 'commercial',
  21. 'corrispettivo_fiscale',
  22. 'deleted',
  23. 'financial_movement',
  24. 'amount',
  25. 'numero_fattura',
  26. //'attachment'
  27. ];
  28. public function member()
  29. {
  30. return $this->belongsTo(Member::class);
  31. }
  32. public function supplier()
  33. {
  34. return $this->belongsTo(Supplier::class);
  35. }
  36. public function payment_method()
  37. {
  38. return $this->belongsTo(PaymentMethod::class);
  39. }
  40. public function rows()
  41. {
  42. return $this->hasMany(RecordRow::class);
  43. }
  44. public function getTotal()
  45. {
  46. $vat = 0;
  47. foreach($this->rows as $r)
  48. {
  49. if ($r->vat_id > 0)
  50. {
  51. $v = \App\Models\Vat::where('id', $r->vat_id)->first();
  52. if ($v && $v->value > 0)
  53. $vat += $r->amount / 100 * $v->value;
  54. }
  55. }
  56. return $this->rows->sum('amount') + $vat;
  57. }
  58. }