unit U_DyeProcessInput;

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, cxDropDownEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu;

type
  TfrmDyeProcessInput = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    GPM_2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2PSName: TcxGridDBColumn;
    v2PSNote: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2SYRName: TcxGridDBColumn;
    vLDPrice: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure v2SYRNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
  public
    { Public declarations }
    RKFlag, FCYID: string;
    fDEFstr5: string;
  end;

var
  frmDyeProcessInput: TfrmDyeProcessInput;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmDyeProcessInput.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from Dye_Process where PSID=''' + Trim(FCYID) + '''');
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmDyeProcessInput.FormDestroy(Sender: TObject);
begin
  frmDyeProcessInput := nil;
end;

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

procedure TfrmDyeProcessInput.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmDyeProcessInput.FormShow(Sender: TObject);
begin
  ReadCxGrid(trim(Self.Caption), Tv2, 'Ⱦɫ�ƻ�');
  InitGrid();
end;

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

function TfrmDyeProcessInput.SaveData(): Boolean;
var
  maxId, CRID, fzdyCode, FFFlag, FStr: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      first;
      while not Eof do
      begin
        if Trim(fieldbyname('PSID').AsString) = '' then
        begin
          if GetLSNo(ADOQueryCmd, maxId, 'JP', 'Dye_Process', 4, 1) = False then
          begin
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('ȡ����ʧ�ܣ�', '��ʾ', 0);
            Exit;
          end;
        end
        else
        begin
          maxId := Trim(FCYID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Dye_Process where PSID=''' + Trim(FCYID) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FCYID) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('PSID').Value := Trim(maxId);
          RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_HZ, 'Dye_Process', 0);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Dye_Process where PSName=''' + Trim(CDS_HZ.fieldbyname('PSName').AsString) + '''');
          Open;
        end;
        if ADOQueryCmd.RecordCount > 1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          application.MessageBox(PChar('��������' + trim(ADOQueryCmd.fieldbyname('PSName').AsString) + '�Ѵ���'), '��ʾ');
          Exit;
        end;
        with CDS_HZ do
        begin
          Edit;
          FieldByName('PSID').Value := Trim(maxId);
        end;
        next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ;
    Result := True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('�����쳣!', '��ʾ', 0);
  end;
end;

procedure TfrmDyeProcessInput.ToolButton3Click(Sender: TObject);
begin
  cxGrid2.SetFocus;
  if Trim(CDS_HZ.FieldByName('PSName').AsString) = '' then
  begin
    Application.MessageBox('�������Ʋ���Ϊ��!', '��ʾ', 0);
    Exit;
  end;
  if Application.MessageBox('ȷ��Ҫ����������', '��ʾ', 32 + 4) <> IDYES then
    Exit;

  if SaveData() then
  begin
    Application.MessageBox('����ɹ�!', '��ʾ', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmDyeProcessInput.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption), Tv2, 'Ⱦɫ�ƻ�');
end;

procedure TfrmDyeProcessInput.v2SYRNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'CJName';
      flagname := '��������';
      if ShowModal = 1 then
      begin
        with CDS_HZ do
        begin
          Edit;
          FieldByName('Workshop').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    ;
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDyeProcessInput.ToolButton1Click(Sender: TObject);
begin
  with CDS_HZ do
  begin
    Append;
    post;
  end;
end;

procedure TfrmDyeProcessInput.ToolButton2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  if application.MessageBox('ȷ��ɾ����', '��ʾ', 1) = 2 then
    exit;
  with ADOQueryCmd do
  begin
    close;
    SQL.Clear;
    sql.Add('delete from Dye_Process where PSId=''' + trim(CDS_HZ.fieldbyname('PSID').AsString) + '''');
    ExecSQL;
  end;
  CDS_HZ.Delete;
end;

end.