299 lines
12 KiB
PHP
299 lines
12 KiB
PHP
<?php
|
|
namespace App\Services\BusinessPlan;
|
|
|
|
use App\Models\UserLevel;
|
|
use App\User;
|
|
use stdClass;
|
|
|
|
class TreeCalcBot
|
|
{
|
|
private $items = [];
|
|
private $parentless = [];
|
|
private $sponsor;
|
|
private $date;
|
|
public $user;
|
|
public $lines = [];
|
|
public $total_tp = 0;
|
|
public $total_qual_tp = 0;
|
|
public $commission_total = 0;
|
|
public $qual_user_level = null;
|
|
private $init_from;
|
|
|
|
|
|
|
|
|
|
private static $userIDs = [];
|
|
|
|
public static function addUserID($id){
|
|
self::$userIDs[$id] = $id;
|
|
}
|
|
|
|
public function __construct($month, $year, $init_from = 'member')
|
|
{
|
|
$this->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 = '<ol class="dd-list">';
|
|
foreach($this->items as $item){
|
|
$ret .= $this->addItem($item, $deep);
|
|
}
|
|
$ret .= '</ol>';
|
|
return $ret;
|
|
}
|
|
|
|
private function addItem($item, $deep){
|
|
|
|
$button = '';
|
|
if(($this->init_from === 'admin' && \Auth::user()->isAdmin()) || ($this->init_from === 'member' && \Auth::user()->id === $item->id)){
|
|
$button = ' | <button type="button" class="btn icon-btn btn-xs btn-secondary" data-toggle="modal" data-target="#modals-load-content"
|
|
data-id="'.$item->id.'"
|
|
data-action="business-user-detail"
|
|
data-back=""
|
|
data-modal="modal-xl"
|
|
data-init_from="'.$this->init_from .'"
|
|
data-route="'.route('modal_load').'"><span class="far fa-calculator"></span></button>';
|
|
}
|
|
return '<li class="dd-item dd-nodrag" data-id="'.$item->id.'">'.
|
|
'<div class="dd-handle">
|
|
<div class="media align-items-center">
|
|
<div class="d-flex flex-column justify-content-center align-items-center">
|
|
'.(($deep > 0) ? '<div class="text-large font-weight-bolder line-height-1 my-2 text-secondary badge badge-outline-secondary">'.$deep.'</div>' : '').'
|
|
</div>
|
|
<div class="media-body ml-2">
|
|
<span class="'.($item->active_account ? '' : 'text-muted').'">
|
|
<span class="mr-1 ion ion-ios-contact '.($item->active_account ? 'text-primary' : 'text-danger').'"></span>
|
|
<strong>'.$item->first_name.' '.$item->last_name.'</strong> <a href="mailto: '.$item->email.'">'.$item->email.'</a> <span class="badge badge-outline-default '.($item->active_account ? '' : 'text-muted').'">'.$item->user_level.' | '.$item->m_account.'</span>
|
|
<br><span class="small">'.
|
|
($item->active_account ?
|
|
'<strong>Points: '.$item->sales_volume_points_sum.'</strong> | B: '.$item->sales_volume_points.' | S: '.$item->sales_volume_points_shop.' <strong> | Umsatz: '.$item->sales_volume_total_sum.' €</strong> | B: '.$item->sales_volume_total.' € | S: '.$item->sales_volume_total_shop.' €'.
|
|
$button
|
|
:
|
|
'Account bis: '.$item->payment_account_date).
|
|
'</span>
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>'.
|
|
$this->addParentItem($item, $deep).
|
|
'</li>';
|
|
|
|
}
|
|
|
|
private function addParentItem($item, $deep){
|
|
if($item->items){
|
|
$ret = '<ol class="dd-list dd-nodrag">';
|
|
foreach($item->items as $parent){
|
|
$ret .= $this->addItem($parent, $deep+1);
|
|
}
|
|
$ret .='</ol>';
|
|
return $ret;
|
|
}
|
|
return;
|
|
}
|
|
|
|
|
|
public function isParentless(){
|
|
return $this->parentless ? true : false;
|
|
}
|
|
|
|
public function makeParentlessHtml(){
|
|
$ret = "";
|
|
foreach($this->parentless as $item){
|
|
$ret .= '<li class="dd-item dd-nodrag" data-id="'.$item->id.'">'.
|
|
'<div class="dd-handle">
|
|
<span class="'.($item->active_account ? '' : 'text-muted').'">
|
|
<span class="mr-1 ion ion-ios-contact '.($item->active_account ? 'text-primary' : 'text-danger').'"></span>
|
|
<strong>'.$item->first_name.' '.$item->last_name.'</strong> <a href="mailto: '.$item->email.'">'.$item->email.'</a> <span class="badge badge-outline-default '.($item->active_account ? '' : 'text-muted').'">'.$item->user_level.' | '.$item->m_account.'</span>
|
|
<br><span class="small">'.
|
|
($item->active_account ?
|
|
'<strong>Points: '.$item->sales_volume_points_sum.'</strong> | B: '.$item->sales_volume_points.' | S: '.$item->sales_volume_points_shop.' <strong> | Umsatz: '.$item->sales_volume_total_sum.' €</strong> | B: '.$item->sales_volume_total.' € | S: '.$item->sales_volume_total_shop.' €'.
|
|
' | <button type="button" class="btn icon-btn btn-xs btn-secondary" data-toggle="modal" data-target="#modals-load-content"
|
|
data-id="'.$item->id.'"
|
|
data-action="business-user-detail"
|
|
data-back=""
|
|
data-modal="modal-xl"
|
|
data-route="'.route('modal_load').'"><span class="far fa-calculator"></span></button>'
|
|
:
|
|
'Account bis: '.$item->payment_account_date).
|
|
'<br>'.$item->m_sponsor_name.
|
|
'</span>
|
|
</span>
|
|
</div>'.
|
|
'</li>';
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
public function makeSponsorHtml(){
|
|
|
|
if($this->sponsor){
|
|
//' | <a href="' . route('admin_business_user_detail', [$this->sponsor->id]) . '" class="btn icon-btn btn-xs btn-secondary"><span class="far fa-calculator"></span></a>'
|
|
$ret = '<li class="dd-item dd-nodrag" data-id="">'.
|
|
'<div class="dd-handle">
|
|
<span class="'.($this->sponsor->active_account ? '' : 'text-muted').'">
|
|
<span class="mr-1 ion ion-ios-contact '.($this->sponsor->active_account ? 'text-primary' : 'text-danger').'"></span>
|
|
<strong>'.$this->sponsor->first_name.' '.$this->sponsor->last_name.'</strong> <a href="mailto: '.$this->sponsor->email.'">'.$this->sponsor->email.'</a> <span class="badge badge-outline-default '.($this->sponsor->active_account ? '' : 'text-muted').'">'.$this->sponsor->user_level.' | '.$this->sponsor->m_account.'</span>';
|
|
|
|
if($this->init_from === 'admin'){
|
|
$ret .= '<br><span class="small">'.
|
|
($this->sponsor->active_account ?
|
|
'<strong>Points: '.$this->sponsor->sales_volume_points_sum.'</strong> | B: '.$this->sponsor->sales_volume_points.' | S: '.$this->sponsor->sales_volume_points_shop.' <strong> | Umsatz: '.$this->sponsor->sales_volume_total_sum.' €</strong> | B: '.$this->sponsor->sales_volume_total.' € | S: '.$this->sponsor->sales_volume_total_shop.' €'
|
|
:
|
|
'Account bis: '.$this->sponsor->payment_account_date).
|
|
'</span>';
|
|
}
|
|
$ret .= '</span>
|
|
</div>
|
|
</li>';
|
|
|
|
return $ret;
|
|
}
|
|
return 'Keinen Sponsor zugewiesen';
|
|
|
|
|
|
}
|
|
|
|
}
|