use vouchers outside system
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Mon, 22 Dec 2008 15:31:45 +0000 (15:31 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Mon, 22 Dec 2008 15:31:45 +0000 (15:31 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@225 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

doc/prog_protocol.html
src/centbox.cpp
src/overview.cpp
src/overview.h
www/inc/classes/voucher.php
www/machine.php

index 2f3df99..84d4630 100644 (file)
@@ -763,6 +763,10 @@ Only privileged users should be able to do this (if anybody at all).
 
 The <tt>getvoucher</tt> transaction can be used to retrieve information about a specific voucher. The request just contains the voucher ID, the response contains the XML representation of it (see the Voucher tag in the Order XML object for details).
 
+<h3>Using a Voucher outside the system</h3>
+
+The <tt>usevoucheroutside</tt> transaction can be used to remove value from a voucher without logging it to a specific order. The request contains the voucherid in the first line and the value to take in the second line. The response contains the actual value taken in the first line and the remaining value of the voucher in the second line.
+
 <!-- ************************************************************************************
      ************************************************************************************
      ************************************************************************************ -->
index c33377b..a8db050 100644 (file)
@@ -35,7 +35,7 @@ MCentSpinBox::MCentSpinBox(QWidget*parent,int value,int maxValue)
 
 int MCentSpinBox::value()const
 {
-       qDebug("current value: %i",mval);
+//     qDebug("current value: %i",mval);
        return mval;
 }
 
@@ -44,7 +44,7 @@ void MCentSpinBox::setValue(int m)
        if(m>mmax)m=mmax;
        if(m<mmin)m=mmin;
        if(mval==m)return;
-       qDebug("setval %i",m);
+//     qDebug("setval %i",m);
        mval=m;
        lineEdit()->setText(cent2str(m));
        emit valueChanged(m);
index d323dcc..be375f7 100644 (file)
@@ -10,6 +10,7 @@
 //
 //
 
+#include "centbox.h"
 #include "checkdlg.h"
 #include "eventedit.h"
 #include "eventsummary.h"
@@ -111,6 +112,8 @@ MOverview::MOverview(MWebRequest*mw,QString pk)
        m->addAction(tr("Return &voucher..."),this,SLOT(voucherReturn()));
        m->addSeparator();
        m->addAction(tr("Edit &Shipping Options..."),this,SLOT(editShipping()));
+       m->addSeparator();
+       m->addAction(tr("&Deduct from voucher..."),this,SLOT(deductVoucher()));
        
        m=mb->addMenu(tr("C&onfigure"));
        m->addAction(tr("&Auto-Refresh settings..."),this,SLOT(setRefresh()));
@@ -1345,6 +1348,50 @@ void MOverview::voucherReturn()
        if(r!="")QMessageBox::warning(this,tr("Warning"),trUtf8(r.toUtf8()));
 }
 
+void MOverview::deductVoucher()
+{
+       //get voucher ID and amount
+       QDialog d;
+       d.setWindowTitle(tr("Deduct from Voucher"));
+       QGridLayout *gl;
+       d.setLayout(gl=new QGridLayout);
+       gl->addWidget(new QLabel(tr("Using a voucher to pay outside the system.")),0,0,1,2);
+       
+       QLineEdit*vid;
+       MCentSpinBox*cent;
+       gl->addWidget(new QLabel(tr("Amount to deduct:")),1,0);
+       gl->addWidget(cent=new MCentSpinBox,1,1);
+       gl->addWidget(new QLabel(tr("Voucher ID:")),2,0);
+       gl->addWidget(vid=new QLineEdit,2,1);
+       
+       gl->setRowMinimumHeight(3,15);
+       QHBoxLayout*hl;
+       gl->addLayout(hl=new QHBoxLayout,4,0,1,2);
+       hl->addStretch(10);
+       QPushButton*p;
+       hl->addWidget(p=new QPushButton(tr("OK")));
+       connect(p,SIGNAL(clicked()),&d,SLOT(accept()));
+       hl->addWidget(p=new QPushButton(tr("Cancel")));
+       connect(p,SIGNAL(clicked()),&d,SLOT(reject()));
+       if(d.exec()!=QDialog::Accepted)return;
+       if(vid->text().trimmed()=="" || cent->value()<=0)return;
+       //query server
+       QByteArray r=vid->text().trimmed().toAscii()+"\n"+QString::number(cent->value()).toAscii();
+       if(!req->request("usevoucheroutside",r)){
+               QMessageBox::warning(this,tr("Warning"),tr("Request failed."));
+               return;
+       }
+       if(req->responseStatus()!=MWebRequest::Ok){
+               QMessageBox::warning(this,tr("Warning"),tr("Request failed."));
+               return;
+       }
+       QStringList sl=QString::fromAscii(req->responseBody().trimmed()).split("\n");
+       int tak=-1,rem=-1;
+       if(sl.size()>0)tak=sl[0].trimmed().toInt();
+       if(sl.size()>1)rem=sl[1].trimmed().toInt();
+       QMessageBox::information(this,tr("Deducted from Voucher"),tr("Value taken from voucher: %1\nValue remaining on voucher: %2").arg(cent2str(tak)).arg(cent2str(rem)));
+}
+
 void MOverview::refreshData()
 {
        QSettings set;
index d7279e4..16cb73f 100644 (file)
@@ -139,6 +139,8 @@ class MOverview:public QMainWindow
                void orderByCustomer();
                /**find and display order by order ID*/
                void orderByOrder();
+               /**deduct some money from a voucher (to pay outside the system)*/
+               void deductVoucher();
                
                /**refresh data that we can refresh*/
                void refreshData();
index 7fbf1f4..8c810f0 100644 (file)
@@ -224,6 +224,42 @@ class Voucher
                return true;
        }
        
+       /**use the voucher to pay for something not in the system; return true on success*/
+       public function payForOutside($amount)
+       {
+               //pre-check
+               if(!$this->isValid() || !$this->canPay())return false;
+               if($amount < 0)return false;
+               //now go to the DB
+               global $db;
+               $db->beginTransaction();
+               //get voucher data and recheck
+               $vres=$db->select("voucher","*","voucherid=".$db->escapeString($this->voucherid));
+               if($vres===false || count($vres)<1){
+                       $db->rollbackTransaction();
+                       return false;
+               }
+               if($vres[0]["value"]<=0){
+                       $db->rollbackTransaction();
+                       return false;
+               }
+               //get amount to swap
+               $pay=$vres[0]["value"];
+               if($amount<$pay)$pay=$amount;
+               //store corrected voucher
+               $this->value=$vres[0]["value"]-$pay;
+               $b=$db->update("voucher",array("value"=>$this->value,"isused"=>1),"voucherid=".$db->escapeString($this->voucherid))!==false;
+               //if anything went wrong: roll back
+               if(!$b){
+                       $db->rollbackTransaction();
+                       return false;
+               }
+               $db->mkLog(array("voucherid"=>$this->voucherid,"vouchervalue"=>$this->value,"moved"=>$pay),tr("pay with voucher outside system"));
+               //whoo. got it!
+               $db->commitTransaction();
+               return true;
+       }
+       
        /**dumps the XML representation of the voucher*/
        function dumpXml()
        {
@@ -299,6 +335,32 @@ function useVoucherXml($txt)
        }
 }
 
