display orders
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 22 Mar 2008 10:53:15 +0000 (10:53 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 22 Mar 2008 10:53:15 +0000 (10:53 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@134 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/customer.cpp
src/customer.h
src/order.cpp [new file with mode: 0644]
src/order.h [new file with mode: 0644]
src/overview.cpp
src/overview.h
src/smoke.pro
src/webrequest.cpp
src/webrequest.h

index 69c3784..c954e71 100644 (file)
@@ -37,6 +37,14 @@ MCustomer::MCustomer(MWebRequest*r,const QDomElement&el)
        scanXml(el);
 }
 
+MCustomer::MCustomer(MWebRequest*r,qint64 i)
+{
+       m_req=r;
+       m_iscomplete=false;
+       m_id=i;
+       getUpdate();
+}
+
 void MCustomer::scanXml(const QDomElement&el)
 {
        //get basic information
index b7e17a6..275f25e 100644 (file)
@@ -25,6 +25,8 @@ class MCustomer
        public:
                /**creates an empty/invalid customer*/
                MCustomer(MWebRequest*r=0);
+               /**fetches customer from the Database*/
+               MCustomer(MWebRequest*,qint64);
                /**creates a customer from XML*/
                MCustomer(MWebRequest*,const QDomElement&);
                /**copies a customer*/
diff --git a/src/order.cpp b/src/order.cpp
new file mode 100644 (file)
index 0000000..68ec1d2
--- /dev/null
@@ -0,0 +1,176 @@
+//
+// C++ Implementation: host
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2007
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+
+#include "order.h"
+#include "webrequest.h"
+
+#include <QDomElement>
+#include <QCoreApplication>
+
+MOrder::MOrder()
+{
+       req=0;
+       m_orderid=m_customer=-1;
+       m_price=m_paid=0;
+       m_status=Invalid;
+}
+
+MOrder::MOrder(MWebRequest*r,qint32 id)
+{
+       req=r;
+       getFromDB(id);
+}
+
+MOrder::MOrder(MWebRequest*r,QDomElement&e)
+{
+       req=r;
+       parseXml(e);
+}
+
+MOrder::MOrder(const MOrder&o)
+{
+       req=o.req;
+       m_orderid=o.m_orderid;
+       m_customer=o.m_customer;
+       m_price=o.m_price;
+       m_paid=o.m_paid;
+       m_status=o.m_status;
+}
+
+int MOrder::orderID()const
+{
+       return m_orderid;
+}
+
+bool MOrder::isValid()const
+{
+       return m_orderid>=0 && m_status!=Invalid;
+}
+
+int MOrder::customerID()const
+{
+       return m_customer;
+}
+
+MCustomer MOrder::customer()const
+{
+       return MCustomer(req,m_customer);
+}
+
+MOrder::OrderStatus MOrder::orderStatus()const
+{
+       return m_status;
+}
+
+QString MOrder::orderStatusString()const
+{
+       switch(m_status){
+               case Placed:return QCoreApplication::translate("MOrder","placed","state");
+               case Sent:return QCoreApplication::translate("MOrder","sent","state");
+               case Cancelled:return QCoreApplication::translate("MOrder","cancelled","state");
+               case Closed:return QCoreApplication::translate("MOrder","closed","state");
+               default:return QCoreApplication::translate("MOrder","invalid","state");
+       }
+}
+
+int MOrder::totalPrice()const
+{
+       return m_price;
+}
+
+QString MOrder::totalPriceString()const
+{
+       return cent2string(m_price);
+}
+
+int MOrder::amountPaid()const
+{
+       return m_paid;
+}
+
+QString MOrder::amountPaidString()const
+{
+       return cent2string(m_paid);
+}
+
+//static
+QString MOrder::cent2string(int c)
+{
+       return QString::number(c/100)+QCoreApplication::translate("MOrder",".","decimal dot")+QString().sprintf("%02d",c%100);
+}
+
+bool MOrder::needsPayment()const
+{
+       if(m_status==Placed || m_status==Sent)
+               if(m_paid<m_price)return true;
+       return false;
+}
+
+bool MOrder::needsRefund()const
+{
+       if(m_status==Cancelled)
+               return m_paid>0;
+       if(m_status==Placed || m_status==Sent)
+               return m_paid>m_price;
+       return false;
+}
+
+int MOrder::amountToPay()const
+{
+       if(m_status==Placed || m_status==Sent)
+               if(m_paid<m_price)return m_price-m_paid;
+       return 0;
+}
+
+int MOrder::amountToRefund()const
+{
+       if(m_status==Cancelled)
+               return m_paid;
+       if(m_status==Placed || m_status==Sent)
+               if(m_paid>m_price)
+                       return m_paid-m_price;
+       return 0;
+}
+
+bool MOrder::isSent()const
+{
+       //only in placed mode there is a need for action, hence in all other modes we assum sent
+       return m_status!=Placed;
+}
+
+void MOrder::getFromDB(qint32 i)
+{
+       //TODO
+}
+
+void MOrder::parseXml(const QDomElement&e)
+{
+       //Basics
+       bool b;
+       m_orderid=e.attribute("id","-1").toInt(&b);
+       if(!b){
+               m_orderid=m_customer=-1;
+               m_price=m_paid=0;
+               m_status=Invalid;
+               return;
+       }
+       m_customer=e.attribute("customer","-1").toInt(&b);
+       if(!b)m_customer=-1;
+       m_price=e.attribute("totalprice","0").toInt();
+       m_paid=e.attribute("paid","0").toInt();
+       QString st=e.attribute("status");
+       if(st=="placed")m_status=Placed;else
+       if(st=="sent")m_status=Sent;else
+       if(st=="cancelled")m_status=Cancelled;else
+       if(st=="closed")m_status=Closed;
+       else m_status=Invalid;
+}
diff --git a/src/order.h b/src/order.h
new file mode 100644 (file)
index 0000000..f1a883c
--- /dev/null
@@ -0,0 +1,109 @@
+//
+// C++ Interface: host
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2007
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+
+#ifndef MAGICSMOKE_ORDER_H
+#define MAGICSMOKE_ORDER_H
+
+#include <QString>
+
+#include "customer.h"
+
+class MWebRequest;
+class QDomElement;
+
+class MOrder
+{
+       public:
+               /**create invalid host*/
+               MOrder();
+               /**create order by id*/
+               MOrder(MWebRequest*,qint32);
+               /**create order from XML element*/
+               MOrder(MWebRequest*,QDomElement&);
+               /**copy order*/
+               MOrder(const MOrder&);
+               
+               /**returns the order ID (-1 for invalid orders)*/
+               int orderID()const;
+               
+               /**returns whether the order is valid*/
+               bool isValid()const;
+               
+               /**returns the customer ID*/
+               int customerID()const;
+               
+               /**returns a customer object (calls database!)*/
+               MCustomer customer()const;
+               
+               /**status*/
+               enum OrderStatus{
+                       /**the order is invalid (not a DB state)*/
+                       Invalid,
+                       /**the order has been placed, nothing is delivered or paid*/
+                       Placed,
+                       /**the tickets/vouchers have been shipped*/
+                       Sent,
+                       /**the order was cancelled*/
+                       Cancelled,
+                       /**the order is closed (currently unused state)*/
+                       Closed
+               };
+               
+               /**returns the status of the order*/
+               OrderStatus orderStatus()const;
+               
+               /**returns the status of the order as localized string*/
+               QString orderStatusString()const;
+               
+               /**returns how much money needs to be paid in total for this order, in cents*/
+               int totalPrice()const;
+               
+               /**returns how much money needs to be paid in total for this order, in cents*/
+               QString totalPriceString()const;
+               
+               /**returns how much money has already been paid for this order, in cents*/
+               int amountPaid()const;
+               
+               /**returns how much money has already been paid for this order, in cents*/
+               QString amountPaidString()const;
+               
+               /**returns whether there is anything left to pay*/
+               bool needsPayment()const;
+               
+               /**returns whether there is anything left to refund*/
+               bool needsRefund()const;
+               
+               /**returns how much there is to be paid, in cents*/
+               int amountToPay()const;
+               
+               /**returns how much there is to be refunded, in cents*/
+               int amountToRefund()const;
+               
+               /**returns whether the tickets of this order have already been shipped*/
+               bool isSent()const;
+               
+       private:
+               MWebRequest*req;
+               int m_orderid,m_customer,m_price,m_paid;
+               OrderStatus m_status;
+               
+               /**internal: requests the order from the database*/
+               void getFromDB(qint32);
+               
+               /**internal: parse XML*/
+               void parseXml(const QDomElement&);
+               
+               /**helper: converts a cent value into a localized string*/
+               static QString cent2string(int);
+};
+
+#endif
index 36ea918..76bdf8d 100644 (file)
 #include <QDomElement>
 #include <QComboBox>
 
+#define ORDERNONE      0
+#define ORDERALL       0xff
+#define ORDEROPEN      7
+#define ORDERPAY       1
+#define ORDERREFUND    2
+#define ORDERUNSENT    4
+
 MOverview::MOverview(MWebRequest*mw,QString pk)
 {
        req=mw;
@@ -157,12 +164,17 @@ MOverview::MOverview(MWebRequest*mw,QString pk)
        ordertab->setLayout(hl=new QHBoxLayout);
        hl->addLayout(vl=new QVBoxLayout,10);
        vl->addWidget(ordermode=new QComboBox,0);
-       ordermode->addItem(tr("-select mode-"));
-       ordermode->addItem(tr("All Orders"));
-       ordermode->addItem(tr("Open Orders"));
-       ordermode->addItem(tr("Outstanding Payments"));
-       ordermode->addItem(tr("Outstanding Refunds"));
-       vl->addWidget(new QTableView);
+       ordermode->addItem(tr("-select mode-"),ORDERNONE);
+       ordermode->addItem(tr("All Orders"),ORDERALL);
+       ordermode->addItem(tr("Open Orders"),ORDEROPEN);
+       ordermode->addItem(tr("Outstanding Payments"),ORDERPAY);
+       ordermode->addItem(tr("Outstanding Refunds"),ORDERREFUND);
+       ordermode->addItem(tr("Undelivered Orders"),ORDERUNSENT);
+       connect(ordermode,SIGNAL(currentIndexChanged(int)),this,SLOT(updateOrders()));
+       vl->addWidget(ordertable=new QTableView);
+       ordertable->setModel(ordermodel=new QStandardItemModel(this));
+       ordertable->setSelectionMode(QAbstractItemView::SingleSelection);
+       ordertable->setEditTriggers(QAbstractItemView::NoEditTriggers);
        hl->addLayout(vl=new QVBoxLayout,0);
        vl->addWidget(new QPushButton(tr("Details...")),0);
        vl->addSpacing(10);
@@ -257,6 +269,12 @@ MOverview::MOverview(MWebRequest*mw,QString pk)
        }else{
                initCart();
        }
+       if(req->hasRole("getorders")){
+               updateOrders();
+       }else{
+               ordertab->setEnabled(false);
+               tab->setTabEnabled(tab->indexOf(ordertab),false);
+       }
        if(req->hasRole("getusers")){
                updateUsers();
        }else{
@@ -828,7 +846,42 @@ void MOverview::entranceValidate()
        entrancescan->setText("");
 }
 
-
+//helper: finds out whether an order should be printed.
+static inline bool candoUpdateOrders(int omode,const MOrder&ord)
+{
+       if(omode==ORDERALL)return true;
+       if((omode&ORDERPAY)!=0 && ord.needsPayment())return true;
+       if((omode&ORDERREFUND)!=0 && ord.needsRefund())return true;
+       if((omode&ORDERUNSENT)!=0 && !ord.isSent())return true;
+       return false;
+}
+
+void MOverview::updateOrders()
+{
+       ordermodel->clear();
+       ordermodel->setHorizontalHeaderLabels(QStringList()<<tr("Status")<<tr("Total")<<tr("Paid")<<tr("Customer"));
+       int omode=ordermode->itemData(ordermode->currentIndex()).toInt();
+       if(omode==ORDERNONE)return;
+       QList<MOrder> orders=req->getAllOrders();
+       if(orders.size()==0)return;
+       QList<MCustomer> cust=req->getAllCustomers();
+       int cl=0;
+       for(int i=0;i<orders.size();i++){
+               if(!candoUpdateOrders(omode,orders[i]))continue;
+               ordermodel->insertRow(cl);
+               ordermodel->setHeaderData(cl,Qt::Vertical,orders[i].orderID());
+               ordermodel->setData(ordermodel->index(cl,0),orders[i].orderStatusString());
+               ordermodel->setData(ordermodel->index(cl,0),orders[i].orderID(),Qt::UserRole);
+               ordermodel->setData(ordermodel->index(cl,1),orders[i].totalPriceString());
+               ordermodel->setData(ordermodel->index(cl,2),orders[i].amountPaidString());
+               int cid=orders[i].customerID();
+               //TODO: make this more effective:
+               for(int j=0;j<cust.size();j++)
+                       if(cust[j].customerID()==cid)
+                               ordermodel->setData(ordermodel->index(cl,3),cust[j].name());
+               cl++;
+       }
+}
 
 /**********************************************/
 
index 11a3d44..70529ad 100644 (file)
@@ -55,6 +55,9 @@ class MOverview:public QMainWindow
                /**open event summary*/
                void eventSummary();
                
+               /**get all orders, update list*/
+               void updateOrders();
+               
                /**update list of users*/
                void updateUsers();
                /**create new user*/
@@ -119,8 +122,8 @@ class MOverview:public QMainWindow
                //widgets
                QTabWidget*tab;
                QWidget*eventtab,*carttab,*usertab,*hosttab,*ordertab,*entrancetab;
-               QTableView*eventtable,*usertable,*hosttable,*carttable;
-               QStandardItemModel*eventmodel,*usermodel,*hostmodel,*cartmodel;
+               QTableView*eventtable,*usertable,*hosttable,*carttable,*ordertable;
+               QStandardItemModel*eventmodel,*usermodel,*hostmodel,*cartmodel,*ordermodel;
                QPushButton*thishostbutton;
                QLabel*cartcustomer,*entrancelabel;
                QTextEdit *cartaddr,*cartcomment;
index 726d883..c3e7303 100644 (file)
@@ -34,6 +34,7 @@ SOURCES = \
        room.cpp \
        user.cpp \
        host.cpp \
+       order.cpp \
        customer.cpp \
        checkdlg.cpp \
        eventsummary.cpp \
@@ -51,6 +52,7 @@ HEADERS = \
        room.h \
        user.h \
        host.h \
+       order.h \
        customer.h \
        checkdlg.h \
        eventsummary.h \
index e7704a0..c12d5b3 100644 (file)
@@ -429,6 +429,29 @@ QList<MCustomer> MWebRequest::getAllCustomers()
        return ret;
 }
 
+QList<MOrder> MWebRequest::getAllOrders()
+{
+       errstr="";
+       if(!request("getorderlist",""))return QList<MOrder>();
+       //parse return document
+       QDomDocument doc;
+       QString msg;int ln,cl;
+       if(!doc.setContent(rspdata,&msg,&ln,&cl)){
+               errstr=tr("Error parsing OrderList XML data (line %1 column %2): %3").arg(ln).arg(cl).arg(msg);
+               return QList<MOrder>();
+       }
+       QDomElement root=doc.documentElement();
+       QDomNodeList nl=root.elementsByTagName("Order");
+       QList<MOrder>ret;
+       for(int i=0;i<nl.size();i++){
+               QDomElement el=nl.at(i).toElement();
+               if(el.isNull())continue;
+               MOrder mo(this,el);
+               if(mo.isValid())ret.append(mo);
+       }
+       return ret;
+}
+
 QByteArray MWebRequest::responseBody()
 {
        return rspdata;
index 2f2f420..62f4e1c 100644 (file)
@@ -27,6 +27,7 @@
 #include "user.h"
 #include "host.h"
 #include "customer.h"
+#include "order.h"
 
 /**abstraction of requests to the web server, handles sessions and all data transfer*/
 class MWebRequest:public QObject
@@ -84,6 +85,9 @@ class MWebRequest:public QObject
                /**returns a list of all customers*/
                QList<MCustomer>getAllCustomers();
                
+               /**returns a list of all orders*/
+               QList<MOrder>getAllOrders();
+               
                /**return current host name of this session*/
                QString hostName();