#include "customerdlg.h"
#include "msinterface.h"
#include "misc.h"
+#include "lambda.h"
+#include "vlambda.h"
-#include "MTGetAllCustomerNames"
-#include "MTGetCustomer"
-#include "MTDeleteCustomer"
#include "MTChangeCustomer"
#include "MTChangeCustomerMail"
-#include "MTCreateCustomer"
-#include "MTResetCustomerPassword"
-#include "MTGetAllContactTypes"
#include "MTCreateContactType"
#include "MTCreateCountry"
-#include "MTGetAllCountries"
+#include "MTCreateCustomer"
+#include "MTDeleteCustomer"
#include "MTGetAddress"
+#include "MTGetAllContactTypes"
+#include "MTGetAllCountries"
+#include "MTGetAllCustomerNames"
+#include "MTGetCreateCustomerHints"
+#include "MTGetCustomer"
+#include "MTResetCustomerPassword"
#include <QBoxLayout>
#include <QCheckBox>
#include <QComboBox>
+#include <QDebug>
#include <QApplication>
#include <QDomElement>
#include <QFormLayout>
#include <QPushButton>
#include <QSignalMapper>
#include <QSortFilterProxyModel>
+#include <QStackedLayout>
#include <QStandardItemModel>
#include <QTableView>
#include <QTextEdit>
void MCustomerListDialog::newCustomer()
{
- MCustomerDialog cd(MOCustomer(),this);
- if(cd.exec()==QDialog::Accepted)
- updateList(cd.getCustomer().id());
+ MOCustomer c=MNewCustomerWizard::getNewCustomer(this);
+ if(c.isValid())
+ updateList(c.id());
+// MCustomerDialog cd(MOCustomer(),this);
+// if(cd.exec()==QDialog::Accepted)
+// updateList(cd.getCustomer().id());
}
void MCustomerListDialog::editCustomer()
{
}
if(req->hasRight(req->RCreateContactType))
box->addItem(tr("(New Contact Type)"),-1);
+ box->setCurrentIndex(curidx);
}
if(c==1){//content
((QLineEdit*)editor)->setText(index.model()->data(index).toString());
return m_addr;
}
+//static
+MOCountry MAddressDialog::createNewCountry(QWidget* parent)
+{
+ QDialog d(parent);
+ d.setWindowTitle(tr("Create New Country"));
+ QVBoxLayout*vl;
+ QHBoxLayout*hl;
+ QFormLayout*fl;
+ QLineEdit*name,*abbr;
+ QPushButton*p;
+ d.setLayout(vl=new QVBoxLayout);
+ vl->addLayout(fl=new QFormLayout);
+ fl->addRow(tr("Country Name:"),name=new QLineEdit);
+ fl->addRow(tr("Abbreviation:"),abbr=new QLineEdit);
+ vl->addStretch(10);
+ vl->addLayout(hl=new QHBoxLayout,0);
+ hl->addStretch(10);
+ hl->addWidget(p=new QPushButton(tr("Ok")),0);
+ connect(p,SIGNAL(clicked()),&d,SLOT(accept()));
+ hl->addWidget(p=new QPushButton(tr("Cancel")),0);
+ connect(p,SIGNAL(clicked()),&d,SLOT(reject()));
+ //wait and check
+ if(d.exec()!=QDialog::Accepted)return MOCountry();
+ if(name->text().trimmed()=="" || abbr->text().trimmed()==""){
+ QMessageBox::warning(parent,tr("Warning"),tr("The country name and abbreviation must contain something!"));
+ return MOCountry();
+ }
+ //create
+ MTCreateCountry cc=MTCreateCountry::query(abbr->text().trimmed(),name->text().trimmed());
+ if(cc.hasError()){
+ QMessageBox::warning(parent,tr("Warning"),tr("Error while creating country: %1").arg(cc.errorString()));
+ return MOCountry();
+ }
+ return cc.getcountry();
+}
+
void MAddressDialog::selectCountry()
{
//get countries
if(cntry[i].name().value() == curstr)cur=i;
}
if(req->hasRight(req->RCreateCountry))
- clst<<tr(" Create New Country...","must contain leading space to distinguish it from genuine countries");
+ clst<<tr(" Create New Country...","this pseudo-entry must contain leading space to distinguish it from genuine countries");
//display selection
bool ok;
QString it=QInputDialog::getItem(this,tr("Select Country"),tr("Please select a country:"),clst,cur,false,&ok);
}
}
//not found: must be the create new entry
- QDialog d(this);
- d.setWindowTitle(tr("Create New Country"));
- QVBoxLayout*vl;
- QHBoxLayout*hl;
- QFormLayout*fl;
- QLineEdit*name,*abbr;
- QPushButton*p;
- d.setLayout(vl=new QVBoxLayout);
- vl->addLayout(fl=new QFormLayout);
- fl->addRow(tr("Country Name:"),name=new QLineEdit);
- fl->addRow(tr("Abbreviation:"),abbr=new QLineEdit);
- vl->addStretch(10);
- vl->addLayout(hl=new QHBoxLayout,0);
- hl->addStretch(10);
- hl->addWidget(p=new QPushButton(tr("Ok")),0);
- connect(p,SIGNAL(clicked()),&d,SLOT(accept()));
- hl->addWidget(p=new QPushButton(tr("Cancel")),0);
- connect(p,SIGNAL(clicked()),&d,SLOT(reject()));
- //wait and check
- if(d.exec()!=QDialog::Accepted)return;
- if(name->text().trimmed()=="" || abbr->text().trimmed()==""){
- QMessageBox::warning(this,tr("Warning"),tr("The country name and abbreviation must contain something!"));
- return;
- }
- //create
- MTCreateCountry cc=MTCreateCountry::query(abbr->text().trimmed(),name->text().trimmed());
- if(cc.hasError()){
- QMessageBox::warning(this,tr("Warning"),tr("Error while creating country: %1").arg(cc.errorString()));
- return;
- }
- m_addr.setcountry(cc.getcountry());
+ MOCountry cc=createNewCountry(this);
+ if(cc.id().isNull())return;
+ m_addr.setcountry(cc);
m_addr.setcountryid(m_addr.country().value().id());
m_country->setText(m_addr.country().value().name());
}
m_unsel=true;
accept();
}
+
+/*****************************************************************************/
+
+MNewCustomerWizard::MNewCustomerWizard(QWidget* parent, Qt::WindowFlags f): QDialog(parent, f)
+{
+ QWidget*w;
+ QHBoxLayout*hl;
+ QVBoxLayout*vl;
+ QGridLayout*gl;
+ QStackedLayout *sl;
+ setLayout(vl=new QVBoxLayout);
+ vl->addLayout(sl=new QStackedLayout);
+ //button bar
+ vl->addSpacing(15);
+ vl->addLayout(hl=new QHBoxLayout);
+ hl->addStretch(1);
+ QPushButton*p,*pprev,*pnext,*pdone;
+ hl->addWidget(p=new QPushButton(tr("Cancel")));
+ p->setIcon(QIcon(":/cancel.png"));
+ connect(p,SIGNAL(clicked()),this,SLOT(reject()));
+ hl->addWidget(pprev=p=new QPushButton(tr("Back")));
+ p->setIcon(QIcon(":/prev.png"));
+ connect(p,SIGNAL(clicked()),new MLambda([=](){sl->setCurrentIndex(sl->currentIndex()-1);},sl),SLOT(call()));
+ hl->addWidget(pnext=p=new QPushButton(tr("Next")));
+ p->setIcon(QIcon(":/next.png"));
+ connect(p,SIGNAL(clicked()),new MLambda([=](){sl->setCurrentIndex(sl->currentIndex()+1);},sl),SLOT(call()));
+ hl->addWidget(pdone=p=new QPushButton(tr("Finish")));
+ p->setIcon(QIcon(":/done.png"));
+ connect(p,SIGNAL(clicked()),this,SLOT(save()));
+ auto pagechange=new MVLambda<void(int)>([=](int idx){
+ pprev->setEnabled(idx>0);
+ int max=sl->count()-1;
+ pnext->setEnabled(idx<max);
+ pdone->setEnabled(idx>=max);
+ },sl);
+ connect(sl,SIGNAL(currentChanged(int)),pagechange,SLOT(call(int)));
+
+ //get helper data
+ MTGetCreateCustomerHints cch=req->queryGetCreateCustomerHints();
+ QStringList titles,cities,states;
+ if(!cch.hasError()){
+ m_countrylist=cch.getcountries();
+ m_typelist=cch.getcontacttypes();
+ titles=cch.gettitles();
+ cities=cch.getcities();
+ states=cch.getstates();
+ }else{
+ //try something else
+ m_countrylist=req->queryGetAllCountries().getcountries();
+ m_typelist=req->queryGetAllContactTypes().gettypes();
+ }
+
+ //address page
+ sl->addWidget(w=new QWidget);
+ int row=0;
+ w->setLayout(gl=new QGridLayout);
+ gl->addWidget(new QLabel(tr("Please enter name and address information.\nPlease enter it also if it is not needed immediately.")),row,0,1,2);
+ gl->addWidget(new QLabel(tr("Name:")),++row,0);
+ gl->addLayout(hl=new QHBoxLayout,row,1);
+ hl->addWidget(m_title=new QComboBox,1);
+ m_title->setEditable(true);
+ m_title->lineEdit()->setPlaceholderText(tr("Title"));
+ for(const QString&tt:titles)m_title->addItem(tt);
+ hl->addWidget(m_name=new QLineEdit,2);
+ m_name->setPlaceholderText(tr("Family Name"));
+ hl->addWidget(m_fname=new QLineEdit,2);
+ m_fname->setPlaceholderText(tr("Given Name"));
+ gl->addWidget(new QLabel(tr("Address:")),++row,0);
+ gl->addWidget(m_addr1=new QLineEdit,row,1);
+ m_addr1->setPlaceholderText(tr("123 Example Street"));
+ gl->addWidget(new QLabel(tr("City:")),++row,0);
+ gl->addLayout(hl=new QHBoxLayout,row,1);
+ hl->addWidget(m_zipcode=new QLineEdit,0);
+ m_zipcode->setPlaceholderText(tr("Zip Code"));
+ hl->addWidget(m_city=new QComboBox,1);
+ m_city->setEditable(true);
+ m_city->lineEdit()->setPlaceholderText(tr("Chose City"));
+ for(const QString&ct:cities)m_city->addItem(ct);
+ gl->addWidget(new QLabel(tr("State:")),++row,0);
+ gl->addWidget(m_state=new QComboBox,row,1);
+ m_state->setEditable(true);
+ m_state->lineEdit()->setPlaceholderText(tr("State (optional)"));
+ for(const QString&st:states)m_state->addItem(st);
+ gl->addWidget(new QLabel(tr("Country:")),++row,0);
+ gl->addLayout(hl=new QHBoxLayout,row,1);
+ hl->addWidget(m_country=new QComboBox,1);
+ updateCountry();
+ if(req->hasRight(req->RCreateCountry)){
+ hl->addWidget(p=new QPushButton(tr("New...")));
+ connect(p,SIGNAL(clicked()),this,SLOT(newcountry()));
+ }
+ // ...spacing
+ gl->setColumnStretch(1,1);
+ gl->addWidget(new QFrame,++row,0,1,2);
+ gl->setRowStretch(row,1);
+
+ //contacts page
+ QScrollArea*sa;
+ sl->addWidget(sa=new QScrollArea);
+ sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+ sa->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
+ sa->setWidgetResizable(true);
+ sa->setWidget(w=new QWidget);
+ w->setLayout(gl=new QGridLayout);
+ gl->addWidget(new QLabel(tr("Please enter at least one way of contacting the customer.")),row=0,0,1,2);
+ for(const MOContactType&ct:m_typelist){
+ gl->addWidget(new QLabel(ct.contacttype()),++row,0);
+ QLineEdit*le;
+ gl->addWidget(le=new QLineEdit,row,1);
+ le->setPlaceholderText(tr("Please enter %1","%1=contact type name").arg(ct.contacttype()));
+ m_contact.append(le);
+ }
+
+ // ...spacing
+ gl->setColumnStretch(1,1);
+ gl->addWidget(new QFrame,++row,0,1,2);
+ gl->setRowStretch(row,1);
+
+ //make sure buttons are initialized
+ pagechange->call(sl->currentIndex());
+ //make size changeable
+ setSizeGripEnabled(true);
+}
+
+void MNewCustomerWizard::save()
+{
+ //create customer
+ MOCustomer inp;
+ inp.setname(m_name->text());
+ inp.setfirstname(m_fname->text());
+ inp.settitle(m_title->currentText());
+ MOAddress adr;
+ adr.setaddr1(m_addr1->text());
+ adr.setcity(m_city->currentText());
+ adr.setstate(m_state->currentText());
+ adr.setzipcode(m_zipcode->text());
+ //handle country
+ qDebug()<<"Country Idx"<<m_country->currentIndex()<<"Name"<<m_country->currentText()<<"ID"<<m_country->itemData(m_country->currentIndex()).toString();
+ return;
+// adr.setcountryid(m_country->);
+ //complete addr
+ inp.addaddresses(adr);
+ //create customer
+ MTCreateCustomer cc=req->queryCreateCustomer(inp);
+ if(QMessageBox::warning(this,tr("Warning"),tr("There was an error while creating the customer: %1").arg(cc.errorString()),QMessageBox::Abort|QMessageBox::Retry)==QMessageBox::Retry)return;
+ //close dialog
+ accept();
+}
+
+void MNewCustomerWizard::updateCountry(QString id)
+{
+ //sort data
+ qSort(m_countrylist.begin(),m_countrylist.end(),
+ [](const MOCountry&a,const MOCountry&b){
+ return a.name().value()<b.name().value();
+ });
+ //remember which one to pre-select
+ if(id.isEmpty())id=m_country->itemData(m_country->currentIndex()).toString();
+ int idx=-1;
+ //clear
+ m_country->clear();
+ //add
+ for(MOCountry ct:m_countrylist){
+ if(ct.id()==id)idx=m_country->count();
+ m_country->addItem(ct.name(),ct.id().value());
+ }
+ //pre-select (last one shown or new one created)
+ if(idx>=0)m_country->setCurrentIndex(idx);
+}
+
+void MNewCustomerWizard::newcountry()
+{
+ //dialog
+ MOCountry co=MAddressDialog::createNewCountry(this);
+ if(co.id().isNull())return;
+ //add to list
+ m_countrylist.append(co);
+ updateCountry(co.id());
+}
+
+MOCustomer MNewCustomerWizard::getNewCustomer(QWidget*parent)
+{
+ //TODO: check which dialog to use
+ MNewCustomerWizard ncw(parent);
+ ncw.exec();
+ return ncw.customer();
+}