private $type;
private $status;
+ public function id(){
+ if(!isset($this->id)){
+ throw new Exception('Invalid Operation');
+ }
+ return $this->id;
+ }
+
// STATUS: 0 - FREE, 1 - ALLOCATED, 2 - SOLD
public static function Get_Free_Count_By_Type($type){
if($type instanceof Type){
$type = $type->id();
}
- $query = 'SELECT COUNT(*) FROM `ticket` WHERE `type` = ?';
+ $query = 'SELECT COUNT(*) FROM `ticket` WHERE `type` = ? AND `status` = 0';
$stmt = Ticketing::Get()->database()->prepare($query);
$stmt->bind_param('i', $type);
$stmt->execute();
return $count;
}
-}
+ public static function Allocate_Available($type){
+ if($type instanceof Type){
+ $type = $type->id();
+ }
+ $query = 'SELECT `id`,`type`,`status` FROM `ticket` WHERE `type` = ? AND `status` = 0 LIMIT 1';
+ $stmt = Ticketing::Get()->database()->prepare($query);
+ $stmt->bind_param('i', $type);
+ $stmt->execute();
+ $stmt->bind_result($id, $type, $status);
+ if($stmt->fetch() == null){
+ throw new Exception('No available tickets!');
+ }
+ $ticket = new Ticket($id, $type, $status);
+ $query = 'UPDATE `ticket` SET `status` = 1 WHERE `id` = ?';
+ $stmt->bind_param('i', $id);
+ $stmt->execute();
+ return $ticket;
+ }
+
+class Purchase {
+
+ private $id;
+ private $ticket;
+ private $order;
+ private $price;
+ private $name;
+
+ public static function Create($order, $type, $name){
+ $ticket = Ticket::Allocate_Available($type);
+ $query = 'INSERT INTO `purchase` SET `order` = ?, `ticket` = ?, `price` = ?, `name` = ?';
+ $stmt = Ticketing::Get()->database()->prepare($query);
+ $stmt->bind_param('iiiis', $order->id(), $ticket->id(), $type->price(), $name);
+ $stmt->execute();
+ $purchase = new Purchase($stmt->insert_id, $order->id(), $ticket->id(), $type->price(), $name);
+
+
+
+ }
+
+
class Order {
private $id;
private $user;
- private $amount;
+ private $time;
private $status;
+ public function id(){
+ if(!isset($this->id)){
+ throw new Exception('Invalid Operation');
+ }
+ return $this->id;
+ }
// STATUS: 0 - IN PROGRESS, 1 - CONFIRMED, 2 - PAID
public static function Get_Confirmed_By_User($user){
return $orders;
}
+ public static function Unconfirmed_By_User($user){
+ $order = Order::Get_Unconfirmed_By_User($user);
+ if($order == null){
+ $query = 'INSERT INTO `order` SET `user` = ? AND `status` = 0 AND time = ?';
+ $stmt = Ticketing::Get()->database()->prepare($query);
+ $time = time();
+ $stmt->bind_param('si', $user, $time);
+ $stmt->execute();
+ $order = new Order($stmt->insert_id, $user, $time, 0);
+ }
+ return $order;
+ }
+
public static function Get_Unconfirmed_By_User($user){
$query = 'SELECT `id`, `user`, `time`, `status` FROM `order` WHERE `user` = ? AND `status` = 0';
$stmt = Ticketing::Get()->database()->prepare($query);
$stmt->bind_result($id, $user, $time, $status);
$orders = array();
while($stmt->fetch()){
- $orders[$id] = new Order($id, $user, $amount, $status);
+ $orders[$id] = new Order($id, $user, $time, $status);
}
$order = array_shift($orders);
foreach($orders as $o){
return $order;
}
+ public function order($type, $name){
+ Purchase::Create($this, $type, $name);
+ $query = 'UPDATE `order` SET `time` = ? WHERE `id` = ?';
+ $stmt = Ticketing::Get()->database()->prepare($query);
+ $time = time();
+ $stmt->bind_param('ii', $time, $this->id);
+ $stmt->execute();
+ }
+
private function merge(){
throw new Exception('Unsupported');
}