unit U_TatInspEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxTextEdit, DBClient, ADODB, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, StdCtrls, ExtCtrls, ComCtrls, ToolWin,
  cxButtonEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, dxScrollbarAnnotations, cxContainer,
  cxMaskEdit;

type
  TfrmTatInspEdit = class(TForm)
    ToolBar1: TToolBar;
    ToolButton2: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    CIID: TEdit;
    ADOTmp: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_MJ: TClientDataSet;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    Label2: TLabel;
    GrossWeight: TEdit;
    Label4: TLabel;
    Label6: TLabel;
    Meter: TEdit;
    RadioGroup1: TRadioGroup;
    Ma: TLabel;
    Label7: TLabel;
    MJQty1: TEdit;
    Label5: TLabel;
    Label10: TLabel;
    Label12: TLabel;
    Label14: TLabel;
    NetWeight: TEdit;
    PieceNo: TEdit;
    Label15: TLabel;
    Label11: TLabel;
    Yardage: TEdit;
    ToolButton4: TToolButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    TV1Column1: TcxGridDBColumn;
    Label3: TLabel;
    Label34: TLabel;
    Label37: TLabel;
    Label39: TLabel;
    SCPerson: TcxButtonEdit;
    JBSCPerson: TcxButtonEdit;
    JBSCPerson3: TcxButtonEdit;
    JBSCPerson4: TcxButtonEdit;
    Label36: TLabel;
    Label38: TLabel;
    Label40: TLabel;
    Label41: TLabel;
    OutPutQty1: TEdit;
    OutPutQty2: TEdit;
    OutPutQty3: TEdit;
    OutPutQty4: TEdit;
    Label8: TLabel;
    InputLen: TEdit;
    Label9: TLabel;
    carno: TEdit;
    xbdate: TDateTimePicker;
    xbdate2: TDateTimePicker;
    xbdate3: TDateTimePicker;
    xbdate4: TDateTimePicker;
    Label13: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure tv2CDWZPropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxGridDBColumn3PropertiesEditValueChanged(Sender: TObject);
    procedure TV1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SCPersonPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure JBSCPersonPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure JBSCPerson3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure JBSCPerson4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveData(): Boolean;
    procedure GetJYXK(FJYIP: string);
  public
    { Public declarations }
  end;

var
  frmTatInspEdit: TfrmTatInspEdit;

implementation

uses
  U_RTFun, U_ZDYHelp, U_DataLink, U_EmployeeSel;

