SaleManage.cpp
上传用户:tangpei45
上传日期:2013-07-14
资源大小:9104k
文件大小:13k
源码类别:

百货/超市行业

开发平台:

C++ Builder

  1. //----------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "main.h"
  5. #include "CustomManage.h"
  6. #include "GoodManage.h"
  7. #include "SaleManage.h"
  8. #include "selectstore.h"
  9. //----------------------------------------------------------------------------
  10. #pragma resource "*.dfm"
  11. TfmSaleManage *fmSaleManage;
  12. //----------------------------------------------------------------------------
  13. __fastcall TfmSaleManage::TfmSaleManage(TComponent *Owner)
  14. : TForm(Owner)
  15. {
  16. }
  17. //----------------------------------------------------------------------------
  18. void __fastcall TfmSaleManage::FormCreate(TObject *Sender)
  19. {
  20.     // 设置明细列表的内容
  21.     sgPurDetail->Cells[0][0] = "序号";
  22.     sgPurDetail->ColWidths[0] = 32;
  23.     sgPurDetail->Cells[1][0] = "货号(双击)";
  24.     sgPurDetail->ColWidths[1] = 84;
  25.     sgPurDetail->Cells[2][0] = "品名";
  26.     sgPurDetail->ColWidths[2] = 128;
  27.     sgPurDetail->Cells[3][0] = "单位";
  28.     sgPurDetail->ColWidths[3] = 32;
  29.     sgPurDetail->Cells[4][0] = "数量";
  30.     sgPurDetail->ColWidths[4] = 64;
  31.     sgPurDetail->Cells[5][0] = "仓库(双击)";
  32.     sgPurDetail->ColWidths[5] = 64;
  33.     sgPurDetail->Cells[6][0] = "单价";
  34.     sgPurDetail->ColWidths[6] = 64;
  35.     sgPurDetail->Cells[7][0] = "金额";
  36.     sgPurDetail->ColWidths[7] = 64;
  37.     sgPurDetail->Cells[8][0] = "税率";
  38.     sgPurDetail->ColWidths[8] = 32;
  39.     sgPurDetail->Cells[9][0] = "税额";
  40.     sgPurDetail->ColWidths[9] = 64;
  41.     sgPurDetail->Cells[10][0] = "不含税额";
  42.     sgPurDetail->ColWidths[10] = 64;
  43.     // 设置序号
  44.     for(int i=1;i<30; i++)
  45.     {
  46.         sgPurDetail->Cells[0][i] = i;
  47.     }
  48.     // 自动调用btNewClick方法,新建销售单
  49.     btNewClick(NULL);
  50.     m_CurrentCol = 1;
  51.     m_CurrentRow = 1;
  52. }
  53. //---------------------------------------------------------------------------
  54. void __fastcall TfmSaleManage::FormClose(TObject *Sender,
  55.       TCloseAction &Action)
  56. {
  57.     // 删除窗体并回收空间
  58.     Action = caFree;
  59. }
  60. //---------------------------------------------------------------------------
  61. void __fastcall TfmSaleManage::cboCustomCodeDropDown(TObject *Sender)
  62. {
  63.     // 弹出客户窗体,供用户选择客户
  64.     TfmCustomManage *pForm = new TfmCustomManage(Application);
  65.     // 改窗口不应可以编辑,把table的可以编辑属性关闭
  66.     pForm->DBNavigator->Enabled = false;
  67.     pForm->DataSource1->AutoEdit = false;
  68.     pForm->FormStyle = fsNormal;
  69.     pForm->WindowState = wsNormal;
  70.     pForm->m_Input = true;
  71.     pForm->Visible = false;
  72.     pForm->Position = poScreenCenter;
  73.     pForm->ShowModal();
  74.     // 赋予客户编号
  75.     if(!pForm->m_szSelectCode.IsEmpty())
  76.         cboCustomCode->Text = pForm->m_szSelectCode;
  77.     delete pForm;
  78.     this->Refresh();
  79.     // 获取客户信息
  80.     cboCustomCodeChange(NULL);
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall TfmSaleManage::cboCustomCodeChange(TObject *Sender)
  84. {
  85.     // 获取客户信息
  86.     Query2->Close();
  87.     Query2->SQL->Clear();
  88.     Query2->SQL->Add("select 名称 from 客户清单 where 客户编号='"
  89.         + cboCustomCode->Text + "'");
  90.     Query2->Open();
  91.     if(Query2->RecordCount > 0)
  92.     {
  93.         edCustomName->Text = Query2->FieldByName("名称")->AsString;
  94.     }
  95.     Query2->Close();
  96. }
  97. //---------------------------------------------------------------------------
  98. void __fastcall TfmSaleManage::sgPurDetailDblClick(TObject *Sender)
  99. {
  100.     // 第二列双击,选择商品; 双击第五列,选择仓库
  101.     if(sgPurDetail->Col != 1 && sgPurDetail->Col != 5 ) return;
  102.     if(sgPurDetail->Col == 1)
  103.     {
  104.         // 弹出客户窗体,供用户选择客户
  105.         TfmGoodManage *pForm = new TfmGoodManage(Application);
  106.         // 改窗口不应可以编辑,把table的可以编辑属性关闭
  107.         pForm->DBNavigator->Enabled = false;
  108.         pForm->DataSource1->AutoEdit = false;
  109.         pForm->FormStyle = fsNormal;
  110.         pForm->WindowState = wsNormal;
  111.         pForm->m_Input = true;
  112.         pForm->Visible = false;
  113.         pForm->Position = poScreenCenter;
  114.         pForm->ShowModal();
  115.         // 赋予客户编号
  116.         if(!pForm->m_szSelectCode.IsEmpty())
  117.         {
  118.             sgPurDetail->Cells[1][sgPurDetail->Row] = pForm->m_szSelectCode;
  119.             // 获取商品商信息
  120.             sgPurDetail->Col = 4;
  121.         }
  122.         delete pForm;
  123.         this->Refresh();
  124.     }
  125.     if(sgPurDetail->Col == 5)
  126.     {
  127.         // 弹出选择仓库窗体,供用户选择仓库
  128.         TfmSelectStore *pForm = new TfmSelectStore(Application);
  129.         // 改窗口不应可以编辑,把table的可以编辑属性关闭
  130.         pForm->Position = poScreenCenter;
  131.         pForm->m_szGoodID = sgPurDetail->Cells[1][sgPurDetail->Row];
  132.         // 查询指定货号的商品库存信息
  133.         pForm->Query1->SQL->Clear();
  134.         AnsiString sql;
  135.         sql = "select 仓库,库存数量 from 库存库 where 货号='";
  136.         sql += sgPurDetail->Cells[1][sgPurDetail->Row] + "'";
  137.         pForm->Query1->SQL->Add(sql);
  138.         pForm->Query1->Open();
  139.         pForm->ShowModal();
  140.         // 赋予选择的仓库
  141.         if(!pForm->m_szSelectStore.IsEmpty())
  142.         {
  143.             sgPurDetail->Cells[5][sgPurDetail->Row] = pForm->m_szSelectStore;
  144.             // 如果需要检查销售数量不能大于库存数量,在此保存仓库的库存数量
  145.             // 并在保存按钮的单击事件中作检查就可以了
  146.         }
  147.         delete pForm;
  148.         this->Refresh();
  149.     }
  150. }
  151. //---------------------------------------------------------------------------
  152. void __fastcall TfmSaleManage::sgPurDetailSelectCell(TObject *Sender,
  153.       int ACol, int ARow, bool &CanSelect)
  154. {
  155.     if(ACol==2 || ACol==3 || (ACol>=7 && ACol<=10))  // 不可以编辑的列
  156.         CanSelect = false;
  157.     // 根据货号更新商品信息
  158.     if(m_CurrentCol == 1)
  159.     {
  160.         Query2->Close();
  161.         Query2->SQL->Clear();
  162.         Query2->SQL->Add("select 品名,单位 from 商品清单 where 货号='"
  163.             + sgPurDetail->Cells[1][m_CurrentRow] + "'");
  164.         Query2->Open();
  165.         if(Query2->RecordCount > 0)
  166.         {
  167.             sgPurDetail->Cells[2][m_CurrentRow] = Query2->FieldByName("品名")->AsString;
  168.             sgPurDetail->Cells[3][m_CurrentRow] = Query2->FieldByName("单位")->AsString;
  169.             // 默认税率为17
  170.             sgPurDetail->Cells[8][m_CurrentRow] = "17";
  171.         }
  172.         Query2->Close();
  173.     }
  174.     else if((m_CurrentCol == 4 || m_CurrentCol == 6) &&
  175.         !sgPurDetail->Cells[4][m_CurrentRow].IsEmpty() &&
  176.         !sgPurDetail->Cells[6][m_CurrentRow].IsEmpty())  // 计算金额
  177.     {
  178.         double fTotalMoney, fNoTax, fTax, fTaxRatio;
  179.         fTaxRatio = StrToFloat(sgPurDetail->Cells[8][m_CurrentRow]);
  180.         // 不含税金额
  181.         fTotalMoney = StrToFloat(sgPurDetail->Cells[4][m_CurrentRow]) *
  182.             StrToFloat(sgPurDetail->Cells[6][m_CurrentRow]);
  183.         fNoTax = fTotalMoney/(1+fTaxRatio/100);
  184.         fTax = fTotalMoney - fNoTax;
  185.         sgPurDetail->Cells[7][m_CurrentRow] = FormatFloat("0.00",fTotalMoney);
  186.         sgPurDetail->Cells[9][m_CurrentRow] = FormatFloat("0.00",fTax);
  187.         sgPurDetail->Cells[10][m_CurrentRow]  = FormatFloat("0.00",fNoTax);
  188.         // 计算汇总信息
  189.         fTotalMoney = 0;
  190.         fTax = 0;
  191.         fNoTax = 0;
  192.         for(int i=1; i<30; i++)
  193.         {
  194.             if(!sgPurDetail->Cells[7][i].IsEmpty())
  195.             {
  196.                 fTotalMoney += StrToFloat(sgPurDetail->Cells[7][i]);
  197.                 fTax += StrToFloat(sgPurDetail->Cells[9][i]);
  198.                 fNoTax += StrToFloat(sgPurDetail->Cells[10][i]);
  199.             }
  200.         }
  201.         edTotal->Text = FormatFloat("0.00",fTotalMoney);
  202.         edTax->Text = FormatFloat("0.00",fTax);
  203.         edNoTax->Text = FormatFloat("0.00",fNoTax);
  204.     }
  205.     m_CurrentCol = ACol;
  206.     m_CurrentRow = ARow;
  207. }
  208. //---------------------------------------------------------------------------
  209. void __fastcall TfmSaleManage::sgPurDetailKeyPress(TObject *Sender,
  210.       char &Key)
  211. {
  212.     if(Key ==13)
  213.     {
  214.         // 跳到下一个单元格
  215.         if(m_CurrentCol == 1)
  216.             sgPurDetail->Col = 4;
  217.         else if(m_CurrentCol == 4 || m_CurrentCol == 5)
  218.             sgPurDetail->Col = m_CurrentCol + 1;
  219.         else if(m_CurrentCol == 6)   // 下一行
  220.         {
  221.             sgPurDetail->Col = 1;
  222.             sgPurDetail->Row = m_CurrentRow + 1;
  223.         }
  224.     }
  225. }
  226. //---------------------------------------------------------------------------
  227. void __fastcall TfmSaleManage::btNewClick(TObject *Sender)
  228. {
  229.     // 新建销售单,清空各种信息
  230.     cboPeople->Text = "";
  231.     cboCustomCode->Text = "";
  232.     edCustomName->Text = "";
  233.     edTotal->Text = "";
  234.     edNoTax->Text = "";
  235.     edTax->Text = "";
  236.     // 清空销售明细
  237.     for(int i=1; i<11; i++)
  238.         for(int j=1; j<30; j++)
  239.            sgPurDetail->Cells[i][j] = "";
  240.     // 设置制单人、日期等初始信息
  241.     TDateTime dt;
  242.     dt = dt.CurrentDateTime();
  243.     edDate->Text = dt.DateString();
  244.     edMan->Text = ((TfmMain*) Application->MainForm)->m_szUserName;
  245.     // 设置业务员数据词库
  246.     Query2->SQL->Clear();
  247.     Query2->SQL->Add("select 姓名 from 业务员清单");
  248.     Query2->Open();
  249.     while(!Query2->Eof)
  250.     {
  251.         cboPeople->Items->Add(Query2->FieldByName("姓名")->AsString);
  252.         Query2->Next();
  253.     }
  254. }
  255. //---------------------------------------------------------------------------
  256. // 保存销售单,写入销售单和销售单明细
  257. void __fastcall TfmSaleManage::btSaveClick(TObject *Sender)
  258. {
  259.     // 这里加入合法性检查,例如客户不能为空等
  260.     if(cboCustomCode->Text.IsEmpty()) return;
  261.     // 获取单据ID
  262.     AnsiString szID,szID1;
  263.     AnsiString sql;
  264.     Query2->SQL->Clear();
  265.     Query2->SQL->Add("select max(编号) as 编号 from 销售单历史");
  266.     Query2->Open();
  267.     TField* pField = Query2->FieldByName("编号");
  268.     if(pField->IsNull)
  269.         szID = "0000000001";
  270.     else
  271.     {
  272.         AnsiString szT = "0000000000";
  273.         szID = pField->AsInteger + 1;
  274.         szID = szT.SubString(1,10-szID.Length()) + szID;
  275.     }
  276.     Query2->Close();
  277.     // 删除已有数据
  278.     Query2->SQL->Clear();
  279.     Query2->SQL->Add("delete from 销售单明细 where 销售单号='" + szID + "'");
  280.     Query2->ExecSQL();
  281.     Query2->Close();
  282.     Query2->SQL->Clear();
  283.     Query2->SQL->Add("delete from 销售单 where 编号='" + szID + "'");
  284.     Query2->ExecSQL();
  285.     Query2->Close();
  286.     // 写入销售单
  287.     Query1->SQL->Clear();
  288.     sql = "insert into 销售单(编号,客户编号,销售日期,制单人,业务员,";
  289.     sql += "税价合计,不含税价,税额) values('" + szID + "','";
  290.     sql += cboCustomCode->Text + "','" + edDate->Text + "','" + edMan->Text+"','";
  291.     sql += cboPeople->Text + "'," + edTotal->Text + "," + edNoTax->Text;
  292.     sql += "," + edTax->Text + ")";
  293.     Query1->SQL->Add(sql);
  294.     Query1->ExecSQL();
  295.     Query1->Close();
  296.     // 写入明细
  297.     Query2->SQL->Clear();
  298.     Query2->SQL->Add("select max(编号) as 编号  from 销售单明细历史");
  299.     Query2->Open();
  300.     pField = Query2->FieldByName("编号");
  301.     if(pField->IsNull)
  302.         szID1 = "0";
  303.     else
  304.         szID1 = pField->AsInteger;
  305.     Query2->Close();
  306.     // 如果遇到货号为空的行就结束写入
  307.     for(int i=1; i<30 && !sgPurDetail->Cells[1][i].IsEmpty(); i++)
  308.     {
  309.         // 数量单价为空的跳过
  310.         if(sgPurDetail->Cells[4][i].IsEmpty() || sgPurDetail->Cells[6][i].IsEmpty())
  311.             continue;
  312.         szID1 = IntToStr(StrToInt(szID1)+1);
  313.         AnsiString szT = "0000000000";
  314.         szID1 = szT.SubString(1,10-szID1.Length()) + szID1;
  315.         sql = "insert into 销售单明细(编号,销售单号,货号,销售数量,销售价,仓库,";
  316.         sql += "税价合计,税率,不含税价,税额) values('" + szID1 + "','";
  317.         sql += szID + "','" + sgPurDetail->Cells[1][i] + "',";
  318.         sql += sgPurDetail->Cells[4][i]+"," + sgPurDetail->Cells[6][i] + ",'";
  319.         sql += sgPurDetail->Cells[5][i] + "'," + sgPurDetail->Cells[7][i] + ",";
  320.         sql += sgPurDetail->Cells[8][i] + "," + sgPurDetail->Cells[9][i] + ",";
  321.         sql += sgPurDetail->Cells[10][i] + ")";
  322.         Query1->SQL->Clear();
  323.         Query1->SQL->Add(sql);
  324.         Query1->ExecSQL();
  325.         Query1->Close();
  326.     }
  327. }
  328. //---------------------------------------------------------------------------
  329. // 删除当前行
  330. void __fastcall TfmSaleManage::btDeleteClick(TObject *Sender)
  331. {
  332.     for(int i=1; i<11; i++)
  333.         sgPurDetail->Cells[i][m_CurrentRow] = "";
  334. }
  335. //---------------------------------------------------------------------------
  336. // 打印输出并确认销售单,调用sf_销售单存储过程增加库存和应付款
  337. void __fastcall TfmSaleManage::btPrintClick(TObject *Sender)
  338. {
  339.     // 保存数据
  340.     btSaveClick(NULL);
  341.     if(Application->MessageBox("是否记帐确认销售单?","提示",MB_YESNO) == IDYES)
  342.     {
  343.         // 调用sf_销售单存储过程增加库存和应付款
  344.         Query1->SQL->Clear();
  345.         Query1->SQL->Add("exec sf_销售单");
  346.         Query1->ExecSQL();
  347.         Query1->Close();
  348.         // 保存成功,清除数据
  349.         btNewClick(NULL);
  350.     }
  351. }
  352. //---------------------------------------------------------------------------