unit U_QryPrtMachInfoList;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.ToolWin, cxGraphics,
  cxControls, cxLookAndFeels,
  cxContainer, cxEdit, dxLayoutcxEditAdapters, dxBarBuiltInMenu, cxPC,
  dxLayoutContainer, cxClasses, cxTextEdit, dxLayoutControl, cxStyles,
  cxCustomData, cxFilter, cxData, cxDataStorage, cxNavigator, dxDateRanges,
  dxScrollbarAnnotations, Data.DB, cxDBData, cxCalendar, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridCustomView,
  cxGrid, U_BaseList, Data.Win.ADODB, cxGridCustomPopupMenu, cxGridPopupMenu,
  Datasnap.DBClient, cxCheckBox, cxButtonEdit, cxSplitter, dxCore, cxDateUtils,
  cxMaskEdit, cxDropDownEdit,  cxProgressBar, Vcl.Menus,
  RM_Common, RM_Class, RM_GridReport, RM_Dataset, cxLookAndFeelPainters,
  Vcl.ExtCtrls, dxSkinsCore, dxSkinsDefaultPainters, dxLayoutControlAdapters,
  FrameDateSel10, Vcl.StdCtrls, MovePanel, BtnEdit, cxCurrencyEdit, U_WindowFormdesign,
  dxSkinWXI;

type
  TfrmQryPrtMachInfoList = class(TfrmBaseList)
    ToolBar3: TToolBar;
    Trefresh: TToolButton;
    TsaveGrid: TToolButton;
    Tclose: TToolButton;
    DataSource1: TDataSource;
    ADOQueryList: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    dxLayoutControl_query: TdxLayoutControl;
    dxLayoutControl_queryGroup_Root: TdxLayoutGroup;
    CDS_List: TClientDataSet;
    cxProgressBar1: TMovePanel;
    ADOQueryCmd: TADOQuery;
    TBADD: TToolButton;
    TBdel: TToolButton;
    TBedit: TToolButton;
    TBexcpet: TToolButton;
    TgridSet: TToolButton;
    P_name: TcxTextEdit;
    dxLayoutItem2: TdxLayoutItem;
    P_spec: TcxTextEdit;
    dxLayoutItem4: TdxLayoutItem;
    ADOQueryTemp: TADOQuery;
    P_Code: TcxTextEdit;
    dxLayoutItem1: TdxLayoutItem;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    cxTabControl1: TcxTabControl;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    procedure TrefreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TcloseClick(Sender: TObject);
    procedure TsaveGridClick(Sender: TObject);
    procedure dxLayoutControl_queryDblClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBADDClick(Sender: TObject);
    procedure TBdelClick(Sender: TObject);
    procedure TBeditClick(Sender: TObject);
    procedure TBexcpetClick(Sender: TObject);
    procedure TgridSetClick(Sender: TObject);
    procedure P_CodeKeyPress(Sender: TObject; var Key: Char);

  private
    fWindowDesign: TWindowFormdesign;
    fDesignCode:string;
    procedure initGrid();
    procedure initForm();
  protected
  public
    canshu1, FSTKName, canshu3: string;

  end;

var
  frmQryPrtMachInfoList: TfrmQryPrtMachInfoList;

implementation
uses
  U_RTFun, U_globalVar, U_dataLink,U_FormLayOutDesign,
  U_LabelPrint,U_cxGridCustomSet, U_ZDYHelp, U_PrtMachInfoInPut;
{$R *.dfm}

procedure TfrmQryPrtMachInfoList.TsaveGridClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, gDllFileCaption);
  if gIsCanDesign then
  begin
    saveLayOut(application, dxLayoutControl_query, ADOQueryCmd,PWideChar( fDllFileName + '|' + Self.Name + '|' + dxLayoutControl_query.Name + '.ini'));
  end;
end;

procedure TfrmQryPrtMachInfoList.TBADDClick(Sender: TObject);
begin
  try
    frmPrtMachInfoInPut := TfrmPrtMachInfoInPut.Create(Application);
    with frmPrtMachInfoInPut do
    begin
      FBCId := '';
      FSTKName := self.FSTKName;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmPrtMachInfoInPut.Free;
  end;
end;

