unit U_DyeOutput;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Menus, cxLookAndFeelPainters, cxButtons,
  cxControls, cxContainer, cxEdit, cxTextEdit, Buttons, DB, ADODB, cxGraphics,
  cxLookAndFeels, cxCheckBox, cxMaskEdit, cxDropDownEdit, cxCheckComboBox,
  cxGroupBox, cxRadioGroup, CheckLst, cxCheckListBox, U_BaseList, dxSkinsCore,
  dxSkinsDefaultPainters, Datasnap.DBClient, MovePanel;

type
  TfrmDyeOutput = class(TfrmBaseList)
    Panel1: TPanel;
    Label1: TLabel;
    Panel2: TPanel;
    Scan: TcxTextEdit;
    Label2: TLabel;
    PCID: TcxTextEdit;
    Label3: TLabel;
    custName: TcxTextEdit;
    Label4: TLabel;
    Label5: TLabel;
    C_Name: TcxTextEdit;
    C_XINHAO: TcxTextEdit;
    Label6: TLabel;
    C_COLORDEPTH: TcxTextEdit;
    Label7: TLabel;
    C_Color: TcxTextEdit;
    Label8: TLabel;
    Qty: TcxTextEdit;
    Label12: TLabel;
    Panel_JP: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton12: TSpeedButton;
    SpeedButton49: TSpeedButton;
    ADOQuery1: TADOQuery;
    ADO_Car: TADOQuery;
    ADO_YG: TADOQuery;
    ADO_Cmd: TADOQuery;
    ADO_Tmp: TADOQuery;
    SpeedButton14: TSpeedButton;
    TSNOTE: TcxTextEdit;
    Label16: TLabel;
    Label17: TLabel;
    EEName: TcxTextEdit;
    SpeedButton15: TSpeedButton;
    SpeedButton16: TSpeedButton;
    SpeedButton17: TSpeedButton;
    SpeedButton19: TSpeedButton;
    GlideMsg: TComboBox;
    CDS_GX: TClientDataSet;
    SpeedButton13: TSpeedButton;
    SpeedButton21: TSpeedButton;
    cxButton3: TcxButton;
    Label9: TLabel;
    Label10: TLabel;
    C_Width: TcxTextEdit;
    C_GramWeight: TcxTextEdit;
    Label11: TLabel;
    JITAINO: TcxTextEdit;
    Label13: TLabel;
    PCPIECE: TcxTextEdit;
    SpeedButton11: TSpeedButton;
    SpeedButton22: TSpeedButton;
    SpeedButton23: TSpeedButton;
    SpeedButton24: TSpeedButton;
    SpeedButton25: TSpeedButton;
    SpeedButton26: TSpeedButton;
    Label14: TLabel;
    rollnum: TcxTextEdit;
    Label15: TLabel;
    KYPS: TcxTextEdit;
    Label54: TLabel;
    Label55: TLabel;
    Edit16: TEdit;
    Edit17: TEdit;
    MovePanel1: TMovePanel;
    tishi: TLabel;
    Button1: TButton;
    Timer1: TTimer;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
    procedure SpeedButton49Click(Sender: TObject);
    procedure GlideRateClick(Sender: TObject);
    procedure ScanKeyPress(Sender: TObject; var Key: Char);
    procedure Sel1Click(Sender: TObject);
    procedure SpeedButton14Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxButton3Click(Sender: TObject);
    procedure TSNOTEExit(Sender: TObject);
    procedure GlideMsgChange(Sender: TObject);
    procedure Edit16Click(Sender: TObject);
    procedure Edit17Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    fGlideName, fSCunit, fJPUnit: string;
    procedure InitCar(fPCID: string);

    { Private declarations }
  public
    fflag: Integer;
    fKeyNo: string;
    { Public declarations }
  end;

var
  frmDyeOutput: TfrmDyeOutput;

implementation

uses
  U_DataLink, U_RTFun;
{$R *.dfm}

procedure SOutData(mParent: TWinControl; FTag: Integer);
var
  i, idx: integer;
begin
  with mParent do
  begin
    for i := 0 to ControlCount - 1 do
    begin
      if Controls[i] is TLabel then
        continue;
      if Controls[i].Tag <> FTag then
        continue;
      if Controls[i] is TEdit then
      begin
        TEdit(Controls[i]).Text := '';
      end
      else if Controls[i] is TcxTextEdit then
      begin
        TcxTextEdit(Controls[i]).Text := '';
      end;
    end;
  end;
