order window: enable add comments, implement payment and refund
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Thu, 31 Dec 2009 16:39:10 +0000 (16:39 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Thu, 31 Dec 2009 16:39:10 +0000 (16:39 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@383 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/dialogs/orderwin.cpp
src/dialogs/orderwin.h
wob/order.wolf
www/inc/wext/order.php

index 30b03ba..69e94c5 100644 (file)
@@ -55,19 +55,16 @@ MOrderWindow::MOrderWindow(QWidget*par,const MOOrder&o)
        
        QMenuBar*mb=menuBar();
        QMenu *m=mb->addMenu(tr("&Order"));
-       m->addAction(tr("&Order..."),this,SLOT(createOrder()))
-        ->setEnabled(req->hasRight(req->RReservationToOrder)&&o.isReservation());
-       m->addAction(tr("C&ancel Order..."),this,SLOT(cancelOrder()))
-        ->setEnabled(req->hasRight(req->RCancelOrder));
-       m->addAction(tr("&Mark Order as Shipped..."),this,SLOT(shipOrder()))
-        ->setEnabled(req->hasRight(req->ROrderMarkShipped));
+       m_res2order=m->addAction(tr("&Order..."),this,SLOT(createOrder()));
+       m_cancel=m->addAction(tr("C&ancel Order..."),this,SLOT(cancelOrder()));
+       m_ship=m->addAction(tr("&Mark Order as Shipped..."),this,SLOT(shipOrder()));
        m->addSeparator();
        m->addAction(tr("Ch&ange Item-Price..."),this,SLOT(changeItem()))
         ->setEnabled(req->hasRole("changeticketprice"));
        m->addAction(tr("&Return Item..."),this,SLOT(itemReturn()))
         ->setEnabled(req->hasRole("ticketreturn"));
-       m->addAction(tr("Change Commen&t..."),this,SLOT(changeComment()))
-        ->setEnabled(req->hasRole("setordercomment"));
+       m->addAction(tr("Add Commen&t..."),this,SLOT(changeComment()))
+        ->setEnabled(req->hasRight(req->ROrderAddComment));
        m->addAction(tr("Change Sh&ipping Method..."),this,SLOT(changeShipping()))
         ->setEnabled(req->hasRight(req->ROrderChangeShipping));
        m->addSeparator();
@@ -79,13 +76,10 @@ MOrderWindow::MOrderWindow(QWidget*par,const MOOrder&o)
        m->addAction(tr("&Close"),this,SLOT(close()));
        
        m=mb->addMenu(tr("&Payment"));
-       m->addAction(tr("Receive &Payment..."),this,SLOT(payment()))
-        ->setEnabled(req->hasRight(req->ROrderPay));
-       m->addAction(tr("&Refund..."),this,SLOT(refund()))
-        ->setEnabled(req->hasRight(req->ROrderRefund));
-       m->addAction(tr("Pay with &Voucher..."),this,SLOT(payvoucher()))
-        ->setEnabled(req->hasRight(req->RUseVoucher));
-        
+       m_pay=m->addAction(tr("Receive &Payment..."),this,SLOT(payment()));
+       m_refund=m->addAction(tr("&Refund..."),this,SLOT(refund()));
+       m_payv=m->addAction(tr("Pay with &Voucher..."),this,SLOT(payvoucher()));
+
        m=mb->addMenu(tr("P&rinting"));
        m->addAction(tr("Print &Bill..."),this,SLOT(printBill()));
        m->addAction(tr("Save Bill &as file..."),this,SLOT(saveBill()));
@@ -227,6 +221,12 @@ void MOrderWindow::updateData()
        }
        //refresh
        m_table->resizeColumnsToContents();
+       m_res2order->setEnabled(req->hasRight(req->RReservationToOrder)&&m_order.isReservation());
+       m_cancel->setEnabled(req->hasRight(req->RCancelOrder));
+       m_ship->setEnabled(req->hasRight(req->ROrderMarkShipped)&&m_order.status()==m_order.Placed);
+       m_pay->setEnabled(req->hasRight(req->ROrderPay)&&m_order.needsPayment());
+       m_payv->setEnabled(req->hasRight(req->RUseVoucher)&&m_order.needsPayment());
+       m_refund->setEnabled(req->hasRight(req->ROrderRefund)&&m_order.needsRefund());
 }
 
 void MOrderWindow::itemView()
