<?php namespace App\Models;
/**
 * File			: UserModel.php
 * Description  : Model untuk halaman login/user
 * Created By	: Hendi
 * Created Date : 15 Mar 2021
 * Last Update  : 06 Apr 2021
**/
use CodeIgniter\Model;
 
class UserModel extends Model
{
    protected $table = 'user';
	protected $primaryKey = 'user_id';
	# bukan hapus data permanen tapi saat call method delete hanya ngisi deleted_at saja
	protected $useSoftDeletes = false;
    protected $allowedFields = ['user_id', 'password', 'full_name', 'description', 'level_id', 'group_id', 'email', 'phone', 'address', 'photo', 'is_active'];
	protected $useTimestamps = true;
	protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';
	
	# fungsi untuk cek is_unique user_id
	public function cekUserID($id) 
	{
		return $this->builder()->selectCount('user_id')
			->where('user_id', $id)
			->countAllResults(); 
	}
    # fungsi untuk cek user di transaksi
	public function cekTrxData($id) 
	{
		return $this->db->query("SELECT COUNT(user_id) AS jumlah FROM transaction WHERE user_id='".$id."' AND deleted_at IS NULL")->getRow()->jumlah;
	}
}