Added ticket purchasing
authorRichard Whitehouse <richardwhiuk@richardwhiuk.com>
Fri, 4 Feb 2011 18:37:10 +0000 (18:37 +0000)
committerRichard Whitehouse <richardwhiuk@richardwhiuk.com>
Fri, 4 Feb 2011 18:37:10 +0000 (18:37 +0000)
index.php
pages/add.php

index 1b9a232f669c8b164218e9811489bfdf93f83182..aa945276480070c46d2867ac3df4fcb138364742 100644 (file)
--- a/index.php
+++ b/index.php
@@ -181,13 +181,20 @@ class Ticket {
        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();
@@ -198,15 +205,60 @@ class Ticket {
                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){
@@ -223,6 +275,19 @@ class Order {
                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);
@@ -231,7 +296,7 @@ class Order {
                $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){
@@ -240,6 +305,15 @@ class Order {
                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');
        }
index de7937e1ca80a8b67205465dbaecebfd37b0baab..467f446a55bb04ef76f028023fdffddb741e773a 100644 (file)
@@ -42,7 +42,8 @@ class Page_Add extends Page {
                                }
                                
                                if(!$this->error){
-                                       $current = Order::Get_Unconfirmed_By_User($system->user());
+                                       $current = Order::Unconfirmed_By_User($system->user());
+                                       $current->order($type, $this->name);
                                        var_dump($this);
                                        exit;
                                }