<?php
class UserRole{
    // database connection and table name
    private $conn;
    private $table_name = "userrole";
 
    // field list start
	public $id;
	public $rolename;
	public $create_role;
	public $employee;
	public $create_account;
	public $change_password;
	public $investor;
    public $room;
    public $agent;
    public $item;
    public $duty_schedule;
    public $attendance;
    public $attendance_update;
    public $reservation;
    public $upcoming_arrivals;
    public $arrival;
    public $occupied_list;
    public $deleted_reservation_list;
    public $sales_service;
    public $stock_in;
    public $stock_out;
    public $stock_in_report;
    public $stock_out_report;
    public $stock_balance_report;
    public $purchase;
    public $purchase_report;
    public $monthly_report;
    public $cash_book_report;
    public $coa;
    public $expense;
    public $income;
    public $expense_report;
    public $income_report;
	public $attendance_report;
    public $guest_report;
    public $arrival_report;
    public $departure_report;

    public function __construct($db){
        $this->conn = $db;
    }

	private function selectColumns(){
		return "id, rolename, create_role, employee, create_account, change_password, investor,
			vehicle AS room, agent, item, duty_schedule, attendance, attendance_update,
			booking AS reservation, upcoming_pickups AS upcoming_arrivals, pickup AS arrival,
			active_rentals AS occupied_list, deleted_booking_list AS deleted_reservation_list,
			sales_service, stock_in, stock_out, stock_in_report, stock_out_report, stock_balance_report,
			purchase, purchase_report, monthly_report, cash_book_report, coa, expense, income,
			expense_report, income_report, attendance_report, customer_report AS guest_report,
			pickup_report AS arrival_report, return_report AS departure_report";
	}
	
	function getAllRows(){
		$query = "SELECT " . $this->selectColumns() . " FROM " . $this->table_name . "  ORDER BY id";
		$stmt = $this->conn->prepare($query);
		$stmt->execute();
		return $stmt;
	}

	function getOneRow(){
		$query = "SELECT " . $this->selectColumns() . " FROM " . $this->table_name . "  WHERE id = ?";
		$stmt = $this->conn->prepare($query);	
		$stmt->bindParam(1, $this->id);	
		$stmt->execute();
		$row = $stmt->fetch(PDO::FETCH_ASSOC);
		
		$this->id = $row['id'];
		$this->rolename = $row['rolename'];
		$this->create_role = $row['create_role'];
		$this->employee = $row['employee'];
		$this->create_account = $row['create_account'];
		$this->change_password = $row['change_password'];
		$this->investor = $row['investor'];
		$this->room = $row['room'];
		$this->agent = $row['agent'];
		$this->item = $row['item'];
		$this->duty_schedule = $row['duty_schedule'];
		$this->attendance = $row['attendance'];
		$this->attendance_update = $row['attendance_update'];
		$this->reservation = $row['reservation'];
		$this->upcoming_arrivals = $row['upcoming_arrivals'];
		$this->arrival = $row['arrival'];
		$this->occupied_list = $row['occupied_list'];
		$this->deleted_reservation_list = $row['deleted_reservation_list'];
		$this->sales_service = $row['sales_service'];
		$this->stock_in = $row['stock_in'];
		$this->stock_out = $row['stock_out'];
		$this->stock_in_report = $row['stock_in_report'];
		$this->stock_out_report = $row['stock_out_report'];
		$this->stock_balance_report = $row['stock_balance_report'];
		$this->purchase = $row['purchase'];
		$this->purchase_report = $row['purchase_report'];
		$this->monthly_report = $row['monthly_report'];
		$this->cash_book_report = $row['cash_book_report'];
		$this->coa = $row['coa'];
		$this->expense = $row['expense'];
		$this->income = $row['income'];
		$this->expense_report = $row['expense_report'];
		$this->income_report = $row['income_report'];
		$this->attendance_report = $row['attendance_report'];
		$this->guest_report = $row['guest_report'];
		$this->arrival_report = $row['arrival_report'];
		$this->departure_report = $row['departure_report'];
		


	}

