Record.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. 'is_paid',
  28. ];
  29. public function member()
  30. {
  31. return $this->belongsTo(Member::class);
  32. }
  33. public function supplier()
  34. {
  35. return $this->belongsTo(Supplier::class);
  36. }
  37. public function payment_method()
  38. {
  39. return $this->belongsTo(PaymentMethod::class);
  40. }
  41. public function rows()
  42. {
  43. return $this->hasMany(RecordRow::class);
  44. }
  45. public function getTotal()
  46. {
  47. $vat = 0;
  48. foreach($this->rows as $r)
  49. {
  50. if ($r->vat_id > 0)
  51. {
  52. $v = \App\Models\Vat::where('id', $r->vat_id)->first();
  53. if ($v && $v->value > 0)
  54. $vat += $r->amount / 100 * $v->value;
  55. }
  56. }
  57. return $this->rows->sum('amount') + $vat;
  58. }
  59. }