unit U_JinDuDJ;

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

type
  TfrmJinDuDJ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    VYB: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    ToolButton4: TToolButton;
    CheckBox1: TCheckBox;
    ToolButton5: TToolButton;
    v2Column4: TcxGridDBColumn;
    Panel2: TPanel;
    v2Column5: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    Tv2Column4: TcxGridDBColumn;
    Tv2Column5: TcxGridDBColumn;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Tv2Column6: TcxGridDBColumn;
    Label7: TLabel;
    Tv2Column7: TcxGridDBColumn;
    Tv2Column8: TcxGridDBColumn;
    Tv2Column9: TcxGridDBColumn;
    Tv2Column10: TcxGridDBColumn;
    Tv2Column11: TcxGridDBColumn;
    Tv2Column12: TcxGridDBColumn;
    Tv2Column13: 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 FactoryName10Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure v2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure VYBPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column4PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv2Column6CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure Label1DblClick(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    { Private declarations }

    procedure InitGrid();
    function  SaveData():Boolean;

  public
    canshu1:string;

  end;

//var
  //frmDiTuiDJ: TfrmDiTuiDJ;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_RTFun;

{$R *.dfm}



procedure TfrmJinDuDJ.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select A.* ');
      sql.Add('  from JinDu_DJ A');
      if CheckBox1.Checked then
      begin
        sql.Add('where A.QDDate>='''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
        sql.Add(' and QDDate<'''+FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1)+''' ');
      end else
      begin
        sql.Add('where A.QDDate>=''2000-01-01'' ');
        sql.Add(' and QDDate<''2500-01-01'' ');
      end;
      sql.Add(' and A.Valid=''Y'' ');
      if Trim(canshu1)<>'高权限' then
      begin
        sql.Add(' and A.FillNo='''+Trim(DCode)+'''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;


procedure TfrmJinDuDJ.FormDestroy(Sender: TObject);
begin
  //frmDiTuiDJ:=nil;
end;

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

procedure TfrmJinDuDJ.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('1',Tv2,'进度');
  Close;
end;
procedure TfrmJinDuDJ.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add(' Update JinDu_DJ Set ValidTime=getdate(),Valider='''+Trim(DName)+''',Valid=''N'' ');
      sql.Add(' where JDID='''+Trim(CDS_HZ.fieldbyname('JDID').AsString)+'''');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_HZ.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除失败!','提示',0);
  end;
end;

procedure TfrmJinDuDJ.FormShow(Sender: TObject);
begin
  ReadCxGrid('1',Tv2,'进度');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=SGetServerDateMBeg(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmJinDuDJ.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

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

procedure TfrmJinDuDJ.FactoryName10Change(Sender: TObject);
begin
  ToolButton2.Click;
end;

function TfrmJinDuDJ.SaveData():Boolean;
var
  maxId,CRID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'JD','JinDu_DJ',4,1)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from JinDu_DJ where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('JDID').Value:=Trim(maxId);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('FillNo').Value:=Trim(DCode);
      FieldByName('QDDate').Value:=formatdateTIme('yyyy-MM-dd',CDS_HZ.fieldbyname('QDDate').AsDateTime);
      FieldByName('Valid').Value:='Y';
      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('JDID').Value:=Trim(maxId);
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
end;

procedure TfrmJinDuDJ.ToolButton3Click(Sender: TObject);
begin
  with Self.CDS_HZ do
  begin
    Append;
    FieldByName('Filler').Value:=Trim(DName);
    FieldByName('QDDate').Value:=SGetServerDate(ADOQueryTemp);
    Post;
  end;
  Self.SaveData();
end;

procedure TfrmJinDuDJ.v2Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;

end;

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

procedure TfrmJinDuDJ.ToolButton4Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  begdate.SetFocus;
  if Application.MessageBox('确定要保存当前选中的数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JinDu_DJ where JDID='''+Trim(CDS_HZ.fieldbyname('JDID').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'JinDu_DJ',7);
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JinDu_DJ Set ');
      if Trim(CDS_HZ.fieldbyname('SSDate').AsString)<>'' then
      begin
        sql.Add(' BegFlag=1');
      end else
      begin
        sql.Add(' BegFlag=0');
      end;
      if Trim(CDS_HZ.fieldbyname('OKDate').AsString)<>'' then
      begin
        sql.Add(' ,OKFlag=1');
      end else
      begin
        sql.Add(' ,OKFlag=0');
      end;
      if Trim(CDS_HZ.fieldbyname('TDDate').AsString)<>'' then
      begin
        sql.Add(' ,TDFlag=1');
      end else
      begin
        sql.Add(' ,TDFlag=0');
      end;
      sql.Add(',QKMoney=isnull(YSMoney,0)-isnull(SKMoney,0)');
      sql.Add(' where JDID='''+Trim(CDS_HZ.fieldbyname('JDID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmJinDuDJ.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  begdate.SetFocus;
  if Application.MessageBox('确定要批量保存当前页面的所有数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while not eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JinDu_DJ where JDID='''+Trim(CDS_HZ.fieldbyname('JDID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Edit;
          RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'JinDu_DJ',7);
          FieldByName('Editer').Value:=Trim(DName);
          FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JinDu_DJ Set ');
          if Trim(CDS_HZ.fieldbyname('SSDate').AsString)<>'' then
          begin
            sql.Add(' BegFlag=1');
          end else
          begin
            sql.Add(' BegFlag=0');
          end;
          if Trim(CDS_HZ.fieldbyname('OKDate').AsString)<>'' then
          begin
            sql.Add(' ,OKFlag=1');
          end else
          begin
            sql.Add(' ,OKFlag=0');
          end;
          if Trim(CDS_HZ.fieldbyname('TDDate').AsString)<>'' then
          begin
            sql.Add(' ,TDFlag=1');
          end else
          begin
            sql.Add(' ,TDFlag=0');
          end;
          sql.Add(',QKMoney=isnull(YSMoney,0)-isnull(SKMoney,0)');
          sql.Add(' where JDID='''+Trim(CDS_HZ.fieldbyname('JDID').AsString)+'''');
          ExecSQL;
        end;
        next;
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmJinDuDJ.VYBPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KF';
      flagname:='开发';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('KF').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJinDuDJ.v2Column4PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='LeiXing';
      flagname:='类型';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('LeiXing').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJinDuDJ.Tv2Column6CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id:Integer;
begin
   Id:=TV2.GetColumnByFieldName('ColorName').Index;//;-TV1.GroupedItemCount;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id]='蓝色' then
  begin
    ACanvas.Brush.Color:=clBlue;
  end else
  if AViewInfo.GridRecord.Values[Id]='红色' then
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  if AViewInfo.GridRecord.Values[Id]='黄色' then
  begin
    ACanvas.Brush.Color:=clYellow;
  end else
  if AViewInfo.GridRecord.Values[Id]='粉色' then
  begin
    ACanvas.Brush.Color:=clFuchsia;
  end;
end;

procedure TfrmJinDuDJ.Label1DblClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JinDu_DJ Set ColorName='''+Trim(TLabel(Sender).Hint)+'''');
    sql.Add(' where JDID='''+Trim(CDS_HZ.fieldbyname('JDID').AsString)+'''');
    ExecSQL;
  end;
  with CDS_HZ do
  begin
    edit;
    FieldByName('ColorName').Value:=Trim(TLabel(Sender).Hint);
    Post;
  end;
end;

procedure TfrmJinDuDJ.cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='XS';
      flagname:='销售';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('XS').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJinDuDJ.v2Column3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SS';
      flagname:='实施';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('SS').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
