hasMany(Feature::class)->orderBy('position'); } public function tasks() { return $this->hasManyThrough(Task::class, Feature::class); } public function mainFeatures() { return $this->hasMany(Feature::class)->orderBy('position')->whereTypeId(1); } public function additionalFeatures() { return $this->hasMany(Feature::class)->orderBy('position')->whereTypeId(2); } public function subscriptions() { return $this->hasMany(Subscription::class); } public function payments() { return $this->hasMany(Payment::class)->orderBy('date','desc'); } public function customer() { return $this->belongsTo(User::class,'customer_id'); } public function cashInTotal() { return $this->payments->sum(function($payment) { return $payment->in_out == 1 ? $payment->amount : 0; }); } public function cashOutTotal() { return $this->payments->sum(function($payment) { return $payment->in_out == 0 ? $payment->amount : 0; }); } public function getFeatureOveralProgress() { $overalProgress = 0; $this->load('features.tasks'); $totalPrice = $this->features->sum('price'); foreach ($this->features as $feature) { $progress = $feature->tasks->avg('progress'); $index = $totalPrice ? ($feature->price / $totalPrice) : 1; $overalProgress += $progress * $index; } return $overalProgress; } }