unit U_HLManage;

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, cxCheckComboBox, cxDropDownEdit, Menus, RM_e_Xls,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, RM_BarCode, dxScrollbarAnnotations, cxContainer,
  cxProgressBar, dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmHLManage = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    GPM_1: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel1: TPanel;
    v2Column8: TcxGridDBColumn;
    Label1: TLabel;
    v2Column12: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    MCNO: TcxTextEdit;
    cxgrdbclmnTv2Column3: TcxGridDBColumn;
    Tv2Column1: 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 ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure CustomerChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure MCNOPropertiesChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v2Column12PropertiesEditValueChanged(Sender: TObject);
    procedure cxgrdbclmnTv2Column3PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv2Column1PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    fFlag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmHLManage: TfrmHLManage;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_LabelPrint, U_MachineInPut, U_EmployeeSel;

{$R *.dfm}

procedure TfrmHLManage.InitGrid();
begin
  MCNO.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* from  BS_HL A order by BegDate ');
//      showmessage(Sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmHLManage.MCNOPropertiesChange(Sender: TObject);
var
  sql: string;
begin
  if ADOQueryMain.Active then
  begin
    sql := SGetFilters(Panel1, 1, 2);
    SDofilter(ADOQueryMain, sql);
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmHLManage.FormDestroy(Sender: TObject);
begin
  inherited;
  frmHLManage := nil;
end;

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

procedure TfrmHLManage.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption), Tv2, '汇率管理');
  Close;
end;

procedure TfrmHLManage.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(Trim(Self.Caption), Tv2, '汇率管理');
  InitGrid();
end;

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

procedure TfrmHLManage.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;

  TcxGridToExcel(self.Caption, cxgrid2);
end;

procedure TfrmHLManage.ToolButton4Click(Sender: TObject);
var
  maxId: string;
begin
  ToolBar1.SetFocus;
  if GetLSNo(ADOQueryCmd, maxId, 'HL', 'BS_HL', 4, 1) = False then
  begin
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('insert into BS_HL(HLID,Filler) values(' + quotedstr(Trim(maxId)) + ',' + quotedstr(Trim(dname)) + ')');
    ExecSQL;
  end;
  InitGrid();
end;

procedure TfrmHLManage.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete BS_HL where HLID=' + QuotedStr(CDS_HZ.FieldByName('HLID').AsString));
    ExecSQL;
  end;

  InitGrid();
end;

procedure TfrmHLManage.Tv2Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' UPdate BS_HL ');
      sql.Add(' Set BZ = ''' + mvalue + '''');
      sql.Add(' where HLID=' + quotedstr(CDS_HZ.fieldbyname('HLID').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmHLManage.v2Column12PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' UPdate BS_HL ');
      sql.Add(' Set Begdate = ''' + mvalue + '''');
      sql.Add(' where HLID=' + quotedstr(CDS_HZ.fieldbyname('HLID').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmHLManage.v2Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' UPdate BS_HL ');
      sql.Add(' Set HL = ' + mvalue + '');
      sql.Add(' where HLID=' + quotedstr(CDS_HZ.fieldbyname('HLID').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmHLManage.cxgrdbclmnTv2Column3PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' UPdate BS_HL ');
      sql.Add(' Set EndDate = ''' + mvalue + '''');
      sql.Add(' where HLID=' + quotedstr(CDS_HZ.fieldbyname('HLID').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

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

procedure TfrmHLManage.N1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  SelOKNo(CDS_HZ, True);
end;

procedure TfrmHLManage.N2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  SelOKNo(CDS_HZ, False);
end;

procedure TfrmHLManage.CustomerChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmHLManage.FormCreate(Sender: TObject);
begin
  inherited;
  fmanage := Trim(DParameters1);
end;

end.

