unit U_FKCR;

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

type
  TfrmFKCR = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    YFName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    FactoryName: TEdit;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    TNochk: TToolButton;
    ToolButton4: TToolButton;
    Label5: TLabel;
    bankNo: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Ssel: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    VBZ: TcxGridDBColumn;
    VHL: TcxGridDBColumn;
    VYB: TcxGridDBColumn;
    VBB: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_YFS: TClientDataSet;
    DS_YFS: TDataSource;
    ToolButton5: TToolButton;
    v2Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    YFGSType: TComboBox;
    Label7: TLabel;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    cv1Column1: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TNochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure bankNoChange(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton5Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure YFGSTypeChange(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    procedure SetStatus();
  public
    fflag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmFKCR: TfrmFKCR;

implementation

uses
  U_DataLink, U_Fun, U_ProductOrderList, U_ProductOrderNewList, U_PbProductList,
  U_ZdyAttachGYS, U_ZdyAttachment, U_FjList10, U_FKInput, U_YKHXList, U_skHx;

{$R *.dfm}

procedure TfrmFKCR.SetStatus();
var
  i: integer;
begin

  Tchk.Visible := false;
  TNochk.Visible := false;
  TBDel.Visible := false;
//  IF fmanage='高权限' then
  begin
    if cxTabControl1.TabIndex = 0 then
    begin
      Tchk.Visible := true;
      TBDel.Visible := true;
    end;
    if cxTabControl1.TabIndex = 1 then
    begin
      TNochk.Visible := true;
    end;
  end;

end;

procedure TfrmFKCR.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select A.*');
      sql.Add(',ISFJ=(cast((case when isnull((select count(x.wbid) from tp_file x where x.wbid = A.yfid and x.TFType = ''YFFJ''),0)>0 then 1 else 0 end ) as bit))');
      sql.Add(' from YF_Money_CR A');
      sql.Add('where A.CRTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
      sql.Add(' and CRTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      sql.Add(' and A.CRType=''付款登记''  ');
         sql.Add('and A.FILLER=''' + Trim(DName) + '''');
      if cxTabControl1.TabIndex < 2 then
        sql.Add(' and A.status=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
     // ShowMessage(SQL.Text);
      Open;

    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmFKCR.FormDestroy(Sender: TObject);
begin
  frmFKCR := nil;
end;

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

procedure TfrmFKCR.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '财务管理');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '财务管理');
  Close;
end;

procedure TfrmFKCR.TBDelClick(Sender: TObject);
var
  FBBMoney, CRID: string;
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    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('delete YF_Money_CR where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
            sql.Add('delete YF_Money_CR_Sub where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmFKCR.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '财务管理');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '财务管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime;

  fsj := 'select distinct(YFGSType) name from YF_Money_CR where CRType=''付款登记'' ';
  SInitComBoxBySql(ADOQueryCmd, YFGSType, False, fsj);

  SetStatus();
  InitGrid();
end;

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

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

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

procedure TfrmFKCR.ToolButton3Click(Sender: TObject);
begin
  try
    frmFKInput := TfrmFKInput.Create(self);
    with frmFKInput do
    begin
      FYFID := '';
      if showmodal = 1 then
      begin
        initGrid();
      end;
    end;
  finally
    frmFKInput.free;
  end;
end;

procedure TfrmFKCR.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv2, ADOQueryMain, '付款列表');
end;

procedure TfrmFKCR.TchkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    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 status=''1'' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmFKCR.TNochkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          if Fieldbyname('HZHxNum').AsFloat > 0 then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            EnableControls;
            application.MessageBox('不能撤销已核销数据！', '提示信息', 0);
            exit;
          end;
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET status=''0'' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmFKCR.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmFKCR.ToolButton4Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  frmFjList10 := TfrmFjList10.create(self);
  with frmFjList10 do
  begin
    fkeyNO := CDS_HZ.fieldbyname('YFID').AsString;
    fType := 'YFFJ';
    showmodal;
    free;
  end;
end;

procedure TfrmFKCR.bankNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmFKCR.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from YF_Money_CR_SUB A');
    sql.Add('where YFID=' + Quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_YFS);
  SInitCDSData20(ADOQueryTemp, CDS_YFS);
end;

procedure TfrmFKCR.ToolButton5Click(Sender: TObject);
begin
  try
    frmFKInput := TfrmFKInput.Create(self);
    with frmFKInput do
    begin
      FYFID := Trim(CDS_HZ.fieldbyname('YFID').AsString);
      if showmodal = 1 then
      begin
        initGrid();
      end;
    end;
  finally
    frmFKInput.free;
  end;
end;

procedure TfrmFKCR.N1Click(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 := true;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmFKCR.N2Click(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 := false;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmFKCR.YFGSTypeChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

end.

