104 lines
3.3 KiB
PHP
104 lines
3.3 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
|
use App\Models\Country;
|
|
use Brick\Money\Context\AutoContext;
|
|
use Brick\Money\Money;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
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());
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public function uploadImage(UploadedFile $file, $imageCode, $folderName)
|
|
{
|
|
$original_filename = $file->getClientOriginalName();
|
|
$original_filename_arr = explode('.', $original_filename);
|
|
$file_ext = end($original_filename_arr);
|
|
$image = $imageCode . '-' . time() . '.' . $file_ext;
|
|
|
|
//Check if the directory already exists.
|
|
$directoryName = './' . $folderName;
|
|
if (!is_dir($directoryName)) {
|
|
//Directory does not exist, so lets create it.
|
|
mkdir($directoryName, 0755);
|
|
}
|
|
|
|
$compressedImage = $this->compressImage($file, './' . $folderName . '/' . $image, 70);
|
|
if ($compressedImage) {
|
|
return $image;
|
|
} else {
|
|
return $this->errorResponse(trans('errors.compression_failed'));
|
|
}
|
|
}
|
|
|
|
private function compressImage($source, $destination, $quality)
|
|
{
|
|
// Get image info
|
|
$imgInfo = getimagesize($source);
|
|
$mime = $imgInfo['mime'];
|
|
|
|
// Create a new image from file
|
|
switch ($mime) {
|
|
case 'image/jpeg':
|
|
$image = imagecreatefromjpeg($source);
|
|
break;
|
|
case 'image/png':
|
|
$image = imagecreatefrompng($source);
|
|
break;
|
|
case 'image/gif':
|
|
$image = imagecreatefromgif($source);
|
|
break;
|
|
default:
|
|
$image = imagecreatefromjpeg($source);
|
|
}
|
|
|
|
// Save image
|
|
imagejpeg($image, $destination, $quality);
|
|
|
|
// Return compressed image
|
|
return $destination;
|
|
}
|
|
}
|