Record.php 1.3 KB

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