Promotion Frontend dynamic

This commit is contained in:
Kevin Adametz 2021-11-26 18:23:10 +01:00
parent c9e1545693
commit 1cc8e025a1
29 changed files with 551 additions and 163 deletions

View file

@ -91,7 +91,6 @@ class PromotionController extends Controller
public function load(){
$data = Request::all();
if(Request::ajax()) {
if(isset($data['action']) && $data['action'] === 'validate_url'){
$rules = array(

View file

@ -9,6 +9,7 @@ use App\Services\Util;
use App\Models\Product;
use App\Models\PaymentMethod;
use App\Models\PromotionUser;
use App\Services\PromotionCart;
use App\Http\Controllers\Controller;
@ -31,16 +32,16 @@ class PromotionController extends Controller
if(!$PromotionUser){
abort(402);
}
if($PromotionUser->checkOutOfStock()){
if(!$PromotionUser->promotion_admin->isActive() || $PromotionUser->checkOutOfStock()){
$data = [
'promotion_user' => $PromotionUser,
];
return view('web.promotion.outofstock', $data);
}
PromotionCart::initYard();
$data = [
'promotion_user' => $PromotionUser,
'shop_products' => Product::where('active', true)->whereJsonContains('show_on', ['1', '2', '3'])->orderBy('pos', 'ASC')->get(),
'shop_products' => Product::where('active', true)->whereJsonContains('show_on', ['3'])->orderBy('pos', 'ASC')->get(),
'user_payment_methods' => PaymentMethod::getDefaultAsArray()->toArray(),
];
return view('web.promotion.index', $data);
@ -93,6 +94,32 @@ class PromotionController extends Controller
$product = Product::find($data['id']); //current user form order
$ret = view("web.promotion.show_product", compact('product', 'data'))->render();
}
if($data['action'] === 'switch-free-product'){
\App\Services\PromotionCart::updateFeeProduct($data);
$ret = view("web.promotion._promotion_cart", compact('data'))->render();
}
if($data['action'] === 'add-shop-product'){
$data['qty'] = \App\Services\PromotionCart::updateProduct($data, true);
$ret = view("web.promotion._promotion_cart", compact('data'))->render();
}
if($data['action'] === 'update-shop-product'){
$data['qty'] = \App\Services\PromotionCart::updateProduct($data);
$ret = view("web.promotion._promotion_cart", compact('data'))->render();
}
if($data['action'] === 'remove-shop-product'){
\App\Services\PromotionCart::updateProduct($data);
$data['qty'] = 0;
$ret = view("web.promotion._promotion_cart", compact('data'))->render();
}
if($data['action'] === 'clear-cart'){
\App\Services\PromotionCart::clearCart($data);
$ret = view("web.promotion._promotion_cart", compact('data'))->render();
}
if($data['action'] === 'switch-shipping'){
$ret = view("web.promotion._promotion_cart", compact('data'))->render();
}
return response()->json(['response' => $data, 'html'=>$ret, 'status'=>$status]);
}
}

View file

@ -296,7 +296,13 @@ class Product extends Model
return $this->hasMany(ProductIngredient::class, 'product_ingredients', 'id');
}
public function getShortCopy($clean = false, $len = false){
$ret = $this->short_copy ? $this->short_copy : $this->description;
if($len && $clean){
return substr_ellipsis($ret, $len, $clean);
}
return $ret;
}
public function getActionName($id = 0){
if(isset($this->actions[$id])){
return $this->actions[$id];

View file

@ -125,6 +125,19 @@ class PromotionAdmin extends Model
}
}
public function isActive(){
if($this->active){
if($this->from && Carbon::parse($this->from)->gt(Carbon::now()->startOfDay())){
return false;
}
if($this->to && Carbon::parse($this->to)->lt(Carbon::now()->startOfDay())){
return false;
}
return true;
}
return false;
}
public function getFromAttribute($value)
{
if(!$value){ return ""; }

View file

@ -78,4 +78,8 @@ class Shipping extends Model
public function shipping_prices(){
return $this->hasMany('App\Models\ShippingPrice', 'shipping_id', 'id');
}
public function getShippingPricesBy($shipping_for){
return $this->hasMany('App\Models\ShippingPrice', 'shipping_id', 'id')->where('shipping_for', $shipping_for);
}
}

View file

@ -29,8 +29,7 @@ use Illuminate\Database\Eloquent\Model;
class ShippingCountry extends Model
{
protected $table = 'shipping_countries';
protected $fillable = [
'shipping_id', 'country_id'
];

View file

@ -47,7 +47,12 @@ class ShippingPrice extends Model
protected $table = 'shipping_prices';
protected $fillable = [
'shipping_id', 'price', 'price_comp', 'num_comp', 'tax_rate', 'factor', 'total_from', 'total_to', 'weight_from', 'weight_to',
'shipping_id', 'price', 'price_comp', 'num_comp', 'tax_rate', 'factor', 'total_from', 'total_to', 'weight_from', 'weight_to', 'shipping_for',
];
public static $shippingForTypes = [
1 => 'Berater Bestellungen',
2 => 'Shop Bestellungen',
];
public function shipping()
@ -55,6 +60,9 @@ class ShippingPrice extends Model
return $this->belongsTo('App\Models\Shipping', 'shipping_id');
}
public function getShippingForType(){
return isset(self::$shippingForTypes[$this->shipping_for]) ? self::$shippingForTypes[$this->shipping_for] : "";
}
public function setPriceAttribute($value)
{

View file

@ -0,0 +1,105 @@
<?php
namespace App\Services;
use Yard;
use App\Models\Product;
use App\Models\ShippingCountry;
class PromotionCart
{
public static $points = 0;
public static $price = 0;
public static $price_net = 0;
public static $ek_price = 0;
public static $income_price = 0;
public static function initYard(){
$id = 1;
if($ShippingCountry = ShippingCountry::all()->first()){
$id = $ShippingCountry->id;
}
Yard::instance('shopping')->setShippingCountryWithPrice($id, 'shop');
}
public static function getLowestShippingPrice($shipping_for = 2)
{
if($ShippingCountry = ShippingCountry::all()->first()){
if($ShippingCountry->shipping){
if($ShippingPrices = $ShippingCountry->shipping->getShippingPricesBy($shipping_for)->orderBy('price', 'ASC')->first()){
return formatNumber($ShippingPrices->price);
}
}
}
}
public static function clearCart($data, $add=false)
{
Yard::instance('shopping')->destroy();
}
public static function updateProduct($data, $add=false)
{
if($product = Product::find($data['product_id'])){
$image = "";
if($product->images->count()){
$image = $product->images->first()->slug;
}
//get the card item
$cartItem = Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, $product->price, $product->tax,
[
'image' => $image,
'slug' => $product->slug,
'weight' => $product->weight,
]);
Yard::setTax($cartItem->rowId, $product->tax);
if(!$add){
if(isset($data['qty']) && $data['qty'] > 0){
Yard::instance('shopping')->update($cartItem->rowId, $data['qty']);
}else{
//if 0 get the item by qty:1 and remove it
Yard::instance('shopping')->remove($cartItem->rowId);
}
}
Yard::instance('shopping')->reCalculate();
return $cartItem->qty;
}
}
public static function updateFeeProduct($data)
{
foreach (Yard::instance('shopping')->content() as $row) {
//wenn kleiner wurde ein produkt entfernt aufgrund der Anzahl
//wenn gleich löschen, da neue Versandkosten
if($row->options->free_product) {
Yard::instance('shopping')->remove($row->rowId);
}
}
if(isset($data['free_poduct_id'])) {
if ($product = Product::find($data['free_poduct_id'])) {
$image = "";
if ($product->images->count()) {
$image = $product->images->first()->slug;
}
$cartItem = Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, 0, 0,
[
'image' => $image,
'slug' => $product->slug,
'weight' => 0,
'free_product' => 1,
'product_id' => $product->id
]);
Yard::setTax($cartItem->rowId, 0);
}
}
}
}

