nanosanteservice/app/Traits/Helper.php

53 lines
1.7 KiB
PHP
Raw Normal View History

2021-10-04 16:24:39 +00:00
<?php
namespace App\Traits;
use App\Models\Country;
use Brick\Money\Context\AutoContext;
use Brick\Money\Money;
use Illuminate\Support\Facades\DB;
2021-10-04 16:24:39 +00:00
trait Helper
{
public function toMoneyWithNetwork($amount, $id_network)
{
$currency = collect(DB::select('SELECT cu.code FROM networks n INNER JOIN countries c ON c.id = n.country_id INNER JOIN currencies cu ON cu.id = c.idCurrency
WHERE n.id = :id', ['id' => $id_network]))->first();
$money = Money::of(round($amount, 2), $currency ? $currency->code : 'XAF', new AutoContext());
return $money->formatTo(app()->getLocale());
}
public function toMoney($amount, $id_country)
{
$country = Country::findOrFail($id_country);
$money = Money::of(round($amount, 2), $country->currency->code, new AutoContext());
return $money->formatTo(app()->getLocale());
}
public function toMoneyWithCurrencyCode($amount, $currency_code)
{
$money = Money::of(round($amount, 2), $currency_code, new AutoContext());
return $money->formatTo(app()->getLocale());
}
2021-10-04 16:24:39 +00:00
public function generateTransactionCode($length = 12)
{
$characters = '23456789ABCDEFGHJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
public function checkPassword($password, $encrypted_password, $salt)
{
$encrypted_password_to_check = base64_encode(sha1($password . $salt, true) . $salt);
return $encrypted_password_to_check == $encrypted_password;
}
2021-10-04 16:24:39 +00:00
}