#include "commande.h"
#include "commande.h"
#include <QString>

commande::commande()
{
 numC=0;
 prixC=0;
 quantite=0;
 etat="";
}

commande::commande(int a,int b ,int c,QString d)
{
    numC=a;
    prixC=b;
    quantite=c;
    etat=d;


}

int commande::getnumC(){return numC;}
int commande::getPrixC(){return prixC;}
int commande::getquantite(){return quantite;}
QString commande::getetat(){return etat;}

bool  commande::ajouter()
{

    QSqlQuery query;
    QString res= QString::number(numC);
    query.prepare("INSERT INTO COMMANDE (NUMC,PRIXC ,QUANTITE, ETAT) "
                        "VALUES (:numC,:prixC,:quantite, :etat)");
    query.bindValue(":numC", res);
    query.bindValue(":prixC", prixC);
    query.bindValue(":quantite", quantite);
    query.bindValue(":etat", etat);


    return    query.exec();

}
bool commande::modifier(int numC,int prixC,int quantite,QString etat)
{

    QSqlQuery query;
    QString res= QString::number(numC);
query.prepare("update commande set etat=:etat , quantite=:quantite,prixC=:prixC where numC=:numC ");
query.bindValue(":numC",res);
    query.bindValue(":prixC",prixC);
    query.bindValue(":quantite",quantite);
     query.bindValue(":etat",etat);
          query.exec();

}