{$R *.dfm}
procedure TfrmTatInspEdit.GetJYXK(FJYIP: string);
begin
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* from Tat_Cloth_Inspect A  ');
    sql.Add(' where A.CIID=''' + Trim(FJYIP) + '''');
    Open;
  end;
  if ADOTmp.IsEmpty then
  begin
    CIID.Text := '';
    Label2.Caption := '';
    Label2.Visible := False;
    Application.MessageBox('条码错误!', '提示', 0);
    Exit;
  end;
  PieceNo.Text := Trim(ADOTmp.fieldbyname('PieceNo').AsString);
  carno.Text := Trim(ADOTmp.fieldbyname('carno').AsString);
  CIID.Text := Trim(ADOTmp.fieldbyname('CIID').AsString);

  GrossWeight.Text := Trim(ADOTmp.fieldbyname('GrossWeight').AsString);
  NetWeight.Text := Trim(ADOTmp.fieldbyname('NetWeight').AsString);
  Meter.Text := Trim(ADOTmp.fieldbyname('Meter').AsString);
  Yardage.Text := Trim(ADOTmp.fieldbyname('Yardage').AsString);
  InputLen.Text := Trim(ADOTmp.fieldbyname('InputLen').AsString);

  SCPerson.Text := Trim(ADOTmp.fieldbyname('SCPerson').AsString);
  JBSCPerson.Text := Trim(ADOTmp.fieldbyname('JBSCPerson').AsString);
  JBSCPerson3.Text := Trim(ADOTmp.fieldbyname('JBSCPerson3').AsString);
  JBSCPerson4.Text := Trim(ADOTmp.fieldbyname('JBSCPerson4').AsString);

  OutPutQty1.Text := Trim(ADOTmp.fieldbyname('OutPutQty1').AsString);
  OutPutQty2.Text := Trim(ADOTmp.fieldbyname('OutPutQty2').AsString);
  OutPutQty3.Text := Trim(ADOTmp.fieldbyname('OutPutQty3').AsString);
  OutPutQty4.Text := Trim(ADOTmp.fieldbyname('OutPutQty4').AsString);

  begin
    if ADOTmp.fieldbyname('xbdate').isnull then
    begin
      xbdate.date := date;
    end
    else
    begin
      xbdate.date := ADOTmp.fieldbyname('xbdate').AsDateTime;
    end;

    if ADOTmp.fieldbyname('xbdate2').isnull then
    begin
      xbdate2.date := date;
    end
    else
    begin
      xbdate2.date := ADOTmp.fieldbyname('xbdate2').AsDateTime;
    end;

    if ADOTmp.fieldbyname('xbdate3').isnull then
    begin
      xbdate3.date := date;
    end
    else
    begin
      xbdate3.date := ADOTmp.fieldbyname('xbdate3').AsDateTime;
    end;

    if ADOTmp.fieldbyname('xbdate4').isnull then
    begin
      xbdate4.date := date;
    end
    else
    begin
      xbdate4.date := ADOTmp.fieldbyname('xbdate4').AsDateTime;
    end;
  end;

  if Trim(Trim(ADOTmp.fieldbyname('Grade').AsString)) = '正品' then
  begin
    RadioGroup1.ItemIndex := 0
  end
  else if Trim(Trim(ADOTmp.fieldbyname('Grade').AsString)) = '次品' then
  begin
    RadioGroup1.ItemIndex := 1;
  end;
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from Tat_Cloth_Inspect_Flaw A   where A.CIID=''' + Trim(CIID.Text) + '''');
    Open;
  end;
  Label2.Caption := Trim(CIID.Text);
  Label2.Visible := True;
  CIID.Text := '';
  SCreateCDS(ADOTmp, Order_MJ);
  SInitCDSData(ADOTmp, Order_MJ);

end;

procedure TfrmTatInspEdit.JBSCPerson3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmEmployeeSel := TfrmEmployeeSel.Create(self);
    with frmEmployeeSel do
    begin
      FPost := '挡车工';
      if ShowModal = 1 then
      begin
        JBSCPerson3.Text := frmEmployeeSel.FRName;
        JBSCPerson3.Hint := frmEmployeeSel.FRCode;
      end;
    end;
  finally
    frmEmployeeSel.Free;
  end;
end;

procedure TfrmTatInspEdit.JBSCPerson4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmEmployeeSel := TfrmEmployeeSel.Create(self);
    with frmEmployeeSel do
    begin
      FPost := '挡车工';
      if ShowModal = 1 then
      begin
        JBSCPerson4.Text := frmEmployeeSel.FRName;
        JBSCPerson4.Hint := frmEmployeeSel.FRCode;
      end;
    end;
  finally
    frmEmployeeSel.Free;
  end;
end;

procedure TfrmTatInspEdit.JBSCPersonPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmEmployeeSel := TfrmEmployeeSel.Create(self);
    with frmEmployeeSel do
    begin
      FPost := '挡车工';
      if ShowModal = 1 then
      begin
        JBSCPerson.Text := frmEmployeeSel.FRName;
        JBSCPerson.Hint := frmEmployeeSel.FRCode;
      end;
    end;
  finally
    frmEmployeeSel.Free;
  end;
end;

procedure TfrmTatInspEdit.cxGridDBColumn3PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, CIFbeg, CIFEnd: string;
  FFReal: Double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  with Order_MJ do
  begin
    Edit;
    FieldByName('CIFEnd').Value := mvalue;
    Post;
  end;
  CIFbeg := Trim(Order_MJ.fieldbyname('CIFbeg').AsString);
  CIFEnd := Trim(Order_MJ.fieldbyname('CIFEnd').AsString);
  if (Trim(CIFbeg) <> '') and (Trim(CIFEnd) <> '') then
  begin
    if TryStrToFloat(CIFbeg, FFReal) and TryStrToFloat(CIFbeg, FFReal) then
    begin
      if StrToFloat(CIFEnd) < StrToFloat(CIFbeg) then
      begin
        Application.MessageBox('终止位置小于起始位置!', '提示', 0);
        Exit;
      end;
      with Order_MJ do
      begin
        Edit;
        FieldByName('CDQty').Value := StrToFloat(CIFEnd) - StrToFloat(CIFbeg);
        Post;
      end;
    end
    else
    begin
      Application.MessageBox('非法数字!', '提示', 0);
      Exit;
    end;
  end;
end;

procedure TfrmTatInspEdit.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmTatInspEdit.FormDestroy(Sender: TObject);
begin
  frmTatInspEdit := nil;
end;

procedure TfrmTatInspEdit.tv2CDWZPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, CIFbeg, CIFEnd: string;
  FFReal: Double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  with Order_MJ do
  begin
    Edit;
    FieldByName('CIFbeg').Value := mvalue;
    Post;
  end;
  CIFbeg := Trim(Order_MJ.fieldbyname('CIFbeg').AsString);
  CIFEnd := Trim(Order_MJ.fieldbyname('CIFEnd').AsString);
  if (Trim(CIFbeg) <> '') and (Trim(CIFEnd) <> '') then
  begin
    if TryStrToFloat(CIFbeg, FFReal) and TryStrToFloat(CIFbeg, FFReal) then
    begin
      if StrToFloat(CIFEnd) < StrToFloat(CIFbeg) then
      begin
        Application.MessageBox('终止位置小于起始位置!', '提示', 0);
        Exit;
      end;
      with Order_MJ do
      begin
        Edit;
        FieldByName('CIFQty').Value := StrToFloat(CIFEnd) - StrToFloat(CIFbeg);
        Post;
      end;
    end
    else
    begin
      Application.MessageBox('非法数字!', '提示', 0);
      Exit;
    end;
  end;
end;

procedure TfrmTatInspEdit.ToolButton1Click(Sender: TObject);
begin
  if Trim(Label2.Caption) = '' then
    Exit;
  Order_MJ.Append;
  Order_MJ.Post;
end;

procedure TfrmTatInspEdit.ToolButton3Click(Sender: TObject);
begin
  if Trim(Label2.Caption) = '' then
    Exit;
  if Order_MJ.IsEmpty then
    Exit;
  if Trim(Order_MJ.fieldbyname('CIFID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Tat_Cloth_Inspect_Flaw where CIFID=''' + Trim(Order_MJ.fieldbyname('CIFID').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_MJ.Delete;
end;

procedure TfrmTatInspEdit.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption), Tv1, '印染检验管理');
end;

