ubook.pas

来自「DELPHI编程入门篇.从基础入手,浅显易懂,一定物有所值.」· PAS 代码 · 共 590 行 · 第 1/2 页

PAS
590
字号
  if edit3.text = '' then
  begin
    showmessage('经手人不能为空');
    exit;
  end;
  for i:=1 to stringgrid1.RowCount-1 do
  begin
    if stringgrid1.Cells[1,i] = '' then
    begin
      showmessage('第'+inttostr(i)+'行商品编号不能为空!');
      exit;
    end;
    if stringgrid1.Cells[3,i] = '' then
    begin
      showmessage('第'+inttostr(i)+'行订购数量不能为空!');
      exit;
    end;
  end;
  try
    adoquery1.Connection.BeginTrans;
    adoquery1.SQL.Text:='select * from stockbook where bookid = '''+edit2.Text+'''';
    adoquery1.Open;
    recount:=adoquery1.RecordCount;
    if ((recount > 0) and (adoquery1.FieldByName('ifdeal').AsBoolean)) then
    begin
      showmessage('单据已经审核,不能修改!');
      adoquery1.close;
      exit;
    end;
    adoquery1.Edit;
    adoquery1.FieldByName('bookid').AsString:=edit2.Text;
    adoquery1.FieldByName('provid').AsString:=copy(combobox2.Text,1,5);
    adoquery1.FieldByName('bookdate').AsDateTime:=dtpicker3.Date;
    adoquery1.FieldByName('reachdate').AsDateTime:=dtpicker4.Date;
    adoquery1.FieldByName('handleman').AsString:=edit3.Text;
    adoquery1.Post;
    if recount > 0 then
    begin
      adoquery2.Connection.BeginTrans;
      for j:=1 to stringgrid1.RowCount-1 do
      begin
        adoquery2.SQL.Text:='select bookid,prid,booknum from stockbook1 where bookid = '''+edit2.Text+''' and prid = '''+stringgrid1.Cells[1,j]+'''';
        adoquery2.open;
        adoquery2.Edit;
        adoquery2.FieldByName('bookid').AsString:=edit2.Text;
        adoquery2.FieldByName('prid').AsString:=stringgrid1.Cells[1,j];
        adoquery2.FieldByName('booknum').AsString:=stringgrid1.Cells[3,j];
        adoquery2.Post;
      end;
    end
    else begin
      adoquery2.Connection.BeginTrans;
      for j:=1 to stringgrid1.RowCount-1 do
      begin
        adoquery2.SQL.Text:='insert into stockbook1 values("'+edit2.Text+'","'+stringgrid1.Cells[1,j]+'","'+stringgrid1.Cells[3,j]+'")';
        adoquery2.ExecSQL;
      end;
    end;
    adoquery2.Connection.CommitTrans;
    adoquery1.Connection.CommitTrans;
    adoquery2.Close;
    adoquery1.Close;
    showmessage('保存成功!');
    speedbutton1.Enabled:=true;
    speedbutton2.Enabled:=true;
    speedbutton4.Enabled:=true;
    speedbutton5.Enabled:=false;
    speedbutton6.Enabled:=false;
    speedbutton7.Enabled:=true;
    speedbutton8.Enabled:=true;
    edit1.Enabled:=true;
    combobox1.Enabled:=true;
    dtpicker1.Enabled:=true;
    dtpicker2.Enabled:=true;
    edit2.Enabled:=false;
    combobox2.Enabled:=false;
    dtpicker3.Enabled:=false;
    dtpicker4.Enabled:=false;
    edit3.Enabled:=false;
  except
    adoquery2.Connection.RollbackTrans;
    adoquery1.Connection.RollbackTrans;
    adoquery2.Close;
    adoquery1.Close;
  end;
end;

procedure Tfrmbook.dxButtonEdit1Exit(Sender: TObject);
begin
  dxbuttonedit1.Text:='';
  dxbuttonedit1.Visible:=false;
end;

procedure Tfrmbook.dxButtonEdit1ButtonClick(Sender: TObject;
  AbsoluteIndex: Integer);
begin
  Application.CreateForm(Tfrmfilprod, frmfilprod);
  frmfilprod.whosel:='book';
  frmfilprod.ShowModal;
  frmfilprod.Release;
end;

procedure Tfrmbook.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if stringgrid1.Col = 3 then
  begin
    if (stringgrid1.Cells[3,stringgrid1.Row] <> '') and (stringgrid1.Cells[1,stringgrid1.Row] <> '') then
    begin
      if key = 13 then
      stringgrid1.RowCount:=stringgrid1.RowCount+1;
    end;
  end;
end;

procedure Tfrmbook.StringGrid2Click(Sender: TObject);
var
  i,j,arow:integer;
begin
  arow:=stringgrid2.Row;
  if stringgrid2.Cells[1,1] <> '' then
  begin
    stringgrid2.Cols[0].Clear;
    stringgrid2.Cells[0,arow]:='*';
    edit2.Text:=stringgrid2.Cells[1,arow];
    dtpicker4.Date:=strtodatetime(stringgrid2.Cells[3,arow]);
    for i:= 0 to combobox2.Items.Count-1 do
    begin
      if copy(combobox2.Items.Strings[i],1,5) = stringgrid2.Cells[2,arow] then
        combobox2.ItemIndex:=i;
      break;
    end;
    adoquery1.SQL.Text:='select reachdate,handleman from stockbook where bookid = '''+stringgrid2.Cells[1,stringgrid2.Row]+'''';
    adoquery1.Open;
    dtpicker4.Date:=adoquery1.FieldValues['reachdate'];
    edit3.Text:=adoquery1.FieldValues['handleman'];
    adoquery1.Close;
    for i:=1 to stringgrid1.RowCount-1 do
    stringgrid1.Rows[i].Clear;
    stringgrid1.RowCount:=2;
    adoquery1.SQL.Text:='select stockbook1.prid,pluinfo.prname,stockbook1.booknum from stockbook1 inner join pluinfo on stockbook1.prid = pluinfo.prid where stockbook1.bookid = '''+stringgrid2.Cells[1,arow]+'''';
    adoquery1.Open;
    stringgrid1.RowCount:=adoquery1.RecordCount+1;
    j:=1;
    while not adoquery1.Eof do
    begin
      stringgrid1.Cells[1,j]:=adoquery1.FieldValues['prid'];
      stringgrid1.Cells[2,j]:=adoquery1.FieldValues['prname'];
      stringgrid1.Cells[3,j]:=adoquery1.FieldValues['booknum'];
      adoquery1.Next;
      inc(j);
    end;
    adoquery1.Close;
  end;
end;

procedure Tfrmbook.SpeedButton8Click(Sender: TObject);
begin
  self.Close;
end;

procedure Tfrmbook.SpeedButton6Click(Sender: TObject);
begin
  speedbutton1.Enabled:=true;
  speedbutton2.Enabled:=true;
  speedbutton4.Enabled:=true;
  speedbutton5.Enabled:=false;
  speedbutton6.Enabled:=false;
  speedbutton7.Enabled:=true;
  speedbutton8.Enabled:=true;
  edit1.Enabled:=true;
  combobox1.Enabled:=true;
  dtpicker1.Enabled:=true;
  dtpicker2.Enabled:=true;
  edit2.Enabled:=false;
  combobox2.Enabled:=false;
  dtpicker3.Enabled:=false;
  dtpicker4.Enabled:=false;
  edit3.Enabled:=false;
  stringgrid1.Col:=1;
end;

procedure Tfrmbook.SpeedButton4Click(Sender: TObject);
var
  i:integer;
begin
  if stringgrid1.Row > 0 then
  begin
    if messagedlg('真的要删除该订货单吗?',mtconfirmation,[mbyes,mbno],0)= mryes  then
    try
      adoquery1.Connection.BeginTrans;
      adoquery1.SQL.Text:='delete from stockbook where bookid = '''+stringgrid2.Cells[1,stringgrid2.Row]+'''';
      adoquery1.ExecSQL;
      adoquery2.Connection.BeginTrans;
      adoquery2.SQL.Text:='delete from stockbook1 where bookid = '''+stringgrid2.Cells[1,stringgrid2.Row]+'''';
      adoquery2.ExecSQL;
      adoquery2.Connection.CommitTrans;
      adoquery1.Connection.CommitTrans;
      adoquery2.Close;
      adoquery1.Close;
      if stringgrid2.RowCount = 2 then
      stringgrid2.Rows[1].Clear
      else begin
        for i:=stringgrid2.Row to stringgrid2.RowCount-2 do
          stringgrid2.Rows[i]:=stringgrid2.Rows[i+1];
        stringgrid2.RowCount:=stringgrid2.RowCount-1;
      end;
      for i:=1 to stringgrid1.RowCount-1 do
        stringgrid1.Rows[i].Clear;
        stringgrid1.RowCount:=2;
      showmessage('删除成功!');
    except
      adoquery2.Connection.RollbackTrans;
      adoquery2.Connection.RollbackTrans;
      adoquery2.Close;
      adoquery1.Close;
    end;
  end;  
end;

procedure Tfrmbook.N1Click(Sender: TObject);
var
  arow:integer;
begin
  if speedbutton5.Enabled then
  begin
    arow:=stringgrid1.RowCount-1;
    if ((stringgrid1.Cells[1,arow]<>'') and (stringgrid1.Cells[2,arow] <> '')) then
    stringgrid1.RowCount:=stringgrid1.RowCount+1;
  end
  else
    showmessage('请先点击添加或修改按钮');
end;

procedure Tfrmbook.N2Click(Sender: TObject);
var
  i:integer;
begin
  if speedbutton5.Enabled then
  begin
    if ((stringgrid1.Cells[1,1] <> '') and (stringgrid1.Row > 0)) then
    begin
      adoquery1.SQL.Text:='select ifdeal from stockbook where bookid = '''+stringgrid2.Cells[1,stringgrid2.Row]+'''';
      adoquery1.Open;
      if not (adoquery1.FieldByName('ifdeal').AsBoolean) then
      begin
        adoquery2.SQL.Text:='delete from stockbook1 where bookid = '
        +'"'+stringgrid2.Cells[1,stringgrid2.Row]+'"'+' and prid = '''+stringgrid1.Cells[1,stringgrid1.Row]+'''';
        adoquery2.ExecSQL;
        adoquery2.Close;
        if stringgrid1.RowCount > 2 then
        begin
          for i:=stringgrid1.Row to stringgrid1.RowCount-2 do
            stringgrid1.Rows[i]:=stringgrid1.Rows[i+1];
          stringgrid1.Rows[stringgrid1.RowCount-1].Clear;
          stringgrid1.RowCount:=stringgrid1.RowCount-1;
        end
        else
          stringgrid1.Rows[1].Clear;
      end;
      adoquery1.Close;
    end;
  end
  else
    showmessage('请先点击添加或修改按钮');  
end;

procedure Tfrmbook.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if not speedbutton8.Enabled then
  begin
    canclose:=false;
    showmessage('请先保存数据然后退出');
  end;
end;

procedure Tfrmbook.SpeedButton7Click(Sender: TObject);
begin
  if stringgrid1.Cells[1,1] <> '' then
  begin
    adoquery1.SQL.Text:='select stockbook1.prid,pluinfo.prname,stockbook1.booknum'
    +' from stockbook1 inner join pluinfo on stockbook1.prid = pluinfo.prid'
    +' where stockbook1.bookid = '''+edit2.Text+'''';
    adoquery2.SQL.Text:='select stockbook.bookid,stockbook.bookdate,'
    +'provinfo.provname,stockbook.handleman from stockbook inner join provinfo '
    +'on stockbook.provid = provinfo.provid where stockbook.bookid = '''+edit2.Text+'''';
    rvproject1.Execute;
  end
  else
    showmessage('请先查询要打印的数据');
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?