Homparty dev

This commit is contained in:
Kevin Adametz 2020-12-16 20:03:51 +01:00
parent 9252094a04
commit ac0d5b781e
60 changed files with 3443 additions and 293 deletions

View file

@ -0,0 +1,78 @@
<?php
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Ingredient;
use App\Models\IqImage;
use App\Models\ProductCategory;
use App\Models\ProductIngredient;
use\Request;
class IngredientController extends Controller
{
public function __construct()
{
$this->middleware('admin');
}
public function index()
{
$data = [
'values' => Ingredient::all(),
];
return view('admin.ingredient.index', $data);
}
public function edit($id)
{
if($id === "new"){
$model = new Ingredient();
$model->active = true;
}else{
$model = Ingredient::findOrFail($id);
}
$data = [
'model' => $model,
//'trans' => array_keys(config('localization.supportedLocales')),
];
return view('admin.ingredient.edit', $data);
}
public function store()
{
$data = Request::all();
$data['active'] = isset($data['active']) ? true : false;
if($data['id'] === "new"){
$model = Ingredient::create($data);
}else{
$model = Ingredient::find($data['id']);
$model->fill($data)->save();
}
\Session()->flash('alert-save', '1');
return redirect(route('admin_product_ingredients'));
}
public function delete($id){
if(ProductIngredient::where('ingredient_id', $id)->count()) {
\Session()->flash('alert-error', 'Eintrag wird als Produkt-Inhaltsstoff verwendet');
return redirect(route('admin_product_ingredients'));
}
$model = Ingredient::findOrFail($id);
$model->delete();
\Session()->flash('alert-success', 'Eintrag gelöscht');
return redirect(route('admin_product_ingredients'));
}
}

View file

@ -2,6 +2,8 @@
namespace App\Http\Controllers;
use App\Models\Homeparty;
use App\Models\HomepartyUser;
use App\Models\Product;
use App\Models\ShoppingOrder;
use App\Models\ShoppingUser;
@ -61,6 +63,14 @@ class ModalController extends Controller
}
if($data['action'] === 'homeparty-add-product') {
$homeparty = Homeparty::find($data['id']);
$homeparty_user = HomepartyUser::find($data['user_id']);
$data['homeparty'] = $homeparty;
$ret = view("user.homeparty.modal_show_products", compact( 'data', 'homeparty', 'homeparty_user'))->render();
}

View file

@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Models\Country;
use App\Models\Product;
use App\Models\ProductImage;
use App\Models\ProductIngredient;
use App\Repositories\ProductRepository;
use Request;
use Validator;
@ -102,11 +103,25 @@ class ProductController extends Controller
return redirect(route('admin_product_show'));
}
public function delete($id){
$model = Product::findOrFail($id);
$model->delete();
\Session()->flash('alert-success', 'Eintrag gelöscht');
return redirect(route('admin_product_show'));
public function delete($id, $do = 'product', $did = null){
if($do === 'product'){
$model = Product::findOrFail($id);
$model->delete();
\Session()->flash('alert-success', 'Eintrag gelöscht');
return redirect(route('admin_product_show'));
}
if($do === 'ingredient'){
$model = Product::findOrFail($id);
$ProductIngredient = ProductIngredient::where('ingredient_id', $did)->where('product_id', $model->id)->first();
if($ProductIngredient){
$ProductIngredient->delete();
\Session()->flash('alert-success', 'Eintrag gelöscht');
return redirect(route('admin_product_edit', [$model->id]));
}
}
}

View file

@ -10,6 +10,7 @@ use App\Mail\MailInfo;
use App\Models\PaymentMethod;
use App\Models\ShoppingUser;
use App\Models\UserShop;
use App\Repositories\ContractPDFRepository;
use App\Services\CustomerPriority;
use App\Services\Shop;
use App\User;
@ -64,6 +65,22 @@ class AdminToolsController extends Controller
if($data['action'] === 'checkForAll'){
$shopping_users = CustomerPriority::checkForAll();
}
if($data['action'] === 'checkContractPDF'){
//create PDF
$user = User::findOrFail(80);
$pdf = new ContractPDFRepository($user);
$pdf->_set('disk', 'user');
$pdf->_set('dir', '/'.$user->id.'/documents/');
$pdf->_set('user_id', $user->id);
$pdf->_set('identifier', 'contract');
$pdf->createContractPDF();
}
if(strpos($data['action'], 'checkOne_') !== false){
$id = (int) str_replace('checkOne_', '', $data['action']);
$shopping_user = ShoppingUser::findOrFail($id);

View file

@ -5,6 +5,7 @@ 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;
@ -12,6 +13,7 @@ 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;
@ -92,6 +94,7 @@ class HomepartyController extends Controller
]);
}
}
$validator = Validator::make(Request::all(), $rules);
if ($validator->fails()) {
return back()->withErrors($validator)->withInput(Request::all());
@ -100,15 +103,17 @@ class HomepartyController extends Controller
if($data['action'] === 'homeparty-party-store'){
if(!$id){
//first save create and empty user/host
$homeparty = Homeparty::create($data);
do {
$token = Util::uuidToken();
} while( HomepartyUser::where('token', $token)->count() );
} while( Homeparty::where('token', $token)->count() );
$data['token'] = $token;
$data['auth_user_id'] = \Auth::user()->id;
$homeparty = Homeparty::create($data);
$homeparty_user = HomepartyUser::create([
'homeparty_id' => $homeparty->id,
'auth_user_id' => \Auth::user()->id,
'is_host' => true,
'token' => $token,
]);
}else {
$homeparty = $this->getHomparty($id);
@ -143,15 +148,7 @@ class HomepartyController extends Controller
{
$homeparty = $this->getHomparty($id);
if($gid === 'new'){
do {
$token = Util::uuidToken();
} while( HomepartyUser::where('token', $token)->count() );
$homeparty_user = HomepartyUser::create([
'homeparty_id' => $homeparty->id,
'auth_user_id' => \Auth::user()->id,
'is_host' => false,
'token' => $token,
]);
$homeparty_user = new HomepartyUser();
}else{
$homeparty_user = HomepartyUser::findOrFail($gid);
if($homeparty->id !== $homeparty_user->homeparty_id){
@ -194,7 +191,15 @@ class HomepartyController extends Controller
}
$homeparty = $this->getHomparty($id);
$homeparty_user = HomepartyUser::findOrFail($gid);
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);
}
@ -209,20 +214,121 @@ class HomepartyController extends Controller
public function order($id = null)
{
$homeparty = $this->getHomparty($id);
abort(404);
/*$shopping_order = ShoppingOrder::findOrFail($id);
if($shopping_order->auth_user_id !== $user->id){
abort(404);
}
$shopping_order->getLastShoppingPayment();
HomepartyCart::calculateHomeparty($homeparty);
$data = [
'shopping_order' => $shopping_order,
'isAdmin' => false,
'homeparty' => $homeparty,
];
return view('user.order.detail', $data);*/
return view('user.homeparty.order', $data);
}
//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{
$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),
'slug' => $product->slug
]);
}
}
}
$homeparty_user = HomepartyUser::findOrFail($data['homeparty_user_id']);
HomepartyCart::calculateHomeparty($homeparty);
$html_user_cart = view("user.homeparty.show_products_order", ['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'] === '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_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_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]);
}
/* $data = [
'homeparty' => $homeparty,
];
return view('user.homeparty.order', $data);*/
}
public function delete($do, $id = null, $gid=null)
{
@ -233,7 +339,6 @@ class HomepartyController extends Controller
if($homeparty->id !== $homeparty_user->homeparty_id){
abort(404);
}
$homeparty_user->token = null;
$homeparty_user->save();
$homeparty_user->delete();
\Session()->flash('alert-success', "Homeparty Gast gelöscht");
@ -246,7 +351,6 @@ class HomepartyController extends Controller
if ($homeparty->id !== $homeparty_user->homeparty_id) {
abort(404);
}
$homeparty_user->token = null;
$homeparty_user->save();
$homeparty_user->delete();
}
@ -266,4 +370,56 @@ 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);
});
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('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) {
return $product->getFormattedPriceWith(true, true). "";
})
->addColumn('price_gross', function (Product $product) {
return $product->getFormattedPriceWith(false, true). "";
})
->addColumn('price_vk_gross', function (Product $product) {
return $product->getFormattedPriceWith(false, false). "";
})
->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', 'product', 'quantity', 'picture', 'action'])
->make(true);
}
}

