unit U_GXGL_Input;

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;

type
  TfrmGXGL_Input = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: 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;
    v2PSSH: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: 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 v2PSNotePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function  SaveData():Boolean;

  public
    { Public declarations }
    RKFlag,FCYID:String;
    fDEFstr5:string;
  end;

var
  frmGXGL_Input: TfrmGXGL_Input;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}



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


procedure TfrmGXGL_Input.FormDestroy(Sender: TObject);
begin
  frmGXGL_Input:=nil;
end;

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

procedure TfrmGXGL_Input.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('工序录入',Tv2,'客户自定义');
  Close;
end;
procedure TfrmGXGL_Input.FormShow(Sender: TObject);
begin
  ReadCxGrid('工序录入',Tv2,'客户自定义');
  InitGrid();
end;

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

function TfrmGXGL_Input.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','JYOrder_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 JYOrder_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,'',0);
          FieldByName('LDPrice').Value:=CDS_HZ.fieldbyname('LDPrice').AsFloat;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_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 TfrmGXGL_Input.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 TfrmGXGL_Input.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('SYRName').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally;
    frmZDYHelp.Free;
  end;
end;

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

procedure TfrmGXGL_Input.v2PSNotePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PSNote';
      flagname:='注意事项/品质说明';
      if ShowModal=1 then
      begin
        with CDS_HZ do
        begin
          Edit;
          FieldByName('PSNote').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmGXGL_Input.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 JYOrder_Process where PSId='''+trim(CDS_HZ.fieldbyname('PSID').AsString)+'''');
    ExecSQL;
  end;
  CDS_HZ.Delete;
end;

end.
