unit U_SingleCustStatement;

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, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  dxBarBuiltInMenu, dxSkinWXI, dxScrollbarAnnotations;

type
  TfrmSingleCustStatement = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    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;
    TV1: TcxGridDBBandedTableView;
    V1Column2: TcxGridDBBandedColumn;
    V1Column4: TcxGridDBBandedColumn;
    V1Column5: TcxGridDBBandedColumn;
    V1Column6: TcxGridDBBandedColumn;
    V1Column7: TcxGridDBBandedColumn;
    V1Column8: TcxGridDBBandedColumn;
    V1Column9: TcxGridDBBandedColumn;
    V1Column12: TcxGridDBBandedColumn;
    V1Column15: TcxGridDBBandedColumn;
    V1Column3: TcxGridDBBandedColumn;
    V1Column10: TcxGridDBBandedColumn;
    V1Column14: TcxGridDBBandedColumn;
    V1Column16: TcxGridDBBandedColumn;
    V1Column17: TcxGridDBBandedColumn;
    V1Column20: TcxGridDBBandedColumn;
    V1Column21: TcxGridDBBandedColumn;
    V1Column22: TcxGridDBBandedColumn;
    V1Column23: TcxGridDBBandedColumn;
    V1Column25: TcxGridDBBandedColumn;
    V1Column26: TcxGridDBBandedColumn;
    V1Column33: TcxGridDBBandedColumn;
    V1Column34: TcxGridDBBandedColumn;
    OppCoName: TEdit;
    ToolButton2: TToolButton;
    TV1Column1: TcxGridDBBandedColumn;
    TV1Column2: TcxGridDBBandedColumn;
    TV1Column3: TcxGridDBBandedColumn;
    TV1Column4: TcxGridDBBandedColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    TV1Column5: TcxGridDBBandedColumn;
    cxGrid1: TcxGrid;
    TV11: TcxGridDBTableView;
    v2CRTime: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    Tv1Column23: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2FactoryName: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v2OrderNo: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column24: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    Tv1Column28: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    TV11Column1: TcxGridDBColumn;
    TV11Column2: TcxGridDBColumn;
    TV11Column3: TcxGridDBColumn;
    TV11Column4: TcxGridDBColumn;
    TV11Column5: TcxGridDBColumn;
    TV11is_checked: TcxGridDBColumn;
    TV11ssel: TcxGridDBColumn;
    TBVerify: TToolButton;
    TBCancelVerify: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure P_OrderNo_YSK(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBVerifyClick(Sender: TObject);
    procedure TBCancelVerifyClick(Sender: TObject);
    procedure TV11CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
  private
    { Private declarations }

    procedure InitGrid();

    procedure UpdateCheckedStatus(Checked: Boolean);
  public
    { Public declarations }
    FOppCoNo, FOurCoNo, FCurrency: string;
  end;

var
  frmSingleCustStatement: TfrmSingleCustStatement;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}
procedure SplitDelimited(const S: string; Delimiter: Char; List: TStrings);
var
  i: Integer;
  SL: TStringList;
begin
  SL := TStringList.Create;
  try
    SL.StrictDelimiter := True;
    SL.Delimiter := Delimiter;
    SL.DelimitedText := S;
    for i := 0 to SL.Count - 1 do
      List.Add(SL[i]);
  finally
    SL.Free;
  end;
end;

