From 714d754bc66174f031c7a03fbf460d9d493f5512 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 22 Mar 2008 10:53:15 +0000 Subject: [PATCH] display orders git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@134 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- src/customer.cpp | 8 +++ src/customer.h | 2 + src/order.cpp | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/order.h | 109 ++++++++++++++++++++++++++++++++ src/overview.cpp | 67 ++++++++++++++++++-- src/overview.h | 7 ++- src/smoke.pro | 2 + src/webrequest.cpp | 23 +++++++ src/webrequest.h | 4 + 9 files changed, 389 insertions(+), 9 deletions(-) create mode 100644 src/order.cpp create mode 100644 src/order.h diff --git a/src/customer.cpp b/src/customer.cpp index 69c3784..c954e71 100644 --- a/src/customer.cpp +++ b/src/customer.cpp @@ -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 diff --git a/src/customer.h b/src/customer.h index b7e17a6..275f25e 100644 --- a/src/customer.h +++ b/src/customer.h @@ -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 index 0000000..68ec1d2 --- /dev/null +++ b/src/order.cpp @@ -0,0 +1,176 @@ +// +// C++ Implementation: host +// +// Description: +// +// +// Author: Konrad Rosenbaum , (C) 2007 +// +// Copyright: See COPYING file that comes with this distribution +// +// + +#include "order.h" +#include "webrequest.h" + +#include +#include + +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_paid0; + 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_paidm_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 index 0000000..f1a883c --- /dev/null +++ b/src/order.h @@ -0,0 +1,109 @@ +// +// C++ Interface: host +// +// Description: +// +// +// Author: Konrad Rosenbaum , (C) 2007 +// +// Copyright: See COPYING file that comes with this distribution +// +// + +#ifndef MAGICSMOKE_ORDER_H +#define MAGICSMOKE_ORDER_H + +#include + +#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 diff --git a/src/overview.cpp b/src/overview.cpp index 36ea918..76bdf8d 100644 --- a/src/overview.cpp +++ b/src/overview.cpp @@ -39,6 +39,13 @@ #include #include +#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()<itemData(ordermode->currentIndex()).toInt(); + if(omode==ORDERNONE)return; + QList orders=req->getAllOrders(); + if(orders.size()==0)return; + QList cust=req->getAllCustomers(); + int cl=0; + for(int i=0;iinsertRow(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;jsetData(ordermodel->index(cl,3),cust[j].name()); + cl++; + } +} /**********************************************/ diff --git a/src/overview.h b/src/overview.h index 11a3d44..70529ad 100644 --- a/src/overview.h +++ b/src/overview.h @@ -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; diff --git a/src/smoke.pro b/src/smoke.pro index 726d883..c3e7303 100644 --- a/src/smoke.pro +++ b/src/smoke.pro @@ -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 \ diff --git a/src/webrequest.cpp b/src/webrequest.cpp index e7704a0..c12d5b3 100644 --- a/src/webrequest.cpp +++ b/src/webrequest.cpp @@ -429,6 +429,29 @@ QList MWebRequest::getAllCustomers() return ret; } +QList MWebRequest::getAllOrders() +{ + errstr=""; + if(!request("getorderlist",""))return QList(); + //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(); + } + QDomElement root=doc.documentElement(); + QDomNodeList nl=root.elementsByTagName("Order"); + QListret; + for(int i=0;igetAllCustomers(); + /**returns a list of all orders*/ + QListgetAllOrders(); + /**return current host name of this session*/ QString hostName(); -- 1.7.2.5