exchange_rate)) * Currency::where('code', 'USD')->first()->exchange_rate;
}
}
if (!function_exists('convert_to_kes')) {
function convert_to_kes($amount)
{
$currency = Currency::find(get_setting('system_default_currency'));
return (floatval($amount) / floatval($currency->exchange_rate)) * Currency::where('code', 'KES')->first()->exchange_rate;
}
}
//filter products based on vendor activation system
if (!function_exists('filter_products')) {
function filter_products($products)
{
// dd(get_setting('vendor_system_activation'));
$verified_sellers = verified_sellers_id();
if (get_setting('vendor_system_activation') == 1) {
return $products->where('p.approved', '1')->where('published', '1')->where('p.auction_product', 0)->orderBy('p.created_at', 'desc')->where(function ($p) use ($verified_sellers) {
$p->where('p.added_by', 'admin')->orWhere(function ($q) use ($verified_sellers) {
// $q->join($verified_sellers ." as sp","sp.user_id","=","p.user_id");
$q->whereIn('p.user_id',$verified_sellers);
});
});
} else {
return $products->where('p.published', '1')->where('p.auction_product', 0)->where('p.added_by', 'admin');
}
}
}
//cache products based on category
if (!function_exists('get_cached_products')) {
function get_cached_products($category_id = null)
{
$products = \App\Models\Product::where('published', 1)->where('approved', '1')->where('auction_product', 0);
$verified_sellers = verified_sellers_id();
// dd($verified_sellers);
// exit();
if (get_setting('vendor_system_activation') == 1) {
$products = $products->where(function ($p) use ($verified_sellers) {
$p->where('p.added_by', 'admin')->orWhere(function ($q) use ($verified_sellers) {
$q->whereIn('p.user_id',$verified_sellers);
});
});
} else {
$products = $products->where('added_by', 'admin');
}
if ($category_id != null) {
return Cache::remember('products-category-' . $category_id, 86400, function () use ($category_id, $products) {
$category_ids = CategoryUtility::children_ids($category_id);
$category_ids[] = $category_id;
return $products->whereIn('category_id', $category_ids)->latest()->take(12)->get();
});
} else {
return Cache::remember('products', 86400, function () use ($products) {
return $products->latest()->take(12)->get();
});
}
}
}
if (!function_exists('verified_sellers_id')) {
function verified_sellers_id()
{
// $builder=DB::table("shops","sp")->where('sp.verification_status', 1)->select(['sp.user_id']);
// $bindings=$builder->getBindings();
// $sql = str_replace('?', '%s', $builder->toSql());
// $sql = sprintf($sql, ...$bindings);
// return $sql;
return Cache::rememberForever('verified_sellers_id', function () {
return App\Models\Shop::where('verification_status', 1)->pluck('user_id')->toArray();
});
}
}
if (!function_exists('get_system_default_currency')) {
function get_system_default_currency()
{
return Cache::remember('system_default_currency', 86400, function () {
return Currency::findOrFail(get_setting('system_default_currency'));
});
}
}
//converts currency to home default currency
if (!function_exists('convert_price')) {
function convert_price($price)
{
if (Session::has('currency_code') && (Session::get('currency_code') != get_system_default_currency()->code)) {
$price = floatval($price) / floatval(get_system_default_currency()->exchange_rate);
$price = floatval($price) * floatval(Session::get('currency_exchange_rate'));
}
return $price;
}
}
//gets currency symbol
if (!function_exists('currency_symbol')) {
function currency_symbol()
{
if (Session::has('currency_symbol')) {
return Session::get('currency_symbol');
}
return get_system_default_currency()->symbol;
}
}
//formats currency
if (!function_exists('format_price')) {
function format_price($price)
{
if (get_setting('decimal_separator') == 1) {
$fomated_price = number_format($price, get_setting('no_of_decimals'));
} else {
$fomated_price = number_format($price, get_setting('no_of_decimals'), ',', '.');
}
if (get_setting('symbol_format') == 1) {
return currency_symbol() . $fomated_price;
} else if (get_setting('symbol_format') == 3) {
return currency_symbol() . ' ' . $fomated_price;
} else if (get_setting('symbol_format') == 4) {
return $fomated_price . ' ' . currency_symbol();
}
return $fomated_price . currency_symbol();
}
}
//formats price to home default price with convertion
if (!function_exists('single_price')) {
function single_price($price)
{
return format_price(convert_price($price));
}
}
if (!function_exists('discount_in_percentage')) {
function discount_in_percentage($product)
{
$base = home_base_price($product, false);
$reduced = home_discounted_base_price($product, false);
$discount = $base - $reduced;
$dp = ($discount * 100) / ($base > 0 ? $base : 1);
return round($dp);
}
}
//Shows Price on page based on carts
if (!function_exists('cart_product_price')) {
function cart_product_price($cart_product, $product, $formatted = true, $tax = true)
{
$str = '';
if (isset($cart_product['variation'])){
if ($cart_product['variation'] != null) {
$str = $cart_product['variation'];
}
}
$price = 0;
$product_stock = $product->stocks->where('variant', $str)->first();
if ($product_stock) {
$price = $product_stock->price;
}
//discount calculation
$discount_applicable = false;
if ($product->discount_start_date == null) {
$discount_applicable = true;
} elseif (strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date) {
$discount_applicable = true;
}
if ($discount_applicable) {
if ($product->discount_type == 'percent') {
$price -= ($price * $product->discount) / 100;
} elseif ($product->discount_type == 'amount') {
$price -= $product->discount;
}
}
//calculation of taxes
if ($tax) {
$taxAmount = 0;
foreach ($product->taxes as $product_tax) {
if ($product_tax->tax_type == 'percent') {
$taxAmount += ($price * $product_tax->tax) / 100;
} elseif ($product_tax->tax_type == 'amount') {
$taxAmount += $product_tax->tax;
}
}
$price += $taxAmount;
}
if ($formatted) {
return format_price(convert_price($price));
} else {
return $price;
}
}
}
if (!function_exists('cart_product_tax')) {
function cart_product_tax($cart_product, $product, $formatted = true)
{
$str = '';
if ($cart_product['variation'] != null) {
$str = $cart_product['variation'];
}
$product_stock = $product->stocks->where('variant', $str)->first();
$price = $product_stock->price;
//discount calculation
$discount_applicable = false;
if ($product->discount_start_date == null) {
$discount_applicable = true;
} elseif (strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date) {
$discount_applicable = true;
}
if ($discount_applicable) {
if ($product->discount_type == 'percent') {
$price -= ($price * $product->discount) / 100;
} elseif ($product->discount_type == 'amount') {
$price -= $product->discount;
}
}
//calculation of taxes
$tax = 0;
foreach ($product->taxes as $product_tax) {
if ($product_tax->tax_type == 'percent') {
$tax += ($price * $product_tax->tax) / 100;
} elseif ($product_tax->tax_type == 'amount') {
$tax += $product_tax->tax;
}
}
if ($formatted) {
return format_price(convert_price($tax));
} else {
return $tax;
}
}
}
if (!function_exists('cart_product_discount')) {
function cart_product_discount($cart_product, $product, $formatted = false)
{
$str = '';
if ($cart_product['variation'] != null) {
$str = $cart_product['variation'];
}
$product_stock = $product->stocks->where('variant', $str)->first();
$price = $product_stock->price;
//discount calculation
$discount_applicable = false;
$discount = 0;
if ($product->discount_start_date == null) {
$discount_applicable = true;
} elseif (strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date) {
$discount_applicable = true;
}
if ($discount_applicable) {
if ($product->discount_type == 'percent') {
$discount = ($price * $product->discount) / 100;
} elseif ($product->discount_type == 'amount') {
$discount = $product->discount;
}
}
if ($formatted) {
return format_price(convert_price($discount));
} else {
return $discount;
}
}
}
// all discount
if (!function_exists('carts_product_discount')) {
function carts_product_discount($cart_products, $formatted = false)
{
$discount = 0;
foreach ($cart_products as $key => $cart_product) {
$str = '';
$product = \App\Models\Product::find($cart_product['product_id']);
if ($cart_product['variation'] != null) {
$str = $cart_product['variation'];
}
$product_stock = $product->stocks->where('variant', $str)->first();
$price = $product_stock->price;
//discount calculation
$discount_applicable = false;
if ($product->discount_start_date == null) {
$discount_applicable = true;
} elseif (strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date) {
$discount_applicable = true;
}
if ($discount_applicable) {
if ($product->discount_type == 'percent') {
$discount += ($price * $product->discount) / 100;
} elseif ($product->discount_type == 'amount') {
$discount += $product->discount;
}
}
}
if ($formatted) {
return format_price(convert_price($discount));
} else {
return $discount;
}
}
}
if (!function_exists('carts_coupon_discount')) {
function carts_coupon_discount($code, $formatted = false)
{
$coupon = Coupon::where('code', $code)->first();
$coupon_discount = 0;
if ($coupon != null) {
if (strtotime(date('d-m-Y')) >= $coupon->start_date && strtotime(date('d-m-Y')) <= $coupon->end_date) {
if (CouponUsage::where('user_id', Auth::user()->id)->where('coupon_id', $coupon->id)->first() == null) {
$coupon_details = json_decode($coupon->details);
$carts = Cart::where('user_id', Auth::user()->id)
->where('owner_id', $coupon->user_id)
->get();
if ($coupon->type == 'cart_base') {
$subtotal = 0;
$tax = 0;
$shipping = 0;
foreach ($carts as $key => $cartItem) {
$product = Product::find($cartItem['product_id']);
$subtotal += cart_product_price($cartItem, $product, false, false) * $cartItem['quantity'];
$tax += cart_product_tax($cartItem, $product, false) * $cartItem['quantity'];
$shipping += $cartItem['shipping_cost'];
}
$sum = $subtotal + $tax + $shipping;
if ($sum >= $coupon_details->min_buy) {
if ($coupon->discount_type == 'percent') {
$coupon_discount = ($sum * $coupon->discount) / 100;
if ($coupon_discount > $coupon_details->max_discount) {
$coupon_discount = $coupon_details->max_discount;
}
} elseif ($coupon->discount_type == 'amount') {
$coupon_discount = $coupon->discount;
}
}
} elseif ($coupon->type == 'product_base') {
foreach ($carts as $key => $cartItem) {
$product = Product::find($cartItem['product_id']);
foreach ($coupon_details as $key => $coupon_detail) {
if ($coupon_detail->product_id == $cartItem['product_id']) {
if ($coupon->discount_type == 'percent') {
$coupon_discount += (cart_product_price($cartItem, $product, false, false) * $coupon->discount / 100) * $cartItem['quantity'];
} elseif ($coupon->discount_type == 'amount') {
$coupon_discount += $coupon->discount * $cartItem['quantity'];
}
}
}
}
}
}
}
if ($coupon_discount > 0) {
Cart::where('user_id', Auth::user()->id)
->where('owner_id', $coupon->user_id)
->update(
[
'discount' => $coupon_discount / count($carts),
]
);
} else {
Cart::where('user_id', Auth::user()->id)
->where('owner_id', $coupon->user_id)
->update(
[
'discount' => 0,
'coupon_code' => null,
]
);
}
}
if ($formatted) {
return format_price(convert_price($coupon_discount));
} else {
return $coupon_discount;
}
}
}
//Shows Price on page based on low to high
if (!function_exists('home_price')) {
function home_price($product, $formatted = true)
{
$lowest_price = $product->unit_price;
$highest_price = $product->unit_price;
if ($product->variant_product) {
foreach ($product->stocks as $key => $stock) {
if ($lowest_price > $stock->price) {
$lowest_price = $stock->price;
}
if ($highest_price < $stock->price) {
$highest_price = $stock->price;
}
}
}
foreach ($product->taxes as $product_tax) {
if ($product_tax->tax_type == 'percent') {
$lowest_price += ($lowest_price * $product_tax->tax) / 100;
$highest_price += ($highest_price * $product_tax->tax) / 100;
} elseif ($product_tax->tax_type == 'amount') {
$lowest_price += $product_tax->tax;
$highest_price += $product_tax->tax;
}
}
if ($formatted) {
if ($lowest_price == $highest_price) {
return format_price(convert_price($lowest_price));
} else {
return format_price(convert_price($lowest_price)) . ' - ' . format_price(convert_price($highest_price));
}
} else {
return $lowest_price . ' - ' . $highest_price;
}
}
}
//Shows Price on page based on low to high with discount
if (!function_exists('home_discounted_price')) {
function home_discounted_price($product, $formatted = true)
{
$lowest_price = $product->unit_price;
$highest_price = $product->unit_price;
if ($product->variant_product) {
foreach ($product->stocks as $key => $stock) {
if ($lowest_price > $stock->price) {
$lowest_price = $stock->price;
}
if ($highest_price < $stock->price) {
$highest_price = $stock->price;
}
}
}
$discount_applicable = false;
if ($product->discount_start_date == null) {
$discount_applicable = true;
} elseif (
strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date
) {
$discount_applicable = true;
}
if ($discount_applicable) {
if ($product->discount_type == 'percent') {
$lowest_price -= ($lowest_price * $product->discount) / 100;
$highest_price -= ($highest_price * $product->discount) / 100;
} elseif ($product->discount_type == 'amount') {
$lowest_price -= $product->discount;
$highest_price -= $product->discount;
}
}
foreach ($product->taxes as $product_tax) {
if ($product_tax->tax_type == 'percent') {
$lowest_price += ($lowest_price * $product_tax->tax) / 100;
$highest_price += ($highest_price * $product_tax->tax) / 100;
} elseif ($product_tax->tax_type == 'amount') {
$lowest_price += $product_tax->tax;
$highest_price += $product_tax->tax;
}
}
if ($formatted) {
if ($lowest_price == $highest_price) {
return format_price(convert_price($lowest_price));
} else {
return format_price(convert_price($lowest_price)) . ' - ' . format_price(convert_price($highest_price));
}
} else {
return $lowest_price . ' - ' . $highest_price;
}
}
}
//Shows Base Price
if (!function_exists('home_base_price_by_stock_id')) {
function home_base_price_by_stock_id($id)
{
$product_stock = ProductStock::findOrFail($id);
$price = $product_stock->price;
$tax = 0;
foreach ($product_stock->product->taxes as $product_tax) {
if ($product_tax->tax_type == 'percent') {
$tax += ($price * $product_tax->tax) / 100;
} elseif ($product_tax->tax_type == 'amount') {
$tax += $product_tax->tax;
}
}
$price += $tax;
return format_price(convert_price($price));
}
}
if (!function_exists('home_base_price')) {
function home_base_price($product, $formatted = true)
{
$price = $product->unit_price;
$tax = 0;
foreach ($product->taxes as $product_tax) {
if ($product_tax->tax_type == 'percent') {
$tax += ($price * $product_tax->tax) / 100;
} elseif ($product_tax->tax_type == 'amount') {
$tax += $product_tax->tax;
}
}
$price += $tax;
return $formatted ? format_price(convert_price($price)) : $price;
}
}
//Shows Base Price with discount
if (!function_exists('home_discounted_base_price_by_stock_id')) {
function home_discounted_base_price_by_stock_id($id)
{
$product_stock = ProductStock::findOrFail($id);
$product = $product_stock->product;
$price = $product_stock->price;
$tax = 0;
$discount_applicable = false;
if ($product->discount_start_date == null) {
$discount_applicable = true;
} elseif (
strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date
) {
$discount_applicable = true;
}
if ($discount_applicable) {
if ($product->discount_type == 'percent') {
$price -= ($price * $product->discount) / 100;
} elseif ($product->discount_type == 'amount') {
$price -= $product->discount;
}
}
foreach ($product->taxes as $product_tax) {
if ($product_tax->tax_type == 'percent') {
$tax += ($price * $product_tax->tax) / 100;
} elseif ($product_tax->tax_type == 'amount') {
$tax += $product_tax->tax;
}
}
$price += $tax;
return format_price(convert_price($price));
}
}
//Shows Base Price with discount
if (!function_exists('home_discounted_base_price')) {
function home_discounted_base_price($product, $formatted = true)
{
$price = $product->unit_price;
$tax = 0;
$discount_applicable = false;
if ($product->discount_start_date == null) {
$discount_applicable = true;
} elseif (
strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date
) {
$discount_applicable = true;
}
if ($discount_applicable) {
if ($product->discount_type == 'percent') {
$price -= ($price * $product->discount) / 100;
} elseif ($product->discount_type == 'amount') {
$price -= $product->discount;
}
}
foreach ($product->taxes as $product_tax) {
if ($product_tax->tax_type == 'percent') {
$tax += ($price * $product_tax->tax) / 100;
} elseif ($product_tax->tax_type == 'amount') {
$tax += $product_tax->tax;
}
}
$price += $tax;
return $formatted ? format_price(convert_price($price)) : $price;
}
}
if (!function_exists('renderStarRating')) {
function renderStarRating($rating, $maxRating = 5)
{
$fullStar = "";
$halfStar = "";
$emptyStar = "";
$rating = $rating <= $maxRating ? $rating : $maxRating;
$fullStarCount = (int)$rating;
$halfStarCount = ceil($rating) - $fullStarCount;
$emptyStarCount = $maxRating - $fullStarCount - $halfStarCount;
$html = str_repeat($fullStar, $fullStarCount);
$html .= str_repeat($halfStar, $halfStarCount);
$html .= str_repeat($emptyStar, $emptyStarCount);
echo $html;
}
}
function translate($key, $lang = null, $addslashes = false)
{
if ($lang == null) {
$lang = App::getLocale();
}
$lang_key = preg_replace('/[^A-Za-z0-9\_]/', '', str_replace(' ', '_', strtolower($key)));
$translations_en = Cache::rememberForever('translations-en', function () {
return Translation::where('lang', 'en')->pluck('lang_value', 'lang_key')->toArray();
});
if (!isset($translations_en[$lang_key])) {
$translation_def = new Translation;
$translation_def->lang = 'en';
$translation_def->lang_key = $lang_key;
$translation_def->lang_value = str_replace(array("\r", "\n", "\r\n"), "", $key);
$translation_def->save();
Cache::forget('translations-en');
}
// return user session lang
$translation_locale = Cache::rememberForever("translations-{$lang}", function () use ($lang) {
return Translation::where('lang', $lang)->pluck('lang_value', 'lang_key')->toArray();
});
if (isset($translation_locale[$lang_key])) {
return $addslashes ? addslashes(trim($translation_locale[$lang_key])) : trim($translation_locale[$lang_key]);
}
// return default lang if session lang not found
$translations_default = Cache::rememberForever('translations-' . env('DEFAULT_LANGUAGE', 'en'), function () {
return Translation::where('lang', env('DEFAULT_LANGUAGE', 'en'))->pluck('lang_value', 'lang_key')->toArray();
});
if (isset($translations_default[$lang_key])) {
return $addslashes ? addslashes(trim($translations_default[$lang_key])) : trim($translations_default[$lang_key]);
}
// fallback to en lang
if (!isset($translations_en[$lang_key])) {
return trim($key);
}
return $addslashes ? addslashes(trim($translations_en[$lang_key])) : trim($translations_en[$lang_key]);
}
function remove_invalid_charcaters($str)
{
$str = str_ireplace(array("\\"), '', $str);
return str_ireplace(array('"'), '\"', $str);
}
function getShippingCost($carts, $index)
{
$admin_products = array();
$seller_products = array();
$cartItem = $carts[$index];
$product = Product::find($cartItem['product_id']);
if ($product->digital == 1) {
return 0;
}
foreach ($carts as $key => $cart_item) {
$item_product = Product::find($cart_item['product_id']);
if ($item_product->added_by == 'admin') {
array_push($admin_products, $cart_item['product_id']);
} else {
$product_ids = array();
if (isset($seller_products[$item_product->user_id])) {
$product_ids = $seller_products[$item_product->user_id];
}
array_push($product_ids, $cart_item['product_id']);
$seller_products[$item_product->user_id] = $product_ids;
}
}
if (get_setting('shipping_type') == 'flat_rate') {
return get_setting('flat_rate_shipping_cost') / count($carts);
} elseif (get_setting('shipping_type') == 'seller_wise_shipping') {
if ($product->added_by == 'admin') {
return get_setting('shipping_cost_admin') / count($admin_products);
} else {
return Shop::where('user_id', $product->user_id)->first()->shipping_cost / count($seller_products[$product->user_id]);
}
} elseif (get_setting('shipping_type') == 'area_wise_shipping') {
$shipping_info = Address::where('id', $carts[0]['address_id'])->first();
$city = City::where('id', $shipping_info->city_id)->first();
if ($city != null) {
if ($product->added_by == 'admin') {
return $city->cost / count($admin_products);
} else {
return $city->cost / count($seller_products[$product->user_id]);
}
}
return 0;
} else {
if ($product->is_quantity_multiplied && get_setting('shipping_type') == 'product_wise_shipping') {
return $product->shipping_cost * $cartItem['quantity'];
}
return $product->shipping_cost;
}
}
function timezones()
{
return Timezones::timezonesToArray();
}
if (!function_exists('app_timezone')) {
function app_timezone()
{
return config('app.timezone');
}
}
//return file uploaded via uploader
if (!function_exists('uploaded_asset')) {
function uploaded_asset($id)
{
if (($asset = \App\Models\Upload::find($id)) != null) {
return $asset->external_link == null ? my_asset($asset->file_name) : $asset->external_link;
}
return null;
}
}
if (!function_exists('my_asset')) {
/**
* Generate an asset path for the application.
*
* @param string $path
* @param bool|null $secure
* @return string
*/
function my_asset($path, $secure = null)
{
if( strpos($path,'http') !== false ) return $path;
if (env('FILESYSTEM_DRIVER') == 's3') {
return Storage::disk('s3')->url($path);
} else {
return app('url')->asset('public/' . $path, $secure);
}
}
}
if (!function_exists('static_asset')) {
/**
* Generate an asset path for the application.
*
* @param string $path
* @param bool|null $secure
* @return string
*/
function static_asset($path, $secure = null)
{
return app('url')->asset('public/' . $path, $secure);
}
}
// if (!function_exists('isHttps')) {
// function isHttps()
// {
// return !empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS']);
// }
// }
if (!function_exists('getBaseURL')) {
function getBaseURL()
{
$root = '//' . $_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
return $root;
}
}
if (!function_exists('getFileBaseURL')) {
function getFileBaseURL()
{
if (env('FILESYSTEM_DRIVER') == 's3') {
return env('AWS_URL') . '/';
} else {
return getBaseURL() . 'public/';
}
}
}
if (!function_exists('isUnique')) {
/**
* Generate an asset path for the application.
*
* @param string $path
* @param bool|null $secure
* @return string
*/
function isUnique($email)
{
$user = \App\Models\User::where('email', $email)->first();
if ($user == null) {
return '1'; // $user = null means we did not get any match with the email provided by the user inside the database
} else {
return '0';
}
}
}
if (!function_exists('get_setting')) {
function get_setting($key, $default = null, $lang = false)
{
$settings = Cache::remember('business_settings', 86400, function () {
return BusinessSetting::all();
});
if ($lang == false) {
$setting = $settings->where('type', $key)->first();
} else {
$setting = $settings->where('type', $key)->where('lang', $lang)->first();
$setting = !$setting ? $settings->where('type', $key)->first() : $setting;
}
return $setting == null ? $default : $setting->value;
}
}
function hex2rgba($color, $opacity = false)
{
return Colorcodeconverter::convertHexToRgba($color, $opacity);
}
if (!function_exists('isAdmin')) {
function isAdmin()
{
if (Auth::check() && (Auth::user()->user_type == 'admin' || Auth::user()->user_type == 'staff')) {
return true;
}
return false;
}
}
if (!function_exists('isSeller')) {
function isSeller()
{
if (Auth::check() && Auth::user()->user_type == 'seller') {
return true;
}
return false;
}
}
if (!function_exists('isCustomer')) {
function isCustomer()
{
if (Auth::check() && Auth::user()->user_type == 'customer') {
return true;
}
return false;
}
}
if (!function_exists('formatBytes')) {
function formatBytes($bytes, $precision = 2)
{
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
// Uncomment one of the following alternatives
$bytes /= pow(1024, $pow);
// $bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
}
}
// duplicates m$ excel's ceiling function
if (!function_exists('ceiling')) {
function ceiling($number, $significance = 1)
{
return (is_numeric($number) && is_numeric($significance)) ? (ceil($number / $significance) * $significance) : false;
}
}
//for api
if (!function_exists('get_images_path')) {
function get_images_path($given_ids, $with_trashed = false)
{
$paths = [];
foreach (explode(',', $given_ids) as $id) {
$paths[] = uploaded_asset($id);
}
return $paths;
}
}
//for api
if (!function_exists('checkout_done')) {
function checkout_done($combined_order_id, $payment)
{
$combined_order = CombinedOrder::find($combined_order_id);
foreach ($combined_order->orders as $key => $order) {
$order->payment_status = 'paid';
$order->payment_details = $payment;
$order->save();
try {
NotificationUtility::sendOrderPlacedNotification($order);
calculateCommissionAffilationClubPoint($order);
} catch (\Exception $e) {
}
}
}
}
//for api
if (!function_exists('wallet_payment_done')) {
function wallet_payment_done($user_id, $amount, $payment_method, $payment_details)
{
$user = \App\Models\User::find($user_id);
$user->balance = $user->balance + $amount;
$user->save();
$wallet = new Wallet;
$wallet->user_id = $user->id;
$wallet->amount = $amount;
$wallet->payment_method = $payment_method;
$wallet->payment_details = $payment_details;
$wallet->save();
}
}
if (!function_exists('purchase_payment_done')) {
function purchase_payment_done($user_id, $package_id)
{
$user = User::findOrFail($user_id);
$user->customer_package_id = $package_id;
$customer_package = CustomerPackage::findOrFail($package_id);
$user->remaining_uploads += $customer_package->product_upload;
$user->save();
return 'success';
}
}
if (!function_exists('product_restock')) {
function product_restock($orderDetail)
{
$variant = $orderDetail->variation;
if ($orderDetail->variation == null) {
$variant = '';
}
$product_stock = ProductStock::where('product_id', $orderDetail->product_id)
->where('variant', $variant)
->first();
if ($product_stock != null) {
$product_stock->qty += $orderDetail->quantity;
$product_stock->save();
}
}
}
//Commission Calculation
if (!function_exists('calculateCommissionAffilationClubPoint')) {
function calculateCommissionAffilationClubPoint($order)
{
(new CommissionController)->calculateCommission($order);
if (addon_is_activated('affiliate_system')) {
(new AffiliateController)->processAffiliatePoints($order);
}
if (addon_is_activated('club_point')) {
if ($order->user != null) {
(new ClubPointController)->processClubPoints($order);
}
}
$order->commission_calculated = 1;
$order->save();
}
}
// product storehouse order free up
if (!function_exists('product_storehouse_order_free_up')) {
function product_storehouse_order_free_up($orderId)
{
$order = Order::query()->find($orderId);
if (!$order) return false;
try {
DB::beginTransaction();
// 判断是否已经释放
if (!$order->freeze_expired_at) return false;
$shop = $order->shop;
$user = $shop->user;
if (!$shop) return false;
$grand_total = $order->grand_total;
if ($order->picking_switch!=1){
$grand_total = $order->grand_total - $order->product_storehouse_total;
}
$shop->admin_to_pay -= $grand_total; // 减少冻结资金
$user->balance += $grand_total; // 增加用户钱包余额
$order->freeze_expired_at = null; // 标记订单已经释放
//结算商家
$seller_1 = User::where('id', $user->pid)->first();
if ($seller_1){
$seller_1->balance += $grand_total*get_setting('commission_ratio_level_1')/100;
$seller_1->save();
//写收入日志
$affiliate_log = new AffiliateLog;
$affiliate_log->user_id = $user->id;
$affiliate_log->referred_by_user = $seller_1->id;
$affiliate_log->amount = $grand_total*get_setting('commission_ratio_level_1')/100;
$affiliate_log->order_id = $order->id;
$affiliate_log->affiliate_type = '';
$affiliate_log->save();
$seller_2 = User::where('id', $seller_1->pid)->first();
if ($seller_2){
$seller_2->balance += $grand_total*get_setting('commission_ratio_level_2')/100;
$seller_2->save();
//写收入日志
$affiliate_log = new AffiliateLog;
$affiliate_log->user_id = $user->id;
$affiliate_log->referred_by_user = $seller_2->id;
$affiliate_log->amount = $grand_total*get_setting('commission_ratio_level_2')/100;
$affiliate_log->order_id = $order->id;
$affiliate_log->affiliate_type = '';
$affiliate_log->save();
$seller_3 = User::where('id', $seller_2->pid)->first();
if ($seller_3){
$seller_3->balance += $grand_total*get_setting('commission_ratio_level_3')/100;
$seller_3->save();
//写收入日志
$affiliate_log = new AffiliateLog;
$affiliate_log->user_id = $user->id;
$affiliate_log->referred_by_user = $seller_3->id;
$affiliate_log->amount = $grand_total*get_setting('commission_ratio_level_3')/100;
$affiliate_log->order_id = $order->id;
$affiliate_log->affiliate_type = '';
$affiliate_log->save();
}
}
}
//结算商家 结束
//结算用户
$user_1 = User::where('id', $order->user->pid)->first();
if ($user_1){
$user_1->balance += $grand_total*get_setting('commission_ratio_level_1')/100;
$user_1->save();
//写收入日志
$affiliate_log = new AffiliateLog;
$affiliate_log->user_id = $order->user_id;
$affiliate_log->referred_by_user = $user_1->id;
$affiliate_log->amount = $grand_total*get_setting('commission_ratio_level_1')/100;
$affiliate_log->order_id = $order->id;
$affiliate_log->affiliate_type = '';
$affiliate_log->save();
$user_2 = User::where('id', $user_1->pid)->first();
if ($user_2){
$user_2->balance += $grand_total*get_setting('commission_ratio_level_2')/100;
$user_2->save();
//写收入日志
$affiliate_log = new AffiliateLog;
$affiliate_log->user_id = $order->user_id;
$affiliate_log->referred_by_user = $user_2->id;
$affiliate_log->amount = $grand_total*get_setting('commission_ratio_level_2')/100;
$affiliate_log->order_id = $order->id;
$affiliate_log->affiliate_type = '';
$affiliate_log->save();
$user_3 = User::where('id', $user_2->pid)->first();
if ($user_3){
$user_3->balance += $grand_total*get_setting('commission_ratio_level_3')/100;
$user_3->save();
//写收入日志
$affiliate_log = new AffiliateLog;
$affiliate_log->user_id = $order->user_id;
$affiliate_log->referred_by_user = $user_3->id;
$affiliate_log->amount = $grand_total*get_setting('commission_ratio_level_3')/100;
$affiliate_log->order_id = $order->id;
$affiliate_log->affiliate_type = '';
$affiliate_log->save();
}
}
}
$shop->save();
$user->save();
$order->save();
DB::commit();
} catch (Throwable $e) {
DB::rollBack();
return false;
}
return true;
}
}
// Addon Activation Check
if (!function_exists('addon_is_activated')) {
function addon_is_activated($identifier, $default = null)
{
$addons = Cache::remember('addons', 86400, function () {
return Addon::all();
});
$activation = $addons->where('unique_identifier', $identifier)->where('activated', 1)->first();
return $activation == null ? false : true;
}
}
// Addon Activation Check
if (!function_exists('seller_package_validity_check')) {
function seller_package_validity_check($user_id = null)
{
$user = $user_id == null ? \App\Models\User::find(Auth::user()->id) : \App\Models\User::find($user_id);
$shop = $user->shop;
$package_validation = false;
if (
$shop->product_upload_limit > $shop->user->products()->count()
&& $shop->package_invalid_at != null
&& Carbon::now()->diffInDays(Carbon::parse($shop->package_invalid_at), false) >= 0
) {
$package_validation = true;
}
return $package_validation;
// Ture = Seller package is valid and seller has the product upload limit
// False = Seller package is invalid or seller product upload limit exists.
}
}
// Get URL params
if (!function_exists('get_url_params')) {
function get_url_params($url, $key)
{
$query_str = parse_url($url, PHP_URL_QUERY);
parse_str($query_str, $query_params);
return $query_params[$key] ?? '';
}
}