mivita/app/Services/BusinessPlan/ExportBot.php
2025-04-01 10:36:47 +02:00

121 lines
4 KiB
PHP

<?php
namespace App\Services\BusinessPlan;
use App\User;
use stdClass;
use Carbon;
class ExportBot
{
public $date;
private $init_from;
private $order;
public $user_tree;
public $user_list;
private $root_user;
public function __construct($init_from = 'member')
{
$this->date = Carbon::now();
$this->init_from = $init_from;
}
public function initStructureUser(User $user, $order = 'list') //tree or list
{
$line = 0;
$this->order = $order;
$this->root_user = $user;
if ($this->order === 'tree') {
$this->user_tree = $this->setUserValues($user, $line);
$this->user_tree->childs = $this->readChildUsers($this->user_tree, $line + 1);
}
if ($this->order === 'list') {
$this->user_list = $this->setUserValues($user, $line);
$this->readChildUsers($this->user_list, $line + 1);
}
}
private function readChildUsers($parent_user, $line)
{
$childUsers = [];
$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', "=", $parent_user->user_id) //<- need the id for childs / sponsors
->where('users.payment_account', "!=", null)
->where('users.active', "=", 1)
->get();
if ($users) {
foreach ($users as $user) {
$user_values = $this->setUserValues($user, $line);
$childUsers[] = $user_values;
if ($this->order === 'list') {
$this->user_list->childs[] = $user_values;
$this->readChildUsers($user_values, $line + 1);
}
}
}
return $childUsers;
}
private function setUserValues(User $user, $line)
{
$sponsor_name = "";
if ($user->m_sponsor && $user->user_sponsor->account) {
$sponsor_name = $user->user_sponsor->account->m_first_name . " " . $user->user_sponsor->account->m_last_name;
}
$obj = new stdClass();
$obj->line = $line;
$obj->user_id = $user->id;
$obj->m_level = $user->m_level;
$obj->level_name = $user->user_level ? $user->user_level->getLang('name') : '';
$obj->m_sponsor = $user->m_sponsor;
$obj->sponsor_name = $sponsor_name;
$obj->m_account = $user->m_account;
$obj->email = $user->email;
$obj->active_account = $user->payment_account ? Carbon::parse($user->payment_account)->gt(Carbon::parse($this->date->format('Y-m-d H:i:s'))) : 0;
$obj->payment_account_date = $user->payment_account ? $user->getPaymentAccountDateFormat(false) : "";
$obj->first_name = $user->account->first_name;
$obj->last_name = $user->account->last_name;
$obj->address = $user->account->address;
$obj->address_2 = $user->account->address_2;
$obj->zipcode = $user->account->zipcode;
$obj->city = $user->account->city;
$obj->country_id = $user->account->country_id ? $user->account->country->getLocated() : "";
$pre_phone = $user->account->pre_phone_id != "" ? $user->account->pre_phone->phone . " " : "";
$pre_mobil = $user->account->pre_mobil_id != "" ? $user->account->pre_mobil->phone . " " : "";
$obj->phone = $pre_phone . $user->account->phone;
$obj->mobil = $pre_mobil . $user->account->mobil;
$obj->birthday = $user->account->birthday;
$obj->partner_since = $user->active_date ? $user->getActiveDateFormat(false) : "";
if ($this->order === 'tree') {
$obj->childs = $this->readChildUsers($obj, $line + 1);
}
if ($this->order === 'list') {
$obj->childs = [];
}
return $obj;
}
public function getUser()
{
return $this->root_user;
}
}