unit U_CustStatementWriteOff;

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, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator,   dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, DateUtils, cxContainer, Vcl.Menus,
  cxCurrencyEdit, System.Math, System.Character, Vcl.Clipbrd, dxSkinsCore,
  dxSkinsDefaultPainters, dxScrollbarAnnotations, cxProgressBar;

type
  TfrmCustStatementWriteOff = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    MPRTCodeName: TcxTextEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    Label3: TLabel;
    OrderNo: TcxTextEdit;
    cxGrid2: TcxGrid;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Label4: TLabel;
    OrdDefNote3: TComboBox;
    RMGridReport1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    ADOQueryPrint: TADOQuery;
    Panetime: TPanel;
    TV1: TcxGridDBBandedTableView;
    V1Column1: TcxGridDBBandedColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    TV1Column1: TcxGridDBBandedColumn;
    TV1Column2: TcxGridDBBandedColumn;
    TV1Column3: TcxGridDBBandedColumn;
    TV1Column4: TcxGridDBBandedColumn;
    TV1Column5: TcxGridDBBandedColumn;
    TV1Column6: TcxGridDBBandedColumn;
    TV1Column7: TcxGridDBBandedColumn;
    TV1Column8: TcxGridDBBandedColumn;
    TV1Column9: TcxGridDBBandedColumn;
    TV1Column10: TcxGridDBBandedColumn;
    TV1Column11: TcxGridDBBandedColumn;
    TV1Column12: TcxGridDBBandedColumn;
    TV1Column13: TcxGridDBBandedColumn;
    TV1Column14: TcxGridDBBandedColumn;
    TV1Column15: TcxGridDBBandedColumn;
    TV1Column16: TcxGridDBBandedColumn;
    TV1Column17: TcxGridDBBandedColumn;
    TV1Column18: TcxGridDBBandedColumn;
    TV1Column19: TcxGridDBBandedColumn;
    TV1Column20: TcxGridDBBandedColumn;
    TV1Column21: TcxGridDBBandedColumn;
    TV1Column22: TcxGridDBBandedColumn;
    TV1Column23: TcxGridDBBandedColumn;
    TV1Column24: TcxGridDBBandedColumn;
    TV1Column25: TcxGridDBBandedColumn;
    TV1Column26: TcxGridDBBandedColumn;
    TV1Column27: TcxGridDBBandedColumn;
    TV1Column28: TcxGridDBBandedColumn;
    TV1Column29: TcxGridDBBandedColumn;
    TV1Column30: TcxGridDBBandedColumn;
    TV1Column31: TcxGridDBBandedColumn;
    TV1Column32: TcxGridDBBandedColumn;
    TV1Column33: TcxGridDBBandedColumn;
    TV1Column34: TcxGridDBBandedColumn;
    TV1Column35: TcxGridDBBandedColumn;
    TV1Column36: TcxGridDBBandedColumn;
    TV1Column37: TcxGridDBBandedColumn;
    Label5: TLabel;
    factoryName: TcxTextEdit;
    TV1Column38: TcxGridDBBandedColumn;
    TV1Column39: TcxGridDBBandedColumn;
    TV1Column40: TcxGridDBBandedColumn;
    ToolButton4: TToolButton;
    TV1Column41: TcxGridDBBandedColumn;
    Label6: TLabel;
    FactoryNo: TcxTextEdit;
    Label7: TLabel;
    FactType: TcxTextEdit;
    TV1Column42: TcxGridDBBandedColumn;
    TV1Column43: TcxGridDBBandedColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    ToolButton5: TToolButton;
    TV1Column44: TcxGridDBBandedColumn;
    TV1Column45: TcxGridDBBandedColumn;
    N2: TMenuItem;
    chkqm: TCheckBox;
    TV1Column46: TcxGridDBBandedColumn;
    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 factoryNameChange(Sender: TObject);
    procedure FKKPFactoryNameBtnDnClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    { Private declarations }
    function getNumberFromStr(strIn: string; sFlag: string): string;
    procedure InitGrid();
  public
    { Public declarations }
    RKFlag, FCYID, FZKType, FBZType: string;
    fType: string;
  end;

var
  frmCustStatementWriteOff: TfrmCustStatementWriteOff;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_SingleCustStatement;

{$R *.dfm}

function TfrmCustStatementWriteOff.getNumberFromStr(strIn: string; sFlag: string): string;
var
  i: Integer;
  tempStr: string;
begin
  tempStr := '';

  if Length(strIn) = 0 then
  begin
    Result := '';
    exit;
  end;

  for i := 1 to strIn.Length do
  begin
    if strIn[i] = sFlag then   //截取到sFlag位置结束
      Break;

    if IsNumber(strIn[i]) then    //isNumber--System.Character
    begin
      tempStr := tempStr + strIn[i];
    end;
  end;

  Result := tempStr;
end;

procedure TfrmCustStatementWriteOff.InitGrid();
begin
  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;
      sql.Add(' exec  P_Fin_CustStatementYearWriteOff  ');
      sql.Add(' @begdate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;
    ToolButton2.Click;
    Panetime.Visible := false;
    screen.Cursor := crdefault;

  end;
end;

procedure TfrmCustStatementWriteOff.FormDestroy(Sender: TObject);
begin
  inherited;
  frmCustStatementWriteOff := nil;
end;

procedure TfrmCustStatementWriteOff.factoryNameChange(Sender: TObject);
begin
  ToolButton2.Click;

end;

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

procedure TfrmCustStatementWriteOff.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmCustStatementWriteOff.FormShow(Sender: TObject);
begin
  inherited;
  readCxBandedGrid(self.Caption + '2', Tv1, '应付款对账单');
  InitGrid();
end;

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

procedure TfrmCustStatementWriteOff.ToolButton2Click(Sender: TObject);
var
  MGL: string;
begin
  if chkqm.Checked then
  begin
    MGL := ' YuEMoney<>0'
  end
  else
  begin
    MGL := '';
  end;
  if SGetFilters(Panel1, 1, 2) <> '' then
  begin
    if MGL <> '' then
      MGL := MGL + ' and ' + SGetFilters(Panel1, 1, 2)
    else
      MGL := SGetFilters(Panel1, 1, 2);
  end;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, MGL);
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmCustStatementWriteOff.ToolButton4Click(Sender: TObject);
begin
//  if CDS_HZ.IsEmpty then
//    exit;
//  try
//    frmSingleCustStatement := TfrmSingleCustStatement.Create(self);
//    with frmSingleCustStatement do
//    begin
//      begdate.DateTime := StartOfTheYear(self.begdate.DateTime);
//      enddate.DateTime := endOfTheYear(self.begdate.DateTime);
//      factoryName.Text := self.CDS_HZ.fieldbyname('factoryName').AsString;
//      factoryName.TxtCode := self.CDS_HZ.fieldbyname('factoryNo').AsString;
//      FZKType := '';
//      Show;
//    end;
//  finally
//  end;

end;

procedure TfrmCustStatementWriteOff.ToolButton5Click(Sender: TObject);
begin
  WriteCxBandedGrid(self.Caption + '2', Tv1, '应付款对账单');
end;

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

procedure TfrmCustStatementWriteOff.FKKPFactoryNameBtnDnClick(Sender: TObject);
begin

  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmCustStatementWriteOff.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid2);
end;

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

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

procedure TfrmCustStatementWriteOff.N2Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_HZ.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

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

end.

