unit U_JBData; //初次使用系统第一卷布禁止交班。    

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, StdCtrls, Buttons, ExtCtrls;

type
  TfrmJBData = class(TForm)
    Button1: TButton;
    MJLen: TEdit;
    Label1: TLabel;
    ADOQueryCmd: TADOQuery;
    Label2: TLabel;
    Panel3: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton12: TSpeedButton;
    ADOQueryTemp: TADOQuery;
    Button2: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmJBData: TfrmJBData;

implementation
Uses
U_DataLink,U_Fun,U_ZDYHelp,U_iniParam;

{$R *.dfm}

procedure TfrmJBData.FormDestroy(Sender: TObject);
begin
  frmJBData:=nil;
end;

procedure TfrmJBData.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  Action:=caFree;
end;

procedure TfrmJBData.FormShow(Sender: TObject);
begin
  MJLen.SetFocus;
end;

procedure TfrmJBData.SpeedButton1Click(Sender: TObject);
begin
  MJLen.Text:=Trim(MJLen.Text)+Trim(TSpeedButton(Sender).Caption);
  MJLen.SelectAll;
end;

procedure TfrmJBData.SpeedButton12Click(Sender: TObject);
begin
  if Trim(MJLen.Text)<>'' then
  begin
    MJLen.Text:=Copy(Trim(MJLen.Text),1,Length(Trim(MJLen.Text))-1);
    MJLen.SelectAll;
  end;
end;

procedure TfrmJBData.Button1Click(Sender: TObject);
var
  FFMJLen:Double;
  MJIDInt:Integer;
  maxno,maxno10,FBanZu,maxno100,maxno200,maxno300:String;
begin
  if Trim(MJLen.Text)='' then
  begin
    Application.MessageBox('交班卷长不能为空！','提示',0);
    MJLen.SetFocus;
    exit;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(Max(Cast(MJID as int)),0) MJIDInt from WFB_MJJY');
    if Trim(SCXFlag)<>'' then
    begin
      sql.Add(' where JTType='''+Trim(SCXFlag)+'''');
    end;
    Open;
  end;
  
  MJIDInt:=ADOQueryTemp.fieldbyname('MJIDInt').Value;
  maxno100:=Trim(FormatDateTime('yyyyMMdd',SGetServerDateTime(ADOQueryTemp)));
  maxno100:=Trim(SCXFlag)+Trim(Copy(maxno100,3,6));
  maxno200:=IntToStr(MJIDInt);
  maxno200:=Copy(maxno200,1,7);
  if StrToInt(maxno100)>StrToInt(maxno200) then
  begin
    maxno300:=Trim(maxno100)+'01';
  end else
  begin
    maxno300:=IntToStr(MJIDInt+1);
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from WFB_JYResult where MJID='''+Trim(maxno300)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('未检验不能交班！','提示',0);
    Exit;
  end;
  // 初次使用系统第一卷布禁止交班。
  if MJIDInt>0 then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from WFB_JB where MJID='''+Trim(Inttostr(MJIDInt+1))+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      Application.MessageBox('交班卷长已登记！','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要执行操作吗？确定后数据将不能修改！','提示',32+4)<>IDYES then
    begin
      MJLen.Text:='';
      MJLen.SetFocus;
      Exit;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      //保存交班表
      if GetLSNo(ADOQueryCmd,maxno,'JB','WFB_JB',2,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取交班ID异常！','提示',0);
        Exit;
      end;

      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from SY_User where UserId='''+Trim(DCode)+'''');
        Open;
      end;
      FBanZu:=Trim(ADOQueryTemp.fieldbyname('BanZu').AsString);
      FFMJLen:=StrToFloat(MJLen.Text);
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from WFB_JB where 1<>1');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('JBID').Value:=Trim(maxno);
        FieldByName('MJID').Value:=Trim(maxno300);
        FieldByName('MJLen').Value:=FFMJLen;
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('BanZu').Value:=Trim(FBanZu);
        if Trim(SCXFlag)<>'' then
        begin
          FieldByName('JTType').Value:=Trim(SCXFlag);
        end;
        Post;
      end;
      //保存交班表
      //保存班组员工产量表
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from SY_User where BanZu='''+Trim(FBanZu)+'''');
        Open;
      end;
      with ADOQueryTemp do
      begin
        First;
        while not eof do
        begin
          if GetLSNo(ADOQueryCmd,maxno10,'BZ','WFB_BanZu',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取班组号失败！','提示',0);
            Exit;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from WFB_BanZu where 1<>1');
            open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('BZID').Value:=Trim(maxno10);
            FieldByName('BanZu').Value:=Trim(FBanZu);
            FieldByName('MJID').Value:=Trim(maxno300);
           // FieldByName('MJID').Value:=Trim(IntToStr(MJIDInt+1));
            FieldByName('MJLen').Value:=StrToFloat(MJLen.Text);
            FieldByName('UserId').Value:=Trim(ADOQueryTemp.fieldbyname('UserId').AsString);
            FieldByName('UserName').Value:=Trim(ADOQueryTemp.fieldbyname('UserName').AsString);
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('JBID').Value:=Trim(maxno);
            Post;
          end;
          Next;
        end;
      end;
      
      //保存班组员工产量表
      ADOQueryCmd.Connection.CommitTrans;
      //Application.MessageBox('登记成功!','提示',0);
      Exit;
    except

      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('保存数据异常！','提示',0);
    end;
  end else
  begin
    Application.MessageBox('初次使用系统，第一卷布禁止交班！','提示',0);
    Exit;
  end;

end;

procedure TfrmJBData.Button2Click(Sender: TObject);
begin
  Close;
end;

end.
