made cart orderable
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 21 Oct 2007 15:08:16 +0000 (15:08 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 21 Oct 2007 15:08:16 +0000 (15:08 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@60 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

www/inc/cart.php
www/inc/db_scheme.php
www/inc/loader_nonadmin.php
www/inc/order.php [new file with mode: 0644]

index 6553b61..8fd4777 100644 (file)
 /**this class represents a bunch of tickets in the shopping cart, it is created by Cart*/
 class CartTicket
 {
+       private $ctid;
        private $cartid;
        private $eventid;
        private $amount;
        
        /**used by Cart to create the tickets, never use this directly*/
-       public function __construct($cid,$eid,$amt)
+       public function __construct($ctid,$cid,$eid,$amt)
        {
+               $this->ctid=$ctid;
                $this->cartid=$cid;
                $this->eventid=$eid;
                $this->amount=$amt;
        }
        
-       /**use this to increase or decrease the amount of tickets*/
+       /**use this to increase or decrease the amount of tickets; if the amount is decreased to zero, the row in the DB is deleted and the ticket can only be re-added with Cart::addTicket */
        public function changeAmount($amt)
        {
                global $db;
@@ -34,7 +36,7 @@ class CartTicket
                        $db->deleteRows("cart_ticket","cartid=".$db->escapeString($this->cartid)." and eventid=".$this->eventid);
                        $this->amount=0;
                }else{
-                       $db->update("cart_ticket",array("amount"=>($amt+0)),"cartid=".$db->escapeString($this->cartid)." and eventid=".$this->eventid);
+                       $db->update("cart_ticket",array("amount"=>($amt+0)),"ctid=".$db->escapeInt($this->ctid));
                        $this->amount=$amt;
                }
        }
@@ -58,6 +60,50 @@ class CartTicket
        }
 };
 
+/**this error is returned if there are no items in a cart and the user wants to order it*/
+define("CE_NOITEMS",1);
+/**this error is returned if the user tries to buy a ticket for a cancelled event*/
+define("CE_EVENTCANCELLED",10);
+/**this error is returned if the event does not have that many tickets left*/
+define("CE_EVENTNOTICKETS",11);
+/**this error is returned if the user wants to buy tickets for an unknown event (internal error?)*/
+define("CE_EVENTUNKNOWN",12);
+/**this error is returned if the event is already over or tickets cannot be purchased anymore*/
+define("CE_EVENTOVER",13);
+
+/**instantiated by Cart::orderCheck to report errors*/
+class CartError
+{
+       private $etype;
+       private $eventid;
+       
+       /**instantiates an error object of the given type and optionally for the given event*/
+       public function __construct($errtype,$eid=false)
+       {
+               $this->etype=$errtype;
+               $this->eventid=$eid;
+       }
+       
+       /**returns the error type (see CE_* constants)*/
+       public function errorType()
+       {
+               return $this->etype;
+       }
+       
+       /**returns the event id associated with this error (false if none)*/
+       public function eventId()
+       {
+               return $this->eventid;
+       }
+       
+       /**returns the Event object associated with this error (false if none)*/
+       public function eventObject()
+       {
+               if($this->eventid===false)return false;
+               return new Event($this->eventid);
+       }
+};
+
 /**this class represents a shopping cart*/
 class Cart
 {
@@ -110,18 +156,44 @@ class Cart
                $ret=array();
                if(count($res)>0)
                foreach($res as $k=>$tc)
-                       $ret[]=new CartTicket($tc["cartid"],$tc["eventid"],$tc["amount"]);
+                       $ret[]=new CartTicket($tc["ctid"],$tc["cartid"],$tc["eventid"],$tc["amount"]);
                return $ret;
        }
        
-       /**use this to add tickets*/
+       /**use this to add tickets, returns new CartTicket object or false if the event does not exist or is cancelled*/
        public function addTickets($eventid,$amount)
        {
                //sanity check
-               if($amount<=0)return;
-               //TODO: check that ticket can be sold
+               if($amount<=0)return false;
+               //check that ticket can be sold
+               $event=new Event($eventid);
+               if($event->isCancelled())return false;
                //insert into cart
-               $db->insert("cart_ticket",array());
+               $nid=$db->insert("cart_ticket",array("cartid"=>$this->cartid,"eventid"=$eventid,"amount"=>$amount));
+               if($nid===false)return false;
+               return new CartTicket($nid,$this->cartid,$eventid,$amount);
+       }
+       
+       /**checks that the whole content of the cart can be ordered; returns an empty array on success or an array of CartError objects on failure*/
+       public function orderCheck()
+       {
+               //TODO: extend to differentiate online, shop and direct sale
+               $ret=array();
+               //go through events
+               global $db;
+               $res=$db->select("cart_ticket","*","where cartid=".$db->escapeString($this->cartid));
+               if(count($res)>0)
+               foreach($res as $k=>$tc)
+                       $ret[]=new CartTicket($tc["ctid"],$tc["cartid"],$tc["eventid"],$tc["amount"]);
+               //vouchers are ok by default, just check amount
+               $itmcnt=count($res);
+               $res=$db->select("cart_voucher","cvid","where cartid=".$db->escapeString($this->cartid));
+               $itmcnt+=count($res);
+               //check that we have something to order
+               if($itmcnt<=0)
+                       $ret[]=new CartError(CE_NOITEMS);
+               //return...
+               return $ret;
        }
 };
 
