restore composer.json, add mysqli extension

This commit is contained in:
2026-04-15 17:02:52 +05:00
commit 77cf56a348
4317 changed files with 1397107 additions and 0 deletions

259
app/Helpers/Cart.php Executable file
View File

@@ -0,0 +1,259 @@
<?php
namespace App\Helpers;
use App\Models\Cart as Model;
use App\Models\Product;
use Illuminate\Support\Facades\Cookie;
class Cart
{
/**
* @return array
*/
public function getProducts()
{
if (Cookie::has('cart_token')) {
$cart = Model::findByToken(Cookie::get('cart_token'))
->with('product:id,child_id', 'product.product:id,name,price,price_discount,poster_thumb,slug,leader_of_sales,currency,article_number,count,available')
->whereHas('product', function ($query) {
$query->whereHas('product', function ($query) {
return $query->isAvailable();
});
})
->get();
} else if (auth()->check()) {
$cart = Model::findByUser(auth()->user()->id)
->with('product:id,child_id', 'product.product:id,name,price,price_discount,poster_thumb,slug,leader_of_sales,currency,article_number,count,available')
->whereHas('product', function ($query) {
$query->whereHas('product', function ($query) {
return $query->isAvailable();
});
})
->get();
} else {
$cart = collect([]);
}
$cart->map(function ($cart) {
if (!empty($cart->product) && !empty($cart->product->product)) {
$cart->price = $cart->product->product->getPrice();
$cart->price_discount = $cart->product->product->price_discount == null ? null : $cart->product->product->getDiscountPrice();
} else {
$cart->price = 0;
$cart->price_discount = 0;
}
});
$prices = $cart->map(function ($cart) {
$price = 0;
$price_discount = 0;
$price_current = 0;
$price += $cart->price * $cart->count;
$price_discount += $cart->price_discount * $cart->count;
$price_current = $cart->price_discount ? $cart->price_discount * $cart->count : $cart->price * $cart->count;
return $price_current;
});
$prices = array_sum($prices->toArray());
return [$prices, $cart];
}
/**
* @param $request
* @return mixed
*/
public function store($request)
{
if (Cookie::has('cart_token')) {
$cart = Model::findByToken(Cookie::get('cart_token'))->where('product_id', $request->product_id)->where('size', $request->getSize())->first();
if (!empty($cart)) {
$cart->update([
'count' => $cart->count + $request->count
]);
} else {
Model::create([
'product_id' => $request->product_id,
'count' => $request->count,
'size' => $request->getSize(),
'token' => $request->cookie('cart_token')
]);
}
$count = Model::findByToken($request->cookie('cart_token'))->whereHas('product', function ($query) {
$query->whereHas('product');
})->count();
} elseif (auth()->check()) {
$cart = Model::findByUser(auth()->user()->id)->where('product_id', $request->product_id)->where('size', $request->getSize())->first();
if (!empty($cart)) {
$cart->update([
'count' => $cart->count + $request->count
]);
} else {
Model::create([
'product_id' => $request->product_id,
'count' => $request->count,
'size' => $request->getSize(),
'user_id' => auth()->user()->id
]);
}
$count = auth()->user()->cart()->whereHas('product', function ($query) {
$query->whereHas('product');
})->count();
}
return $count;
}
/**
* @param $product
* @return mixed
*/
public function delete($product)
{
if (Cookie::has('cart_token')) {
$cart = Model::findByToken(Cookie::get('cart_token'))->where('product_id', $product)->first();
if (!empty($cart))
$cart->delete();
$count = Model::findByToken(Cookie::get('cart_token'))->whereHas('product', function ($query) {
$query->whereHas('product');
})->count();
} else if (auth()->check()) {
$cart = Model::findByUser(auth()->user()->id)->where('product_id', $product)->first();
if (!empty($cart))
$cart->delete();
$count = auth()->user()->cart()->whereHas('product', function ($query) {
$query->whereHas('product');
})->count();
}
return $count;
}
/**
*
*/
public function removeAll()
{
if (Cookie::has('cart_token')) {
$cart = Model::findByToken(Cookie::get('cart_token'))->delete();
} elseif (auth()->check()) {
$cart = Model::findByUser(auth()->user()->id)->delete();
}
}
/**
* @param $request
* @return array
*/
public function update($request)
{
$product = Product::find($request->product_id);
$max_count = $product->product->count;
if (Cookie::has('cart_token')) {
$cart = Model::findByToken($request->cookie('cart_token'))->where('product_id', $request->product_id)->where('size', $request->getSize())->first();
if (!empty($cart)) {
$cart->update([
'count' => $request->count
]);
} else {
$cart = Model::create([
'product_id' => $request->product_id,
'count' => $request->count,
'size' => $request->getSize(),
'token' => $request->cookie('cart_token')
]);
}
$count = Model::findByToken($request->cookie('cart_token'))->whereHas('product', function ($query) {
$query->whereHas('product');
})->count();
} elseif (auth()->check()) {
$cart = Model::findByUser(auth()->user()->id)->where('product_id', $request->product_id)->where('size', $request->getSize())->first();
if (!empty($cart)) {
$cart->update([
'count' => $request->count
]);
} else {
$cart = Model::create([
'product_id' => $request->product_id,
'count' => $request->count,
'size' => $request->getSize(),
'user_id' => auth()->user()->id
]);
}
$count = auth()->user()->cart()->whereHas('product', function ($query) {
$query->whereHas('product');
})->count();
}
// $price = 0;
// $price_discount = 0;
// $price_current = 0;
//
// $price += $cart->product->product->getPrice();
// $price_discount += $cart->product->product->getDiscountPrice();
//
// $price_current = $cart->product->product->price_discount ? $price_discount : $price;
list($price) = $this->getProducts();
return [$price, $count, $max_count];
}
public function getBasketCount()
{
if (Cookie::has('cart_token')) {
$count = Model::findByToken(Cookie::get('cart_token'))->whereHas('product', function ($query) {
$query->whereHas('product', function ($query) {
return $query->isAvailable();
});
})->count();
} elseif (auth()->check()) {
$count = auth()->user()->cart()->whereHas('product', function ($query) {
$query->whereHas('product', function ($query) {
return $query->isAvailable();
});
})->count();
}
return $count;
}
/**
* @param $user_id
*/
public function AddToCartUpdate($user_id)
{
$token = Cookie::get('cart_token');
$carts = Model::findByToken($token)->get()->map(function ($cart) {
return $cart->product_id;
});
$cart_user = Model::findByUser($user_id)->get()->map(function ($cart) {
return $cart->product_id;
});
$product_id = array_diff($carts->toArray(), $cart_user->toArray());
Model::whereIn('product_id', $product_id)->findByToken($token)->update([
'token' => null,
'user_id' => $user_id
]);
Cookie::queue(Cookie::forget('cart_token'));
}
}

