unit U_ClaimsableList;

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, cxPC, RM_e_Xls, Menus, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, U_BaseList, Vcl.Clipbrd, dxSkinWXI,
  dxScrollbarAnnotations;

type
  TfrmClaimsableList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_Invoice: TDataSource;
    CDS_Invoice: TClientDataSet;
    TADD: TToolButton;
    TUPDATE: TToolButton;
    TBDEL: TToolButton;
    v1InvoiceDate: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    Label5: TLabel;
    OppCoName: TEdit;
    TCHK: TToolButton;
    TNOCHK: TToolButton;
    cxTabControl1: TcxTabControl;
    TSel: TToolButton;
    v1Column2: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton4: TToolButton;
    v1Column3: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    Label1: TLabel;
    SYRName: TEdit;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    ToolButton3: TToolButton;
    N3: TMenuItem;
    ToolButton5: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    ToolButton6: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    ToolButton9: 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 ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure IVNoChange(Sender: TObject);
    procedure TADDClick(Sender: TObject);
    procedure TUPDATEClick(Sender: TObject);
    procedure TBDELClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TCHKClick(Sender: TObject);
    procedure TNOCHKClick(Sender: TObject);
    procedure TSelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1Column14PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    procedure SetStatus();
  public
    FZKType, canshu1, canshu2: string;
    { Public declarations }
  end;

//var
//  frmInvoiceXX: TfrmInvoiceXX;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_InvoiceInInPut, U_HandPayableInPut,
  U_HandPayableImport, U_ClaimsableInPut, U_LabelPrint;

{$R *.dfm}

procedure TfrmClaimsableList.SetStatus();
begin
  tchk.Visible := false;
  tnochk.Visible := false;
  TUPDATE.Visible := false;
  tbdel.Visible := false;
  ToolButton7.Visible := false;
  ToolButton8.Visible := false;
  if trim(DParameters1) <> '申请' then
  begin

    case cxTabControl1.TabIndex of
      0:
        begin
          TUPDATE.Visible := true;
          tbdel.Visible := true;
          if trim(DParameters1) = '审核' then
          begin
            TCHK.Visible := true;
          end;

        end;
      1:
        begin
          if trim(DParameters1) = '审核' then
          begin
            tnochk.Visible := true;
          end
          else
          begin
            ToolButton7.Visible := true;
          end;

        end;
      2:
        begin
          if trim(DParameters1) = '复核' then
          begin
            ToolButton8.Visible := true;
          end;

        end;
    end;
  end
  else
  begin

    case cxTabControl1.TabIndex of
      0:
        begin
//          tchk.Visible := true;
          TUPDATE.Visible := true;
          tbdel.Visible := true;
//          TCHK.Visible := true;
        end;
      1:
        begin
//          tnochk.Visible := true;

        end;
      2:
        begin
        end;
    end;
  end;

end;

procedure TfrmClaimsableList.InitGrid();
begin
  try
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select CONVERT(varchar,A.FFTime,23) FFTime,A.*  from Finance_Flow A ');
      sql.add(' where A.FFTime>=''' + formatdatetime('yyyy-MM-dd', begdate.datetime) + ''' ');
      sql.Add(' and A.FFTime<''' + formatdateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
      sql.Add(' and A.FFType=''索赔登记'' ');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.add(' and isnull(A.Status,''0'')=''0''');
          end;
        1:
          begin
            sql.add(' and A.Status=''9''');
          end;
        2:
          begin
            sql.add(' and A.Status=''11''');
          end;
      end;

      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_Invoice);
    SInitCDSData(ADOQueryMain, CDS_Invoice);
  finally
    TV1.DataController.Filter.Clear;
  end;
end;

procedure TfrmClaimsableList.FormDestroy(Sender: TObject);
begin
  inherited;
//  frmInvoiceXX := nil;
end;

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

procedure TfrmClaimsableList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmClaimsableList.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.Caption, Tv1, '财务管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime - 30;

  SetStatus();
  InitGrid();
end;

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

procedure TfrmClaimsableList.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_Invoice);
    SInitCDSData(ADOQueryMain, CDS_Invoice);
  end;
end;

procedure TfrmClaimsableList.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '财务管理');
end;

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

procedure TfrmClaimsableList.IVNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmClaimsableList.TADDClick(Sender: TObject);
begin
  frmClaimsableInPut := TfrmClaimsableInPut.Create(self);
  with frmClaimsableInPut do
  begin
    FFID := '';
//    FZKType := Self.FZKType;

    if showmodal = 1 then
    begin
      initGrid();
    end;
    free;
  end;

end;

procedure TfrmClaimsableList.TUPDATEClick(Sender: TObject);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Trade_Plan_Main where OrderNo = ' + QuotedStr(CDS_Invoice.FieldByName('OrderNo').AsString) + ' and status = ''10'' ');
    open;
  end;

  if not ADOQueryTemp.IsEmpty then
  begin
    application.MessageBox('计划单已完成，无法删除', '提示');
    Exit;
  end;

  frmClaimsableInPut := TfrmClaimsableInPut.Create(self);
  with frmClaimsableInPut do
  begin
