unit U_ContractListNXSel;

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,
  cxTextEdit, cxDropDownEdit;

type
  TfrmContractListNXSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_Main: TDataSource;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    CDS_Sub: TClientDataSet;
    RM1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    v1Column4: TcxGridDBColumn;
    PopupMenu2: TPopupMenu;
    N11: TMenuItem;
    N21: TMenuItem;
    N31: TMenuItem;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    BuyName: TEdit;
    YwyName: TEdit;
    MPRTCodeName: TEdit;
    ConNo: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    cxTabControl1: TcxTabControl;
    cxSplitter1: TcxSplitter;
    DS_Sub: TDataSource;
    v1Column8: TcxGridDBColumn;
    Label2: TLabel;
    v1Column10: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v1Column13: TcxGridDBColumn;
    Label3: TLabel;
    Filler: TEdit;
    v1Column14: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    Label6: TLabel;
    P_Code: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    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 Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure Tv1DblClick(Sender: TObject);
    procedure P_CodeKeyPress(Sender: TObject; var Key: Char);
  private
    procedure InitGrid();
    procedure InitForm();
    procedure InitGridSub();
    procedure InitSub();
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, FGSTT: string;
    { Public declarations }
  end;

var
  frmContractListNXSel: TfrmContractListNXSel;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_SysLogHelp;

{$R *.dfm}
procedure TfrmContractListNXSel.InitSub();
begin

  if CDS_Main.IsEmpty then
    exit;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from Contract_sub ');
    sql.Add('where conmainID =' + quotedstr((CDS_Main.fieldbyname('conmainID').AsString)));
    open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Sub);
  SInitCDSData20(ADOQueryTemp, CDS_Sub);
end;

procedure TfrmContractListNXSel.InitGridSub();
begin
 try
   ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select A.*,C.*');
      sql.Add(',ISSC=cast((case when isnull((select count(WBID) from TP_File X where X.WBID=A.ConMainId and X.TFType=''合同''),0)>0 then 1 else 0 end) as bit)');
      sql.Add(',ZJinE=(select sum(Money) Money from Contract_sub B where B.ConMainId=A.ConMainId group by B.ConMainId)');
      sql.Add(',ZJinUnit=(select TOP 1 BZType from Contract_sub B where B.ConMainId=A.ConMainId)');
      sql.Add(',ZShuL=(select sum(Qty) Qty from Contract_sub B where B.ConMainId=A.ConMainId group by B.ConMainId)');
      sql.Add(',ZShuUnit=(select TOP 1 QtyUnit from Contract_sub B where B.ConMainId=A.ConMainId)');
      sql.Add(' from Contract_Main A inner join Contract_sub C ON C.ConMainId=A.ConMainId ');
      SQL.Add('where  A.ConDate>=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + '''');
      SQL.Add('and  A.ConDate<''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + '''');
      sql.Add(' and A.ConType=''销售'' ');
      sql.Add(' and C.P_Code='+QuotedStr(Trim(P_Code.Text)));

      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' and  isnull(A.status,''-1'')=''-1''');
          end;
        1:
          begin
            sql.Add(' and  isnull(A.status,''-1'')=''0''');
          end;
        2:
          begin
            sql.Add(' and  isnull(A.status,''-1'')=''1''');
          end;
        3:
          begin
            sql.Add(' and  isnull(A.status,''-1'')=''2''');
          end;
      end;
      //ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
 finally
   ADOQueryMain.EnableControls;
 end
 
end;

procedure TfrmContractListNXSel.FormDestroy(Sender: TObject);
begin
//  frmContractListNX:=nil;
end;

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

procedure TfrmContractListNXSel.TBCloseClick(Sender: TObject);
begin
  Close;
  writeCxGrid(self.Caption + tv1.Name, Tv1, '销售合同管理');
  writeCxGrid(self.Caption + tv2.Name, Tv2, '销售合同管理');
end;

procedure TfrmContractListNXSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select A.*');
      sql.Add(',ISSC=cast((case when isnull((select count(WBID) from TP_File X where X.WBID=A.ConMainId and X.TFType=''合同''),0)>0 then 1 else 0 end) as bit)');
      sql.Add(',ZJinE=(select sum(Money) Money from Contract_sub B where B.ConMainId=A.ConMainId group by B.ConMainId)');
      sql.Add(',ZJinUnit=(select TOP 1 BZType from Contract_sub B where B.ConMainId=A.ConMainId)');
      sql.Add(',ZShuL=(select sum(Qty) Qty from Contract_sub B where B.ConMainId=A.ConMainId group by B.ConMainId)');
      sql.Add(',ZShuUnit=(select TOP 1 QtyUnit from Contract_sub B where B.ConMainId=A.ConMainId)');
      sql.Add(' from Contract_Main A  ');
      SQL.Add('where  A.ConDate>=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + '''');
      SQL.Add('and  A.ConDate<''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + '''');
      sql.Add(' and A.ConType=''销售'' ');

      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' and  isnull(A.status,''-1'')=''-1''');
          end;
        1:
          begin
            sql.Add(' and  isnull(A.status,''-1'')=''0''');
          end;
        2:
          begin
            sql.Add(' and  isnull(A.status,''-1'')=''1''');
          end;
        3:
          begin
            sql.Add(' and  isnull(A.status,''-1'')=''2''');
          end;
      end;
    //showmessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);

  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmContractListNXSel.InitForm();
begin
  readCxGrid(self.Caption + tv1.Name, Tv1, '销售合同管理');
  readCxGrid(self.Caption + tv2.Name, Tv2, '销售合同管理');

  if FCloth = 1 then
  begin
    v1Column4.Visible := True;
  end
  else
  begin
    v1Column4.Visible := False;
  end;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 90;
  InitGrid();

end;

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

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

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

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

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

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

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

procedure TfrmContractListNXSel.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitSub();
end;

procedure TfrmContractListNXSel.Tv1DblClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  ModalResult := 1;
end;

procedure TfrmContractListNXSel.P_CodeKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = #13 then
  begin
    if P_Code.Text<>''then
    InitGridSub();
  end;
end;

end.

