unit U_TradeConSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ToolWin, ComCtrls, U_BaseHelp, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, ImgList, StdCtrls, ExtCtrls,
  cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, System.ImageList, U_BaseInput, cxButtonEdit,
  cxDropDownEdit, cxCheckBox, Vcl.Menus, cxPC, dxSkinWXI, dxScrollbarAnnotations;

type
  TfrmTradeConSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    ADOConnection1: TADOConnection;
    Panel1: TPanel;
    Label1: TLabel;
    ConNo: TEdit;
    GPM_1: TcxGridPopupMenu;
    ImageList1: TImageList;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    VC_SCSCode: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    SHuangSeCu: TcxStyle;
    TV1Column7: TcxGridDBColumn;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label2: TLabel;
    C_Name: TEdit;
    Label3: TLabel;
    BuyName: TEdit;
    cxTabControl1: TcxTabControl;
    Label4: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    TV1Column8: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    FCoType, FAuthority,Canshu2: string;
    { Public declarations }
  end;

var
  frmTradeConSel: TfrmTradeConSel;

implementation

uses
  U_DataLink, U_RTFun, U_TradeSalesContractSel;

{$R *.dfm}

procedure TfrmTradeConSel.FormCreate(Sender: TObject);
begin
  inherited;
  EndDate.DateTime := Now();
  BegDate.DateTime := EndDate.DateTime - 30;
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    application.MessageBox('网络连接失败！', '提示信息');
  end;
end;

procedure TfrmTradeConSel.ConNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);

end;

procedure TfrmTradeConSel.cxTabControl1Change(Sender: TObject);
begin
  inherited;
  InitGrid();
end;

procedure TfrmTradeConSel.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  ConNo.SetFocus;

  Action := cahide;
end;

procedure TfrmTradeConSel.InitGrid();
var
  WSql: string;
begin
  WSql := WSql + ' and ConDate>' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
  WSql := WSql + ' and ConDate<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.* from BS_Contract_Main A inner join BS_Contract_Sub B on A.ConMId=B.ConMId  ');
      sql.Add(' where  isnull(A.status,''0'')=''9''');
      if Canshu2='理单' then
      begin
         sql.add(' and  A.Tallyer= ' + QuotedStr(Trim(DName)));
      end;
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' and not  EXISTS ( select ConNo from Trade_Plan_Main X where X.ConNo=A.ConNo )');
          end;
        1:
          begin
            sql.Add(' and  EXISTS ( select ConNo from Trade_Plan_Main X where X.ConNo=A.ConNo )'+ (WSql));
          end;
        2:
          begin
            sql.Add('and 1=1'+ (WSql));
          end;

      end;
      sql.Add('Order By A.ConDate Desc');
//      ShowMessage(sql.text);
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmTradeConSel.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_1, True);

end;

procedure TfrmTradeConSel.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_1, False);
end;

procedure TfrmTradeConSel.FormShow(Sender: TObject);
begin
  inherited;

  InitGrid();

  ReadCxGrid(Trim(self.Caption), TV1, '自定义数据');

end;

procedure TfrmTradeConSel.TBCloseClick(Sender: TObject);
begin
  inherited;
  Close;
end;

procedure TfrmTradeConSel.TBSaveClick(Sender: TObject);
begin
  WriteCxGrid(Trim(self.Caption), TV1, '自定义数据');

end;

procedure TfrmTradeConSel.ToolButton1Click(Sender: TObject);
var
  Count: Integer;
  Value: string;
begin
  ConNo.SetFocus;
  Count := 1;
//  value :=FConNo;
  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.fieldbyname('SSel').AsBoolean then
      begin
        if Value = '' then
        begin
          Value := FieldByName('ConNo').AsString;
        end;
        if Value <> '' then
        begin
          if Value <> FieldByName('ConNo').AsString then
          begin
            Count := Count + 1;
          end;
        end;
      end;

      Next;
    end;
  end;
  if Count = 1 then
  begin
    ModalResult := 1;
  end
  else
  begin
    application.MessageBox('选择的不是同一销售合同！', '提示信息');
    exit;
  end;
end;

procedure TfrmTradeConSel.TV1DblClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmTradeConSel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradeConSel := nil;
end;

end.

