implement order search by user
authorKonrad Rosenbaum <konrad@silmor.de>
Thu, 29 Dec 2011 12:36:13 +0000 (13:36 +0100)
committerKonrad Rosenbaum <konrad@silmor.de>
Thu, 29 Dec 2011 12:36:13 +0000 (13:36 +0100)
src/mwin/orderstab.cpp
src/mwin/orderstab.h
wob/db/user.wolf
wob/transact/order.wolf
www/inc/wext/order.php

index 7f421db..f5146a6 100644 (file)
 #include <TimeStamp>
 
 #include <QBoxLayout>
+#include <QCheckBox>
 #include <QComboBox>
 #include <QDateTimeEdit>
+#include <QDebug>
+#include <QFormLayout>
 #include <QInputDialog>
 #include <QLabel>
 #include <QMessageBox>
 #include <QPushButton>
 #include <QSettings>
+#include <QSpinBox>
 #include <QStandardItemModel>
 #include <QTableView>
 
@@ -87,6 +91,9 @@ MOrdersTab::MOrdersTab(QString pk)
        vl->addWidget(p=new QPushButton(tr("Find by Customer...")),0);
        connect(p,SIGNAL(clicked()),this,SLOT(orderByCustomer()));
        p->setEnabled(req->hasRight(req->RGetOrdersByCustomer));
+       vl->addWidget(p=new QPushButton(tr("Find by User...")),0);
+       connect(p,SIGNAL(clicked()),this,SLOT(orderByUser()));
+       p->setEnabled(req->hasRight(req->RGetOrdersByUser) || req->hasRight(req->RGetMyOrders));
        vl->addWidget(p=new QPushButton(tr("Find by Order ID...")),0);
        connect(p,SIGNAL(clicked()),this,SLOT(orderByOrder()));
        p->setEnabled(req->hasRight(req->RGetOrder));
@@ -110,6 +117,7 @@ qint64 MOrdersTab::oldestOrderStamp(OrderTimeStampMode mode,int age)
                        case OldestForAll:age=set.value("maxorderage",0).toInt();break;
                        case OldestForOpen:age=set.value("maxorderageopen",0).toInt();break;
                        case OldestForEvent:age=set.value("maxorderageevent",0).toInt();break;
+                       case OldestForUser://currently we re-use the customer setting
                        case OldestForCustomer:age=set.value("maxorderagecust",0).toInt();break;
                        case OldestForSince:age=set.value("deforderagesince",7).toInt();break;
                        default: age=0;break;
@@ -370,3 +378,106 @@ void MOrdersTab::orderByOrder()
        om->setAttribute(Qt::WA_DeleteOnClose);
        om->show();
 }
+
+void MOrdersTab::orderByUser()
+{
+       MOrdersByUserDlg d(oldestOrderStamp(OldestForUser),this);
+       if(d.exec()!=QDialog::Accepted)return;
+       resetModel();
+//     qDebug()<<"searching: my"<<d.mySelfOnly()<<"user"<<d.userName()<<"incall"<<d.includeAll()<<"old"<<d.oldest();
+       QList<MOOrderInfo>orders;
+       QList<MOCustomerInfo>cust;
+       if(d.mySelfOnly()){
+               MTGetMyOrders gmo=req->queryGetMyOrders(d.oldest(),d.includeAll());
+               if(gmo.hasError()){
+                       QMessageBox::warning(this,tr("Warning"),tr("Unable to get user orders, server error: %1").arg(gmo.errorString()));
+                       return;
+               }
+               orders=gmo.getorders();
+               cust=gmo.getcustomers();
+       }else{
+               MTGetOrdersByUser obu=req->queryGetOrdersByUser(d.oldest(),d.includeAll(),d.userName());
+               if(obu.hasError()){
+                       QMessageBox::warning(this,tr("Warning"),tr("Unable to get user orders, server error: %1").arg(obu.errorString()));
+                       return;
+               }
+               orders=obu.getorders();
+               cust=obu.getcustomers();
+       }
+       for(int i=0;i<orders.size();i++){
+               addOrderToModel(orders[i],cust);
+       }
+       ordermode->setCurrentIndex(ordermode->count()-1);
+}
+
+MOrdersByUserDlg::MOrdersByUserDlg(int olddef,QWidget* parent, Qt::WindowFlags f): QDialog(parent, f)
+{
+       setWindowTitle(tr("Select User Criteria"));
+       //layout
+       QVBoxLayout*vl;
+       setLayout(vl=new QVBoxLayout);
+       QFormLayout*fl;
+       vl->addLayout(fl=new QFormLayout);
+       vl->addStretch(1);
+       //buttons
+       QHBoxLayout*hl;
+       vl->addLayout(hl=new QHBoxLayout);
+       hl->addStretch(1);
+       QPushButton*p;
+       hl->addWidget(p=new QPushButton(tr("Ok")));
+       connect(p,SIGNAL(clicked()),this,SLOT(accept()));
+       hl->addWidget(p=new QPushButton(tr("Cancel")));
+       connect(p,SIGNAL(clicked()),this,SLOT(reject()));
+       //form
+       fl->addRow(tr("My orders:"),m_myself=new QCheckBox(tr("look for my own orders")));
+       fl->addRow(tr("User Name:"),m_uname=new QComboBox);
+       m_uname->setEditable(true);
+       connect(m_myself,SIGNAL(toggled(bool)),m_uname,SLOT(setDisabled(bool)));
+       fl->addRow(tr("Maximum Age (days):"),m_days=new QSpinBox);
+       m_days->setRange(0,9999);
+       m_days->setValue(olddef);
+       fl->addRow(tr("Limit:"),m_incall=new QCheckBox(tr("Include all orders the user touched")));
+       //init and preselect
+       if(req->hasRight(req->RGetOrdersByUser)){
+               //try to get all user names
+               if(req->hasRight(req->RGetAllUsers)){
+                       MTGetAllUsers gau=req->queryGetAllUsers();
+                       if(!gau.hasError()){
+                               QList<MOUser>ul=gau.getusers();
+                               foreach(MOUser u,ul){
+                                       m_uname->addItem(u.name());
+                               }
+                       }
+               }
+               //this would be odd, but it is possible
+               if(!req->hasRight(req->RGetMyOrders))
+                       m_myself->setEnabled(false);
+       }else{
+               m_myself->setChecked(true);
+               m_myself->setEnabled(false);
+       }
+       //use own name as preselection
+       m_uname->setEditText(req->currentUser());
+}
+
+bool MOrdersByUserDlg::includeAll() const
+{
+       return m_incall->isChecked();
+}
+
+bool MOrdersByUserDlg::mySelfOnly() const
+{
+       return m_myself->isChecked();
+}
+
+QString MOrdersByUserDlg::userName() const
+{
+       return m_uname->currentText();
+}
+
+qint64 MOrdersByUserDlg::oldest() const
+{
+       qint64 d=m_days->value();
+       if(d<=0)return 0;
+       return QDateTime::currentDateTime().toTime_t() - (d*86400);
+}
index fb13381..9942ccd 100644 (file)
@@ -13,7 +13,7 @@
 #ifndef MAGICSMOKE_ORDERSTAB_H
 #define MAGICSMOKE_ORDERSTAB_H
 
-#include <QWidget>
+#include <QDialog>
 
 class QAction;
 class QCheckBox;
@@ -54,6 +54,8 @@ class MOrdersTab:public QWidget
                void orderByOrder();
                /**display all orders since a specific date*/
                void orderSinceDate();
+               ///find and display orders by user who created it
+               void orderByUser();
                
        signals:
                /**needs to be connected to the event tab*/
@@ -71,6 +73,7 @@ class MOrdersTab:public QWidget
                        OldestForEvent,
                        OldestForCustomer,
                        OldestForSince,
+                       OldestForUser,
                };
                /**returns timestamp for oldest order; if age is given it returns a timestamp age days in the past, if age is not given it uses the configured time*/
                qint64 oldestOrderStamp(OrderTimeStampMode mode,int age=-1);
@@ -83,4 +86,21 @@ class MOrdersTab:public QWidget
                QComboBox*ordermode;
 };
 
+class MOrdersByUserDlg:public QDialog
+{
+       Q_OBJECT
+       public:
+               MOrdersByUserDlg(int oldestdefault, QWidget* parent, Qt::WindowFlags f = 0);
+               
+               bool mySelfOnly()const;
+               QString userName()const;
+               bool includeAll()const;
+               qint64 oldest()const;
+               
+       private:
+               QComboBox*m_uname;
+               QCheckBox*m_myself,*m_incall;
+               QSpinBox*m_days;
+};
+
 #endif
index 0768b14..be6e851 100644 (file)
                <Preset><V col="rolename" val="OrderAdvanced"/><V col="rightname" val="ResetCustomerPassword"/></Preset>
                <Preset><V col="rolename" val="OrderAdvanced"/><V col="rightname" val="ReturnTicketVoucher"/></Preset>
                <Preset><V col="rolename" val="OrderAdvanced"/><V col="rightname" val="ChangeOrderAddress"/></Preset>
+               <Preset><V col="rolename" val="OrderAdvanced"/><V col="rightname" val="GetMyOrders"/></Preset>
                <Preset><V col="rolename" val="OrderCancelling"/><V col="rightname" val="CancelOrder"/></Preset>
                <Preset><V col="rolename" val="OrderCancelling"/><V col="rightname" val="OrderChangeShipping"/></Preset>
                <Preset><V col="rolename" val="OrderCancelling"/><V col="rightname" val="ReturnTicketVoucher"/></Preset>
                <Preset><V col="rolename" val="OrderHighPrivilege"/><V col="rightname" val="UseTicket"/></Preset>
                <Preset><V col="rolename" val="OrderHighPrivilege"/><V col="rightname" val="UseVoucher"/></Preset>
                <Preset><V col="rolename" val="OrderHighPrivilege"/><V col="rightname" val="ChangeOrderAddress"/></Preset>
+               <Preset><V col="rolename" val="OrderHighPrivilege"/><V col="rightname" val="GetMyOrders"/></Preset>
+               <Preset><V col="rolename" val="OrderHighPrivilege"/><V col="rightname" val="GetOrdersByUser"/></Preset>
                <Preset><V col="rolename" val="OrderListing"/><V col="rightname" val="GetOrder"/></Preset>
                <Preset><V col="rolename" val="OrderListing"/><V col="rightname" val="GetOrderByBarcode"/></Preset>
                <Preset><V col="rolename" val="OrderListing"/><V col="rightname" val="GetOrderList"/></Preset>
index 8b37434..0e401fb 100644 (file)
                </Output>
        </Transaction>
        
+       <Transaction name="GetMyOrders" updating="no">
+               <Doc>returns all orders created by the current user</Doc>
+               <Input>
+                       <Var name="oldest" type="int64">unix timestamp for the oldest order to be returned (compared with ordertime)</Var>
+                       <Var name="includeall" type="bool">if true: include all orders that the user ever touched</Var>
+               </Input>
+               <Call lang="php" method="WOOrderInfo::getOrdersByUser($this,true);"/>
+               <Output>
+                       <Var name="orders" type="List:OrderInfo"/>
+                       <Var name="customers" type="List:CustomerInfo"/>
+               </Output>
+       </Transaction>
+       <Transaction name="GetOrdersByUser" updating="no">
+               <Doc>returns all orders created by the a specified user</Doc>
+               <Input>
+                       <Var name="oldest" type="int64">unix timestamp for the oldest order to be returned (compared with ordertime)</Var>
+                       <Var name="includeall" type="bool">if true: include all orders that the user ever touched</Var>
+                       <Var name="username" type="astring">the name of the user to look for</Var>
+               </Input>
+               <Call lang="php" method="WOOrderInfo::getOrdersByUser($this,false);"/>
+               <Output>
+                       <Var name="orders" type="List:OrderInfo"/>
+                       <Var name="customers" type="List:CustomerInfo"/>
+               </Output>
+       </Transaction>
+       
        <Transaction name="GetOrderByBarcode" updating="no">
                <Doc>retrieves the order in which a specific ticket or voucher was bought</Doc>
                <Input>
index 6594835..2898316 100644 (file)
@@ -94,6 +94,47 @@ class WOOrderInfo extends WOOrderInfoAbstract
                global $db;
                $trans->setorders(self::fromTableArrayorder(WTorder::selectFromDB("customerid=".$db->escapeInt($cid)." AND ordertime>=".$db->escapeInt($old),"ORDER BY orderid")));
        }
+
+       /**called from GetOrdersByUser and getMyOrders transaction*/
+       static public function getOrdersByUser($trans,$meonly)
+       {
+               global $session;
+               if($meonly)
+                       $uid=$session->currentUserName();
+               else
+                       $uid=$trans->getusername();
+               $old=$trans->getoldest();
+               if($old===false)$old=0;
+               $incall=$trans->getincludeall();
+               //get primary list: orders of this user
+               global $db;
+               $prim=WTorder::selectFromDB("soldby=".$db->escapeString($uid)." AND ordertime>=".$db->escapeInt($old));
+               //get secondary list: orders the user touched
+               if($incall){
+                       $seca=WTorder_audit::selectFromDB("audituname=".$db->escapeString($uid)." AND audittime>=".$db->escapeInt($old));
+                       $rs=array();
+                       foreach($seca as $oa)
+                               if(!in_array($oa->orderid,$rs))
+                                       $rs[]=$oa->orderid;
+                       $sec=WTorder::selectFromDB("orderid IN ".$db->escapeIntList($rs));
+               }else $sec=array();
+               //merge and sort
+               $res=array();
+               $cst=array();
+               foreach($prim as $o){
+                       $res[$o->orderid]=$o;
+                       $cst[]=$o->customerid;
+               }
+               foreach($sec as $o){
+                       $res[$o->orderid]=$o;
+                       $cst[]=$o->customerid;
+               }
+               ksort($res);
+               //construct result
+               $trans->setorders(self::fromTableArrayorder(array_values($res)));
+               $cust=WTcustomer::selectFromDB("customerid IN ".$db->escapeIntList(array_unique($cst,SORT_NUMERIC)));
+               $trans->setcustomers(WOCustomerInfo::fromTableArraycustomer($cust));
+       }
        
        /**helper function for several transactions: selects customers by their IDs references by the given order info objects*/
        static protected function getCustomerListByOrders(array $olist)