382
app/Helpers/DashboardStatic.php Executable file
View File

@@ -0,0 +1,382 @@
<?php
namespace App\Helpers;
use App\Models\Billing;
use App\Models\Order;
use App\Models\User;
use Illuminate\Support\Facades\DB;
class DashboardStatic
{
public static function getCountProcessing()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = self::getCountStaticsSql('processing', $start, $now);
return $data;
}
public static function getCountCollected()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = self::getCountStaticsSql('collected', $start, $now);
return $data;
}
public static function getCountClosed()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = self::getCountStaticsSql('closed', $start, $now);
return $data;
}
public static function getCountCancelled()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = self::getCountStaticsSql('cancelled', $start, $now);
return $data;
}
public static function getCountReplacement()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = self::getCountStaticsSql('replacement', $start, $now);
return $data;
}
public static function getCountArchived()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = Order::select(DB::raw("COUNT(id) as count"), DB::raw('DATE(created_at) as date'))
->whereBetween('created_at', ["{$start->toDateString()} 00:00:01", "{$now} 23:59:59"])
->groupBy('date')
// ->archived(true)
->get();
$data = $data->map(function ($order) {
$array = [];
$array[$order->date] = $order->count;
return $array;
})->collapse();
$order_data = [];
for ($i = 0; $i <= 30; $i++) {
$day = $start->copy()->addDays($i)->toDateString();
$order_data[] = isset($data[$day]) ? $data[$day] : 0;
}
return $order_data;
}
public static function getUserStatics()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$users = User::select(DB::raw("COUNT(id) as count"), DB::raw('DATE(created_at) as date'))
->whereBetween('created_at', ["{$start->toDateString()} 00:00:01", "{$now} 23:59:59"])
->groupBy('date')
->get();
//$users = collect($users)->groupBy('date');
$users = $users->map(function ($order) {
$array = [];
$array[$order->date] = $order->count;
return $array;
});
return $users;
$data = [];
for ($i = 0; $i <= 30; $i++) {
$day = $start->copy()->addDays($i)->toDateString();
$data[] = isset($users[$day]) ? $users[$day] : 0;
}
return $data;
}
private static function getCountStaticsSql($status, $start, $now)
{
$data = Order::select(DB::raw("COUNT(id) as count"), DB::raw('DATE(created_at) as date'))
->where('status', $status)
->whereBetween('created_at', ["{$start->toDateString()} 00:00:01", "{$now} 23:59:59"])
->groupBy('date')
// ->archived(false)
->get();
$data = $data->map(function ($order) {
$array = [];
$array[$order->date] = $order->count;
return $array;
})->collapse();
$order_data = [];
for ($i = 0; $i <= 30; $i++) {
$day = $start->copy()->addDays($i)->toDateString();
$order_data[] = isset($data[$day]) ? $data[$day] : 0;
}
return $order_data;
}
public static function getCountInWay()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = Order::select(DB::raw("COUNT(id) as count"), DB::raw('DATE(created_at) as date'))
->whereIn('status', ['waiting_buyer', 'in_way'])
->whereBetween('created_at', ["{$start->toDateString()} 00:00:01", "{$now} 23:59:59"])
->groupBy('date')
// ->archived(false)
->get();
$data = $data->map(function ($order) {
$array = [];
$array[$order->date] = $order->count;
return $array;
})->collapse();
$order_data = [];
for ($i = 0; $i <= 30; $i++) {
$day = $start->copy()->addDays($i)->toDateString();
$order_data[] = isset($data[$day]) ? $data[$day] : 0;
}
return $order_data;
}
public static function getSuccessTransactions()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = Billing::select(DB::raw("COUNT(id) as count"), DB::raw('DATE(created_at) as date'))
->where('status', 'payed')
->whereBetween('created_at', ["{$start->toDateString()} 00:00:01", "{$now} 23:59:59"])
->groupBy('date')
->get();
$data = $data->map(function ($order) {
$array = [];
$array[$order->date] = $order->count;
return $array;
})->collapse();
$transactions = [];
for ($i = 0; $i <= 30; $i++) {
$day = $start->copy()->addDays($i)->toDateString();
$transactions[] = isset($data[$day]) ? $data[$day] : 0;
}
return $transactions;
}
public static function getWaitingTransactions()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = Billing::select(DB::raw("COUNT(id) as count"), DB::raw('DATE(created_at) as date'))
->where('status', 'waiting')
->whereBetween('created_at', ["{$start->toDateString()} 00:00:01", "{$now} 23:59:59"])
->groupBy('date')
->get();
$data = $data->map(function ($order) {
$array = [];
$array[$order->date] = $order->count;
return $array;
})->collapse();
$transactions = [];
for ($i = 0; $i <= 30; $i++) {
$day = $start->copy()->addDays($i)->toDateString();
$transactions[] = isset($data[$day]) ? $data[$day] : 0;
}
return $transactions;
}
public static function getRefusedTransactions()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = Billing::select(DB::raw("COUNT(id) as count"), DB::raw('DATE(created_at) as date'))
->where('status', 'refused')
->whereBetween('created_at', ["{$start->toDateString()} 00:00:01", "{$now} 23:59:59"])
->groupBy('date')
->get();
$data = $data->map(function ($order) {
$array = [];
$array[$order->date] = $order->count;
return $array;
})->collapse();
$transactions = [];
for ($i = 0; $i <= 30; $i++) {
$day = $start->copy()->addDays($i)->toDateString();
$transactions[] = isset($data[$day]) ? $data[$day] : 0;
}
return $transactions;
}
public static function getCreditPayed()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = Order::select(DB::raw("COUNT(id) as count"), DB::raw('DATE(created_at) as date'))
->where('payment_type', 'credit')
->where('payment_status', 'payed')
->whereBetween('created_at', ["{$start->toDateString()} 00:00:01", "{$now} 23:59:59"])
->groupBy('date')
// ->archived(false)
->get();
$data = $data->map(function ($order) {
$array = [];
$array[$order->date] = $order->count;
return $array;
})->collapse();
$order_data = [];
for ($i = 0; $i <= 30; $i++) {
$day = $start->copy()->addDays($i)->toDateString();
$order_data[] = isset($data[$day]) ? $data[$day] : 0;
}
return $order_data;
}
public static function getCancelledCredit()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = Order::select(DB::raw("COUNT(id) as count"), DB::raw('DATE(created_at) as date'))
->where('payment_type', 'credit')
->where('payment_status', 'cancelled')
->whereBetween('created_at', ["{$start->toDateString()} 00:00:01", "{$now} 23:59:59"])
->groupBy('date')
// ->archived(false)
->get();
$data = $data->map(function ($order) {
$array = [];
$array[$order->date] = $order->count;
return $array;
})->collapse();
$order_data = [];
for ($i = 0; $i <= 30; $i++) {
$day = $start->copy()->addDays($i)->toDateString();
$order_data[] = isset($data[$day]) ? $data[$day] : 0;
}
return $order_data;
}
public static function getReviewCredit()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = Order::select(DB::raw("COUNT(id) as count"), DB::raw('DATE(created_at) as date'))
->where('payment_type', 'credit')
->where('payment_status', 'review')
->whereBetween('created_at', ["{$start->toDateString()} 00:00:01", "{$now} 23:59:59"])
->groupBy('date')
// ->archived(false)
->get();
$data = $data->map(function ($order) {
$array = [];
$array[$order->date] = $order->count;
return $array;
})->collapse();
$order_data = [];
for ($i = 0; $i <= 30; $i++) {
$day = $start->copy()->addDays($i)->toDateString();
$order_data[] = isset($data[$day]) ? $data[$day] : 0;
}
return $order_data;
}
public static function getWaitingCredit()
{
$start = now()->subDays(30);
$now = now()->toDateString();
$data = Order::select(DB::raw("COUNT(id) as count"), DB::raw('DATE(created_at) as date'))
->where('payment_type', 'credit')
->where('payment_status', 'waiting')
->whereBetween('created_at', ["{$start->toDateString()} 00:00:01", "{$now} 23:59:59"])
->groupBy('date')
// ->archived(false)
->get();
$data = $data->map(function ($order) {
$array = [];
$array[$order->date] = $order->count;
return $array;
})->collapse();
$order_data = [];
for ($i = 0; $i <= 30; $i++) {
$day = $start->copy()->addDays($i)->toDateString();
$order_data[] = isset($data[$day]) ? $data[$day] : 0;
}
return $order_data;
}
}

