unit U_GRYearPFList_FHDQDJ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxPC, cxCheckBox, Menus, cxCurrencyEdit;


type
  TfrmGRYearPFList_FHDQDJ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    Panel1: TPanel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    ToolButton1: TToolButton;
    ADOQueryPrint: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1QSDate: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    TBAdd: TToolButton;
    TVYXSMoney: TcxGridDBColumn;
    TVFHStatus: TcxGridDBColumn;
    TBDel: TToolButton;
    TBSave: TToolButton;
    v1XSMBMoney: TcxGridDBColumn;
    v1JSDate: TcxGridDBColumn;
    v1WCLv: TcxGridDBColumn;
    v1PriceUnit: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    v1Ssel: TcxGridDBColumn;
    TSel: TToolButton;
    v1FHZMoney: TcxGridDBColumn;
    v1HuiLV: TcxGridDBColumn;
    Panel2: TPanel;
    v1FHYear: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TSelClick(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);

  private
    FInt,PFInt:Integer;
    canshu1,canshu2:string;
    FNowDate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    { Private declarations }
  public
    Formid: string;
    { Public declarations }
  end;

var
  frmGRYearPFList_FHDQDJ: TfrmGRYearPFList_FHDQDJ;

implementation
uses
  U_DataLink,U_RTFun,U_KDInPut,U_ModuleNote, U_ZDYHelp,U_GRYearPFList_Sub,
  U_ZDYHelpSel;

{$R *.dfm}

procedure TfrmGRYearPFList_FHDQDJ.FormDestroy(Sender: TObject);
begin
  frmGRYearPFList_FHDQDJ:=nil;
end;

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

procedure TfrmGRYearPFList_FHDQDJ.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('分红档期登记',Tv1,'分红管理');
end;

procedure TfrmGRYearPFList_FHDQDJ.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_FH_WCMoney '''+trim(formatdatetime('yyyy-MM-dd',begdate.DateTime))+'''');
    sql.Add(','''+trim(formatdatetime('yyyy-MM-dd',enddate.DateTime+1))+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  with Order_Main do
  begin
    first;
    while not eof do
    begin
      if Order_Main.fieldbyname('WCLv').AsFloat>=100 then
      begin
        with order_Main do
        begin
          edit;
          fieldbyname('FHStatus').AsString:='已完成';
          fieldbyname('FHZMoney').AsFloat:=1000000;
          post;
        end;
      end;
      if (Order_Main.fieldbyname('WCLv').AsFloat>=50) and (Order_Main.fieldbyname('WCLv').AsFloat<100) then
      begin
        with order_Main do
        begin
          edit;
          fieldbyname('FHStatus').AsString:='未完成';
          fieldbyname('FHZMoney').AsFloat:=fieldbyname('WCLv').AsFloat*10000;
          post;
        end;
      end;
      next;
    end;
  end;
end;

procedure TfrmGRYearPFList_FHDQDJ.InitForm();
begin
  begdate.Date:=strtodate(formatdateTime('yyyy',SGetServerDate(ADOQueryTemp))+'-01-01');
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  ReadCxGrid('分红档期登记',Tv1,'分红管理');
  TBAdd.Visible:=False;
  TBDel.Visible:=False;
  TBSave.Visible:=False;
  TBExport.Visible:=False;
  Tsel.Visible:=False;
  if Formid<>'99' then
  begin
    TBAdd.Visible:=True;
    TBDel.Visible:=True;
    TBSave.Visible:=True;
    TBExport.Visible:=True;
  end
  else
  begin
    TSel.Visible:=True;
  end;
  InitGrid();
end;

procedure TfrmGRYearPFList_FHDQDJ.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('分红档期',cxGrid1);
end;

procedure TfrmGRYearPFList_FHDQDJ.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  Panel2.Visible:=True;
  Panel2.Refresh;
  InitGrid();
  Panel2.Visible:=False;
end;

procedure TfrmGRYearPFList_FHDQDJ.FormShow(Sender: TObject);
var
  i:Integer;
begin
  InitForm();
end;

procedure TfrmGRYearPFList_FHDQDJ.ToolButton1Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmGRYearPFList_FHDQDJ.TBDelClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Order_Main.IsEmpty then exit;
  if application.MessageBox('确定要删除吗','提示',1)=2 then exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete from GRYearFH_Main ');
    sql.add('where FHID='''+trim(Order_Main.fieldbyname('FHID').AsString)+'''');
    execsql;
  end;
  initGrid();
end;

procedure TfrmGRYearPFList_FHDQDJ.TBSaveClick(Sender: TObject);
var maxno: string;
begin
  ToolBar1.SetFocus;
  if Order_Main.IsEmpty then exit;
  with Order_Main do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('HuiLV').AsFloat=0 then
      begin
        application.MessageBox('汇率不能为空','提示');
        exit;
      end;
      if fieldbyname('FHYear').asstring='' then
      begin
        application.MessageBox('年份不能为空','提示');
        exit;
      end;
      if fieldbyname('FHID').AsString='' then
      begin
        if GetLSNo(ADOQueryCmd,maxno,'FH','GRYearFH_Main',3,1)=False then
        begin
          Application.MessageBox('取主流水号失败!','提示',0);
          Exit;
        end;
      end
      else
      maxno:=fieldbyname('FHID').AsString;
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from GRYearFH_Main ');
        sql.add('where FHID='''+trim(maxno)+'''');
        open;
        if isempty then append
        else edit;
        fieldbyname('FHID').AsString:=trim(maxno);
        fieldbyname('HuiLV').Value:=fieldbyname('HuiLV').AsFloat;
        RTSetSaveDataCDS(ADOQueryCmd,Tv1,Order_Main,'GRYearFH_Main',0);
        post;
      end;
      next;
    end;
    application.MessageBox('保存成功','提示');
    initGrid();
  end;
end;

procedure TfrmGRYearPFList_FHDQDJ.TBAddClick(Sender: TObject);
begin
  with Order_Main do
  begin
    append;
    fieldbyname('FHStatus').AsString:='未完成';
    fieldbyname('PriceUnit').AsString:='$';
    fieldbyname('HuiLV').Value:=6.3;
    post;
  end;
end;

procedure TfrmGRYearPFList_FHDQDJ.v1PriceUnitPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='PriceUnit';
    flagname:='币种';
    if showModal=1 then
    begin
      with Order_Main do
      begin
        edit;
        fieldbyname('PriceUnit').asstring:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmGRYearPFList_FHDQDJ.TSelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  ModalResult:=1;
end;

procedure TfrmGRYearPFList_FHDQDJ.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  TSel.Click;
end;

end.
