Helpers.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. <?php
  2. use App\Http\Controllers\ClubPointController;
  3. use App\Http\Controllers\AffiliateController;
  4. use App\Http\Controllers\CommissionController;
  5. use App\Models\AffiliateLog;
  6. use App\Models\Currency;
  7. use App\Models\BusinessSetting;
  8. use App\Models\Order;
  9. use App\Models\ProductStock;
  10. use App\Models\Address;
  11. use App\Models\CustomerPackage;
  12. use App\Models\Upload;
  13. use App\Models\Translation;
  14. use App\Models\City;
  15. use App\Utility\CategoryUtility;
  16. use App\Models\Wallet;
  17. use App\Models\CombinedOrder;
  18. use App\Models\User;
  19. use App\Models\Addon;
  20. use App\Models\Cart;
  21. use App\Models\Coupon;
  22. use App\Models\CouponUsage;
  23. use App\Models\Product;
  24. use App\Models\Shop;
  25. use App\Utility\SendSMSUtility;
  26. use App\Utility\NotificationUtility;
  27. use Carbon\Carbon;
  28. //sensSMS function for OTP
  29. if (!function_exists('sendSMS')) {
  30. function sendSMS($to, $from, $text, $template_id)
  31. {
  32. return SendSMSUtility::sendSMS($to, $from, $text, $template_id);
  33. }
  34. }
  35. //highlights the selected navigation on admin panel
  36. if (!function_exists('areActiveRoutes')) {
  37. function areActiveRoutes(array $routes, $output = "active")
  38. {
  39. foreach ($routes as $route) {
  40. if (Route::currentRouteName() == $route) return $output;
  41. }
  42. }
  43. }
  44. //highlights the selected navigation on frontend
  45. if (!function_exists('areActiveRoutesHome')) {
  46. function areActiveRoutesHome(array $routes, $output = "active")
  47. {
  48. foreach ($routes as $route) {
  49. if (Route::currentRouteName() == $route) return $output;
  50. }
  51. }
  52. }
  53. //highlights the selected navigation on frontend
  54. if (!function_exists('default_language')) {
  55. function default_language()
  56. {
  57. return env("DEFAULT_LANGUAGE");
  58. }
  59. }
  60. /**
  61. * Save JSON File
  62. * @return Response
  63. */
  64. if (!function_exists('convert_to_usd')) {
  65. function convert_to_usd($amount)
  66. {
  67. $currency = Currency::find(get_setting('system_default_currency'));
  68. return (floatval($amount) / floatval($currency->exchange_rate)) * Currency::where('code', 'USD')->first()->exchange_rate;
  69. }
  70. }
  71. if (!function_exists('convert_to_kes')) {
  72. function convert_to_kes($amount)
  73. {
  74. $currency = Currency::find(get_setting('system_default_currency'));
  75. return (floatval($amount) / floatval($currency->exchange_rate)) * Currency::where('code', 'KES')->first()->exchange_rate;
  76. }
  77. }
  78. //filter products based on vendor activation system
  79. if (!function_exists('filter_products')) {
  80. function filter_products($products)
  81. {
  82. // dd(get_setting('vendor_system_activation'));
  83. $verified_sellers = verified_sellers_id();
  84. if (get_setting('vendor_system_activation') == 1) {
  85. 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) {
  86. $p->where('p.added_by', 'admin')->orWhere(function ($q) use ($verified_sellers) {
  87. // $q->join($verified_sellers ." as sp","sp.user_id","=","p.user_id");
  88. $q->whereIn('p.user_id',$verified_sellers);
  89. });
  90. });
  91. } else {
  92. return $products->where('p.published', '1')->where('p.auction_product', 0)->where('p.added_by', 'admin');
  93. }
  94. }
  95. }
  96. //cache products based on category
  97. if (!function_exists('get_cached_products')) {
  98. function get_cached_products($category_id = null)
  99. {
  100. $products = \App\Models\Product::where('published', 1)->where('approved', '1')->where('auction_product', 0);
  101. $verified_sellers = verified_sellers_id();
  102. // dd($verified_sellers);
  103. // exit();
  104. if (get_setting('vendor_system_activation') == 1) {
  105. $products = $products->where(function ($p) use ($verified_sellers) {
  106. $p->where('p.added_by', 'admin')->orWhere(function ($q) use ($verified_sellers) {
  107. $q->whereIn('p.user_id',$verified_sellers);
  108. });
  109. });
  110. } else {
  111. $products = $products->where('added_by', 'admin');
  112. }
  113. if ($category_id != null) {
  114. return Cache::remember('products-category-' . $category_id, 86400, function () use ($category_id, $products) {
  115. $category_ids = CategoryUtility::children_ids($category_id);
  116. $category_ids[] = $category_id;
  117. return $products->whereIn('category_id', $category_ids)->latest()->take(12)->get();
  118. });
  119. } else {
  120. return Cache::remember('products', 86400, function () use ($products) {
  121. return $products->latest()->take(12)->get();
  122. });
  123. }
  124. }
  125. }
  126. if (!function_exists('verified_sellers_id')) {
  127. function verified_sellers_id()
  128. {
  129. // $builder=DB::table("shops","sp")->where('sp.verification_status', 1)->select(['sp.user_id']);
  130. // $bindings=$builder->getBindings();
  131. // $sql = str_replace('?', '%s', $builder->toSql());
  132. // $sql = sprintf($sql, ...$bindings);
  133. // return $sql;
  134. return Cache::rememberForever('verified_sellers_id', function () {
  135. return App\Models\Shop::where('verification_status', 1)->pluck('user_id')->toArray();
  136. });
  137. }
  138. }
  139. if (!function_exists('get_system_default_currency')) {
  140. function get_system_default_currency()
  141. {
  142. return Cache::remember('system_default_currency', 86400, function () {
  143. return Currency::findOrFail(get_setting('system_default_currency'));
  144. });
  145. }
  146. }
  147. //converts currency to home default currency
  148. if (!function_exists('convert_price')) {
  149. function convert_price($price)
  150. {
  151. if (Session::has('currency_code') && (Session::get('currency_code') != get_system_default_currency()->code)) {
  152. $price = floatval($price) / floatval(get_system_default_currency()->exchange_rate);
  153. $price = floatval($price) * floatval(Session::get('currency_exchange_rate'));
  154. }
  155. return $price;
  156. }
  157. }
  158. //gets currency symbol
  159. if (!function_exists('currency_symbol')) {
  160. function currency_symbol()
  161. {
  162. if (Session::has('currency_symbol')) {
  163. return Session::get('currency_symbol');
  164. }
  165. return get_system_default_currency()->symbol;
  166. }
  167. }
  168. //formats currency
  169. if (!function_exists('format_price')) {
  170. function format_price($price)
  171. {
  172. if (get_setting('decimal_separator') == 1) {
  173. $fomated_price = number_format($price, get_setting('no_of_decimals'));
  174. } else {
  175. $fomated_price = number_format($price, get_setting('no_of_decimals'), ',', '.');
  176. }
  177. if (get_setting('symbol_format') == 1) {
  178. return currency_symbol() . $fomated_price;
  179. } else if (get_setting('symbol_format') == 3) {
  180. return currency_symbol() . ' ' . $fomated_price;
  181. } else if (get_setting('symbol_format') == 4) {
  182. return $fomated_price . ' ' . currency_symbol();
  183. }
  184. return $fomated_price . currency_symbol();
  185. }
  186. }
  187. //formats price to home default price with convertion
  188. if (!function_exists('single_price')) {
  189. function single_price($price)
  190. {
  191. return format_price(convert_price($price));
  192. }
  193. }
  194. if (!function_exists('discount_in_percentage')) {
  195. function discount_in_percentage($product)
  196. {
  197. $base = home_base_price($product, false);
  198. $reduced = home_discounted_base_price($product, false);
  199. $discount = $base - $reduced;
  200. $dp = ($discount * 100) / ($base > 0 ? $base : 1);
  201. return round($dp);
  202. }
  203. }
  204. //Shows Price on page based on carts
  205. if (!function_exists('cart_product_price')) {
  206. function cart_product_price($cart_product, $product, $formatted = true, $tax = true)
  207. {
  208. $str = '';
  209. if (isset($cart_product['variation'])){
  210. if ($cart_product['variation'] != null) {
  211. $str = $cart_product['variation'];
  212. }
  213. }
  214. $price = 0;
  215. $product_stock = $product->stocks->where('variant', $str)->first();
  216. if ($product_stock) {
  217. $price = $product_stock->price;
  218. }
  219. //discount calculation
  220. $discount_applicable = false;
  221. if ($product->discount_start_date == null) {
  222. $discount_applicable = true;
  223. } elseif (strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
  224. strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date) {
  225. $discount_applicable = true;
  226. }
  227. if ($discount_applicable) {
  228. if ($product->discount_type == 'percent') {
  229. $price -= ($price * $product->discount) / 100;
  230. } elseif ($product->discount_type == 'amount') {
  231. $price -= $product->discount;
  232. }
  233. }
  234. //calculation of taxes
  235. if ($tax) {
  236. $taxAmount = 0;
  237. foreach ($product->taxes as $product_tax) {
  238. if ($product_tax->tax_type == 'percent') {
  239. $taxAmount += ($price * $product_tax->tax) / 100;
  240. } elseif ($product_tax->tax_type == 'amount') {
  241. $taxAmount += $product_tax->tax;
  242. }
  243. }
  244. $price += $taxAmount;
  245. }
  246. if ($formatted) {
  247. return format_price(convert_price($price));
  248. } else {
  249. return $price;
  250. }
  251. }
  252. }
  253. if (!function_exists('cart_product_tax')) {
  254. function cart_product_tax($cart_product, $product, $formatted = true)
  255. {
  256. $str = '';
  257. if ($cart_product['variation'] != null) {
  258. $str = $cart_product['variation'];
  259. }
  260. $product_stock = $product->stocks->where('variant', $str)->first();
  261. $price = $product_stock->price;
  262. //discount calculation
  263. $discount_applicable = false;
  264. if ($product->discount_start_date == null) {
  265. $discount_applicable = true;
  266. } elseif (strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
  267. strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date) {
  268. $discount_applicable = true;
  269. }
  270. if ($discount_applicable) {
  271. if ($product->discount_type == 'percent') {
  272. $price -= ($price * $product->discount) / 100;
  273. } elseif ($product->discount_type == 'amount') {
  274. $price -= $product->discount;
  275. }
  276. }
  277. //calculation of taxes
  278. $tax = 0;
  279. foreach ($product->taxes as $product_tax) {
  280. if ($product_tax->tax_type == 'percent') {
  281. $tax += ($price * $product_tax->tax) / 100;
  282. } elseif ($product_tax->tax_type == 'amount') {
  283. $tax += $product_tax->tax;
  284. }
  285. }
  286. if ($formatted) {
  287. return format_price(convert_price($tax));
  288. } else {
  289. return $tax;
  290. }
  291. }
  292. }
  293. if (!function_exists('cart_product_discount')) {
  294. function cart_product_discount($cart_product, $product, $formatted = false)
  295. {
  296. $str = '';
  297. if ($cart_product['variation'] != null) {
  298. $str = $cart_product['variation'];
  299. }
  300. $product_stock = $product->stocks->where('variant', $str)->first();
  301. $price = $product_stock->price;
  302. //discount calculation
  303. $discount_applicable = false;
  304. $discount = 0;
  305. if ($product->discount_start_date == null) {
  306. $discount_applicable = true;
  307. } elseif (strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
  308. strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date) {
  309. $discount_applicable = true;
  310. }
  311. if ($discount_applicable) {
  312. if ($product->discount_type == 'percent') {
  313. $discount = ($price * $product->discount) / 100;
  314. } elseif ($product->discount_type == 'amount') {
  315. $discount = $product->discount;
  316. }
  317. }
  318. if ($formatted) {
  319. return format_price(convert_price($discount));
  320. } else {
  321. return $discount;
  322. }
  323. }
  324. }
  325. // all discount
  326. if (!function_exists('carts_product_discount')) {
  327. function carts_product_discount($cart_products, $formatted = false)
  328. {
  329. $discount = 0;
  330. foreach ($cart_products as $key => $cart_product) {
  331. $str = '';
  332. $product = \App\Models\Product::find($cart_product['product_id']);
  333. if ($cart_product['variation'] != null) {
  334. $str = $cart_product['variation'];
  335. }
  336. $product_stock = $product->stocks->where('variant', $str)->first();
  337. $price = $product_stock->price;
  338. //discount calculation
  339. $discount_applicable = false;
  340. if ($product->discount_start_date == null) {
  341. $discount_applicable = true;
  342. } elseif (strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
  343. strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date) {
  344. $discount_applicable = true;
  345. }
  346. if ($discount_applicable) {
  347. if ($product->discount_type == 'percent') {
  348. $discount += ($price * $product->discount) / 100;
  349. } elseif ($product->discount_type == 'amount') {
  350. $discount += $product->discount;
  351. }
  352. }
  353. }
  354. if ($formatted) {
  355. return format_price(convert_price($discount));
  356. } else {
  357. return $discount;
  358. }
  359. }
  360. }
  361. if (!function_exists('carts_coupon_discount')) {
  362. function carts_coupon_discount($code, $formatted = false)
  363. {
  364. $coupon = Coupon::where('code', $code)->first();
  365. $coupon_discount = 0;
  366. if ($coupon != null) {
  367. if (strtotime(date('d-m-Y')) >= $coupon->start_date && strtotime(date('d-m-Y')) <= $coupon->end_date) {
  368. if (CouponUsage::where('user_id', Auth::user()->id)->where('coupon_id', $coupon->id)->first() == null) {
  369. $coupon_details = json_decode($coupon->details);
  370. $carts = Cart::where('user_id', Auth::user()->id)
  371. ->where('owner_id', $coupon->user_id)
  372. ->get();
  373. if ($coupon->type == 'cart_base') {
  374. $subtotal = 0;
  375. $tax = 0;
  376. $shipping = 0;
  377. foreach ($carts as $key => $cartItem) {
  378. $product = Product::find($cartItem['product_id']);
  379. $subtotal += cart_product_price($cartItem, $product, false, false) * $cartItem['quantity'];
  380. $tax += cart_product_tax($cartItem, $product, false) * $cartItem['quantity'];
  381. $shipping += $cartItem['shipping_cost'];
  382. }
  383. $sum = $subtotal + $tax + $shipping;
  384. if ($sum >= $coupon_details->min_buy) {
  385. if ($coupon->discount_type == 'percent') {
  386. $coupon_discount = ($sum * $coupon->discount) / 100;
  387. if ($coupon_discount > $coupon_details->max_discount) {
  388. $coupon_discount = $coupon_details->max_discount;
  389. }
  390. } elseif ($coupon->discount_type == 'amount') {
  391. $coupon_discount = $coupon->discount;
  392. }
  393. }
  394. } elseif ($coupon->type == 'product_base') {
  395. foreach ($carts as $key => $cartItem) {
  396. $product = Product::find($cartItem['product_id']);
  397. foreach ($coupon_details as $key => $coupon_detail) {
  398. if ($coupon_detail->product_id == $cartItem['product_id']) {
  399. if ($coupon->discount_type == 'percent') {
  400. $coupon_discount += (cart_product_price($cartItem, $product, false, false) * $coupon->discount / 100) * $cartItem['quantity'];
  401. } elseif ($coupon->discount_type == 'amount') {
  402. $coupon_discount += $coupon->discount * $cartItem['quantity'];
  403. }
  404. }
  405. }
  406. }
  407. }
  408. }
  409. }
  410. if ($coupon_discount > 0) {
  411. Cart::where('user_id', Auth::user()->id)
  412. ->where('owner_id', $coupon->user_id)
  413. ->update(
  414. [
  415. 'discount' => $coupon_discount / count($carts),
  416. ]
  417. );
  418. } else {
  419. Cart::where('user_id', Auth::user()->id)
  420. ->where('owner_id', $coupon->user_id)
  421. ->update(
  422. [
  423. 'discount' => 0,
  424. 'coupon_code' => null,
  425. ]
  426. );
  427. }
  428. }
  429. if ($formatted) {
  430. return format_price(convert_price($coupon_discount));
  431. } else {
  432. return $coupon_discount;
  433. }
  434. }
  435. }
  436. //Shows Price on page based on low to high
  437. if (!function_exists('home_price')) {
  438. function home_price($product, $formatted = true)
  439. {
  440. $lowest_price = $product->unit_price;
  441. $highest_price = $product->unit_price;
  442. if ($product->variant_product) {
  443. foreach ($product->stocks as $key => $stock) {
  444. if ($lowest_price > $stock->price) {
  445. $lowest_price = $stock->price;
  446. }
  447. if ($highest_price < $stock->price) {
  448. $highest_price = $stock->price;
  449. }
  450. }
  451. }
  452. foreach ($product->taxes as $product_tax) {
  453. if ($product_tax->tax_type == 'percent') {
  454. $lowest_price += ($lowest_price * $product_tax->tax) / 100;
  455. $highest_price += ($highest_price * $product_tax->tax) / 100;
  456. } elseif ($product_tax->tax_type == 'amount') {
  457. $lowest_price += $product_tax->tax;
  458. $highest_price += $product_tax->tax;
  459. }
  460. }
  461. if ($formatted) {
  462. if ($lowest_price == $highest_price) {
  463. return format_price(convert_price($lowest_price));
  464. } else {
  465. return format_price(convert_price($lowest_price)) . ' - ' . format_price(convert_price($highest_price));
  466. }
  467. } else {
  468. return $lowest_price . ' - ' . $highest_price;
  469. }
  470. }
  471. }
  472. //Shows Price on page based on low to high with discount
  473. if (!function_exists('home_discounted_price')) {
  474. function home_discounted_price($product, $formatted = true)
  475. {
  476. $lowest_price = $product->unit_price;
  477. $highest_price = $product->unit_price;
  478. if ($product->variant_product) {
  479. foreach ($product->stocks as $key => $stock) {
  480. if ($lowest_price > $stock->price) {
  481. $lowest_price = $stock->price;
  482. }
  483. if ($highest_price < $stock->price) {
  484. $highest_price = $stock->price;
  485. }
  486. }
  487. }
  488. $discount_applicable = false;
  489. if ($product->discount_start_date == null) {
  490. $discount_applicable = true;
  491. } elseif (
  492. strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
  493. strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date
  494. ) {
  495. $discount_applicable = true;
  496. }
  497. if ($discount_applicable) {
  498. if ($product->discount_type == 'percent') {
  499. $lowest_price -= ($lowest_price * $product->discount) / 100;
  500. $highest_price -= ($highest_price * $product->discount) / 100;
  501. } elseif ($product->discount_type == 'amount') {
  502. $lowest_price -= $product->discount;
  503. $highest_price -= $product->discount;
  504. }
  505. }
  506. foreach ($product->taxes as $product_tax) {
  507. if ($product_tax->tax_type == 'percent') {
  508. $lowest_price += ($lowest_price * $product_tax->tax) / 100;
  509. $highest_price += ($highest_price * $product_tax->tax) / 100;
  510. } elseif ($product_tax->tax_type == 'amount') {
  511. $lowest_price += $product_tax->tax;
  512. $highest_price += $product_tax->tax;
  513. }
  514. }
  515. if ($formatted) {
  516. if ($lowest_price == $highest_price) {
  517. return format_price(convert_price($lowest_price));
  518. } else {
  519. return format_price(convert_price($lowest_price)) . ' - ' . format_price(convert_price($highest_price));
  520. }
  521. } else {
  522. return $lowest_price . ' - ' . $highest_price;
  523. }
  524. }
  525. }
  526. //Shows Base Price
  527. if (!function_exists('home_base_price_by_stock_id')) {
  528. function home_base_price_by_stock_id($id)
  529. {
  530. $product_stock = ProductStock::findOrFail($id);
  531. $price = $product_stock->price;
  532. $tax = 0;
  533. foreach ($product_stock->product->taxes as $product_tax) {
  534. if ($product_tax->tax_type == 'percent') {
  535. $tax += ($price * $product_tax->tax) / 100;
  536. } elseif ($product_tax->tax_type == 'amount') {
  537. $tax += $product_tax->tax;
  538. }
  539. }
  540. $price += $tax;
  541. return format_price(convert_price($price));
  542. }
  543. }
  544. if (!function_exists('home_base_price')) {
  545. function home_base_price($product, $formatted = true)
  546. {
  547. $price = $product->unit_price;
  548. $tax = 0;
  549. foreach ($product->taxes as $product_tax) {
  550. if ($product_tax->tax_type == 'percent') {
  551. $tax += ($price * $product_tax->tax) / 100;
  552. } elseif ($product_tax->tax_type == 'amount') {
  553. $tax += $product_tax->tax;
  554. }
  555. }
  556. $price += $tax;
  557. return $formatted ? format_price(convert_price($price)) : $price;
  558. }
  559. }
  560. //Shows Base Price with discount
  561. if (!function_exists('home_discounted_base_price_by_stock_id')) {
  562. function home_discounted_base_price_by_stock_id($id)
  563. {
  564. $product_stock = ProductStock::findOrFail($id);
  565. $product = $product_stock->product;
  566. $price = $product_stock->price;
  567. $tax = 0;
  568. $discount_applicable = false;
  569. if ($product->discount_start_date == null) {
  570. $discount_applicable = true;
  571. } elseif (
  572. strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
  573. strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date
  574. ) {
  575. $discount_applicable = true;
  576. }
  577. if ($discount_applicable) {
  578. if ($product->discount_type == 'percent') {
  579. $price -= ($price * $product->discount) / 100;
  580. } elseif ($product->discount_type == 'amount') {
  581. $price -= $product->discount;
  582. }
  583. }
  584. foreach ($product->taxes as $product_tax) {
  585. if ($product_tax->tax_type == 'percent') {
  586. $tax += ($price * $product_tax->tax) / 100;
  587. } elseif ($product_tax->tax_type == 'amount') {
  588. $tax += $product_tax->tax;
  589. }
  590. }
  591. $price += $tax;
  592. return format_price(convert_price($price));
  593. }
  594. }
  595. //Shows Base Price with discount
  596. if (!function_exists('home_discounted_base_price')) {
  597. function home_discounted_base_price($product, $formatted = true)
  598. {
  599. $price = $product->unit_price;
  600. $tax = 0;
  601. $discount_applicable = false;
  602. if ($product->discount_start_date == null) {
  603. $discount_applicable = true;
  604. } elseif (
  605. strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
  606. strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date
  607. ) {
  608. $discount_applicable = true;
  609. }
  610. if ($discount_applicable) {
  611. if ($product->discount_type == 'percent') {
  612. $price -= ($price * $product->discount) / 100;
  613. } elseif ($product->discount_type == 'amount') {
  614. $price -= $product->discount;
  615. }
  616. }
  617. foreach ($product->taxes as $product_tax) {
  618. if ($product_tax->tax_type == 'percent') {
  619. $tax += ($price * $product_tax->tax) / 100;
  620. } elseif ($product_tax->tax_type == 'amount') {
  621. $tax += $product_tax->tax;
  622. }
  623. }
  624. $price += $tax;
  625. return $formatted ? format_price(convert_price($price)) : $price;
  626. }
  627. }
  628. if (!function_exists('renderStarRating')) {
  629. function renderStarRating($rating, $maxRating = 5)
  630. {
  631. $fullStar = "<i class = 'las la-star active'></i>";
  632. $halfStar = "<i class = 'las la-star half'></i>";
  633. $emptyStar = "<i class = 'las la-star'></i>";
  634. $rating = $rating <= $maxRating ? $rating : $maxRating;
  635. $fullStarCount = (int)$rating;
  636. $halfStarCount = ceil($rating) - $fullStarCount;
  637. $emptyStarCount = $maxRating - $fullStarCount - $halfStarCount;
  638. $html = str_repeat($fullStar, $fullStarCount);
  639. $html .= str_repeat($halfStar, $halfStarCount);
  640. $html .= str_repeat($emptyStar, $emptyStarCount);
  641. echo $html;
  642. }
  643. }
  644. function translate($key, $lang = null, $addslashes = false)
  645. {
  646. if ($lang == null) {
  647. $lang = App::getLocale();
  648. }
  649. $lang_key = preg_replace('/[^A-Za-z0-9\_]/', '', str_replace(' ', '_', strtolower($key)));
  650. $translations_en = Cache::rememberForever('translations-en', function () {
  651. return Translation::where('lang', 'en')->pluck('lang_value', 'lang_key')->toArray();
  652. });
  653. if (!isset($translations_en[$lang_key])) {
  654. $translation_def = new Translation;
  655. $translation_def->lang = 'en';
  656. $translation_def->lang_key = $lang_key;
  657. $translation_def->lang_value = str_replace(array("\r", "\n", "\r\n"), "", $key);
  658. $translation_def->save();
  659. Cache::forget('translations-en');
  660. }
  661. // return user session lang
  662. $translation_locale = Cache::rememberForever("translations-{$lang}", function () use ($lang) {
  663. return Translation::where('lang', $lang)->pluck('lang_value', 'lang_key')->toArray();
  664. });
  665. if (isset($translation_locale[$lang_key])) {
  666. return $addslashes ? addslashes(trim($translation_locale[$lang_key])) : trim($translation_locale[$lang_key]);
  667. }
  668. // return default lang if session lang not found
  669. $translations_default = Cache::rememberForever('translations-' . env('DEFAULT_LANGUAGE', 'en'), function () {
  670. return Translation::where('lang', env('DEFAULT_LANGUAGE', 'en'))->pluck('lang_value', 'lang_key')->toArray();
  671. });
  672. if (isset($translations_default[$lang_key])) {
  673. return $addslashes ? addslashes(trim($translations_default[$lang_key])) : trim($translations_default[$lang_key]);
  674. }
  675. // fallback to en lang
  676. if (!isset($translations_en[$lang_key])) {
  677. return trim($key);
  678. }
  679. return $addslashes ? addslashes(trim($translations_en[$lang_key])) : trim($translations_en[$lang_key]);
  680. }
  681. function remove_invalid_charcaters($str)
  682. {
  683. $str = str_ireplace(array("\\"), '', $str);
  684. return str_ireplace(array('"'), '\"', $str);
  685. }
  686. function getShippingCost($carts, $index)
  687. {
  688. $admin_products = array();
  689. $seller_products = array();
  690. $cartItem = $carts[$index];
  691. $product = Product::find($cartItem['product_id']);
  692. if ($product->digital == 1) {
  693. return 0;
  694. }
  695. foreach ($carts as $key => $cart_item) {
  696. $item_product = Product::find($cart_item['product_id']);
  697. if ($item_product->added_by == 'admin') {
  698. array_push($admin_products, $cart_item['product_id']);
  699. } else {
  700. $product_ids = array();
  701. if (isset($seller_products[$item_product->user_id])) {
  702. $product_ids = $seller_products[$item_product->user_id];
  703. }
  704. array_push($product_ids, $cart_item['product_id']);
  705. $seller_products[$item_product->user_id] = $product_ids;
  706. }
  707. }
  708. if (get_setting('shipping_type') == 'flat_rate') {
  709. return get_setting('flat_rate_shipping_cost') / count($carts);
  710. } elseif (get_setting('shipping_type') == 'seller_wise_shipping') {
  711. if ($product->added_by == 'admin') {
  712. return get_setting('shipping_cost_admin') / count($admin_products);
  713. } else {
  714. return Shop::where('user_id', $product->user_id)->first()->shipping_cost / count($seller_products[$product->user_id]);
  715. }
  716. } elseif (get_setting('shipping_type') == 'area_wise_shipping') {
  717. $shipping_info = Address::where('id', $carts[0]['address_id'])->first();
  718. $city = City::where('id', $shipping_info->city_id)->first();
  719. if ($city != null) {
  720. if ($product->added_by == 'admin') {
  721. return $city->cost / count($admin_products);
  722. } else {
  723. return $city->cost / count($seller_products[$product->user_id]);
  724. }
  725. }
  726. return 0;
  727. } else {
  728. if ($product->is_quantity_multiplied && get_setting('shipping_type') == 'product_wise_shipping') {
  729. return $product->shipping_cost * $cartItem['quantity'];
  730. }
  731. return $product->shipping_cost;
  732. }
  733. }
  734. function timezones()
  735. {
  736. return Timezones::timezonesToArray();
  737. }
  738. if (!function_exists('app_timezone')) {
  739. function app_timezone()
  740. {
  741. return config('app.timezone');
  742. }
  743. }
  744. //return file uploaded via uploader
  745. if (!function_exists('uploaded_asset')) {
  746. function uploaded_asset($id)
  747. {
  748. if (($asset = \App\Models\Upload::find($id)) != null) {
  749. return $asset->external_link == null ? my_asset($asset->file_name) : $asset->external_link;
  750. }
  751. return null;
  752. }
  753. }
  754. if (!function_exists('my_asset')) {
  755. /**
  756. * Generate an asset path for the application.
  757. *
  758. * @param string $path
  759. * @param bool|null $secure
  760. * @return string
  761. */
  762. function my_asset($path, $secure = null)
  763. {
  764. if( strpos($path,'http') !== false ) return $path;
  765. if (env('FILESYSTEM_DRIVER') == 's3') {
  766. return Storage::disk('s3')->url($path);
  767. } else {
  768. return app('url')->asset('public/' . $path, $secure);
  769. }
  770. }
  771. }
  772. if (!function_exists('static_asset')) {
  773. /**
  774. * Generate an asset path for the application.
  775. *
  776. * @param string $path
  777. * @param bool|null $secure
  778. * @return string
  779. */
  780. function static_asset($path, $secure = null)
  781. {
  782. return app('url')->asset('public/' . $path, $secure);
  783. }
  784. }
  785. // if (!function_exists('isHttps')) {
  786. // function isHttps()
  787. // {
  788. // return !empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS']);
  789. // }
  790. // }
  791. if (!function_exists('getBaseURL')) {
  792. function getBaseURL()
  793. {
  794. $root = '//' . $_SERVER['HTTP_HOST'];
  795. $root .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  796. return $root;
  797. }
  798. }
  799. if (!function_exists('getFileBaseURL')) {
  800. function getFileBaseURL()
  801. {
  802. if (env('FILESYSTEM_DRIVER') == 's3') {
  803. return env('AWS_URL') . '/';
  804. } else {
  805. return getBaseURL() . 'public/';
  806. }
  807. }
  808. }
  809. if (!function_exists('isUnique')) {
  810. /**
  811. * Generate an asset path for the application.
  812. *
  813. * @param string $path
  814. * @param bool|null $secure
  815. * @return string
  816. */
  817. function isUnique($email)
  818. {
  819. $user = \App\Models\User::where('email', $email)->first();
  820. if ($user == null) {
  821. return '1'; // $user = null means we did not get any match with the email provided by the user inside the database
  822. } else {
  823. return '0';
  824. }
  825. }
  826. }
  827. if (!function_exists('get_setting')) {
  828. function get_setting($key, $default = null, $lang = false)
  829. {
  830. $settings = Cache::remember('business_settings', 86400, function () {
  831. return BusinessSetting::all();
  832. });
  833. if ($lang == false) {
  834. $setting = $settings->where('type', $key)->first();
  835. } else {
  836. $setting = $settings->where('type', $key)->where('lang', $lang)->first();
  837. $setting = !$setting ? $settings->where('type', $key)->first() : $setting;
  838. }
  839. return $setting == null ? $default : $setting->value;
  840. }
  841. }
  842. function hex2rgba($color, $opacity = false)
  843. {
  844. return Colorcodeconverter::convertHexToRgba($color, $opacity);
  845. }
  846. if (!function_exists('isAdmin')) {
  847. function isAdmin()
  848. {
  849. if (Auth::check() && (Auth::user()->user_type == 'admin' || Auth::user()->user_type == 'staff')) {
  850. return true;
  851. }
  852. return false;
  853. }
  854. }
  855. if (!function_exists('isSeller')) {
  856. function isSeller()
  857. {
  858. if (Auth::check() && Auth::user()->user_type == 'seller') {
  859. return true;
  860. }
  861. return false;
  862. }
  863. }
  864. if (!function_exists('isCustomer')) {
  865. function isCustomer()
  866. {
  867. if (Auth::check() && Auth::user()->user_type == 'customer') {
  868. return true;
  869. }
  870. return false;
  871. }
  872. }
  873. if (!function_exists('formatBytes')) {
  874. function formatBytes($bytes, $precision = 2)
  875. {
  876. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  877. $bytes = max($bytes, 0);
  878. $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  879. $pow = min($pow, count($units) - 1);
  880. // Uncomment one of the following alternatives
  881. $bytes /= pow(1024, $pow);
  882. // $bytes /= (1 << (10 * $pow));
  883. return round($bytes, $precision) . ' ' . $units[$pow];
  884. }
  885. }
  886. // duplicates m$ excel's ceiling function
  887. if (!function_exists('ceiling')) {
  888. function ceiling($number, $significance = 1)
  889. {
  890. return (is_numeric($number) && is_numeric($significance)) ? (ceil($number / $significance) * $significance) : false;
  891. }
  892. }
  893. //for api
  894. if (!function_exists('get_images_path')) {
  895. function get_images_path($given_ids, $with_trashed = false)
  896. {
  897. $paths = [];
  898. foreach (explode(',', $given_ids) as $id) {
  899. $paths[] = uploaded_asset($id);
  900. }
  901. return $paths;
  902. }
  903. }
  904. //for api
  905. if (!function_exists('checkout_done')) {
  906. function checkout_done($combined_order_id, $payment)
  907. {
  908. $combined_order = CombinedOrder::find($combined_order_id);
  909. foreach ($combined_order->orders as $key => $order) {
  910. $order->payment_status = 'paid';
  911. $order->payment_details = $payment;
  912. $order->save();
  913. try {
  914. NotificationUtility::sendOrderPlacedNotification($order);
  915. calculateCommissionAffilationClubPoint($order);
  916. } catch (\Exception $e) {
  917. }
  918. }
  919. }
  920. }
  921. //for api
  922. if (!function_exists('wallet_payment_done')) {
  923. function wallet_payment_done($user_id, $amount, $payment_method, $payment_details)
  924. {
  925. $user = \App\Models\User::find($user_id);
  926. $user->balance = $user->balance + $amount;
  927. $user->save();
  928. $wallet = new Wallet;
  929. $wallet->user_id = $user->id;
  930. $wallet->amount = $amount;
  931. $wallet->payment_method = $payment_method;
  932. $wallet->payment_details = $payment_details;
  933. $wallet->save();
  934. }
  935. }
  936. if (!function_exists('purchase_payment_done')) {
  937. function purchase_payment_done($user_id, $package_id)
  938. {
  939. $user = User::findOrFail($user_id);
  940. $user->customer_package_id = $package_id;
  941. $customer_package = CustomerPackage::findOrFail($package_id);
  942. $user->remaining_uploads += $customer_package->product_upload;
  943. $user->save();
  944. return 'success';
  945. }
  946. }
  947. if (!function_exists('product_restock')) {
  948. function product_restock($orderDetail)
  949. {
  950. $variant = $orderDetail->variation;
  951. if ($orderDetail->variation == null) {
  952. $variant = '';
  953. }
  954. $product_stock = ProductStock::where('product_id', $orderDetail->product_id)
  955. ->where('variant', $variant)
  956. ->first();
  957. if ($product_stock != null) {
  958. $product_stock->qty += $orderDetail->quantity;
  959. $product_stock->save();
  960. }
  961. }
  962. }
  963. //Commission Calculation
  964. if (!function_exists('calculateCommissionAffilationClubPoint')) {
  965. function calculateCommissionAffilationClubPoint($order)
  966. {
  967. (new CommissionController)->calculateCommission($order);
  968. if (addon_is_activated('affiliate_system')) {
  969. (new AffiliateController)->processAffiliatePoints($order);
  970. }
  971. if (addon_is_activated('club_point')) {
  972. if ($order->user != null) {
  973. (new ClubPointController)->processClubPoints($order);
  974. }
  975. }
  976. $order->commission_calculated = 1;
  977. $order->save();
  978. }
  979. }
  980. // product storehouse order free up
  981. if (!function_exists('product_storehouse_order_free_up')) {
  982. function product_storehouse_order_free_up($orderId)
  983. {
  984. $order = Order::query()->find($orderId);
  985. if (!$order) return false;
  986. try {
  987. DB::beginTransaction();
  988. // 判断是否已经释放
  989. if (!$order->freeze_expired_at) return false;
  990. $shop = $order->shop;
  991. $user = $shop->user;
  992. if (!$shop) return false;
  993. $grand_total = $order->grand_total;
  994. if ($order->picking_switch!=1){
  995. $grand_total = $order->grand_total - $order->product_storehouse_total;
  996. }
  997. $shop->admin_to_pay -= $grand_total; // 减少冻结资金
  998. $user->balance += $grand_total; // 增加用户钱包余额
  999. $order->freeze_expired_at = null; // 标记订单已经释放
  1000. //结算商家
  1001. $seller_1 = User::where('id', $user->pid)->first();
  1002. if ($seller_1){
  1003. $seller_1->balance += $grand_total*get_setting('commission_ratio_level_1')/100;
  1004. $seller_1->save();
  1005. //写收入日志
  1006. $affiliate_log = new AffiliateLog;
  1007. $affiliate_log->user_id = $user->id;
  1008. $affiliate_log->referred_by_user = $seller_1->id;
  1009. $affiliate_log->amount = $grand_total*get_setting('commission_ratio_level_1')/100;
  1010. $affiliate_log->order_id = $order->id;
  1011. $affiliate_log->affiliate_type = '';
  1012. $affiliate_log->save();
  1013. $seller_2 = User::where('id', $seller_1->pid)->first();
  1014. if ($seller_2){
  1015. $seller_2->balance += $grand_total*get_setting('commission_ratio_level_2')/100;
  1016. $seller_2->save();
  1017. //写收入日志
  1018. $affiliate_log = new AffiliateLog;
  1019. $affiliate_log->user_id = $user->id;
  1020. $affiliate_log->referred_by_user = $seller_2->id;
  1021. $affiliate_log->amount = $grand_total*get_setting('commission_ratio_level_2')/100;
  1022. $affiliate_log->order_id = $order->id;
  1023. $affiliate_log->affiliate_type = '';
  1024. $affiliate_log->save();
  1025. $seller_3 = User::where('id', $seller_2->pid)->first();
  1026. if ($seller_3){
  1027. $seller_3->balance += $grand_total*get_setting('commission_ratio_level_3')/100;
  1028. $seller_3->save();
  1029. //写收入日志
  1030. $affiliate_log = new AffiliateLog;
  1031. $affiliate_log->user_id = $user->id;
  1032. $affiliate_log->referred_by_user = $seller_3->id;
  1033. $affiliate_log->amount = $grand_total*get_setting('commission_ratio_level_3')/100;
  1034. $affiliate_log->order_id = $order->id;
  1035. $affiliate_log->affiliate_type = '';
  1036. $affiliate_log->save();
  1037. }
  1038. }
  1039. }
  1040. //结算商家 结束
  1041. //结算用户
  1042. $user_1 = User::where('id', $order->user->pid)->first();
  1043. if ($user_1){
  1044. $user_1->balance += $grand_total*get_setting('commission_ratio_level_1')/100;
  1045. $user_1->save();
  1046. //写收入日志
  1047. $affiliate_log = new AffiliateLog;
  1048. $affiliate_log->user_id = $order->user_id;
  1049. $affiliate_log->referred_by_user = $user_1->id;
  1050. $affiliate_log->amount = $grand_total*get_setting('commission_ratio_level_1')/100;
  1051. $affiliate_log->order_id = $order->id;
  1052. $affiliate_log->affiliate_type = '';
  1053. $affiliate_log->save();
  1054. $user_2 = User::where('id', $user_1->pid)->first();
  1055. if ($user_2){
  1056. $user_2->balance += $grand_total*get_setting('commission_ratio_level_2')/100;
  1057. $user_2->save();
  1058. //写收入日志
  1059. $affiliate_log = new AffiliateLog;
  1060. $affiliate_log->user_id = $order->user_id;
  1061. $affiliate_log->referred_by_user = $user_2->id;
  1062. $affiliate_log->amount = $grand_total*get_setting('commission_ratio_level_2')/100;
  1063. $affiliate_log->order_id = $order->id;
  1064. $affiliate_log->affiliate_type = '';
  1065. $affiliate_log->save();
  1066. $user_3 = User::where('id', $user_2->pid)->first();
  1067. if ($user_3){
  1068. $user_3->balance += $grand_total*get_setting('commission_ratio_level_3')/100;
  1069. $user_3->save();
  1070. //写收入日志
  1071. $affiliate_log = new AffiliateLog;
  1072. $affiliate_log->user_id = $order->user_id;
  1073. $affiliate_log->referred_by_user = $user_3->id;
  1074. $affiliate_log->amount = $grand_total*get_setting('commission_ratio_level_3')/100;
  1075. $affiliate_log->order_id = $order->id;
  1076. $affiliate_log->affiliate_type = '';
  1077. $affiliate_log->save();
  1078. }
  1079. }
  1080. }
  1081. $shop->save();
  1082. $user->save();
  1083. $order->save();
  1084. DB::commit();
  1085. } catch (Throwable $e) {
  1086. DB::rollBack();
  1087. return false;
  1088. }
  1089. return true;
  1090. }
  1091. }
  1092. // Addon Activation Check
  1093. if (!function_exists('addon_is_activated')) {
  1094. function addon_is_activated($identifier, $default = null)
  1095. {
  1096. $addons = Cache::remember('addons', 86400, function () {
  1097. return Addon::all();
  1098. });
  1099. $activation = $addons->where('unique_identifier', $identifier)->where('activated', 1)->first();
  1100. return $activation == null ? false : true;
  1101. }
  1102. }
  1103. // Addon Activation Check
  1104. if (!function_exists('seller_package_validity_check')) {
  1105. function seller_package_validity_check($user_id = null)
  1106. {
  1107. $user = $user_id == null ? \App\Models\User::find(Auth::user()->id) : \App\Models\User::find($user_id);
  1108. $shop = $user->shop;
  1109. $package_validation = false;
  1110. if (
  1111. $shop->product_upload_limit > $shop->user->products()->count()
  1112. && $shop->package_invalid_at != null
  1113. && Carbon::now()->diffInDays(Carbon::parse($shop->package_invalid_at), false) >= 0
  1114. ) {
  1115. $package_validation = true;
  1116. }
  1117. return $package_validation;
  1118. // Ture = Seller package is valid and seller has the product upload limit
  1119. // False = Seller package is invalid or seller product upload limit exists.
  1120. }
  1121. }
  1122. // Get URL params
  1123. if (!function_exists('get_url_params')) {
  1124. function get_url_params($url, $key)
  1125. {
  1126. $query_str = parse_url($url, PHP_URL_QUERY);
  1127. parse_str($query_str, $query_params);
  1128. return $query_params[$key] ?? '';
  1129. }
  1130. }