unit U_ZDYCPName;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ToolWin, ComCtrls,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGrid, DBClient, ADODB, ImgList,
  StdCtrls, ExtCtrls, cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxTimeEdit, cxDropDownEdit;

type
  TfrmZDYCPName = class(TForm)
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    V1Code: TcxGridDBColumn;
    V1Name: TcxGridDBColumn;
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    TBAdd: TToolButton;
    TBSave: TToolButton;
    TBEdit: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    ZDYName: TEdit;
    cxGridPopupMenu1: TcxGridPopupMenu;
    V1ZdyFlag: TcxGridDBColumn;
    V1HelpType: TcxGridDBColumn;
    V1ZdyStr1: TcxGridDBColumn;
    V1ZdyStr2: TcxGridDBColumn;
    V1ZdyStr3: TcxGridDBColumn;
    V1ZdyStr4: TcxGridDBColumn;
    V1ZdyStr5: TcxGridDBColumn;
    V1CYMName: TcxGridDBColumn;
    Label2: TLabel;
    ZDYCYName: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TV1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ZDYNameChange(Sender: TObject);
    procedure V1NamePropertiesEditValueChanged(Sender: TObject);
    procedure V1OrderNoPropertiesEditValueChanged(Sender: TObject);
    procedure V1NotePropertiesEditValueChanged(Sender: TObject);
    procedure V1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure V1HelpTypePropertiesEditValueChanged(Sender: TObject);
    procedure V1ZdyStr1PropertiesEditValueChanged(Sender: TObject);
    procedure ZDYCYNameChange(Sender: TObject);
    procedure V1CYMNamePropertiesEditValueChanged(Sender: TObject);
  private
    procedure InitGrid();
    procedure initCom(FZDYNO:string);
    { Private declarations }
  public
    flag,flagname,snote,MainType:string;
    fnote,forderno,fZdyFlag,ViewFlag:Boolean;
    PPSTE:integer;
    { Public declarations }
  end;

var
  frmZDYCPName: TfrmZDYCPName;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}

procedure TfrmZDYCPName.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  ZDYName.SetFocus;
  WriteCxGrid('自定义'+Trim(flag),TV1,'自定义数据');
  Action:=caFree;
end;

procedure TfrmZDYCPName.initCom(FZDYNO:string);
begin
  with ADOQueryTemp do
  begin
    close;
    sql.clear;
    sql.Add('select ZDYName from KH_ZDY_LS ');
    sql.add('where ZDYNo='''+trim(FZDYNO)+'''');
    Open;
    if isEmpty then
    begin
      exit;
    end;
    (V1CYMName.Properties as TcxComboBoxProperties).Items.Clear;
    while not EOF do
    begin
      (V1CYMName.Properties as TcxComboBoxProperties).Items.Add(trim(fieldByName('ZDYName').AsString));
      next;
    end;
  end;
end;

procedure TfrmZDYCPName.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,ZJM=dbo.getPinYin(A.ZdyName)');
      sql.Add(' from KH_ZDY A where A.Type='''+flag+'''');
      if Trim(MainType)<>'' then
      begin
        sql.Add(' and A.MainType='''+Trim(MainType)+'''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while not eof do
      begin
        InitCom(fieldbyname('ZDYName').AsString);
        Next;
      end;
    end;
    ClientDataSet1.EnableControls;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmZDYCPName.TBAddClick(Sender: TObject);
var
  i:Integer;
begin
  ZDYName.SetFocus;
  TV1.OptionsData.Editing:=True;
  TV1.OptionsSelection.CellSelect:=True;
  with ClientDataSet1 do
  begin
    Append;
    Post;
  end;
end;
procedure TfrmZDYCPName.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  Application.MessageBox('保存成功！','提示',0);
end;

procedure TfrmZDYCPName.TBDelClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if (Trim(ClientDataSet1.FieldByName('ZDYNo').AsString)<>'') or
     (Trim(ClientDataSet1.FieldByName('ZDYname').AsString)<>'') then
  begin
    if application.MessageBox('确定要删除吗?','提示信息',1)=2 then exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete KH_ZDY where ZDYNo='''+Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString)+'''');
      SQL.Add(' and Type='''+Trim(flag)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet1.Delete;
