Record.php 1.5 KB

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