end;

procedure SInData(ADOQueryTmp: TADOQuery; mParent: TWinControl; FTag: Integer);
var
  i, idx: integer;
  mfield, mfieldCode: string;
begin
  with ADOQueryTmp do
  begin
    if isEmpty then
      exit;
    with mParent do
    begin
      for i := 0 to ControlCount - 1 do
      begin
        if Controls[i] is TLabel then
          continue;
        if Controls[i].Tag <> FTag then
          continue;
        mfield := Controls[i].Name;
        if Controls[i] is TEdit then
        begin
          if Trim(fieldByName(mfield).AsString) <> '' then
            TEdit(Controls[i]).Text := trim(fieldByName(mfield).AsString)
          else
            TEdit(Controls[i]).Text := '';
        end
        else if Controls[i] is TcxTextEdit then
        begin
          if Trim(fieldByName(mfield).AsString) <> '' then
            TcxTextEdit(Controls[i]).Text := trim(fieldByName(mfield).AsString)
          else
            TcxTextEdit(Controls[i]).Text := '';
        end;
      end; // end for
    end; // end with
  end;  //end for with
end;

procedure TfrmDyeOutput.InitCar(fPCID: string);
begin
  try
    with ADO_Car do
    begin
      close;
      sql.Clear;
      sql.Add(' select * from Dye_Plan_Card ');
      sql.Add('where PCID=' + quotedstr(trim(fPCID)));
//      showmessage(sql.Text);
      open;
    end;
//    if trim(ADO_Car.fieldbyname('ShowMsg').AsString) <> '' then
//    begin
//      Scan.text := '';
//      Application.MessageBox(pchar(trim(ADO_Car.fieldbyname('ShowMsg').AsString)), '提示信息', MB_ICONERROR);
//      ADO_Car.Close;
//      exit;
//    end
//    else
    SInData(ADO_Car, Panel2, 2);
//    qty.text := ADO_Car.FieldByName('pcqty').AsString;
    Qty.Enabled := false;

    Qty.Enabled := true;

    Scan.text := '';
    Scan.OnClick(Scan);
  except
    Scan.text := '';
    Scan.OnClick(Scan);
  end;
end;

procedure TfrmDyeOutput.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := cafree;
end;

procedure TfrmDyeOutput.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyeOutput := nil;
end;

procedure TfrmDyeOutput.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_ESCAPE then  // VK_ESCAPE 是 ESC 键的常量
  begin
    Key := 0;  // 清除按键，防止继续传递
    Close;     // 或者 ModalResult := mrCancel;
  end;
end;

procedure TfrmDyeOutput.FormCreate(Sender: TObject);
begin
  inherited;
  Panel2.Align := alclient;
  SOutData(Panel2, 2);
  SOutData(Panel2, 0);
end;

procedure TfrmDyeOutput.SpeedButton1Click(Sender: TObject);
var
  fsj: string;
begin
  fsj := Trim(TSpeedButton(Sender).Hint);
  if Trim(fsj) = '' then
    Exit;
  fsj := Trim(TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text);
  TcxTextEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text := fsj + Trim(TSpeedButton(Sender).Caption);

  TcxTextEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).SelStart := length(TcxTextEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text);

end;

procedure TfrmDyeOutput.SpeedButton12Click(Sender: TObject);
var
  fsj: string;
begin
  fsj := Trim(TEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text);
  if Trim(fsj) = '' then
    Exit;
  TcxTextEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text := Copy(fsj, 1, Length(fsj) - 1);
  TcxTextEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).SelStart := length(TcxTextEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).Text);

end;

procedure TfrmDyeOutput.SpeedButton49Click(Sender: TObject);
begin
  Panel_JP.Visible := false;
end;

procedure TfrmDyeOutput.Edit16Click(Sender: TObject);
begin
  inherited;
  if Trim(Edit16.Text) = '' then
  begin
    Edit16.Text := '√';
    Edit17.Text := '';

  end
  else
  begin
    Edit16.Text := '';

  end;
end;

procedure TfrmDyeOutput.Edit17Click(Sender: TObject);
begin
  inherited;
  if Trim(Edit17.Text) = '' then
  begin
    Edit16.Text := '';
    Edit17.Text := '√';

  end
  else
  begin
    Edit17.Text := '';

  end;
end;

procedure TfrmDyeOutput.Timer1Timer(Sender: TObject);
begin
  inherited;
  Button1.Click();
  Timer1.Enabled := False;
end;

