render orders according to new capabilities (added voucher + shipping)
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 29 Nov 2008 17:26:00 +0000 (17:26 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 29 Nov 2008 17:26:00 +0000 (17:26 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@204 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/misc.cpp
src/misc.h
src/odtrender.cpp
src/order.cpp
src/order.h
src/orderwin.cpp
src/orderwin.h
src/shipping.cpp
src/shipping.h
src/templates.cpp

index 7f23964..c5c60ce 100644 (file)
@@ -30,6 +30,20 @@ QString htmlize(QString str)
        return out;
 }
 
+QString xmlize(QString str)
+{
+       QString out;
+       for(int i=0;i<str.size();i++){
+               QChar c=str[i];
+               ushort ci=c.unicode();
+               if(c=='<')out+="&lt;";else
+               if(c=='>')out+="&gt;";else
+               if(c=='&')out+="&amp;";else
+               if(c.isSpace()||(c.unicode()>=32&&c.unicode()<=0x7f))out+=c;
+               else out+="&#"+QString::number(ci)+";";
+       }
+       return out;
+}
 QString cent2str(int c)
 {
        QString ret=QCoreApplication::translate("misc","%1.%2","price with decimal dot");
index 8062066..a816068 100644 (file)
@@ -19,6 +19,9 @@
 /**converts special HTML characters into harmless &-codes, so the text can be included*/
 QString htmlize(QString str);
 
+/**converts special XML characters into harmless &-codes, so the text can be included*/
+QString xmlize(QString str);
+
 /**converts a cent value into a localized string*/
 QString cent2str(int c);
 
index 85972ba..15da178 100644 (file)
@@ -11,6 +11,7 @@
 //
 
 #include "odtrender.h"
+#include "misc.h"
 
 #include <QDir>
 #include <QFile>
@@ -31,18 +32,25 @@ class MOdtRendererPrivate
        protected:
                friend class MOdtRenderer;
                
+               //methods that the parent uses
                MOdtRendererPrivate(QString file,MOdtRenderer*p);
                ~MOdtRendererPrivate();
                
                void render(QIODevice*);
                
                QString render(QString);
+               
+               //data the parent uses
+               QString extension;
+               
+       private:
+               //methods that the parent does not call
                QString renderLine(QString,QString,int);
                
                QString getVariable(QString varname);
                QString getLoopVariable(QString loopname,int iteration,QString varname);
                
-       private:
+               //data the parent does not access
                MOdtRenderer*parent;
                QUnZip temp;
                QFile tfile;
@@ -51,6 +59,7 @@ class MOdtRendererPrivate
 MOdtRenderer::MOdtRenderer(MTemplate file)
 {
        d=new MOdtRendererPrivate(file.cacheFileName(),this);
+       d->extension=file.targetExtension();
 }
 
 MOdtRendererPrivate::MOdtRendererPrivate(QString file,MOdtRenderer*p)
@@ -97,7 +106,7 @@ void MOdtRenderer::renderToPrinter()
        //generate temporary file
        QTemporaryFile tfile;
        tfile.setAutoRemove(false);
-       tfile.setFileTemplate(QDir::tempPath()+"/msmoke_XXXXXX.odt");
+       tfile.setFileTemplate(QDir::tempPath()+"/msmoke_XXXXXX."+d->extension);
        tfile.open();
        QString tname=tfile.fileName();
        //render
@@ -228,11 +237,11 @@ QString MOdtRendererPrivate::renderLine(QString line,QString loop,int lpos)
                                                //or any loop at all...
                                                if(loop!="" && loop==vl[0]){
                                                        //get value
-                                                       ret+=getLoopVariable(loop,lpos,vl[1]);
+                                                       ret+=xmlize(getLoopVariable(loop,lpos,vl[1]));
                                                }
                                        }else
                                                //this is a normal variable, get valie
-                                               ret+=getVariable(vname);
+                                               ret+=xmlize(getVariable(vname));
                                }
                                //reset mode
                                isvar=false;
index 57b7f30..852ac67 100644 (file)
@@ -889,9 +889,9 @@ MVoucher& MVoucher::operator=(const MVoucher&v)
 QString MVoucher::voucherID()const{return m_id;}
 
 int MVoucher::price()const{return m_price;}
-QString MVoucher::priceString()const{return cent2str(m_price);}
+QString MVoucher::priceString(int off)const{return cent2str(m_price+off);}
 int MVoucher::value()const{return m_value;}
-QString MVoucher::valueString()const{return cent2str(m_value);}
+QString MVoucher::valueString(int off)const{return cent2str(m_value+off);}
 
 bool MVoucher::isValid()const{return req!=0 && m_id!="";}
 
index 1c8a4bd..cf02525 100644 (file)
@@ -180,11 +180,11 @@ class MVoucher
                /**returns the price in cent of the voucher (what it costs)*/
                int price()const;
                /**returns the price of the voucher as string*/
-               QString priceString()const;
+               QString priceString(int off=0)const;
                /**returns the remaining value in cent of the voucher (what it is worth)*/
                int value()const;
                /**returns the remaining value of the voucher as string*/
-               QString valueString()const;
+               QString valueString(int off=0)const;
                
                /**returns whether this is a valid voucher object*/
                bool isValid()const;
index c6b3ee2..0b8bc4e 100644 (file)
 //
 //
 
-#include "orderwin.h"
 #include "event.h"
-#include "webrequest.h"
-#include "ticketrender.h"
 #include "labeldlg.h"
+#include "misc.h"
 #include "odtrender.h"
+#include "orderwin.h"
+#include "ticketrender.h"
+#include "webrequest.h"
 
 #include <QApplication>
 #include <QBoxLayout>
@@ -338,11 +339,13 @@ void MOrderWindow::printBill()
                m_sentdate->setText(m_order.sentDateTimeStr());
        }
        //print bill
+       initPrintBuffer();
        MOdtSignalRenderer rend(tf);
        connect(&rend,SIGNAL(getVariable(QString,int,QString&)),this,SLOT(getVariable(QString,int,QString&)));
        connect(&rend,SIGNAL(getLoopIterations(QString,int&)),this,SLOT(getLoopIterations(QString,int&)));
        connect(&rend,SIGNAL(getLoopVariable(QString,int,QString,int,QString&)),this,SLOT(getLoopVariable(QString,int,QString,int,QString&)));
        rend.renderToPrinter();
+       donePrintBuffer();
 }
 
 void MOrderWindow::saveBill()
@@ -350,7 +353,7 @@ void MOrderWindow::saveBill()
        //get template
        MTemplate tf=req->getTemplate("bill");
        if(!tf.isValid()){
-               QMessageBox::warning(this,tr("Warning"),tr("Unable to get template file (eventsummary). Giving up."));
+               QMessageBox::warning(this,tr("Warning"),tr("Unable to get template file (bill). Giving up."));
                return;
        }
        //get target file name
@@ -374,11 +377,13 @@ void MOrderWindow::saveBill()
                m_sentdate->setText(m_order.sentDateTimeStr());
        }
        //render bill
+       initPrintBuffer();
        MOdtSignalRenderer rend(tf);
        connect(&rend,SIGNAL(getVariable(QString,int,QString&)),this,SLOT(getVariable(QString,int,QString&)));
        connect(&rend,SIGNAL(getLoopIterations(QString,int&)),this,SLOT(getLoopIterations(QString,int&)));
        connect(&rend,SIGNAL(getLoopVariable(QString,int,QString,int,QString&)),this,SLOT(getLoopVariable(QString,int,QString,int,QString&)));
        rend.renderToFile(fname);
+       donePrintBuffer();
 }
 
 void MOrderWindow::getVariable(QString vn,int av,QString&value)
@@ -399,21 +404,26 @@ void MOrderWindow::getVariable(QString vn,int av,QString&value)
        if(vn=="COMMENT")value=m_order.comment();else
        if(vn=="AMOUNTTOPAY")value=m_order.amountToPayStr(av);else
        if(vn=="AMOUNTTOREFUND")value=m_order.amountToRefundStr(av);else
