saleunit.pas
上传用户:bjkrmsxy
上传日期:2010-01-31
资源大小:9004k
文件大小:9k
源码类别:

百货/超市行业

开发平台:

Delphi

  1. unit saleunit;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, ComCtrls, Grids, DBGrids, ExtCtrls, DB, ADODB, StdCtrls, Menus;
  6. type
  7.   TSale_Form = class(TForm)
  8.     Panel1: TPanel;
  9.     DBGrid1: TDBGrid;
  10.     StatusBar1: TStatusBar;
  11.     DataSource1: TDataSource;
  12.     ADOQuery1: TADOQuery;
  13.     ADOQuery1id: TAutoIncField;
  14.     ADOQuery1p_id: TSmallintField;
  15.     ADOQuery1p_name: TWideStringField;
  16.     ADOQuery1p_price: TBCDField;
  17.     ADOQuery1p_unit: TWideStringField;
  18.     ADOQuery1p_type: TSmallintField;
  19.     ADOQuery1p_store: TSmallintField;
  20.     ADOQuery1p_time: TDateTimeField;
  21.     ADOQuery1p_sale: TSmallintField;
  22.     Label1: TLabel;
  23.     Edit1: TEdit;
  24.     Button1: TButton;
  25.     ADOQuery2: TADOQuery;
  26.     Button2: TButton;
  27.     RichEdit1: TRichEdit;
  28.     PopupMenu1: TPopupMenu;
  29.     N1: TMenuItem;
  30.     ADOTable1: TADOTable;
  31.     N2: TMenuItem;
  32.     N3: TMenuItem;
  33.     Button3: TButton;
  34.     Label2: TLabel;
  35.     Edit2: TEdit;
  36.     Panel2: TPanel;
  37.     GroupBox1: TGroupBox;
  38.     Button4: TButton;
  39.     Edit4: TEdit;
  40.     Edit3: TEdit;
  41.     Label3: TLabel;
  42.     Label4: TLabel;
  43.     Label5: TLabel;
  44.     Label6: TLabel;
  45.     Button5: TButton;
  46.     Button6: TButton;
  47.     N4: TMenuItem;
  48.     Timer1: TTimer;
  49.     N5: TMenuItem;
  50.     procedure Button1Click(Sender: TObject);
  51.     procedure Button2Click(Sender: TObject);
  52.     procedure Button3Click(Sender: TObject);
  53.     procedure Button4Click(Sender: TObject);
  54.     procedure FormActivate(Sender: TObject);
  55.     procedure Button5Click(Sender: TObject);
  56.     procedure Button6Click(Sender: TObject);
  57.     procedure Timer1Timer(Sender: TObject);
  58.     procedure N5Click(Sender: TObject);
  59.   private
  60.     { Private declarations }
  61.     count:double;
  62.   public
  63.     { Public declarations }
  64.   end;
  65. var
  66.   Sale_Form: TSale_Form;
  67. implementation
  68. uses datamodule, UseLogin, help;
  69. {$R *.dfm}
  70. procedure TSale_Form.Button1Click(Sender: TObject); //提交商品的操作
  71. var
  72. sqls:string;
  73. Parami:tparameter;
  74. count1:double;
  75. left:integer;
  76. begin
  77. count1:=0;
  78. if (edit1.text='') and (edit2.text='') then
  79. showmessage('请输入条形码或者是商品名称噢^_^')
  80. else
  81.  begin
  82.    with adoquery1 do    //从库存中查找提交的商品是否存在
  83.         begin
  84.            close;
  85.            sql.clear;
  86.            sqls:='select * from produce_t where 1=1';
  87.            if edit2.Text<>'' then
  88.               sqls:=sqls+'and p_name='''+edit2.Text+'''';
  89.            if edit1.Text<>'' then
  90.               begin
  91.                 sqls:=sqls+'and p_id=:id';
  92.                 Parameters.Clear;
  93.                 Parami:=Parameters.ADDParameter; //创建参数1
  94.                 Parami.Name:='id';
  95.                 Parami.Value:=strtoint(edit1.text);
  96.                end;
  97.             sql.add(sqls);
  98.             Prepared;
  99.             open;
  100.             if recordcount=0 then
  101.             showmessage('对不起,该商品不存在')
  102.             else          //如果商品存在就将他写入sell_t表中保存
  103.             begin
  104.              if not adoquery2.active then
  105.              adoquery2.open;
  106.              adoquery2.insert;
  107.              adoquery2.fields.fields[0]:=fields.fields[1];   //条形码
  108.              adoquery2.fields.fields[1]:=fields.fields[2];   //商品名
  109.              adoquery2.fields.fields[2]:=fields.fields[3];   //商品价格
  110.              adoquery2.fields.fields[3]:=fields.fields[4];   //商品计量单位
  111.                with datamodule1.adocommand1 do               //修改produce_t中sale_p的值
  112.                  begin
  113.                  commandtext:='update produce_t set p_sale=p_sale+1 where p_id=:i';
  114.                  parameters.ParamValues['i']:=adoquery2.fields[0].AsInteger;
  115.                  execute;
  116.                  end;
  117.              left:=fields.fields[6].asinteger-fields.fields[8].asinteger;
  118.              adoquery2.fields.fields[4].asinteger:=left; //得到此时的库存量
  119.              if left<=0 then
  120.                begin
  121.                  showmessage('确货中');
  122.                  adoquery2.Cancel;
  123.                end
  124.                else
  125.                begin
  126.                 adoquery2.post;
  127.                 count1:=fields.Fields[3].AsFloat;  //求当前商品应付金额
  128.                 end;
  129.             end;
  130.         end;
  131.         count:=count+count1;    //求出总的应付金额
  132.         richedit1.text:='购物金额共计:'+floattostr(count)+'元整。'; //显示应付金额数
  133.   end;
  134. end;
  135. procedure TSale_Form.Button2Click(Sender: TObject);
  136. begin
  137. if count=0 then        //当没有商品卖出时结算操作为错误操作
  138.    showmessage('错误操作')
  139. else
  140. begin
  141.   edit3.Text:=floattostr(count);      //应付金额
  142.   panel2.Visible:=true;
  143. end;
  144. end;
  145. procedure TSale_Form.Button3Click(Sender: TObject);
  146. begin
  147. if count>0 then  //必需先结算方可离开
  148. showmessage('!!还没结算,请结算后再离开吧!!')
  149. else
  150. begin
  151. adoquery1.close;
  152. adoquery2.close;
  153. adotable1.close;
  154. close;
  155. end;
  156. end;
  157. procedure TSale_Form.Button4Click(Sender: TObject);
  158. var
  159. money:double;
  160. ret:integer;
  161. begin
  162. if edit4.text='' then
  163.  showmessage('请输入你的付款金额')
  164. else
  165. begin
  166.  money:=strtofloat(edit4.Text)-count;
  167.  if money<0 then
  168.      showmessage('金额不足')
  169.  else
  170.    begin
  171.      ret:=messagebox(0,pchar('找零:'+floattostr(money)),'交易确定',1);
  172.      if ret=2 then
  173.        begin
  174.          with adoquery2 do
  175.           begin
  176.            first;
  177.            while not eof do
  178.              begin
  179.                with datamodule1.adocommand1 do               //修改produce_t中sale_p的值
  180.                  begin
  181.                   commandtext:='update produce_t set p_sale=p_sale-1 where p_id=:i';
  182.                   parameters.ParamValues['i']:=adoquery2.fields[0].AsInteger;
  183.                   execute;
  184.                  end;
  185.                 next;
  186.             end;
  187.           end;
  188.          richedit1.text:='上次'+richedit1.text+'  交易被取消!!!';
  189.         end
  190.         else
  191.          begin
  192.           with adoquery2 do//查找商品库中库存量
  193.            begin
  194.            first;
  195.             begin
  196.             while not eof do
  197.              begin
  198.                with adotable1 do   //如果可以交易就将sell_t中的数据保存到sell_back_t中
  199.                    begin
  200.                    insert;
  201.                    fields.fields[1]:=adoquery2.fields[0];
  202.                    fields.fields[2]:=adoquery2.fields[1];
  203.                    fields.fields[3]:=adoquery2.fields[2];
  204.                    fields.fields[4].asinteger:=1;
  205.                    fields.fields[5].asdatetime:=now;
  206.                    fields.fields[6].asboolean:=true;
  207.                    post;
  208.                    end;
  209.                next;
  210.              end;
  211.             richedit1.text:='上次'+richedit1.text+'实收:'+edit4.text+'元整'+'  找零:'+floattostr(money)+'元整,交易成功^_^'+'交易时间:'+datetimetostr(now);
  212.             edit1.clear;
  213.             edit2.clear;
  214.             edit3.clear;
  215.             edit4.clear;
  216.             end;   //一次成功交易完成
  217.           end;
  218.          end;
  219.        with datamodule1.adocommand1 do    //删除sell_t中的数据
  220.         begin
  221.         commandtext:='delete from sell_t';
  222.         execute;
  223.         adoquery2.close;
  224.         panel2.visible:=false;
  225.         count:=0;
  226.         end;
  227.    end;
  228.  end;
  229. end;
  230. procedure TSale_Form.FormActivate(Sender: TObject);
  231. begin
  232.   if not adoquery1.active then
  233.   adoquery1.open;
  234.    if not adoquery2.active then
  235.   adoquery2.open;
  236.     if not adotable1.active then
  237.   adotable1.open;
  238. end;
  239. procedure TSale_Form.Button5Click(Sender: TObject); //取消本次交易
  240. var
  241. money:double;
  242. begin
  243. if edit4.text='' then
  244. edit4.text:='0';
  245.  money:=strtofloat(edit4.Text)-count;
  246.  with adoquery2 do
  247.   begin
  248.    first;
  249.    while not eof do
  250.     begin
  251.      with datamodule1.adocommand1 do               //恢复produce_t中sale_p的值
  252.       begin
  253.        commandtext:='update produce_t set p_sale=p_sale-1 where p_id=:i';
  254.        parameters.ParamValues['i']:=adoquery2.fields[0].AsInteger;
  255.        execute;
  256.       end;
  257.      next;
  258.     end;
  259.    end;                                     //清除sell_t中的数据
  260.    with datamodule1.adocommand1 do
  261.     begin
  262.      commandtext:='delete from sell_t';
  263.       execute;
  264.       adoquery2.close;
  265.       panel2.visible:=false;
  266.       count:=0;
  267.       if money<0 then
  268.         richedit1.text:='上次'+richedit1.text+'  交易因金额不足被取消!!!'
  269.       else
  270.           richedit1.text:='上次'+richedit1.text+'  交易被迫取消!!!' ;
  271.       end;
  272. end;
  273. procedure TSale_Form.Button6Click(Sender: TObject);
  274. begin
  275.   with adoquery2 do
  276.   begin
  277.    first;
  278.    while not eof do
  279.     begin
  280.      with datamodule1.adocommand1 do               //恢复produce_t中sale_p的值
  281.       begin
  282.        commandtext:='update produce_t set p_sale=p_sale-1 where p_id=:i';
  283.        parameters.ParamValues['i']:=adoquery2.fields[0].AsInteger;
  284.        execute;
  285.       end;
  286.      next;
  287.     end;
  288.    end;                                     //清除sell_t中的数据
  289.    with datamodule1.adocommand1 do
  290.     begin
  291.      commandtext:='delete from sell_t';
  292.       execute;
  293.       adoquery2.close;
  294.       panel2.visible:=false;
  295.       count:=0;
  296.      richedit1.text:='上次'+richedit1.text+'  所有记录被强制删除' ;
  297.       end;
  298. end;
  299. procedure TSale_Form.Timer1Timer(Sender: TObject);
  300. begin
  301. statusbar1.panels[6].text:='系统时间:'+DateTimeToStr(now);
  302. statusbar1.panels[0].text:='收银员: '+Login_Form.UseName;
  303. end;
  304. procedure TSale_Form.N5Click(Sender: TObject);
  305. begin
  306. help_form.show;
  307. end;
  308. end.