404 lines
18 KiB
PHP
404 lines
18 KiB
PHP
<?php
|
|
|
|
namespace App\Services\BusinessPlan;
|
|
|
|
use App\User;
|
|
use stdClass;
|
|
use Carbon;
|
|
use App\Models\UserBusinessStructure;
|
|
|
|
class TreeCalcBot
|
|
{
|
|
public $date;
|
|
public $business_user;
|
|
|
|
public $business_users = [];
|
|
public $parentless = [];
|
|
|
|
private $sponsor;
|
|
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 initStructureAdmin($check = true, $forceLiveCalculation = false)
|
|
{
|
|
//check is month is saved.
|
|
if ($check && $UserBusinessStructure = self::isFromStored($this->date->month, $this->date->year)) {
|
|
$this->readStoredRootUsers($UserBusinessStructure);
|
|
$this->readStoredParentsUsers($UserBusinessStructure);
|
|
$this->readStoredParentlessUser($UserBusinessStructure);
|
|
} else {
|
|
$this->readRootUsers();
|
|
$this->readParentsUsers();
|
|
$this->readParentlessUser();
|
|
}
|
|
}
|
|
|
|
public function initStructureUser($user_id)
|
|
{
|
|
|
|
$BusinessUserItem = new BusinessUserItem($this->date);
|
|
$BusinessUserItem->makeUser($user_id);
|
|
$BusinessUserItem->addUserID();
|
|
$this->business_users[] = $BusinessUserItem;
|
|
|
|
//check is month is saved.
|
|
if ($UserBusinessStructure = self::isFromStored($this->date->month, $this->date->year)) {
|
|
$this->readStoredParentsUsers($UserBusinessStructure);
|
|
|
|
if (isset($this->business_users[0]) && $this->business_users[0]->sponsor) {
|
|
$this->readStoredSponsorUser($this->business_users[0]->sponsor->user_id);
|
|
}
|
|
} else {
|
|
$this->readParentsUsers();
|
|
$this->readSponsorUser($user_id);
|
|
}
|
|
}
|
|
|
|
public function initBusinesslUserDetail($user)
|
|
{
|
|
$this->business_user = new BusinessUserItem($this->date);
|
|
$this->business_user->makeUser($user->id);
|
|
$this->business_user->checkSponsor($user);
|
|
if (!$this->business_user->isSave()) {
|
|
//Aufbau der Struktur für den User in die unendliche Tiefe.
|
|
$this->business_user->readParentsBusinessUsers();
|
|
//calculate Points in Lines
|
|
if (count($this->business_user->businessUserItems) > 0) {
|
|
$this->calcUserPoints($this->business_user->businessUserItems, 1);
|
|
}
|
|
//qualifikation nach qual_kp (KundenPoints) und qual_pp (PaylinePoints)
|
|
$this->business_user->calcQualPP();
|
|
}
|
|
}
|
|
|
|
/*public function storeBusinesslUser()
|
|
{
|
|
$this->business_user->storeUser();
|
|
}*/
|
|
|
|
public static function isFromStored($month, $year)
|
|
{
|
|
//when is stored an completed
|
|
$UserBusinessStructure = UserBusinessStructure::where('year', $year)->where('month', $month)->first();
|
|
if ($UserBusinessStructure && $UserBusinessStructure->completed) {
|
|
return $UserBusinessStructure;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private function calcUserPoints($businessUserItems, $line)
|
|
{
|
|
if (!isset($this->business_user->business_lines[$line])) {
|
|
$obj = new stdClass();
|
|
$obj->points = 0;
|
|
$this->business_user->addBusinessLineToUser($line, $obj);
|
|
}
|
|
foreach ($businessUserItems as $business_user_item) {
|
|
if (count($business_user_item->businessUserItems) > 0) {
|
|
$this->calcUserPoints($business_user_item->businessUserItems, $line + 1);
|
|
}
|
|
//business_lines points nach line
|
|
$this->business_user->addBusinessLinePoints($line, $business_user_item->sales_volume_points_TP_sum); //TP + Shop Points
|
|
//total_pp gesamte Punkte
|
|
$this->business_user->addTotalTP($business_user_item->sales_volume_points_TP_sum); //TP + Shop Points
|
|
}
|
|
}
|
|
|
|
public function getGrowthBonus()
|
|
{
|
|
if (count($this->business_user->business_lines) > 6) {
|
|
$b_lines = $this->business_user->business_lines->toArray();
|
|
return array_slice($b_lines, 6);
|
|
}
|
|
return [];
|
|
}
|
|
|
|
|
|
public function getKeybyLine($line, $key)
|
|
{
|
|
if ($this->business_user->business_lines) {
|
|
$b_lines = $this->business_user->business_lines;
|
|
if (isset($b_lines[$line])) {
|
|
if ($b_lines[$line] instanceof stdClass) {
|
|
if (isset($b_lines[$line]->{$key})) {
|
|
return $b_lines[$line]->{$key};
|
|
}
|
|
} else {
|
|
if (isset($b_lines[$line][$key])) {
|
|
return $b_lines[$line][$key];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
//* reading from current*//
|
|
private function readRootUsers()
|
|
{
|
|
$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) {
|
|
$BusinessUserItem = new BusinessUserItem($this->date);
|
|
$BusinessUserItem->makeUser($user->id);
|
|
$BusinessUserItem->addUserID();
|
|
$this->business_users[] = $BusinessUserItem;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function readParentsUsers()
|
|
{
|
|
foreach ($this->business_users as $business_user) {
|
|
$business_user->readParentsBusinessUsers();
|
|
}
|
|
}
|
|
|
|
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])) {
|
|
$BusinessUserItem = new BusinessUserItem($this->date);
|
|
$BusinessUserItem->makeUser($user->id);
|
|
$this->parentless[] = $BusinessUserItem;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//* reading from stored*//
|
|
private function readStoredRootUsers(UserBusinessStructure $userBusinessStructure)
|
|
{
|
|
//first level is root
|
|
if ($userBusinessStructure->structure) {
|
|
foreach ($userBusinessStructure->structure as $obj) {
|
|
$BusinessUserItem = new BusinessUserItem($this->date);
|
|
$BusinessUserItem->makeUser($obj->user_id);
|
|
$BusinessUserItem->addUserID();
|
|
$this->business_users[] = $BusinessUserItem;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function readStoredParentsUsers(UserBusinessStructure $userBusinessStructure)
|
|
{
|
|
foreach ($this->business_users as $business_user) {
|
|
$business_user->readStoredParentsBusinessUsers($userBusinessStructure->structure);
|
|
}
|
|
}
|
|
|
|
private function readStoredParentlessUser(UserBusinessStructure $userBusinessStructure)
|
|
{
|
|
if ($userBusinessStructure->parentless) {
|
|
foreach ($userBusinessStructure->parentless as $obj) {
|
|
if (!isset(self::$userIDs[$obj->user_id])) {
|
|
$BusinessUserItem = new BusinessUserItem($this->date);
|
|
$BusinessUserItem->makeUser($obj->user_id);
|
|
$this->parentless[] = $BusinessUserItem;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public function readSponsorUser($user_id)
|
|
{
|
|
$user = User::find($user_id);
|
|
$userSponsor = User::find($user->m_sponsor);
|
|
if ($userSponsor) {
|
|
$this->sponsor = new BusinessUserItem($this->date);
|
|
$this->sponsor->makeUser($userSponsor->id);
|
|
}
|
|
}
|
|
|
|
public function readStoredSponsorUser($user_id)
|
|
{
|
|
|
|
$this->sponsor = new BusinessUserItem($this->date);
|
|
$this->sponsor->makeUser($user_id);
|
|
}
|
|
|
|
|
|
public function getItems()
|
|
{
|
|
return $this->business_users;
|
|
}
|
|
|
|
public function makeHtmlTree()
|
|
{
|
|
$deep = 0;
|
|
$ret = '<ol class="dd-list">';
|
|
foreach ($this->business_users as $business_user) {
|
|
$ret .= $this->addItem($business_user, $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->user_id
|
|
$button = ' | <button type="button" class="btn icon-btn btn-xs btn-secondary" data-toggle="modal" data-target="#modals-load-content"
|
|
data-id="' . $item->user_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="fa fa-calculator"></span></button>';
|
|
}
|
|
return '<li class="dd-item dd-nodrag" data-id="' . $item->user_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') . '">
|
|
<a href="#" class="text-black" data-toggle="modal" data-target="#modals-load-content" data-id="' . $item->user_id . '" data-action="business-user-show" data-back="" data-modal="modal-md" data-init_from="' . $this->init_from . '" data-route="' . route('modal_load') . '">
|
|
<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>
|
|
<a href="mailto: ' . $item->email . '">' . $item->email . '</a>
|
|
' . ($item->user_birthday ? ' | <i class="ion ion-ios-gift text-primary"></i> ' . $item->user_birthday : '') . '
|
|
' . ($item->user_phone ? ' | <i class="ion ion-ios-call text-primary"></i> ' . $item->user_phone : '') . '
|
|
<span class="badge badge-outline-default ' . ($item->active_account ? '' : 'text-muted') . '">' . \App\Services\TranslationHelper::transUserLevelName($item->user_level_name) . ' | ' . $item->m_account . '</span>
|
|
<br><span class="small">' .
|
|
($item->active_account ?
|
|
'<strong>' . __('team.total_points') . ': ' . formatNumber($item->sales_volume_points_KP_sum) . '</strong> | ' . __('team.e') . ': ' . formatNumber($item->sales_volume_KP_points) . ' | ' . __('team.s') . ': ' . formatNumber($item->sales_volume_points_shop) . ' <strong>
|
|
| ' . __('team.net_turnover') . ': ' . formatNumber($item->sales_volume_total_sum) . ' €</strong> | ' . __('team.e') . ': ' . formatNumber($item->sales_volume_total) . ' € | ' . __('team.s') . ': ' . formatNumber($item->sales_volume_total_shop) . ' €' .
|
|
$button
|
|
:
|
|
__('team.account_to') . ': ' . $item->payment_account_date) .
|
|
'</span>
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>' .
|
|
$this->addParentItem($item, $deep) .
|
|
'</li>';
|
|
}
|
|
|
|
private function addParentItem($item, $deep)
|
|
{
|
|
if ($item->businessUserItems) {
|
|
$ret = '<ol class="dd-list dd-nodrag">';
|
|
foreach ($item->businessUserItems 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->user_id . '">' .
|
|
'<div class="dd-handle">
|
|
<span class="' . ($item->active_account ? '' : 'text-muted') . '">
|
|
<a href="#" class="text-black" data-toggle="modal" data-target="#modals-load-content" data-id="' . $item->user_id . '" data-action="business-user-show" data-back="" data-modal="modal-md" data-init_from="' . $this->init_from . '" data-route="' . route('modal_load') . '">
|
|
<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>
|
|
<a href="mailto: ' . $item->email . '">' . $item->email . '</a>
|
|
' . ($item->user_birthday ? ' | <i class="ion ion-ios-gift text-primary"></i> ' . $item->user_birthday : '') . '
|
|
' . ($item->user_phone ? ' | <i class="ion ion-ios-call text-primary"></i> ' . $item->user_phone : '') . '
|
|
<span class="badge badge-outline-default ' . ($item->active_account ? '' : 'text-muted') . '">' . \App\Services\TranslationHelper::transUserLevelName($item->user_level_name) . ' | ' . $item->m_account . '</span>
|
|
<br><span class="small">' .
|
|
($item->active_account ?
|
|
'<strong>' . __('team.total_points') . ': ' . $item->sales_volume_points_KP_sum . '</strong> | ' . __('team.e') . ': ' . $item->sales_volume_KP_points . ' | ' . __('team.s') . ': ' . $item->sales_volume_points_shop . ' <strong>
|
|
| ' . __('team.net_turnover') . ': ' . formatNumber($item->sales_volume_total_sum) . ' €</strong> | ' . __('team.e') . ': ' . formatNumber($item->sales_volume_total) . ' € | ' . __('team.s') . ': ' . formatNumber($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->user_id . '"
|
|
data-action="business-user-detail"
|
|
data-back=""
|
|
data-modal="modal-xl"
|
|
data-route="' . route('modal_load') . '"><span class="fa fa-calculator"></span></button>'
|
|
:
|
|
__('team.account_to') . ' ' . $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="fa 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') . '">
|
|
<a href="#" class="text-black" data-toggle="modal" data-target="#modals-load-content" data-id="' . $this->sponsor->user_id . '" data-action="business-user-show" data-back="" data-init_from="' . $this->init_from . '" data-modal="modal-md" data-route="' . route('modal_load') . '">
|
|
<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>
|
|
<a href="mailto: ' . $this->sponsor->email . '">' . $this->sponsor->email . '</a>
|
|
' . ($this->sponsor->user_birthday ? ' | <i class="ion ion-ios-gift text-primary"></i> ' . $this->sponsor->user_birthday : '') . '
|
|
' . ($this->sponsor->user_phone ? ' | <i class="ion ion-ios-call text-primary"></i> ' . $this->sponsor->user_phone : '') . '
|
|
<span class="badge badge-outline-default ' . ($this->sponsor->active_account ? '' : 'text-muted') . '">' . \App\Services\TranslationHelper::transUserLevelName($this->sponsor->user_level_name) . ' | ' . $this->sponsor->m_account . '</span>';
|
|
|
|
if ($this->init_from === 'admin') {
|
|
$ret .= '<br><span class="small">' .
|
|
($this->sponsor->active_account ?
|
|
'<strong>' . __('team.total_points') . ': ' . $this->sponsor->sales_volume_points_KP_sum . '</strong> | ' . __('team.e') . ': ' . $this->sponsor->sales_volume_KP_points . ' | ' . __('team.s') . ': ' . $this->sponsor->sales_volume_points_shop . ' <strong>
|
|
| ' . __('team.net_turnover') . ': ' . formatNumber($this->sponsor->sales_volume_total_sum) . ' €</strong> | ' . __('team.e') . ': ' . formatNumber($this->sponsor->sales_volume_total) . ' € | ' . __('team.s') . ': ' . formatNumber($this->sponsor->sales_volume_total_shop) . ' €'
|
|
:
|
|
__('team.account_to') . ' ' . $this->sponsor->payment_account_date) .
|
|
'</span>';
|
|
}
|
|
$ret .= '</span>
|
|
</div>
|
|
</li>';
|
|
|
|
return $ret;
|
|
}
|
|
return __('team.no_sponsor_assigned');
|
|
}
|
|
}
|