index a2fabcc..43612e7 100644 (file)
@@ -245,16 +245,4 @@ define("TICKET_REVERSE",30);
 /**ticket has been cancelled*/
 define("TICKET_CANCELLED",40);
 
-/**an order has been placed*/
-define("ORDER_PLACED",0);
-/**the order has been sent out*/
-define("ORDER_SENT",10);
-/**the order has been paid*/
-define("ORDER_PAID",20);
-/**the order is to be reversed (it has been paid, but is to be cancelled)*/
-define("ORDER_REVERSE",30);
-/**the order has been cancelled*/
-define("ORDER_CANCELLED",40);
-
-
 ?>
\ No newline at end of file
index df76c9b..ce0d6d3 100644 (file)
@@ -6,4 +6,5 @@ if(!$db->canUseDb())
 include('./inc/event.php');
 include('./inc/room.php');
 include("./inc/random.php");
+include("./inc/order.php");
 ?>
\ No newline at end of file
diff --git a/www/inc/order.php b/www/inc/order.php
new file mode 100644 (file)
index 0000000..adffdcb
--- /dev/null
@@ -0,0 +1,127 @@
+<?
+//
+// PHP Implementation: order
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2007
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+/**an order has been placed, this flag is set when the order is filled and finalized*/
+define("ORDER_PLACED",1);
+/**the order has been sent out (it must be placed first; direct sales are automatically sent)*/
+define("ORDER_SENT",2);
+/**the order has been cancelled by the user (this is only possible as long as no money has been paid and nothing has been sent yet)*/
+define("ORDER_CANCELLED",4);
+/**the order is closed (optional: this flag means no further payment/cancellation/etc. is possible)*/
+define("ORDER_CLOSED",8);
+
+
+/**this class represents an order in the database*/
+class Order
+{
+       private $orderid;
+       private $status;
+       
+       /**instantiates an existing order with the given orderid or creates a new one if orderid===false*/
+       public function __construct($orderid=false)
+       {
+               global $db;
+               if($orderid===false){
+                       //create a new one
+                       $odr=array(
+                               //set to default
+                               "soldby"=>"_online",
+                               "status" => 0,
+                               "ordertime" => time()
+                       );
+                       $this->orderid=$db->insert("order",$odr);
+               }else{
+                       //get it from DB
+                       $res=$db->select("order","*","orderid=".$db->escapeInt($orderid));
+                       if(count($res)==0){
+                               $this->orderid=false;
+                               return;
+                       }
+                       $this->orderid=$res[0]["orderid"];
+                       $this->status=$res[0]["status"];
+               }
+       }
+       
+       /**returns whether the order can still be changed*/
+       public function canChange()
+       {
+               return $this->isValid() && $this->status == 0;
+       }
+       
+       /**returns whether the order is a valid DB object*/
+       public function isValid()
+       {
+               return $this->orderid!==false;
+       }
+
+       /**removes all items from the given Cart and enters them into itself; returns false if some items cannot be ordered or the order is already closed*/
+       public function emptyCart($cart)
+       {
+               //check carts contents
+               if(count($cart->orderCheck())>0)return false;
+               //check myself
+               if(!$this->canChange())return false;
+               //get tickets
+               $db->beginTransaction();
+               $tick=$cart->getTickets();
+               if(count($tick)>0)
+               foreach($tick as $k=>$tc){
+                       $this->addTickets($tc->getEventId(),$tc->getAmount());
+                       $tc->changeAmount(0);
+               }
+               //TODO: get vouchers
+               //done
+               $db->commitTransaction();
+               return true;
+       }
+       
+       /**adds some tickets to the order, returns ticketid or false if change is not possible*/
+       public function addTickets($eventid,$amount)
+       {
+               if(!$this->canChange() || $amount <= 0)return false;
+               global $db;
+               //get event
+               $event=new Event($eventid);
+               //create ticket
+               $tc=array("eventid" => $eventid,
+                       "price" => $event->getDefaultPrice(),
+                       "status" => 0,
+                       "oderid" => $this->orderid
+               );
+               $ret=array();
+               for($i=0;$i<$amount;$i++)$ret[]=$db->insert("ticket",$tc);
+               return $ret;
+       }
+       
+       /**places/finalizes the order; returns false on failure, true on success or if the order already was finalized()*/
+       public function placeOrder()
+       {
+               if(!$this->canChange())return;
+               global $db;
+               $db->beginTransaction();
+               //get orderstatus and correct it
+               $res=$db->select("order","status","orderid=".$db->escapeInt($this->orderid));
+               if(count($res)==0){
+                       $this->orderid=false;
+                       $db->rollbackTransaction();
+                       return false;
+               }
+               $db->update("order",array("status"=>ORDER_PLACED),"orderid=".$db->escapeInt($this->orderid));
+               $this->status=ORDER_PLACED;
+               //end
+               $db->commitTransaction();
+               return true;
+       }
+};
+
+?>
\ No newline at end of file