*Split customer table into two
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 9 Mar 2008 13:32:46 +0000 (13:32 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 9 Mar 2008 13:32:46 +0000 (13:32 +0000)
*started to provide customer data to GUI

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

doc/prog_protocol.html
src/customer.cpp [new file with mode: 0644]
src/customer.h [new file with mode: 0644]
src/overview.cpp
src/overview.h
src/smoke.pro
src/webrequest.cpp
src/webrequest.h
www/inc/classes/customer.php
www/inc/db/db_scheme.php
www/machine.php

index 7202895..f22d5a0 100644 (file)
@@ -361,6 +361,17 @@ FIXME: special hosts (beginning with "_") return errors. Non-existing hosts are
 
 <h2>Customers</h2>
 
+<h3>Getting List of Customers</h3>
+
+The <tt>getcustomerlist</tt> transaction returns a short list of customers. The request is empty. The response returns this:
+
+<pre>
+&lt;CustomerList>
+  &lt;Customer id="integer ID" name="full name"/>
+  ...
+&lt;/CustomerList>
+</pre>
+
 <h2>Orders and Sales</h2>
 
 <h2>Templates</h2>
diff --git a/src/customer.cpp b/src/customer.cpp
new file mode 100644 (file)
index 0000000..2b0fd25
--- /dev/null
@@ -0,0 +1,147 @@
+//
+// C++ Implementation: customer
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2008
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+#include "customer.h"
+#include "webrequest.h"
+
+#include <QDomElement>
+#include <QPushButton>
+#include <QListView>
+#include <QStandardItemModel>
+#include <QBoxLayout>
+
+MCustomer::MCustomer()
+{
+       m_req=0;
+       m_id=-1;
+}
+
+MCustomer::MCustomer(MWebRequest*r,const QDomElement&el)
+{
+       m_req=r;
+       bool b;
+       m_id=el.attribute("id","-1").toInt(&b);
+       if(!b)m_id=-1;
+       m_name=el.attribute("name");
+}
+
+MCustomer::MCustomer(const MCustomer&c)
+{
+       m_req=c.m_req;
+       m_id=c.m_id;
+       m_name=c.m_name;
+}
+
+MCustomer& MCustomer::operator=(const MCustomer&c)
+{
+       m_req=c.m_req;
+       m_id=c.m_id;
+       m_name=c.m_name;
+       return *this;
+}
+
+qint64 MCustomer::customerID()
+{
+       return m_id;
+}
+
+bool MCustomer::isValid()
+{
+       return m_id>-1;
+}
+
+QString MCustomer::name()
+{
+       return m_name;
+}
+
+QString MCustomer::getNameAddress()
+{
+       return m_name;
+}
+
+
+/*********************************************************/
+
+MCustomerListDialog::MCustomerListDialog(MWebRequest*r,QWidget*par,bool isselect,qint64 presel)
+       :QDialog(par)
+{
+       m_req=r;
+       if(isselect)
+               setWindowTitle(tr("Select a Customer"));
+       else
+               setWindowTitle(tr("Customers"));
+       
+       //layout
+       QVBoxLayout*vl,*vl2;
+       QHBoxLayout*hl;
+       setLayout(vl=new QVBoxLayout);
+       vl->addLayout(hl=new QHBoxLayout,10);
+       hl->addWidget(m_listview=new QListView,10);
+       m_listview->setModel(m_listmodel=new QStandardItemModel(this));
+       m_listview->setEditTriggers(QAbstractItemView::NoEditTriggers);
+       hl->addLayout(vl2=new QVBoxLayout,0);
+       QPushButton*p;
+       vl2->addWidget(p=new QPushButton(tr("Details...")),0);
+       vl2->addWidget(p=new QPushButton(tr("Create new...")),0);
+       vl2->addWidget(p=new QPushButton(tr("Delete...")),0);
+       vl2->addStretch(2);
+       vl->addSpacing(15);
+       vl->addLayout(hl=new QHBoxLayout,0);
+       hl->addStretch(10);
+       if(isselect){
+               hl->addWidget(p=new QPushButton(tr("Select")),0);
+               connect(p,SIGNAL(clicked()),this,SLOT(accept()));
+               hl->addWidget(p=new QPushButton(tr("Cancel")),0);
+               connect(p,SIGNAL(clicked()),this,SLOT(reject()));
+       }else{
+               hl->addWidget(p=new QPushButton(tr("Close")),0);
+               connect(p,SIGNAL(clicked()),this,SLOT(reject()));
+       }
+       
+       //update data
+       updateList();
+       if(presel>-1){
+               for(int i=0;i<m_listmodel->rowCount();i++){
+                       QModelIndex idx=m_listmodel->index(0,i);
+                       if(m_listmodel->data(idx,Qt::UserRole).toInt()==presel)
+                               m_listview->setCurrentIndex(idx);
+               }
+       }
+}
+
+void MCustomerListDialog::updateList()
+{
+       //go to server
+       m_list=m_req->getAllCustomers();
+       //update widget
+       m_listmodel->clear();
+       m_listmodel->insertRows(0,m_list.size());
+       m_listmodel->insertColumn(0);
+       for(int i=0;i<m_list.size();i++){
+               QModelIndex idx=m_listmodel->index(i,0);
+               m_listmodel->setData(idx,m_list[i].name());
+               m_listmodel->setData(idx,i,Qt::UserRole);
+       }
+       m_listmodel->sort(0);
+}
+
+MCustomer MCustomerListDialog::getCustomer()
+{
+       //get selection
+       QModelIndex idx=m_listview->currentIndex();
+       if(!idx.isValid())return MCustomer();
+       //return object
+       int i=m_listmodel->data(idx,Qt::UserRole).toInt();
+       if(i<0||i>=m_list.size())return MCustomer();
+       return m_list[i];
+}
diff --git a/src/customer.h b/src/customer.h
new file mode 100644 (file)
index 0000000..3e0b2a0
--- /dev/null
@@ -0,0 +1,76 @@
+//
+// C++ Interface: customer
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2008
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+#ifndef MAGICSMOKE_CUSTOMER_H
+#define MAGICSMOKE_CUSTOMER_H
+
+#include <QString>
+#include <QDialog>
+#include <QList>
+
+class MWebRequest;
+class QDomElement;
+
+class MCustomer
+{
+       public:
+               /**creates an empty/invalid customer*/
+               MCustomer();
+               /**creates a customer from XML*/
+               MCustomer(MWebRequest*,const QDomElement&);
+               /**copies a customer*/
+               MCustomer(const MCustomer&);
+               
+               /**copies a customer*/
+               MCustomer& operator=(const MCustomer&);
+               
+               /**returns the ID of this customer or -1 if it is invalid*/
+               qint64 customerID();
+               
+               /**returns whether the customer is valid*/
+               bool isValid();
+               
+               /**returns the customers name*/
+               QString name();
+               
+               /**returns the customers name and address*/
+               QString getNameAddress();
+       private:
+               qint64 m_id;
+               QString m_name;
+               MWebRequest*m_req;
+};
+
+class QListView;
+class QStandardItemModel;
+
+/**shows a list of customers, lets the user select and offers to alter/create customers*/
+class MCustomerListDialog:public QDialog
+{
+       Q_OBJECT
+       public:
+               /**creates a new customer list dialog; expects a usable webrequest object and a parent, if isselect is set to true it offers a select button, if selected is set to a matching customerID it will be pre-selected*/
+               MCustomerListDialog(MWebRequest*,QWidget*,bool isselect=false,qint64 selected=-1);
+               
+               MCustomer getCustomer();
+
+       private:
+               QList<MCustomer> m_list;
+               QListView*m_listview;
+               QStandardItemModel*m_listmodel;
+               MWebRequest*m_req;
+               
+               /**updates internal list*/
+               void updateList();
+};
+
+#endif
index f3da814..ad7eb1b 100644 (file)
@@ -100,15 +100,16 @@ MOverview::MOverview(MWebRequest*mw,QString pk)
        QHBoxLayout*hl2;
        vl2->addLayout(hl2=new QHBoxLayout,0);
        hl2->addStretch(10);
-       hl2->addWidget(new QPushButton(tr("Add Ticket")),0);
-       hl2->addWidget(new QPushButton(tr("Add Voucher")),0);
-       hl2->addWidget(new QPushButton(tr("Remove Item")),0);
+       hl2->addWidget(p=new QPushButton(tr("Add Ticket")),0);
+       hl2->addWidget(p=new QPushButton(tr("Add Voucher")),0);
+       hl2->addWidget(p=new QPushButton(tr("Remove Item")),0);
        QFrame*frm;
        hl->addWidget(frm=new QFrame,0);
        frm->setFrameShape(QFrame::VLine);
        hl->addLayout(vl2=new QVBoxLayout,1);
-       vl2->addWidget(new QPushButton(tr("Customer:")),0);
-       vl2->addWidget(new QLabel("...blah\nPerson\nsomewhere"));
+       vl2->addWidget(p=new QPushButton(tr("Customer:")),0);
+       connect(p,SIGNAL(clicked()),this,SLOT(setCustomer()));
+       vl2->addWidget(cartcustomer=new QLabel("...\n \n \n "));
        vl2->addWidget(frm=new QFrame,0);
        frm->setFrameShape(QFrame::HLine);
        vl2->addSpacing(10);
@@ -122,8 +123,11 @@ MOverview::MOverview(MWebRequest*mw,QString pk)
        frm->setFrameShape(QFrame::HLine);
        vl->addLayout(hl=new QHBoxLayout,0);
        hl->addStretch(10);
-       hl->addWidget(new QPushButton(tr("Save Order")));
-       hl->addWidget(new QPushButton(tr("Clear")));
+       hl->addWidget(p=new QPushButton(tr("Order Items")));
+       p->setEnabled(req->hasRole("createorder"));
+       hl->addWidget(p=new QPushButton(tr("Sell Items")));
+       p->setEnabled(req->hasRole("createsale"));
+       hl->addWidget(p=new QPushButton(tr("Clear")));
        
        //user tab
        tab->addTab(usertab=new QWidget,tr("Users"));
@@ -195,7 +199,7 @@ MOverview::MOverview(MWebRequest*mw,QString pk)
                eventtab->setEnabled(false);
                tab->setTabEnabled(tab->indexOf(eventtab),false);
        }
