#include "article2.h"

article2::article2()
{
codeAbarre=0;
nom="";
prix=0;
}
article2::article2(int prix,QString nom,int codeAbarre)
{
  this->prix=prix;
  this->nom=nom;
  this->codeAbarre=codeAbarre;
}

int article2::get_codeAbarre(){return  codeAbarre;}


bool article2::ajouter()
{
QSqlQuery query;
QString res= QString::number(codeAbarre);
query.prepare("INSERT INTO articlerayon (NOM, CODEABARRE, PRIX) "
                    "VALUES (:nom, :codeAbarre,:prix)");
query.bindValue(":codeAbarre", res);
query.bindValue(":nom", nom);
query.bindValue(":prix", prix);

return    query.exec();
}

QSqlQueryModel * article2::afficher()
{QSqlQueryModel * model= new QSqlQueryModel();

model->setQuery("select * from articlerayon");
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Nom"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Code a barre"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Prix"));
    return model;
}

bool article2::supprimer(QString nom)
{
QSqlQuery query;
QString res= QString(nom);
query.prepare("Delete from articlerayon where nom = :nom  ");
query.bindValue(":nom", res);
return    query.exec();
}


bool article2::modifier(QString nom,int prix)
{
    QSqlQuery query;
    QString res= QString(nom);
    query.prepare("update articlerayon set prix=:prix where nom=:nom");
    query.bindValue(":nom",nom);
    query.bindValue(":prix",prix);
          query.exec();
}

QSqlQueryModel *article2::trier()
{


    QSqlQueryModel * model=new QSqlQueryModel();

    model->setQuery("select *from articlerayon  ORDER BY nom");
    model->setHeaderData(0,Qt::Horizontal,QObject::tr("nom"));
    model->setHeaderData(1,Qt::Horizontal,QObject::tr("codeabarre"));
    model->setHeaderData(2,Qt::Horizontal,QObject::tr("prix"));


    return model;
}
