<commit_before><?php defined('BASEPATH') OR exit('No direct script access allowed');

class Reservation extends CI_Model{

	private $table = "rental_reservations";
	private $id = "reserve_id";
	private $date = "reserve_date";
	private $dateRented = "date_rented";
	private $dateReturned = "date_returned";
	private $totalAmt = "total_amt";
	private $downPayment = "down_payment";
	private $totalBalance = "total_balance";
	private $penalty = "penalty";
	private $status = "status";
	private $lesseeId = "lessee_id";
	private $subscriberId = "subscriber_id";

	private $data = array(
		"reserve_id" => "",
		"reserve_date" => "",
		"date_rented" => "",
		"date_returned" => "",
		"total_amt" => "",
		"down_payment" => "",
		"total_balance" => "",
		"penalty" => "",
		"status" => "",
		"lessee_id"=> "",
		"subscriber_id" => ""
	);

	public function clean() {
		foreach($this->data as $key => $value) {
			$this->data[$key] = "";
		}
	}

	public function create() {
		$data = array_filter($this->data);
		$this->db->insert($this->table, $data);
		$this->deleteCache();
		return $this->db->insert_id();
	}

	public function update() {
		$data = array_filter($this->data, function($value) {
    	return ($value !== null && $value !== false && $value !== ''); 
		});
		$this->db->update($this->table, $data, array(
				$this->id => $data[$this->id]
			)
		);
		$this->deleteCache();
		return $this->db->affected_rows();
	}

	public function delete($id) {
		$this->db->delete($this->table, array(
				$this->id => $id
			)
		);
		$this->deleteCache();
		return $this->db->affected_rows();	
	}

	public function all() {
		$query = $this->db->get($this->table);
		return $query->result();
	}

	public function find() {
		$data = array_filter($this->data);
		$query = $this->db->get_where($this->table, $data);
		return $query->result();
	}

	public function findComplete($where = "") {
		// $data = array_filter($this->data);
		$joinDetail = $this->_joinReservationDetail();
		$joinItem = $this->_joinItem();
		$joinShop = $this->_joinShop();
		$joinLessee = $this->_joinLessee();
		$select = $this->rAlias.'.*, ' . $this->rsAlias.'.*, ' . $this->lAlias . '.*, ' . $this->rAlias . '.status as "reserved_status"';
		if (!empty($where)) {
			$this->db->where($where);
		}
		$query = $this->db
			->select($select)
			->from($this->table . ' as ' . $this->rAlias)
			->join($joinLessee['table'], $joinLessee['on'])
			->join($joinDetail['table'], $joinDetail['on'])
			->join($joinItem['table'], $joinItem['on'])
			->join($joinShop['table'], $joinShop['on'], 'LEFT')
			->group_by($this->rAlias .'.'. $this->id)
			->get();
		return $query->result();
	}

	public function findById($id) {
		$query = $this->db->get_where($this->table, array($this->id => $id));
		return $query->row_array();
	}

	public function findBySubscriberId($id, $status = "") {
		$where = array($this->subscriberId => $id);
		if (!empty($status)) {
			$where[$this->status] = $status;
		}
		$query = $this->db->get_where($this->table, $where);
		return $query->result();
	}

