/**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
// print("a");
global $db,$session;
$db->beginTransaction();
- if(!$this->validateOrder()){
+ if(!$this->validateOrder($isSale?VALIDATEORDER_SALE:VALIDATEORDER_ORDER)){
$db->rollbackTransaction();
return false;
}
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";
//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;
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
}
$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
$ret=false;
continue;
}
+ //check whether enough tickets are available
$avail=$evt->availableTicketAmount();
if($avail<$amount){
if($dumpxml===false)return false;
$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);
//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));
$xml->appendChild($ord);
print($xml->saveXml());
}
+ //return result of tests
return $ret;
}
case "check":
// check order
header("X-MagicSmoke-Status: Ok");
- $order->validateOrder(true);
+ $order->validateOrder(VALIDATEORDER_XML);
break;
case "order":
// create order
$order->dumpXml();
}else{
header("X-MagicSmoke-Status: Error");
- die(tr("Cannot place order, sorry."));
+ die(tr("Cannot place sale, sorry."));
}
break;
default: