⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit3.~pas

📁 < SQL SERVER 2000 案例教程>>,冶金工业出版社,这本书的代码
💻 ~PAS
📖 第 1 页 / 共 2 页
字号:
end;

procedure Tmain.delleaveClick(Sender: TObject);
var temp:string;
begin
  //判断是否有类型被选择
  if  trim(leave.Text)='' then
  begin
    showmessage('请先选择一个类型');
  end else
  begin
    with  mydata.Query2 do
    begin
      close;
      with  sql do
      begin
        clear;
        //判断请假表中是否有该类型的请假时间结束时间大于今天
        add('select * from 请假表 where 请假类型=:type and 结束时间>=:time');
        ParamByName('type').AsString:=trim(leave.Text);
        ParamByName('time').AsString:=datetostr(now());
      end;
      open;
      if  findlast()  then
      begin
        showmessage('该请假类型在使用中,不能删除,请修改相关资料');
      end else
      begin
        close;
        with  sql do
        begin
          clear;
          //添加SQL语句删除该记录
          add('delete from 请假类型表 where 类型名=:type');
          ParamByName('type').AsString:=trim(leave.Text);
        end;
        execsql;
        oninit1();
      end;
    end;
  end;
end;

procedure Tmain.inputcheckClick(Sender: TObject);
var index:integer; //记录出勤资料表中最后一条记录的下标
begin
  with  mydata.Query3 do//使用Query3进行数据库操作
  begin
    close;
    with  sql do
    begin
      clear;//先判断输入的员工号是否在员工资料表中
      add('select * from 员工资料表 where 员工号=:code');
      ParamByName('code').AsString:=trim(checkcode.Text);
    end;
    open;
    //如果输入的员工号正确,则执行下一步操作
    if  findlast()  then
    begin
      close;
      with  sql do
      begin
        clear;
        //先判断在出勤资料表中是否有该员工的当天记录
        add('select * from  出勤资料表 where 员工号=:code and 记录日期=:date');
        ParamByName('code').AsString:=trim(checkcode.Text);
        ParamByName('date').AsString:=datetostr(now());
      end;
      open;
      //如果存在,则进行更新操作,否则执行插入操作
      if  findlast()  then
      begin
        close;
        with  sql do
        begin
          clear;
          //根据单选框的选择情况添加SQL语句
          add('update 出勤资料表 set');
          //当选择为上午上班时间时,更新上午上班时间
          if  radioamstart.Checked  then
          begin
            add(' 上午上班时间=:time');
            ParamByName('time').AsString:=timetostr(checkamstart.Time);
          end;
          //当选择为上午下班时间时,更新上午下班时间
          if  radioamend.Checked  then
          begin
            add(' 上午下班时间=:time');
            ParamByName('time').AsString:=timetostr(checkamend.Time);
          end;
          //当选择为下午上班时间时,更新下午上班时间
          if  radiopmstart.Checked  then
          begin
            add(' 下午上班时间=:time');
            ParamByName('time').AsString:=timetostr(checkpmstart.Time);
          end;
          //当选择为下午下班时间时,更新下午下班时间
          if  radiopmend.Checked  then
          begin
            add(' 下午下班时间=:time');
            ParamByName('time').AsString:=timetostr(checkpmend.Time);
          end;
          add(' where 员工号=:code');
          ParamByName('code').AsString:=trim(checkcode.Text);
        end;
        execsql;
      end else
      begin
        close;
        with  sql do
        begin
          clear;
          //添加插入资料的SQL语句
          add('insert into 出勤资料表 values(:index,:code,:amstart,:amend,:pmstart,:pmend,:date1)');
          //获取出勤资料表中最后一条记录的编号
          with  mydata.Query_time do
          begin
            close;
            with  sql do
            begin
              clear;
              add('select * from  出勤资料表');
            end;
            open;
            last; //移到最后一条记录
            if  findlast  then
            begin
              //如果表不为空则欲插入资料的记录为表中最大记录号加1
              index:=FieldByName('记录号').AsInteger+1;
            end else
            begin
              //如果表为空,则记录号为0
              index:=1;
            end;
          end;
          //给各参数赋值,开始时假设所有时间为'0:0:0',然后根据选择情况重新赋值
          ParamByName('index').AsInteger:=index;
          ParamByName('code').AsString:=trim(checkcode.Text);
          ParamByName('amstart').AsString:='0:0:0';
          ParamByName('amend').AsString:='0:0:0';
          ParamByName('pmstart').AsString:='0:0:0';
          ParamByName('pmend').AsString:='0:0:0';
          //获取当天的日期,并将它赋给欲插入资料
          ParamByName('date1').AsString:=datetostr(now());
          //如果选择上午上班时间
          if  radioamstart.Checked  then
          begin
            ParamByName('amstart').AsString:=timetostr(checkamstart.Time);
          end;
          //如果选择上午下班时间
          if  radioamend.Checked  then
          begin
            ParamByName('amend').AsString:=timetostr(checkamend.Time);
          end;
          //如果选择下午上班时间
          if  radiopmstart.Checked  then
          begin
            ParamByName('pmstart').AsString:=timetostr(checkpmstart.Time);
          end;
          //如果选择下午下班时间
          if  radiopmend.Checked  then
          begin
            ParamByName('pmend').AsString:=timetostr(checkpmend.Time);
          end;
        end;
        execsql;
        mydata.Query_staff.Close;
        mydata.Query_staff.SQL.Clear;
        mydata.Query_staff.SQL.Add('update 员工资料表 set 考勤=:checkstate where 员工号=:code');
        mydata.Query_staff.ParamByName('checkstate').AsString:='1';
        mydata.Query_staff.ParamByName('code').AsString:=trim(checkcode.Text);
        mydata.Query_staff.ExecSQL;
      end;
    end else
    begin
      showmessage('该工号不存在');
    end;
  end;