	public function findActiveItemBySubscriberId($id) {
		$join = $this->_joinReservationDetail();
		$joinItem = $this->_joinItem();
		$joinLessee = $this->_joinLessee();
		$joinShop = $this->_joinShop();

		$where = $this->rAlias . '.'. $this->subscriberId . ' = ' . $id;
		$where .= " AND '" . date('Y-m-d H:i:s') . "'";
		$where .= " BETWEEN " . $this->rAlias . '.' . $this->getDateRented();
		$where .= " AND " . $this->rAlias . '.' . $this->getDateReturned();
		
		$data['count'] = $this->db
			->from($this->table . ' as ' . $this->rAlias)
			->join($join['table'], $join['on'])
			->join($joinLessee['table'], $joinLessee['on'], 'LEFT')<commit_msg>Add method findItemPenaltyById in Reservation<commit_after><?php defined('BASEPATH') OR exit('No direct script access allowed');

class Reservation extends CI_Model{

	private $table = "rental_reservations";
	private $id = "reserve_id";
	private $date = "reserve_date";
	private $dateRented = "date_rented";
	private $dateReturned = "date_returned";
	private $totalAmt = "total_amt";
	private $downPayment = "down_payment";
	private $totalBalance = "total_balance";
	private $penalty = "penalty";
	private $status = "status";
	private $lesseeId = "lessee_id";
	private $subscriberId = "subscriber_id";

	private $data = array(
		"reserve_id" => "",
		"reserve_date" => "",
		"date_rented" => "",
		"date_returned" => "",
		"total_amt" => "",
		"down_payment" => "",
		"total_balance" => "",
		"penalty" => "",
		"status" => "",
		"lessee_id"=> "",
		"subscriber_id" => ""
	);

	public function clean() {
		foreach($this->data as $key => $value) {
			$this->data[$key] = "";
		}
	}

	public function create() {
		$data = array_filter($this->data);
		$this->db->insert($this->table, $data);
		$this->deleteCache();
		return $this->db->insert_id();
	}

	public function update() {
		$data = array_filter($this->data, function($value) {
    	return ($value !== null && $value !== false && $value !== ''); 
		});
		$this->db->update($this->table, $data, array(
				$this->id => $data[$this->id]
			)
		);
		$this->deleteCache();
		return $this->db->affected_rows();
	}

	public function delete($id) {
		$this->db->delete($this->table, array(
				$this->id => $id
			)
		);
		$this->deleteCache();
		return $this->db->affected_rows();	
	}

	public function all() {
		$query = $this->db->get($this->table);
		return $query->result();
	}

	public function find() {
		$data = array_filter($this->data);
		$query = $this->db->get_where($this->table, $data);
		return $query->result();
	}

	public function findComplete($where = "") {
		// $data = array_filter($this->data);
		$joinDetail = $this->_joinReservationDetail();
		$joinItem = $this->_joinItem();
		$joinShop = $this->_joinShop();
		$joinLessee = $this->_joinLessee();
		$select = $this->rAlias.'.*, ' . $this->rsAlias.'.*, ' . $this->lAlias . '.*, ' . $this->rAlias . '.status as "reserved_status"';
		if (!empty($where)) {
			$this->db->where($where);
		}
		$query = $this->db
			->select($select)
			->from($this->table . ' as ' . $this->rAlias)
			->join($joinLessee['table'], $joinLessee['on'])
			->join($joinDetail['table'], $joinDetail['on'])
			->join($joinItem['table'], $joinItem['on'])
			->join($joinShop['table'], $joinShop['on'], 'LEFT')
			->group_by($this->rAlias .'.'. $this->id)
			->get();
		return $query->result();
	}

	public function findById($id) {
		$query = $this->db->get_where($this->table, array($this->id => $id));
		return $query->row_array();
	}

	public function findItemPenaltyById($id) {
		$joinDetail = $this->_joinReservationDetail();
		$joinItem = $this->_joinItem();

		$select = $this->rAlias . ".*, ";
		$select .= "(SELECT SUM({$this->iAlias}.{$this->Item->getPenalty()}) ";
		$select .= "from {$joinDetail['table']} JOIN {$joinItem['table']} ";
		$select .= "ON {$joinItem['on']} WHERE {$joinDetail['on']}";
		$select .= ") as total_penalty";

		$query = $this->db
			->select($select)
			->from($this->table . ' as ' . $this->rAlias)
			->where(array("{$this->rAlias}.{$this->id}" => $id))
			->get();

		return $query->row_array();
	}

	public function findBySubscriberId($id, $status = "") {
		$where = array($this->subscriberId => $id);
		if (!empty($status)) {
			$where[$this->status] = $status;
		}
		$query = $this->db->get_where($this->table, $where);
		return $query->result();
	}

	public function findActiveItemBySubscriberId($id) {
		$join = $this->_joinReservationDetail();
		$joinItem = $this->_joinItem();
		$joinLessee = $this->_joinLessee();
		$joinShop = $this->_joinShop();

		$where = $this->rAlias . '.'. $this->subscriberId . ' = ' . $id;
		$where .= " AND '" . date('Y-m-d H:i:s') . "'";
		$where .= " BETWEEN " . $this->rAlias . '.' . $this->getDateRented();
		$where .= " AND " . $this->rAlias . '.' . $this->getDateReturned();
		
		$data['count'] = $this->db
			->from($this->table . ' as ' . $this->rAlias)
			->join($join['table'], $join['on'])
			->join($joinLessee['table'], $joinLessee['on'], 'LEFT')
{'fraction_non_alphanumeric': 0.23983537041656272, 'fraction_numerical': 0.0, 'mean_word_length': 4.011875, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 224, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '800481', 'n_tokens_mistral': 3137, 'n_tokens_neox': 2867, 'n_words': 505}