unit U_ClothInfoTechnicsinput;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxPC, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, Menus, U_BaseList,  
  U_BaseInput, dxDateRanges, System.ImageList, Vcl.ImgList;

type
  TfrmDyeTechnicsinput = class(TfrmBaseInput)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    GPM_2: TcxGridPopupMenu;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    ADOQuery1: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    PM_1: TPopupMenu;
    N2: TMenuItem;
    cxSplitter1: TcxSplitter;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2PSName: TcxGridDBColumn;
    v2PSNote: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel1: TPanel;
    Panel3: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    VC_PSNO: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Label5: TLabel;
    Label1: TLabel;
    SYRName: TEdit;
    PSName: TEdit;
    ToolButton2: TToolButton;
    btnSave: TToolButton;
    Panel4: TPanel;
    Label2: TLabel;
    Label3: TLabel;
    CTMName: TEdit;
    CTMNo: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v2Column4PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure PSNameChange(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Tv2DblClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    procedure InitGlide();
    procedure SetXH();
    function SaveData(): boolean;
  public
    FCTMID, PState: string;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmDyeTechnicsinput: TfrmDyeTechnicsinput;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmDyeTechnicsinput.SetXH();
var
  i: Integer;
begin
  with CDS_1 do
  begin
    First;
    i := 1;
    while not Eof do
    begin
      with CDS_1 do
      begin
        Edit;
        FieldByName('XHNoTemp').Value := i;
        Post;
      end;
      i := i + 1;
      Next;
    end;
  end;

  with CDS_1 do
  begin
    CDS_1.IndexFieldNames := '';
    First;
    i := 1;
    while not Eof do
    begin
      with CDS_1 do
      begin
        Edit;
        FieldByName('PSNO').Value := FieldByName('XHNoTemp').AsInteger;
        Post;
      end;
      i := i + 1;
      Next;
    end;
    CDS_1.IndexFieldNames := 'PSNO';
  end;
end;

procedure TfrmDyeTechnicsinput.InitGlide();
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * FROM BS_Cloth_Technics_Main where  CTMID=' + quotedstr(FCTMID));
    Open;
  end;
  SCSHData(ADOQueryTemp, Panel4, 2);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.* FROM BS_Cloth_Technics_Sub A inner join Bs_Cloth_Process B on A.PSId=B.PSId ');
    sql.Add(' Where A.CTMID=''' + FCTMID + '''');
    SQL.Add(' ORDER BY A.PSNO');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);

end;

function TfrmDyeTechnicsinput.SaveData(): boolean;
var
  CTMPSlist, maxno: string;
begin
  result := false;
  ADOQueryCmd.Connection.BeginTrans;
  try

    if Trim(FCTMID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxno, 'GY', 'BS_Cloth_Technics_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败！');
    end
    else
    begin
      maxno := FCTMID;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete BS_Cloth_Technics_Sub where CTMID=' + quotedstr(trim(maxno)));
      execsql;
    end;

    CTMPSlist := '';
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('SELECT * FROM BS_Cloth_Technics_Sub WHERE 1=2');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('PSNO').Value := Trim(CDS_1.fieldbyname('PSNO').AsString);
          FieldByName('CTMID').Value := Trim(maxno);
          FieldByName('PSId').Value := Trim(CDS_1.fieldbyname('PSId').AsString);
          Post;
        end;
        if CTMPSlist = '' then
        begin
          CTMPSlist := Trim(CDS_1.fieldbyname('PSNO').AsString) + '.' + Trim(CDS_1.fieldbyname('PSName').AsString);
        end
        else
        begin
          CTMPSlist := CTMPSlist + '+' + Trim(CDS_1.fieldbyname('PSNO').AsString) + '.' + Trim(CDS_1.fieldbyname('PSName').AsString);
        end;
        Next;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Cloth_Technics_Main where CTMID =' + quotedstr(Trim(maxno)));
      Open;
    end;
    with ADOQueryCmd do
    begin
      if ADOQueryCmd.IsEmpty then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('CTMID').Value := Trim(maxno);
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      RTSetsavedata(ADOQueryCmd, 'BS_Cloth_Technics_Main', Panel4, 2);
      FieldByName('CTMPSlist').Value := Trim(CTMPSlist);

      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Cloth_Technics_Main where CTMNo=''' + Trim(CTMNo.Text) + '''');
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
      raise Exception.Create('工艺编号重复!');

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Cloth_Technics_Main where CTMName=''' + Trim(CTMName.Text) + '''');
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
      raise Exception.Create('工艺名称重复!');

    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    result := false;
  end;
end;

procedure TfrmDyeTechnicsinput.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select *,PSXH=0 from Bs_Cloth_Process ');
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_2);
    SInitCDSData(ADOQueryMain, CDS_2);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmDyeTechnicsinput.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyeTechnicsinput := nil;
end;

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

procedure TfrmDyeTechnicsinput.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmDyeTechnicsinput.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '工艺管理');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '工艺管理');
  InitGrid();
  InitGlide();
end;

procedure TfrmDyeTechnicsinput.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmDyeTechnicsinput.ToolButton2Click(Sender: TObject);
begin
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '工艺管理');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '工艺管理');

end;

procedure TfrmDyeTechnicsinput.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmDyeTechnicsinput.v2Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
    mvalue := '';
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BS_Cloth_Technics_Main where CTMNo =' + quotedstr(Trim(mvalue)));
    Open;
  end;
  if ADOQueryCmd.IsEmpty = false then
  begin
    application.MessageBox('已存在工艺编号', '提示');
    CDS_2.Edit;
    CDS_2.FieldByName('CTMNo').AsString := '';
    exit;
  end;
  with CDS_2 do
  begin
    Edit;
    FieldByName('CTMNo').Value := Trim(mvalue);
    //Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update BS_Cloth_Technics_Main set CTMNo=''' + trim(mvalue) + ''' ');
    sql.Add(',Editer=''' + trim(DName) + '''');
    sql.Add(',Edittime=getdate()');
    sql.add(' where CTMID=''' + trim(CDS_2.fieldbyname('CTMID').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmDyeTechnicsinput.btnSaveClick(Sender: TObject);
begin
  if trim(CTMNo.Text) = '' then
  begin
    application.MessageBox('工艺编号不能为空', '提示');
    Exit;
  end;
  if trim(CTMName.Text) = '' then
  begin
    application.MessageBox('工艺名称不能为空', '提示');
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
  end;
end;

procedure TfrmDyeTechnicsinput.v2Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
    mvalue := '';
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BS_Cloth_Technics_Main where CTMName =' + quotedstr(Trim(mvalue)));
    Open;
  end;
  if ADOQueryCmd.IsEmpty = false then
  begin
    application.MessageBox('已存在工艺名称', '提示');
    CDS_2.Edit;
    CDS_2.FieldByName('CTMName').AsString := '';
    exit;
  end;
  with CDS_2 do
  begin
    Edit;
    FieldByName('CTMName').Value := Trim(mvalue);
    //Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update BS_Cloth_Technics_Main set CTMName=''' + trim(mvalue) + ''' ');
    sql.Add(',Editer=''' + trim(DName) + '''');
    sql.Add(',Edittime=getdate()');
    sql.add(' where CTMID=''' + trim(CDS_2.fieldbyname('CTMID').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmDyeTechnicsinput.Tv2DblClick(Sender: TObject);
begin
  with CDS_1 do
  begin
    Append;
    FieldByName('PSNO').Value := CDS_1.RecordCount + 1;
    FieldByName('PSName').Value := CDS_2.fieldbyname('PSName').AsString;
    FieldByName('PSId').Value := CDS_2.fieldbyname('PSId').AsString;
    post;
  end;

end;

procedure TfrmDyeTechnicsinput.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitGlide();
end;

procedure TfrmDyeTechnicsinput.FormCreate(Sender: TObject);
begin
  inherited;
  fmanage := Trim(DParameters1);
end;

procedure TfrmDyeTechnicsinput.N2Click(Sender: TObject);
var
  i: Integer;
  CTMPSlist: string;
begin
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  CDS_1.Delete;
  SetXH();
end;

procedure TfrmDyeTechnicsinput.PSNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_2);
    SInitCDSData(ADOQueryMain, CDS_2);
  end;
end;

end.

