Custom Price / Land / User Order Homeparty

This commit is contained in:
Kevin Adametz 2021-08-20 18:22:21 +02:00
parent d46824a4ac
commit 51d81d8ec6
55 changed files with 1951 additions and 681 deletions

View file

@ -2,25 +2,22 @@
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use App\Models\Homeparty;
use App\Models\HomepartyUser;
use App\Models\HomepartyUserOrderItem;
use App\Models\Product;
use App\Models\ShippingCountry;
use App\Models\ShoppingInstance;
use App\Models\ShoppingOrder;
use App\Models\ShoppingUser;
use App\Models\UserHistory;
use App\Models\UserShop;
use App\Services\HomepartyCart;
use App\Services\Payment;
use App\Services\Util;
use App\User;
use Auth;
use Request;
use Validator;
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
@ -34,18 +31,28 @@ class HomepartyController extends Controller
public function index()
{
$data = [
'homepartys' => Homeparty::where('auth_user_id', '=', \Auth::user()->id)->get(),
'homepartys' => Homeparty::where('auth_user_id', '=', \Auth::user()->id)->orderByDesc('id')->get(),
];
return view('user.homeparty.index', $data);
}
public function detail($id)
public function detail($id, $step = false)
{
if($id === 'new'){
$homeparty = new Homeparty();
$homeparty->id = 0;
$step = 1;
$homeparty->description = "Willkommen zu unserer Auszeitparty rund um das Thema Bio Aloe Vera und Naturkosmetik. Wir informieren Dich darüber, was Premiumqualität bei Naturkosmetik wirklich ausmacht, zeigen Dir viele Anwendungsbeispiele bei Haut- und Darmproblemen und natürlich kannst Du unsere Produkte ausgiebig testen. Wir freuen uns auf Dich!";
}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;
@ -61,22 +68,43 @@ class HomepartyController extends Controller
$data = [
'homeparty' => $homeparty,
'homeparty_user' => $homeparty_user,
'step' => $step,
];
return view('user.homeparty.detail', $data);
}
public function store($id = null)
public function store($id = null, $step = false)
{
$data = Request::all();
if($data['action'] === 'homeparty-party-store'){
if($data['action'] === 'homeparty-party-store-detail'){
$rules = array(
'date' => 'required',
'name' => 'required',
'place' => '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-user-store'){
if($data['action'] === 'homeparty-party-store-host'){
$rules = array(
'billing_salutation' => 'required',
'billing_firstname' => 'required',
@ -86,25 +114,13 @@ class HomepartyController extends Controller
'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());
}
if($data['action'] === 'homeparty-party-store'){
if($data['action'] === 'homeparty-party-store-detail'){
if(!$id){
//first save create and empty user/host
do {
@ -112,29 +128,51 @@ class HomepartyController extends Controller
} 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);
$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();
$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-user-store'){
if($data['action'] === 'homeparty-party-store-host'){
$homeparty = $this->getHomparty($id);
$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 = $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));
return redirect(route('user_homeparty_detail', [$homeparty->id, $step]));
}
@ -154,6 +192,8 @@ class HomepartyController extends Controller
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){
@ -222,9 +262,20 @@ class HomepartyController extends Controller
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', 'Das Rechnungsland wurde geändert und der Warenkrob zurückgesetzt.');
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,
@ -232,10 +283,34 @@ class HomepartyController extends Controller
];
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()) {
@ -257,20 +332,37 @@ class HomepartyController extends Controller
$HomepartyUserOrderItem->qty = $HomepartyUserOrderItem->qty+1;
$HomepartyUserOrderItem->save();
}else{
$HomepartyUserOrderItem = HomepartyUserOrderItem::create([
'homeparty_id' => $homeparty->id,
'homeparty_user_id' => $homeparty_user->id,
'product_id' => $product->id,
'qty' => 1,
'price' => $product->price,
'price_net' => $product->getPriceWith(true, false),
'tax_rate' => $product->tax,
'points' => $product->points,
'margin' => $margin,
'ek-price' => $product->getPriceWith(false, true),
'ek-price_net' => $product->getPriceWith(true, true),
'slug' => $product->slug
]);
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
]);
}
}
}
@ -280,7 +372,7 @@ class HomepartyController extends Controller
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_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]);
}
@ -458,18 +550,24 @@ class HomepartyController extends Controller
return $homeparty;
}
public function datatable(){
$query = Product::select('products.*')->where('active', true)->where(function ($q) {
$q->where('show_at', '=', 0)
->orWhere('show_at', '=', 1);
});
public function datatable($homeparty_id){
$query = Product::select('products.*')->where('active', true)->whereJsonContains('show_on', '3');
$homeparty = Homeparty::findOrFail($homeparty_id);
return \DataTables::eloquent($query)
->addColumn('add_card', function (Product $product) {
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).'</strong>&nbsp; +<span class="ion ion-md-cart"></span>
</button>';
->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)){
@ -477,14 +575,28 @@ class HomepartyController extends Controller
}
return "";
})
->addColumn('price_net', function (Product $product) {
return $product->getFormattedPriceWith(true, true). "";
/*->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('price_gross', function (Product $product) {
return $product->getFormattedPriceWith(false, true). "";
*/
->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) {
return $product->getFormattedPriceWith(false, false). "";
->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"
@ -506,7 +618,7 @@ class HomepartyController extends Controller
->orderColumn('contents_total', 'contents_total $1')
->orderColumn('weight', 'weight $1')
->rawColumns(['add_card', 'product', 'quantity', 'picture', 'action'])
->rawColumns(['add_card', 'product', 'quantity', 'picture', 'price_net', 'price_gross', 'price_vk_gross', 'action'])
->make(true);
}

View file

@ -11,6 +11,7 @@ use App\Models\ShoppingUser;
use App\Models\UserHistory;
use App\Models\UserShop;
use App\Services\Payment;
use App\Services\UserService;
use App\Services\Util;
use App\User;
use Auth;
@ -102,7 +103,6 @@ class OrderController extends Controller
public function delivery($for, $id=null)
{
$user = User::find(\Auth::user()->id);
$shopping_user = null;
$delivery_id = null;
if($for === 'ot'){
@ -134,10 +134,10 @@ class OrderController extends Controller
public function list($for, $id=null)
{
$user = User::find(\Auth::user()->id);
$shopping_user = null;
$delivery_id = null;
if($for === 'ot'){
$shopping_user = $this->checkShoppingUser($id, $user);
$delivery_id = $shopping_user->id;
@ -147,8 +147,10 @@ class OrderController extends Controller
\Session()->flash('custom-error', __('validation.custom.shipping_not_found'));
return redirect(route('user_order_my_delivery', [$for, $delivery_id]));
}
UserService::checkUserTaxShippingCountry($user, $shipping_country_id);
Yard::instance('shopping')->setShippingCountryWithPrice($shipping_country_id, $for);
Yard::instance('shopping')->setUserPriceInfos(UserService::getYardInfo());
$data = [
'shopping_user' => $shopping_user,
@ -202,6 +204,7 @@ class OrderController extends Controller
$data['is_from'] = 'user_order';
$data['is_for'] = $for;
$data['shopping_user_id'] = $id;
$data['user_price_infos'] = Yard::instance('shopping')->getUserPriceInfos();
unset($data['quantity']);
unset($data['_token']);
@ -232,7 +235,7 @@ class OrderController extends Controller
$country_id = null;
if($for === 'me'){
$user = User::find(\Auth::user()->id);
if($user->same_as_billing){
if($user->account->same_as_billing){
$country_id = $user->account->country_id;
}else{
$country_id = $user->account->shipping_country_id;
@ -246,7 +249,6 @@ class OrderController extends Controller
$country_id = $shopping_user->shipping_country_id;
}
}
if($country_id){
if($shipping_country = ShippingCountry::whereCountryId($country_id)->first()){
return $shipping_country->id;
@ -272,50 +274,28 @@ class OrderController extends Controller
public function datatable(){
$show_at = 1;
// $user = User::find(\Auth::user()->id);
;
if(Request::get('shipping_is_for') === 'me'){
$query = Product::select('products.*')->where('active', true)->where(function ($q) {
$q->where('show_at', '=', 1)
->orWhere('show_at', '=', 2)
->orWhere('show_at', '=', 6);
});
$query = Product::select('products.*')->where('active', true)->whereJsonContains('show_on', '2');
}else{
$query = Product::select('products.*')->where('active', true)->where(function ($q) {
$q->where('show_at', '=', 0)
->orWhere('show_at', '=', 1);
});
$query = Product::select('products.*')->where('active', true)->whereJsonContains('show_on', '1');
}
//Kunden und Berater
//->orWhere('show_at', '=', 2); // Nur Berater
//->orderBy('pos', 'DESC')
//->orderBy('id', 'DESC');
return \DataTables::eloquent($query)
->addColumn('product', function (Product $product) {
$cartItem = Yard::instance('shopping')->getCartItemByProduct($product->id);
$qty = isset($cartItem->qty) ? $cartItem->qty : 0;
$rowId = isset($cartItem->rowId) ? $cartItem->rowId : '';
return '<strong>'.$product->name.'</strong><br><div class="no-line-break input-group-min-w">
<div class="input-group d-inline-flex w-auto">
<span class="input-group-prepend">
<button type="button" class="btn btn-secondary icon-btn md-btn-extra remove-product-basket" data-row-id="'.$rowId.'" data-product-id="'.$product->id.'">-</button>
</span>
<input type="text" class="form-control text-center input-extra table-input-event-onchange" name="product_qty_'.$product->id.'" data-row-id="'.$rowId.'" data-product-id="'.$product->id.'" value="'.$qty.'">
<span class="input-group-append">
<button type="button" class="btn btn-secondary icon-btn md-btn-extra add-product-basket" data-row-id="'.$rowId.'" data-product-id="'.$product->id.'">+</button>
</span>
</div>
return '<strong>'.$product->name.'</strong><br>
<div class="no-line-break input-group-min-w">
<div class="input-group d-inline-flex w-auto">
<span class="input-group-prepend">
<button type="button" class="btn btn-secondary icon-btn md-btn-extra remove-product-basket" data-row-id="'.$rowId.'" data-product-id="'.$product->id.'">-</button>
</span>
<input type="text" class="form-control text-center input-extra table-input-event-onchange" name="product_qty_'.$product->id.'" data-row-id="'.$rowId.'" data-product-id="'.$product->id.'" value="'.$qty.'">
<span class="input-group-append">
<button type="button" class="btn btn-secondary icon-btn md-btn-extra add-product-basket" data-row-id="'.$rowId.'" data-product-id="'.$product->id.'">+</button>
</span>
</div>
</div>';
})
/*
@ -349,13 +329,13 @@ class OrderController extends Controller
return "";
})
->addColumn('price_net', function (Product $product) {
return $product->getFormattedPriceWith(true, true). "";
return '<span class="no-line-break">'.$product->getFormattedPriceWith(true, true, Yard::instance('shopping')->getUserCountry()). " €</span>".'<span class="no-line-break">'.$product->getFormattedPriceCurrencyWith(true, true, Yard::instance('shopping')->getUserCountry()).'</span>';
})
->addColumn('price_gross', function (Product $product) {
return $product->getFormattedPriceWith(false, true). "";
return '<span class="no-line-break">'.$product->getFormattedPriceWith(false, true, Yard::instance('shopping')->getUserCountry()). " €</span>".'<span class="no-line-break">'.$product->getFormattedPriceCurrencyWith(false, true, Yard::instance('shopping')->getUserCountry()).'</span>';
})
->addColumn('price_vk_gross', function (Product $product) {
return $product->getFormattedPriceWith(false, false). "";
return '<span class="no-line-break">'.$product->getFormattedPriceWith(false, false, Yard::instance('shopping')->getUserCountry()). " €</span>".'<span class="no-line-break">'.$product->getFormattedPriceCurrencyWith(false, false, Yard::instance('shopping')->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"
@ -377,7 +357,7 @@ class OrderController extends Controller
->orderColumn('contents_total', 'contents_total $1')
->orderColumn('weight', 'weight $1')
->rawColumns(['add_card', 'product', 'quantity', 'picture', 'action'])
->rawColumns(['add_card', 'price_net', 'price_gross', 'price_vk_gross', 'product', 'quantity', 'picture', 'action'])
->make(true);
}
@ -398,8 +378,8 @@ class OrderController extends Controller
}
//get the card item
$cartItem = Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, $product->getPriceWith(false, true), ['image' => $image, 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points]);
Yard::setTax($cartItem->rowId, $product->tax);
$cartItem = Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, $product->getPriceWith(false, true, Yard::instance('shopping')->getUserCountry()), ['image' => $image, 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points]);
Yard::setTax($cartItem->rowId, $product->getTaxWith(Yard::instance('shopping')->getUserCountry()));
if(isset($data['qty']) && $data['qty'] > 0){
Yard::instance('shopping')->update($cartItem->rowId, $data['qty']);
@ -485,7 +465,7 @@ class OrderController extends Controller
private function getCompProducts($for){
if($for === 'me'){
return Product::whereActive(true)->where('show_at', '=', 1)->where('shipping_addon', true)->orderBy('pos', 'DESC')->get();
return Product::whereActive(true)->whereJsonContains('show_on', ['1', '2', '3'])->where('shipping_addon', true)->orderBy('pos', 'DESC')->get();
}
return null;
}