Files
portal-api/app/Helpers/NumberToWords.php
baodev0 520f21cb11 feat: add GeneratesUniqueSlug class for unique slug generation
feat: create PostCategoryFactory for generating post category data

feat: create PostFactory for generating post data

feat: add retry_requested_at column to services table

feat: add vcenter provisioning fields to products table

feat: add module_host_id to os table

feat: create email_notification_templates table for email templates

feat: add scenario and format fields to template table

feat: add admin fields to transactions table

fix: update ticket table columns and types

feat: add uuid to ticket table and backfill existing records

fix: make ticket_reply user_id nullable

feat: create notifications table for user notifications

fix: make ticket uuid nullable

feat: create post_categories table for blog categories

feat: create posts table for blog posts

feat: add invoice email template for PDF generation
2026-05-25 15:39:42 +07:00

77 lines
2.2 KiB
PHP

<?php
namespace App\Helpers;
class NumberToWords
{
private static array $ones = [
'', 'một', 'hai', 'ba', 'bốn', 'năm', 'sáu', 'bảy', 'tám', 'chín',
];
private static array $groups = [
'', 'nghìn', 'triệu', 'tỷ',
];
public static function convert(int $number): string
{
if ($number === 0) return 'Không đồng';
$result = self::readNumber($number) . ' đồng';
return mb_strtoupper(mb_substr($result, 0, 1)) . mb_substr($result, 1);
}
private static function readNumber(int $n): string
{
if ($n === 0) return '';
if ($n < 0) return 'âm ' . self::readNumber(-$n);
$parts = [];
$groups = self::$groups;
// Split into groups of 3 digits
$groupIdx = 0;
while ($n > 0) {
$chunk = $n % 1000;
if ($chunk !== 0) {
$text = self::readHundreds($chunk);
$parts[] = $text . ($groups[$groupIdx] ? ' ' . $groups[$groupIdx] : '');
}
$n = intdiv($n, 1000);
$groupIdx++;
}
return implode(' ', array_reverse($parts));
}
private static function readHundreds(int $n): string
{
if ($n < 10) return self::$ones[$n];
if ($n < 20) return 'mười' . ($n > 10 ? ($n === 15 ? ' lăm' : ' ' . self::$ones[$n - 10]) : '');
$h = intdiv($n, 100);
$t = intdiv($n % 100, 10);
$u = $n % 10;
$result = '';
if ($h > 0) $result .= self::$ones[$h] . ' trăm';
if ($t === 0 && $u === 0) return $result;
if ($t === 0) {
// hàng trăm > 0, hàng chục = 0
$result .= ' linh ' . self::$ones[$u];
} elseif ($t === 1) {
$result .= ($h > 0 ? ' ' : '') . 'mười' . ($u > 0 ? ($u === 5 ? ' lăm' : ' ' . self::$ones[$u]) : '');
} else {
$result .= ($h > 0 ? ' ' : '') . self::$ones[$t] . ' mươi';
if ($u === 1) $result .= ' mốt';
elseif ($u === 4) $result .= ' tư';
elseif ($u === 5) $result .= ' lăm';
elseif ($u > 0) $result .= ' ' . self::$ones[$u];
}
return $result;
}
}