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.
+
<!-- ************************************************************************************
************************************************************************************
************************************************************************************ -->
int MCentSpinBox::value()const
{
- qDebug("current value: %i",mval);
+// qDebug("current value: %i",mval);
return mval;
}
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);
//
//
+#include "centbox.h"
#include "checkdlg.h"
#include "eventedit.h"
#include "eventsummary.h"
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()));
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;
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();
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()
{
}
}
+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);
$vc->dumpXml();
}
+
+
?>
\ No newline at end of file
//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"),
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));