263 lines
10 KiB
PHP
263 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Services\BusinessPlan;
|
|
|
|
use App\User;
|
|
use stdClass;
|
|
use App\Services\Util;
|
|
use App\Models\ShoppingOrder;
|
|
use App\Models\UserSalesVolume;
|
|
use App\Events\BusinessDataChanged;
|
|
|
|
class SalesPointsVolume
|
|
{
|
|
|
|
public static function changeSalesPointsVolumeUser(ShoppingOrder $shoppingOrder, $to_user_id)
|
|
{
|
|
|
|
if ($shoppingOrder->user_sales_volume) {
|
|
$to_user_id = intval($to_user_id);
|
|
if ($shoppingOrder->user_sales_volume->user_id === $to_user_id) {
|
|
\Session()->flash('alert-error', 'Keine Änderung: selber Berater');
|
|
return;
|
|
}
|
|
if (!$shoppingOrder->user_sales_volume->isCurrentMonthYear()) {
|
|
\Session()->flash('alert-error', 'Änderung muss im selben Monat sein');
|
|
return;
|
|
}
|
|
|
|
$month = $shoppingOrder->user_sales_volume->month;
|
|
$year = $shoppingOrder->user_sales_volume->year;
|
|
$form_user_id = $shoppingOrder->user_sales_volume->user_id;
|
|
|
|
$to_user = User::find($to_user_id);
|
|
$form_user = User::find($form_user_id);
|
|
|
|
$shoppingOrder->user_sales_volume->user_id = $to_user_id;
|
|
$shoppingOrder->user_sales_volume->message = 'zugewiesen: ' . date('d.m.Y');
|
|
|
|
$syslog = $shoppingOrder->user_sales_volume->syslog;
|
|
$from_email = $form_user ? $form_user->email : '';
|
|
$to_email = $to_user ? $to_user->email : '';
|
|
$syslog[date('d.m.Y-h:i:s')] = 'change form: #' . $form_user_id . ' ' . $from_email . ' to: #' . $to_user_id . ' ' . $to_email;
|
|
$shoppingOrder->user_sales_volume->syslog = $syslog;
|
|
|
|
$shoppingOrder->user_sales_volume->save();
|
|
|
|
//recalculate
|
|
self::reCalculateSalesPointsVolume($to_user_id, $month, $year);
|
|
self::reCalculateSalesPointsVolume($form_user_id, $month, $year);
|
|
\Session()->flash('alert-save', true);
|
|
}
|
|
}
|
|
|
|
private static function add_KP_TP_Points($userSalesVolume, $month_points)
|
|
{
|
|
if ($userSalesVolume->status_points === 2) { //KP
|
|
$month_points->KP += $userSalesVolume->points;
|
|
} else {
|
|
// === 1 //TP + KP
|
|
$month_points->KP += $userSalesVolume->points;
|
|
$month_points->TP += $userSalesVolume->points;
|
|
}
|
|
return $month_points;
|
|
}
|
|
|
|
public static function reCalculateSalesPointsVolume($user_id, $month, $year)
|
|
{
|
|
|
|
$userSalesVolumes = UserSalesVolume::where('user_id', $user_id)->where('month', $month)->where('year', $year)->orderBy('id', 'ASC')->get();
|
|
$month_points = new stdClass();
|
|
$month_points->KP = 0;
|
|
$month_points->TP = 0;
|
|
$month_total_net = 0;
|
|
$month_shop_points = 0;
|
|
$month_shop_total_net = 0;
|
|
//TDOO Status === 3???
|
|
|
|
foreach ($userSalesVolumes as $userSalesVolume) {
|
|
switch ($userSalesVolume->status) {
|
|
case 1: //Bestellung Berater
|
|
$month_points = self::add_KP_TP_Points($userSalesVolume, $month_points);
|
|
$month_total_net += $userSalesVolume->total_net;
|
|
break;
|
|
case 2: //Shop
|
|
$month_shop_points += $userSalesVolume->points;
|
|
$month_shop_total_net += $userSalesVolume->total_net;
|
|
break;
|
|
case 4: //Gutschrift
|
|
$month_points = self::add_KP_TP_Points($userSalesVolume, $month_points);
|
|
|
|
if ($userSalesVolume->status_turnover === 2) {
|
|
$month_shop_total_net += $userSalesVolume->total_net;
|
|
//ggf hier zu den Shop Points zählen wäre aber immer KP + TP kann nicht keine trennung bei month_shop_points
|
|
} else {
|
|
$month_total_net += $userSalesVolume->total_net;
|
|
}
|
|
|
|
break;
|
|
case 5: //Registrierung
|
|
$month_points = self::add_KP_TP_Points($userSalesVolume, $month_points);
|
|
$month_total_net += $userSalesVolume->total_net;
|
|
break;
|
|
}
|
|
$userSalesVolume->month_shop_points = $month_shop_points;
|
|
$userSalesVolume->month_shop_total_net = $month_shop_total_net;
|
|
$userSalesVolume->month_KP_points = $month_points->KP;
|
|
$userSalesVolume->month_TP_points = $month_points->TP;
|
|
$userSalesVolume->month_total_net = $month_total_net;
|
|
$userSalesVolume->save();
|
|
}
|
|
|
|
// Event für Business-Neuberechnung (Bubble Up zur Upline)
|
|
if ($user_id) {
|
|
event(new BusinessDataChanged($user_id, BusinessDataChanged::TYPE_SALES_VOLUME, (int)$month, (int)$year));
|
|
}
|
|
}
|
|
|
|
|
|
public static function addSalesPointsVolumeUser(ShoppingOrder $shoppingOrder)
|
|
{
|
|
|
|
/*
|
|
status
|
|
1 => 'hinzugefügt aus Bestellung',
|
|
2 => 'hinzugefügt aus Shop',
|
|
3 => 'hinzugefügt aus Shop / pending',
|
|
*/
|
|
|
|
$status = self::getStatusByOrderPaymentFor($shoppingOrder);
|
|
$user_id = $shoppingOrder->auth_user_id ? $shoppingOrder->auth_user_id : $shoppingOrder->member_id;
|
|
//akuteller tag / Monat.
|
|
$month = date('m');
|
|
$year = date('Y');
|
|
$date = date('d.m.Y');
|
|
|
|
|
|
if ($status === 3) { //shop bestellung User pending if is_like
|
|
$user_id = NULL;
|
|
}
|
|
$user_sales_volume = UserSalesVolume::create([
|
|
'user_id' => $user_id,
|
|
'shopping_order_id' => $shoppingOrder->id,
|
|
'month' => $month,
|
|
'year' => $year,
|
|
'date' => $date,
|
|
'points' => $shoppingOrder->points,
|
|
'total_net' => $shoppingOrder->subtotal,
|
|
'status_points' => 1, //KP + TP
|
|
'message' => '',
|
|
'status' => $status,
|
|
]);
|
|
|
|
if ($status !== 3) {
|
|
self::reCalculateSalesPointsVolume($user_sales_volume->user_id, $user_sales_volume->month, $user_sales_volume->year);
|
|
}
|
|
|
|
return $user_sales_volume;
|
|
}
|
|
|
|
public static function setToUserAndReCalculate(UserSalesVolume $user_sales_volume, $user_id)
|
|
{
|
|
|
|
//set month year date new, calculate it in the currently month!
|
|
//If the month has changed, it can no longer be added to the month before
|
|
$month = date('m');
|
|
$year = date('Y');
|
|
$date = date('d.m.Y');
|
|
|
|
$user_sales_volume->user_id = $user_id;
|
|
$user_sales_volume->month = $month;
|
|
$user_sales_volume->year = $year;
|
|
$user_sales_volume->date = $date;
|
|
$user_sales_volume->status = 2; //hinzugefügt aus Shop can only Pending
|
|
$user_sales_volume->save();
|
|
|
|
self::reCalculateSalesPointsVolume($user_id, $month, $year);
|
|
}
|
|
|
|
public static function getStatusByOrderPaymentFor(ShoppingOrder $shoppingOrder)
|
|
{
|
|
if ($shoppingOrder->payment_for) {
|
|
if ($shoppingOrder->payment_for === 6) { //Kunde-Shop
|
|
if ($shoppingOrder->shopping_user && $shoppingOrder->shopping_user->is_like) {
|
|
return 3; //shop Kunden, berater zuordnen <- need?
|
|
}
|
|
return 2;
|
|
}
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
public static function editSalesPointsVolume($data)
|
|
{
|
|
$user_sales_volume = UserSalesVolume::findOrFail($data['id']);
|
|
if (!$user_sales_volume->isCurrentMonthYear()) {
|
|
\Session()->flash('alert-error', 'Änderung muss im selben Monat sein');
|
|
return;
|
|
}
|
|
$old_points = $user_sales_volume->points;
|
|
$old_total_net = $user_sales_volume->total_net;
|
|
$user_sales_volume->total_net = Util::reFormatNumber($data['total_net']);
|
|
$user_sales_volume->points = Util::reFormatNumber($data['points']);
|
|
|
|
$user_sales_volume->message = 'geändert: ' . date('d.m.Y');
|
|
$user_sales_volume->info = $data['info'];
|
|
$user_sales_volume->status_points = $data['status_points'];
|
|
$user_sales_volume->status_turnover = isset($data['status_turnover']) ? intval($data['status_turnover']) : null;
|
|
|
|
$syslog = $user_sales_volume->syslog;
|
|
$syslog[date('d.m.Y-h:i:s')] = 'edit points: #' . $old_points . ' ' . $user_sales_volume->points . ' total: #' . $old_total_net . ' ' . $user_sales_volume->total_net;
|
|
$user_sales_volume->syslog = $syslog;
|
|
|
|
$user_sales_volume->save();
|
|
|
|
self::reCalculateSalesPointsVolume($user_sales_volume->user_id, $user_sales_volume->month, $user_sales_volume->year);
|
|
|
|
\Session()->flash('alert-success', "Points geändert");
|
|
|
|
return;
|
|
}
|
|
|
|
public static function addSalesPointsVolume($data)
|
|
{
|
|
|
|
if (!isset($data['user_id'])) {
|
|
\Session()->flash('alert-error', 'Kein Berater ausgewählt');
|
|
return;
|
|
}
|
|
$user = User::findOrFail($data['user_id']);
|
|
$month = date('m');
|
|
$year = date('Y');
|
|
$date = date('d.m.Y');
|
|
|
|
$total_net = isset($data['total_net']) ? Util::reFormatNumber($data['total_net']) : 0;
|
|
$points = isset($data['points']) ? Util::reFormatNumber($data['points']) : 0;
|
|
$syslog[date('d.m.Y-h:i:s')] = 'add points: #' . $points . ' total: #' . $total_net;
|
|
$status = isset($data['status']) ? intval($data['status']) : 4;
|
|
$status_turnover = isset($data['status_turnover']) ? intval($data['status_turnover']) : null;
|
|
|
|
$user_sales_volume = UserSalesVolume::create([
|
|
'user_id' => $user->id,
|
|
'shopping_order_id' => null,
|
|
'month' => $month,
|
|
'year' => $year,
|
|
'date' => $date,
|
|
'points' => $points,
|
|
'status_points' => $data['status_points'],
|
|
'status_turnover' => $status_turnover,
|
|
'total_net' => $total_net,
|
|
'message' => 'hinzugefügt: ' . date('d.m.Y'),
|
|
'info' => $data['info'],
|
|
'syslog' => $syslog,
|
|
'status' => $status,
|
|
]);
|
|
|
|
|
|
self::reCalculateSalesPointsVolume($user_sales_volume->user_id, $user_sales_volume->month, $user_sales_volume->year);
|
|
|
|
\Session()->flash('alert-success', "Points hinzugefügt");
|
|
}
|
|
}
|