-       if(!req->hasRole("createorder")){
+       if(!req->hasRole("createorder")&&!req->hasRole("createsale")){
                carttab->setEnabled(false);
                tab->setTabEnabled(tab->indexOf(carttab),false);
        }
@@ -567,6 +571,14 @@ void MOverview::eventOrderTicket()
 {
 }
 
+void MOverview::setCustomer()
+{
+       MCustomerListDialog mcl(req,this,true,customer.customerID());
+       if(mcl.exec()!=QDialog::Accepted)return;
+       customer=mcl.getCustomer();
+       cartcustomer->setText(customer.getNameAddress());
+}
+
 
 /**********************************************/
 
index b58f75b..567151d 100644 (file)
 #include <QPointer>
 #include <QDialog>
 
+#include "customer.h"
+
 class MWebRequest;
 class QTabWidget;
 class QTableView;
 class QStandardItemModel;
 class QPushButton;
 class QLineEdit;
+class QLabel;
 
 /**Main Overview Window*/
 class MOverview:public QMainWindow
@@ -80,6 +83,9 @@ class MOverview:public QMainWindow
                /**export host to file*/
                void exportHost();
                
+               /**decide what customer to use*/
+               void setCustomer();
+               
        private:
                //my session object
                QPointer<MWebRequest>req;
@@ -91,6 +97,9 @@ class MOverview:public QMainWindow
                QTableView*eventtable,*usertable,*hosttable;
                QStandardItemModel*eventmodel,*usermodel,*hostmodel;
                QPushButton*thishostbutton;
+               QLabel*cartcustomer;
+               //cart
+               MCustomer customer;
 };
 
 /**Helper dialog for changing passwords*/
