douba1617 2015-02-11 11:15
浏览 11

laravel十进制增量或+得到奇怪的结果

i'm trying to do when a user deposit, there is a deposit record, once admin approve, there is a transaction inserted, deposit record status update and increase user credit, but all the amount field is store as decimal(12,2)

here come the weird thing

user_credit = 1001.00 deposit_amount = 500.00

when user_credit + deposit_amount, the results is 501, what the ????

here is the transaction model approveDeposit() method

    /**
     * 
     * @param int $depositId
     * @return boolean|array
     */
    public function addDepositTransaction($depositId) {

        $dp = null;
        $user = null;
        $t = null;

        \DB::beginTransaction();

        $errors = array();

        //Select the deposit id
        try {
            $dp = new \Deposit();
            $dp = $dp::find($depositId);

            if (!$dp || !$dp->exists()) {
                $errors[] = 'Deposit Id Not Found.';
                \DB::rollback();
                return $errors;
            } else {
//                if ($dp->status != 0) {
//                    $errors[] = 'This Deposit is already ' . $dp->getStatusText();
//                    \DB::rollback();
//                    return $errors;
//                }
            }
        } catch(\Exception $ex) {
            $errors[] = $ex->getMessage();
            \DB::rollback();
            return $errors;
        }

        //Select the user which taken from deposit
        try {
            $user = \User::find($dp->user_id);
        } catch (\Exception $ex) {
            $errors[] = $ex->getMessage();
            \DB::rollback();
            return $errors;
        }

        //Insert new transaction
        try {
            $t = new \Transactions();
            $t->user_id = $dp->user_id;
            $t->action = \Transactions::ACTION_DEPOSIT;
            $t->credit = \Transactions::CREDIT_POINTS;
            $t->credit_tag = \Transactions::CREDIT_TAG_POINTS;
            $t->amount = $dp->deposit_amount;
            $t->description_tag = 'deposit';
            $t->description = 'User Deposit Accepted';
            $t->related_one = $dp->id;

            if ($t->save()) {

            } else {
                $errors = $t->errors()->all();
                \DB::rollback();
                return $errors;
            }
        } catch (\Exception $ex) {
            $errors[] = $ex->getMessage();
            \DB::rollback();
            return $errors;
        }

        //Update the deposit status to 1(complete)
        try {
            $dp->status = 1;
            if ($dp->save(\Deposit::getStatusRules())) {

            } else {
                $errors = $dp->errors()->all();
                \DB::rollback();
                return $errors;
            }
        } catch (\Exception $ex) {
            $errors[] = $ex->getMessage();
            \DB::rollback();
            return $errors;
        }

        //Finally, update the user credits
        try {
//            $user->increment('points', (int)$dp->deposit_amount);
//            (float)$user->points = (float)($user->points) + (float)($dp->deposit_amount);
            dd($user->points + $dp->deposit_amount);
            if ($user->save(\User::getPointsRules())) {

            } else {
                $errors = $user->errors()->all();
                \DB::rollback();
                return $errors;
            }
        } catch (\Exception $ex) {
            $errors[] = $ex->getMessage();
            \DB::rollback();
            return $errors;
        }

        \DB::commit();
        return true;
    }

i try setting the variable type to both (int) (float) (string), also the result is wrong, i also tried the $model->increment('field', number), but the result become 2.00

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 路易威登官网 里边的参数逆向
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程