procedure TfrmQryPrtMachInfoList.TBdelClick(Sender: TObject);
begin
  if CDS_List.IsEmpty then
    Exit;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BS_Product_IO where P_Code=''' + Trim(CDS_List.fieldbyname('P_Code').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已入库不能修改!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' delete BS_Product_Info where BPIID=''' + Trim(CDS_List.fieldbyname('BPIID').AsString) + '''');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除异常!', '提示', 0);
  end;
  CDS_List.Delete;
end;

procedure TfrmQryPrtMachInfoList.TBeditClick(Sender: TObject);
begin
  if CDS_List.IsEmpty then
    Exit;
  try
    frmPrtMachInfoInPut := TfrmPrtMachInfoInPut.Create(Application);
    with frmPrtMachInfoInPut do
    begin
      FBCId := Trim(CDS_List.fieldbyname('BPIID').AsString);
      FSTKName := self.FSTKName;
      TBDel.Visible := False;
      TBAdd.Visible := False;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmPrtMachInfoInPut.Free;
  end;
end;

procedure TfrmQryPrtMachInfoList.TBexcpetClick(Sender: TObject);
begin
  if ADOQueryList.IsEmpty then
    exit;
  TcxGridToExcel(trim(self.Caption), cxGrid2);
end;

procedure TfrmQryPrtMachInfoList.TcloseClick(Sender: TObject);
begin
   close;
end;

procedure TfrmQryPrtMachInfoList.TgridSetClick(Sender: TObject);
begin
  fWindowDesign.OpenGridDesignWin10(fDesignCode,'cxgrid2',tv1);
end;

procedure TfrmQryPrtMachInfoList.TrefreshClick(Sender: TObject);
begin
  inherited;
  initGrid();
end;

procedure TfrmQryPrtMachInfoList.cxTabControl1Change(Sender: TObject);
begin
  inherited;
  TBEdit.Visible := false;
  TBDel.Visible := false;
  ToolButton1.Visible := false;
  ToolButton2.Visible := false;
  case cxTabControl1.TabIndex of
    0:
      begin
        TBEdit.Visible := true;
        TBDel.Visible := true;
        ToolButton2.Visible := true;
      end;
    1:
      begin
        ToolButton2.Visible := true;
      end;
  end;
  InitGrid();
end;

procedure TfrmQryPrtMachInfoList.dxLayoutControl_queryDblClick(
  Sender: TObject);
begin
  layoutDesign(TdxLayoutControl(Sender),ADOQueryCmd,PWideChar(dcode));
end;

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

procedure TfrmQryPrtMachInfoList.FormCreate(Sender: TObject);
begin
  inherited;
  canshu1 := Trim(Self.fParameters1);
  FSTKName := Trim(Self.fParameters2);
  canshu3 := Trim(Self.fParameters3);
end;

procedure TfrmQryPrtMachInfoList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmQryPrtMachInfoList := nil;
end;

procedure TfrmQryPrtMachInfoList.FormShow(Sender: TObject);
begin
  inherited;
  InitForm();
end;

procedure TfrmQryPrtMachInfoList.InitForm();
begin
  inherited;
  addQryContionByLay(ADOQueryBaseTemp,fformId,'cxgrid2',dxLayoutControl_query,7);
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, gDllFileCaption);
  InitGrid();
end;

procedure TfrmQryPrtMachInfoList.InitGrid();
var
  mSqlWhere: string;
begin
  mSqlWhere := SLGetFilters(dxLayoutControl_query, 1, 2);
  if trim(mSqlWhere) <> '' then
  begin
    mSqlWhere := ' and ' + trim(mSqlWhere);
  end;
  try
    ADOQueryList.DisableControls;
    with ADOQueryList do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(' from BS_Product_Info A');
      sql.Add(' where isnull(STKNAME,'''')=''' + Trim(FSTKName) + '''');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add('  and  isnull(A.chkstatus,''0'')=''0'' ');
          end;
        1:
          begin
            sql.Add('  and  isnull(A.chkstatus,''0'')=''1'' ');
          end;
      end;
      sql.Add(Trim(mSqlWhere));
      Open;
    end;
    SCreateCDS(ADOQueryList, CDS_List);
    SInitCDSData(ADOQueryList, CDS_List);
  finally
    ADOQueryList.EnableControls;
  end;
end;
procedure TfrmQryPrtMachInfoList.P_CodeKeyPress(Sender: TObject; var Key: Char);
begin
  inherited;
  if key = #13 then
  begin
    initGrid();
  end;
end;

end.
