Record.php 1.7 KB

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