181
app/Helpers/MainPage.php Executable file
View File

@@ -0,0 +1,181 @@
<?php
namespace App\Helpers;
use App\Models\Category;
use App\Models\Compilation;
use App\Models\Post;
use App\Models\Slider;
use Illuminate\Support\Str;
use Jenssegers\Agent\Agent;
class MainPage
{
protected $agent;
protected $lang;
/**
* MainPage constructor.
*/
public function __construct()
{
$this->agent = new Agent();
$this->lang = app()->getLocale();
}
/**
* @param $type
* @return mixed
*/
public function getMainProducts($type)
{
$newProducts = Compilation::published();
switch ($type) {
case 'new':
$newProducts = $newProducts->newProducts();
break;
case 'popular':
$newProducts = $newProducts->popularProducts();
break;
case 'lider':
$newProducts = $newProducts->liderProducts();
break;
default:
$newProducts = $newProducts->newProducts();
break;
}
$newProducts = $newProducts->published()->first()->products()->published()->with('categories:id,name,parent_id,slug', 'categories.parent.parent', 'children:id,child_id', 'children')->isAvailable()->get(['id', 'name', 'price', 'price_discount', 'poster_thumb', 'slug', 'leader_of_sales', 'currency', 'available', 'count']);
$newProducts->map(function ($product) {
$product->categories->map(function ($category) {
if ($category->parent) {
if ($category->parent->parent) {
$category->link = route('category.showParent', [$category->parent->parent->slug, $category->parent->slug, $category->slug]);
} else {
$category->link = route('category.show', [$category->parent->slug, $category->slug]);
}
} else {
$category->link = route('category.view', $category->slug);
}
});
$product->price = $product->getPrice();
$product->price_discount = $product->price_discount == null ? null : $product->getDiscountPrice();
});
return $newProducts;
}
/**
* @return mixed
*/
public function getPopularCategories()
{
$popularCategories = Category::latest('id')
->latest('id')
->where('popular', true)
->get();
$popularCategories->map(function ($category) {
if ($category->parent) {
if ($category->parent->parent) {
$category->link = route('category.showParent', [$category->parent->parent->slug, $category->parent->slug, $category->slug]);
} else {
$category->link = route('category.show', [$category->parent->slug, $category->slug]);
}
} else {
$category->link = route('category.view', $category->slug);
}
});
return $popularCategories;
}
/**
* @param $lang
* @return array
*/
public function getSliders($lang)
{
if ($this->agent->isMobile()) {
if ($this->agent->isTablet()) {
$sliders = Slider::where('language', $lang)
->where('type', 'desktop')
->where('placement', 'top')
->orderBy('position', 'asc')
->published()
->get();
$middleSliders = Slider::where('language', $lang)
->where('type', 'desktop')
->where('placement', 'middle')
->orderBy('position', 'asc')
->published()
->get();
} else {
$sliders = Slider::where('language', $lang)
->where('type', 'mobile')
->where('placement', 'top')
->orderBy('position', 'asc')
->published()
->get();
$middleSliders = Slider::where('language', $lang)
->where('type', 'mobile')
->where('placement', 'middle')
->orderBy('position', 'asc')
->published()
->get();
}
} else {
$sliders = Slider::where('language', $lang)
->where('type', 'desktop')
->where('placement', 'top')
->orderBy('position', 'asc')
->published()
->get();
$middleSliders = Slider::where('language', $lang)
->where('type', 'desktop')
->where('placement', 'middle')
->orderBy('position', 'asc')
->published()
->get();
}
return [$sliders, $middleSliders];
}
/**
* @param $lang
* @return array
*/
public function getPosts($lang)
{
$posts = Post::latest('id')
->where('language', $lang)
->where('topped', false)
->whereIn('type', ['news','sales'])
->limit(3)
->get();
$posts->map(function ($post) {
$post->date = $post->getDatePublic();
$post->content = Str::limit($post->content, 150);
});
$toppedPost = Post::where('topped', true)
->where('language', $lang)
->select('id', 'name', 'language', 'created_at', 'image', 'slug', 'type', 'content')
->first();
$toppedPost->date = $toppedPost->getDatePublic();
return [$posts, $toppedPost];
}
}