@@ -663,62 +663,49 @@ void MOrderWindow::initPrintBuffer()
 }
 
 void MOrderWindow::payment()
-{/*TODO
-       if(!m_order.isValid())return;
+{
        //get value
        bool ok;
        int pay=MCentDialog::getCents(this,tr("Enter Payment"),tr("Please enter the amount that has been paid:"),m_order.amountToPay(),m_order.amountToPay(),&ok);
        if(!ok)return;
        if(pay<=0)return;
        //submit
-       QByteArray rq=QByteArray::number(m_order.orderID())+" "+QByteArray::number(pay);
-       if(!req->request("orderpay",rq)){
-               QMessageBox::warning(this,tr("Warning"),tr("Unable to submit payment request."));
+       MTOrderPay op=req->queryOrderPay(m_order.orderid(),pay);
+       if(op.hasError()){
+               QMessageBox::warning(this,tr("Warning"),tr("Error while trying to pay: %1").arg(op.errorString()));
                return;
        }
-       if(req->responseStatus()!=MWebRequest::Ok){
-               QMessageBox::warning(this,tr("Warning"),tr("Error while trying to pay: %1").arg(qApp->translate("php::",req->responseBody())));
-               return;
-       }
-       m_order.setAmountPaid(req->responseBody().trimmed().toInt());
-       m_paid->setText(m_order.amountPaidString());*/
+       m_order=op.getorder();
+       updateData();
 }
 
 void MOrderWindow::payvoucher()
-{/*TODO
+{
        if(!m_order.isValid())return;
        //get voucher
        bool ok;
        QString vid=QInputDialog::getText(this,tr("Enter Voucher"),tr("Please enter the ID of the voucher you want to use:"),QLineEdit::Normal,"",&ok);
        if(!ok)return;
        if(vid=="")return;
-       MVoucher vou(req,vid);
-       if(!vou.isValid()){
-               QMessageBox::warning(this,tr("Warning"),tr("This voucher is not valid."));
-               return;
-       }
        //submit
-       QByteArray rq=vid.toAscii()+"\n"+QByteArray::number(m_order.orderID());
-       if(!req->request("usevoucher",rq)){
-               QMessageBox::warning(this,tr("Warning"),tr("Unable to submit payment request."));
+       MTUseVoucher uv=req->queryUseVoucher(m_order.orderid(),vid);
+       if(uv.hasError()){
+               QMessageBox::warning(this,tr("Warning"),tr("Error while trying to pay with voucher '%1': %2").arg(vid).arg(uv.errorString()));
                return;
        }
-       if(req->responseStatus()!=MWebRequest::Ok){
-               QMessageBox::warning(this,tr("Warning"),tr("Error while trying to pay: %1").arg(qApp->translate("php::",req->responseBody())));
-               return;
-       }
-       QStringList sl=QString::fromAscii(req->responseBody().trimmed()).split("\n");
-       if(sl.size()>1){
-               m_order.setAmountPaid(sl[1].toInt());
-               m_paid->setText(m_order.amountPaidString());
-       }
-       if(sl.size()>0){
-               QMessageBox::information(this,tr("Voucher Info"),tr("Remaining value of this voucher: %1").arg(cent2str(sl[0].toInt())));
-       }*/
+       m_order=uv.getorder();
+       QMessageBox::information(this,tr("Voucher Info"),
+               tr("Successfully paid order %1 with voucher '%2'.\nAmount deducted: %3\nRemaining value of this voucher: %4")
+               .arg(m_order.orderid())
+               .arg(vid)
+               .arg(cent2str(uv.getamount()))
+               .arg(cent2str(uv.getvoucher().value().value()))
+       );
+       updateData();
 }
 
 void MOrderWindow::refund()
-{/*TODO
+{
        if(!m_order.isValid())return;
        //get value
        bool ok;
@@ -726,17 +713,13 @@ void MOrderWindow::refund()
        if(!ok)return;
        if(pay<=0)return;
        //submit
-       QByteArray rq=QByteArray::number(m_order.orderID())+" "+QByteArray::number(pay);
-       if(!req->request("orderrefund",rq)){
-               QMessageBox::warning(this,tr("Warning"),tr("Unable to submit refund request."));
+       MTOrderRefund op=req->queryOrderRefund(m_order.orderid(),pay);
+       if(op.hasError()){
+               QMessageBox::warning(this,tr("Warning"),tr("Error while trying to refund: %1").arg(op.errorString()));
                return;
        }
-       if(req->responseStatus()!=MWebRequest::Ok){
-               QMessageBox::warning(this,tr("Warning"),tr("Error whily trying to refund: %1").arg(qApp->translate("php::",req->responseBody())));
-               return;
-       }
-       m_order.setAmountPaid(req->responseBody().trimmed().toInt());
-       m_paid->setText(m_order.amountPaidString());*/
+       m_order=op.getorder();
+       updateData();
 }
 
 void MOrderWindow::changeItem()