index 1686e0e..7e59249 100644 (file)
@@ -34,6 +34,7 @@ SOURCES = \
        room.cpp \
        user.cpp \
        host.cpp \
+       customer.cpp \
        checkdlg.cpp
 
 #some PHP files are listed below to scan them for translatable items:
@@ -48,6 +49,7 @@ HEADERS = \
        room.h \
        user.h \
        host.h \
+       customer.h \
        checkdlg.h \
        ../www/machine.php \
        ../www/inc/machine/session.php \
index 526f5ed..e7704a0 100644 (file)
@@ -406,6 +406,29 @@ QList<MHost> MWebRequest::getAllHosts()
        return ret;
 }
 
+QList<MCustomer> MWebRequest::getAllCustomers()
+{
+       errstr="";
+       if(!request("getcustomerlist",""))return QList<MCustomer>();
+       //parse return document
+       QDomDocument doc;
+       QString msg;int ln,cl;
+       if(!doc.setContent(rspdata,&msg,&ln,&cl)){
+               errstr=tr("Error parsing CustomerList XML data (line %1 column %2): %3").arg(ln).arg(cl).arg(msg);
+               return QList<MCustomer>();
+       }
+       QDomElement root=doc.documentElement();
+       QDomNodeList nl=root.elementsByTagName("Customer");
+       QList<MCustomer>ret;
+       for(int i=0;i<nl.size();i++){
+               QDomElement el=nl.at(i).toElement();
+               if(el.isNull())continue;
+               MCustomer cr(this,el);
+               if(cr.isValid())ret.append(cr);
+       }
+       return ret;
+}
+
 QByteArray MWebRequest::responseBody()
 {
        return rspdata;
index ef3f60d..2f2f420 100644 (file)
@@ -26,6 +26,7 @@
 #include "event.h"
 #include "user.h"
 #include "host.h"
+#include "customer.h"
 
 /**abstraction of requests to the web server, handles sessions and all data transfer*/
 class MWebRequest:public QObject
@@ -80,6 +81,9 @@ class MWebRequest:public QObject
                /**returns a list of all hosts**/
                QList<MHost>getAllHosts();
                
+               /**returns a list of all customers*/
+               QList<MCustomer>getAllCustomers();
+               
                /**return current host name of this session*/
                QString hostName();
                
index 9f4b99f..8c630fa 100644 (file)
@@ -45,7 +45,7 @@ class Customer
        public function getByMail($mail)
        {
                global $db;
-               $res=$db->select("customer","customerid","email=".$db->escapeString($mail));
+               $res=$db->select("webuser","customerid","email=".$db->escapeString($mail));
                if(count($res)>0){
                        $this->id=$res[0]["customerid"];
                        return true;
@@ -125,7 +125,13 @@ class Customer
        {
                if($this->id===false)return;
                global $db;
-               $db->update("customer",array("email"=>$mail),"customerid=".$db->escapeInt($this->id));
+               $db->beginTransaction();
+               $res=$db->select("webuser","customerid","customerid=".$db->escapeInt($this->id));
+               if(count($res)==1)
+                       $db->update("webuser",array("email"=>$mail),"customerid=".$db->escapeInt($this->id));
+               else
+                       $db->insert("webuser",array("email"=>$mail,"customerid"=>$this->id));
+               $db->commitTransaction();
        }
        
        /**sets the password of this customer*/
@@ -134,7 +140,13 @@ class Customer
                if($this->id===false)return;
                global $db;
                $pass=calcPasswd($pwd,getSalt());
-               $db->update("customer",array("passwd"=>$pass),"customerid=".$db->escapeInt($this->id));
+               $db->beginTransaction();
+               $res=$db->select("webuser","customerid","customerid=".$db->escapeInt($this->id));
+               if(count($res)==1)
+                       $db->update("webuser",array("passwd"=>$pass),"customerid=".$db->escapeInt($this->id));
+               else
+                       $db->insert("webuser",array("passwd"=>$pass,"customerid"=>$this->id));
+               $db->commitTransaction();
        }
        
        /**sets the address of this customer*/
@@ -161,7 +173,7 @@ class Customer
                if($this->id===false)return false;
                //get record
                global $db;
-               $res=$db->select("customer","passwd","customerid=".$db->escapeInt($this->id));
+               $res=$db->select("webuser","passwd","customerid=".$db->escapeInt($this->id));
                //found anything?
                if(count($res)<0)return false;
                //is it a password
@@ -173,4 +185,25 @@ class Customer
        }
 };
 
