customer managemen finished
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Tue, 5 Jan 2010 11:08:02 +0000 (11:08 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Tue, 5 Jan 2010 11:08:02 +0000 (11:08 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@403 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/dialogs/customerdlg.cpp
src/dialogs/customerdlg.h
wob/customer.wolf
www/inc/wext/customer.php

index 181dca4..47621ec 100644 (file)
@@ -29,6 +29,7 @@
 #include <QSignalMapper>
 #include <QSortFilterProxyModel>
 #include <QStandardItemModel>
+#include <QTableView>
 #include <QTextEdit>
 
 MCustomerListDialog::MCustomerListDialog(QWidget*par,bool isselect,qint64 presel)
@@ -200,6 +201,9 @@ void MCustomerListDialog::deleteCustomer()
 
 /****************************************************************/
 
+static const int ContactIdRole = Qt::UserRole;
+static const int ContactTypeIdRole = Qt::UserRole+1;
+
 MCustomerDialog::MCustomerDialog(MOCustomer c,QWidget*par)
        :QDialog(par),m_cust(c)
 {
@@ -249,12 +253,31 @@ MCustomerDialog::MCustomerDialog(MOCustomer c,QWidget*par)
        connect(p,SIGNAL(clicked()),m_addr,SLOT(createAddress()));
        
        tab->addTab(w=new QWidget,tr("Contact Information"));
-       w->setLayout(gl=new QGridLayout);
-       gl->addWidget(new QLabel(tr("Contact Information:")),++lc,0);
-       gl->addWidget(new QTextEdit,lc,1);
-       //FIXME
-//     m_cont->setPlainText(m_cust.contact());
-       
+       w->setLayout(vl2=new QVBoxLayout);
+       vl2->addWidget(m_contact=new QTableView,10);
+       m_contact->setModel(m_contactmodel=new QStandardItemModel(this));
+       m_contact->setItemDelegate(new MContactTableDelegate(this));
+       vl2->addLayout(hl=new QHBoxLayout,0);
+       hl->addStretch(10);
+       hl->addWidget(p=new QPushButton(tr("Add")),0);
+       connect(p,SIGNAL(clicked()),this,SLOT(addContact()));
+       hl->addWidget(p=new QPushButton(tr("Remove")),0);
+       connect(p,SIGNAL(clicked()),this,SLOT(removeContact()));
+       QList<MOContact>cts=m_cust.contacts();
+       m_contactmodel->insertColumns(0,2);
+       m_contactmodel->insertRows(0,cts.size());
+       m_contactmodel->setHorizontalHeaderLabels(QStringList()<<tr("Type","table: contact type")<<tr("Contact","table: contact info"));
+       for(int i=0;i<cts.size();i++){
+               m_contactmodel->setData(m_contactmodel->index(i,0),
+                       cts[i].contacttype().value().contacttype().value());
+               m_contactmodel->setData(m_contactmodel->index(i,0),
+                       cts[i].contactid().value(), ContactIdRole);
+               m_contactmodel->setData(m_contactmodel->index(i,0),
+                       cts[i].contacttypeid().value(), ContactTypeIdRole);
+               m_contactmodel->setData(m_contactmodel->index(i,1),
+                       cts[i].contact().value());
+       }
+       m_contact->resizeColumnsToContents();
        
        vl->addLayout(hl=new QHBoxLayout,0);
        hl->addStretch(10);
@@ -268,6 +291,27 @@ MCustomerDialog::MCustomerDialog(MOCustomer c,QWidget*par)
        setSizeGripEnabled(true);
 }
 
+void MCustomerDialog::addContact()
+{
+       //create new row
+       int rn=m_contactmodel->rowCount();
+       m_contactmodel->insertRows(rn,1);
+       //set basic data (guaranteed invalid IDs)
+       QModelIndex idx=m_contactmodel->index(rn,0);
+       m_contactmodel->setData(idx,-1,ContactIdRole);
+       m_contactmodel->setData(idx,-1,ContactTypeIdRole);
+       //scroll to it, edit it
+       m_contact->scrollTo(idx);
+       m_contact->edit(idx);
+}
+
+void MCustomerDialog::removeContact()
+{
+       QModelIndex idx=m_contact->currentIndex();
+       if(!idx.isValid())return;
+       m_contactmodel->removeRows(idx.row(),1);
+}
+
 MOCustomer MCustomerDialog::getCustomer()
 {
        //copy data from input fields
@@ -275,8 +319,18 @@ MOCustomer MCustomerDialog::getCustomer()
        m_cust.setfirstname(m_fname->text());
        m_cust.settitle(m_title->text());
        m_cust.setaddresses(m_addr->addressList());
-       //m_cust.setcontact(m_cont->toPlainText());
        m_cust.setcomments(m_comm->toPlainText());
+       QList<MOContact>clst;
+       for(int i=0;i<m_contactmodel->rowCount();i++){
+               MOContact c;
+               QModelIndex idx0=m_contactmodel->index(i,0);
+               c.setcontactid(m_contactmodel->data(idx0,ContactIdRole).toInt());
+               c.setcontacttypeid(m_contactmodel->data(idx0,ContactTypeIdRole).toInt());
+               c.setcustomerid(m_cust.customerid());
+               c.setcontact(m_contactmodel->data(m_contactmodel->index(i,1)).toString());
+               clst<<c;
+       }
+       m_cust.setcontacts(clst);
        return m_cust;
 }
 
@@ -299,6 +353,99 @@ void MCustomerDialog::save()
                m_cust=cc.getcustomer();
        }
 }
