unit U_YSCRMXList;

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, cxDropDownEdit, Menus, BtnEdit, cxLookAndFeels,
  cxLookAndFeelPainters, dxBarBuiltInMenu, cxNavigator;

type
  TfrmYSCRMXList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    YFName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    cxTabControl1: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    CDS_YFS: TClientDataSet;
    DS_YFS: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    chk1: TCheckBox;
    CPCode: TEdit;
    Label7: TLabel;
    ishs: TComboBox;
    v1Column16: TcxGridDBColumn;
    Label5: TLabel;
    CPName: TEdit;
    CheckBox1: TCheckBox;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    ToolButton3: TToolButton;
    FactoryNameSel: TBtnEditA;
    ADOQueryPrint: TADOQuery;
    FactoryName: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column23: 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 ToolButton2Click(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure YFGSTypeChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure chk1Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure FactoryNameSelBtnClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    procedure SetStatus();
  public

    { Public declarations }
    FCYID: string;
    fmanage: string;
  end;

var
  frmYSCRMXList: TfrmYSCRMXList;

implementation

uses
  U_DataLink, U_Fun, U_SXCKNewList, U_YSSCList, U_YsScXsList, U_YSKInput,
  U_XXFPHXList, U_ZdyAttachment;

{$R *.dfm}
procedure TfrmYSCRMXList.SetStatus();
begin

end;

procedure TfrmYSCRMXList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*,B.* from YF_Money_CR A');
      sql.Add(' inner join  YF_Money_CR_SUB B on A.yfid=B.yfid');
      sql.Add(' where A.CRTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
      sql.Add(' and CRTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      sql.Add(' and A.CRType=''应收款登记''  ');
      if chk1.Checked then
      begin
        sql.Add(' and B.Price=0  ');
      end;
      if cxTabControl1.TabIndex < 2 then
        sql.Add(' and A.status=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_YFS);
    SInitCDSData20(ADOQueryMain, CDS_YFS);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmYSCRMXList.FormDestroy(Sender: TObject);
begin
  frmYSCRMXList := nil;
end;

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

procedure TfrmYSCRMXList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '财务管理');

  Close;
end;

procedure TfrmYSCRMXList.FormShow(Sender: TObject);
var
  fsj: string;
begin

  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '财务管理');

//  fsj := 'select distinct(YFGSType) name from YF_Money_CR where CRType=''应收款登记'' ';
//  SInitComBoxBySql(ADOQueryCmd, YFGSType, False, fsj);
  SetStatus();
  InitGrid();
end;

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

procedure TfrmYSCRMXList.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    FactoryName.Text := Trim(FactoryNameSel.Text);
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_YFS);
    SInitCDSData20(ADOQueryMain, CDS_YFS);
  end;
end;

procedure TfrmYSCRMXList.FactoryNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYSCRMXList.Button2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if Trim(CDS_HZ.fieldbyname('SubID').AsString) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete YCL_SG_HZ where SubID=''' + Trim(CDS_HZ.fieldbyname('SubID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmYSCRMXList.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '应收款列表');
end;

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

procedure TfrmYSCRMXList.N1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := TRUE;
      post;
      next;
    end;
    First;
    EnableControls;
  end;

end;

procedure TfrmYSCRMXList.N2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := FALSE;
      post;
      next;
    end;
    First;
    EnableControls;
  end;

end;

procedure TfrmYSCRMXList.YFGSTypeChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYSCRMXList.Tv1DblClick(Sender: TObject);
begin
  frmXXFPHXList := TfrmXXFPHXList.create(self);
  with frmXXFPHXList do
  begin
    FTONO := TRIM(CDS_YFS.Fieldbyname('YFSID').AsString);
    FFromNo := '';
    if showmodal = 1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmYSCRMXList.chk1Click(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

procedure TfrmYSCRMXList.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked then
    CPCode.Tag := 1
  else
    CPCode.Tag := 2;

  ToolButton2.Click;
end;

procedure TfrmYSCRMXList.FactoryNameSelBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        FactoryNameSel.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        FactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        Self.InitGrid();
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmYSCRMXList.ToolButton3Click(Sender: TObject);
var
  filepath: string;
  mBegdate, mEnddate: string;
begin

  if CDS_YFS.IsEmpty then
    exit;
  ExportFtErpFile('应收款明细.rmf', ADOQueryTemp);
  try

    filepath := ExtractFilePath(Application.ExeName) + 'report\应收款明细.rmf';

    if not FileExists(Pchar(filepath)) then
    begin
      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
      exit;
    end;

    RmVariables['begDate'] := FormatDateTime('yyyy-mm-dd', begDate.Date);
    RmVariables['enddate'] := FormatDateTime('yyyy-mm-dd', enddate.Date);
    RMVariables['zdr'] := trim(DName);
    RmVariables['begDate'] := FormatDateTime('yymmdd', begDate.Date) + '-' + FormatDateTime('yymmdd', enddate.Date);

    RM1.LoadFromFile(filepath);
    RM1.ShowReport;

  finally

  end;

end;

procedure TfrmYSCRMXList.FormCreate(Sender: TObject);
begin
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime;
end;

end.