end;

procedure TfrmZDYCPName.TBCloseClick(Sender: TObject);
begin
  ModalResult:=2;
  ZDYName.SetFocus;
  WriteCxGrid('自定义'+Trim(flag),TV1,'自定义数据');
  Close;
end;

procedure TfrmZDYCPName.FormShow(Sender: TObject);
var
  fsj,fsj1:string;
begin
  InitGrid();
  fsj:=Trim(flag)+'01';
  fsj1:=Trim(flagname)+'01';
  ReadCxGrid('自定义'+Trim(flag),TV1,'自定义数据');
  if ViewFlag=True then
  begin
    TBAdd.Visible:=False;
    TBSave.Visible:=False;
    TBEdit.Visible:=False;
  end;
end;

procedure TfrmZDYCPName.ToolButton1Click(Sender: TObject);
begin
  ZDYName.SetFocus;
  WriteCxGrid('自定义'+Trim(flag),TV1,'自定义数据');
  ModalResult:=1;
end;

procedure TfrmZDYCPName.TBEditClick(Sender: TObject);
begin
  TV1.OptionsData.Editing:=True;
  TV1.OptionsSelection.CellSelect:=True;
end;

procedure TfrmZDYCPName.TV1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if TV1.OptionsData.Editing=False then
  begin
    ModalResult:=1;
  end;
end;

procedure TfrmZDYCPName.ZDYNameChange(Sender: TObject);
var
  fsj:String;
