unit U_ContractListSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, cxPC, cxButtonEdit,
  cxDropDownEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu;

type
  TfrmContractListSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label9: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    BuyName: TEdit;
    Salesman: TEdit;
    OrderNO: TEdit;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    V2Column2: TcxGridDBColumn;
    V2Column5: TcxGridDBColumn;
    V1Column1: TcxGridDBColumn;
    ToolButton2: TToolButton;
    V1Column2: TcxGridDBColumn;
    V1Column3: TcxGridDBColumn;
    V1Column4: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    Label3: TLabel;
    MPRTCodeName: TEdit;
    TV1Column1: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure BuyNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure OrderNOKeyPress(Sender: TObject; var Key: Char);
  private
    procedure InitGrid();
    procedure InitForm();

    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmContractListSel: TfrmContractListSel;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmContractListSel.FormDestroy(Sender: TObject);
begin
  frmContractListSel := nil;
end;

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

procedure TfrmContractListSel.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
end;

procedure TfrmContractListSel.TBCloseClick(Sender: TObject);
begin
  Close;
  writeCxGrid(self.Caption +'1122', Tv1, '合同管理');
end;

procedure TfrmContractListSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select PM.OrderNo,PM.MPRTCodeName,PM.Filler,PM.FillTime,A.MainId,Salesman=A.YWY,BuyName=A.CustomerNoName,A.LiDan');
      sql.Add(' from PBOrder_Main PM ');
      sql.Add(' left join CP_Info CP on PM.MPRTCodeName=CP.SPName');
      sql.Add(' inner join JYOrder_Main A on PM.OrderNo=A.ConNo');
      SQL.Add('where  PM.FillTime>=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + '''');
      SQL.Add(' and  PM.FillTime<''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + '''');
      SQL.Add(' and  isnull(PM.Valid,'''')=''Y'' ');
      sql.Add(' and not exists(select * from Order_HS_OrderNo OO inner join Order_HS HS on OO.HSID=HS.HSID  where HS.Valid=''Y'' and OO.orderNo=PM.OrderNo)');
      if CheckBox1.Checked then
      begin
        sql.Add(' and A.YWY='''+Trim(DName)+'''');
      end;
      sql.Add(' order by PM.OrderNo');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmContractListSel.InitForm();
begin
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 90;
  ReadCxGrid(self.Caption +'1122', Tv1, '合同管理');
  InitGrid();
end;

procedure TfrmContractListSel.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;


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

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

procedure TfrmContractListSel.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmContractListSel.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmContractListSel.BuyNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

procedure TfrmContractListSel.cxTabControl1Change(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmContractListSel.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  ModalResult := 1;
end;

procedure TfrmContractListSel.OrderNOKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.Add('select PM.OrderNo,PM.MPRTCodeName,PM.Filler,PM.FillTime,A.MainId,Salesman=A.YWY,BuyName=A.CustomerNoName,A.LiDan');
        sql.Add(' from PBOrder_Main PM ');
        sql.Add(' left join CP_Info CP on PM.MPRTCodeName=CP.SPName');
        sql.Add(' inner join JYOrder_Main A on PM.OrderNo=A.ConNo');
        SQL.Add('where  PM.OrderNo like'''+'%'+Trim(OrderNO.Text)+'%'+'''');
        SQL.Add(' and  isnull(PM.Valid,'''')=''Y'' ');
        sql.Add(' and not exists(select * from Order_HS_OrderNo OO inner join Order_HS OS on OO.HSID=OS.HSID  where OO.OrderNo=A.ConNo and OS.Valid=''Y'')');
        if CheckBox1.Checked then
        begin
          sql.Add(' and A.YWY='''+Trim(DName)+'''');
        end;
        sql.Add(' order by PM.OrderNo');
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

end.