procedure TfrmDyeOutput.TSNOTEExit(Sender: TObject);
var
  fsj: string;
  str: string;
begin
//  with ADO_Tmp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('select * from SY_Employee ');
//    sql.Add('where EECode =' + quotedstr(EECode.Text));
//    open;
//  end;
//  EEName.Text := trim(ADO_Tmp.fieldbyname('EEName').AsString);

end;

procedure TfrmDyeOutput.GlideMsgChange(Sender: TObject);
begin
  inherited;
  if CDS_GX.Locate('Name', GlideMsg.Text, []) then
  begin
    with ADO_Tmp do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT yyps=isnull(clps,0) from Dye_Plan_Glide X where pcid=' + QuotedStr(PCID.Text));

      sql.Add('and X.GlideNo=' + QuotedStr(trim(CDS_GX.FieldByName('GlideNo').AsString)));
      sql.Add('and X.GlideName=' + QuotedStr(trim(CDS_GX.FieldByName('GlideName').AsString)));
      Open;
    end;

    if ADO_Tmp.IsEmpty = false then
    begin
      KYPS.Text := FloatToStr(strtofloatdef(PCPIECE.Text, 0) - ADO_Tmp.FieldByName('yyps').asfloat);
      rollnum.text := KYPS.Text;
    end;
  end;

end;

procedure TfrmDyeOutput.GlideRateClick(Sender: TObject);
var
  i: Integer;
begin
  Panel_JP.Visible := True;
  with Panel_JP do
  begin
    for i := 0 to ControlCount - 1 do
    begin
      if Controls[i] is TSpeedButton then
      begin
        TSpeedButton(Controls[i]).Hint := Trim(TEdit(Sender).Name);
      end;
    end;
  end;

end;

procedure TfrmDyeOutput.ScanKeyPress(Sender: TObject; var Key: Char);
var
  fsj: string;
  str: string;
begin
  if trim(Scan.Text) = '' then
    exit;
  if Key = #13 then
  begin
    InitCar(Scan.Text);

    fsj := 'exec P_Dye_Glide_Get_Idx ' + quotedstr(PCID.Text);

    SInitComBoxBySql(ADO_Cmd, GlideMsg, False, fsj);

    with ADO_Tmp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Dye_Glide_Get_Idx ' + quotedstr(PCID.Text));
      Open;
    end;
    SCreateCDS(ADO_Tmp, CDS_GX);
    SInitCDSData(ADO_Tmp, CDS_GX);
    CDS_GX.First;
    GlideMsg.ItemIndex := 0;
    GlideMsgChange(Sender);
  end;
end;

procedure TfrmDyeOutput.Button1Click(Sender: TObject);
begin
  inherited;
  MovePanel1.Visible := false;
end;

procedure TfrmDyeOutput.cxButton3Click(Sender: TObject);
var
  key: Char;
begin
  key := #13;
  ScanKeyPress(Scan, key);

end;

procedure TfrmDyeOutput.Sel1Click(Sender: TObject);
begin
  if trim(TcxTextEdit(Sender).Text) = '√' then
  begin
    TcxTextEdit(Sender).Text := '';
  end
  else
  begin
    TcxTextEdit(Sender).Text := '√';
  end;
end;

procedure TfrmDyeOutput.SpeedButton14Click(Sender: TObject);
begin

  try
    SpeedButton14.Enabled := False;
//    if PCID.Text = '' then
//    begin
//      raise Exception.Create(pchar('请重新刷卡！'));
//    end;

    if PCID.Text = '' then
    begin
      MovePanel1.Visible := true;
      SpeedButton14.Enabled := True;
      tishi.Caption := '请重新刷卡';
      Timer1.Enabled := true;
      exit;
    end;
    if strtofloatdef(trim(ROLLNUM.Text), 0) = 0 then
    begin
      MovePanel1.Visible := true;
      SpeedButton14.Enabled := True;
      tishi.Caption := '产出匹数不能为空或者零';
      Timer1.Enabled := true;
      exit;
    end;
    if not CDS_GX.Locate('Name', GlideMsg.Text, []) then
    begin
      MovePanel1.Visible := true;
      SpeedButton14.Enabled := True;
      tishi.Caption := '工序号不能为空';
      Timer1.Enabled := true;
      exit;
    end;
//    raise Exception.Create(pchar('产出匹数不能为空或者零！'));
//    if not CDS_GX.Locate('Name', GlideMsg.Text, []) then
//      raise Exception.Create(pchar('工序号不能为空!'));
    if GetLSNo(ADO_Tmp, fkeyNo, 'CL', 'CL_Group', 4, 1) = False then
      raise Exception.Create(pchar('取最大号失败!'));
