⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 purchasemanage.cpp

📁 一个简单的企业进销存管理系统,对于新手学习c++builder很有帮助的,用的是BC++6.0,数据库是sql server 2000
💻 CPP
字号:
//----------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "PurchaseManage.h"
#include "main.h"
#include "SupplyManage.h"
#include "GoodManage.h"
#include "print.h"

//----------------------------------------------------------------------------
#pragma resource "*.dfm"
TfmPurchaseManage *fmPurchaseManage;
//----------------------------------------------------------------------------
__fastcall TfmPurchaseManage::TfmPurchaseManage(TComponent *Owner)
	: TForm(Owner)
{
}
//----------------------------------------------------------------------------
void __fastcall TfmPurchaseManage::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;
    // 初始化仓库下拉选项
    Query2->Close();
    Query2->SQL->Clear();
    Query2->SQL->Add("select 仓库名 from 仓库清单");
    Query2->Open();
    while(!Query2->Eof)
    {
        cboSelectStore->Items->Add(Query2->FieldByName("仓库名")->AsString);
        Query2->Next();
    }
    Query2->Close();
}
//---------------------------------------------------------------------------
void __fastcall TfmPurchaseManage::FormClose(TObject *Sender,
      TCloseAction &Action)
{
    // 删除窗体并回收空间
    Action = caFree;
}
//---------------------------------------------------------------------------
void __fastcall TfmPurchaseManage::cboSupplyCodeDropDown(TObject *Sender)
{
    // 弹出供货商窗体,供用户选择供货商
    TfmSupplyManage *pForm = new TfmSupplyManage(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())
        cboSupplyCode->Text = pForm->m_szSelectCode;
    delete pForm;
    this->Refresh();
    // 获取供货商信息
    cboSupplyCodeChange(NULL);
}
//---------------------------------------------------------------------------

void __fastcall TfmPurchaseManage::cboSupplyCodeChange(TObject *Sender)
{
    // 获取供货商信息
    Query2->Close();
    Query2->SQL->Clear();
    Query2->SQL->Add("select 名称 from 供货商清单 where 供货商号='"
        + cboSupplyCode->Text + "'");
    Query2->Open();
    if(Query2->RecordCount > 0)
    {
        edSupplyName->Text = Query2->FieldByName("名称")->AsString;
    }
    Query2->Close();
}
//---------------------------------------------------------------------------

void __fastcall TfmPurchaseManage::sgPurDetailDblClick(TObject *Sender)
{
    // 第二列双击,选择商品
    if(sgPurDetail->Col != 1) return;
    // 弹出供货商窗体,供用户选择供货商
    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();
}
//---------------------------------------------------------------------------
void __fastcall TfmPurchaseManage::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);
    }
    // 设置仓库下拉选项
    if(ACol == 5)
    {
        cboSelectStore->Visible = true;
        cboSelectStore->Width = sgPurDetail->ColWidths[5];
        cboSelectStore->Left = sgPurDetail->CellRect(ACol,ARow).Left+1;
        cboSelectStore->Top = sgPurDetail->CellRect(ACol,ARow).Top+sgPurDetail->Top;
    }
    else
    {
        cboSelectStore->Visible = false;
        if(m_CurrentCol ==5 )
            sgPurDetail->Cells[m_CurrentCol][m_CurrentRow] = cboSelectStore->Text;
    }
    m_CurrentCol = ACol;
    m_CurrentRow = ARow;
}
//---------------------------------------------------------------------------

void __fastcall TfmPurchaseManage::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 TfmPurchaseManage::btNewClick(TObject *Sender)
{
    // 新建进货单,清空各种信息
    cboPeople->Text = "";
    cboSupplyCode->Text = "";
    edSupplyName->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 TfmPurchaseManage::btSaveClick(TObject *Sender)
{
    // 这里加入合法性检查,例如供货商不能为空,必须有商品等
    if(cboSupplyCode->Text.IsEmpty() || edTotal->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 = "0000000001";
    else
    {
        AnsiString szT = "0000000000";
        szID = pField->AsInteger + 1;
        szID = szT.SubString(1,10-szID.Length()) + szID;
    }
    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 += cboSupplyCode->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)+1);
        AnsiString szT = "0000000000";
        szID1 = szT.SubString(1,10-szID1.Length()) + szID1;
        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 TfmPurchaseManage::btDeleteClick(TObject *Sender)
{
    for(int i=1; i<11; i++)
        sgPurDetail->Cells[i][m_CurrentRow] = "";
}
//---------------------------------------------------------------------------
// 打印输出并确认进货单,调用sf_进货单存储过程增加库存和应付款
void __fastcall TfmPurchaseManage::btPrintClick(TObject *Sender)
{
    // 保存数据
    btSaveClick(NULL);
    // 打印
    fmPrint->Query1->Close();
    fmPrint->Query1->Open();
    fmPrint->QuickRep1->DataSet = NULL;
    fmPrint->QuickRep1->DataSet = fmPrint->Query1;
    fmPrint->QuickRep1->Preview();
    // 提示是否记帐确认进货单
    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 + -