	function getPermission(){
		$query = "SELECT " . $this->selectColumns() . " FROM " . $this->table_name . "  WHERE id = ?";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(1, $this->userrole_id);
		$stmt->execute();
		$row = $stmt->fetch(PDO::FETCH_ASSOC);
		
		if ($row) {
			$this->id = $row['id'];
			$this->rolename = $row['rolename'];
			$this->create_role = $row['create_role'];
			$this->employee = $row['employee'];
			$this->create_account = $row['create_account'];
			$this->change_password = $row['change_password'];
			$this->investor = $row['investor'];
			$this->room = $row['room'];
			$this->agent = $row['agent'];
			$this->item = $row['item'];
			$this->duty_schedule = $row['duty_schedule'];
			$this->attendance = $row['attendance'];
			$this->attendance_update = $row['attendance_update'];
			$this->reservation = $row['reservation'];
			$this->upcoming_arrivals = $row['upcoming_arrivals'];
			$this->arrival = $row['arrival'];
			$this->occupied_list = $row['occupied_list'];
			$this->deleted_reservation_list = $row['deleted_reservation_list'];
			$this->sales_service = $row['sales_service'];
			$this->stock_in = $row['stock_in'];
			$this->stock_out = $row['stock_out'];
			$this->stock_in_report = $row['stock_in_report'];
			$this->stock_out_report = $row['stock_out_report'];
			$this->stock_balance_report = $row['stock_balance_report'];
			$this->purchase = $row['purchase'];
			$this->purchase_report = $row['purchase_report'];
			$this->monthly_report = $row['monthly_report'];
			$this->cash_book_report = $row['cash_book_report'];
			$this->coa = $row['coa'];
			$this->expense = $row['expense'];
			$this->income = $row['income'];
			$this->expense_report = $row['expense_report'];
			$this->income_report = $row['income_report'];
			$this->attendance_report = $row['attendance_report'];
			$this->guest_report = $row['guest_report'];
			$this->arrival_report = $row['arrival_report'];
			$this->departure_report = $row['departure_report'];
		}else{
			$this->id = null; 
			$this->rolename = null; 
			$this->create_role = null; 
			$this->employee = null; 
			$this->create_account = null; 
			$this->change_password = null; 
			$this->investor = null; 
			$this->room = null; 
			$this->agent = null; 
			$this->item = null; 
			$this->duty_schedule = null; 
			$this->attendance = null; 
			$this->attendance_update = null; 
			$this->reservation = null; 
			$this->upcoming_arrivals = null; 
			$this->arrival = null; 
			$this->occupied_list = null;
			$this->deleted_reservation_list = null; 
			$this->sales_service = null; 
			$this->stock_in = null; 
			$this->stock_out = null; 
			$this->stock_in_report = null; 
			$this->stock_out_report = null; 
			$this->stock_balance_report = null; 
			$this->purchase = null; 
			$this->purchase_report = null; 
			$this->monthly_report = null; 
			$this->cash_book_report = null; 
			$this->coa = null; 
			$this->expense = null; 
			$this->income = null; 
			$this->expense_report = null; 
			$this->income_report = null; 
			$this->attendance_report = null; 
			$this->guest_report = null; 
			$this->arrival_report = null; 
			$this->departure_report = null;

		}
		
		
	}
	
	function create() {
		$query = "INSERT INTO " . $this->table_name . " SET 
			rolename = :rolename,
			create_role = :create_role,
			employee = :employee,
			create_account = :create_account,
			change_password = :change_password,
			investor = :investor,
			vehicle = :room,
			agent = :agent,
			item = :item,
			duty_schedule = :duty_schedule,
			attendance = :attendance,
			attendance_update = :attendance_update,
			booking = :reservation,
			upcoming_pickups = :upcoming_arrivals,
			pickup = :arrival,
			active_rentals = :occupied_list,
			deleted_booking_list = :deleted_reservation_list,
			sales_service = :sales_service,
			stock_in = :stock_in,
			stock_out = :stock_out,
			stock_in_report = :stock_in_report,
			stock_out_report = :stock_out_report,
			stock_balance_report = :stock_balance_report,
			purchase = :purchase,
			purchase_report = :purchase_report,
			monthly_report = :monthly_report,
			cash_book_report = :cash_book_report,
			coa = :coa,
			expense = :expense,
			income = :income,
			expense_report = :expense_report,
			income_report = :income_report,
			attendance_report = :attendance_report,
			customer_report = :guest_report,
			pickup_report = :arrival_report,
			return_report = :departure_report";
	
		$stmt = $this->conn->prepare($query);
	
		// Bind parameters
		$stmt->bindParam(":rolename", $this->rolename);
		$stmt->bindParam(":create_role", $this->create_role);
		$stmt->bindParam(":employee", $this->employee);
		$stmt->bindParam(":create_account", $this->create_account);
		$stmt->bindParam(":change_password", $this->change_password);
		$stmt->bindParam(":investor", $this->investor);
		$stmt->bindParam(":room", $this->room);
		$stmt->bindParam(":agent", $this->agent);
		$stmt->bindParam(":item", $this->item);
		$stmt->bindParam(":duty_schedule", $this->duty_schedule);
		$stmt->bindParam(":attendance", $this->attendance);
		$stmt->bindParam(":attendance_update", $this->attendance_update);
		$stmt->bindParam(":reservation", $this->reservation);
		$stmt->bindParam(":upcoming_arrivals", $this->upcoming_arrivals);
		$stmt->bindParam(":arrival", $this->arrival);
		$stmt->bindParam(":occupied_list", $this->occupied_list);
		$stmt->bindParam(":deleted_reservation_list", $this->deleted_reservation_list);
		$stmt->bindParam(":sales_service", $this->sales_service);
		$stmt->bindParam(":stock_in", $this->stock_in);
		$stmt->bindParam(":stock_out", $this->stock_out);
		$stmt->bindParam(":stock_in_report", $this->stock_in_report);
		$stmt->bindParam(":stock_out_report", $this->stock_out_report);
		$stmt->bindParam(":stock_balance_report", $this->stock_balance_report);
		$stmt->bindParam(":purchase", $this->purchase);
		$stmt->bindParam(":purchase_report", $this->purchase_report);
		$stmt->bindParam(":monthly_report", $this->monthly_report);
		$stmt->bindParam(":cash_book_report", $this->cash_book_report);
		$stmt->bindParam(":coa", $this->coa);
		$stmt->bindParam(":expense", $this->expense);
		$stmt->bindParam(":income", $this->income);
		$stmt->bindParam(":expense_report", $this->expense_report);
		$stmt->bindParam(":income_report", $this->income_report);
		$stmt->bindParam(":attendance_report", $this->attendance_report);
		$stmt->bindParam(":guest_report", $this->guest_report);
		$stmt->bindParam(":arrival_report", $this->arrival_report);
		$stmt->bindParam(":departure_report", $this->departure_report);
	
		// Execute query
		if ($stmt->execute()) {
			return true;
		}
		return false;
	}
	

