date = new stdClass(); $date = \Carbon::parse($year.'-'.$month.'-1'); $this->date->month = $month; $this->date->year = $year; $this->date->start_date = $date->format('Y-m-d H:i:s'); $this->date->end_date = $date->endOfMonth()->format('Y-m-d H:i:s'); $this->init_from = $init_from; } public function initMain() { $this->readMain(); $this->readParentsUser(); $this->readParentlessUser(); } public function initUser($user_id) { $user = User::find($user_id); $TreeUserItem = new TreeUserItem($this->date); $TreeUserItem->makeUser($user); $TreeUserItem->addUserID(); $this->items[] = $TreeUserItem; $this->readParentsUser(); $this->readSponsorUser($user->m_sponsor); } public function initDetailUser($user) { $this->user = new TreeUserItem($this->date); $this->user->makeUser($user); $this->user->readParentsUser(); //calculate Lines if(count($this->user->items) > 0){ $this->calcUserTP($this->user->items, 1); } $this->calcQualTP(); } private function calcUserTP($items, $line){ if(!isset($this->lines[$line])){ $this->lines[$line] = new stdClass(); $this->lines[$line]->points = 0; } foreach($items as $item){ if(count($item->items) > 0){ $this->calcUserTP($item->items, $line+1); } $this->lines[$line]->points += $item->sales_volume_points_sum; $this->total_tp += $item->sales_volume_points_sum; } } public function getKeybyLine($line, $key){ if(!isset($this->lines[$line])){ return 0; } return isset($this->lines[$line]->{$key}) ? $this->lines[$line]->{$key} : 0; } public function calcQualTP(){ if($this->user->isQualKP()){ $this->total_qual_tp = $this->total_tp + $this->user->getRestQualKP(); $this->qual_user_level = UserLevel::where('qual_tp', '<=', $this->total_qual_tp)->orderBy('qual_tp', 'desc')->first(); $this->commission_total = 0; if($this->qual_user_level){ foreach($this->lines as $line => $values){ $values->margin = $this->qual_user_level->{'pr_line_'.$line}; $values->commission = round($values->points / 100 * $values->margin, 2); $this->commission_total += $values->commission; $this->lines[$line] = $values; } } } } private function readMain(){ $users = User::with('account')->select('users.*') ->where('users.deleted_at', '=', null) ->where('users.id', '!=', 1) ->where('users.admin', "<", 4) ->where('users.m_level', "!=", null) ->where('users.m_sponsor', "=", null) ->where('users.payment_account', "!=", null) ->where('users.active_date', "<=", $this->date->end_date) ->get(); if($users){ foreach($users as $user){ $TreeUserItem = new TreeUserItem($this->date); $TreeUserItem->makeUser($user); $TreeUserItem->addUserID(); $this->items[] = $TreeUserItem; } } } private function readParentsUser(){ foreach($this->items as $item){ $item->readParentsUser(); } } private function readParentlessUser(){ $users = User::with('account')->select('users.*') ->where('users.deleted_at', '=', null) ->where('users.id', '!=', 1) ->where('users.admin', "<", 4) ->where('users.payment_account', "!=", null) ->where('users.active_date', "<=", $this->date->end_date) ->get(); foreach($users as $user){ if(!isset(self::$userIDs[$user->id])){ $TreeUserItem = new TreeUserItem($this->date); $TreeUserItem->makeUser($user); $TreeUserItem->checkSponsor(); $this->parentless[] = $TreeUserItem; } } } public function readSponsorUser($m_sponsor_id){ $sponsor = User::find($m_sponsor_id); if($sponsor){ $this->sponsor = new TreeUserItem($this->date); $this->sponsor->makeUser($sponsor); } } public function getItems(){ return $this->items; } public function makeHtmlTree(){ $deep = 0; $ret = '