+/********************************************************************************/
+
+MContactTableDelegate::MContactTableDelegate(QObject*p)
+       :QItemDelegate(p)
+{
+}
+
+QWidget *MContactTableDelegate::createEditor(QWidget *parent,
+        const QStyleOptionViewItem &/* option */,
+        const QModelIndex & index ) const
+{
+       if(index.column()==0){//contact type: dropdown
+               QComboBox *editor = new QComboBox(parent);
+               editor->installEventFilter(const_cast<MContactTableDelegate*>(this));
+               return editor;
+       }
+       if(index.column()==1){//contact string: normal delegate, line edit
+               QLineEdit*editor=new QLineEdit(parent);
+               editor->installEventFilter(const_cast<MContactTableDelegate*>(this));
+               return editor;
+       }
+       
+       return 0;
+}
+
+void MContactTableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
+{
+       int c=index.column();
+       if(c==0){//type
+               if(m_typelist.size()==0)m_typelist=MTGetAllContactTypes::query().gettypes();
+               QComboBox*box=(QComboBox*)editor;
+               int curidx=0,cur=index.model()->data(index,ContactTypeIdRole).toInt();
+               for(int i=0;i<m_typelist.size();i++){
+                       box->addItem(m_typelist[i].contacttype(),m_typelist[i].contacttypeid().value());
+                       if(m_typelist[i].contacttypeid().value()==cur)
+                               curidx=i;
+               }
+               if(req->hasRight(req->RCreateContactType))
+                       box->addItem(tr("(New Contact Type)"),-1);
+       }
+       if(c==1){//content
+               ((QLineEdit*)editor)->setText(index.model()->data(index).toString());
+       }
+}
+
+void MContactTableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
+       const QModelIndex &index) const
+{
+       int c=index.column();
+       if(c==0){//type
+               //get content of box
+               QComboBox*box=(QComboBox*)editor;
+               QString tn=box->itemText(box->currentIndex());
+               int tid=box->itemData(box->currentIndex()).toInt();
+               if(tid<0){//create new one
+                       QDialog d(editor->window());
+                       d.setWindowTitle(tr("Create new Contact Type"));
+                       QFormLayout*fl;
+                       QVBoxLayout*vl;
+                       QHBoxLayout*hl;
+                       QLineEdit*nm,*up;
+                       QPushButton*p;
+                       d.setLayout(vl=new QVBoxLayout);
+                       vl->addLayout(fl=new QFormLayout);
+                       fl->addRow(tr("Contact Type Name:"),nm=new QLineEdit);
+                       fl->addRow(tr("Contact Type URI Prefix:"),up=new QLineEdit);
+                       vl->addStretch(1);
+                       vl->addLayout(hl=new QHBoxLayout);
+                       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;
+                       MTCreateContactType ct=MTCreateContactType::query(nm->text(),up->text());
+                       if(ct.hasError()){
+                               QMessageBox::warning(editor->window(),tr("Warning"),tr("Error while creating contact type: %1").arg(ct.errorString()));
+                               return;
+                       }
+                       //get data
+                       MOContactType cty=ct.getcontacttype();
+                       tn=cty.contacttype();
+                       tid=cty.contacttypeid();
+                       //clear cache, so it is retrieved again next time
+                       m_typelist.clear();
+               }
+               //set new contact type
+               model->setData(index,tn);
+               model->setData(index,tid,ContactTypeIdRole);
+       }
+       if(c==1){//content
+               model->setData(index,((QLineEdit*)editor)->text());
+       }
+}
 
 /*****************************************************************************/
 
index bdd5a2c..c456f7b 100644 (file)
 #include <QString>
 #include <QDialog>
 #include <QFrame>