27
app/Helpers/MassAction.php Executable file
View File

@@ -0,0 +1,27 @@
<?php
namespace App\Helpers;
use App\Models\Product;
class MassAction
{
public function massDelete(array $attributes)
{
Product::whereIn('id', $attributes)->delete();
}
public function massUnpublish(array $attributes)
{
Product::whereIn('id', $attributes)->update([
'published' => false
]);
}
public function massPublish(array $attributes)
{
Product::whereIn('id', $attributes)->update([
'published' => true
]);
}
}

132
app/Helpers/Month.php Executable file
View File

@@ -0,0 +1,132 @@
<?php
namespace App\Helpers;
class Month
{
/**
* @param $lang
* @param $month
* @return string
*/
public static function LayoutMonth($lang, $month)
{
switch ($lang) {
case 'ru':
return self::monthRu($month);
break;
case 'uz':
return self::monthUz($month);
break;
}
}
/**
* @param $lang
* @param $month
* @return string
*/
public static function getMonth($lang, $month)
{
switch ($lang) {
case 'ru':
return self::monthRu($month);
break;
case 'uz':
return self::monthUz($month);
break;
}
}
/**
* @param $month
* @return string
*/
protected static function monthRu($month)
{
switch ($month) {
case '01':
return 'января';
break;
case '02':
return 'февраля';
break;
case '03':
return 'март';
break;
case '04':
return 'апреля';
break;
case '05':
return 'май';
break;
case '06':
return 'июня';
break;
case '07':
return 'июля';
break;
case '08':
return 'августа';
break;
case '09':
return 'сентября';
break;
case '10':
return 'октября';
break;
case '11':
return 'ноября';
break;
case '12':
return 'декабря';
break;
}
}
/**
* @param $month
* @return string
*/
protected static function monthUz($month)
{
switch ($month) {
case '01':
return 'yanvar';
break;
case '02':
return 'fevral';
break;
case '03':
return 'mart';
break;
case '04':
return 'april';
break;
case '05':
return 'may';
break;
case '06':
return 'iyun';
break;
case '07':
return 'iyul';
break;
case '08':
return 'avgust';
break;
case '09':
return 'sentyabr';
break;
case '10':
return 'oktyabr';
break;
case '11':
return 'noyabr';
break;
case '12':
return 'dekabr';
break;
}
}
}

