164 lines
5.2 KiB
PHP
164 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Cron;
|
|
|
|
use App\User;
|
|
use stdClass;
|
|
use App\Models\UserBusinessStructure;
|
|
use App\Services\BusinessPlan\TreeCalcBotOptimized;
|
|
|
|
class BusinessUsersStore
|
|
{
|
|
private $month;
|
|
private $year;
|
|
|
|
private $user_business_structure;
|
|
private $users_structure = [];
|
|
|
|
|
|
public function __construct($month, $year)
|
|
{
|
|
$this->month = $month;
|
|
$this->year = $year;
|
|
}
|
|
|
|
|
|
public function getStoreUserBusinessStructure()
|
|
{
|
|
return UserBusinessStructure::where('year', $this->year)->where('month', $this->month)->first();
|
|
}
|
|
|
|
public function storeUserBusinessStructure()
|
|
{
|
|
if ($this->user_business_structure = $this->getStoreUserBusinessStructure()) {
|
|
return $this->user_business_structure;
|
|
}
|
|
$treeCalcBot = new TreeCalcBotOptimized($this->month, $this->year, 'admin');
|
|
//only load, when no structur is save
|
|
$treeCalcBot->initStructureAdmin(false);
|
|
$this->storeStructure($treeCalcBot);
|
|
}
|
|
|
|
public function storeBusinessUsersDetail()
|
|
{
|
|
if (!$this->user_business_structure) {
|
|
$this->user_business_structure = $this->getStoreUserBusinessStructure();
|
|
if (!$this->user_business_structure) {
|
|
abort(403, 'not found UserBusinessStructure');
|
|
}
|
|
}
|
|
foreach ($this->user_business_structure->users as $user_id => $completed) {
|
|
if ($completed === 0) {
|
|
$user = User::find($user_id);
|
|
if ($user) {
|
|
$TreeCalcBot = new TreeCalcBotOptimized($this->month, $this->year, 'admin');
|
|
$TreeCalcBot->initBusinesslUserDetail($user);
|
|
if (!$TreeCalcBot->business_user) {
|
|
abort(403, 'not found TreeCalcBot->business_user');
|
|
}
|
|
$this->storeBusinesslUser($TreeCalcBot->business_user);
|
|
$users = $this->user_business_structure->users;
|
|
$users[$user_id] = 1;
|
|
$this->user_business_structure->users = $users;
|
|
$this->user_business_structure->save();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public function storeBusinesslUser($business_user)
|
|
{
|
|
$b_user = $business_user->getBUser();
|
|
$b_user->user_items = $this->storeUserItems($business_user->businessUserItems, 1);
|
|
$b_user->b_structure_id = $this->user_business_structure->id;
|
|
$b_user->save();
|
|
}
|
|
|
|
|
|
public function storeBusinessCompleted()
|
|
{
|
|
if (!$this->user_business_structure) {
|
|
$this->user_business_structure = $this->getStoreUserBusinessStructure();
|
|
}
|
|
foreach ($this->user_business_structure->users as $user_id => $completed) {
|
|
if ($completed === 0) {
|
|
return false;
|
|
}
|
|
$this->user_business_structure->completed = 1;
|
|
$this->user_business_structure->save();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
private function storeUserItems($userItems, $line)
|
|
{
|
|
$ret = [];
|
|
foreach ($userItems as $userItem) {
|
|
$temp = null;
|
|
if (count($userItem->businessUserItems) > 0) {
|
|
$temp = $this->storeUserItems($userItem->businessUserItems, $line + 1);
|
|
}
|
|
$obj = new stdClass();
|
|
$obj->user_id = $userItem->user_id;
|
|
$obj->line = $line;
|
|
$obj->points = $userItem->sales_volume_points_sum;
|
|
$obj->parents = $temp;
|
|
$ret[] = $obj;
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
|
|
private function storeStructure($treeCalcBot)
|
|
{
|
|
/*if($this->user_business_structure = $this->getStoreUserBusinessStructure()){
|
|
return $this->user_business_structure;
|
|
}*/
|
|
|
|
$structure = [];
|
|
foreach ($treeCalcBot->business_users as $business_user) {
|
|
$structure[] = $this->storeStructureItem($business_user, 0);
|
|
}
|
|
|
|
$parentless = [];
|
|
if ($treeCalcBot->parentless) {
|
|
foreach ($treeCalcBot->parentless as $pless) {
|
|
$parentless[] = $this->storeStructureItem($pless, 0);
|
|
}
|
|
}
|
|
$fill = [
|
|
'month' => $this->month,
|
|
'year' => $this->year,
|
|
'structure' => $structure,
|
|
'parentless' => $parentless,
|
|
'users' => $this->users_structure,
|
|
'completed' => false,
|
|
'status' => 0
|
|
];
|
|
$this->user_business_structure = UserBusinessStructure::create($fill);
|
|
return $this->user_business_structure;
|
|
}
|
|
|
|
|
|
|
|
private function storeStructureItem($item, $deep)
|
|
{
|
|
$temp = null;
|
|
if ($item->businessUserItems) {
|
|
foreach ($item->businessUserItems as $parent) {
|
|
$temp[] = $this->storeStructureItem($parent, $deep + 1);
|
|
}
|
|
}
|
|
$this->users_structure[$item->user_id] = 0;
|
|
$obj = new stdClass();
|
|
$obj->user_id = $item->user_id;
|
|
//$obj->name = $item->first_name .' '. $item->last_name ;
|
|
$obj->email = $item->email;
|
|
$obj->deep = $deep;
|
|
//$obj->points = $item->sales_volume_points_sum;
|
|
$obj->parents = $temp;
|
|
return $obj;
|
|
}
|
|
}
|