View file

@ -82,7 +82,7 @@ class ContactController extends Controller
}
$checkout_mail = config('app.checkout_mail');
$checkout_mail = config('app.contact_mail');
if($user_shop){
Mail::to($contact['email'])->bcc([$user_shop->user->email, $checkout_mail])->send(new MailContact($contact));
}else{

View file

@ -26,19 +26,31 @@ class HomepartyController extends Controller
}
public function detail($token = null)
public function detail($token = null, $gid = null)
{
if(!$token){
abort(404);
}
$homeparty_user = HomepartyUser::where('token', $token)->where('token_active', true)->first();
if(!$homeparty_user){
$homeparty = Homeparty::where('token', $token)->where('token_active', true)->first();
if(!$homeparty){
abort(403, 'Link für die Homeparty wurde nicht gefunden, oder ist nicht mehr aktiv.');
}
$homeparty_user = null;
if($gid){
if($gid === 'new'){
$homeparty_user = new HomepartyUser();
}else{
$homeparty_user = HomepartyUser::find($gid);
if(!$homeparty_user || $homeparty_user->homeparty_id !== $homeparty->id){
abort(403, 'Link für die Homeparty wurde nicht gefunden, oder ist nicht mehr aktiv.');
}
}
}
$data = [
'homeparty' => $homeparty_user->homeparty,
'homeparty' => $homeparty,
'homeparty_user' => $homeparty_user,
];
@ -46,21 +58,33 @@ class HomepartyController extends Controller
}
public function detailStore($token = null)
public function detailStore($token = null, $gid = null)
{
if(!$token){
abort(404);
}
$homeparty_user = HomepartyUser::where('token', $token)->where('token_active', true)->first();
$homeparty = Homeparty::where('token', $token)->where('token_active', true)->first();
if(!$homeparty){
abort(403, 'Link für die Homeparty wurde nicht gefunden, oder ist nicht mehr aktiv.');
}
if($gid === null){
$homeparty_user = HomepartyUser::create([
'homeparty_id' => $homeparty->id,
'auth_user_id' => $homeparty->auth_user_id,
'is_host' => false,
]);
}else{
$homeparty_user = HomepartyUser::find($gid);
if(!$homeparty_user || $homeparty_user->homeparty_id !== $homeparty->id){
abort(403, 'Link für die Homeparty wurde nicht gefunden, oder ist nicht mehr aktiv.');
}
}
if(!$homeparty_user){
abort(403, 'Link für die Homeparty wurde nicht gefunden, oder ist nicht mehr aktiv.');
}
$rules = array(
'billing_salutation' => 'required',
'billing_firstname' => 'required',

View file

@ -51,6 +51,12 @@ use Illuminate\Database\Eloquent\Model;
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereStatus($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereUpdatedAt($value)
* @mixin \Eloquent
* @property string|null $token
* @property bool|null $token_active
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\HomepartyUser[] $homeparty_guests
* @property-read int|null $homeparty_guests_count
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereToken($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Homeparty whereTokenActive($value)
*/
class Homeparty extends Model
{
@ -62,13 +68,18 @@ class Homeparty extends Model
'status' => 'int',
'order_to' => 'bool',
'active' => 'bool',
'default' => 'bool'
'default' => 'bool',
'token_active' => 'bool'
];
protected $dates = [
'date'
];
protected $hidden = [
'token'
];
protected $fillable = [
'auth_user_id',
'date',
@ -80,7 +91,9 @@ class Homeparty extends Model
'status',
'order_to',
'active',
'default'
'default',
'token',
'token_active',
];
public function auth_user()
@ -116,5 +129,9 @@ class Homeparty extends Model
{
$this->attributes['date'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL;
}
public function getTokenLink(){
return url('homeparty/'.$this->token);
}
}

View file

@ -6,6 +6,7 @@
namespace App\Models;
use App\Services\HomepartyUserCart;
use App\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
@ -102,10 +103,15 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @method static \Illuminate\Database\Query\Builder|\App\Models\HomepartyUser withTrashed()
* @method static \Illuminate\Database\Query\Builder|\App\Models\HomepartyUser withoutTrashed()
* @mixin \Eloquent
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\HomepartyUserOrderItem[] $homeparty_user_order_items
* @property-read int|null $homeparty_user_order_items_count
*/
class HomepartyUser extends Model
{
use SoftDeletes;
protected $userCart = null;
protected $table = 'homeparty_users';
protected $casts = [
@ -117,16 +123,12 @@ class HomepartyUser extends Model
'shipping_country_id' => 'int',
'has_buyed' => 'bool',
'subscribed' => 'bool',
'token_active' => 'bool'
];
protected $dates = [
'user_deleted_at'
];
protected $hidden = [
'token'
];
protected $fillable = [
'homeparty_id',
@ -157,8 +159,6 @@ class HomepartyUser extends Model
'shipping_email',
'has_buyed',
'subscribed',
'token',
'token_active',
'notice',
'mode',
'user_deleted_at'
@ -184,9 +184,15 @@ class HomepartyUser extends Model
return $this->belongsTo('App\Models\Country','shipping_country_id');
}
public function getTokenLink(){
return url('homeparty/'.$this->token);
public function homeparty_user_order_items(){
return $this->hasMany('App\Models\HomepartyUserOrderItem','homeparty_user_id');
}
public function isAddress(){
if($this->billing_firstname !== null){
return true;
}
return false;
}
}

View file

@ -0,0 +1,174 @@
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Class HomepartyUserOrderItem
*
* @property int $id
* @property int $homeparty_id
* @property int $homeparty_user_id
* @property int $product_id
* @property int $qty
* @property float $price
* @property float $price_net
* @property float $tax_rate
* @property int $points
* @property float $margin
* @property float $ek-price
* @property string $slug
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Homeparty $homeparty
* @property HomepartyUser $homeparty_user
* @property Product $product
* @package App\Models
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem whereEkPrice($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem whereHomepartyId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem whereHomepartyUserId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem whereMargin($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem wherePoints($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem wherePrice($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem wherePriceNet($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem whereProductId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem whereQty($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem whereSlug($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem whereTaxRate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\HomepartyUserOrderItem whereUpdatedAt($value)
* @mixin \Eloquent
*/
class HomepartyUserOrderItem extends Model
{
protected $table = 'homeparty_user_order_items';
protected $casts = [
'homeparty_id' => 'int',
'homeparty_user_id' => 'int',
'product_id' => 'int',
'qty' => 'int',
'price' => 'float',
'price_net' => 'float',
'tax_rate' => 'float',
'points' => 'int',
'margin' => 'float',
'ek-price' => 'float'
];
protected $fillable = [
'homeparty_id',
'homeparty_user_id',
'product_id',
'qty',
'price',
'price_net',
'tax_rate',
'points',
'margin',
'ek-price',
'slug'
];
public function homeparty()
{
return $this->belongsTo(Homeparty::class);
}
public function homeparty_user()
{
return $this->belongsTo(HomepartyUser::class);
}
public function product()
{
return $this->belongsTo(Product::class);
}
public function getFormattedPrice()
{
return formatNumber($this->attributes['price']);
}
public function getFormattedTotalPrice()
{
return formatNumber($this->attributes['price'] * $this->attributes['qty']);
}
public function getFormattedPriceNet()
{
return formatNumber($this->attributes['price_net']);
}
public function getFormattedTotalPriceNet()
{
return formatNumber($this->attributes['price_net'] * $this->attributes['qty']);
}
public function getFormattedEKPrice()
{
return formatNumber($this->attributes['ek-price']);
}
public function getFormattedTotalEKPrice()
{
return formatNumber($this->attributes['ek-price'] * $this->attributes['qty']);
}
public function getFormattedIncomePrice()
{
return formatNumber($this->attributes['price'] - $this->attributes['ek-price']);
}
public function getFormattedTotalIncomePrice()
{
return formatNumber(($this->attributes['price'] - $this->attributes['ek-price']) * $this->attributes['qty']);
}
public function getFormattedTotalPoints()
{
return formatNumber($this->attributes['points'] * $this->attributes['qty'], 0);
}
public function getTotalPrice()
{
return (float) ($this->attributes['price'] * $this->attributes['qty']);
}
public function getTotalPoints()
{
return ($this->attributes['points'] * $this->attributes['qty']);
}
public function geTotalPriceNet()
{
return (float) ($this->attributes['price_net'] * $this->attributes['qty']);
}
public function geTotalEKPrice()
{
return (float) ($this->attributes['ek-price'] * $this->attributes['qty']);
}
public function getIncomePrice()
{
return (float) ($this->attributes['price'] - $this->attributes['ek-price']);
}
public function geTotalIncomePrice()
{
return (float) (($this->attributes['price'] - $this->attributes['ek-price']) * $this->attributes['qty']);
}
}

80
app/Models/Ingredient.php Normal file
View file

@ -0,0 +1,80 @@
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* Class Ingredient
*
* @property int $id
* @property string $name
* @property string $trans_name
* @property string $inci
* @property string $trans_inci
* @property string $effect
* @property string $trans_effect
* @property bool $active
* @property int $pos
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Collection|Product[] $products
* @package App\Models
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductIngredient[] $product_ingredients
* @property-read int|null $product_ingredients_count
* @property-read int|null $products_count
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereEffect($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereInci($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient wherePos($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereTransEffect($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereTransInci($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereTransName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Ingredient whereUpdatedAt($value)
* @mixin \Eloquent
*/
class Ingredient extends Model
{
protected $table = 'ingredients';
protected $casts = [
'active' => 'bool',
'pos' => 'int'
];
protected $fillable = [
'name',
'trans_name',
'inci',
'trans_inci',
'effect',
'trans_effect',
'active',
'pos'
];
public function products()
{
return $this->belongsToMany(Product::class, 'product_ingredients')
->withPivot('id')
->withTimestamps();
}
public function product_ingredients()
{
return $this->hasMany(ProductIngredient::class, 'product_ingredients', 'id');
}
}

View file

@ -105,6 +105,9 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereWpNumber($value)
* @property bool|null $shipping_addon
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereShippingAddon($value)
* @property-read int|null $ingredients_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductIngredient[] $product_ingredients
* @property-read int|null $product_ingredients_count
*/
class Product extends Model
{
@ -230,6 +233,20 @@ class Product extends Model
return $this->hasMany(CountryPrice::class, 'product_id');
}
public function p_ingredients()
{
return $this->belongsToMany(Ingredient::class, 'product_ingredients')
->withPivot('id')
->withTimestamps();
}
public function product_ingredients()
{
return $this->hasMany(ProductIngredient::class, 'product_ingredients', 'id');
}
public function getActionName($id = 0){
if(isset($this->actions[$id])){
return $this->actions[$id];

View file

@ -0,0 +1,56 @@
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Class ProductIngredient
*
* @property int $id
* @property int $product_id
* @property int $ingredient_id
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Ingredient $ingredient
* @property Product $product
* @package App\Models
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductIngredient newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductIngredient newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductIngredient query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductIngredient whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductIngredient whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductIngredient whereIngredientId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductIngredient whereProductId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductIngredient whereUpdatedAt($value)
* @mixin \Eloquent
*/
class ProductIngredient extends Model
{
protected $table = 'product_ingredients';
protected $casts = [
'product_id' => 'int',
'ingredient_id' => 'int'
];
protected $fillable = [
'product_id',
'ingredient_id'
];
public function ingredient()
{
return $this->belongsTo(Ingredient::class, 'ingredient_id');
}
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
}

View file

@ -34,6 +34,10 @@ class ContractPDFRepository extends BaseRepository {
$this->{$name} = $value;
}
private function convert($str){
return iconv('UTF-8', 'windows-1252', $str);
}
public function createContractPDF() {
$pdf = new ContractPDF();
@ -47,56 +51,58 @@ class ContractPDFRepository extends BaseRepository {
$y = 70;
$nl = 17.5;
$pdf->SetXY($x1, $y);
$pdf->Write(0, $this->model->account->m_account);
$pdf->Write(0, $this->convert($this->model->account->m_account));
$pdf->SetXY($x2, $y);
$pdf->Write(0, now()->format("d.m.Y"));
$y += $nl;
$pdf->SetXY($x1, $y);
$pdf->Write(0, $this->model->account->company);
$pdf->Write(0, $this->convert($this->model->account->company));
$pdf->SetXY($x2, $y);
$pre = $this->model->account->pre_phone_id != "" ? $this->model->account->pre_phone->phone." " : "";
$pdf->Write(0, $pre.$this->model->account->phone);
$pre = $this->model->account->pre_phone_id != "" ? $this->convert($this->model->account->pre_phone->phone)." " : "";
$pdf->Write(0, $pre.$this->convert($this->model->account->phone));
$y += $nl;
$pdf->SetXY($x1, $y);
$pdf->Write(0, $this->model->account->m_first_name);
$pdf->Write(0, $this->convert($this->model->account->m_first_name));
$pdf->SetXY($x2, $y);
$pre = $this->model->account->pre_mobil_id != "" ? $this->model->account->pre_mobil->phone." " : "";
$pdf->Write(0, $pre.$this->model->account->mobil);
$pre = $this->model->account->pre_mobil_id != "" ? $this->convert($this->model->account->pre_mobil->phone)." " : "";
$pdf->Write(0, $pre.$this->convert($this->model->account->mobil));
$y += $nl;
$pdf->SetXY($x1, $y);
$pdf->Write(0, $this->model->account->m_last_name);
$pdf->Write(0, $this->convert($this->model->account->m_last_name));
$pdf->SetXY($x2, $y);
$pdf->Write(0, $this->model->email);
$pdf->Write(0, $this->convert($this->model->email));
$y += $nl;
$pdf->SetXY($x1, $y);
$pdf->Write(0, $this->model->account->address);
$pdf->Write(0, $this->convert($this->model->account->address));
$pdf->SetXY($x2, $y);
$pdf->Write(0, $this->model->account->birthday);
$pdf->Write(0, $this->convert($this->model->account->birthday));
$y += $nl;
$pdf->SetXY($x1, $y);
$pdf->Write(0, $this->model->account->zipcode." ".$this->model->account->city);
$pdf->Write(0, $this->convert($this->model->account->zipcode)." ".$this->convert($this->model->account->city));
$y += $nl;
$pdf->SetXY($x1, $y);
$pre = $this->model->account->country_id ? $this->model->account->country->de." " : "";
$pre = $this->model->account->country_id ? $this->convert($this->model->account->country->de)." " : "";
$pdf->Write(0, $pre);
if($this->model->m_sponsor){
$y += 48;
$pdf->SetXY($x1, $y);
$pdf->Write(0, $this->model->user_sponsor->account->company);
$pdf->Write(0, $this->convert($this->model->user_sponsor->account->company));
$pdf->SetXY($x2, $y);
$pdf->Write(0, $this->model->user_sponsor->account->m_account);
$pdf->Write(0, $this->convert($this->model->user_sponsor->account->m_account));
$y += $nl;
$pdf->SetXY($x1, $y);
$pdf->Write(0, $this->model->user_sponsor->account->m_first_name." ".$this->model->user_sponsor->account->m_last_name);
$pdf->Write(0, $this->convert($this->model->user_sponsor->account->m_first_name)." ".$this->convert($this->model->user_sponsor->account->m_last_name));
$website = $this->model->user_sponsor->shop()->count() ? $this->model->user_sponsor->shop->getSubdomain(false) : "www.mivita.care";
@ -112,9 +118,9 @@ class ContractPDFRepository extends BaseRepository {
$pdf->SetXY($x1, 65);
$pdf->Write(0, $this->model->account->m_first_name." ".$this->model->account->m_last_name);
$pdf->Write(0, $this->convert($this->model->account->m_first_name)." ".$this->convert($this->model->account->m_last_name));
$pdf->SetXY($x2, 65);
$pdf->Write(0, $this->model->account->m_account);
$pdf->Write(0, $this->convert($this->model->account->m_account));
$pdf->AddPage('P', array(210, 297));

View file

@ -9,6 +9,7 @@ use App\Models\Product;
use App\Models\ProductAttribute;
use App\Models\ProductCategory;
use App\Models\ProductImage;
use App\Models\ProductIngredient;
class ProductRepository extends BaseRepository {
@ -40,12 +41,32 @@ class ProductRepository extends BaseRepository {
$this->updateCategories(isset($data['categories']) ? $data['categories'] : array());
$this->updateAttributes(isset($data['attributes']) ? $data['attributes'] : array());
$this->updateIngredients(isset($data['product_ingredients']) ? $data['product_ingredients'] : array());
$this->updateCountryPrices($data);
return $this->model;
}
public function updateIngredients($data = array())
{
$ProductIngredient = $this->model->p_ingredients()->pluck('ingredient_id')->toArray();
//set attr
if(is_array($data)){
foreach ($data as $id) {
//not use
if(!in_array($id, $ProductIngredient)){
ProductIngredient::create([
'product_id' => $this->model->id,
'ingredient_id' => $id,
]);
}
}
}
return true;
}
public function updateCategories($data = array())
{
foreach ($this->model->categories as $category) {

View file

@ -4,6 +4,7 @@ namespace App\Services;
use App\Models\Attribute;
use App\Models\Category;
use App\Models\Country;
use App\Models\Ingredient;
use App\Models\Product;
use App\Models\ShippingCountry;
use App\Models\ShoppingUser;
@ -146,6 +147,18 @@ class HTMLHelper
return $ret;
}
public static function getProductIngredientsOptions($has_ids = array(), $all = true){
$values = Ingredient::where('active', 1)->get();
$ret = "";
$attr = "";
foreach ($values as $value){
if(!in_array($value->id, $has_ids)){
$ret .= '<option value="'.$value->id.'" '.$attr.'>'.$value->name.'</option>\n';
}
}
return $ret;
}
public static function getAttributesOptions($ids = array(), $all = true){
$values = Attribute::where('active', 1)->get();
$ret = "";

View file

@ -0,0 +1,552 @@
<?php
namespace App\Services;
use App\Models\Homeparty;
class HomepartyCart
{
/*private $shipping_price = 0;
private $shipping_price_net = 0;
private $shipping_tax_rate = 0;
private $shipping_tax = 0;
private $shipping_country_id = 0; //default de
private $shipping_is_for;
private $num_comp;
private $ysession;
private $yinstance;
private $shopping_data = [];*/
public static $points = 0;
public static $price = 0;
public static $price_net = 0;
public static $ek_price = 0;
public static $income_price = 0;
private static $homeparty;
private static $userCarts = [];
public static $user_host_id;
public static $is_bonus = false;
public static $is_bonus_coupon = false;
private static $bonus_coupon = 0;
private static $bonus_value = 30;
private static $bonus_price = 0;
private static $bonus_diff = 0;
private static $bonus_points_diff = 0;
private static $bonus_coupon_fault = 0;
private static $bonus_coupon_next_step = 0;
private static $bonus_coupon_next_value = 0;
private static $bonus_start = 200;
private static $bonusTable = [
250 => 15,
300 => 20,
400 => 30,
500 => 35,
600 => 40,
700 => 50,
];
public static function calculateHomeparty(Homeparty $homeparty){
self::$homeparty = $homeparty;
foreach ($homeparty->homeparty_users as $homeparty_user){
if($homeparty_user->is_host){
self::$user_host_id = $homeparty_user->id;
}
self::$userCarts[$homeparty_user->id] = new HomepartyUserCart($homeparty_user);
self::addCart(self::$userCarts[$homeparty_user->id]);
}
self::caluclateBonus();
self::calculateBonusHost();
}
public static function addCart(HomepartyUserCart $homepartyUserCart){
self::$points += $homepartyUserCart->points;
self::$price += $homepartyUserCart->price;
self::$price_net += $homepartyUserCart->price_net;
self::$ek_price += $homepartyUserCart->ek_price;
self::$income_price += $homepartyUserCart->income_price;
}
public static function getUserCart($id){
return isset(self::$userCarts[$id]) ? self::$userCarts[$id] : null;
}
public static function caluclateBonus(){
if(self::$price >= 200){
self::$is_bonus = true;
//TODO get from PRODUCT
self::$bonus_price = 11.90;
self::$price += self::$bonus_price;
self::$price_net += 10;
$bonus_tmp = self::$price - self::$bonus_value;
//look up for coupon in table
foreach (self::$bonusTable as $val=>$coupon){
if($bonus_tmp >= $val){
self::$is_bonus_coupon = true;
self::$bonus_coupon = $coupon;
}else{
if(self::$bonus_coupon_fault === 0){
self::$bonus_coupon_fault = $val - $bonus_tmp;
self::$bonus_coupon_next_step = $val;
self::$bonus_coupon_next_value = $coupon;
}
}
}
$keys = array_keys(self::$bonusTable);
$step = $keys[count($keys)-1];
if($bonus_tmp > $step){
self::$bonus_coupon_next_step = $step;
self::$bonus_coupon_next_value =self::$bonusTable[$step];
}
}else{
self::$bonus_diff = 200 - self::$price;
}
}
public static function calculateBonusHost(){
if(self::$is_bonus){
$bonus_total = self::$bonus_value + self::$bonus_coupon;
$user_cart = self::getUserCart(self::$user_host_id);
//cart lower the bonus
if($user_cart->price <= $bonus_total){
self::$bonus_points_diff = $user_cart->points;
$user_cart->points = 0;
$user_cart->price = 0;
return;
}
$bonus_percent = (100/$user_cart->price*$bonus_total);
self::$bonus_points_diff = round($user_cart->points/100*$bonus_percent);
$user_cart->points -= self::$bonus_points_diff;
$user_cart->price -= $bonus_total;
}
}
public static function getUserCartHost(){
return self::getUserCart(self::$user_host_id);
}
public static function getFormattedPoints()
{
return formatNumber(self::$points, 0);
}
public static function getFormattedPrice()
{
return formatNumber(self::$price);
}
public static function getFormattedPriceNet()
{
return formatNumber(self::$price_net);
}
public static function getFormattedEkPrice()
{
return formatNumber(self::$ek_price);
}
public static function getFormattedIncomePrice()
{
return formatNumber(self::$income_price);
}
public static function getFormattedPriceTax(){
return formatNumber(self::$price - self::$price_net);
}
public static function getFormattedBonusPrice()
{
return formatNumber(self::$bonus_price);
}
public static function getFormattedBonusStart(){
return formatNumber(self::$bonus_start);
}
public static function getFormattedBonusValue(){
return formatNumber(self::$bonus_value);
}
public static function getFormattedBonusDiff(){
return formatNumber(self::$bonus_diff);
}
public static function getFormattedBonusCoupon(){
return formatNumber(self::$bonus_coupon);
}
public static function getFormattedBonusCouponFault(){
return formatNumber(self::$bonus_coupon_fault);
}
public static function getFormattedBonusCouponNextStep(){
return formatNumber(self::$bonus_coupon_next_step);
}
public static function getFormattedBonusCouponNextValue(){
return formatNumber(self::$bonus_coupon_next_value);
}
public static function getFormattedBonusTotal(){
if(self::$is_bonus){
return formatNumber(self::$bonus_value + self::$bonus_coupon);
}
return formatNumber(0);
}
public static function getFormattedBonusPointsDiff(){
return formatNumber( self::$bonus_points_diff, 0 );
}
/* public static function getTaxRate()
{
return config('cart.tax');
}
public function putYardExtra($key, $value){
$content = $this->getYContent();
$content->put($key, $value);
$this->ysession->put($this->yinstance, $content);
}
public function getYardExtra($key){
$content = $this->getYContent();
if ($content->has($key)){
return $content->get($key);
}
return false;
}
public function getShippingCountryName(){
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
if($shippingCountry && $shippingCountry->country){
return $shippingCountry->country->getLocated();
}
return "";
}
public function getShippingCountryCountryId()
{
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
if($shippingCountry && $shippingCountry->country){
return $shippingCountry->country->id;
}
return 1; //default DE
}
public function getShippingCountryId()
{
return $this->shipping_country_id;
}
public function getYContent()
{
if (is_null($this->ysession->get($this->yinstance))) {
return new Collection([]);
}
return $this->ysession->get($this->yinstance);
}
public function reCalculateShippingPrice(){
$this->calculateShippingPrice();
}
public function setShippingCountryWithPrice($shipping_country_id, $shipping_is_for = 'ot')
{
$this->shipping_country_id = $shipping_country_id;
$this->putYardExtra('shipping_country_id', $shipping_country_id);
$this->shipping_is_for = $shipping_is_for;
$this->putYardExtra('shipping_is_for', $shipping_is_for);
$this->calculateShippingPrice();
}
private function calculateShippingPrice(){
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
if(!$shippingCountry){
return;
}
$shipping = $shippingCountry->shipping;
if($this->weight() == 0){
$shipping_price = $shipping->shipping_prices->first();
$shipping_price->price = 0;
$shipping_price->price_comp = 0;
}else{
//first by price
$shipping_price = $this->shippingPriceByTotal($shipping->shipping_prices, $this->total(2, '.', ''));
//sec by weight
if(!$shipping_price){
$shipping_price = $this->shippingPriceByWeight($shipping->shipping_prices, $this->weight());
}
//default
if(!$shipping_price){
$shipping_price = $shipping->shipping_prices->first();
}
}
if($shipping_price){
$price = $shipping_price->price;
$this->num_comp = 0;
if($this->shipping_is_for === 'me'){
$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);
$this->shipping_tax = round($price / (100+$shipping_price->tax_rate) * 100, 2);
$this->putYardExtra('num_comp', $this->num_comp);
$this->putYardExtra('shipping_price', $this->shipping_price);
$this->putYardExtra('shipping_tax_rate', $this->shipping_tax_rate);
$this->putYardExtra('shipping_tax', $this->shipping_tax);
$this->putYardExtra('shipping_price_net', $this->shipping_price_net);
}
}
private function shippingPriceByTotal($prices, $total){
foreach ($prices as $price){
if($price->total_from > 0 && $price->total_to > 0){
if($total >= $price->total_from && $total <= $price->total_to){
return $price;
}
}
}
return false;
}
private function shippingPriceByWeight($prices, $weight){
foreach ($prices as $price){
if($price->weight_from > 0 && $price->weight_to > 0){
if($weight >= $price->weight_from && $weight <= $price->weight_to){
return $price;
}
}
}
return false;
}
public function shipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->shipping_price, $decimals, $decimalPoint, $thousandSeperator);
}
public function shippingNet($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->shipping_price_net, $decimals, $decimalPoint, $thousandSeperator);
}
//
private function shippingTax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->shipping_tax, $decimals, $decimalPoint, $thousandSeperator);
}
//netto
public function subtotalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$subtotal = (float) $this->shipping_price_net + $this->subtotal(2, '.', '');
return $this->numberFormat($subtotal, $decimals, $decimalPoint, $thousandSeperator);
}
public function taxWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$total = $this->totalWithShipping(2, '.', '');
// $totalTax = (float) $this->tax(2, '.', '') + $this->shipping_tax;
$totalTax = $this->subtotalWithShipping(2, '.', '');
return $this->numberFormat(($total - $totalTax), $decimals, $decimalPoint, $thousandSeperator);
}
public function totalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$total = (float) ($this->total(2, '.', '')) + $this->shipping_price;
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
}
public function weight($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();
$total = $content->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->options->weight ? ($cartItem->options->weight*$cartItem->qty) : 0);
}, 0);
return $total;
}
public function points()
{
$content = $this->getContent();
$total = $content->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->options->points ? ($cartItem->options->points * $cartItem->qty) : 0);
}, 0);
return $total;
}
public function compCount()
{
$content = $this->getContent();
$count = parent::count();
$comp_count = $content->reduce(function ($comp_count, CartItem $cartItem) {
return $cartItem->options->comp ? $comp_count + 1 : $comp_count;
}, 0);
return $count-$comp_count;
}
public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();
$total = $content->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->qty * $cartItem->price);
}, 0);
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
}
public function tax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();
$tax = $content->reduce(function ($tax, CartItem $cartItem) {
$priceTax = $cartItem->price / (100 + $cartItem->taxRate) * $cartItem->taxRate;
return $tax + ($cartItem->qty * $priceTax);
}, 0);
return $this->numberFormat($tax, $decimals, $decimalPoint, $thousandSeperator);
}
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();
$subTotal = $content->reduce(function ($subTotal, CartItem $cartItem) {
$price_net = $cartItem->price / ((100 + $cartItem->taxRate) / 100);
return $subTotal + ($cartItem->qty * $price_net);
}, 0);
return $this->numberFormat($subTotal, $decimals, $decimalPoint, $thousandSeperator);
}
public function getCartItemByProduct($product_id, $set_price='with'){
if($product = Product::find($product_id)) {
$image = "";
if ($product->images->count()) {
$image = $product->images->first()->slug;
}
$price = $product->price;
if($set_price === 'with'){
$price = $product->getPriceWith(false, true);
}
$cartItem = $this->getCartItem($product->id, $product->getLang('name'), 1, $price, ['image' => $image, 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points]);
$content = $this->getContent();
if ($content->has($cartItem->rowId)){
return $content->get($cartItem->rowId);
}
return $cartItem;
}
return null;
}
public function getCartItem($id, $name = null, $qty = null, $price = null, array $options = []){
if ($id instanceof Buyable) {
$cartItem = CartItem::fromBuyable($id, $qty ?: []);
} elseif (is_array($id)) {
$cartItem = CartItem::fromArray($id);
} else {
$cartItem = CartItem::fromAttributes($id, $name, $price, $options);
}
return $cartItem;
}
public function destroy()
{
$this->ysession->remove($this->yinstance);
parent::destroy();
}
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 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);
}
public function getNumComp(){
return $this->num_comp;
}
public function getCompProductBy($comp, $product_id=false){
foreach ($this->content() as $row) {
if($row->options->comp == $comp) {
return $row->options->product_id;
}
}
return false;
}
public function getContentByOrder(){
$ret = [];
$comp = [];
foreach ($this->content() as $row) {
if($row->options->comp){
$comp[100+$row->options->comp] = $row;
}else{
$ret[] = $row;
}
}
ksort($comp);
$ret = array_merge($ret, $comp);
return $ret;
}
*/
protected function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
{
if(is_null($decimals)){
$decimals = is_null(config('cart.format.decimals')) ? 2 : config('cart.format.decimals');
}
if(is_null($decimalPoint)){
$decimalPoint = is_null(config('cart.format.decimal_point')) ? '.' : config('cart.format.decimal_point');
}
if(is_null($thousandSeperator)){
$thousandSeperator = is_null(config('cart.format.thousand_seperator')) ? ',' : config('cart.format.thousand_seperator');
}
return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
}
}

View file

@ -0,0 +1,423 @@
<?php
namespace App\Services;
use App\Models\HomepartyUser;
use App\Models\Product;
use App\Models\ShippingCountry;
use \Gloudemans\Shoppingcart\Cart;
use Gloudemans\Shoppingcart\CartItem;
use Gloudemans\Shoppingcart\Contracts\Buyable;
use Illuminate\Session\SessionManager;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Collection;
class HomepartyUserCart
{
/*private $shipping_price = 0;
private $shipping_price_net = 0;
private $shipping_tax_rate = 0;
private $shipping_tax = 0;
private $shipping_country_id = 0; //default de
private $shipping_is_for;
private $num_comp;
private $ysession;
private $yinstance;
private $shopping_data = [];*/
public $points;
public $price;
public $price_net;
public $ek_price;
public $income_price;
private $homepartyUser;
public function __construct(HomepartyUser $homepartyUser)
{
$this->homepartyUser = $homepartyUser;
$this->points = 0;
$this->price = 0;
$this->price_net = 0;
$this->ek_price = 0;
$this->income_price = 0;
$this->calculateUserCart();
}
public function calculateUserCart(){
foreach ($this->homepartyUser->homeparty_user_order_items as $order_item) {
$this->points += $order_item->getTotalPoints();
$this->price += $order_item->getTotalPrice();
$this->price_net += $order_item->geTotalPriceNet();
$this->ek_price += $order_item->geTotalEKPrice();
$this->income_price += $order_item->geTotalIncomePrice();
}
}
public function getFormattedPoints()
{
return formatNumber($this->points, 0);
}
public function getFormattedPrice()
{
return formatNumber($this->price);
}
public function getFormattedPriceNet()
{
return formatNumber($this->price_net);
}
public function getFormattedEkPrice()
{
return formatNumber($this->ek_price);
}
public function getFormattedIncomePrice()
{
return formatNumber($this->income_price);
}
/* public static function getTaxRate()
{
return config('cart.tax');
}
public function putYardExtra($key, $value){
$content = $this->getYContent();
$content->put($key, $value);
$this->ysession->put($this->yinstance, $content);
}
public function getYardExtra($key){
$content = $this->getYContent();
if ($content->has($key)){
return $content->get($key);
}
return false;
}
public function getShippingCountryName(){
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
if($shippingCountry && $shippingCountry->country){
return $shippingCountry->country->getLocated();
}
return "";
}
public function getShippingCountryCountryId()
{
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
if($shippingCountry && $shippingCountry->country){
return $shippingCountry->country->id;
}
return 1; //default DE
}
public function getShippingCountryId()
{
return $this->shipping_country_id;
}
public function getYContent()
{
if (is_null($this->ysession->get($this->yinstance))) {
return new Collection([]);
}
return $this->ysession->get($this->yinstance);
}
public function reCalculateShippingPrice(){
$this->calculateShippingPrice();
}
public function setShippingCountryWithPrice($shipping_country_id, $shipping_is_for = 'ot')
{
$this->shipping_country_id = $shipping_country_id;
$this->putYardExtra('shipping_country_id', $shipping_country_id);
$this->shipping_is_for = $shipping_is_for;
$this->putYardExtra('shipping_is_for', $shipping_is_for);
$this->calculateShippingPrice();
}
private function calculateShippingPrice(){
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
if(!$shippingCountry){
return;
}
$shipping = $shippingCountry->shipping;
if($this->weight() == 0){
$shipping_price = $shipping->shipping_prices->first();
$shipping_price->price = 0;
$shipping_price->price_comp = 0;
}else{
//first by price
$shipping_price = $this->shippingPriceByTotal($shipping->shipping_prices, $this->total(2, '.', ''));
//sec by weight
if(!$shipping_price){
$shipping_price = $this->shippingPriceByWeight($shipping->shipping_prices, $this->weight());
}
//default
if(!$shipping_price){
$shipping_price = $shipping->shipping_prices->first();
}
}
if($shipping_price){
$price = $shipping_price->price;
$this->num_comp = 0;
if($this->shipping_is_for === 'me'){
$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);
$this->shipping_tax = round($price / (100+$shipping_price->tax_rate) * 100, 2);
$this->putYardExtra('num_comp', $this->num_comp);
$this->putYardExtra('shipping_price', $this->shipping_price);
$this->putYardExtra('shipping_tax_rate', $this->shipping_tax_rate);
$this->putYardExtra('shipping_tax', $this->shipping_tax);
$this->putYardExtra('shipping_price_net', $this->shipping_price_net);
}
}
private function shippingPriceByTotal($prices, $total){
foreach ($prices as $price){
if($price->total_from > 0 && $price->total_to > 0){
if($total >= $price->total_from && $total <= $price->total_to){
return $price;
}
}
}
return false;
}
private function shippingPriceByWeight($prices, $weight){
foreach ($prices as $price){
if($price->weight_from > 0 && $price->weight_to > 0){
if($weight >= $price->weight_from && $weight <= $price->weight_to){
return $price;
}
}
}
return false;
}
public function shipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->shipping_price, $decimals, $decimalPoint, $thousandSeperator);
}
public function shippingNet($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->shipping_price_net, $decimals, $decimalPoint, $thousandSeperator);
}
//
private function shippingTax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->shipping_tax, $decimals, $decimalPoint, $thousandSeperator);
}
//netto
public function subtotalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$subtotal = (float) $this->shipping_price_net + $this->subtotal(2, '.', '');
return $this->numberFormat($subtotal, $decimals, $decimalPoint, $thousandSeperator);
}
public function taxWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$total = $this->totalWithShipping(2, '.', '');
// $totalTax = (float) $this->tax(2, '.', '') + $this->shipping_tax;
$totalTax = $this->subtotalWithShipping(2, '.', '');
return $this->numberFormat(($total - $totalTax), $decimals, $decimalPoint, $thousandSeperator);
}
public function totalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$total = (float) ($this->total(2, '.', '')) + $this->shipping_price;
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
}
public function weight($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();
$total = $content->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->options->weight ? ($cartItem->options->weight*$cartItem->qty) : 0);
}, 0);
return $total;
}
public function points()
{
$content = $this->getContent();
$total = $content->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->options->points ? ($cartItem->options->points * $cartItem->qty) : 0);
}, 0);
return $total;
}
public function compCount()
{
$content = $this->getContent();
$count = parent::count();
$comp_count = $content->reduce(function ($comp_count, CartItem $cartItem) {
return $cartItem->options->comp ? $comp_count + 1 : $comp_count;
}, 0);
return $count-$comp_count;
}
public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();
$total = $content->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->qty * $cartItem->price);
}, 0);
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
}
public function tax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();
$tax = $content->reduce(function ($tax, CartItem $cartItem) {
$priceTax = $cartItem->price / (100 + $cartItem->taxRate) * $cartItem->taxRate;
return $tax + ($cartItem->qty * $priceTax);
}, 0);
return $this->numberFormat($tax, $decimals, $decimalPoint, $thousandSeperator);
}
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();
$subTotal = $content->reduce(function ($subTotal, CartItem $cartItem) {
$price_net = $cartItem->price / ((100 + $cartItem->taxRate) / 100);
return $subTotal + ($cartItem->qty * $price_net);
}, 0);
return $this->numberFormat($subTotal, $decimals, $decimalPoint, $thousandSeperator);
}
public function getCartItemByProduct($product_id, $set_price='with'){
if($product = Product::find($product_id)) {
$image = "";
if ($product->images->count()) {
$image = $product->images->first()->slug;
}
$price = $product->price;
if($set_price === 'with'){
$price = $product->getPriceWith(false, true);
}
$cartItem = $this->getCartItem($product->id, $product->getLang('name'), 1, $price, ['image' => $image, 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points]);
$content = $this->getContent();
if ($content->has($cartItem->rowId)){
return $content->get($cartItem->rowId);
}
return $cartItem;
}
return null;
}
public function getCartItem($id, $name = null, $qty = null, $price = null, array $options = []){
if ($id instanceof Buyable) {
$cartItem = CartItem::fromBuyable($id, $qty ?: []);
} elseif (is_array($id)) {
$cartItem = CartItem::fromArray($id);
} else {
$cartItem = CartItem::fromAttributes($id, $name, $price, $options);
}
return $cartItem;
}
public function destroy()
{
$this->ysession->remove($this->yinstance);
parent::destroy();
}
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 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);
}
public function getNumComp(){
return $this->num_comp;
}
public function getCompProductBy($comp, $product_id=false){
foreach ($this->content() as $row) {
if($row->options->comp == $comp) {
return $row->options->product_id;
}
}
return false;
}
public function getContentByOrder(){
$ret = [];
$comp = [];
foreach ($this->content() as $row) {
if($row->options->comp){
$comp[100+$row->options->comp] = $row;
}else{
$ret[] = $row;
}
}
ksort($comp);
$ret = array_merge($ret, $comp);
return $ret;
}
*/
protected function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
{
if(is_null($decimals)){
$decimals = is_null(config('cart.format.decimals')) ? 2 : config('cart.format.decimals');
}
if(is_null($decimalPoint)){
$decimalPoint = is_null(config('cart.format.decimal_point')) ? '.' : config('cart.format.decimal_point');
}
if(is_null($thousandSeperator)){
$thousandSeperator = is_null(config('cart.format.thousand_seperator')) ? ',' : config('cart.format.thousand_seperator');
}
return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
}
}