//    IvNo.Enabled := False;
    FFID := trim(Self.CDS_Invoice.fieldbyname('FFID').asstring);
    if Canshu1 = '申请' then
    begin
      if Trim(Self.CDS_Invoice.fieldbyname('Filler').AsString) <> Trim(DName) then
      begin
        Application.MessageBox('不能操作他人的数据!', '提示', 0);
        Exit;
      end;
    end;

    if showmodal = 1 then
    begin
      Self.initGrid();
    end;
    free;
  end;

end;

procedure TfrmClaimsableList.TBDELClick(Sender: TObject);
begin
  if CDS_Invoice.IsEmpty then
    exit;
  if Canshu1 = '申请' then
  begin
    if Trim(CDS_Invoice.fieldbyname('Filler').AsString) <> Trim(DName) then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Trade_Plan_Main where OrderNo = ' + QuotedStr(CDS_Invoice.FieldByName('OrderNo').AsString) + ' and status = ''10'' ');
    open;
  end;

  if not ADOQueryTemp.IsEmpty then
  begin
    application.MessageBox('计划单已完成，无法删除', '提示');
    Exit;
  end;

  if application.MessageBox('确定要删除吗？', '提示', 1) = 2 then
    exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('delete from Finance_Flow where FFID=''' + trim(CDS_Invoice.fieldbyname('FFID').asstring) + ''' ');

//      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
//      sql.Add(' ' + quotedstr(trim(DName)));
//      sql.Add(',getdate() ');
//      sql.Add(',' + quotedstr(trim(self.Caption)));
//      sql.Add(',' + quotedstr(trim('手工删除')));
//      sql.Add(',' + quotedstr(trim('发票号：' + trim(CDS_Invoice.FieldByName('ivNo').AsString))));
//      sql.Add(',' + quotedstr(trim('成功')));
//      sql.Add(')');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_Invoice.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;

end;

procedure TfrmClaimsableList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  initGrid();
end;

procedure TfrmClaimsableList.TCHKClick(Sender: TObject);
var
  MFFIDS: string;
begin
  if CDS_Invoice.IsEmpty then
    exit;

  if not CDS_Invoice.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;

  with CDS_Invoice do
  begin
//    DisableControls;
    while CDS_Invoice.Locate('SSel', True, []) do
    begin
      MFFIDS := MFFIDS + Trim(CDS_Invoice.fieldbyname('FFID').AsString) + ',';
      CDS_Invoice.Delete;
    end;
//    EnableControls;
  end;

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update Finance_Flow set Chker=''' + trim(DName) + ''',Chktime=getdate(),status=''9'' ');
      sql.add('where  FFType=''索赔登记'' and EXISTS (select *  from [dbo].[F_Tool_SplitString]( ''' + trim(MFFIDS) + '''');
      sql.add(','','') X where X.RTValue=Finance_Flow.FFID) ');
//       showmessage(sql.text);

      execsql;
    end;
    application.MessageBox('审核成功', '提示');
//    InitGrid();
//    CDS_Invoice.delete;
  except
    application.MessageBox('审核失败', '提示');
  end;
end;

procedure TfrmClaimsableList.TNOCHKClick(Sender: TObject);
var
  MFFIDS: string;
begin
  if CDS_Invoice.IsEmpty then
    exit;

  if not CDS_Invoice.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;

  with CDS_Invoice do
  begin
//    DisableControls;
    while CDS_Invoice.Locate('SSel', True, []) do
    begin
      MFFIDS := MFFIDS + Trim(CDS_Invoice.fieldbyname('FFID').AsString) + ',';
      CDS_Invoice.Delete;
    end;
//    EnableControls;
  end;

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update Finance_Flow set Chker=null,Chktime=null,status=''0'' ');
      sql.add('where  FFType=''索赔登记'' and EXISTS (select *  from [dbo].[F_Tool_SplitString]( ''' + trim(MFFIDS) + '''');
      sql.add(','','') X where X.RTValue=Finance_Flow.FFID) ');

      execsql;
    end;
    application.MessageBox('撤销成功', '提示');
//    CDS_Invoice.delete;
  except
    application.MessageBox('撤销失败', '提示');
  end;
end;

procedure TfrmClaimsableList.TSelClick(Sender: TObject);
begin
  frmClaimsableInPut := TfrmClaimsableInPut.Create(self);
  with frmClaimsableInPut do
  begin
    TSave.Visible := False;
    FFID := trim(Self.CDS_Invoice.fieldbyname('FFID').asstring);
    if showmodal = 1 then
    begin

    end;
  end;
end;

procedure TfrmClaimsableList.FormCreate(Sender: TObject);
begin
  inherited;
  canshu1 := trim(DParameters1);
end;

procedure TfrmClaimsableList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Invoice, True);

end;

procedure TfrmClaimsableList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Invoice, false);
end;

procedure TfrmClaimsableList.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Invoice.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));

end;

procedure TfrmClaimsableList.ToolButton4Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  OrderKg: Double;
begin
  if CDS_Invoice.IsEmpty then
    Exit;
  if CDS_Invoice.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  CDS_Invoice.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Invoice do
    begin
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value := Trim(CDS_Invoice.fieldbyname('InvoiceNo').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_Invoice.EnableControls;

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_View_FPTJ ' + quotedstr(Trim(DCode)));
    Open;
  end;
  ExportFtErpFile('进项发票列表.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\进项发票列表.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
//    RMVariables['SYRName']:=Trim(SYRName);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找Report\进项发票列表.rmf'), '提示', 0);
  end;
end;

procedure TfrmClaimsableList.ToolButton5Click(Sender: TObject);
begin
  frmClaimsableInPut := TfrmClaimsableInPut.Create(self);
  with frmClaimsableInPut do
  begin
//    IvNo.Enabled := False;
    FFID := trim(Self.CDS_Invoice.fieldbyname('FFID').asstring);
    PState := '复制';
    if showmodal = 1 then
    begin
      Self.initGrid();
    end;
    free;
  end;

end;

procedure TfrmClaimsableList.ToolButton6Click(Sender: TObject);
begin
  try
    frmHandPayableImport := TfrmHandPayableImport.Create(Application);
    with frmHandPayableImport do
    begin
//      FCTID := Trim(Self.CDS_Tree.fieldbyname('CTID').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmHandPayableImport.Free;
  end;

end;

procedure TfrmClaimsableList.ToolButton7Click(Sender: TObject);
var
  MFFIDS: string;
begin
  if CDS_Invoice.IsEmpty then
    exit;

  if not CDS_Invoice.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;

  with CDS_Invoice do
  begin
//    DisableControls;
    while CDS_Invoice.Locate('SSel', True, []) do
    begin
      MFFIDS := MFFIDS + Trim(CDS_Invoice.fieldbyname('FFID').AsString) + ',';
      CDS_Invoice.Delete;
    end;
//    EnableControls;
  end;

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update Finance_Flow set FChker=''' + trim(DName) + ''',FChktime=getdate(),status=''11'' ');
      sql.add('where  FFType=''索赔登记'' and EXISTS (select *  from [dbo].[F_Tool_SplitString]( ''' + trim(MFFIDS) + '''');
      sql.add(','','') X where X.RTValue=Finance_Flow.FFID) ');
//       showmessage(sql.text);

      execsql;
    end;
    application.MessageBox('复核成功', '提示');
//    InitGrid();
//    CDS_Invoice.delete;
  except
    application.MessageBox('复核失败', '提示');
  end;
end;

procedure TfrmClaimsableList.ToolButton8Click(Sender: TObject);
var
  MFFIDS: string;
begin
  if CDS_Invoice.IsEmpty then
    exit;

  if not CDS_Invoice.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;

  with CDS_Invoice do
  begin
//    DisableControls;
    while CDS_Invoice.Locate('SSel', True, []) do
    begin
      MFFIDS := MFFIDS + Trim(CDS_Invoice.fieldbyname('FFID').AsString) + ',';
      CDS_Invoice.Delete;
    end;
//    EnableControls;
  end;

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update Finance_Flow set FChker=null,FChktime=null,status=''9'' ');
      sql.add('where  FFType=''索赔登记'' and EXISTS (select *  from [dbo].[F_Tool_SplitString]( ''' + trim(MFFIDS) + '''');
      sql.add(','','') X where X.RTValue=Finance_Flow.FFID) ');

      execsql;
    end;
    application.MessageBox('撤销成功', '提示');
//    CDS_Invoice.delete;
  except
    application.MessageBox('撤销失败', '提示');
  end;
end;

procedure TfrmClaimsableList.ToolButton9Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_Invoice.IsEmpty then
    Exit;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'ClaimsPrint';
      FFiltration1 := QuotedStr(Trim(Self.CDS_Invoice.fieldbyname('FFID').AsString));
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmClaimsableList.v1Column14PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FMoney, Ftax, FtaxMoney: double;
begin
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_Invoice do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    FQty := FieldbyName('Qty').AsFloat;
    FtaxMoney := FieldbyName('taxMoney').AsFloat;
    Ftax := FieldbyName('tax').AsFloat;
  end;
  if FQty <> 0 then
  begin

    FMoney := strtofloat(format('%.2f', [FtaxMoney / (1 + Ftax / 100)]));
    FPrice := strtofloat(format('%.2f', [FMoney / FQty]));
  end
  else
  begin
    FPrice := 0;
    FMoney := strtofloat(format('%.2f', [FtaxMoney / (1 + Ftax / 100)]));
  end;

  with CDS_Invoice do
  begin
    Edit;
    FieldByName('Price').Value := FPrice;
    FieldByName('Money').Value := FMoney;
    FieldByName('TaxFee').Value := FtaxMoney - FMoney;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

end.