procedure TfrmSingleCustStatement.InitGrid();
begin

  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    Filtered := False;
    sql.Add(' exec  P_Fin_CustStatementSingle  ');
    sql.Add('  @Begdate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
    sql.Add(' ,@Enddate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));
    sql.Add(' ,@OppCoNo=' + QuotedStr(Trim(FOppCoNo)));
    sql.Add(' ,@OurCoNo=' + QuotedStr(Trim(FOurCoNo)));
    sql.Add(' ,@Currency=' + QuotedStr(Trim(FCurrency)));
//    showmessage(sql.text);
    Open;
  end;
  SCreateCDS(ADOQueryMain, CDS_HZ);
  SInitCDSData(ADOQueryMain, CDS_HZ);
end;

procedure TfrmSingleCustStatement.FormDestroy(Sender: TObject);
begin
  frmSingleCustStatement := nil;
end;

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

procedure TfrmSingleCustStatement.TBVerifyClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if MessageDlg('确认将选中的记录标记为已核对吗？', mtConfirmation, [mbYes, mbNo], 0) <> mrYes then Exit;
  UpdateCheckedStatus(True);
end;

procedure TfrmSingleCustStatement.TBCancelVerifyClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if MessageDlg('确认将选中的记录取消核对吗？', mtConfirmation, [mbYes, mbNo], 0) <> mrYes then Exit;
  UpdateCheckedStatus(False);
end;

procedure TfrmSingleCustStatement.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmSingleCustStatement.FormShow(Sender: TObject);
begin
//  readCxBandedGrid(trim(self.Caption), Tv11, '应收款对账单');
//  ReadCxGrid(trim(Self.Caption), Tv11, '应收款对账单');

  InitGrid();
end;

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



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

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

procedure TfrmSingleCustStatement.ToolButton2Click(Sender: TObject);
begin
//  WriteCxBandedGrid(trim(self.Caption), Tv11, '应收款对账单');
  WriteCxGrid(trim(Self.Caption), Tv11, '应收款对账单');
end;

procedure TfrmSingleCustStatement.FormCreate(Sender: TObject);
begin
  cxGrid2.Align := AlClient;
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime;
end;

procedure TfrmSingleCustStatement.ToolButton3Click(Sender: TObject);
var
  filepath: string;
  mBegdate, mEnddate: string;
begin
  mBegdate := FormatDateTime('yyyy-MM-dd', begdate.Date);
  mEnddate := FormatDateTime('yyyy-MM-dd', enddate.Date);
  if CDS_HZ.IsEmpty then
    exit;

  filepath := ExtractFilePath(Application.ExeName) + 'report\应收款明细对账单.rmf';
  if not FileExists(Pchar(filepath)) then
  begin
    application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
    exit;
  end;
  RMGridReport1.LoadFromFile(filepath);
  RMGridReport1.ShowReport;

end;

procedure TfrmSingleCustStatement.TV11CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  isCheckedCol: TcxGridColumn;
  isCheckedVal: Variant;
begin
  try
    isCheckedCol := TV11.GetColumnByFieldName('is_checked');
    if isCheckedCol = nil then Exit;

    isCheckedVal := AViewInfo.GridRecord.Values[isCheckedCol.Index];

    if not VarIsNull(isCheckedVal) and (isCheckedVal = True) then
    begin
      ACanvas.Brush.Color := clMoneyGreen;
    end;
  except
    on E: Exception do
      OutputDebugString(PChar('TV11CustomDrawCell错误: ' + E.Message));
  end;
end;

procedure TfrmSingleCustStatement.UpdateCheckedStatus(Checked: Boolean);
var
  FlowIDList, InvoiceIDList, TempIDs: string;
  FlowIDsAll, InvoiceIDsAll: TStringList;
  vBookmark: TBookmark;
  targetValue: Integer;
begin
  if CDS_HZ.IsEmpty then Exit;

  targetValue := Ord(Checked);

  FlowIDsAll := TStringList.Create;
  InvoiceIDsAll := TStringList.Create;
  try
    CDS_HZ.DisableControls;
    vBookmark := CDS_HZ.Bookmark;
    try
      CDS_HZ.First;
      while not CDS_HZ.Eof do
      begin
        if CDS_HZ.FieldByName('ssel').AsBoolean then
        begin
          if not CDS_HZ.FieldByName('flow_ids').IsNull then
          begin
            TempIDs := CDS_HZ.FieldByName('flow_ids').AsString;
            SplitDelimited(TempIDs, ',', FlowIDsAll);
          end;
          if not CDS_HZ.FieldByName('invoice_ids').IsNull then
          begin
            TempIDs := CDS_HZ.FieldByName('invoice_ids').AsString;
            SplitDelimited(TempIDs, ',', InvoiceIDsAll);
          end;
        end;
        CDS_HZ.Next;
      end;
    finally
      CDS_HZ.Bookmark := vBookmark;
      CDS_HZ.EnableControls;
    end;

    FlowIDsAll.Sorted := True;
    FlowIDsAll.Duplicates := dupIgnore;
    InvoiceIDsAll.Sorted := True;
    InvoiceIDsAll.Duplicates := dupIgnore;

    if FlowIDsAll.Count > 0 then
    begin
      FlowIDList := FlowIDsAll.CommaText;
      ADOQueryCmd.Close;
      ADOQueryCmd.SQL.Text :=
        'UPDATE FF SET is_checked = :p ' +
        'FROM Finance_Flow FF ' +
        'INNER JOIN dbo.F_Tool_SplitString(:ids, '','') S ON FF.FFID = S.RTValue';
      ADOQueryCmd.Parameters.ParamByName('p').Value := targetValue;
      ADOQueryCmd.Parameters.ParamByName('ids').Value := FlowIDList;
      ADOQueryCmd.ExecSQL;
    end;

    if InvoiceIDsAll.Count > 0 then
    begin
      InvoiceIDList := InvoiceIDsAll.CommaText;
      ADOQueryCmd.Close;
      ADOQueryCmd.SQL.Text :=
        'UPDATE FI SET is_checked = :p ' +
        'FROM Finance_Invoice FI ' +
        'INNER JOIN dbo.F_Tool_SplitString(:ids, '','') S ON FI.IVId = S.RTValue';
      ADOQueryCmd.Parameters.ParamByName('p').Value := targetValue;
      ADOQueryCmd.Parameters.ParamByName('ids').Value := InvoiceIDList;
      ADOQueryCmd.ExecSQL;
    end;

  finally
    FlowIDsAll.Free;
    InvoiceIDsAll.Free;
  end;

  InitGrid();
end;

end.

