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