*augment order/customer to work together
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 9 Mar 2008 18:51:57 +0000 (18:51 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 9 Mar 2008 18:51:57 +0000 (18:51 +0000)
*add some more customer info transactions

git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@110 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

www/inc/classes/customer.php
www/inc/classes/order.php
www/machine.php

index 8a92e26..cfc3483 100644 (file)
@@ -54,6 +54,14 @@ class Customer
                        return $this->id;
        }
        
+       /**returns whether the customer has a valid id*/
+       public function isValid()
+       {
+               if($this->id===false)return false;
+               if($this->id<0)return false;
+               return true;
+       }
+       
        /** returns the name of the customer */
        public function getName()
        {
@@ -222,7 +230,75 @@ function getCustomerListXml()
        $xml->appendChild($root);
        header("X-MagicSmoke-Status: Ok");
        print($xml->saveXML());
-       
+}
+
+/**machine interface: get a specific customer*/
+function getCustomerXml($cid)
+{
+       global $db;
+       //return customers
+       $res=$db->select("customer","*","customerid=".$db->escapeInt($cid));
+       if(count($res)>0){
+               $xml=new DOMDocument;
+               $cs=$xml->createElement("Customer");
+               $cs->setAttribute("id",$res[0]["customerid"]);
+               $cs->setAttribute("name",$res[0]["name"]);
+               $cs->appendChild($xml->createElement("Address",$res[0]["address"]));
+               $cs->appendChild($xml->createElement("Contact",$res[0]["contact"]));
+               $cs->appendChild($xml->createElement("Comment",$res[0]["comments"]));
+               $res=$db->select("webuser","email","customerid=".$db->escapeInt($cid));
+               if(count($res)>0)
+                       $cs->setAttribute("mail",$res[0]["email"]);
+               $xml->appendChild($cs);
+               header("X-MagicSmoke-Status: Ok");
+               print($xml->saveXML());
+       }else{
+               header("X-MagicSmoke-Status: Error");
+               die(tr("Unknown Customer"));
+       }
+}
+
+/**machine interface: set a customer*/
+function setCustomerXml($xmldata)
+{
+       //TODO:do more extensive syntax checking and better error reporting
+       //get XML
+       $xml=new DOMDocument;
+       if($xml->loadXML($xmldata)===false){
+               header("X-MagicSmoke-Status: SyntaxError");
+               die(tr("Unable to parse XML."));
+       }
+       //stage 2: extract data from XML
+       $doc=$xml->documentElement;
+       global $db;
+       //get data
+       if($doc->hasAttribute("id"))
+               $id=$doc->getAttribute("id")+0;
+       else $id=-1;
+       $dt["name"]=$doc->getAttribute("name");
+       $dt["address"]="";
+       $dt["contact"]="";
+       $dt["comments"]="";
+       foreach($doc->getElementsByTagName("Address") as $el)
+               foreach($el->childNodes as $cn)
+                       if($cn->nodeType==XML_TEXT_NODE)
+                               $dt["address"]=trim($cn->wholeText);
+       foreach($doc->getElementsByTagName("Contact") as $el)
+               foreach($el->childNodes as $cn)
+                       if($cn->nodeType==XML_TEXT_NODE)
+                               $dt["contact"]=trim($cn->wholeText);
+       foreach($doc->getElementsByTagName("Comment") as $el)
+               foreach($el->childNodes as $cn)
+                       if($cn->nodeType==XML_TEXT_NODE)
+                               $dt["comments"]=trim($cn->wholeText);
+                               
+       if($id>=0){
+               $db->update("customer",$dt,"customerid=".$db->escapeInt($id));
+       }else{
+               $dt["customerid"]=$id;
+               $db->insert("customer",$dt);
+       }
+       header("X-MagicSmoke-Status: Ok");
 }
 
 ?>
\ No newline at end of file
index adffdcb..be8c896 100644 (file)
@@ -25,7 +25,8 @@ define("ORDER_CLOSED",8);
 class Order
 {
        private $orderid;
-       private $status;
+       private $status=false;
+       private $customerid=false;
        
        /**instantiates an existing order with the given orderid or creates a new one if orderid===false*/
        public function __construct($orderid=false)
@@ -49,6 +50,7 @@ class Order
                        }
                        $this->orderid=$res[0]["orderid"];
                        $this->status=$res[0]["status"];
+                       $this->customerid=$res[0]["customerid"];
                }
        }
        
@@ -92,17 +94,27 @@ class Order
                global $db;
                //get event
                $event=new Event($eventid);
+               //check that there are tickets available
+               if($event->availableTicketAmount()<$amount)return false;
                //create ticket
                $tc=array("eventid" => $eventid,
                        "price" => $event->getDefaultPrice(),
                        "status" => 0,
-                       "oderid" => $this->orderid
+                       "orderid" => $this->orderid
                );
                $ret=array();
                for($i=0;$i<$amount;$i++)$ret[]=$db->insert("ticket",$tc);
                return $ret;
        }
        
+       /**sets the customer of this order; returns 1 on success, false or 0 on failure*/
+       public function setCustomer($cust)
+       {
+               global $db;
+               if(!$this->canChange() || !$cust->isValid())return false;
+               return $db->update("order",array("customerid"=>$cust->getID()),"orderid=".$db->escapeInt($this->orderid));
+       }
+       
        /**places/finalizes the order; returns false on failure, true on success or if the order already was finalized()*/
        public function placeOrder()
        {
@@ -124,4 +136,9 @@ class Order
        }
 };
 
+function createOrderXml($xmldata,$ispaid)
+{
+       //parse XML data
+}
+
 ?>
\ No newline at end of file
index f13ff5d..89f672d 100644 (file)
@@ -22,7 +22,7 @@ $ALLOWEDREQUESTS=array(
        tr("gethosts"),tr("sethost"),tr("addhost"),tr("deletehost"), //host management
        tr("geteventlist"),tr("geteventdata"),tr("seteventdata"), //event infos
        tr("getroomdata"),tr("setroomdata"),//room infos
-       tr("getcustomerlist"), //customer info
+       tr("getcustomerlist"),tr("getcustomer"),tr("setcustomer"), //customer info
        tr("createorder"),tr("createsale"), //sell/order stuff
        tr("gettemplatelist"),tr("gettemplate"),tr("settemplate") //templates
 );
@@ -242,9 +242,27 @@ if($SMOKEREQUEST=="getcustomerlist"){
        exit();
 }
 
+//get a specific customer
+if($SMOKEREQUEST=="getcustomer"){
+       getCustomerXml(trim($REQUESTDATA));
+       exit();
+}
+
+//set/create a specific customer
+if($SMOKEREQUEST=="setcustomer"){
+       setCustomerXml($REQUESTDATA);
+       exit();
+}
 
-if($SMOKEREQUEST=="createorder"){}
-if($SMOKEREQUEST=="createsale"){}
+
+if($SMOKEREQUEST=="createorder"){
+       createOrderXml($REQUESTDATA,false);
+       exit();
+}
+if($SMOKEREQUEST=="createsale"){
+       createOrderXml($REQUESTDATA,true);
+       exit();
+}
 
 
 //EOF