@@ -905,16 +888,18 @@ void MOrderWindow::shipOrder()
 }
 
 void MOrderWindow::changeComment()
-{/*TODO
+{
        //create editor dialog
-       QString cmt=m_order.comment();
+       QString cmt=m_order.comments();
        QDialog d;
-       d.setWindowTitle(tr("Set comment: order %1").arg(m_order.orderID()));
+       d.setWindowTitle(tr("Add comment: order %1").arg(m_order.orderid()));
        QVBoxLayout*vl;
        d.setLayout(vl=new QVBoxLayout);
        QTextEdit*te;
+       QLabel*lab;
+       vl->addWidget(lab=new QLabel("Current comment:\n"+cmt));
+       lab->setWordWrap(true);
        vl->addWidget(te=new QTextEdit,1);
-       te->setPlainText(cmt);
        vl->addSpacing(15);
        QHBoxLayout*hl;
        vl->addLayout(hl=new QHBoxLayout,0);
@@ -927,9 +912,14 @@ void MOrderWindow::changeComment()
        //get status
        if(d.exec()!=QDialog::Accepted)return;
        //send to server
-       m_order.sendComment(cmt=te->toPlainText());
+       MTOrderAddComment ac=req->queryOrderAddComment(m_order.orderid(),te->toPlainText());
+       if(ac.hasError()){
+               QMessageBox::warning(this,tr("Warning"),tr("There was a problem uploading the new comment: %1").arg(ac.errorString()));
+               return;
+       }
+       m_order=ac.getorder();
        //reset display
-       m_comment->setText(cmt);*/
+       updateData();
 }
 
 void MOrderWindow::changeShipping()
index a82d9ba..bdff567 100644 (file)
@@ -20,6 +20,7 @@
 #include "MOOrder.h"
 #include "MOShipping.h"
 
+class QAction;
 class QLabel;
 class QTableView;
 class QStandardItemModel;
@@ -102,6 +103,7 @@ class MOrderWindow:public QMainWindow
                 *m_shipmeth,*m_shipprice,*m_daddr,*m_iaddr,*m_soldby,*m_custname;
                QTableView *m_table;
                QStandardItemModel *m_model;
