boka3/include/Loan.php
Erik Thuning fa46874ae3 Mail notifications get pushed forward in time when a new loan is created if there is a pending notification.
This way the user can add multiple loans over time without the notification getting sent prematurely.
2023-01-11 10:43:57 +01:00

148 lines
4.5 KiB
PHP

<?php
class Loan extends Event {
private $user = 0;
private $endtime = 0;
public static function create_loan($user, $product, $endtime) {
begin_trans();
$event = parent::create_event($product, 'loan');
$event_id = $event->get_id();
$insert = prepare('insert into `loan`(`event`, `user`, `endtime`)
values (?, ?, ?)');
$endtime .= '13:00';
bind($insert, 'iii', $event_id, $user->get_id(), strtotime($endtime));
execute($insert);
$loan = new Loan($event_id);
$loan->queue_receipt($user);
commit_trans();
return $loan;
}
public function __construct($id) {
parent::__construct($id);
$search = prepare('select * from `loan` where `event`=?');
bind($search, 'i', $id);
execute($search);
$result = result_single($search);
if($result === null) {
throw new Exception('Loan does not exist.');
}
$this->update_fields();
}
protected function update_fields() {
parent::update_fields();
$get = prepare('select * from `loan` where `event`=?');
bind($get, 'i', $this->get_id());
execute($get);
$loan = result_single($get);
$this->user = $loan['user'];
$this->endtime = $loan['endtime'];
}
protected function queue_receipt($user) {
$now = time();
$sendtime = $now + 3600;
$pending = prepare('select * from `pending_receipt` where `user` = ?
and `since_time` < ? and `send_time` > ?');
bind($pending, 'iii', $user->get_id(), $now, $now);
execute($pending);
$result = result_single($pending);
if($result === null) {
$add = prepare('insert into `pending_receipt`
(`user`, `since_time`, `send_time`)
values(?, ?, ?)');
bind($add, 'iii', $user->get_id(), $now, $sendtime);
execute($add);
} else {
$update = prepare('update `pending_receipt` set `send_time` = ?
where `user` = ? and `since_time` < ?
and `send_time` > ?');
bind($update, 'iiii', $sendtime, $user->get_id(), $now, $now);
execute($update);
}
}
public function get_user() {
return new User($this->user);
}
public function get_endtime() {
return $this->endtime;
}
public function extend($time) {
$oldend = $this->get_endtime();
$now = time();
$ts = strtotime($time . ' 13:00');
begin_trans();
$extend = prepare('update `loan` set `endtime`=? where `event`=?');
bind($extend, 'ii', $ts, $this->get_id());
execute($extend);
$log = prepare('insert into `loan_extension`
(`loan`, `extend_time`, `old_end`, `new_end`)
values (?, ?, ?, ?)');
bind($log, 'iiii', $this->get_id(), $now, $oldend, $ts);
execute($log);
$this->queue_receipt($this->get_user());
$this->endtime = $ts;
commit_trans();
return true;
}
public function get_last_extension() {
$select = prepare('select max(`extend_time`) as `extend_time`
from `loan_extension` where `loan`=?');
bind($select, 'i', $this->get_id());
execute($select);
return result_single($select)['extend_time'];
}
public function end() {
$now = time();
$query = prepare('update `event` set `returntime`=? where `id`=?');
bind($query, 'ii', $now, $this->get_id());
execute($query);
$this->returntime = $now;
return true;
}
public function is_overdue() {
if($this->returntime !== null) {
return false;
}
$now = time();
if($now > $this->endtime) {
return true;
}
return false;
}
public function expires_before($datetime) {
if($this->returntime !== null) {
return false;
}
$endtime = new DateTime();
$endtime->setTimestamp($this->endtime);
if(!$this->is_overdue() && $endtime < $datetime) {
return true;
}
return false;
}
public function get_status() {
if($this->is_overdue()) {
return 'overdue_loan';
}
if($this->is_active()) {
return 'active_loan';
}
return 'inactive_loan';
}
}
?>