unit U_YF_Money_LL;

interface

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

type
  TfrmMoney_LL = class(TForm)
    ToolBar1: TToolBar;
    TINIT: TToolButton;
    TGLV: TToolButton;
    TADD: TToolButton;
    TUPDATE: TToolButton;
    TDEL: TToolButton;
    TSELECT: TToolButton;
    Texcel: TToolButton;
    TCLOSE: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label2: TLabel;
    CRType: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CRZY: TEdit;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1CRTime: TcxGridDBColumn;
    V1CRType: TcxGridDBColumn;
    V1InMoney: TcxGridDBColumn;
    V1NOTE: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    DataSource1: TDataSource;
    AdoQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    TZDY: TToolButton;
    cxTabControl1: TcxTabControl;
    V1CRZY: TcxGridDBColumn;
    V1OutMoney: TcxGridDBColumn;
    V1YEMoney: TcxGridDBColumn;
    Label3: TLabel;
    V1Column1: TcxGridDBColumn;
    TofactoryName: TEdit;
    Label5: TLabel;
    V1Column2: TcxGridDBColumn;
    Label6: TLabel;
    OrderNo: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TV1DblClick(Sender: TObject);
    procedure TCLOSEClick(Sender: TObject);
    procedure TADDClick(Sender: TObject);
    procedure TINITClick(Sender: TObject);
    procedure TGLVClick(Sender: TObject);
    procedure TUPDATEClick(Sender: TObject);
    procedure TSELECTClick(Sender: TObject);
    procedure TexcelClick(Sender: TObject);
    procedure TDELClick(Sender: TObject);
    procedure TZDYClick(Sender: TObject);
    procedure CRTypeChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    procedure initGrid();
    procedure SetTab();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMoney_LL: TfrmMoney_LL;

implementation

uses U_DataLink, U_Fun,U_YF_Money_Sub, U_ZDYHelp;

{$R *.dfm}
procedure TfrmMoney_LL.SetTab();
begin
  cxTabControl1.Tabs.Clear;
  with AdoqueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type =''XJLLFLAG'' ');
    open;
    If not isempty then
    begin
      while not eof do
      begin
         cxTabControl1.Tabs.Add(trim(fieldbyname('ZDYName').AsString));
         next;
      end;
    end;
  end;

  IF cxTabControl1.Tabs.Count<1 then
  cxTabControl1.Tabs.Add('初始数据');