+               QAction*m_res2order,*m_cancel,*m_ship,*m_pay,*m_payv,*m_refund;
                
                //printing buffer
                struct TickInfo{
index 70cd756..80af929 100644 (file)
                <Input>
                        <Var name="ticketid" type="astring"/>
                </Input>
+               <Call lang="php" method="$this->setticket(WOTicket::fromTableticket(WTticket::getFromDB($this->getticketid())));"/>
                <Output>
                        <Var name="ticket" type="Ticket"/>
                </Output>
                        <Map column="orderid"/>
                        <Map column="isused"/>
                        <Map property="status">
+                               <!-- dummy, since this conversion is only used for existing valid vouchers -->
                                <Call lang="php" method="self::Ok"/>
                        </Map>
                </Mapping>
                <Input>
                        <Var name="voucherid" type="astring"/>
                </Input>
+               <Call lang="php" method="$this->setvoucher(WOVoucher::fromTablevoucher(WTvoucher::getFromDB($this->getvoucherid())));"/>
                <Output>
                        <Var name="voucher" type="Voucher"/>
                </Output>
                <Property name="vouchers" type="List:Voucher"/>
                <Property name="items" type="List:ItemInfo"/>
                
-               <Property name="amountpaid" type="int"/>
                <Property name="status" type="OrderState"/>
                
-               <Property name="amountdue" type="int"/><!-- abstract="yes"? -->
-               <Property name="totalprice" type="int"/><!-- abstract="yes"? -->
-               <Property name="shippingcosts" type="int"/>
+               <Property name="amountpaid" type="int">amount that has been paid for this order</Property>
+               <Property name="amountdue" type="int">amount that needs to be paid, negative if too much has been paid</Property>
+               <Property name="totalprice" type="int">total price for this order (including shipping and all items)</Property>
+               <Property name="shippingcosts" type="int">costs for shipping</Property>
                
                <Property name="shippingtypeid" type="int"/>
                <Property name="shippingtype" type="Shipping"/>
                
                <Property name="status" type="OrderState"/>
                
-               <Property name="amountpaid" type="int"/>
-               <Property name="amountdue" type="int"/>
-               <Property name="totalprice" type="int"/>
-               <Property name="shippingcosts" type="int"/>
+               <Property name="amountpaid" type="int">amount that has been paid for this order</Property>
+               <Property name="amountdue" type="int">amount that needs to be paid, negative if too much has been paid</Property>
+               <Property name="totalprice" type="int">total price for this order (including shipping and all items)</Property>
+               <Property name="shippingcosts" type="int">costs for shipping</Property>
                
                <Property name="ordertime" type="int64"/>
                <Property name="senttime" type="int64"/>
        <Transaction name="CreateReservation"/>
        <Transaction name="ReservationToOrder"/>
        <Transaction name="CancelOrder"/>
-       <Transaction name="OrderPay"/>
-       <Transaction name="OrderRefund"/>
-       <Transaction name="UseVoucher"/>
+       <Transaction name="OrderPay">
+               <Input>
+                       <Var name="orderid" type="int">The order to be paid</Var>
+                       <Var name="amount" type="int">amount offered, it must be positive</Var>
+               </Input>
+               <Call lang="php" method="WOOrder::payForOrder($this);"/>
+               <Output>
+                       <Var name="order" type="Order">fresh copy of the order</Var>
+                       <Var name="amount" type="int">the amount actually taken, it is between 0 and at maximum the amount due for the order, it is never above the offered amount</Var>
+               </Output>
+       </Transaction>
+       
+       <Transaction name="OrderRefund">
+               <Input>
+                       <Var name="orderid" type="int"/>
+                       <Var name="amount" type="int"/>
+               </Input>
+               <Call lang="php" method="WOOrder::refundOrder($this);"/>
+               <Output>
+                       <Var name="order" type="Order"/>
+                       <Var name="amount" type="int"/>
+               </Output>
+       </Transaction>
+       
+       <Transaction name="UseVoucher">
+               <Input>
+                       <Var name="orderid" type="int"/>
+                       <Var name="voucherid" type="string"/>
+               </Input>
+               <Call lang="php" method="WOOrder::payForOrderVoucher($this);"/>
+               <Output>
+                       <Var name="order" type="Order"/>
+                       <Var name="voucher" type="Voucher"/>
+                       <Var name="amount" type="int"/>
+               </Output>
+       </Transaction>
+       
        
        <Transaction name="OrderChangeShipping">
                <Doc>Changes the shipping option and/or price of an order</Doc>
                </Output>
        </Transaction>
        
+       <Transaction name="OrderAddComment">
+               <Input>
+                       <Var name="orderid" type="int">the order to be commented upon</Var>
+                       <Var name="comment" type="string">the comment</Var>
+               </Input>
+               <Call lang="php" method="WOOrder::addComment($this);"/>
+               <Output>
+                       <Var name="order" type="Order">a fresh copy of the changed order</Var>
+               </Output>
+       </Transaction>
+       
        <Transaction name="GetAllShipping">
                <Input/>
                <Call lang="php" method="$this->setshipping(WOShipping::fromTableArrayshipping(WTshipping::selectFromDB()));"/>
index 8a60598..54c584c 100644 (file)
@@ -222,6 +222,144 @@ class WOOrder extends WOOrderAbstract
                //return
                $trans->setorder(WOOrder::fromTableorder($ord));
        }
