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

时间:2015-02-11 11:15:26

标签: php laravel laravel-4

我试图在用户存款,存款记录,管理员批准,插入交易,存款记录状态更新和增加用户信用时尝试做,但所有金额字段都存储为小数(12,2)

这是奇怪的事情

user_credit = 1001.00 deposit_amount = 500.00

当user_credit + deposit_amount时,结果是501,是什么????

这是交易模型 approveDeposit()方法

    /**
     * 
     * @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;
    }

我尝试将变量类型设置为(int)(float)(字符串),结果也是错误的,我也尝试了$ model-> increment(' field',number),但结果变成2.00

0 个答案:

没有答案