#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include<QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQueryModel>
#include <QtSql/QSqlTableModel>
#include <QMessageBox>
#include<iostream>
#include <QTextcodec>
#include <QTranslator>
#include <QtDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));//tr()函数支持中文字符
setWindowTitle(tr("陶文---------"));
inittimer();
connectDatabase();
createmenu();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createmenu()
{
//-----------------------出货界面---------------------------
pushout = new QGroupBox(tr("出货"));
QFormLayout *push_layout = new QFormLayout(pushout);
// QComboBox chioce;
// choice->addItem(tr("名称"));
// choice->addItem(tr("数量"));
countout=new QSpinBox();//选择数量
nameout=new QLineEdit();//输入商品名称
checkout = new QPushButton(tr("出货"));//确认按钮
countout->setMaximum(10000);//设置数量输入最多不超过1000
countout->setMaximumWidth(100);//设置最数量输入框大小
checkout->setMaximumWidth(100);
push_layout->addRow(new QLabel(tr("名称")),nameout);
push_layout->addRow(new QLabel(tr("数量")),countout);
push_layout->addRow(checkout);
push_layout->setMargin(100);
ui->formLayout->addWidget(pushout);
pushout->hide();
connect(checkout,SIGNAL(clicked()),this,SLOT(on_checkout_clicked()));//出货确认按钮传送信息
//----------------------入货界面-------------------------------
comein = new QGroupBox(tr("入货"));
QFormLayout *come_layout = new QFormLayout(comein);
countin=new QSpinBox();//选择数量
namein=new QLineEdit();//输入商品名称
leastin=new QSpinBox();//最小提醒量
checkin = new QPushButton(tr("入货"));//确认按钮
leastin->setMaximum(10000);
countin->setMaximum(10000);//设置数量输入最多不超过1000
countin->setMaximumWidth(100);//设置最数量输入框大小
checkin->setMaximumWidth(100);
come_layout->addRow(new QLabel(tr("名称")),namein);
come_layout->addRow(new QLabel(tr("数量")),countin);
come_layout->addRow(new QLabel("最小提醒量"),leastin);
come_layout->addRow(checkin);
come_layout->setMargin(100);
ui->formLayout->addWidget(comein);
comein->hide();
connect(checkin,SIGNAL(clicked()),this,SLOT(on_checkin_clicked()));//出货确认按钮传送信息
//--------------------修改提醒量界面------------------------------
warn = new QGroupBox();
QFormLayout *warn_layout = new QFormLayout(warn);
countwarn=new QSpinBox();//选择数量
namewarn=new QLineEdit();//输入商品名称
checkwarn = new QPushButton(tr("确定"));//确认按钮
countwarn->setMaximum(10000);//设置数量输入最多不超过1000
countwarn->setMaximumWidth(100);//设置最数量输入框大小
checkwarn->setMaximumWidth(100);
warn_layout->addRow(new QLabel(tr("名称")),namewarn);
warn_layout->addRow(new QLabel(tr("提醒数量")),countwarn);
warn_layout->addRow(checkwarn);
warn_layout->setMargin(100);
ui->formLayout->addWidget(warn);
warn->hide();
connect(checkwarn,SIGNAL(clicked()),this,SLOT(on_checkwarn_clicked()));//出货确认按钮传送信息
}
void MainWindow::on_pushButton_3_clicked()//进入出货界面
{
view->hide();
comein->hide();
warn->hide();
pushout->show();
}
void MainWindow::on_checkout_clicked()//出货确认
{
QString count_text = countout->text();
QString name_text = nameout->text();
if(name_text=="")
{
QApplication::beep();
QMessageBox::information(0,"错误","输入商品名称");
}
else if(count_text=="0")
{
QApplication::beep();
QMessageBox::information(0,"错误","商品数量不能小于1");
}
else
{
//这里输入数据到数据库
int row = model->rowCount();
QVariant name;
bool ifsuccess = false;
for(int i=0;i<row;i++)
{
name = model->record(i).value("names");
if(name==name_text)
{
model->setData(model->index(i,2),model->record(i).value("goodout").toInt()+count_text.toInt());
model->setData(model->index(i,3),model->record(i).value("surplus").toInt()-count_text.toInt());
model->submitAll();
ifsuccess = true;
break;
}
}
if(ifsuccess)
{
QApplication::beep();
QMessageBox::information(0,"成功","成功");
//检测是否有商品不足
ui->textBrowser->clear();
for(int i=0;i<row;i++)
{
if(model->record(i).value("tip").toInt()>=model->record(i).value("surplus").toInt())
{
ui->textBrowser->append(model->record(i).value("names").toString());
}
}
}
else
{
QApplication::beep();
QMessageBox::information(0,"失败","no exist!");
}
}
}
void MainWindow::on_pushButton_4_clicked()//进入入货界面
{
view->hide();
pushout->hide();
warn->hide();
comein->show();
}
void MainWindow::on_checkin_clicked()//入货确认
{
QString count_text = countin->text();
QString name_text = namein->text();
QString leastin_text = leastin->text();
if(name_text=="")
{
QApplication::beep();
QMessageBox::information(0,"错误","输入商品名称");
}
else if(count_text=="0")
{
QApplication::beep();
QMessageBox::information(0,"错误","商品数量不能小于1");
}
else
{
//这里输入数据到数据库
int row = model->rowCount();
QVariant name ;
QVariant count;
QSqlRecord record = model->record();
bool ifexist = false;
for(int i=0;i<row;i++)//检查是否为新的货物
{
name = model->record(i).value("names");
count = model->record(i).value("goodin");
if(name_text==name)
{
model->setData(model->index(i,1),count.toInt()+count_text.toInt());
model->setData(model->index(i,3),model->record(i).value("goodin").toInt()-model->record(i).value("goodout").toInt());
model->setData(model->index(i,4),leastin_text.toInt());
model->submitAll();
ifexist = true;
break;
}
}
if(!ifexist)
{
// model->setTable("goods");
record.setValue("names",name_text);
record.setValue("goodin",count_text);
record.setValue("goodout",0);
record.setValue("surplus",count_text);
record.setValue("tip",leastin_text);
model->insertRecord(row,record);
model->submitAll();
}
QApplication::beep();
QMessageBox::information(0,"成功","成功");
//检测是否有商品不足
ui->textBrowser->clear();
for(int i=0;i<row;i++)
{
if(model->record(i).value("tip").toInt()>=model->record(i).value("surplus").toInt())
{
ui->textBrowser->append(model->record(i).value("names").toString());
}
}
}
}
void MainWindow::on_pushButton_5_clicked()
{
view->hide();
pushout->hide();
comein->hide();
warn->show();
}
void MainWindow::on_checkwarn_clicked()//修改最小提醒量确认
{
QString leastin_text = co