+#include <QItemDelegate>
 #include <QList>
 #include <QScrollArea>
 
 #include "MOCustomerInfo.h"
 #include "MOCustomer.h"
 
+class QLineEdit;
 class QListView;
-class QStandardItemModel;
 class QSortFilterProxyModel;
-class QLineEdit;
+class QStandardItemModel;
+class QTableView;
 
 /**shows a list of customers, lets the user select and offers to alter/create customers*/
 class MCustomerListDialog:public QDialog
@@ -210,6 +212,10 @@ class MCustomerDialog:public QDialog
        private slots:
                /**internal: save data*/
                void save();
+               /**internal: add contact*/
+               void addContact();
+               /**internal: remove contact*/
+               void removeContact();
                
        private:
                MOCustomer m_cust;
@@ -217,6 +223,27 @@ class MCustomerDialog:public QDialog
                QTextEdit*m_comm;
                MAddressListWidget*m_addr;
                QLabel*m_mail;
+               QTableView*m_contact;
+               QStandardItemModel*m_contactmodel;
+};
+
+/**Helper class for customer contact table*/
+class MContactTableDelegate:public QItemDelegate
+{
+       Q_OBJECT
+       public:
+               MContactTableDelegate(QObject *parent = 0);
+               
+               QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
+                                     const QModelIndex &index) const;
+               
+               void setEditorData(QWidget *editor, const QModelIndex &index) const;
+               void setModelData(QWidget *editor, QAbstractItemModel *model,
+                                 const QModelIndex &index) const;
+       private:
+               //cache of contact types
+               //why ever the interesting delegate methods are const, this has to be mutable then
+               mutable QList<MOContactType>m_typelist;
 };
 
 #endif
index 5877844..6b63bd5 100644 (file)
                        <Map column="uriprefix"/>
                </Mapping>
        </Class>
+       
+       <Transaction name="GetAllContactTypes">
+               <Input/>
+               <Call lang="php" method="$this->settypes(WOContactType::fromTableArraycontacttype(WTcontacttype::selectFromDB()));"/>
+               <Output>
+                       <Var name="types" type="List:ContactType"/>
+               </Output>
+       </Transaction>
+       <Transaction name="CreateContactType">
+               <Input>
+                       <Var name="typename" type="string"/>
+                       <Var name="uriprefix" type="astring"/>
+               </Input>
+               <Call lang="php" method="WOCustomer::createContactType($this);"/>
+               <Output>
+                       <Var name="contacttype" type="ContactType"/>
+               </Output>
+       </Transaction>
+       
        <Class name="Contact">
                <Property name="contactid" type="int64"/>
                <Property name="customerid" type="int32"/>
index 412df14..08dc8f5 100644 (file)
@@ -101,6 +101,7 @@ class WOCustomer extends WOCustomerAbstract
                        }
                }
                //sync contacts
+               $cnlist=array();
                foreach($cc->get_contacts() as $cont){
                        $cn=WTcontact::getFromDB($cont->get_contactid());
                        //new one?
@@ -122,6 +123,14 @@ class WOCustomer extends WOCustomerAbstract
                                //known one, update
                                if($cn->isChanged())$cn->update();
                        }
+                       //remember
+                       $cnlist[]=$cn->contactid;
+               }
+               //remove deleted contacts
+               $cns=WTcontact::selectFromDB("customerid=".$db->escapeInt($cc->get_id()));
+               foreach($cns as $cn){
+                       if(!in_array($cn->contactid,$cnlist))
+                               $cn->deleteFromDB();
                }
                
                //return
@@ -157,5 +166,28 @@ class WOCustomer extends WOCustomerAbstract
                //return
                $trans->setcountry(WOCountry::fromTablecountry($tab));
        }
+       
+       /**called from CreateContactType transaction*/
+       public static function createContactType($trans)
+       {
+               global $db;
+               $cname=trim($trans->gettypename());
+               if($cname==""){
+                       $trans->abortWithError(tr("Contact Type names must not be empty!"));
+                       return;
+               }
+               $tab=$db->select("contacttype","*","contacttype=".$db->escapeString($cname));
+               if(count($tab)>0){
+                       $trans->abortWithError(tr("Contact Type already exists."));
+                       return;
+               }
+               //create
+               $tab=WTcontacttype::newRow();
+               $tab->contacttype=$cname;
+               $tab->uriprefix=trim($trans->geturiprefix());
+               $tab->insert();
+               //return
+               $trans->setcontacttype(WOContactType::fromTablecontacttype($tab));
+       }
 };
 ?>
\ No newline at end of file