implemented deduct voucher
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Fri, 14 May 2010 12:42:42 +0000 (12:42 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Fri, 14 May 2010 12:42:42 +0000 (12:42 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@447 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/mwin/overview.cpp
wob/order.wolf
www/inc/wext/order.php

index 74fe30d..31967c5 100644 (file)
@@ -24,6 +24,8 @@
 #include "entrancetab.h"
 #include "aclwin.h"
 
+#include "centbox.h"
+
 #include <QBoxLayout>
 #include <QCheckBox>
 #include <QComboBox>
@@ -221,7 +223,7 @@ void MOverview::ticketReturn()
 }
 
 void MOverview::deductVoucher()
-{/*TODO
+{
        //get voucher ID and amount
        QDialog d;
        d.setWindowTitle(tr("Deduct from Voucher"));
@@ -248,20 +250,20 @@ void MOverview::deductVoucher()
        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."));
+       MTDeductVoucher dv=req->queryDeductVoucher(vid->text(),cent->value());
+       if(dv.hasError()){
+               QMessageBox::warning(this,tr("Warning"),tr("Unable to deduct voucher: %1").arg(dv.errorString()));
                return;
        }
-       if(req->responseStatus()!=MSInterface::Ok){
-               QMessageBox::warning(this,tr("Warning"),tr("Request failed."));
+       if(dv.getamount().value()==0){
+               QMessageBox::warning(this,tr("Warning"),tr("Voucher does not contain enough money. Money left: %1").arg(dv.getvoucher().value().value()));
                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)));*/
+       QMessageBox::information(this,tr("Deducted from Voucher"),
+         tr("Value taken from voucher: %1\nValue remaining on voucher: %2")
+         .arg(cent2str(dv.getamount()))
+         .arg(cent2str(dv.getvoucher().value().value()))
+       );
 }
 
 void MOverview::refreshData()
index f3eceae..be3ea52 100644 (file)
        </Transaction>
        
        <Transaction name="UseVoucher">
+               <Doc>Use a voucher to pay for an order. The system automatically deducts as much as is due for the order or as much as is left on the voucher - whichever is lower. If the voucher is not paid for the transaction will fail. The transaction will never transfer money from an order back to the voucher.</Doc>
                <Input>
-                       <Var name="orderid" type="int"/>
-                       <Var name="voucherid" type="string"/>
+                       <Var name="orderid" type="int">ID of the order that is to be paid for</Var>
+                       <Var name="voucherid" type="astring">ID of the voucher that is to be used for payment</Var>
                </Input>
                <Call lang="php" method="WOOrder::payForOrderVoucher($this);"/>
                <Output>
-                       <Var name="order" type="Order"/>
-                       <Var name="voucher" type="Voucher"/>
-                       <Var name="amount" type="int"/>
+                       <Var name="order" type="Order">The full order object after the transaction</Var>
+                       <Var name="voucher" type="Voucher">The full voucher object after the transaction</Var>
+                       <Var name="amount" type="int">The amount that was deducted</Var>
+               </Output>
+       </Transaction>
+       
+       <Transaction name="DeductVoucher">
+               <Doc>Deducts an amount from a voucher - this can be used for items that are not in the MagicSmoke shop repository. Fails if the voucher is not paid for yet. Will not transfer anything if the voucher does not contain enough money.</Doc>
+               <Input>
+                       <Var name="voucherid" type="astring">ID of the voucher to be used</Var>
+                       <Var name="amount" type="int">Amount to be deducted. Must be positive.</Var>
+               </Input>
+               <Call lang="php" method="WOOrder::deductVoucher($this);"/>
+               <Output>
+                       <Var name="voucher" type="Voucher">The full voucher object after the transaction</Var>
+                       <Var name="amount" type="int">The amount actually transferred. Can only be zero (not enough left on the voucher) or the same as above.</Var>
                </Output>
        </Transaction>
        
index 0033bd3..050f087 100644 (file)
@@ -565,6 +565,51 @@ class WOOrder extends WOOrderAbstract
                //return order
                $trans->setorder(WOOrder::fromTableorder(WTorder::getFromDB($tick->orderid)));
        }
+       
+       /**called from the DeductVoucher transaction*/
+       static public function deductVoucher($trans)
+       {
+               //verify amount
+               $amt=$trans->getamount()+0;
+               if($amt<0){//negative: error out
+                       $trans->abortWithError(tr("Amount to be paid must be positive."));
+                       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->getamountdue()>0){
+                       $trans->abortWithError(tr("Voucher cannot be used: it has not been paid for."));
+                       return;
+               }
+               //calculate payment
+               $lft=$vou->value+0;
+               if($lft<0){//negative: error out
+                       $trans->abortWithError(tr("Internal error: negative voucher."));
+                       return;
+               }
+               if($lft<$amt){
+                       //not enough left, tell user
+                       $trans->setamount(0);
+                       $trans->setvoucher(WOVoucher::fromTablevoucher($vou));
+                       return;
+               }
+               //make corrections
+               $vou->value-=$amt;
+               $vou->isused=true;
+               $vou->update();
+               //return
+               $trans->setamount($amt);
+               $trans->setvoucher(WOVoucher::fromTablevoucher($vou));
+       }
 };
 
 ?>
\ No newline at end of file