Introducing events - loans are now events, and there is also a service event type
This commit is contained in:
parent
caca5dddbd
commit
adc8d36bb0
31
database.sql
31
database.sql
@ -75,29 +75,34 @@ create table `user` (
|
|||||||
) character set utf8mb4,
|
) character set utf8mb4,
|
||||||
collate utf8mb4_unicode_ci;
|
collate utf8mb4_unicode_ci;
|
||||||
|
|
||||||
create table `loan` (
|
create table `event` (
|
||||||
`id` bigint(20) not null auto_increment,
|
`id` bigint(20) not null auto_increment,
|
||||||
primary key(`id`),
|
primary key(`id`),
|
||||||
`user` bigint(20) not null,
|
|
||||||
constraint `l_f_user`
|
|
||||||
foreign key(`user`) references `user`(`id`),
|
|
||||||
`product` bigint(20) not null,
|
`product` bigint(20) not null,
|
||||||
constraint `l_f_product`
|
constraint `e_f_product`
|
||||||
foreign key(`product`) references `product`(`id`),
|
foreign key(`product`) references `product`(`id`),
|
||||||
`starttime` bigint(20) not null,
|
`starttime` bigint(20) not null,
|
||||||
`endtime` bigint(20) not null,
|
|
||||||
`returntime` bigint(20) default null
|
`returntime` bigint(20) default null
|
||||||
) character set utf8mb4,
|
) character set utf8mb4,
|
||||||
collate utf8mb4_unicode_ci;
|
collate utf8mb4_unicode_ci;
|
||||||
|
|
||||||
|
create table `loan` (
|
||||||
|
`event` bigint(20) not null,
|
||||||
|
primary key(`event`),
|
||||||
|
constraint `l_f_event`
|
||||||
|
foreign key(`event`) references `event`(`id`),
|
||||||
|
`user` bigint(20) not null,
|
||||||
|
constraint `l_f_user`
|
||||||
|
foreign key(`user`) references `user`(`id`),
|
||||||
|
`endtime` bigint(20) not null
|
||||||
|
) character set utf8mb4,
|
||||||
|
collate utf8mb4_unicode_ci;
|
||||||
|
|
||||||
create table `service` (
|
create table `service` (
|
||||||
`id` bigint(20) not null auto_increment,
|
`event` bigint(20) not null,
|
||||||
primary key(`id`),
|
primary key(`event`),
|
||||||
`product` bigint(20) not null,
|
constraint `s_f_event`
|
||||||
constraint `s_f_product`
|
foreign key(`event`) references `event`(`id`)
|
||||||
foreign key(`product`) references `product`(`id`),
|
|
||||||
`starttime` bigint(20) not null,
|
|
||||||
`returntime` bigint(20) default null
|
|
||||||
) character set utf8mb4,
|
) character set utf8mb4,
|
||||||
collate utf8mb4_unicode_ci;
|
collate utf8mb4_unicode_ci;
|
||||||
|
|
||||||
|
104
include/Event.php
Normal file
104
include/Event.php
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
<?php
|
||||||
|
class Event {
|
||||||
|
protected $id = 0;
|
||||||
|
protected $product = 0;
|
||||||
|
protected $starttime = 0;
|
||||||
|
protected $returntime = null;
|
||||||
|
|
||||||
|
protected static function create_event($product) {
|
||||||
|
$status = $product->get_status();
|
||||||
|
if($status != 'available') {
|
||||||
|
$emsg = '';
|
||||||
|
$prod_id = $product->get_id();
|
||||||
|
switch($status) {
|
||||||
|
case 'on_loan':
|
||||||
|
case 'overdue':
|
||||||
|
$loan_id = $product->get_active_loan()->get_id();
|
||||||
|
$emsg = "Product $prod_id has an active "
|
||||||
|
. "loan (id $loan_id).";
|
||||||
|
break;
|
||||||
|
case 'discarded':
|
||||||
|
$emsg = "Product $prod_id has been discarded.";
|
||||||
|
break;
|
||||||
|
case 'service':
|
||||||
|
$service_id = $product->get_active_service()->get_id();
|
||||||
|
$emsg = "Product $prod_id is on service "
|
||||||
|
. "(id $service_id).";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
throw new Exception($emsg);
|
||||||
|
}
|
||||||
|
$now = time();
|
||||||
|
$insert = prepare('insert into
|
||||||
|
`event`(`product`, `starttime`)
|
||||||
|
values (?, ?)');
|
||||||
|
bind($insert, 'ii', $product->get_id(), $now);
|
||||||
|
execute($insert);
|
||||||
|
$event_id = $insert->insert_id;
|
||||||
|
return new Event($event_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __construct($id) {
|
||||||
|
$search = prepare('select `id` from `event`
|
||||||
|
where `id`=?');
|
||||||
|
bind($search, 'i', $id);
|
||||||
|
execute($search);
|
||||||
|
$result = result_single($search);
|
||||||
|
if($result === null) {
|
||||||
|
throw new Exception('Event does not exist.');
|
||||||
|
}
|
||||||
|
$this->id = $result['id'];
|
||||||
|
$this->update_fields();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function update_fields() {
|
||||||
|
$get = prepare('select * from `event` where `id`=?');
|
||||||
|
bind($get, 'i', $this->id);
|
||||||
|
execute($get);
|
||||||
|
$result = result_single($get);
|
||||||
|
$this->product = $result['product'];
|
||||||
|
$this->starttime = $result['starttime'];
|
||||||
|
$this->returntime = $result['returntime'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_id() {
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_product() {
|
||||||
|
return new Product($this->product);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_starttime() {
|
||||||
|
return $this->starttime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_returntime() {
|
||||||
|
return $this->returntime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function is_active() {
|
||||||
|
if($this->returntime === null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_status() {
|
||||||
|
$class = strtolower(get_class($this));
|
||||||
|
if($this->is_active()) {
|
||||||
|
return 'active_' . $class;
|
||||||
|
}
|
||||||
|
return 'inactive_' . $class;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function end() {
|
||||||
|
$now = time();
|
||||||
|
$query = prepare('update `event` set `returntime`=? where `id`=?');
|
||||||
|
bind($query, 'ii', $now, $this->id);
|
||||||
|
execute($query);
|
||||||
|
$this->returntime = $now;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
103
include/Loan.php
103
include/Loan.php
@ -1,111 +1,52 @@
|
|||||||
<?php
|
<?php
|
||||||
class Loan {
|
class Loan extends Event {
|
||||||
private $id = 0;
|
|
||||||
private $user = 0;
|
private $user = 0;
|
||||||
private $product = 0;
|
|
||||||
private $starttime = 0;
|
|
||||||
private $endtime = 0;
|
private $endtime = 0;
|
||||||
private $returntime = null;
|
|
||||||
|
|
||||||
public static function create_loan($user, $product, $endtime) {
|
public static function create_loan($user, $product, $endtime) {
|
||||||
$status = $product->get_status();
|
begin_trans();
|
||||||
if($status != 'available') {
|
$event = parent::create_event($product);
|
||||||
$emsg = '';
|
$event_id = $event->get_id();
|
||||||
$prod_id = $product->get_id();
|
$insert = prepare('insert into `loan`(`user`, `endtime`) values (?, ?)');
|
||||||
switch($status) {
|
bind($insert, 'ii', $user->get_id(), strtotime($endtime . ' 13:00'));
|
||||||
case 'on_loan':
|
|
||||||
case 'overdue':
|
|
||||||
$loan_id = $product->get_active_loan()->get_id();
|
|
||||||
$emsg = "Product $prod_id has an active ";
|
|
||||||
$emsg .= "loan (id $loan_id) already.";
|
|
||||||
break;
|
|
||||||
case 'discarded':
|
|
||||||
$emsg = "Product $prod_id has been discarded.";
|
|
||||||
break;
|
|
||||||
case 'service':
|
|
||||||
$service_id = $product->get_active_service()->get_id();
|
|
||||||
$emsg = "Product $prod_id is on service (id $service_id).";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
throw new Exception($emsg);
|
|
||||||
}
|
|
||||||
$now = time();
|
|
||||||
$insert = prepare('insert into
|
|
||||||
`loan`(`user`, `product`, `starttime`, `endtime`)
|
|
||||||
values (?, ?, ?, ?)');
|
|
||||||
bind($insert, 'iiii',
|
|
||||||
$user->get_id(), $product->get_id(),
|
|
||||||
$now, strtotime($endtime . ' 13:00'));
|
|
||||||
execute($insert);
|
execute($insert);
|
||||||
$loan_id = $insert->insert_id;
|
commit_trans();
|
||||||
return new Loan($loan_id);
|
return new Loan($event_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct($id) {
|
public function __construct($id) {
|
||||||
$search = prepare('select `id` from `loan`
|
parent::__construct($id);
|
||||||
where `id`=?');
|
$search = prepare('select * from `loan` where `event`=?');
|
||||||
bind($search, 'i', $id);
|
bind($search, 'i', $id);
|
||||||
execute($search);
|
execute($search);
|
||||||
$result = result_single($search);
|
$result = result_single($search);
|
||||||
if($result === null) {
|
if($result === null) {
|
||||||
throw new Exception('Loan does not exist.');
|
throw new Exception('Loan does not exist.');
|
||||||
}
|
}
|
||||||
$this->id = $result['id'];
|
|
||||||
$this->update_fields();
|
$this->update_fields();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function update_fields() {
|
protected function update_fields() {
|
||||||
$get = prepare('select * from `loan` where `id`=?');
|
parent::update_fields();
|
||||||
|
$get = prepare('select * from `loan` where `event`=?');
|
||||||
bind($get, 'i', $this->id);
|
bind($get, 'i', $this->id);
|
||||||
execute($get);
|
execute($get);
|
||||||
$loan = result_single($get);
|
$loan = result_single($get);
|
||||||
$this->user = $loan['user'];
|
$this->user = $loan['user'];
|
||||||
$this->product = $loan['product'];
|
|
||||||
$this->starttime = $loan['starttime'];
|
|
||||||
$this->endtime = $loan['endtime'];
|
$this->endtime = $loan['endtime'];
|
||||||
$this->returntime = $loan['returntime'];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get_id() {
|
|
||||||
return $this->id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_user() {
|
public function get_user() {
|
||||||
return new User($this->user);
|
return new User($this->user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_product() {
|
public function get_endtime() {
|
||||||
return new Product($this->product);
|
return $this->endtime;
|
||||||
}
|
|
||||||
|
|
||||||
public function get_duration($format = true) {
|
|
||||||
$style = function($time) {
|
|
||||||
return $time;
|
|
||||||
};
|
|
||||||
if($format) {
|
|
||||||
$style = function($time) {
|
|
||||||
if($time) {
|
|
||||||
return gmdate('Y-m-d', $time);
|
|
||||||
}
|
|
||||||
return $time;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return array('start' => $style($this->starttime),
|
|
||||||
'end' => $style($this->endtime),
|
|
||||||
'end_renew' => $style($this->endtime + 604800), # +1 week
|
|
||||||
'return' => $style($this->returntime));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function is_active() {
|
|
||||||
if($this->returntime === null) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function extend($time) {
|
public function extend($time) {
|
||||||
$ts = strtotime($time . ' 13:00');
|
$ts = strtotime($time . ' 13:00');
|
||||||
$query = prepare('update `loan` set `endtime`=? where `id`=?');
|
$query = prepare('update `loan` set `endtime`=? where `event`=?');
|
||||||
bind($query, 'ii', $ts, $this->id);
|
bind($query, 'ii', $ts, $this->id);
|
||||||
execute($query);
|
execute($query);
|
||||||
$this->endtime = $ts;
|
$this->endtime = $ts;
|
||||||
@ -114,7 +55,7 @@ class Loan {
|
|||||||
|
|
||||||
public function end() {
|
public function end() {
|
||||||
$now = time();
|
$now = time();
|
||||||
$query = prepare('update `loan` set `returntime`=? where `id`=?');
|
$query = prepare('update `event` set `returntime`=? where `id`=?');
|
||||||
bind($query, 'ii', $now, $this->id);
|
bind($query, 'ii', $now, $this->id);
|
||||||
execute($query);
|
execute($query);
|
||||||
$this->returntime = $now;
|
$this->returntime = $now;
|
||||||
@ -131,5 +72,15 @@ class Loan {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function get_status() {
|
||||||
|
if($this->is_overdue()) {
|
||||||
|
return 'overdue_loan';
|
||||||
|
}
|
||||||
|
if($this->is_active()) {
|
||||||
|
return 'active_loan';
|
||||||
|
}
|
||||||
|
return 'inactive_loan';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
@ -1,109 +1,30 @@
|
|||||||
<?php
|
<?php
|
||||||
class Service {
|
class Service extends Event {
|
||||||
private $id = 0;
|
|
||||||
private $product = 0;
|
|
||||||
private $starttime = 0;
|
|
||||||
private $returntime = null;
|
|
||||||
|
|
||||||
public static function create_service($product) {
|
public static function create_service($product) {
|
||||||
$status = $product->get_status();
|
begin_trans();
|
||||||
if($status != 'available') {
|
$event = parent::create_event($product);
|
||||||
$emsg = '';
|
$event_id = $event->get_id();
|
||||||
$prod_id = $product->get_id();
|
$insert = prepare('insert into `service`(`event`) values (?)');
|
||||||
switch($status) {
|
bind($insert, 'i', $event_id);
|
||||||
case 'on_loan':
|
|
||||||
case 'overdue':
|
|
||||||
$loan_id = $product->get_active_loan()->get_id();
|
|
||||||
$emsg = "Product $prod_id has an active ";
|
|
||||||
$emsg .= "loan (id $loan_id).";
|
|
||||||
break;
|
|
||||||
case 'discarded':
|
|
||||||
$emsg = "Product $prod_id has been discarded.";
|
|
||||||
break;
|
|
||||||
case 'service':
|
|
||||||
$service_id = $product->get_active_service()->get_id();
|
|
||||||
$emsg = "Product $prod_id is on service "
|
|
||||||
."(id $service_id) already.";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
throw new Exception($emsg);
|
|
||||||
}
|
|
||||||
$now = time();
|
|
||||||
$insert = prepare('insert into
|
|
||||||
`service`(`product`, `starttime`)
|
|
||||||
values (?, ?)');
|
|
||||||
bind($insert, 'ii', $product->get_id(), $now);
|
|
||||||
execute($insert);
|
execute($insert);
|
||||||
$service_id = $insert->insert_id;
|
commit_trans();
|
||||||
return new Loan($service_id);
|
return new Service($event_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct($id) {
|
public function __construct($id) {
|
||||||
$search = prepare('select `id` from `service`
|
parent::__construct($id);
|
||||||
where `id`=?');
|
$search = prepare('select * from `service` where `event`=?');
|
||||||
bind($search, 'i', $id);
|
bind($search, 'i', $id);
|
||||||
execute($search);
|
execute($search);
|
||||||
$result = result_single($search);
|
$result = result_single($search);
|
||||||
if($result === null) {
|
if($result === null) {
|
||||||
throw new Exception('Service does not exist.');
|
throw new Exception('Service does not exist.');
|
||||||
}
|
}
|
||||||
$this->id = $result['id'];
|
|
||||||
$this->update_fields();
|
$this->update_fields();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function update_fields() {
|
|
||||||
$get = prepare('select * from `service` where `id`=?');
|
|
||||||
bind($get, 'i', $this->id);
|
|
||||||
execute($get);
|
|
||||||
$loan = result_single($get);
|
|
||||||
$this->product = $loan['product'];
|
|
||||||
$this->starttime = $loan['starttime'];
|
|
||||||
$this->returntime = $loan['returntime'];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get_id() {
|
protected function update_fields() {
|
||||||
return $this->id;
|
parent::update_fields();
|
||||||
}
|
|
||||||
|
|
||||||
public function get_user() {
|
|
||||||
return 'Service';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get_product() {
|
|
||||||
return new Product($this->product);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get_duration($format = true) {
|
|
||||||
$style = function($time) {
|
|
||||||
return $time;
|
|
||||||
};
|
|
||||||
if($format) {
|
|
||||||
$style = function($time) {
|
|
||||||
if($time) {
|
|
||||||
return gmdate('Y-m-d', $time);
|
|
||||||
}
|
|
||||||
return $time;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return array('start' => $style($this->starttime),
|
|
||||||
'end' => '',
|
|
||||||
'return' => $style($this->returntime));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function is_active() {
|
|
||||||
if($this->returntime === null) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function end() {
|
|
||||||
$now = time();
|
|
||||||
$query = prepare('update `service` set `returntime`=? where `id`=?');
|
|
||||||
bind($query, 'ii', $now, $this->id);
|
|
||||||
execute($query);
|
|
||||||
$this->returntime = $now;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user