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

百货/超市行业

开发平台:

C++ Builder

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