//  cxTabControl1.TabIndex:=0;
end;
procedure TfrmMoney_LL.InitGrid();
begin
  with AdoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,YEMoney=dbo.F_Get_MoneyLlYe(A.LLNo,A.CRTime,A.factoryName) ');
    sql.Add('from YF_Money_LL A ');
    sql.Add('where CRTime>='''+formatdateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
    sql.Add('and CRTime<'''+formatdateTime('yyyy-MM-dd',enddate.DateTime+1)+''' ');
    sql.Add('and factoryName='+quotedstr(Trim(cxTabControl1.Tabs.Strings[cxTabControl1.TabIndex])));
    //SQL.Add('group by LLNO,CRTime,CRType,InMoney,OutMoney,CRZY,NOTE ');
    SQL.Add('order by A.CRTime,LLNO');
    open;
  end;
  SCreateCDS20(ADOQueryTmp,CDS_Main);
  SInitCDSData20(ADOQueryTmp,CDS_Main);
end;

procedure TfrmMoney_LL.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alclient; 
end;

procedure TfrmMoney_LL.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption+tv1.Name,Tv1);
  enddate.datetime:=SGetServerDateTime(ADOQueryTmp);
  begdate.datetime:=strtodate(formatdateTime('yyyy-MM',enddate.DateTime)+'-01');
  SetTab();
  initGrid();
end;

procedure TfrmMoney_LL.FormDestroy(Sender: TObject);
begin
  frmMoney_LL:=nil;
end;

procedure TfrmMoney_LL.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  writeCxGrid(self.Caption+tv1.Name,Tv1);
  action:=cafree;
end;

procedure TfrmMoney_LL.TV1DblClick(Sender: TObject);
begin
  TSELECT.click;
end;

procedure TfrmMoney_LL.TCLOSEClick(Sender: TObject);
begin
  close;
end;

procedure TfrmMoney_LL.TADDClick(Sender: TObject);
begin
  try
    FrmYF_Money_Sub:=TFrmYF_Money_Sub.Create(Application);
    with FrmYF_Money_Sub do
    begin
      factoryName.text:=Trim(cxTabControl1.Tabs.Strings[cxTabControl1.TabIndex]);
      Fkeyno:='';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally;
    FrmYF_Money_Sub.free;
  end;
end;

procedure TfrmMoney_LL.TINITClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMoney_LL.TGLVClick(Sender: TObject);
begin
  SDofilter(AdoQueryTmp,SGetFilters(Panel1,1,0));
  SCreateCDS20(AdoQueryTmp,CDS_Main);
  SInitCDSData20(AdoQueryTmp,CDS_Main);
end;

procedure TfrmMoney_LL.TUPDATEClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  try
    FrmYF_Money_Sub:=TFrmYF_Money_Sub.Create(Application);
    with FrmYF_Money_Sub do
    begin
      Fkeyno:=trim(Self.CDS_Main.fieldbyname('LLNO').asstring);;
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally;
    FrmYF_Money_Sub.free;
  end;
end;

procedure TfrmMoney_LL.TSELECTClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  try
    FrmYF_Money_Sub:=TFrmYF_Money_Sub.Create(Application);
    with FrmYF_Money_Sub do
    begin
      Fkeyno:=trim(Self.CDS_Main.fieldbyname('LLNO').asstring);
      TSave.Visible:=False;
      panel1.Enabled:=False;
      if ShowModal=1 then
      begin
      //  InitGrid();
      end;
    end;
  finally;
    FrmYF_Money_Sub.free;
  end;
end;

procedure TfrmMoney_LL.TexcelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel(Trim(cxTabControl1.Tabs.Strings[cxTabControl1.TabIndex]),cxGrid1);
end;

procedure TfrmMoney_LL.TDELClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  if CDS_Main.fieldbyname('frommainID').AsString<>'' then
  begin
    application.messagebox('自动生成输入不能删除!','提示',0);
    exit;
  end;
  if application.MessageBox('确定要删除吗？','提示',1)=2 then exit;
  try
    ADOQuerycmd.Connection.BeginTrans;
    with ADOQuerycmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from YF_Money_LL where LLNO='''+trim(CDS_Main.fieldbyname('LLNO').AsString)+'''');
      sql.Add('delete from YF_Money_LL where fromMainID='''+trim(CDS_Main.fieldbyname('LLNO').AsString)+'''');
      execsql;
    end;
    ADOQuerycmd.Connection.CommitTrans;
    initGrid();
  except
    ADOQuerycmd.connection.rollbackTrans;
    application.messagebox('数据删除失败','提示',0);
  end;

end;

procedure TfrmMoney_LL.TZDYClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(self);
    with frmZDYHelp do
    begin
      flag:='XJLLFLAG';
      flagname:='账户类型定义';
      if showmodal=1 then
      begin
        SetTab();
      //  cxTabControl1.Tabs.Text:=trim(ClientDataSet1.fieldbyname('ZDYName').asstring);
      end;
    end;
   // initGrid();
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMoney_LL.CRTypeChange(Sender: TObject);
begin
  SDofilter(AdoQueryTmp,SGetFilters(Panel1,1,0));
  SCreateCDS20(AdoQueryTmp,CDS_Main);
  SInitCDSData20(AdoQueryTmp,CDS_Main);
end;

procedure TfrmMoney_LL.cxTabControl1Change(Sender: TObject);
begin
  initGrid();
end;

end.