	function update() {
		$query = "UPDATE " . $this->table_name . " SET 
			rolename = :rolename,
			create_role = :create_role,
			employee = :employee,
			create_account = :create_account,
			change_password = :change_password,
			investor = :investor,
			vehicle = :room,
			agent = :agent,
			item = :item,
			duty_schedule = :duty_schedule,
			attendance = :attendance,
			attendance_update = :attendance_update,
			booking = :reservation,
			upcoming_pickups = :upcoming_arrivals,
			pickup = :arrival,
			active_rentals = :occupied_list,
			deleted_booking_list = :deleted_reservation_list,
			sales_service = :sales_service,
			stock_in = :stock_in,
			stock_out = :stock_out,
			stock_in_report = :stock_in_report,
			stock_out_report = :stock_out_report,
			stock_balance_report = :stock_balance_report,
			purchase = :purchase,
			purchase_report = :purchase_report,
			monthly_report = :monthly_report,
			cash_book_report = :cash_book_report,
			coa = :coa,
			expense = :expense,
			income = :income,
			expense_report = :expense_report,
			income_report = :income_report,
			attendance_report = :attendance_report,
			customer_report = :guest_report,
			pickup_report = :arrival_report,
			return_report = :departure_report
		WHERE id = :id";
	
		$stmt = $this->conn->prepare($query);
	
		// Bind parameters
		$stmt->bindParam(":id", $this->id);
		$stmt->bindParam(":rolename", $this->rolename);
		$stmt->bindParam(":create_role", $this->create_role);
		$stmt->bindParam(":employee", $this->employee);
		$stmt->bindParam(":create_account", $this->create_account);
		$stmt->bindParam(":change_password", $this->change_password);
		$stmt->bindParam(":investor", $this->investor);
		$stmt->bindParam(":room", $this->room);
		$stmt->bindParam(":agent", $this->agent);
		$stmt->bindParam(":item", $this->item);
		$stmt->bindParam(":duty_schedule", $this->duty_schedule);
		$stmt->bindParam(":attendance", $this->attendance);
		$stmt->bindParam(":attendance_update", $this->attendance_update);
		$stmt->bindParam(":reservation", $this->reservation);
		$stmt->bindParam(":upcoming_arrivals", $this->upcoming_arrivals);
		$stmt->bindParam(":arrival", $this->arrival);
		$stmt->bindParam(":occupied_list", $this->occupied_list);
		$stmt->bindParam(":deleted_reservation_list", $this->deleted_reservation_list);
		$stmt->bindParam(":sales_service", $this->sales_service);
		$stmt->bindParam(":stock_in", $this->stock_in);
		$stmt->bindParam(":stock_out", $this->stock_out);
		$stmt->bindParam(":stock_in_report", $this->stock_in_report);
		$stmt->bindParam(":stock_out_report", $this->stock_out_report);
		$stmt->bindParam(":stock_balance_report", $this->stock_balance_report);
		$stmt->bindParam(":purchase", $this->purchase);
		$stmt->bindParam(":purchase_report", $this->purchase_report);
		$stmt->bindParam(":monthly_report", $this->monthly_report);
		$stmt->bindParam(":cash_book_report", $this->cash_book_report);
		$stmt->bindParam(":coa", $this->coa);
		$stmt->bindParam(":expense", $this->expense);
		$stmt->bindParam(":income", $this->income);
		$stmt->bindParam(":expense_report", $this->expense_report);
		$stmt->bindParam(":income_report", $this->income_report);
		$stmt->bindParam(":attendance_report", $this->attendance_report);
		$stmt->bindParam(":guest_report", $this->guest_report);
		$stmt->bindParam(":arrival_report", $this->arrival_report);
		$stmt->bindParam(":departure_report", $this->departure_report);
	
		// Execute query
		if ($stmt->execute()) {
			return true;
		}
		return false;
	}
	
	
	function delete(){
		$query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
		$stmt = $this->conn->prepare($query);
		
		$stmt->bindParam(1, $this->id);
		if($stmt->execute()){
			return true;
		}
		return false;
	}

	function exist(){
		$query = "SELECT id FROM " . $this->table_name . " WHERE rolename = ?  LIMIT 0,1";
		$stmt = $this->conn->prepare( $query );
		$this->rolename=htmlspecialchars(strip_tags($this->rolename));
	 
		$stmt->bindParam(1, $this->rolename);
		
		$stmt->execute();
		if($stmt->rowCount()>0){
			return true;
		}
		return false;
	}


}
?>