end;

procedure Tmain.RadioButton1Click(Sender: TObject);
begin
  radiobutton1.Checked:=true;
  radiobutton2.Checked:=false;
  radiobutton3.Checked:=false;
  oninit2();
end;

procedure Tmain.RadioButton2Click(Sender: TObject);
begin
  radiobutton1.Checked:=false;
  radiobutton2.Checked:=true;
  radiobutton3.Checked:=false;
  oninit2();
end;

procedure Tmain.RadioButton3Click(Sender: TObject);
begin
  radiobutton1.Checked:=false;
  radiobutton2.Checked:=false;
  radiobutton3.Checked:=true;

  oninit2();
end;

procedure Tmain.managecheckClick(Sender: TObject);
var index:integer;
    s1,e1:TDateTime;
    s2,e2:TTime;
    S3,e3:TDate;
    hh,mm,ss,kk:Word;
begin
  with  mydata.Query3   do //使用Query3进行数据库操作
  begin
    close;
    with  sql do
    begin
      clear;
      //判断员工资料表中是否有该工号
      add('select * from 员工资料表 where 员工号=:code');
      ParamByName('code').AsString:=trim(Edit5.Text);
    end;
    open;
    if  findlast()  then  //当员工号输入无误则进行进一步操作
    begin
      if  radiobutton1.Checked  then   //如果是请假操作
      begin
        close;
        with  sql do
        begin
          clear;
          //添加插入资料的SQL语句
          add('insert into 请假表 values(:index,:code,:type,:starttime,:endtime)');
          //获取在请假表中最后一条记录的记录号
          mydata.Query_time.Close;   //关闭mydata.Query_time
          mydata.Query_time.SQL.Clear; //清空mydata.Query_time中SQL属性内容
          mydata.Query_time.SQL.Add('select * from 请假表');
          mydata.Query_time.Open;
          mydata.Query_time.Last;
          if  mydata.Query_time.FindLast() then
          begin
            index:=mydata.Query_time.fieldByName('记录号').AsInteger+1;
          end else
          begin
            index:=0;
          end;
          //给插入语句中的各参数赋值
          ParamByName('index').AsInteger:=index;
          ParamByName('code').AsString:=trim(edit5.Text);
          ParamByName('type').AsString:=trim(checktype.Text);
          ParamByName('starttime').AsString:=datetostr(startdate.Date)+' '+timetostr(starttime.Time);
          ParamByName('endtime').AsString:=datetostr(enddate.Date)+' '+timetostr(endtime.Time);
        end;
        execsql;
        //更新员工资料表中的考勤标志
        mydata.Query_staff.Close;
        mydata.Query_staff.SQL.Clear;
        mydata.Query_staff.SQL.Add('update 员工资料表 set 考勤=:checkstate where 员工号=:code');
        mydata.Query_staff.ParamByName('checkstate').AsString:='1';
        mydata.Query_staff.ParamByName('code').AsString:=trim(edit5.Text);
        mydata.Query_staff.ExecSQL;
      end;
    end else
    begin
      //当员工号输入不正确时给出提示信息
      showmessage('工号不正确');
    end;
  end;
end;
procedure Tmain.execcheckClick(Sender: TObject);
var index:integer;//用来标记考勤表中最后一条记录的序号
    money,money1,money2,money3,money4,money5,money6:double;//用来记录各种金额
    checkresult:string;//记录某员工的考勤情况
    //用来保存从上下班时间表中读取的时间和员工的出勤时间
    amstart,amend,pmstart,pmend,temp:TDateTime;
    sign:boolean; //用来判断是不是第一次进行考勤
