fix: validateOrder needs to differ between order and sale
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Fri, 1 Aug 2008 10:41:49 +0000 (10:41 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Fri, 1 Aug 2008 10:41:49 +0000 (10:41 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@166 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/overview.cpp
www/inc/classes/order.php

index a7ddc07..e65d9c2 100644 (file)
@@ -707,6 +707,7 @@ void MOverview::initCart()
        cartmodel->setHorizontalHeaderLabels(QStringList()<<tr("Amount")<<tr("Title")<<tr("Start Time"));
        //clear customer
        customer=MCustomer();
+       cartcustomer->setText("");
        //clear address/comment
        cartaddr->setPlainText("");
        cartcomment->setPlainText("");
@@ -829,6 +830,8 @@ void MOverview::cartOrder()
        //display order and give user chance to actually order it
        MOrderWindow *ow=new MOrderWindow(this,req,MOrder(req,rdoc.documentElement()));
        ow->show();
+       //empty the cart
+       initCart();
 }
 
 void MOverview::customerMgmt()
index c1926f7..2b36516 100644 (file)
@@ -20,6 +20,14 @@ define("ORDER_CANCELLED",2);
 /**the order has been finalized; no more changes possible*/
 define("ORDER_CLOSED",10);
 
+/**order validation: output XML*/
+define("VALIDATEORDER_XML",1);
+/**order validation: return whether it can be ordered (no XML)*/
+define("VALIDATEORDER_ORDER",2);
+/**order validation: return whether it can be sold (no XML)*/
+define("VALIDATEORDER_SALE",3);
+
+
 
 /**this class represents an order in the database*/
 class Order
@@ -144,7 +152,7 @@ class Order
 //             print("a");
                global $db,$session;
                $db->beginTransaction();
-               if(!$this->validateOrder()){
+               if(!$this->validateOrder($isSale?VALIDATEORDER_SALE:VALIDATEORDER_ORDER)){
                        $db->rollbackTransaction();
                        return false;
                }
@@ -189,10 +197,12 @@ class Order
                return true;
        }
        
-       /**validates the order against the database; returns whether it can be opened as an order; prints an order object fit for checkorder if $dumpxml is true*/
-       public function validateOrder($dumpxml=false)
+       /**validates the order against the database; returns whether it can be opened as an order; $mode must be one of the VALIDATEORDER_* constants; prints an order object fit for checkorder if $mode is VALIDATEORDER_XML*/
+       public function validateOrder($mode)
        {
                global $db;
+               $dumpxml=$mode==VALIDATEORDER_XML;
+               $isSale=$mode==VALIDATEORDER_SALE;
                $ret=true;
                $price=0;
                $ostat="ok";
@@ -201,6 +211,7 @@ class Order
                //check customer
                $res=$db->select("customer","customerid","customerid=".$db->escapeInt($this->customerid));
                if(count($res)<1){
+                       //no customer: cannot work
                        if($dumpxml===false)return false;
                        $ostat="fail";
                        $ret=false;
@@ -216,6 +227,7 @@ class Order
                foreach($this->newtickets as $evid => $tcs){
                        $amount=count($tcs);
                        $evt=new Event($evid);
+                       //check whether the event exists
                        if(!$evt->exists()){
                                if($dumpxml===false)return false;
                                //create only one ticket and make it sound negative
@@ -230,26 +242,42 @@ class Order
                        }
                        $stime=$evt->getStartTime();
                        $estat="ok";
+                       //check whether we can still order
                        if(($stime-$orderstop)<=$curtime){
-                               if($dumpxml===false)return false;
                                $estat="saleonly";
-                               $ret=false;
+                               //if this is an order: can't do it
+                               if(!$isSale){
+                                       $ret=false;
+                                       if($dumpxml===false)
+                                               return false;
+                               }
                        }
-                       if(($stime-$salestop)<=$curtime)
+                       //check whether we can still sell
+                       if(($stime-$salestop)<=$curtime){
                                if($estat=="ok")$estat="orderonly";
                                else $estat="toolate";
+                               //if this is a sale: can't do it
+                               if($isSale){
+                                       $ret=false;
+                                       if($dumpxml===false)
+                                               return false;
+                               }
+                       }
+                       //create matching error XML for order/sale failure
                        if($estat!="ok"){
                                //create only one ticket and make it sound negative
                                $ev=$xml->createElement("Ticket");
                                $ev->setAttribute("event",$evid);
                                $ev->setAttribute("status",$estat);
                                $ev->setAttribute("id",$ftid++);
+                               if($estat!="toolate")
+                                       $ev->setAttribute("price",$evt->getDefaultPrice());
                                $ord->appendChild($ev);
                                if($ostat=="ok")$ostat=$estat;
                                else if($ostat!=$estat)$ostat="fail";
-                               $ret=false;
                                continue;
                        }
+                       //check whether event is cancelled
                        if($evt->isCancelled()){
                                if($dumpxml===false)return false;
                                //create only one ticket and make it sound negative
@@ -261,6 +289,7 @@ class Order
                                $ret=false;
                                continue;
                        }
+                       //check whether enough tickets are available
                        $avail=$evt->availableTicketAmount();
                        if($avail<$amount){
                                if($dumpxml===false)return false;
@@ -283,7 +312,7 @@ class Order
                                $ret=false;
                                continue;
                        }
-                       //finally create good ones
+                       //survived all tests: finally create good tickets
                        for($i=0;$i<$amount;$i++){
                                $ev=$xml->createElement("Ticket");
                                $ev->setAttribute("event",$evid);
@@ -297,7 +326,7 @@ class Order
                
                //TODO: check vouchers
                
-               //add other data
+               //add other data and dump XML
                if($dumpxml){
                        $ord->appendChild($xml->createElement("DeliveryAddress",$this->deliveryaddress));
                        $ord->appendChild($xml->createElement("Comment",$this->comment));
@@ -306,6 +335,7 @@ class Order
                        $xml->appendChild($ord);
                        print($xml->saveXml());
                }
+               //return result of tests
                return $ret;
        }
        
@@ -474,7 +504,7 @@ function createOrderXml($xmldata,$action)
                case "check":
                        // check order
                        header("X-MagicSmoke-Status: Ok");
-                       $order->validateOrder(true);
+                       $order->validateOrder(VALIDATEORDER_XML);
                        break;
                case "order":
                        // create order
@@ -495,7 +525,7 @@ function createOrderXml($xmldata,$action)
                                $order->dumpXml();
                        }else{
                                header("X-MagicSmoke-Status: Error");
-                               die(tr("Cannot place order, sorry."));
+                               die(tr("Cannot place sale, sorry."));
                        }
                        break;
                default: