mivita/app/Http/Controllers/User/HomepartyController.php
2026-01-23 17:35:23 +01:00

657 lines
30 KiB
PHP

<?php
namespace App\Http\Controllers\User;
use Auth;
use Yard;
use Request;
use App\User;
use Validator;
use App\Services\Util;
use App\Models\Product;
use App\Models\Homeparty;
use App\Models\UserHistory;
use App\Models\HomepartyUser;
use App\Services\UserService;
use App\Models\ShippingCountry;
use App\Services\HomepartyCart;
use App\Models\ShoppingInstance;
use App\Http\Controllers\Controller;
use App\Models\HomepartyUserOrderItem;
class HomepartyController extends Controller
{
public function __construct()
{
$this->middleware('active.account');
}
public function index()
{
$data = [
'homepartys' => Homeparty::where('auth_user_id', '=', \Auth::user()->id)->orderByDesc('id')->get(),
];
return view('user.homeparty.index', $data);
}
public function detail($id, $step = false)
{
if ($id === 'new') {
$homeparty = new Homeparty();
$homeparty->id = 0;
$step = 1;
} else {
$homeparty = $this->getHomparty($id);
if ($homeparty->step < 10) {
$step = $homeparty->step;
} else {
if (!$step) {
$step = 10;
}
}
}
if ($homeparty->homeparty_host) {
$homeparty_user = $homeparty->homeparty_host;
} else {
$homeparty_user = new HomepartyUser();
$homeparty_user->is_host = true;
}
if ($homeparty->completed) {
abort(404);
}
$data = [
'homeparty' => $homeparty,
'homeparty_user' => $homeparty_user,
'step' => $step,
];
return view('user.homeparty.detail', $data);
}
public function store($id = null, $step = false)
{
$data = Request::all();
if ($data['action'] === 'homeparty-party-store-detail') {
$rules = array(
'date' => 'required',
'name' => 'required',
'place' => 'required',
);
if (!$id) {
$rules = array(
'date' => 'required',
'name' => 'required',
'place' => 'required',
'country_id' => 'required'
);
}
}
if ($data['action'] === 'homeparty-party-store-address') {
$rules = array(
'shipping_firstname' => 'required',
'shipping_lastname' => 'required',
'shipping_address' => 'required',
'shipping_zipcode' => 'required',
'shipping_city' => 'required',
'shipping_salutation' => 'required',
'shipping_country_id' => 'required'
);
}
if ($data['action'] === 'homeparty-party-store-host') {
$rules = array(
'billing_salutation' => 'required',
'billing_firstname' => 'required',
'billing_lastname' => 'required',
'billing_address' => 'required',
'billing_zipcode' => 'required',
'billing_city' => 'required',
'billing_country_id' => 'required',
);
}
$validator = Validator::make(Request::all(), $rules);
if ($validator->fails()) {
return back()->withErrors($validator)->withInput(Request::all());
}
if ($data['action'] === 'homeparty-party-store-detail') {
if (!$id) {
//first save create and empty user/host
do {
$token = Util::uuidToken();
} while (Homeparty::where('token', $token)->count());
$data['token'] = $token;
$data['auth_user_id'] = \Auth::user()->id;
$data['step'] = 2;
$step = 2;
$homeparty = Homeparty::create($data);
$this->storeTranslations($homeparty, \App::getLocale(), $data);
$homeparty_user = HomepartyUser::create([
'homeparty_id' => $homeparty->id,
'auth_user_id' => \Auth::user()->id,
'shipping_country_id' => $homeparty->country_id,
'billing_country_id' => $homeparty->country_id,
'same_as_billing' => false,
'is_host' => true,
]);
} else {
$homeparty = $this->getHomparty($id);
$homeparty->fill($data)->save();
$this->storeTranslations($homeparty, \App::getLocale(), $data);
$step = 10;
}
}
if ($data['action'] === 'homeparty-party-store-address') {
$homeparty = $this->getHomparty($id);
$homeparty_user = $homeparty->homeparty_host;
$homeparty_user->fill($data)->save();
if ($homeparty->step === 2) {
$homeparty->step = 3;
$homeparty->save();
$step = 3;
} else {
$step = 12;
}
}
if ($data['action'] === 'homeparty-party-store-host') {
$homeparty = $this->getHomparty($id);
$homeparty_user = $homeparty->homeparty_host;
$homeparty_user->fill($data)->save();
if ($homeparty->step === 3) {
$homeparty->step = 10;
$homeparty->save();
$step = 10;
} else {
$step = 13;
}
}
\Session()->flash('alert-save', '1');
return redirect(route('user_homeparty_detail', [$homeparty->id, $step]));
}
private function storeTranslations($homeparty, $lang, $data)
{
if ($lang == 'de') {
$homeparty->description = $data['description'];
$homeparty->save();
return;
}
$trans = $homeparty->trans_description;
$trans[$lang] = $data['description'];
$homeparty->trans_description = $trans;
$homeparty->save();
return;
}
public function guests($id = null)
{
$homeparty = $this->getHomparty($id);
$data = [
'homeparty' => $homeparty,
];
return view('user.homeparty.guests', $data);
}
public function guestDetail($id = null, $gid = null)
{
$homeparty = $this->getHomparty($id);
if ($gid === 'new') {
$homeparty_user = new HomepartyUser();
$homeparty_user->same_as_billing = true;
$homeparty_user->billing_country_id = $homeparty->country_id;
$homeparty_user->shipping_country_id = $homeparty->country_id;
} else {
$homeparty_user = HomepartyUser::findOrFail($gid);
if ($homeparty->id !== $homeparty_user->homeparty_id) {
abort(404);
}
}
if ($homeparty->completed) {
abort(404);
}
$data = [
'homeparty' => $homeparty,
'homeparty_user' => $homeparty_user,
];
return view('user.homeparty.guest_detail', $data);
}
public function guestStore($id = null, $gid = null)
{
$data = Request::all();
$rules = array(
'billing_salutation' => 'required',
'billing_firstname' => 'required',
'billing_lastname' => 'required',
'billing_address' => 'required',
'billing_zipcode' => 'required',
'billing_city' => 'required',
'billing_country_id' => 'required',
);
if (!Request::get('same_as_billing')) {
$rules = array_merge($rules, [
'shipping_firstname' => 'required',
'shipping_lastname' => 'required',
'shipping_address' => 'required',
'shipping_zipcode' => 'required',
'shipping_city' => 'required',
'shipping_salutation' => 'required',
'shipping_country_id' => 'required'
]);
}
$validator = Validator::make(Request::all(), $rules);
if ($validator->fails()) {
return back()->withErrors($validator)->withInput(Request::all());
}
$homeparty = $this->getHomparty($id);
if ($gid === null) {
$homeparty_user = HomepartyUser::create([
'homeparty_id' => $homeparty->id,
'auth_user_id' => \Auth::user()->id,
'is_host' => false,
]);
} else {
$homeparty_user = HomepartyUser::findOrFail($gid);
}
if ($homeparty->id !== $homeparty_user->homeparty_id) {
abort(404);
}
$data['same_as_billing'] = isset($data['same_as_billing']) ? true : false;
$data['shipping_country_id'] = isset($data['shipping_country_id']) ? $data['shipping_country_id'] : $data['billing_country_id'];
$homeparty_user->fill($data)->save();
\Session()->flash('alert-save', '1');
return redirect(route('user_homeparty_guests', [$homeparty->id]));
}
public function order($id = null)
{
$user = User::find(Auth::user()->id);
$homeparty = $this->getHomparty($id);
$shipping_country_id = $this->checkShoppingCountry($homeparty->country_id);
if (!$shipping_country_id) {
\Session()->flash('custom-error', __('validation.custom.shipping_not_found'));
return redirect(route('user_homepartys'));
}
UserService::checkUserTaxShippingCountry($user, $shipping_country_id);
if ($this->userChangeCountry($homeparty)) {
\Session()->flash('custom-error', __('msg.country_account_has_been_changed__cost_has_been_reset'));
return redirect(route('user_homeparty_order', [$homeparty->id]));
}
HomepartyCart::calculateHomeparty($homeparty);
$homeparty->card_info = UserService::getYardInfo();
$homeparty->save();
$userHistoryPaymentOrder = UserHistory::whereUserId($user->id)->whereAction('payment_homeparty')->where('referenz', $homeparty->id)->get()->last();
$data = [
'homeparty' => $homeparty,
'userHistoryPaymentOrder' => $userHistoryPaymentOrder,
];
return view('user.homeparty.order', $data);
}
private function userChangeCountry($homeparty)
{
if (isset($homeparty->card_info['user_country_id'])) {
if ($homeparty->card_info['user_country_id'] !== UserService::$user_country->id) {
// es wurde schon eine order angelegt, aber das Rechungsland geändert
if ($homeparty->homeparty_order_items->count()) {
foreach ($homeparty->homeparty_order_items as $homeparty_order_item) {
$homeparty_order_item->delete();
}
return true;
}
}
}
return false;
}
private function checkShoppingCountry($country_id)
{
if ($country_id) {
if ($shipping_country = ShippingCountry::whereCountryId($country_id)->first()) {
return $shipping_country->id;
}
}
return false;
}
//perform Request
public function orderStore($id = null)
{
$homeparty = $this->getHomparty($id);
if (Request::ajax()) {
$data = Request::all();
if ($data['action'] === 'addProduct') {
if ($data['homeparty_id'] == $homeparty->id) {
$homeparty_user = HomepartyUser::findOrFail($data['homeparty_user_id']);
if ($homeparty_user->homeparty_id !== $homeparty->id) {
abort(404);
}
if ($product = Product::find($data['product_id'])) {
$margin = 0;
if (\Auth::user() && \Auth::user()->user_level) {
$margin = \Auth::user()->user_level->margin;
}
$HomepartyUserOrderItem = HomepartyUserOrderItem::where('homeparty_user_id', $homeparty_user->id)->where('product_id', $product->id)->first();
if ($HomepartyUserOrderItem) {
$HomepartyUserOrderItem->qty = $HomepartyUserOrderItem->qty + 1;
$HomepartyUserOrderItem->save();
} else {
if ($homeparty->getCardInfo('user_tax_free')) {
$HomepartyUserOrderItem = HomepartyUserOrderItem::create([
'homeparty_id' => $homeparty->id,
'homeparty_user_id' => $homeparty_user->id,
'product_id' => $product->id,
'qty' => 1,
'price' => $product->getPriceWith(true, false, $homeparty->getUserCountry()),
'price_net' => $product->getPriceWith(true, false, $homeparty->getUserCountry()),
'tax_rate' => 0,
'points' => $product->points,
'margin' => $margin,
'ek_price' => $product->getPriceWith(true, true, $homeparty->getUserCountry()),
'ek_price_net' => $product->getPriceWith(true, true, $homeparty->getUserCountry()),
'slug' => $product->slug
]);
} else {
$HomepartyUserOrderItem = HomepartyUserOrderItem::create([
'homeparty_id' => $homeparty->id,
'homeparty_user_id' => $homeparty_user->id,
'product_id' => $product->id,
'qty' => 1,
'price' => $product->getPriceWith(false, false, $homeparty->getUserCountry()),
'price_net' => $product->getPriceWith(true, false, $homeparty->getUserCountry()),
'tax_rate' => $product->getTaxWith($homeparty->getUserCountry()),
'points' => $product->points,
'margin' => $margin,
'ek_price' => $product->getPriceWith(false, true, $homeparty->getUserCountry()),
'ek_price_net' => $product->getPriceWith(true, true, $homeparty->getUserCountry()),
'slug' => $product->slug
]);
}
}
}
}
$homeparty_user = HomepartyUser::findOrFail($data['homeparty_user_id']);
HomepartyCart::calculateHomeparty($homeparty);
$html_user_cart = view("user.homeparty.show_products_order", ['homeparty' => $homeparty, 'homeparty_guest' => $homeparty_user])->render();
$html_bonus = view("user.homeparty.show_bonus", ['homeparty' => $homeparty])->render();
$html_host_bonus = view("user.homeparty.show_calc_bonus_host", ['homeparty' => $homeparty])->render();
$html_total = view("user.homeparty.show_total_order", ['homeparty' => $homeparty])->render();
return response()->json(['response' => true, 'data' => $data, 'html_user_cart' => $html_user_cart, 'html_bonus' => $html_bonus, 'html_host_bonus' => $html_host_bonus, 'html_total' => $html_total]);
}
if ($data['action'] === 'updateCart') {
if ($data['homeparty_id'] == $homeparty->id) {
$homeparty_user = HomepartyUser::findOrFail($data['homeparty_user_id']);
if ($homeparty_user->homeparty_id !== $homeparty->id) {
abort(404);
}
if (isset($data['product_id']) && $product = Product::find($data['product_id'])) {
if (isset($data['order_item_id']) && $HomepartyUserOrderItem = HomepartyUserOrderItem::find($data['order_item_id'])) {
if (isset($data['qty'])) {
$qty = (int) $data['qty'];
$qty = $qty < 1 ? 1 : $qty;
$qty = $qty > 100 ? 100 : $qty;
$HomepartyUserOrderItem->qty = $qty;
$HomepartyUserOrderItem->save();
}
}
}
}
$homeparty_user = HomepartyUser::findOrFail($data['homeparty_user_id']);
HomepartyCart::calculateHomeparty($homeparty);
$html_user_cart = view("user.homeparty.show_products_order", ['homeparty' => $homeparty, 'homeparty_guest' => $homeparty_user])->render();
$html_bonus = view("user.homeparty.show_bonus", ['homeparty' => $homeparty])->render();
$html_host_bonus = view("user.homeparty.show_calc_bonus_host")->render();
$html_total = view("user.homeparty.show_total_order", ['homeparty' => $homeparty])->render();
return response()->json(['response' => true, 'data' => $data, 'html_user_cart' => $html_user_cart, 'html_bonus' => $html_bonus, 'html_host_bonus' => $html_host_bonus, 'html_total' => $html_total]);
}
if ($data['action'] === 'removeFromCart') {
if ($data['homeparty_id'] == $homeparty->id) {
$homeparty_user = HomepartyUser::findOrFail($data['homeparty_user_id']);
if ($homeparty_user->homeparty_id !== $homeparty->id) {
abort(404);
}
if (isset($data['product_id']) && $product = Product::find($data['product_id'])) {
if (isset($data['order_item_id']) && $HomepartyUserOrderItem = HomepartyUserOrderItem::find($data['order_item_id'])) {
$HomepartyUserOrderItem->delete();
}
}
}
$homeparty_user = HomepartyUser::findOrFail($data['homeparty_user_id']);
HomepartyCart::calculateHomeparty($homeparty);
$html_user_cart = view("user.homeparty.show_products_order", ['homeparty' => $homeparty, 'homeparty_guest' => $homeparty_user])->render();
$html_bonus = view("user.homeparty.show_bonus", ['homeparty' => $homeparty])->render();
$html_host_bonus = view("user.homeparty.show_calc_bonus_host")->render();
$html_total = view("user.homeparty.show_total_order", ['homeparty' => $homeparty])->render();
return response()->json(['response' => true, 'data' => $data, 'html_user_cart' => $html_user_cart, 'html_bonus' => $html_bonus, 'html_host_bonus' => $html_host_bonus, 'html_total' => $html_total]);
}
if ($data['action'] === 'updateDeliveryOption') {
if ($data['homeparty_id'] == $homeparty->id) {
$homeparty_user = HomepartyUser::findOrFail($data['homeparty_user_id']);
if ($homeparty_user->homeparty_id !== $homeparty->id) {
abort(404);
}
if (isset($data['delivery'])) {
$homeparty_user->delivery = $data['delivery'];
$homeparty_user->save();
}
}
$homeparty_user = HomepartyUser::findOrFail($data['homeparty_user_id']);
HomepartyCart::calculateHomeparty($homeparty);
$html_user_cart = view("user.homeparty.show_products_order", ['homeparty' => $homeparty, 'homeparty_guest' => $homeparty_user])->render();
$html_bonus = view("user.homeparty.show_bonus", ['homeparty' => $homeparty])->render();
$html_host_bonus = view("user.homeparty.show_calc_bonus_host")->render();
$html_total = view("user.homeparty.show_total_order", ['homeparty' => $homeparty])->render();
return response()->json(['response' => true, 'data' => $data, 'html_user_cart' => $html_user_cart, 'html_bonus' => $html_bonus, 'html_host_bonus' => $html_host_bonus, 'html_total' => $html_total]);
}
return response()->json(['response' => false, 'data' => $data]);
}
HomepartyCart::calculateHomeparty($homeparty);
if (\App\Services\HomepartyCart::$price === 0) {
\Session()->flash('alert-error', __('msg.your_shopping_cart_is_empty_please_add_products_first'));
return redirect(route('user_homeparty_order', [$homeparty->id]));
}
//save the calucalte card!
$time = time();
$date = date('d.m.Y H:i:s', $time);
$user = User::find(Auth::user()->id);
Yard::instance('shopping')->destroy();
$cartItem = Yard::instance('shopping')->add($homeparty->id, 'Bestellung Homeparty ' . $date, 1, \App\Services\HomepartyCart::$ek_price, false, false, ['image' => "", 'slug' => $time, 'weight' => 0]);
Yard::setTax($cartItem->rowId, 0);
do {
$identifier = Util::getToken();
} while (ShoppingInstance::where('identifier', $identifier)->count());
HomepartyCart::store($identifier, $date);
$data = [];
$data['is_from'] = 'homeparty';
if ($homeparty->getCardInfo('user_tax_free')) {
$data['shop_price'] = HomepartyCart::getFormattedEkPrice();
$data['shop_price_net'] = HomepartyCart::getFormattedEkPrice();
$data['shop_price_tax'] = 0;
$data['user_tax_free'] = true;
} else {
$data['shop_price'] = HomepartyCart::getFormattedEkPrice();
$data['shop_price_net'] = HomepartyCart::getFormattedEkPriceNet();
$data['shop_price_tax'] = HomepartyCart::getFormattedEkPriceTax();
$data['user_tax_free'] = false;
}
$data['homeparty_id'] = $homeparty->id;
$data['is_for'] = 'hp';
$data['user_price_infos'] = $homeparty->card_info;
ShoppingInstance::create([
'identifier' => $identifier,
'user_shop_id' => 1, //is first faker shop for nuy intern
'auth_user_id' => Auth::user()->id,
'payment' => 5, //Berater Homeparty
'subdomain' => url('/'),
'country_id' => Yard::instance('shopping')->getShippingCountryId(),
'language' => \App::getLocale(),
'shopping_data' => $data,
'back' => url()->previous(),
]);
HomepartyCart::store($identifier, $date);
Yard::instance('shopping')->store($identifier);
$path = route('checkout.checkout_card', ['identifier' => $identifier]);
UserHistory::create(['user_id' => $user->id, 'action' => 'payment_homeparty', 'status' => 1, 'referenz' => $homeparty->id, 'identifier' => $identifier]);
//$path = str_replace('http', 'https', $path);
return redirect()->secure($path);
}
public function delete($do, $id = null, $gid = null)
{
$homeparty = $this->getHomparty($id);
if ($do === 'hpu') {
$homeparty_user = HomepartyUser::findOrFail($gid);
if ($homeparty->id !== $homeparty_user->homeparty_id) {
abort(404);
}
if ($homeparty_user->homeparty_user_order_items) {
foreach ($homeparty_user->homeparty_user_order_items as $homeparty_user_order_item) {
$homeparty_user_order_item->delete();
}
}
//$homeparty_user->save();
$homeparty_user->delete();
\Session()->flash('alert-success', __('msg.homeparty_guest_delete'));
return redirect(route('user_homeparty_guests', [$homeparty->id]));
}
if ($do === 'hp') {
foreach ($homeparty->homeparty_users as $homeparty_user) {
if ($homeparty->id !== $homeparty_user->homeparty_id) {
abort(404);
}
if ($homeparty_user->homeparty_user_order_items) {
foreach ($homeparty_user->homeparty_user_order_items as $homeparty_user_order_item) {
$homeparty_user_order_item->delete();
}
}
$homeparty_user->delete();
}
if ($homeparty->homeparty_order_items) {
foreach ($homeparty->homeparty_order_items as $homeparty_order_item) {
$homeparty_order_item->delete();
}
}
$homeparty->delete();
\Session()->flash('alert-success', __('msg.homeparty_delete'));
return redirect(route('user_homepartys'));
}
abort(404);
}
private function getHomparty($id)
{
$homeparty = Homeparty::findOrFail($id);
if ($homeparty->auth_user_id !== \Auth::user()->id) {
abort(404);
}
return $homeparty;
}
public function datatable($homeparty_id)
{
$query = Product::select('products.*')->where('active', true)->whereJsonContains('show_on', '4');
$homeparty = Homeparty::findOrFail($homeparty_id);
return \DataTables::eloquent($query)
->addColumn('add_card', function (Product $product) use ($homeparty) {
if ($homeparty->getCardInfo('user_tax_free')) {
return '<button type="button" class="btn btn-sm btn-md-extra btn-secondary add-product-basket" data-product-id="' . $product->id . '">
<strong>&euro; ' . $product->getFormattedPriceWith(true, false, $homeparty->getUserCountry()) . '</strong>&nbsp; +<span class="ion ion-md-cart"></span>
</button>';
} else {
return '<button type="button" class="btn btn-sm btn-md-extra btn-secondary add-product-basket" data-product-id="' . $product->id . '">
<strong>&euro; ' . $product->getFormattedPriceWith(false, false, $homeparty->getUserCountry()) . '</strong>&nbsp; +<span class="ion ion-md-cart"></span>
</button>';
}
})
->addColumn('picture', function (Product $product) {
if (count($product->images)) {
return '<img class="img-fluid img-extra" alt="" src="' . route('product_image', [$product->images->first()->slug]) . '">';
}
return "";
})
/*->addColumn('price_net', function (Product $product) use ($homeparty) {
return '<span class="no-line-break">'.$product->getFormattedPriceWith(true, true, $homeparty->getUserCountry()). " €</span>".
'<span class="no-line-break">'.$product->getFormattedPriceCurrencyWith(true, true, $homeparty->getUserCountry()).'</span>';
})
*/
->addColumn('points', function (Product $product) use ($homeparty) {
return '<span class="no-line-break">' . $product->getFormattedPoints() . '</span>';
})
->addColumn('price_gross', function (Product $product) use ($homeparty) {
if ($homeparty->getCardInfo('user_tax_free')) {
return '<span class="no-line-break">' . $product->getFormattedPriceWith(true, true, $homeparty->getUserCountry()) . " €</span>" .
'<span class="no-line-break">' . $product->getFormattedPriceCurrencyWith(true, true, $homeparty->getUserCountry()) . '</span>';
} else {
return '<span class="no-line-break">' . $product->getFormattedPriceWith(false, true, $homeparty->getUserCountry()) . " €</span>" .
'<span class="no-line-break">' . $product->getFormattedPriceCurrencyWith(false, true, $homeparty->getUserCountry()) . '</span>';
}
})
->addColumn('price_vk_gross', function (Product $product) use ($homeparty) {
if ($homeparty->getCardInfo('user_tax_free')) {
return '<span class="no-line-break">' . $product->getFormattedPriceWith(true, false, $homeparty->getUserCountry()) . " €</span>" .
'<span class="no-line-break">' . $product->getFormattedPriceCurrencyWith(true, false, $homeparty->getUserCountry()) . '</span>';
} else {
return '<span class="no-line-break">' . $product->getFormattedPriceWith(false, false, $homeparty->getUserCountry()) . " €</span>" .
'<span class="no-line-break">' . $product->getFormattedPriceCurrencyWith(false, false, $homeparty->getUserCountry()) . '</span>';
}
})
->addColumn('action', function (Product $product) {
return '<button class="btn btn-default btn-sm icon-btn md-btn-flat product-tooltip" title="details" data-modal="modal-lg"
data-toggle="modal" data-target="#modals-load-content" data-id="' . $product->id . '" data-route="' . route('modal_load') . '"
data-action="user-order-show-product" data-view="customer"><i class="ion ion-md-eye"></i></button>';
})
->filterColumn('product', function ($query, $keyword) {
if ($keyword != "") {
$query->where('name', 'LIKE', '%' . $keyword . '%');
}
})
->orderColumn('name', 'name $1')
->orderColumn('product', 'name $1')
->orderColumn('number', 'number $1')
->orderColumn('points', 'points $1')
->orderColumn('price_net', 'price_net $1')
->orderColumn('price_gross', 'price_gross $1')
->orderColumn('price_vk_gross', 'price $1')
->orderColumn('contents_total', 'contents_total $1')
->orderColumn('weight', 'weight $1')
->rawColumns(['add_card', 'points', 'product', 'quantity', 'picture', 'price_net', 'price_gross', 'price_vk_gross', 'action'])
->make(true);
}
}