unit U_CollectWriteOffList;

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, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, 
   dxDateRanges, dxBarBuiltInMenu;

type
  TfrmCollectWriteOffList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    OppCoName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Ssel: TcxGridDBColumn;
    Label4: TLabel;
    ADOQueryMain: TADOQuery;
    ToolButton3: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    Label7: TLabel;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    FromId: TEdit;
    ToId: TEdit;
    CheckBox1: TCheckBox;
    ToolButton2: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FromIdKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton2Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    { Public declarations }
    RKFlag, FCYID: string;
  end;

var
  frmCollectWriteOffList: TfrmCollectWriteOffList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmCollectWriteOffList.InitGrid();
var
  WSql: string;
begin
  WSql := SGetFilters(Panel1, 1, 2);
  if Trim(WSql) <> '' then
    WSql := ' and ' + WSql
  else
    CheckBox1.Checked := True;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
      sql.Add(' select AA.*  from(  ');
      sql.Add(' select A.*,WoIDStr=Cast(A.WoID as varchar(60)),C.F_Code,C.F_Name,C.F_Color,C.F_ColorNo  ');
      sql.Add(' from Finance_WriteOff A ');
      sql.Add(' inner join Finance_Flow B on A.FromId=B.FFID   ');
      sql.Add(' inner join Finance_Flow C on A.ToId=C.FFID  ');
      sql.Add(' where 1=1  ');
      if CheckBox1.Checked then
      begin
        sql.Add(' and A.Filltime>=''' + formatdateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
        sql.Add(' and A.Filltime<''' + formatdateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
      end;
      sql.Add(' ) AA where 1=1   ');
      sql.Add(WSql);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCollectWriteOffList.FormDestroy(Sender: TObject);
begin
  frmCollectWriteOffList := nil;
end;

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

procedure TfrmCollectWriteOffList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmCollectWriteOffList.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption, Tv2, '财务管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime - 7;

  InitGrid();
end;

procedure TfrmCollectWriteOffList.FromIdKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

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

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

procedure TfrmCollectWriteOffList.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv2, '财务管理');
end;

procedure TfrmCollectWriteOffList.FormCreate(Sender: TObject);
begin
  cxGrid2.Align := AlClient;
end;

procedure TfrmCollectWriteOffList.ToolButton3Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    with CDS_HZ do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with AdoqueryCmd do
          begin
            Close;
            SQL.Clear;
            SQL.Add(' exec P_Fin_Flow_WriteOff_Del');
            SQL.Add(' @WoID = ' + Trim(CDS_HZ.FieldByName('WoIDStr').AsString));
            SQL.Add(',@DName = ' + QuotedStr(Trim(DName)));
            Open;

          end;
          if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
            raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    application.MessageBox('撤销成功！', '提示信息');
    TBRafresh.Click;
  except
    CDS_HZ.EnableControls;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

end.