212
app/Helpers/Product.php Executable file
View File

@@ -0,0 +1,212 @@
<?php
namespace App\Helpers;
use App\Models\User;
use App\Models\Order;
use App\Models\Currency;
use App\Models\OrderProducts;
use App\Models\Product as Model;
use App\Models\NotificationAvailable;
class Product
{
protected $currency;
public function __construct()
{
$this->currency = Currency::latest('id', 'desc')->limit(1)->first();
}
/**
* @param $product
* @return mixed
*/
public function getPopularProducts($product)
{
$popularProducts = Model::inRandomOrder()
->notChilds()
->published()
->whereNotIn('id', [$product->id])
->whereHas('childrens')
->isAvailable()
->with('comments', 'childrens:id,child_id,sizes,color_id', 'childrens.screens', 'childrens.color', 'children')
->limit(10)
->get();
$popularProducts->map(function ($product) {
$product->categories->map(function ($category) {
if ($category->parent) {
if ($category->parent->parent) {
$category->link = route('category.showParent', [$category->parent->parent->slug, $category->parent->slug, $category->slug]);
} else {
$category->link = route('category.show', [$category->parent->slug, $category->slug]);
}
} else {
$category->link = route('category.view', $category->slug);
}
});
});
$popularProducts->makeHidden(['created_at', 'updated_at', 'sizes', 'color_id']);
return $popularProducts;
}
/**
* @param Model $product
* @return array
*/
public function getProductShowAttributes(Model $product)
{
$product->update([
'views' => $product->views + 1
]);
$product->loadMissing(['comments', 'categories:id,name', 'childrens:id,child_id,sizes,color_id', 'childrens.screens', 'childrens.color', 'characteristics', 'children.screen']);
$product->price = $product->getPrice();
$product->price_discount = $product->price_discount == null ? null : $product->getDiscountPrice();
$product->makeHidden(['created_at', 'updated_at', 'sizes', 'color_id']);
$category = $product->categories()->first();
return [$product, $category];
}
/**
* @param $name
* @return mixed
*/
public function getSearchProduct($name)
{
$products = Model::select('id', 'name', 'poster_thumb', 'price', 'price_discount', 'slug', 'currency', 'popular', 'article_number', 'leader_of_sales', 'available', 'count')
->where('name->ru', 'ilike', '%'.$name.'%')
->with('children')
->whereHas('childrens')
->published()
->paginate(20);
$products->map(function ($product) {
$product->price = $product->getPrice();
$product->price_discount = $product->price_discount == null ? null : $product->getDiscountPrice();
});
return $products;
}
/**
* @param $request
*/
public function createNotificationProduct($request)
{
$phone = str_replace(['-', ' ', '(', ')', '+'], '', $request->phone);
NotificationAvailable::firstOrCreate([
'product_id' => $request->product_id,
'phone' => $phone
]);
}
/**
* @param Model $product
* @param $request
* @return mixed
*/
public function StoreBuyOneClick(Model $product, $request)
{
$currency = Currency::latest('id', 'desc')->first();
$price = $product->price_discount ? $product->getPriceDiscount() : $product->getPrice();
$phone = str_replace(['+', '(', ')', ' ', '-'], '', $request->phone);
if (auth()->check() && auth()->user()->phone == $phone) {
$user_id = auth()->user()->id;
} else {
$user_id = $this->createUser($phone, $request);
}
$order = Order::create([
'payment_type' => 'cash',
'user_id' => $user_id,
'type_delivery' => 'pickup',
'type' => 'one_click',
'currency' => $currency,
'payment_status' => 'cash',
'price_delivery' => 0,
'price_product' => $price,
'comment' => $request->comment
]);
$discount = $product->price_discount ? 100 - $product->price_discount * 100 / $product->price : null;
OrderProducts::create([
'order_id' => $order->id,
'product_id' => $product->id,
'discount' => round($discount),
'count' => 1,
'size' => null,
'color_id' => $product->children->id,
'price' => $price
]);
return $order;
}
public function storeOnCredit($product)
{
$price = $product->price_discount ? $product->getPriceDiscount() : $product->getPrice();
$currency = Currency::latest('id', 'desc')->first();
$order = Order::create([
'payment_type' => 'credit',
'user_id' => auth()->user()->id,
'type_delivery' => 'pickup',
'currency' => $currency,
'payment_status' => 'waiting',
'price_delivery' => 0,
'price_product' => $price,
]);
$discount = $product->price_discount ? 100 - $product->price_discount * 100 / $product->price : null;
OrderProducts::create([
'order_id' => $order->id,
'product_id' => $product->id,
'discount' => round($discount),
'count' => 1,
'size' => null,
'color_id' => $product->children->id,
'price' => round($price, 0)
]);
return $order;
}
/**
* @param $phone
* @param $request
* @return mixed
*/
private function createUser($phone, $request)
{
$user = User::findByPhone($phone)->first();
if (!empty($user)) {
$user_id = $user->id;
} else {
$user = User::create([
'first_name' => $request->first_name,
'phone' => $phone,
'email' => $request->email
]);
$user_id = $user->id;
}
return $user_id;
}
}

50
app/Helpers/helpers.php Executable file
View File

@@ -0,0 +1,50 @@
<?php
use App\Models\Cart;
use App\Models\PersonalAccessToken;
use App\Models\User;
use Illuminate\Support\Facades\Cookie;
if (! function_exists('getAuthUser')) {
function getAuthUser()
{
$bearer = request()->bearerToken();
if ($bearer) {
[$personalAccessTokenId, $token] = explode('|', $bearer, 2);
$personalAccessToken = PersonalAccessToken::find($personalAccessTokenId);
if ($personalAccessToken && hash_equals($personalAccessToken->token, hash('sha256', $token))) {
return User::find($personalAccessToken->tokenable_id);
}
}
return null;
}
}
if (! function_exists('getCart')) {
function getCart($productId = null)
{
$user = getAuthUser();
if ($user) {
$cart = Cart::where('user_id', $user->id)->get();
} else {
$cart = Cart::where('token', Cookie::get('cart_token'))->get();
}
if ($productId) {
return $cart->where('product_id', $productId)->first();
}
return $cart;
}
}
if (! function_exists('ceiling')) {
function ceiling(float|int $num, $significance = 1)
{
return (ceil($num / $significance)) * $significance;
}
}