+function useVoucher2Xml($txt)
+{
+       //split data
+       $splt=explode("\n",$txt);
+       if(count($splt)<2){
+               header("X-MagicSmoke-Status: SyntaxError");
+               echo tr("Expected two arguments: voucher id and amount to deduct.");
+               return;
+       }
+       $vc=new Voucher(trim($splt[0]));
+       if(!$vc->isValid()){
+               header("X-MagicSmoke-Status: Error");
+               echo tr("Invalid voucher id.");
+               return;
+       }
+       $val=$vc->remainingValue();
+       if($vc->payForOutside(trim($splt[1])+0)){
+               header("X-MagicSmoke-Status: Ok");
+               $val2=$vc->remainingValue();
+               print(($val-$val2)."\n".$val2);
+       }else{
+               header("X-MagicSmoke-Status: Error");
+               echo tr("Unable to process payment via voucher.");
+       }
+}
+
 function getVoucherXml($vid)
 {
        $vc=new Voucher($vid);
@@ -311,4 +373,6 @@ function getVoucherXml($vid)
        $vc->dumpXml();
 }
 
+
+
 ?>
\ No newline at end of file
index 8116499..9fe7ed3 100644 (file)
@@ -46,7 +46,7 @@ $ALLOWEDREQUESTS=array(
        //ticket management
        tr("getticket"),tr("useticket"),tr("changeticketprice"),tr("ticketreturn"),
        //voucher management
-       tr("getvoucherprices"),tr("cancelvoucher"),tr("emptyvoucher"),tr("usevoucher"),tr("getvoucher"),
+       tr("getvoucherprices"),tr("cancelvoucher"),tr("emptyvoucher"),tr("usevoucher"),tr("usevoucheroutside"),tr("getvoucher"),
        //templates
        tr("gettemplatelist"),tr("gettemplate"),tr("settemplate"),tr("settemplatedescription"),
        tr("deletetemplate"),
@@ -459,6 +459,11 @@ if($SMOKEREQUEST=="usevoucher"){
        useVoucherXml(trim($REQUESTDATA));
        exit();
 }
+//use a voucher to pay
+if($SMOKEREQUEST=="usevoucheroutside"){
+       useVoucher2Xml(trim($REQUESTDATA));
+       exit();
+}
 //get info about a voucher
 if($SMOKEREQUEST=="getvoucher"){
        getVoucherXml(trim($REQUESTDATA));