Record.php 1.6 KB

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