procedure TfrmTatInspEdit.TV1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'WFBCD';
      flagname := '疵点';
      if ShowModal = 1 then
      begin
        Self.Order_MJ.Edit;
        Self.Order_MJ.FieldByName('CIFName').value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

function TfrmTatInspEdit.SaveData(): Boolean;
var
  maxno: string;
  FFreal: Double;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存疵点表
    with Order_MJ do
    begin
      First;
      while not Eof do
      begin
        if trim(Order_MJ.FieldByName('CIFName').AsString) = '' then
          raise Exception.Create(pchar('疵点名称不能为空！'));

        if Trim(Order_MJ.fieldbyname('CIFID').AsString) = '' then
        begin
          if GetLSNo(ADOTmp, maxno, 'JC', 'Tat_Cloth_Inspect_Flaw', 5, 1) = False then
            raise Exception.Create(pchar('取子流水号失败！'));
        end
        else
        begin
          maxno := Trim(Order_MJ.fieldbyname('CIFID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add(' select * from Tat_Cloth_Inspect_Flaw ');
          sql.Add(' where CIFID=''' + Trim(maxno) + ''' ');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_MJ.fieldbyname('CIFID').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('CIID').Value := Trim(Label2.Caption);
          FieldByName('CIFID').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_MJ, 'Tat_Cloth_Inspect_Flaw', 0);
          Post;
        end;

        Order_MJ.Edit;
        Order_MJ.FieldByName('CIFID').Value := Trim(maxno);
        Order_MJ.FieldByName('CIID').Value := Trim(Label2.Caption);
        Next;
      end;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' UPdate Tat_Cloth_Inspect  Set  ');
      sql.Add(' PieceNo=''' + Trim(PieceNo.Text) + ''' ');
      sql.Add(' ,carno=''' + Trim(carno.Text) + ''' ');

      sql.Add(' ,SCPerson=''' + Trim(SCPerson.Text) + ''' ');
      sql.Add(' ,JBSCPerson=''' + Trim(JBSCPerson.Text) + ''' ');
      sql.Add(' ,JBSCPerson3=''' + Trim(JBSCPerson3.Text) + ''' ');
      sql.Add(' ,JBSCPerson4=''' + Trim(JBSCPerson4.Text) + ''' ');
      sql.Add(',GrossWeight=' + FloatToStr(Strtofloatdef(GrossWeight.Text, 0)));
      sql.Add(',NetWeight=' + FloatToStr(Strtofloatdef(NetWeight.Text, 0)));
      sql.Add(',Meter=' + FloatToStr(Strtofloatdef(Meter.Text, 0)));
      sql.Add(',Yardage=' + FloatToStr(Strtofloatdef(Yardage.Text, 0)));
      sql.Add(',InputLen=' + FloatToStr(Strtofloatdef(InputLen.Text, 0)));

      sql.Add(',OutPutQty1=' + FloatToStr(Strtofloatdef(OutPutQty1.Text, 0)));
      sql.Add(',OutPutQty2=' + FloatToStr(Strtofloatdef(OutPutQty2.Text, 0)));
      sql.Add(',OutPutQty3=' + FloatToStr(Strtofloatdef(OutPutQty3.Text, 0)));
      sql.Add(',OutPutQty4=' + FloatToStr(Strtofloatdef(OutPutQty4.Text, 0)));

      sql.Add(',xbdate=''' + datetostr(xbdate.date) + ''' ');
      sql.Add(',xbdate2=''' + datetostr(xbdate2.date) + ''' ');
      sql.Add(',xbdate3=''' + datetostr(xbdate3.date) + ''' ');
      sql.Add(',xbdate4=''' + datetostr(xbdate4.date) + ''' ');
      sql.Add(' where CIID=''' + Trim(Label2.CAPTION) + '''');
//      ShowMessage(SQL.TEXT);
      ExecSQL;
    end;

//////////////////       更新卷号疵点信息       //////////////////
///
//////////////////       出入库及库存       //////////////////
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Tat_Insp_In_Stk @CIID=''' + Trim(Label2.Caption) + '''');
      execsql;
    end;
//////////////////       出入库及库存       //////////////////
///
//////////////////       更新进度       //////////////////
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Tat_Need_Up(UOperation,UType,UDataId) values(''检验'',''检验编辑'',' + quotedstr(Trim(Label2.Caption)) + ')  ');
      execsql;
    end;
//////////////////       更新进度       //////////////////

    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTatInspEdit.SCPersonPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmEmployeeSel := TfrmEmployeeSel.Create(self);
    with frmEmployeeSel do
    begin
      FPost := '挡车工';
      if ShowModal = 1 then
      begin
        SCPerson.Text := frmEmployeeSel.FRName;
        SCPerson.Hint := frmEmployeeSel.FRCode;
      end;
    end;
  finally
    frmEmployeeSel.Free;
  end;
end;

procedure TfrmTatInspEdit.ToolButton2Click(Sender: TObject);
begin
  if Trim(Label2.Caption) = '' then
    Exit;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    Exit;
  end;
end;

procedure TfrmTatInspEdit.TBCloseClick(Sender: TObject);
begin
  Close;

end;

procedure TfrmTatInspEdit.FormShow(Sender: TObject);
begin
  ReadCxGrid(trim(Self.Caption), Tv1, '印染检验管理');
  if CIID.Text <> '' then
  begin
    GetJYXK(CIID.Text);
  end;

end;

end.

