unit U_YCLView;

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, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  cxDropDownEdit, Menus;

type
  TfrmYCLView = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    SPName: TEdit;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Label4: TLabel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column6: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolButton1: TToolButton;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    v2Column5: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    SPXS: TComboBox;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    ToolButton5: TToolButton;
    Panel2: TPanel;
    Label1: TLabel;
    ComboBox1: TComboBox;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure SPNameChange(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v2Column5PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    fDEFstr5: string;
  end;

var
  frmYCLView: TfrmYCLView;

implementation

uses
  U_DataLink, U_RTFun, U_ZdyAttInputCP;

{$R *.dfm}

procedure TfrmYCLView.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from CP_Info where DataType=''CP'' and ZFFlag=0 ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
  ToolButton2.Click;
end;

procedure TfrmYCLView.FormDestroy(Sender: TObject);
begin
  frmYCLView := nil;
end;

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

procedure TfrmYCLView.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('原料信息管理', Tv2, '客户自定义');
  Close;
end;

procedure TfrmYCLView.FormShow(Sender: TObject);
begin
//  ReadCxGrid('原料信息管理', Tv2, '客户自定义');
  InitGrid();
end;

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

procedure TfrmYCLView.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmYCLView.SPNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYCLView.Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  ModalResult := 1;
end;

procedure TfrmYCLView.ToolButton1Click(Sender: TObject);
begin
  try
    frmZdyAttInputCP := TfrmZdyAttInputCP.Create(Application);
    with frmZdyAttInputCP do
    begin
      FCYID := '';
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmZdyAttInputCP.Free;
  end;
end;

procedure TfrmYCLView.ToolButton3Click(Sender: TObject);
begin
  try
    frmZdyAttInputCP := TfrmZdyAttInputCP.Create(Application);
    with frmZdyAttInputCP do
    begin
      FCYID := Trim(Self.CDS_HZ.fieldbyname('CPID').AsString);
      if ShowModal = 1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmZdyAttInputCP.Free;
  end;
end;

procedure TfrmYCLView.ToolButton4Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if Trim(CDS_HZ.fieldbyname('CPID').AsString) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' UPdate CP_Info Set ZFFlag=1,ZFTime=Getdate(),ZFPerson=''' + Trim(DName) + '''');
      sql.Add(' where CPID=''' + Trim(CDS_HZ.fieldbyname('CPID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmYCLView.v2Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxCheckBox(Sender).EditValue;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    if mvalue = 'False' then
    begin
      sql.Add('update CP_Info set ISQY=0 where CPID=' + quotedstr(Trim(CDS_HZ.fieldbyname('CPID').AsString)));
    end
    else
    begin
      sql.Add('update CP_Info set ISQY=1 where CPID=' + quotedstr(Trim(CDS_HZ.fieldbyname('CPID').AsString)));
    end;
    execsql;
  end;
end;

procedure TfrmYCLView.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if CDS_HZ.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Trim(ComboBox1.Text)='' then
  begin
    Application.MessageBox('首选单位不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while Locate('SSel',True,[])=True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate CP_Info Set QtyUnit='''+Trim(ComboBox1.Text)+'''');
          sql.Add(' where CPID='''+Trim(CDS_HZ.fieldbyname('CPID').AsString)+'''');
          ExecSQL;
        end;
        Edit;
        FieldByName('SSel').Value:=False;
        FieldByName('QtyUnit').Value:=Trim(ComboBox1.Text);
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;

end;

procedure TfrmYCLView.N1Click(Sender: TObject);
begin
  SelOKNoFilter(Tv2,True);
end;

procedure TfrmYCLView.N2Click(Sender: TObject);
begin
  SelOKNoFilter(Tv2,False);
end;

end.