View file

@ -323,36 +323,46 @@ class Yard extends Cart
return;
}
$shipping = $shippingCountry->shipping;
$shipping_price_for = 1;
if($this->shipping_is_for === 'me' || $this->shipping_is_for === 'ot'){
$shipping_price_for = 1;
}
if($this->shipping_is_for === 'shop'){
$shipping_price_for = 2;
}
if($this->weight() == 0){
$shipping_price = $shipping->shipping_prices->first();
$shipping_price = $shipping->getShippingPricesBy($shipping_price_for)->first();
if(!$shipping_price){
$shipping_price = new \App\Models\ShippingPrice();
}
$shipping_price->price = 0;
$shipping_price->price_comp = 0;
}else{
//first by price
$shipping_price = $this->shippingPriceBySubTotal($shipping->shipping_prices, $this->subtotal(2, '.', ''));
$shipping_price = $this->shippingPriceBySubTotal($shipping->getShippingPricesBy($shipping_price_for), $this->subtotal(2, '.', ''));
//sec by weight
if(!$shipping_price){
$shipping_price = $this->shippingPriceByWeight($shipping->shipping_prices, $this->weight());
$shipping_price = $this->shippingPriceByWeight($shipping->getShippingPricesBy($shipping_price_for), $this->weight());
}
//default
if(!$shipping_price){
$shipping_price = $shipping->shipping_prices->first();
$shipping_price = $shipping->getShippingPricesBy($shipping_price_for)->first();
}
}
if($shipping_price){
$price = $shipping_price->price;
$this->num_comp = 0;
if($this->shipping_is_for === 'me' && \App\Models\Setting::getContentBySlug('order_partner_is_comp_me')){
$price = $shipping_price->price_comp;
$this->num_comp = $shipping_price->num_comp;
if($this->weight() > 0){
if($this->shipping_is_for === 'me' && \App\Models\Setting::getContentBySlug('order_partner_is_comp_me')){
$price = $shipping_price->price_comp;
$this->num_comp = $shipping_price->num_comp;
}
if($this->shipping_is_for === 'ot' && \App\Models\Setting::getContentBySlug('order_partner_is_comp_ot')){
$price = $shipping_price->price_comp;
$this->num_comp = $shipping_price->num_comp;
}
}
if($this->shipping_is_for === 'ot' && \App\Models\Setting::getContentBySlug('order_partner_is_comp_ot')){
$price = $shipping_price->price_comp;
$this->num_comp = $shipping_price->num_comp;
}
$this->shipping_price = $price;
$this->shipping_tax_rate = $shipping_price->tax_rate;
$this->shipping_price_net = round($price / ((100+$shipping_price->tax_rate) / 100), 2);
@ -584,7 +594,7 @@ class Yard extends Cart
}
public function getCartItemByProduct($product_id, $set_price='with'){
public function getCartItemByProduct($product_id, $set_price='with', $commission=true){
if($product = Product::find($product_id)) {
$image = "";
if ($product->images->count()) {
@ -594,8 +604,8 @@ class Yard extends Cart
if($set_price === 'with'){
$price = $product->getPriceWith(false, true);
}
$cartItem = $this->getCartItem($product->id, $product->getLang('name'), 1, $price,
[
if($commission){
$options = [
'image' => $image,
'slug' => $product->slug,
'weight' => $product->weight,
@ -603,8 +613,16 @@ class Yard extends Cart
'amount_commission' => $product->amount_commission,
'value_commission' => $product->value_commission,
'partner_commission' => $product->partner_commission,
]
);
];
}else{
$options = [
'image' => $image,
'slug' => $product->slug,
'weight' => $product->weight,
];
}
$cartItem = $this->getCartItem($product->id, $product->getLang('name'), 1, $price, $options);
$content = $this->getContent();
if ($content->has($cartItem->rowId)){
@ -635,11 +653,19 @@ class Yard extends Cart
}
public function rowPrice(CartItem $row, $decimals = null, $decimalPoint = null, $thousandSeperator = null){
return $this->numberFormat($row->price, $decimals, $decimalPoint, $thousandSeperator);
}
public function rowPriceNet(CartItem $row, $decimals = null, $decimalPoint = null, $thousandSeperator = null){
$price = round($row->price / ((100 + $row->taxRate) /100), 4);
return $this->numberFormat($price, $decimals, $decimalPoint, $thousandSeperator);
}
public function rowSubtotal(CartItem $row, $decimals = null, $decimalPoint = null, $thousandSeperator = null){
return $this->numberFormat(($row->price * $row->qty), $decimals, $decimalPoint, $thousandSeperator);
}
public function rowSubtotalNet(CartItem $row, $decimals = null, $decimalPoint = null, $thousandSeperator = null){
$price = round($row->price / ((100 + $row->taxRate) /100), 4);
return $this->numberFormat(($price * $row->qty), $decimals, $decimalPoint, $thousandSeperator);
@ -658,6 +684,15 @@ class Yard extends Cart
return false;
}
public function getFreeProductId(){
foreach ($this->content() as $row) {
if($row->options->free_product) {
return $row->options->product_id;
}
}
return false;
}
public function getContentByOrder(){
$ret = [];
$comp = [];