unit U_CaiWuHuilV;

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, cxCurrencyEdit, cxDropDownEdit, Menus;

type
  TfrmCaiWuHuiLV = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    V1BegDate: TcxGridDBColumn;
    V1HuiLv: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Edit1: TEdit;
    Label7: TLabel;
    Button1: TButton;
    TADD: TToolButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    TSave: TToolButton;
    ADOQuery1: TADOQuery;
    Label4: TLabel;
    v2Column1: TcxGridDBColumn;
    Panel2: TPanel;
    Label1: TLabel;
    HuiLv: TEdit;
    Panel3: TPanel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure cxTabControl2Change(Sender: TObject);
    procedure TSaveClick(Sender: TObject);
    procedure begdateChange(Sender: TObject);
    procedure TADDClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();

  public
    fFlag:integer;
    { Public declarations }
    RKFlag,FCYID,fmanage:String;

  end;

var
  frmCaiWuHuiLV: TfrmCaiWuHuiLV;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmCaiWuHuiLV.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*,BegDateStr=Substring(Convert(varchar(7),BegDate,120),6,2)');
      sql.Add(' from HuiLV A');
      sql.Add(' where Convert(varchar(4),BegDate,120)='''+Trim(FormatDateTime('yyyy',begdate.Date))+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;



procedure TfrmCaiWuHuiLV.FormDestroy(Sender: TObject);
begin
  frmCaiWuHuiLV:=nil;
end;

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

procedure TfrmCaiWuHuiLV.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('汇率信息',Tv2,'财务管理');
  Close;
end;
procedure TfrmCaiWuHuiLV.FormShow(Sender: TObject);
begin
  ReadCxGrid('汇率信息',Tv2,'财务管理');
  begdate.DateTime:=SGetServerDate(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmCaiWuHuiLV.TBRafreshClick(Sender: TObject);
begin
  toolbar1.SetFocus;
  InitGrid();
end;

procedure TfrmCaiWuHuiLV.cxTabControl2Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCaiWuHuiLV.TSaveClick(Sender: TObject);
var
  maxid:string;
  FReal:Double;
begin
  IF CDS_HZ.IsEmpty then exit;
  if TryStrToFloat(HuiLv.Text,FReal)=False then
  begin
    Application.MessageBox('汇率非法数字!','提示',0);
    Exit;
  end;
  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要设定汇率吗？','提示',32+4)<>IDYES then Exit;
  Panel3.Visible:=True;
  Panel3.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      while Locate('SSel',True,[]) do
      begin
        maxid:=trim(fieldbyname('HLID').AsString);
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from HuiLV ');
          sql.add('where HLID='''+Trim(maxid)+'''');
          open;
        end;
        with ADOQueryCmd do
        begin
          Edit;
          fieldbyname('Edittime').AsDateTime:=SGetServerDate(ADOQueryTemp);
          fieldbyname('Editer').Value:=Trim(DName);
          fieldbyname('HuilV').Value:=HuiLv.Text;
          post;
        end;
        Edit;
        FieldByName('SSel').Value:=False;
        Post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Panel3.Visible:=False;
    CDS_HZ.EnableControls;
  except
    Panel3.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_HZ.EnableControls;
    Application.MessageBox('设置汇率失败!','提示',0);
    Exit;
  end;
  initgrid();
end;

procedure TfrmCaiWuHuiLV.begdateChange(Sender: TObject);
begin
  initgrid();
end;

procedure TfrmCaiWuHuiLV.TADDClick(Sender: TObject);
var
  i:Integer;
  maxid,FBegDate,FEndDate:string;
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from HuiLv where Convert(varchar(4),BegDate,120)='''+Trim(FormatDateTime('yyyy',begdate.Date))+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_HZ);
  SInitCDSData20(ADOQueryTemp,CDS_HZ);
  if ADOQueryTemp.IsEmpty=False then Exit;
  Panel3.Visible:=True;
  Panel3.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    for i:=1 to 12 do
    begin
      if i<10 then
      begin
        FBegDate:=Trim(FormatDateTime('yyyy',begdate.Date))+'-0'+Inttostr(i)+'-01'
      end else
      begin
        FBegDate:=Trim(FormatDateTime('yyyy',begdate.Date))+'-'+Inttostr(i)+'-01'
      end;

      if i in[1,3,5,7,8,10,12] then
      begin
        FEndDate:=Copy(FBegDate,1,7)+'-31'
      end else
      if i=2 then
      begin
        FEndDate:=FormatDateTime('yyyy-MM-dd',(strtodate(Copy(FBegDate,1,4)+'-03-01')-1));
      end else
      begin
        FEndDate:=Copy(FBegDate,1,7)+'-30'
      end;
      if GetLSNo(ADOQueryCmd,maxId,'HL','JYOrder_HuiLV',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Panel3.Visible:=False;
        Application.MessageBox('取最大号失败！','提示',0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from HuiLV ');
        sql.add('where HLID='''+Trim(maxid)+'''');
        open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        Fieldbyname('HLID').AsString:=trim(maxID);
        Fieldbyname('BegDate').Value:=FBegDate;
        Fieldbyname('EndDate').Value:=FEndDate;
        Post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Panel3.Visible:=False;
    InitGrid();
  except
    Panel3.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增加月份失败!','提示',0);
    Exit;
  end;
end;

procedure TfrmCaiWuHuiLV.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ,True);
end;

procedure TfrmCaiWuHuiLV.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ,False);
end;

end.
