Record.php 1.7 KB

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