fix bug of pargar
This commit is contained in:
@@ -18,10 +18,10 @@ class TransactionInterface
|
||||
private $wallet_interface;
|
||||
private $transaction;
|
||||
|
||||
function __construct($wallet_interface = null, $_inp = null, $_type = null,$deposit_gold = false,$deposit_cash=false, $gold_block = 0,$meta = [])
|
||||
function __construct($wallet_interface = null, $_inp = null, $_type = null,$deposit_gold = false,$deposit_cash=false, $gold_block = 0,$meta = [], $allow_negative = false)
|
||||
|
||||
{
|
||||
$this->transaction = TransactionInterface::apply_transaction($wallet_interface, $_inp, $_type,$deposit_gold ,$deposit_cash,$gold_block,$meta);
|
||||
$this->transaction = TransactionInterface::apply_transaction($wallet_interface, $_inp, $_type,$deposit_gold ,$deposit_cash,$gold_block,$meta,$allow_negative);
|
||||
}
|
||||
|
||||
static function validate_last_transaction($wallet_id)
|
||||
@@ -53,20 +53,53 @@ class TransactionInterface
|
||||
$transaction->save();
|
||||
}
|
||||
|
||||
public static function apply_transaction($wallet_interface, $_inp, $_type,$deposit_gold,$deposit_cash,$gold_block, $meta = [])
|
||||
/**
|
||||
* کف موجودی طلا — فقط سمت کسر. شارژ مثبت هیچوقت بسته نمیشود، حتی اگر مانده منفی باشد.
|
||||
* ستون gold از نوع float است، پس رواداری نسبی میگذاریم تا «فروش کل موجودی» به خطای گرد کردن نخورد.
|
||||
*/
|
||||
private static function assert_gold_floor($wallet, $_inp, $deposit_gold)
|
||||
{
|
||||
if ($deposit_gold || !isset($_inp['gold'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$requested = abs((float) $_inp['gold']);
|
||||
if ($requested <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$balance = (float) $wallet->gold;
|
||||
$tolerance = 1e-6 + abs($balance) * 1e-6;
|
||||
if ($balance + $tolerance < $requested) {
|
||||
throw new UnprocessableEntityHttpException('مقدار وارد شده بیشتر از موجودی طلای شماست');
|
||||
}
|
||||
}
|
||||
|
||||
/** کف موجودی ریالی — فقط سمت کسر؛ واریز و شارژ مثبت همیشه باز است. */
|
||||
private static function assert_cash_floor($wallet, $_inp, $deposit_cash)
|
||||
{
|
||||
if ($deposit_cash || !isset($_inp['cash'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$requested = abs((float) $_inp['cash']);
|
||||
if ($requested > 0 && (float) $wallet->cash < $requested) {
|
||||
throw new UnprocessableEntityHttpException('موجودی ریالی کافی نیست');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $allow_negative فقط برای تطبیق مانده با سیستم حسابداری؛ آنجا مانده میتواند
|
||||
* واقعاً منفی باشد و کیف باید همان را بازتاب دهد.
|
||||
*/
|
||||
public static function apply_transaction($wallet_interface, $_inp, $_type,$deposit_gold,$deposit_cash,$gold_block, $meta = [], $allow_negative = false)
|
||||
{
|
||||
$wallet = $wallet_interface->wallet;
|
||||
TransactionInterface::validate_last_transaction($wallet->id);
|
||||
#-------------Check Balance --------
|
||||
if (!$deposit_gold) {
|
||||
// if ($_type === 1 && $wallet->cash < $_inp['value']) {
|
||||
// throw new Exceptions\NotEnoughCashBalance();
|
||||
//
|
||||
// }
|
||||
if ($_type === 2 && $wallet->gold < $_inp['gold']) {
|
||||
throw new UnprocessableEntityHttpException('مقدار وارد شده کمتر از موجودی شماست');
|
||||
|
||||
}
|
||||
if (!$allow_negative) {
|
||||
TransactionInterface::assert_gold_floor($wallet, $_inp, $deposit_gold);
|
||||
TransactionInterface::assert_cash_floor($wallet, $_inp, $deposit_cash);
|
||||
}
|
||||
// $_inp['value'] = abs($_inp['value']) * ($deposit ? 1 : -1);
|
||||
if (isset($_inp['gold'])){
|
||||
|
||||
Reference in New Issue
Block a user