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

百货/超市行业

开发平台:

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 = "1";
  270.     else
  271.         szID = pField->AsInteger + 1;
  272.     Query2->Close();
  273.     // 删除已有数据
  274.     Query2->SQL->Clear();
  275.     Query2->SQL->Add("delete from 销售单明细 where 销售单号='" + szID + "'");
  276.     Query2->ExecSQL();
  277.     Query2->Close();
  278.     Query2->SQL->Clear();
  279.     Query2->SQL->Add("delete from 销售单 where 编号='" + szID + "'");
  280.     Query2->ExecSQL();
  281.     Query2->Close();
  282.     // 写入销售单
  283.     Query1->SQL->Clear();
  284.     sql = "insert into 销售单(编号,客户编号,销售日期,制单人,业务员,";
  285.     sql += "税价合计,不含税价,税额) values('" + szID + "','";
  286.     sql += cboCustomCode->Text + "','" + edDate->Text + "','" + edMan->Text+"','";
  287.     sql += cboPeople->Text + "'," + edTotal->Text + "," + edNoTax->Text;
  288.     sql += "," + edTax->Text + ")";
  289.     Query1->SQL->Add(sql);
  290.     Query1->ExecSQL();
  291.     Query1->Close();
  292.     // 写入明细
  293.     Query2->SQL->Clear();
  294.     Query2->SQL->Add("select max(编号) as 编号  from 销售单明细历史");
  295.     Query2->Open();
  296.     pField = Query2->FieldByName("编号");
  297.     if(pField->IsNull)
  298.         szID1 = "0";
  299.     else
  300.         szID1 = pField->AsInteger;
  301.     Query2->Close();
  302.     // 如果遇到货号为空的行就结束写入
  303.     for(int i=1; i<30 && !sgPurDetail->Cells[1][i].IsEmpty(); i++)
  304.     {
  305.         // 数量单价为空的跳过
  306.         if(sgPurDetail->Cells[4][i].IsEmpty() || sgPurDetail->Cells[6][i].IsEmpty())
  307.             continue;
  308.         szID1 = IntToStr(StrToInt(szID1)+i);
  309.         sql = "insert into 销售单明细(编号,销售单号,货号,销售数量,销售价,仓库,";
  310.         sql += "税价合计,税率,不含税价,税额) values('" + szID1 + "','";
  311.         sql += szID + "','" + sgPurDetail->Cells[1][i] + "',";
  312.         sql += sgPurDetail->Cells[4][i]+"," + sgPurDetail->Cells[6][i] + ",'";
  313.         sql += sgPurDetail->Cells[5][i] + "'," + sgPurDetail->Cells[7][i] + ",";
  314.         sql += sgPurDetail->Cells[8][i] + "," + sgPurDetail->Cells[9][i] + ",";
  315.         sql += sgPurDetail->Cells[10][i] + ")";
  316.         Query1->SQL->Clear();
  317.         Query1->SQL->Add(sql);
  318.         Query1->ExecSQL();
  319.         Query1->Close();
  320.     }
  321. }
  322. //---------------------------------------------------------------------------
  323. // 删除当前行
  324. void __fastcall TfmSaleManage::btDeleteClick(TObject *Sender)
  325. {
  326.     for(int i=1; i<11; i++)
  327.         sgPurDetail->Cells[i][m_CurrentRow] = "";
  328. }
  329. //---------------------------------------------------------------------------
  330. // 打印输出并确认销售单,调用sf_销售单存储过程增加库存和应付款
  331. void __fastcall TfmSaleManage::btPrintClick(TObject *Sender)
  332. {
  333.     // 保存数据
  334.     btSaveClick(NULL);
  335.     if(Application->MessageBox("是否记帐确认销售单?","提示",MB_YESNO) == IDYES);
  336.     {
  337.         // 调用sf_销售单存储过程增加库存和应付款
  338.         Query1->SQL->Clear();
  339.         Query1->SQL->Add("exec sf_销售单");
  340.         Query1->ExecSQL();
  341.         Query1->Close();
  342.         // 保存成功,清除数据
  343.         btNewClick(NULL);
  344.     }
  345. }
  346. //---------------------------------------------------------------------------