QSqlQueryModel *commande:: afficher()
{
    QSqlQueryModel * model=new QSqlQueryModel();
    model->setQuery("select *from COMMANDE");
    model->setHeaderData(0,Qt::Horizontal,QObject::tr("NUMC"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("PRIXC"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("QUANTITE"));
    model->setHeaderData(3,Qt::Horizontal,QObject::tr("ETAT"));
    //model->setHeaderData(4,Qt::Horizontal,QObject::tr("DATE"));
    return model;
}
bool commande:: supprimer(int numC)
{
    QSqlQuery query;
    QString res=QString::number(numC);
    query.prepare("delete   from COMMANDE where numC=:numC");
    query.addBindValue(numC);
    return query.exec();
}

QSqlQueryModel *commande::sortnumC()
{

    QSqlQueryModel * model=new QSqlQueryModel();
    model->setQuery("select *from Commande ORDER BY NUMC");
    model->setHeaderData(0,Qt::Horizontal,QObject::tr("NUMC"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("PRIXC"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("QUANTITE"));
    model->setHeaderData(3,Qt::Horizontal,QObject::tr("ETAT"));
   // model->setHeaderData(4,Qt::Horizontal,QObject::tr("DATE"));

    return model;
}
/*QSqlQueryModel *commande::sortPrixC()
{
    QSqlQueryModel * model=new QSqlQueryModel();
    model->setQuery("select *from COMMANDE ORDER BY PRIXC");
    model->setHeaderData(0,Qt::Horizontal,QObject::tr("NUMC"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("PRIXC"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("QUANTITE"));
    model->setHeaderData(3,Qt::Horizontal,QObject::tr("ETAT"));
    model->setHeaderData(4,Qt::Horizontal,QObject::tr("DATE"));

    return model;
}
QSqlQueryModel *Commande::sortquantite()
{
    QSqlQueryModel * model=new QSqlQueryModel();
    model->setQuery("select *from COMMANDE ORDER BY QUANTITE");
    model->setHeaderData(0,Qt::Horizontal,QObject::tr("NUMC"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("PRIXC"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("QUANTITE"));
    model->setHeaderData(3,Qt::Horizontal,QObject::tr("ETAT"));
    model->setHeaderData(4,Qt::Horizontal,QObject::tr("DATE"));

    return model;
}

QSqlQueryModel *Commande::sortetat()
{
    QSqlQueryModel * model=new QSqlQueryModel();
    model->setQuery("select *from COMMANDE ORDER BY ETAT");
    model->setHeaderData(0,Qt::Horizontal,QObject::tr("NUMC"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("PRIXC"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("QUANTITE"));
    model->setHeaderData(3,Qt::Horizontal,QObject::tr("ETAT"));
    model->setHeaderData(4,Qt::Horizontal,QObject::tr("DATE"));

    return model;
}

QSqlQueryModel *Commande::sortdate()
{
    QSqlQueryModel * model=new QSqlQueryModel();
    model->setQuery("select *from COMMANDE ORDER BY DATE");
    model->setHeaderData(0,Qt::Horizontal,QObject::tr("NUMC"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("PRIXC"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("QUANTITE"));
    model->setHeaderData(3,Qt::Horizontal,QObject::tr("ETAT"));
    model->setHeaderData(4,Qt::Horizontal,QObject::tr("DATE"));

    return model;
}
*/
QSqlQueryModel *commande::cherchernumC(int numC)
{
    QSqlQueryModel * model=new QSqlQueryModel();
    QSqlQuery query;
    query.prepare("select * from COMMANDE where NUMC= ?");
    query.addBindValue(numC);
    query.exec();

    model->setQuery(query);
     model->setHeaderData(0,Qt::Horizontal,QObject::tr("NUMC"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("PRIXC"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("QUANTITE"));
    model->setHeaderData(3,Qt::Horizontal,QObject::tr("ETAT"));
    //model->setHeaderData(4,Qt::Horizontal,QObject::tr("DATE"));
  return model;
}

QSqlQueryModel *commande::chercherPrixC(int prixC)
{
    QSqlQueryModel * model=new QSqlQueryModel();
    QSqlQuery query;
    query.prepare("select * from COMMANDE where PRIXC= ? ");
    query.addBindValue(prixC);
    query.exec();

    model->setQuery(query);
     model->setHeaderData(0,Qt::Horizontal,QObject::tr("NUMC"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("PRIXC"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("QUANTITE"));
    model->setHeaderData(3,Qt::Horizontal,QObject::tr("ETAT"));
    model->setHeaderData(4,Qt::Horizontal,QObject::tr("DATE"));
  return model;
}
/*
QSqlQueryModel *Commande::chercherquantite(int quantite)
{
    QSqlQueryModel * model=new QSqlQueryModel();
    QSqlQuery query;
    query.prepare("select * from COMMANDE where QUANTITE= ? ");
    query.addBindValue(quantite);
    query.exec();

    model->setQuery(query);
     model->setHeaderData(0,Qt::Horizontal,QObject::tr("NUMC"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("PRIXC"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("QUANTITE"));
    model->setHeaderData(3,Qt::Horizontal,QObject::tr("ETAT"));
    model->setHeaderData(4,Qt::Horizontal,QObject::tr("DATE"));
  return model;
}
QSqlQueryModel *Commande::chercheretat(QString etat)
{
    QSqlQueryModel * model=new QSqlQueryModel();
    QSqlQuery query;
    query.prepare("select * from COMMANDE where ETAT= ? ");
    query.addBindValue(etat);
    query.exec();

    model->setQuery(query);
    model->setHeaderData(0,Qt::Horizontal,QObject::tr("NUMC"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("PRIXC"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("QUANTITE"));
    model->setHeaderData(3,Qt::Horizontal,QObject::tr("ETAT"));
    model->setHeaderData(4,Qt::Horizontal,QObject::tr("DATE"));

    return model;
}
QSqlQueryModel *Commande::chercheretat(QDate date)
{
    QSqlQueryModel * model=new QSqlQueryModel();
    QSqlQuery query;
    query.prepare("select * from COMMANDE where DATE= ? ");
    query.addBindValue(date);
    query.exec();

    model->setQuery(query);
    model->setHeaderData(0,Qt::Horizontal,QObject::tr("NUMC"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("PRIXC"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("QUANTITE"));
    model->setHeaderData(3,Qt::Horizontal,QObject::tr("ETAT"));
    model->setHeaderData(4,Qt::Horizontal,QObject::tr("DATE"));

    return model;
}
 */
/*bool commande::verifierExetat(QString etat)
{
    int i=0;
    QSqlQuery query;
    query.prepare("select * from COMMANDE where ETAT= ? ");
   query.addBindValue(etat);
    if(query.exec())
    {while(query.next())
        {i++;}}
    if(i!=0)
        return true ;
    else
        return false;
}*/

bool commande::verifierExnumC(int numC)
{
    int i=0;
    QSqlQuery query;
    query.prepare("select * from COMMANDE where numC= ? ");
   query.addBindValue(numC);
    if(query.exec())
    {while(query.next())
        {i++;}}
    if(i!=0)
        return true ;
    else
        return false;
}

bool commande::verifierExPrixC(int PrixC)
{
    int i=0;
    QSqlQuery query;
    query.prepare("select * from COMMANDE where PrixC= ? ");
   query.addBindValue(PrixC);
    if(query.exec())
    {while(query.next())
        {i++;}}
    if(i!=0)
        return true ;
    else
        return false;
}
/*
bool Commande::verifierExquantite(int quantite)
{
    int i=0;
    QSqlQuery query;
    query.prepare("select * from COMMANDE where QUANTITE= ? ");
   query.addBindValue(quantite);
    if(query.exec())
    {while(query.next())
        {i++;}}
    if(i!=0)
        return true ;
    else
        return false;
}
bool Commande::verifierExdate(QDate date)
{
    int i=0;
    QSqlQuery query;
    query.prepare("select * from COMMANDE where DATE= ? ");
   query.addBindValue(DATE);
    if(query.exec())
    {while(query.next())
        {i++;}}
    if(i!=0)
        return true ;
    else
        return false;
}
*/
bool commande::notifier()
{
    int i=0 ;
    QSqlQuery query;
    query.prepare("select * from COMMANDE where NUMC=30");
    if(query.exec())
    {while (query.next())
        {i++;}
    }
}

