unit U_YKHXList;

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;

type
  TfrmYKHXList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    FactoryName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    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;
    listNo: TEdit;
    Label5: TLabel;
    ToNo: TEdit;
    ADOQueryMain: TADOQuery;
    CheckBox1: TCheckBox;
    ToolButton3: TToolButton;
    v2Column6: TcxGridDBColumn;
    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 FactoryNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    { Public declarations }
    RKFlag,FCYID:String;

  end;

var
  frmYKHXList: TfrmYKHXList;

implementation
uses
  U_DataLink,U_Fun10,U_ZDYHelp;

{$R *.dfm}



procedure TfrmYKHXList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered:=False;
      sql.Add(' select A.*,B.factoryName ');
      sql.Add(' from YF_Log A ');
      sql.Add(' inner join YF_Money_CR B on B.YFID=A.ToNo ');
      sql.Add(' where A.Filldate>='''+formatdateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
      sql.Add(' and A.Filldate<'''+formatdateTime('yyyy-MM-dd',enddate.DateTime+1)+''' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmYKHXList.FormDestroy(Sender: TObject);
begin
  frmYKHXList:=nil;
end;

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

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


  InitGrid();
end;

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

procedure TfrmYKHXList.ToolButton2Click(Sender: TObject);
var
  fsj:string;
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  end;
end;

procedure TfrmYKHXList.FactoryNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYKHXList.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(self.Caption,cxgrid2);
//  SelExportData(Tv2,ADOQueryMain,'应付款付款统计列表');
end;

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

procedure TfrmYKHXList.CheckBox1Click(Sender: TObject);
begin
  IF  CDS_HZ.IsEmpty then exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=checkbox1.Checked;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmYKHXList.ToolButton3Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  ADOQueryCmd.Connection.BeginTrans;
  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(' update YF_Money_CR SET HXNum=A.HxNum - B.num ');
            sql.Add(' from YF_Money_CR A ');
            sql.Add(' inner join  YF_Log B on B.listNo=A.YFID ');
            sql.Add(' where B.AutoID='+quotedstr(trim(CDS_HZ.fieldbyname('AutoID').AsString)));

            sql.Add(' update YF_Money_CR SET HXNum=A.HxNum - B.num ');
            sql.Add(' from YF_Money_CR A ');
            sql.Add(' inner join  YF_Log B on B.ToNo=A.YFID ');
            sql.Add(' where B.AutoID='+quotedstr(trim(CDS_HZ.fieldbyname('AutoID').AsString)));

            sql.Add(' update YF_Money_CR SET status=''0'' ');
            sql.Add(' from YF_Money_CR A ');
            sql.Add(' inner join  YF_Log B on B.listNo=A.YFID ');
            sql.Add(' where B.AutoID='+quotedstr(trim(CDS_HZ.fieldbyname('AutoID').AsString)));
            sql.Add(' and  hxNum=0 ');

            sql.Add(' update YF_Money_CR SET status=''0'' ');
            sql.Add(' from YF_Money_CR A ');
            sql.Add(' inner join  YF_Log B on B.ToNo=A.YFID ');
            sql.Add(' where B.AutoID='+quotedstr(trim(CDS_HZ.fieldbyname('AutoID').AsString)));
            sql.Add(' and  hxNum=0 ');


            sql.Add(' delete from YF_Log ');
            sql.Add(' where AutoID='+quotedstr(trim(CDS_HZ.fieldbyname('AutoID').AsString)));


            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('撤销成功！','提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_HZ.EnableControls;
    application.MessageBox('撤销失败！','提示信息',0);
  end;

end;

end.
