Record.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 rows()
  40. {
  41. return $this->hasMany(RecordRow::class);
  42. }
  43. public function getTotal()
  44. {
  45. $vat = 0;
  46. foreach($this->rows as $r)
  47. {
  48. if ($r->vat_id > 0)
  49. {
  50. $v = \App\Models\Vat::where('id', $r->vat_id)->first();
  51. if ($v && $v->value > 0)
  52. $vat += $r->amount / 100 * $v->value;
  53. }
  54. }
  55. return $this->rows->sum('amount') + $vat;
  56. }
  57. }