Record.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. //'month',
  15. //'year',
  16. 'type',
  17. //'amount',
  18. //'note',
  19. 'commercial',
  20. 'corrispettivo_fiscale',
  21. 'deleted',
  22. 'financial_movement',
  23. 'amount'
  24. ];
  25. public function member()
  26. {
  27. return $this->belongsTo(Member::class);
  28. }
  29. public function supplier()
  30. {
  31. return $this->belongsTo(Supplier::class);
  32. }
  33. public function payment_method()
  34. {
  35. return $this->belongsTo(PaymentMethod::class);
  36. }
  37. public function rows()
  38. {
  39. return $this->hasMany(RecordRow::class);
  40. }
  41. public function getTotal()
  42. {
  43. $vat = 0;
  44. foreach($this->rows as $r)
  45. {
  46. if ($r->vat_id > 0)
  47. {
  48. $v = \App\Models\Vat::where('id', $r->vat_id)->first();
  49. if ($v && $v->value > 0)
  50. $vat += $r->amount / 100 * $v->value;
  51. }
  52. }
  53. return $this->rows->sum('amount') + $vat;
  54. }
  55. }