-       if(vn=="TICKETS")value=QString::number(m_model->rowCount()+av);
-       if(vn=="ADDRESSLINES")value=QString::number(m_order.customer().address().split("\n").size()+av);
+       if(vn=="TICKETS")value=QString::number(printBuffer.tickets.size()+av);else
+       if(vn=="ACCTICKETS")value=QString::number(printBuffer.tickinfo.size()+av);else
+       if(vn=="VOUCHERS")value=QString::number(printBuffer.vouchers.size()+av);else
+       if(vn=="ADDRESSLINES")value=QString::number(m_order.customer().address().split("\n").size()+av);else
+       if(vn=="SHIPPING")value=m_order.shipping().description();else
+       if(vn=="SHIPPINGPRICE")value=m_order.shipping().priceString(av);
 }
 
 void MOrderWindow::getLoopIterations(QString loopname,int&iterations)
 {
-       if(loopname=="TICKETS")iterations=m_model->rowCount();
+       if(loopname=="TICKETS")iterations=printBuffer.tickets.size();
+       if(loopname=="ACCTICKETS")iterations=printBuffer.tickinfo.size();
+       if(loopname=="VOUCHERS")iterations=printBuffer.vouchers.size();
        if(loopname=="ADDRESSLINES")iterations=m_order.customer().address().split("\n").size();
 }
 void MOrderWindow::getLoopVariable(QString loopname,int it,QString vn,int av,QString&value)
 {
        if(loopname=="TICKETS"){
-               if(it<0 || it>=m_model->rowCount())return;
-               
-               QList<MTicket>tickets=m_order.tickets();
+               QList<MTicket> &tickets=printBuffer.tickets;
+               if(it<0 || it>=tickets.size())return;
                
                if(vn=="PRICE")value=tickets[it].priceString(av);else
                if(vn=="ID")value=tickets[it].ticketID();else
@@ -423,6 +433,25 @@ void MOrderWindow::getLoopVariable(QString loopname,int it,QString vn,int av,QSt
                if(vn=="STARTTIME")value=tickets[it].event().startTimeString();else
                if(vn=="ENDTIME")value=tickets[it].event().endTimeString();else
                if(vn=="ROOM")value=tickets[it].event().room();
+       }else if(loopname=="ACCTICKETS"){
+               QList<TickInfo> &tickets=printBuffer.tickinfo;
+               if(it<0 || it>=tickets.size())return;
+               
+               if(vn=="PRICE")value=tickets[it].proto.priceString(av);else
+               if(vn=="FULLPRICE")value=cent2str(tickets[it].proto.price()*tickets[it].amount+av);else
+               if(vn=="TITLE")value=tickets[it].proto.event().title();else
+               if(vn=="ARTIST")value=tickets[it].proto.event().artist();else
+               if(vn=="DATE")value=tickets[it].proto.event().startDateString();else
+               if(vn=="STARTTIME")value=tickets[it].proto.event().startTimeString();else
+               if(vn=="ENDTIME")value=tickets[it].proto.event().endTimeString();else
+               if(vn=="ROOM")value=tickets[it].proto.event().room();else
+               if(vn=="AMOUNT")value=QString::number(tickets[it].amount+av);
+       }else if(loopname=="VOUCHERS"){
+               if(it<0 || it>=printBuffer.vouchers.size())return;
+               
+               if(vn=="PRICE")value=printBuffer.vouchers[it].priceString(av);else
+               if(vn=="VALUE")value=printBuffer.vouchers[it].valueString(av);else
+               if(vn=="ID")value=printBuffer.vouchers[it].voucherID();
        }else if(loopname=="ADDRESSLINES"){
                QStringList lst=m_order.customer().address().split("\n");
                if(it<0 || it>=lst.size())return;
@@ -431,6 +460,49 @@ void MOrderWindow::getLoopVariable(QString loopname,int it,QString vn,int av,QSt
                return /*empty handed*/;
 }
 
+void MOrderWindow::donePrintBuffer()
+{
+       printBuffer.tickets.clear();
+       printBuffer.vouchers.clear();
+       printBuffer.tickinfo.clear();
+}
+
+static inline bool compare(const MTicket&a,const MTicket&b)
+{
+       if(a.eventID()!=b.eventID())return false;
+       if(a.price()!=b.price())return false;
+       return true;
+}
+
+void MOrderWindow::initPrintBuffer()
+{
+       //clear
+       donePrintBuffer();
+       //get tickets (only valid ones)
+       QList<MTicket>tlst=m_order.tickets();
+       for(int i=0;i<tlst.size();i++)
+               if(tlst[i].isValid())
+                       printBuffer.tickets.append(tlst[i]);
+       //accumulated view on tickets
+       for(int i=0;i<printBuffer.tickets.size();i++){
+               MTicket t=printBuffer.tickets[i];
+               bool found=false;
+               for(int j=0;j<printBuffer.tickinfo.size();j++){
+                       if(compare(printBuffer.tickinfo[j].proto,t)){
+                               found=true;
+                               printBuffer.tickinfo[j].amount++;
+                               break;
+                       }
+               }
+               if(!found)printBuffer.tickinfo.append(t);
+       }
+       //get valid vouchers
+       QList<MVoucher>vlst=m_order.vouchers();
+       for(int i=0;i<vlst.size();i++)
+               if(!vlst[i].isCancelled())
+                       printBuffer.vouchers.append(vlst[i]);
+}
+
 void MOrderWindow::payment()
 {
        if(!m_order.isValid())return;
index 0f07c9b..3a1467c 100644 (file)
@@ -108,6 +108,22 @@ class MOrderWindow:public QMainWindow
                 *m_shipmeth,*m_shipprice;
                QTableView *m_table;
                QStandardItemModel *m_model;
+               
+               //printing buffer
+               struct TickInfo{
+                       TickInfo(const MTicket&t):proto(t){amount=1;}
+                       TickInfo(const TickInfo&t):proto(t.proto){amount=t.amount;}
+                       TickInfo(){amount=0;}
+                       MTicket proto;
+                       int amount;
+               };
+               struct PrintBuffer{
+                       QList<MTicket> tickets;
+                       QList<MVoucher> vouchers;
+                       QList<TickInfo> tickinfo;
+               }printBuffer;
+               void initPrintBuffer();
+               void donePrintBuffer();
 };
 
 class MTicketRenderer;
index 22e7521..1476a45 100644 (file)
@@ -10,6 +10,7 @@
 //
 //
 
+#include "misc.h"
 #include "shipping.h"
 #include "webrequest.h"
 
@@ -70,9 +71,9 @@ bool MShipping::isValid()const
 int MShipping::id()const{return m_id;}
 int MShipping::price()const{return m_price;}
 
-QString MShipping::priceString()const
+QString MShipping::priceString(int off)const
 {
-       return QString::number(m_price/100)+QCoreApplication::translate("MShipping",".","decimal dot")+QString().sprintf("%02d",m_price%100);
+       return cent2str(m_price+off);
 }
 
 bool MShipping::webUsable()const{return m_web;}
index 2587dcc..f0be5b4 100644 (file)
@@ -42,7 +42,7 @@ class MShipping
                int price()const;
                
                /**returns the price as string*/
-               QString priceString()const;
+               QString priceString(int off=0)const;
                
                /**returns whether the type is usable via web interface*/
                bool webUsable()const;
index 6da43bd..f0bd492 100644 (file)
@@ -89,6 +89,8 @@ void MTemplateStore::updateTemplates(bool force)
        QString dname=req->dataDir()+"/templates/";
        QSettings set;
        set.beginGroup("templates/"+profileid);
+       //make sure directory exists
+       QDir(req->dataDir()).mkpath("templates");
        //do we need an update yet?
        QDateTime last=QDateTime::fromTime_t(set.value("lastupdate",0).toInt()+300);
        if(last>=QDateTime::currentDateTime() && !force)return;