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);
}