Record.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 origin()
  45. {
  46. return $this->belongsTo(\App\Models\Bank::class, 'origin_id');
  47. }
  48. public function destination()
  49. {
  50. return $this->belongsTo(\App\Models\Bank::class, 'destination_id');
  51. }
  52. public function rows()
  53. {
  54. return $this->hasMany(RecordRow::class);
  55. }
  56. public function getTotal()
  57. {
  58. $vat = 0;
  59. foreach($this->rows as $r)
  60. {
  61. if ($r->vat_id > 0)
  62. {
  63. $v = \App\Models\Vat::where('id', $r->vat_id)->first();
  64. if ($v && $v->value > 0)
  65. $vat += $r->amount / 100 * $v->value;
  66. }
  67. }
  68. return $this->rows->sum('amount') + $vat;
  69. }
  70. }