begin
  if Trim(ZDYName.Text)<>'' then
  begin
    fsj:=' ZDYName like '''+'%'+Trim(ZDYName.Text)+'%'+'''';
  end;
  if ADOQueryMain.Active then
  begin
   // SDofilter(ADOQueryMain,fsj);
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        if Trim(fsj)='' then
        begin
          Filtered:=False;
        end else
        begin
          Filtered:=False;
          Filter:=fsj;
          Filtered:=True;
        end;
      end;
    finally
      ADOQueryMain.EnableControls;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  end;
end;

procedure TfrmZDYCPName.V1NamePropertiesEditValueChanged(Sender: TObject);
var
  maxno,mvalue,mxmaxno,FPM:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    Exit;
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('ZdyName').Value:=Trim(mvalue);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where ZdyName='''+trim(mvalue)+'''');
    sql.Add(' and Type='''+trim(flag)+'''');
    if Trim(MainType)<>'' then
    sql.Add(' and MainType='''+trim(MainType)+'''');
    Open;
  end;
  if ADOQueryCmd.IsEmpty=false then
  begin
    Application.MessageBox('名称重复','提示');
    exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where ZDYCYName like '''+'%'+trim(mvalue)+'%'+'''');
    sql.Add(' and Type='''+trim(flag)+'''');
    if Trim(MainType)<>'' then
    sql.Add(' and MainType='''+trim(MainType)+'''');
    Open;
  end;
  if ADOQueryCmd.IsEmpty=false then
  begin
    Application.MessageBox('名称已存在曾用名中','提示');
    exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(ClientDataSet1.FieldByName('ZDYNO').AsString)='' then
    begin
      if GetLSNo(ADOQueryTemp,maxno,'B','TBSubID',4,0)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大编号失败！','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      if GetLSNo(ADOQueryTemp,mxmaxno,'LS','TBSubID',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大编号失败！','提示',0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from KH_ZDY where ZdyNo='''+trim(maxno)+'''');
        Open;
      end;
      FPM:=Trim(ADOQueryCmd.fieldbyname('ZDYname').AsString);
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into KH_ZDY_LS (LSID,ZDYName,LSTime,LSName) values');
        sql.Add(' ('''+trim(mxmaxno)+''','''+trim(FPM)+''',getdate(),'''+trim(DName)+''')');
        ExecSQL;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update KH_ZDY set ZDYCYName=dbo.F_Get_Order_SubStr(ZdyNo,''LSCodeName'') ');
      sql.Add('where ZDYNo='''+trim(maxno)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_ZDY where ZdyNo='''+trim(maxno)+'''');
      Open;
    end;
    if ADOQueryCmd.IsEmpty then
      ADOQueryCmd.Append
    else
      ADOQueryCmd.Edit;
    ADOQueryCmd.FieldByName('ZDYNo').Value:=Trim(maxno);
    ADOQueryCmd.FieldByName('ZDYName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
    ADOQueryCmd.FieldByName('note').Value:=Trim(snote);
    ADOQueryCmd.FieldByName('Type').Value:=flag;
    ADOQueryCmd.FieldByName('valid').Value:='Y';
    if Trim(MainType)<>'' then
      ADOQueryCmd.FieldByName('MainType').Value:=Trim(MainType);
    ADOQueryCmd.Post;
    ClientDataSet1.Edit;
    ClientDataSet1.FieldByName('ZDYNo').Value:=Trim(maxno);
    ClientDataSet1.FieldByName('ZDYCYName').Value:=Trim(ADOQueryCmd.fieldbyname('ZDYCYName').AsString);
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update CK_MYSC_CR set CYName=dbo.F_Get_Order_SubStr(MYCode,''LSCodeName'')');
      sql.Add(',MYName='''+trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+''' ');
      sql.Add('where MYCode='''+trim(maxno)+'''');
      sql.Add(' and isnull(CRType,'''')=''期初入库''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmZDYCPName.V1OrderNoPropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  if Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)='' then
  begin
    Application.MessageBox('名称不能为空!','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('OrderNo').Value:=mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Zdy Set OrderNo='+mvalue);
    sql.Add(' where ZdyNo='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmZDYCPName.V1NotePropertiesEditValueChanged(Sender: TObject);
var
  mvalue:string;
begin
  if Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)='' then
  begin
    Application.MessageBox('名称不能为空!','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('Note').Value:=mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Zdy Set Note='''+Trim(mvalue)+'''');
    sql.Add(' where ZdyNo='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmZDYCPName.V1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue:String;
begin
  if Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)='' then
  begin
    Application.MessageBox('名称不能为空!','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('ZdyFlag').Value:=StrToInt(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Zdy Set ZdyFlag='+Trim(mvalue));
    sql.Add(' where ZdyNo='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmZDYCPName.V1HelpTypePropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  if Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)='' then
  begin
    Application.MessageBox('名称不能为空!','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('HelpType').Value:=mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Zdy Set HelpType='''+Trim(mvalue)+'''');
    sql.Add(' where ZdyNo='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmZDYCPName.V1ZdyStr1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FFieldName:string;
begin
  if Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)='' then
  begin
    Application.MessageBox('名称不能为空!','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=TV1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if Trim(mvalue)='' then
  begin
    mvalue:='';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName(FFieldName).Value:=mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Zdy Set '+FFieldName+'='''+Trim(mvalue)+'''');
    sql.Add(' where ZdyNo='''+Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString)+'''');
    ExecSQL;
  end;
end;


procedure TfrmZDYCPName.ZDYCYNameChange(Sender: TObject);
var
  fsj:String;
begin
  if Trim(ZDYCYName.Text)<>'' then
  begin
    fsj:=' ZDYCYName like '''+'%'+Trim(ZDYCYName.Text)+'%'+'''';
  end;
  if ADOQueryMain.Active then
  begin
   // SDofilter(ADOQueryMain,fsj);
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        if Trim(fsj)='' then
        begin
          Filtered:=False;
        end else
        begin
          Filtered:=False;
          Filter:=fsj;
          Filtered:=True;
        end;
      end;
    finally
      ADOQueryMain.EnableControls;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  end;
end;

procedure TfrmZDYCPName.V1CYMNamePropertiesEditValueChanged(
  Sender: TObject);
var
  maxno,mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('ZDYCYName').Value:=Trim(mvalue);
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update KH_ZDY set ZDYCYName='''+trim(mvalue)+'''');
      sql.Add('where ZDYNo='''+trim(ClientDataSet1.fieldbyname('ZDYNo').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

end.