+/**machine interface: get the list of all existing customers*/
+function getCustomerListXml()
+{
+       global $db;
+       //return customers
+       $res=$db->select("customer","customerid,name","");
+       $xml=new DOMDocument;
+       $root=$xml->createElement("CustomerList");
+       if(count($res)>0)
+       foreach($res as $k => $rm){
+               $cs=$xml->createElement("Customer");
+               $cs->setAttribute("id",$rm["customerid"]);
+               $cs->setAttribute("name",$rm["name"]);
+               $root->appendChild($cs);
+       }
+       $xml->appendChild($root);
+       header("X-MagicSmoke-Status: Ok");
+       print($xml->saveXML());
+       
+}
+
 ?>
\ No newline at end of file
index ccbf9bb..6092457 100644 (file)
@@ -88,9 +88,12 @@ class DbScheme {
                        "name" => array("string","notnull"),
                        "address" => array("text"),
                        "contact" => array("string"),//phone or something
-                       "comments" => array("text"),
+                       "comments" => array("text")
+               );
+               $this->scheme["webuser"]=array(
                        //online login data
-                       "email" => array("string","unique"),
+                       "email" => array("string","primarykey"),
+                       "customerid" => array("int32","unique","foreignkey:customer:customerid"),
                        "passwd" => array("string:64"),//salted SHA-1 hash of passwd
                );
                //orders by customers
index 0c21d37..f13ff5d 100644 (file)
@@ -236,6 +236,17 @@ if($SMOKEREQUEST=="settemplate"){
 }
 
 
+//get the list of customers
+if($SMOKEREQUEST=="getcustomerlist"){
+       getCustomerListXml();
+       exit();
+}
+
+
+if($SMOKEREQUEST=="createorder"){}
+if($SMOKEREQUEST=="createsale"){}
+
+
 //EOF
 header("X-MagicSmoke-Status: Error");
 die(tr("Internal Error: unknown command, hiccup in code structure."));