12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292 |
- <?php
- use App\Http\Controllers\ClubPointController;
- use App\Http\Controllers\AffiliateController;
- use App\Http\Controllers\CommissionController;
- use App\Models\AffiliateLog;
- use App\Models\Currency;
- use App\Models\BusinessSetting;
- use App\Models\Order;
- use App\Models\ProductStock;
- use App\Models\Address;
- use App\Models\CustomerPackage;
- use App\Models\Upload;
- use App\Models\Translation;
- use App\Models\City;
- use App\Utility\CategoryUtility;
- use App\Models\Wallet;
- use App\Models\CombinedOrder;
- use App\Models\User;
- use App\Models\Addon;
- use App\Models\Cart;
- use App\Models\Coupon;
- use App\Models\CouponUsage;
- use App\Models\Product;
- use App\Models\Shop;
- use App\Utility\SendSMSUtility;
- use App\Utility\NotificationUtility;
- use Carbon\Carbon;
- //sensSMS function for OTP
- if (!function_exists('sendSMS')) {
- function sendSMS($to, $from, $text, $template_id)
- {
- return SendSMSUtility::sendSMS($to, $from, $text, $template_id);
- }
- }
- //highlights the selected navigation on admin panel
- if (!function_exists('areActiveRoutes')) {
- function areActiveRoutes(array $routes, $output = "active")
- {
- foreach ($routes as $route) {
- if (Route::currentRouteName() == $route) return $output;
- }
- }
- }
- //highlights the selected navigation on frontend
- if (!function_exists('areActiveRoutesHome')) {
- function areActiveRoutesHome(array $routes, $output = "active")
- {
- foreach ($routes as $route) {
- if (Route::currentRouteName() == $route) return $output;
- }
- }
- }
- //highlights the selected navigation on frontend
- if (!function_exists('default_language')) {
- function default_language()
- {
- return env("DEFAULT_LANGUAGE");
- }
- }
- /**
- * Save JSON File
- * @return Response
- */
- if (!function_exists('convert_to_usd')) {
- function convert_to_usd($amount)
- {
- $currency = Currency::find(get_setting('system_default_currency'));
- return (floatval($amount) / floatval($currency->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 = "<i class = 'las la-star active'></i>";
- $halfStar = "<i class = 'las la-star half'></i>";
- $emptyStar = "<i class = 'las la-star'></i>";
- $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] ?? '';
- }
- }
|