Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/Currency.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan;
 12: 
 13: /**
 14:  * แปลงตัวเลขเป็นจำนวนเงิน บาท ดอลล่าร์.
 15:  *
 16:  * @author Goragod Wiriya <admin@goragod.com>
 17:  *
 18:  * @since 1.0
 19:  */
 20: class Currency
 21: {
 22:     /**
 23:      * แปลงจำนวนเงินเป็นตัวหนังสือ.
 24:      *
 25:      * @assert (100.00) [==] 'one hundred Baht'
 26:      * @assert (101.00) [==] 'one hundred and one Baht'
 27:      * @assert (1000.50) [==] 'one thousand Baht and fifty Satang'
 28:      * @assert (1000050) [==] 'one million fifty Baht'
 29:      * @assert (-1000000050) [==] 'negative one billion fifty Baht'
 30:      * @assert (1000000050) [==] 'one billion fifty Baht'
 31:      * @assert (10000000050.25) [==] 'ten billion fifty Baht and twenty-five Satang'
 32:      *
 33:      * @param string $thb
 34:      *
 35:      * @return string
 36:      */
 37:     public static function bahtEng($thb)
 38:     {
 39:         if (preg_match('/^(\-?[0-9]+)(\.([0-9]+))/', (string) $thb, $match)) {
 40:             $thb = self::engFormat((int) $match[1]).' Baht';
 41:             if ((int) $match[3] > 0) {
 42:                 $thb .= ' and '.self::engFormat((int) substr($match[3].'00', 0, 2)).' Satang';
 43:             }
 44:         } else {
 45:             $thb = self::engFormat((int) $thb).' Baht';
 46:         }
 47:         return $thb;
 48:     }
 49: 
 50:     /**
 51:      * ตัวเลขเป็นตัวหนังสือ (ไทย).
 52:      *
 53:      * @assert (101.00) [==] 'หนึ่งร้อยเอ็ดบาทถ้วน'
 54:      * @assert (1000.50) [==] 'หนึ่งพันบาทห้าสิบสตางค์'
 55:      * @assert (1000.00) [==] 'หนึ่งพันบาทถ้วน'
 56:      * @assert (1000) [==] 'หนึ่งพันบาทถ้วน'
 57:      * @assert (1000050) [==] 'หนึ่งล้านห้าสิบบาทถ้วน'
 58:      * @assert (-1000000050) [==] 'ลบหนึ่งพันล้านห้าสิบบาทถ้วน'
 59:      * @assert (10000000050.25) [==] 'หนึ่งหมื่นล้านห้าสิบบาทยี่สิบห้าสตางค์'
 60:      *
 61:      * @param string $thb
 62:      *
 63:      * @return string
 64:      */
 65:     public static function bahtThai($thb)
 66:     {
 67:         if (preg_match('/(\-){0,1}([0-9]+)(\.([0-9]+))?/', (string) $thb, $match)) {
 68:             $thb = $match[2];
 69:             $ths = !empty($match[4]) ? substr($match[4].'00', 0, 2) : '';
 70:             $thaiNum = array('', 'หนึ่ง', 'สอง', 'สาม', 'สี่', 'ห้า', 'หก', 'เจ็ด', 'แปด', 'เก้า');
 71:             $unitBaht = array('บาท', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน');
 72:             $unitSatang = array('สตางค์', 'สิบ');
 73:             $THB = '';
 74:             $j = 0;
 75:             for ($i = strlen($thb) - 1; $i >= 0; $i--, $j++) {
 76:                 $num = $thb[$i];
 77:                 $tnum = $thaiNum[$num];
 78:                 $unit = $unitBaht[$j];
 79:                 switch (true) {
 80:                     case $j == 0 && $num == 1 && strlen($thb) > 1:
 81:                         $tnum = 'เอ็ด';
 82:                         break;
 83:                     case $j == 1 && $num == 1:
 84:                         $tnum = '';
 85:                         break;
 86:                     case $j == 1 && $num == 2:
 87:                         $tnum = 'ยี่';
 88:                         break;
 89:                     case $j == 6 && $num == 1 && strlen($thb) > 7:
 90:                         $tnum = 'เอ็ด';
 91:                         break;
 92:                     case $j == 7 && $num == 1:
 93:                         $tnum = '';
 94:                         break;
 95:                     case $j == 7 && $num == 2:
 96:                         $tnum = 'ยี่';
 97:                         break;
 98:                     case $j != 0 && $j != 6 && $num == 0:
 99:                         $unit = '';
100:                         break;
101:                 }
102:                 $S = $tnum.$unit;
103:                 $THB = $S.$THB;
104:             }
105:             $THB = ($match[1] == '-' ? 'ลบ' : '').$THB;
106:             if ($ths == '' || $ths == '00') {
107:                 $THS = 'ถ้วน';
108:             } else {
109:                 $j = 0;
110:                 $THS = '';
111:                 for ($i = strlen($ths) - 1; $i >= 0; $i--, $j++) {
112:                     $num = $ths[$i];
113:                     $tnum = $thaiNum[$num];
114:                     $unit = $unitSatang[$j];
115:                     switch (true) {
116:                         case $j == 0 && $num == 1 && strlen($ths) > 1:
117:                             $tnum = 'เอ็ด';
118:                             break;
119:                         case $j == 1 && $num == 1:
120:                             $tnum = '';
121:                             break;
122:                         case $j == 1 && $num == 2:
123:                             $tnum = 'ยี่';
124:                             break;
125:                         case $j != 0 && $j != 6 && $num == 0:
126:                             $unit = '';
127:                             break;
128:                     }
129:                     $S = $tnum.$unit;
130:                     $THS = $S.$THS;
131:                 }
132:             }
133:             return $THB.$THS;
134:         } else {
135:             return '';
136:         }
137:     }
138: 
139:     /**
140:      * ฟังก์ชั่นคำนวณภาษี
141:      * $vat_ex = true ราคาสินค้ารวม VAT เช่น ราคาสินค้า 100 + VAT 7 = ราคาขาย 107
142:      * $vat_ex = false ราคาสินค้ารวม VAT เช่น ราคาขาย 100 = ราคาสินค้า 93 + VAT 7.
143:      * คืนค่า VAT จากราคาขาย.
144:      *
145:      * @assert (1000, 7, true) [==] 70
146:      * @assert (1000, 7, false) [==] 65.420560747663558
147:      *
148:      * @param float $amount ราคาขาย
149:      * @param float $vat    VAT
150:      * @param bool  $vat_ex
151:      *
152:      * @return float
153:      */
154:     public static function calcVat($amount, $vat, $vat_ex = true)
155:     {
156:         if ($vat_ex) {
157:             $result = (($vat * $amount) / 100);
158:         } else {
159:             $result = $amount - ($amount * (100 / (100 + $vat)));
160:         }
161:         return $result;
162:     }
163: 
164:     /**
165:      * ฟังก์ชั่น แปลงตัวเลขเป็นจำนวนเงิน
166:      * คืนค่าข้อความจำนวนเงิน
167:      *
168:      * @param float $amount จำนวนเงิน
169:      * @param int $digit จำนวนทศนิยม (optional) ค่าเริ่มต้น 2 หลัก
170:      * @param string $thousands_sep (optional) เครื่องหมายหลักพัน (default ,)
171:      * @param bool $round (optional) true (default) หลังจุดทศนิยมในหลักที่เกินตั้งแต่ 5 ขึ้นไปปัดขึ้น (round), false ตัดหลักที่เกินทิ้ง (floor)
172:      *
173:      * @return string
174:      */
175:     public static function format($amount, $digit = 2, $thousands_sep = ',', $round = true)
176:     {
177:         if (!$round && preg_match('/^([0-9]+)(\.[0-9]{'.$digit.','.$digit.'})[0-9]+$/', (string) $amount, $match)) {
178:             return number_format(floatval($match[1].$match[2]), $digit, '.', $thousands_sep);
179:         } else {
180:             return number_format((float) $amount, $digit, '.', $thousands_sep);
181:         }
182:     }
183: 
184:     /**
185:      * ตัวเลขเป็นตัวหนังสือ (eng).
186:      *
187:      * @param int $number
188:      *
189:      * @return string
190:      */
191:     private static function engFormat($number)
192:     {
193:         if (is_int($number) && $number < abs(pow(10, 18))) {
194:             switch ($number) {
195:                 case $number < 0:
196:                     $prefix = 'negative';
197:                     $suffix = self::engFormat(-1 * $number);
198:                     $string = $prefix.' '.$suffix;
199:                     break;
200:                 case 1:
201:                     $string = 'one';
202:                     break;
203:                 case 2:
204:                     $string = 'two';
205:                     break;
206:                 case 3:
207:                     $string = 'three';
208:                     break;
209:                 case 4:
210:                     $string = 'four';
211:                     break;
212:                 case 5:
213:                     $string = 'five';
214:                     break;
215:                 case 6:
216:                     $string = 'six';
217:                     break;
218:                 case 7:
219:                     $string = 'seven';
220:                     break;
221:                 case 8:
222:                     $string = 'eight';
223:                     break;
224:                 case 9:
225:                     $string = 'nine';
226:                     break;
227:                 case 10:
228:                     $string = 'ten';
229:                     break;
230:                 case 11:
231:                     $string = 'eleven';
232:                     break;
233:                 case 12:
234:                     $string = 'twelve';
235:                     break;
236:                 case 13:
237:                     $string = 'thirteen';
238:                     break;
239:                 case 15:
240:                     $string = 'fifteen';
241:                     break;
242:                 case $number < 20:
243:                     $string = self::engFormat($number % 10);
244:                     if ($number == 18) {
245:                         $string .= 'een';
246:                     } else {
247:                         $string .= 'teen';
248:                     }
249:                     break;
250:                 case 20:
251:                     $string = 'twenty';
252:                     break;
253:                 case 30:
254:                     $string = 'thirty';
255:                     break;
256:                 case 40:
257:                     $string = 'forty';
258:                     break;
259:                 case 50:
260:                     $string = 'fifty';
261:                     break;
262:                 case 60:
263:                     $string = 'sixty';
264:                     break;
265:                 case 70:
266:                     $string = 'seventy';
267:                     break;
268:                 case 80:
269:                     $string = 'eighty';
270:                     break;
271:                 case 90:
272:                     $string = 'ninety';
273:                     break;
274:                 case $number < 100:
275:                     $prefix = self::engFormat($number - $number % 10);
276:                     $suffix = self::engFormat($number % 10);
277:                     $string = $prefix.'-'.$suffix;
278:                     break;
279:                 case $number < pow(10, 3):
280:                     $string = self::engFormat(intval(floor($number / pow(10, 2)))).' hundred';
281:                     if ($number % pow(10, 2)) {
282:                         $string .= ' and '.self::engFormat($number % pow(10, 2));
283:                     }
284:                     break;
285:                 case $number < pow(10, 6):
286:                     $string = self::engFormat(intval(floor($number / pow(10, 3)))).' thousand';
287:                     if ($number % pow(10, 3)) {
288:                         $string .= self::engFormat($number % pow(10, 3));
289:                     }
290:                     break;
291:                 case $number < pow(10, 9):
292:                     $string = self::engFormat(intval(floor($number / pow(10, 6)))).' million';
293:                     if ($number % pow(10, 6)) {
294:                         $string .= ' '.self::engFormat($number % pow(10, 6));
295:                     }
296:                     break;
297:                 case $number < pow(10, 12):
298:                     $string = self::engFormat(intval(floor($number / pow(10, 9)))).' billion';
299:                     if ($number % pow(10, 9)) {
300:                         $string .= ' '.self::engFormat($number % pow(10, 9));
301:                     }
302:                     break;
303:                 case $number < pow(10, 15):
304:                     $string = self::engFormat(intval(floor($number / pow(10, 12)))).' trillion';
305:                     if ($number % pow(10, 12)) {
306:                         $string .= ' '.self::engFormat($number % pow(10, 12));
307:                     }
308:                     break;
309:                 case $number < pow(10, 18):
310:                     $string = self::engFormat(intval(floor($number / pow(10, 15)))).' quadrillion';
311:                     if ($number % pow(10, 15)) {
312:                         $string .= ' '.self::engFormat($number % pow(10, 15));
313:                     }
314:                     break;
315:             }
316:             return $string;
317:         } else {
318:             return 'zero';
319:         }
320:     }
321: }
322: 
Kotchasan API documentation generated by ApiGen