unit U_InvoiceOutSel;

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, cxDropDownEdit, cxPC, RM_e_Xls, Menus, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, dxDateRanges, U_BaseHelp,
  Vcl.Clipbrd, cxContainer, dxCore, cxDateUtils, dxScrollbarAnnotations,
  FrameDateSel, dxSkinsCore, dxSkinsDefaultPainters, cxProgressBar, U_BaseList,
  System.ImageList, Vcl.ImgList;

type
  TfrmInvoiceOutSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    F_Name: TcxTextEdit;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    IVNo: TcxTextEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_Invoice: TDataSource;
    CDS_Invoice: TClientDataSet;
    v1InvoiceNo: TcxGridDBColumn;
    v1InvoiceType: TcxGridDBColumn;
    v1InvoiceDate: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    F_Spec: TcxTextEdit;
    Label5: TLabel;
    OppCoName: TcxTextEdit;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    Label7: TLabel;
    OurCoName: TcxTextEdit;
    Label8: TLabel;
    Label9: TLabel;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    ToolButton3: TToolButton;
    N3: TMenuItem;
    Tv1Column1: TcxGridDBColumn;
    btnok: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    frmFrameDateSel1: TfrmFrameDateSel;
    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 ToolButton1Click(Sender: TObject);
    procedure IVNoChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure btnokClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    procedure SetStatus();
  public
    FZKType, canshu1, canshu2: string;
    { Public declarations }
  end;

var
  frmInvoiceOutSel: TfrmInvoiceOutSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmInvoiceOutSel.SetStatus();
begin

end;

procedure TfrmInvoiceOutSel.InitGrid();
var
  Msql, mbegdate, menddate: string;
  SqlStr: string;
begin

  SqlStr := '  select A.*  ';
  SqlStr := SqlStr + ',FJFlag=CAST((CASE WHEN (SELECT COUNT(X.FileName) FROM FJ_File X where X.TFType=''销项发票'' and X.WBID=A.IVNo)>0 THEN 1 ELSE 0 END) AS BIT)';
  SqlStr := SqlStr + ' from Finance_Invoice A ';
  SqlStr := SqlStr + ' left join Bs_Company B on A.OppCoNo=B.CoCode ';
  SqlStr := SqlStr + 'where  IVFlag=''销项'' ';
  SqlStr := SqlStr + ' and A.IVDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)) + '''';
  SqlStr := SqlStr + ' and A.IVDate<''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)) + '''';

  SqlStr := SqlStr + ' and isnull(A.status,''0'')=''9'' ';

  if trim(canshu1) = '录入' then
    SqlStr := SqlStr + ' and CHARINDEX(' + quotedstr(DCode) + ',(ISNULL(B.SalesId, '''') +'',''+ISNULL(B.TallyId, '''')+'',''+ISNULL(A.FillId, '''') )  ) > 0 ';

//  ShowMessage(SqlStr);
  InitCDSData(ADOQueryMain, CDS_Invoice, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'IVId');
end;

procedure TfrmInvoiceOutSel.FormDestroy(Sender: TObject);
begin
  inherited;
//  frmInvoiceXX := nil;
end;

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

procedure TfrmInvoiceOutSel.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmInvoiceOutSel.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.Caption, Tv1, '财务管理');
  frmFrameDateSel1.Enddate.Date := SGetServerDate(ADOQueryTemp);
  frmFrameDateSel1.begdate.Date := frmFrameDateSel1.Enddate.Date - 30;

  SetStatus();
  InitGrid();
end;

procedure TfrmInvoiceOutSel.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmInvoiceOutSel.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_Invoice);
    SInitCDSData(ADOQueryMain, CDS_Invoice);
  end;
end;

procedure TfrmInvoiceOutSel.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '财务管理');
end;

procedure TfrmInvoiceOutSel.Tv1DblClick(Sender: TObject);
begin
  btnok.Click;
end;

procedure TfrmInvoiceOutSel.btnokClick(Sender: TObject);
begin
  inherited;
  ModalResult := 1;
end;

procedure TfrmInvoiceOutSel.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmInvoiceOutSel.IVNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmInvoiceOutSel.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  initGrid();
end;

procedure TfrmInvoiceOutSel.N1Click(Sender: TObject);
begin

  SelOKNo(CDS_Invoice, True);

end;

procedure TfrmInvoiceOutSel.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Invoice, false);
end;

procedure TfrmInvoiceOutSel.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Invoice.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));

end;

end.

