2023-08-14 14:16:56 +07:00
|
|
|
<?php
|
2023-08-14 15:59:53 +07:00
|
|
|
/**
|
2023-10-12 15:55:42 +07:00
|
|
|
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
|
|
|
|
* by https://t.me/ibnux
|
|
|
|
**/
|
|
|
|
/**
|
|
|
|
* This script is for managing user balance
|
2023-08-14 15:59:53 +07:00
|
|
|
**/
|
2023-08-14 14:16:56 +07:00
|
|
|
|
2023-08-14 15:59:53 +07:00
|
|
|
class Balance
|
|
|
|
{
|
2023-08-14 14:16:56 +07:00
|
|
|
|
2023-08-14 15:59:53 +07:00
|
|
|
public static function plus($id_customer, $amount)
|
|
|
|
{
|
|
|
|
$c = ORM::for_table('tbl_customers')->where('id', $id_customer)->find_one();
|
|
|
|
$c->balance = $amount + $c['balance'];
|
|
|
|
$c->save();
|
2023-08-14 14:16:56 +07:00
|
|
|
}
|
|
|
|
|
2023-08-14 15:59:53 +07:00
|
|
|
public static function transfer($id_customer, $phoneTarget, $amount)
|
|
|
|
{
|
|
|
|
global $config;
|
2023-08-23 16:46:05 +07:00
|
|
|
if (Balance::min($id_customer, $amount)) {
|
2023-09-13 15:38:56 +07:00
|
|
|
return Balance::plusByPhone($phoneTarget, $amount);
|
2023-08-18 09:47:03 +07:00
|
|
|
} else {
|
2023-08-14 15:59:53 +07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-08-14 14:16:56 +07:00
|
|
|
|
2023-08-14 15:59:53 +07:00
|
|
|
public static function min($id_customer, $amount)
|
|
|
|
{
|
|
|
|
$c = ORM::for_table('tbl_customers')->where('id', $id_customer)->find_one();
|
2024-11-04 15:10:58 +07:00
|
|
|
$c->balance = $c['balance'] - $amount;
|
|
|
|
$c->save();
|
|
|
|
return true;
|
2023-08-14 14:16:56 +07:00
|
|
|
}
|
|
|
|
|
2023-08-14 15:59:53 +07:00
|
|
|
public static function plusByPhone($phone_customer, $amount)
|
|
|
|
{
|
|
|
|
$c = ORM::for_table('tbl_customers')->where('username', $phone_customer)->find_one();
|
2023-08-18 09:47:03 +07:00
|
|
|
if ($c) {
|
2023-08-14 15:59:53 +07:00
|
|
|
$c->balance = $amount + $c['balance'];
|
|
|
|
$c->save();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-14 14:16:56 +07:00
|
|
|
|
2023-08-14 15:59:53 +07:00
|
|
|
public static function minByPhone($phone_customer, $amount)
|
|
|
|
{
|
|
|
|
$c = ORM::for_table('tbl_customers')->where('username', $phone_customer)->find_one();
|
|
|
|
if ($c && $c['balance'] >= $amount) {
|
2023-08-18 09:47:03 +07:00
|
|
|
$c->balance = $c['balance'] - $amount;
|
2023-08-14 15:59:53 +07:00
|
|
|
$c->save();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-14 14:16:56 +07:00
|
|
|
}
|
2023-08-14 15:59:53 +07:00
|
|
|
}
|