+       
+       /**called from the OrderAddComment transaction*/
+       public static function addComment($trans)
+       {
+               global $session;
+               //get order
+               $ord=WTorder::getFromDB($trans->getorderid());
+               if($ord===false){
+                       $trans->abortWithError(tr("Order ID is not valid."));
+                       return;
+               }
+               //modify comment
+               $cmt=trim($ord->comments);
+               $nc=trim($trans->getcomment()."");
+               if($nc!=""){
+                       if($cmt!="")$cmt.="\n";
+                       $cmt.="[".$session->currentUserName()." ".date("Y-m-d G:i")."] ".$nc;
+                       $ord->comments=$cmt;
+                       $ord->update();
+               }
+               //return
+               $trans->setorder(WOOrder::fromTableorder($ord));
+       }
+       
+       /**called from the OrderPay transaction*/
+       public static function payForOrder($trans)
+       {
+               //get order
+               $ord=WTorder::getFromDB($trans->getorderid());
+               if($ord===false){
+                       $trans->abortWithError(tr("Order ID is not valid."));
+                       return;
+               }
+               //calculate payment
+               $pay=$trans->getamount()+0;
+               if($pay<0){//negative: error out
+                       $trans->abortWithError(tr("Amount to be paid must be positive."));
+                       return;
+               }
+               $order=WOOrder::fromTableorder($ord);
+               $max=$order->prop_amountdue;
+               if($max<=0)$pay=0;else
+               if($max<$pay)$pay=$max;
+               if($pay==0){//zero: ignore it
+                       $trans->setorder($order);
+                       $trans->setamount(0);
+                       return;
+               }
+               //make corrections
+               $ord->amountpaid+=$pay;
+               $ord->update();
+               //return (redo object conversion: calculated data has changed!)
+               $trans->setorder(WOOrder::fromTableorder($ord));
+               $trans->setamount($pay);
+       }
+       
+       /**called from the UseVoucher transaction*/
+       public static function payForOrderVoucher($trans)
+       {
+               //get order
+               $ord=WTorder::getFromDB($trans->getorderid());
+               if($ord===false){
+                       $trans->abortWithError(tr("Order ID is not valid."));
+                       return;
+               }
+               //get and verify voucher
+               $vou=WTvoucher::getFromDB($trans->getvoucherid());
+               if($vou===false){
+                       $trans->abortWithError(tr("Voucher is not valid!"));
+                       return;
+               }
+               $vord=WOOrder::fromTableorder(WTorder::getFromDB($vou->orderid));
+               if($vord===false){
+                       $trans->abortWithError(tr("Voucher is not valid!"));
+                       return;
+               }
+               if($vord->get_amountdue()>0){
+                       $trans->abortWithError(tr("Voucher cannot be used: it has not been paid for."));
+                       return;
+               }
+               //calculate payment
+               $pay=$vou->value+0;
+               if($pay<0){//negative: error out
+                       $trans->abortWithError(tr("Amount to be paid must be positive."));
+                       return;
+               }
+               $order=WOOrder::fromTableorder($ord);
+               $max=$order->prop_amountdue;
+               if($max<=0)$pay=0;else
+               if($max<$pay)$pay=$max;
+               if($pay==0){//zero: ignore it
+                       $trans->setorder($order);
+                       $trans->setamount(0);
+                       return;
+               }
+               //make corrections
+               $ord->amountpaid+=$pay;
+               $ord->update();
+               $vou->value-=$pay;
+               $vou->isused=true;
+               $vou->update();
+               //return (redo object conversion: calculated data has changed!)
+               $trans->setorder(WOOrder::fromTableorder($ord));
+               $trans->setvoucher(WOVoucher::fromTablevoucher($vou));
+               $trans->setamount($pay);
+       }
+       
+       /**called from the OrderRefund transaction*/
+       public static function refundOrder($trans)
+       {
+               //get order
+               $ord=WTorder::getFromDB($trans->getorderid());
+               if($ord===false){
+                       $trans->abortWithError(tr("Order ID is not valid."));
+                       return;
+               }
+               //calculate payment
+               $pay=$trans->getamount()+0;
+               if($pay<0){//negative: error out
+                       $trans->abortWithError(tr("Amount to be refunded must be positive."));
+                       return;
+               }
+               $order=WOOrder::fromTableorder($ord);
+               $max=0-$order->prop_amountdue; //refund is negative due
+               if($max<=0)$pay=0;else
+               if($max<$pay)$pay=$max;
+               if($pay==0){//zero: ignore it
+                       $trans->setorder($order);
+                       $trans->setamount(0);
+                       return;
+               }
+               //make corrections
+               $ord->amountpaid-=$pay;
+               $ord->update();
+               //return (redo object conversion: calculated data has changed!)
+               $trans->setorder(WOOrder::fromTableorder($ord));
+               $trans->setamount($pay);
+       }
 };
 
 ?>
\ No newline at end of file