//    if (Edit16.Text = '') and (Edit17.Text = '') then
//    begin
//      raise Exception.Create(pchar('请先选择班组！'));
//    end;

    if (Edit16.Text = '') and (Edit17.Text = '') then
    begin
      MovePanel1.Visible := true;
      SpeedButton14.Enabled := True;
      tishi.Caption := '请先选择班组';
      Timer1.Enabled := true;
      exit;
    end;

    if StrToFloatDef(ROLLNUM.Text, 0) > StrToFloatDef(KYPS.Text, 0) then
    begin
      MovePanel1.Visible := true;
      SpeedButton14.Enabled := True;
      tishi.Caption := '登记匹数超过开卡匹数';
      Timer1.Enabled := true;
      exit;
    end;
    if (CDS_GX.FieldByName('GlideNo').AsString = CDS_GX.FieldByName('maxGlideNo').AsString) and (StrToFloatDef(QTY.Text, 0) = 0) then
    begin
      MovePanel1.Visible := true;
      SpeedButton14.Enabled := True;
      tishi.Caption := '最后一道工序需输入重量';
      Timer1.Enabled := true;
      exit;
    end;

//    if StrToFloatDef(ROLLNUM.Text, 0) > StrToFloatDef(KYPS.Text, 0) then
//    begin
//      Application.MessageBox('登记匹数超过开卡匹数！', '提示信息', 0);
//      SpeedButton14.Enabled := True;
//      Exit;
//    end;
//    if (CDS_GX.FieldByName('GlideNo').AsString = CDS_GX.FieldByName('maxGlideNo').AsString) and (StrToFloatDef(QTY.Text, 0) = 0) then
//    begin
//      Application.MessageBox('最后一道工序需输入重量！', '提示信息', 0);
//      SpeedButton14.Enabled := True;
//      Exit;
//    end;

    with ADO_Cmd do
    begin
      close;
      sql.Clear;
      sql.Add(' select * from Dye_Plan_Glide_OutPut where 1=2 ');
      open;
      Append;
      FieldByName('GOID').Value := fkeyNo;
      FieldByName('PCID').Value := trim(PCID.text);
      FieldByName('GlideNo').Value := trim(CDS_GX.FieldByName('GlideNo').AsString);
      FieldByName('GlideName').Value := trim(CDS_GX.FieldByName('GlideName').AsString);
      if Edit16.Text <> '' then
      begin
        FieldByName('StaffName').Value := trim('甲');
      end;
      if Edit17.Text <> '' then
      begin
        FieldByName('StaffName').Value := trim('乙');
      end;

      FieldByName('Qty').Value := StrToFloatDef(Qty.text, 0);

      FieldByName('RollNum').Value := trim(ROLLNUM.text);
      FieldByName('Filler').Value := trim(DName);
      Post;
    end;
    with ADO_Cmd do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_Dye_Card_upCL @PCID=' + QuotedStr(PCID.Text));

      execsql;
    end;
    if (CDS_GX.FieldByName('GlideNo').AsString = CDS_GX.FieldByName('maxGlideNo').AsString) then
    begin
      with ADO_Cmd do
      begin
        close;
        sql.Clear;
        sql.Add('exec P_Dye_Card_CRCP @PCID=' + QuotedStr(PCID.Text));
        execsql;
      end;
    end;
//////////////////       更新进度       //////////////////
    with ADO_Cmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Dye_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''机台刷卡''    ');
      sql.Add(',' + quotedstr(PCID.text));
      sql.Add(',''PC刷卡''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(fkeyNo));
      sql.Add(')  ');
      execsql;
    end;
//////////////////       更新进度       //////////////////

    Scan.SetFocus;
    SpeedButton14.Enabled := True;
//    Application.MessageBox('刷卡成功！', '提示信息');

    MovePanel1.Visible := true;

    tishi.Caption := '刷卡成功';
    Timer1.Enabled := true;
    Qty.Text := '';
    PCID.TEXT := '';
  except
    Scan.SetFocus;
    SpeedButton14.Enabled := True;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    Exit;
  end;

end;

procedure TfrmDyeOutput.FormShow(Sender: TObject);
begin
  inherited;
  Scan.OnClick(Scan);
  KeyPreview := True;  // 确保表单可以预览按键
end;

end.

