| Server IP : 103.48.194.25 / Your IP : 216.73.216.47 Web Server : Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.33 System : Linux VMHostDefault 3.10.0-1160.42.2.el7.x86_64 #1 SMP Tue Sep 7 14:49:57 UTC 2021 x86_64 User : sieuthimay ( 1016) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/zomuaban.com/public_html/site/models/ |
Upload File : |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Transaction_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
header('Content-Type: text/html; charset=utf-8');
}
public function create($userId, $amount, $description) {
# lấy tổng tiền user hiện có
$wallet = $this->get_wallet_by_userId($userId);
# thêm một record vào db
try {
$q = $this->db->insert('transaction', array( 'userId' => $userId,
'amount_transaction'=> $amount,
'total_before' => $wallet->amount,
'total_after' => ($wallet->amount + $amount),
'description' => $description,
'date_time' => _now()
));
# Nếu insert thành công > update lại tài khoản trong ví
if ($q) {
$this->db->where('userId',$userId);
$this->db->update('wallet', array('transactionId' => $this->db->insert_id(), 'amount' => ($wallet->amount + $amount)));
}else {
# TODO: ghi ra log
}
}catch (Exception $e) {
echo '< error: error on transaction > ' . $e;
}
}
public function get_wallet_by_userId($userId) {
$this->db->where('userId', $userId);
$q = $this->db->get('wallet');
if ($q->num_rows() > 0 ) {
return $q->row();
}else {
# Nếu ko có thì insert record vào cho user này
$this->db->insert('wallet', array( 'userId' => $userId,
'amount' => 0,
'transactionId' => null,
'active' => 1,
'active_date' => _now()
));
return $this->get_wallet_by_userId($userId);
}
}
public function get_transaction_by_userId($userId){
$this->db->where('userId', $userId);
$this->db->group_by('date_time');
$q = $this->db->order_by('date_time desc');
$q = $this->db->limit(10);
$q = $this->db->get('transaction');
if($q->num_rows()>0){
//return $q->result();
$arr=[];
foreach($q->result() as $row) {
$date = strtotime($row->date_time);
$day = date('d/m/y', $date);
$arr[$day][$row->id] = $row;
}
return $arr;
}else return false;
}
}