From 93ddd2b89397b556459052e98c81cb5d5f7a7cca Mon Sep 17 00:00:00 2001 From: konrad Date: Sun, 9 Mar 2008 13:32:46 +0000 Subject: [PATCH] *Split customer table into two *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 | 11 +++ src/customer.cpp | 147 ++++++++++++++++++++++++++++++++++++++++++ src/customer.h | 76 ++++++++++++++++++++++ src/overview.cpp | 28 ++++++-- src/overview.h | 9 +++ src/smoke.pro | 2 + src/webrequest.cpp | 23 +++++++ src/webrequest.h | 4 + www/inc/classes/customer.php | 41 +++++++++++- www/inc/db/db_scheme.php | 7 ++- www/machine.php | 11 +++ 11 files changed, 345 insertions(+), 14 deletions(-) create mode 100644 src/customer.cpp create mode 100644 src/customer.h diff --git a/doc/prog_protocol.html b/doc/prog_protocol.html index 7202895..f22d5a0 100644 --- a/doc/prog_protocol.html +++ b/doc/prog_protocol.html @@ -361,6 +361,17 @@ FIXME: special hosts (beginning with "_") return errors. Non-existing hosts are

Customers

+

Getting List of Customers

+ +The getcustomerlist transaction returns a short list of customers. The request is empty. The response returns this: + +
+<CustomerList>
+  <Customer id="integer ID" name="full name"/>
+  ...
+</CustomerList>
+
+

Orders and Sales

Templates

diff --git a/src/customer.cpp b/src/customer.cpp new file mode 100644 index 0000000..2b0fd25 --- /dev/null +++ b/src/customer.cpp @@ -0,0 +1,147 @@ +// +// C++ Implementation: customer +// +// Description: +// +// +// Author: Konrad Rosenbaum , (C) 2008 +// +// Copyright: See README/COPYING files that come with this distribution +// +// + +#include "customer.h" +#include "webrequest.h" + +#include +#include +#include +#include +#include + +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;irowCount();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;iindex(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 index 0000000..3e0b2a0 --- /dev/null +++ b/src/customer.h @@ -0,0 +1,76 @@ +// +// C++ Interface: customer +// +// Description: +// +// +// Author: Konrad Rosenbaum , (C) 2008 +// +// Copyright: See README/COPYING files that come with this distribution +// +// + +#ifndef MAGICSMOKE_CUSTOMER_H +#define MAGICSMOKE_CUSTOMER_H + +#include +#include +#include + +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 m_list; + QListView*m_listview; + QStandardItemModel*m_listmodel; + MWebRequest*m_req; + + /**updates internal list*/ + void updateList(); +}; + +#endif diff --git a/src/overview.cpp b/src/overview.cpp index f3da814..ad7eb1b 100644 --- a/src/overview.cpp +++ b/src/overview.cpp @@ -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()); +} + /**********************************************/ diff --git a/src/overview.h b/src/overview.h index b58f75b..567151d 100644 --- a/src/overview.h +++ b/src/overview.h @@ -17,12 +17,15 @@ #include #include +#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 QPointerreq; @@ -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*/ diff --git a/src/smoke.pro b/src/smoke.pro index 1686e0e..7e59249 100644 --- a/src/smoke.pro +++ b/src/smoke.pro @@ -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 \ diff --git a/src/webrequest.cpp b/src/webrequest.cpp index 526f5ed..e7704a0 100644 --- a/src/webrequest.cpp +++ b/src/webrequest.cpp @@ -406,6 +406,29 @@ QList MWebRequest::getAllHosts() return ret; } +QList MWebRequest::getAllCustomers() +{ + errstr=""; + if(!request("getcustomerlist",""))return QList(); + //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(); + } + QDomElement root=doc.documentElement(); + QDomNodeList nl=root.elementsByTagName("Customer"); + QListret; + for(int i=0;igetAllHosts(); + /**returns a list of all customers*/ + QListgetAllCustomers(); + /**return current host name of this session*/ QString hostName(); diff --git a/www/inc/classes/customer.php b/www/inc/classes/customer.php index 9f4b99f..8c630fa 100644 --- a/www/inc/classes/customer.php +++ b/www/inc/classes/customer.php @@ -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 diff --git a/www/inc/db/db_scheme.php b/www/inc/db/db_scheme.php index ccbf9bb..6092457 100644 --- a/www/inc/db/db_scheme.php +++ b/www/inc/db/db_scheme.php @@ -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 diff --git a/www/machine.php b/www/machine.php index 0c21d37..f13ff5d 100644 --- a/www/machine.php +++ b/www/machine.php @@ -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.")); -- 1.7.2.5