File: //home/armgroup/libs/app/helpers.php
<?php
use App\Models\Setting;
use Gloudemans\Shoppingcart\Facades\Cart;
if (! function_exists('to_persian_numbers')) {
function to_persian_numbers($string): string
{
return str_replace(range(0, 9), ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'], $string);
}
}
if (! function_exists('to_latin_numbers')) {
function to_latin_numbers($string): string
{
return str_replace(['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'], range(0, 9), $string);
}
}
if (! function_exists('words')) {
function words($value, $words = 100, $end = '...'): string
{
return \Illuminate\Support\Str::words(strip_tags($value), $words, $end);
}
}
if (! function_exists('eventNameTranslate')) {
function eventNameTranslate($name): string
{
return [
'deleted' => 'حذف شد',
'created' => 'ایجاد شد',
'updated' => 'ویرایش شد',
][$name];
}
}
if (! function_exists('saveActivity')) {
function saveActivity($description, $model = null)
{
$model ? activity()->performedOn($model)->log($description) : activity()->log($description);
}
}
if (! function_exists('checkAccess')) {
function checkAccess($id)
{
return auth()->user()->cannot('manage other users content') && auth()->id() != $id;
}
}
if (! function_exists('categoryExists')) {
function categoryExists($slug)
{
return collect(\App\Models\Category::TYPE)->firstWhere('slug', $slug) ?: abort(404);
}
}
if (! function_exists('settings')) {
function settings($name)
{
$settings = Cache::rememberForever('settings', function () {
return Setting::query()->select('name', 'value')->get();
});
return optional($settings->firstWhere('name', $name))->value;
}
}
if (! function_exists('calculateDiscount')) {
function calculateDiscount($special, $price)
{
return round(100 - $special * 100 / $price);
}
}
if (! function_exists('checkSaleStatus')) {
function checkSaleStatus()
{
return settings('offline-status') == 1 || settings('online-status') == 1 || settings('wallet-status') == 1;
}
}
if (! function_exists('calculateShippingPrice')) {
function calculateShippingPrice($city, $type): int
{
$carts = Cart::content()->groupBy('id');
$total_weight = 0;
$province_type = $city->province->type;
foreach (\App\Models\Product::whereIn('id', Cart::content()->pluck('id')->toArray())->select('id', 'weight')->get() as $product) {
$total_weight += ($product->weight ?: 0) * $carts[$product->id]->first()?->qty;
}
$shippingPrice = \App\Models\ShippingPrice::where('from', '<=', $total_weight)
->where(function ($query) use ($total_weight) {
return $query->where('up_to', '>=', $total_weight)->orWhereNull('up_to');
})->first();
if ($type == \App\Models\Order::SHIPPING_EXPRESS && $city->express_status) {
if ($shippingPrice) {
return getExpressShippingPriceWithType($shippingPrice, $province_type);
}
return $city->express_default ?: 0;
} elseif ($type == \App\Models\Order::SHIPPING_COURIER && $city->courier_status) {
if ($shippingPrice) {
return getCourierShippingPriceWithType($shippingPrice, $province_type);
}
return $city->courier_default ?: 0;
}
return -1;
}
}
if (! function_exists('calculateMaxSendingDays')) {
function calculateMaxSendingDays(): int
{
return \App\Models\Product::whereIn('id', Cart::content()->pluck('id')->toArray())
->max('sending_days') ?: 1;
}
}
if (! function_exists('getExpressShippingPriceWithType')) {
function getExpressShippingPriceWithType($shippingPrice, $type): int
{
switch ($type) {
case \App\Models\Province::TYPE_MY_PROVINCE:
return $shippingPrice->my_province_express ?: 0;
case \App\Models\Province::TYPE_NEIGHBOR:
return $shippingPrice->neighbor_province_express ?: 0;
case \App\Models\Province::TYPE_NOT_NEIGHBOR:
return $shippingPrice->not_neighbor_province_express ?: 0;
default:
return -1;
}
}
}
if (! function_exists('getCourierShippingPriceWithType')) {
function getCourierShippingPriceWithType($shippingPrice, $type): int
{
switch ($type) {
case \App\Models\Province::TYPE_MY_PROVINCE:
return $shippingPrice->my_province_courier ?: 0;
case \App\Models\Province::TYPE_NEIGHBOR:
return $shippingPrice->neighbor_province_courier ?: 0;
case \App\Models\Province::TYPE_NOT_NEIGHBOR:
return $shippingPrice->not_neighbor_province_courier ?: 0;
default:
return -1;
}
}
}
if (! function_exists('getFileManagerSharedFolder')) {
function getFileManagerSharedFolder(): string
{
return config('lfm.folder_categories.image.folder_name').'/'.config('lfm.shared_folder_name');
}
}
if (! function_exists('checkStatusAndPermission')) {
function checkStatusAndPermission($model, string $permission): bool
{
return $model->status || (auth()->check() && auth()->user()->can($permission) && (auth()->user()->can('manage other users content') || auth()->id() === $model->user_id));
}
}
if (! function_exists('slug')) {
function slug($title, $separator = '-', $language = null): string
{
return \Illuminate\Support\Str::slug($title, $separator, $language);
}
}
if (! function_exists('send_sms')) {
function send_sms($mobile, $text, $pattern_id)
{
ini_set("soap.wsdl_cache_enabled", "0");
try {
$variables = [
(string) ($text['var1'] ?? ''),
(string) ($text['var2'] ?? ''),
(string) ($text['var3'] ?? ''),
];
$client = new SoapClient('http://api.payamak-panel.com/post/send.asmx?wsdl', array('encoding' => 'UTF-8'));
$data = array(
"username" => '989126365066',
// "password" => '83642e',
"password" => '7471cf04-d0ba-4a3e-a8ac-9fe6be028075',
"text" => $variables,
"to" => "$mobile",
"bodyId" => $pattern_id);
$send_Result = $client->SendByBaseNumber($data)->SendByBaseNumberResult;
} catch (SoapFault $ex) {
Log::debug($ex);
}
}
}