begin
  money:=0;
  sign:=true;

  //读取不正常上班表中各种不正常上班类型对应的金额
  with  mydata.Query3 do
  begin
    close;
    with  sql do
    begin
      clear;
      add('select * from 不正常上班表');
    end;
    open;
    if  findlast() then
    begin
      money1:=strtofloat(FieldByName('上午迟到').asstring);
      money2:=strtofloat(FieldByName('上午早退').AsString);
      money3:=strtofloat(FieldByName('上午旷工').AsString);
      money4:=strtofloat(FieldByName('下午迟到').asstring);
      money5:=strtofloat(FieldByName('下午早退').AsString);
      money6:=strtofloat(FieldByName('下午旷工').AsString);
    end else
    begin
    //当表中无记录时所有金额为0
      money1:=0;
      money2:=0;
      money3:=0;
      money4:=0;
      money5:=0;
      money6:=0;
    end;
    close;
    //获取考勤表中最后一条记录的序号,同时判断是否当天第一次考勤
    with  sql do
    begin
      clear;
      add('select * from 考勤表');
    end;
    open;
    last;
    if  findlast()  then
    begin
      index:=FieldByName('记录号').AsInteger+1;
      //如果最后一条记录的日期是当天,则说明已经进行考勤,无须再进行考勤
      if (trim(FieldbyName('日期').AsString)=trim(datetostr(now()))) then
      begin
        sign:=false;
        showmessage('今天已考勤,不能再进行考勤');
      end;
    end else
    begin
      index:=0;
    end;
    close;
    if sign then
    begin
      //读取上下班时间表中的时间设定,以便判断员工上下班是否正常
      with  sql do
      begin
        clear;
        add('select * from 上下班时间表');
      end;
      open;
      amstart:=strtotime(timetostr(FieldByName('上午上班时间').AsDateTime));
      amend:=strtotime(timetostr(FieldByName('上午下班时间').AsDateTime));
      pmstart:=strtotime(timetostr(FieldByName('下午上班时间').AsDateTime));
      pmend:=strtotime(timetostr(FieldByName('下午下班时间').AsDateTime));
      close;
      //读取出勤资料表中的内容,并作相应处理
      with  sql do
      begin
        clear;
        add('select * from 出勤资料表 where 记录日期=:date');
        ParamByName('date').AsString:=datetostr(now());
      end;
      open;
      while eof=false do
      begin
        checkresult:='';
        //当上下班时间相等时可认为其旷工,因为在往出勤资料表中添加资料时会初始各种时间是相同的
        if (FieldByName('上午上班时间').AsDateTime=FieldByName('上午下班时间').AsDateTime)then
        begin
          checkresult:=checkresult+' 上午旷工 ';
          money:=money+money3;
        end else
        begin
          temp:=strtotime(timetostr(FieldByName('上午上班时间').asDateTime));
          //对上午上班迟到进行处理
          if temp>amstart then
          begin
            checkresult:=checkresult+' 上午迟到 ';
            money:=money+money1*gettimecount(amstart,temp);
          end else
          begin
            //对上午早退进行处理
            temp:=strtotime(timetostr(FieldByName('上午下班时间').asDateTime));
            if  temp<amend then
            begin
              checkresult:=checkresult+' 上午早退 ';
              money:=money+money2*gettimecount(temp,amend);
            end else
            begin
              //正常上班
              checkresult:=checkresult+' 上午正常上班 ';
              money:=0;
            end;
          end;
        end;
        if (FieldByName('下午上班时间').AsDateTime=FieldByName('下午下班时间').AsDateTime)then
        begin
          checkresult:=checkresult+' 下午旷工 ';
          money:=money+money6;
        end else
        begin
          temp:=strtotime(timetostr(FieldByName('下午上班时间').asDateTime));
          if temp>pmstart then
          begin
            checkresult:=checkresult+' 下午迟到 ';
            money:=money+money4*gettimecount(pmstart,temp);
          end else
          begin
            temp:=strtotime(timetostr(FieldByName('下午下班时间').asDateTime));
            if  temp<pmend then
            begin
              checkresult:=checkresult+' 下午迟到 ';
              money:=money+money5*gettimecount(temp,pmend);
            end else
            begin
              checkresult:=checkresult+' 下午正常上班 ';
              money:=money+0;
            end;
          end;
        end;
        //把各种考勤结果保存在考勤资料表中,以便进一下处理
        mydata.Query_staff.Close; //关闭mydata.Query_staff
        mydata.Query_staff.SQL.Clear;//清空mydata.Query_staff中SQL内容
        //添加SQL语句
        mydata.Query_staff.SQL.Add('insert into 考勤表 values(:index,:code,:record,:money,:date)');
        //给各参数赋值
        mydata.Query_staff.ParamByName('index').AsInteger:=index;
        index:=index+1;
        showmessage(inttostr(index));
        mydata.Query_staff.ParamByName('code').AsString:=trim(FieldByName('员工号').AsString);
        mydata.Query_staff.ParamByName('record').AsString:=checkresult;
        mydata.Query_staff.ParamByName('money').AsFloat:=money;
        mydata.Query_staff.ParamByName('date').AsString:=datetostr(now());
        //执行SQL语句,把考勤结果写入考勤表
        mydata.Query_staff.ExecSQL;
        //把mydata.Query3中保存的记录集移到下一条,其相当于mydata.query3.next;
        next;
      end;
    end;
  end;
end;

end.

⌨️ 快捷键说明

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