Record.php 1.4 KB

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