📄 salemanage.~cpp
字号:
//----------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main.h"
#include "CustomManage.h"
#include "GoodManage.h"
#include "SaleManage.h"
#include "selectstore.h"
//----------------------------------------------------------------------------
#pragma resource "*.dfm"
TfmSaleManage *fmSaleManage;
//----------------------------------------------------------------------------
__fastcall TfmSaleManage::TfmSaleManage(TComponent *Owner)
: TForm(Owner)
{
}
//----------------------------------------------------------------------------
void __fastcall TfmSaleManage::FormCreate(TObject *Sender)
{
// 设置明细列表的内容
sgPurDetail->Cells[0][0] = "序号";
sgPurDetail->ColWidths[0] = 32;
sgPurDetail->Cells[1][0] = "货号(双击)";
sgPurDetail->ColWidths[1] = 84;
sgPurDetail->Cells[2][0] = "品名";
sgPurDetail->ColWidths[2] = 128;
sgPurDetail->Cells[3][0] = "单位";
sgPurDetail->ColWidths[3] = 32;
sgPurDetail->Cells[4][0] = "数量";
sgPurDetail->ColWidths[4] = 64;
sgPurDetail->Cells[5][0] = "仓库(双击)";
sgPurDetail->ColWidths[5] = 64;
sgPurDetail->Cells[6][0] = "单价";
sgPurDetail->ColWidths[6] = 64;
sgPurDetail->Cells[7][0] = "金额";
sgPurDetail->ColWidths[7] = 64;
sgPurDetail->Cells[8][0] = "税率";
sgPurDetail->ColWidths[8] = 32;
sgPurDetail->Cells[9][0] = "税额";
sgPurDetail->ColWidths[9] = 64;
sgPurDetail->Cells[10][0] = "不含税额";
sgPurDetail->ColWidths[10] = 64;
// 设置序号
for(int i=1;i<30; i++)
{
sgPurDetail->Cells[0][i] = i;
}
// 自动调用btNewClick方法,新建销售单
btNewClick(NULL);
m_CurrentCol = 1;
m_CurrentRow = 1;
}
//---------------------------------------------------------------------------
void __fastcall TfmSaleManage::FormClose(TObject *Sender,
TCloseAction &Action)
{
// 删除窗体并回收空间
Action = caFree;
}
//---------------------------------------------------------------------------
void __fastcall TfmSaleManage::cboCustomCodeDropDown(TObject *Sender)
{
// 弹出客户窗体,供用户选择客户
TfmCustomManage *pForm = new TfmCustomManage(Application);
// 改窗口不应可以编辑,把table的可以编辑属性关闭
pForm->DBNavigator->Enabled = false;
pForm->DataSource1->AutoEdit = false;
pForm->FormStyle = fsNormal;
pForm->WindowState = wsNormal;
pForm->m_Input = true;
pForm->Visible = false;
pForm->Position = poScreenCenter;
pForm->ShowModal();
// 赋予客户编号
if(!pForm->m_szSelectCode.IsEmpty())
cboCustomCode->Text = pForm->m_szSelectCode;
delete pForm;
this->Refresh();
// 获取客户信息
cboCustomCodeChange(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TfmSaleManage::cboCustomCodeChange(TObject *Sender)
{
// 获取客户信息
Query2->Close();
Query2->SQL->Clear();
Query2->SQL->Add("select 名称 from 客户清单 where 客户编号='"
+ cboCustomCode->Text + "'");
Query2->Open();
if(Query2->RecordCount > 0)
{
edCustomName->Text = Query2->FieldByName("名称")->AsString;
}
Query2->Close();
}
//---------------------------------------------------------------------------
void __fastcall TfmSaleManage::sgPurDetailDblClick(TObject *Sender)
{
// 第二列双击,选择商品; 双击第五列,选择仓库
if(sgPurDetail->Col != 1 && sgPurDetail->Col != 5 ) return;
if(sgPurDetail->Col == 1)
{
// 弹出客户窗体,供用户选择客户
TfmGoodManage *pForm = new TfmGoodManage(Application);
// 改窗口不应可以编辑,把table的可以编辑属性关闭
pForm->DBNavigator->Enabled = false;
pForm->DataSource1->AutoEdit = false;
pForm->FormStyle = fsNormal;
pForm->WindowState = wsNormal;
pForm->m_Input = true;
pForm->Visible = false;
pForm->Position = poScreenCenter;
pForm->ShowModal();
// 赋予客户编号
if(!pForm->m_szSelectCode.IsEmpty())
{
sgPurDetail->Cells[1][sgPurDetail->Row] = pForm->m_szSelectCode;
// 获取商品商信息
sgPurDetail->Col = 4;
}
delete pForm;
this->Refresh();
}
if(sgPurDetail->Col == 5)
{
// 弹出选择仓库窗体,供用户选择仓库
TfmSelectStore *pForm = new TfmSelectStore(Application);
// 改窗口不应可以编辑,把table的可以编辑属性关闭
pForm->Position = poScreenCenter;
pForm->m_szGoodID = sgPurDetail->Cells[1][sgPurDetail->Row];
// 查询指定货号的商品库存信息
pForm->Query1->SQL->Clear();
AnsiString sql;
sql = "select 仓库,库存数量 from 库存库 where 货号='";
sql += sgPurDetail->Cells[1][sgPurDetail->Row] + "'";
pForm->Query1->SQL->Add(sql);
pForm->Query1->Open();
pForm->ShowModal();
// 赋予选择的仓库
if(!pForm->m_szSelectStore.IsEmpty())
{
sgPurDetail->Cells[5][sgPurDetail->Row] = pForm->m_szSelectStore;
// 如果需要检查销售数量不能大于库存数量,在此保存仓库的库存数量
// 并在保存按钮的单击事件中作检查就可以了
}
delete pForm;
this->Refresh();
}
}
//---------------------------------------------------------------------------
void __fastcall TfmSaleManage::sgPurDetailSelectCell(TObject *Sender,
int ACol, int ARow, bool &CanSelect)
{
if(ACol==2 || ACol==3 || (ACol>=7 && ACol<=10)) // 不可以编辑的列
CanSelect = false;
// 根据货号更新商品信息
if(m_CurrentCol == 1)
{
Query2->Close();
Query2->SQL->Clear();
Query2->SQL->Add("select 品名,单位 from 商品清单 where 货号='"
+ sgPurDetail->Cells[1][m_CurrentRow] + "'");
Query2->Open();
if(Query2->RecordCount > 0)
{
sgPurDetail->Cells[2][m_CurrentRow] = Query2->FieldByName("品名")->AsString;
sgPurDetail->Cells[3][m_CurrentRow] = Query2->FieldByName("单位")->AsString;
// 默认税率为17
sgPurDetail->Cells[8][m_CurrentRow] = "17";
}
Query2->Close();
}
else if((m_CurrentCol == 4 || m_CurrentCol == 6) &&
!sgPurDetail->Cells[4][m_CurrentRow].IsEmpty() &&
!sgPurDetail->Cells[6][m_CurrentRow].IsEmpty()) // 计算金额
{
double fTotalMoney, fNoTax, fTax, fTaxRatio;
fTaxRatio = StrToFloat(sgPurDetail->Cells[8][m_CurrentRow]);
// 不含税金额
fTotalMoney = StrToFloat(sgPurDetail->Cells[4][m_CurrentRow]) *
StrToFloat(sgPurDetail->Cells[6][m_CurrentRow]);
fNoTax = fTotalMoney/(1+fTaxRatio/100);
fTax = fTotalMoney - fNoTax;
sgPurDetail->Cells[7][m_CurrentRow] = FormatFloat("0.00",fTotalMoney);
sgPurDetail->Cells[9][m_CurrentRow] = FormatFloat("0.00",fTax);
sgPurDetail->Cells[10][m_CurrentRow] = FormatFloat("0.00",fNoTax);
// 计算汇总信息
fTotalMoney = 0;
fTax = 0;
fNoTax = 0;
for(int i=1; i<30; i++)
{
if(!sgPurDetail->Cells[7][i].IsEmpty())
{
fTotalMoney += StrToFloat(sgPurDetail->Cells[7][i]);
fTax += StrToFloat(sgPurDetail->Cells[9][i]);
fNoTax += StrToFloat(sgPurDetail->Cells[10][i]);
}
}
edTotal->Text = FormatFloat("0.00",fTotalMoney);
edTax->Text = FormatFloat("0.00",fTax);
edNoTax->Text = FormatFloat("0.00",fNoTax);
}
m_CurrentCol = ACol;
m_CurrentRow = ARow;
}
//---------------------------------------------------------------------------
void __fastcall TfmSaleManage::sgPurDetailKeyPress(TObject *Sender,
char &Key)
{
if(Key ==13)
{
// 跳到下一个单元格
if(m_CurrentCol == 1)
sgPurDetail->Col = 4;
else if(m_CurrentCol == 4 || m_CurrentCol == 5)
sgPurDetail->Col = m_CurrentCol + 1;
else if(m_CurrentCol == 6) // 下一行
{
sgPurDetail->Col = 1;
sgPurDetail->Row = m_CurrentRow + 1;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TfmSaleManage::btNewClick(TObject *Sender)
{
// 新建销售单,清空各种信息
cboPeople->Text = "";
cboCustomCode->Text = "";
edCustomName->Text = "";
edTotal->Text = "";
edNoTax->Text = "";
edTax->Text = "";
// 清空销售明细
for(int i=1; i<11; i++)
for(int j=1; j<30; j++)
sgPurDetail->Cells[i][j] = "";
// 设置制单人、日期等初始信息
TDateTime dt;
dt = dt.CurrentDateTime();
edDate->Text = dt.DateString();
edMan->Text = ((TfmMain*) Application->MainForm)->m_szUserName;
// 设置业务员数据词库
Query2->SQL->Clear();
Query2->SQL->Add("select 姓名 from 业务员清单");
Query2->Open();
while(!Query2->Eof)
{
cboPeople->Items->Add(Query2->FieldByName("姓名")->AsString);
Query2->Next();
}
}
//---------------------------------------------------------------------------
// 保存销售单,写入销售单和销售单明细
void __fastcall TfmSaleManage::btSaveClick(TObject *Sender)
{
// 这里加入合法性检查,例如客户不能为空等
if(cboCustomCode->Text.IsEmpty()) return;
// 获取单据ID
AnsiString szID,szID1;
AnsiString sql;
Query2->SQL->Clear();
Query2->SQL->Add("select max(编号) as 编号 from 销售单历史");
Query2->Open();
TField* pField = Query2->FieldByName("编号");
if(pField->IsNull)
szID = "1";
else
szID = pField->AsInteger + 1;
Query2->Close();
// 删除已有数据
Query2->SQL->Clear();
Query2->SQL->Add("delete from 销售单明细 where 销售单号='" + szID + "'");
Query2->ExecSQL();
Query2->Close();
Query2->SQL->Clear();
Query2->SQL->Add("delete from 销售单 where 编号='" + szID + "'");
Query2->ExecSQL();
Query2->Close();
// 写入销售单
Query1->SQL->Clear();
sql = "insert into 销售单(编号,客户编号,销售日期,制单人,业务员,";
sql += "税价合计,不含税价,税额) values('" + szID + "','";
sql += cboCustomCode->Text + "','" + edDate->Text + "','" + edMan->Text+"','";
sql += cboPeople->Text + "'," + edTotal->Text + "," + edNoTax->Text;
sql += "," + edTax->Text + ")";
Query1->SQL->Add(sql);
Query1->ExecSQL();
Query1->Close();
// 写入明细
Query2->SQL->Clear();
Query2->SQL->Add("select max(编号) as 编号 from 销售单明细历史");
Query2->Open();
pField = Query2->FieldByName("编号");
if(pField->IsNull)
szID1 = "0";
else
szID1 = pField->AsInteger;
Query2->Close();
// 如果遇到货号为空的行就结束写入
for(int i=1; i<30 && !sgPurDetail->Cells[1][i].IsEmpty(); i++)
{
// 数量单价为空的跳过
if(sgPurDetail->Cells[4][i].IsEmpty() || sgPurDetail->Cells[6][i].IsEmpty())
continue;
szID1 = IntToStr(StrToInt(szID1)+i);
sql = "insert into 销售单明细(编号,销售单号,货号,销售数量,销售价,仓库,";
sql += "税价合计,税率,不含税价,税额) values('" + szID1 + "','";
sql += szID + "','" + sgPurDetail->Cells[1][i] + "',";
sql += sgPurDetail->Cells[4][i]+"," + sgPurDetail->Cells[6][i] + ",'";
sql += sgPurDetail->Cells[5][i] + "'," + sgPurDetail->Cells[7][i] + ",";
sql += sgPurDetail->Cells[8][i] + "," + sgPurDetail->Cells[9][i] + ",";
sql += sgPurDetail->Cells[10][i] + ")";
Query1->SQL->Clear();
Query1->SQL->Add(sql);
Query1->ExecSQL();
Query1->Close();
}
}
//---------------------------------------------------------------------------
// 删除当前行
void __fastcall TfmSaleManage::btDeleteClick(TObject *Sender)
{
for(int i=1; i<11; i++)
sgPurDetail->Cells[i][m_CurrentRow] = "";
}
//---------------------------------------------------------------------------
// 打印输出并确认销售单,调用sf_销售单存储过程增加库存和应付款
void __fastcall TfmSaleManage::btPrintClick(TObject *Sender)
{
// 保存数据
btSaveClick(NULL);
if(Application->MessageBox("是否记帐确认销售单?","提示",MB_YESNO) == IDYES);
{
// 调用sf_销售单存储过程增加库存和应付款
Query1->SQL->Clear();
Query1->SQL->Add("exec sf_销售单");
Query1->ExecSQL();
Query1->Close();
// 保存成功,清除数据
btNewClick(NULL);
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -