unit U_YSPBCGLIST;

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, cxDropDownEdit, RM_e_Xls, cxGridBandedTableView,
  cxGridDBBandedTableView, BtnEdit, cxPC, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu;

type
  TFRMYSPBCGLIST = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    MPRTCodeName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    Label3: TLabel;
    OrderNo: TEdit;
    cxGrid2: TcxGrid;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Label4: TLabel;
    OrdDefNote3: TComboBox;
    Label5: TLabel;
    ToolButton3: TToolButton;
    RMGridReport1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    ADOQueryPrint: TADOQuery;
    Panetime: TPanel;
    TV1: TcxGridDBBandedTableView;
    V1Column2: TcxGridDBBandedColumn;
    V1Column5: TcxGridDBBandedColumn;
    V1Column7: TcxGridDBBandedColumn;
    V1Column8: TcxGridDBBandedColumn;
    V1Column9: TcxGridDBBandedColumn;
    factoryName: TBtnEditA;
    V1Column10: TcxGridDBBandedColumn;
    V1Column13: TcxGridDBBandedColumn;
    cxTabControl1: TcxTabControl;
    Label7: TLabel;
    YFGSType: TComboBox;
    V1Column18: TcxGridDBBandedColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label8: TLabel;
    ishs: TComboBox;
    V1Column1: TcxGridDBBandedColumn;
    cpname: TEdit;
    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 P_OrderNo_YSK(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure factoryNameBtnClick(Sender: TObject);
    procedure V1Column9CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure cxTabControl1Change(Sender: TObject);
    procedure cpnameChange(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
  public
    { Public declarations }
    RKFlag, FCYID: string;
    fType: string;
  end;

var
  FRMYSPBCGLIST: TFRMYSPBCGLIST;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ProductOrderNewList, U_ZdyAttachment;

{$R *.dfm}

procedure TFRMYSPBCGLIST.InitGrid();
begin
//  if trim(factoryName.Text) = '' then
//  begin
//    application.MessageBox('请输入客户，以便提高查询速度！', '提示信息', MB_ICONERROR);
//    exit;
//  end;
//  if trim(YFGSType.Text) = '' then
//  begin
//    application.MessageBox('请选择应收类别，以便提高查询速度！', '提示信息', MB_ICONERROR);
//    exit;
//  end;
  screen.Cursor := crsqlwait;
  Panetime.Visible := true;
  Panetime.Left := (Width - Panetime.Width) div 2;
  Panetime.top := (Height - Panetime.Height - 200) div 2;
  application.ProcessMessages;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
      sql.Add(' select A.FactoryName,b.CPName,a.crtime,B.Qty,b.price,B.PS,b.mxmoney,B.QtyUnit from YF_Money_CR A  ');
      SQL.Add('inner join YF_Money_CR_Sub B on A.YFID=b.YFID where ');
      SQL.Add('CRFlag=''应付付'' and CPName<>'''' and Qty>0 and CRType=''应付款登记'' and status=''1''');

      sql.Add(' AND crtime>' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
      sql.Add('AND crtime<=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));
      SQL.Add('ORDER BY cpname,FactoryName,crtime');

//      case cxTabControl1.TabIndex of
//        0:
//          begin
//            sql.Add('and status=''已审核'' ');
//          end;
//        1:
//          begin
//            sql.Add(',@flag=''全部'' ');
//          end;
//      end;
     // sql.Add(',@YFGSType=' + quotedstr(Trim(YFGSType.Text)));
      //sql.Add(',@ISHS=' + quotedstr(Trim(ISHS.Text)));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
//    ToolButton2.Click;
    Panetime.Visible := false;
    screen.Cursor := crdefault;
  end;
end;

procedure TFRMYSPBCGLIST.FormDestroy(Sender: TObject);
begin
  FRMYSpbcgLIST := nil;
end;

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

procedure TFRMYSPBCGLIST.TBCloseClick(Sender: TObject);
begin
  WriteCxBandedGrid(self.Caption + '2', Tv1, '应收销售表');
  Close;
end;

procedure TFRMYSPBCGLIST.FormShow(Sender: TObject);
begin
  readCxBandedGrid(self.Caption + '2', Tv1, '应收销售表');

end;

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

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

procedure TFRMYSPBCGLIST.P_OrderNo_YSK(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TFRMYSPBCGLIST.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption + '-' + trim(factoryName.Text), cxgrid2);
end;

procedure TFRMYSPBCGLIST.OrderNoChange(Sender: TObject);
begin
  if Length(Trim(OrderNo.Text)) < 3 then
    Exit;
  ToolButton2.Click;
end;

procedure TFRMYSPBCGLIST.MPRTCodeNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TFRMYSPBCGLIST.FormCreate(Sender: TObject);
var
  fsj: string;
begin
  cxGrid2.Align := AlClient;
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime;
  fsj := 'select distinct(YFGSType) name from YF_Money_CR where CRType=''应收款登记'' ';
  SInitComBoxBySql(ADOQueryCmd, YFGSType, False, fsj);
end;

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

  if CDS_HZ.IsEmpty then
    exit;
  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['ishs'] := Trim(ishs.Text);

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

    RMGridReport1.LoadFromFile(filepath);
   // RMGridReport1.PrintReport;
    RMGridReport1.ShowReport;

  finally

  end;

end;

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

procedure TFRMYSPBCGLIST.V1Column9CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if ARow1.Values[V1Column10.Index] = ARow2.Values[V1Column10.Index] then
    AAreEqual := True
  else
    AAreEqual := False;
end;

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

procedure TFRMYSPBCGLIST.cpnameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

end.

