unit U_InvoiceOutSQList;

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, dxDateRanges, U_BaseList,
  Vcl.Clipbrd, cxContainer, dxCore, cxDateUtils, dxScrollbarAnnotations,
  FrameDateSel, dxSkinsCore, dxSkinsDefaultPainters, cxProgressBar;

type
  TfrmInvoiceOutSQList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    F_Name: TcxTextEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    IVNo: TcxTextEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_Invoice: TDataSource;
    CDS_Invoice: TClientDataSet;
    TADD: TToolButton;
    TUPDATE: TToolButton;
    TBDEL: TToolButton;
    v1InvoiceType: TcxGridDBColumn;
    v1InvoiceDate: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    F_Spec: TcxTextEdit;
    Label5: TLabel;
    OppCoName: TcxTextEdit;
    TCHK: TToolButton;
    TNOCHK: TToolButton;
    cxTabControl1: TcxTabControl;
    TSel: TToolButton;
    v1Column2: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton4: TToolButton;
    v1Column3: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    Label7: TLabel;
    OurCoName: TcxTextEdit;
    Label8: TLabel;
    Label9: TLabel;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    ToolButton3: TToolButton;
    N3: TMenuItem;
    Tv1Column1: TcxGridDBColumn;
    ToolButton5: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    frmFrameDateSel1: TfrmFrameDateSel;
    btnWC: TToolButton;
    btnWCCX: 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 btnWCClick(Sender: TObject);
    procedure btnWCCXClick(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_InvoiceOutSQInPut, U_LabelPrint,
  U_AttachmentUpload;

{$R *.dfm}

procedure TfrmInvoiceOutSQList.SetStatus();
begin
  tchk.Visible := false;
  tnochk.Visible := false;
  TUPDATE.Visible := false;
  tbdel.Visible := false;
  btnWC.Visible := false;
  btnWCCX.Visible := false;
  if trim(canshu1) = '录入' then
  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;
  if trim(canshu1) = '管理' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          TUPDATE.Visible := true;
          tbdel.Visible := true;
          TCHK.Visible := true;
        end;
      1:
        begin
          tnochk.Visible := true;
        end;

    end;
  end;
  if trim(canshu1) = '完成' then
  begin
    case cxTabControl1.TabIndex of
      1:
        begin
          btnWC.Visible := true;
        end;
      2:
        begin
          btnWCCX.Visible := true;
        end;
    end;
  end;
end;

procedure TfrmInvoiceOutSQList.InitGrid();
var
  Msql, mbegdate, menddate: string;
  SqlStr: string;
begin

  SqlStr := '  select A.*  ';
  SqlStr := SqlStr + ',FJFlag=CAST((CASE WHEN (SELECT COUNT(X.FileName) FROM FJ_File X where X.TFType=''销项发票'' and X.WBID=A.IVID)>0 THEN 1 ELSE 0 END) AS BIT)';
  SqlStr := SqlStr + ' from Finance_Invoice_Apply A ';
  SqlStr := SqlStr + ' left join Bs_Company B on A.OppCoNo=B.CoCode ';

  SqlStr := SqlStr + 'where  IVFlag=''销项'' ';
  SqlStr := SqlStr + ' and A.IVDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)) + '''';
  SqlStr := SqlStr + ' and A.IVDate<''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)) + '''';

  case cxTabControl1.TabIndex of
    0:
      begin
        SqlStr := SqlStr + ' and isnull(A.status,''0'')=''0'' ';
      end;
    1:
      begin
        SqlStr := SqlStr + ' and isnull(A.status,''0'')=''9'' ';
      end;
    2:
      begin
        SqlStr := SqlStr + ' and isnull(A.status,''0'')=''10'' ';
      end;

  end;

  if trim(canshu1) = '录入' then
    SqlStr := SqlStr + ' and CHARINDEX(' + quotedstr(DCode) + ',(ISNULL(B.SalesId, '''') +'',''+ISNULL(B.TallyId, '''')+'',''+ISNULL(A.FillId, '''') )  ) > 0 ';



//  ShowMessage(SqlStr);
  InitCDSData(ADOQueryMain, CDS_Invoice, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'IVId');
end;

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

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

procedure TfrmInvoiceOutSQList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

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

  SetStatus();
  InitGrid();
end;

procedure TfrmInvoiceOutSQList.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmInvoiceOutSQList.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 TfrmInvoiceOutSQList.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '财务管理');
end;

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

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

procedure TfrmInvoiceOutSQList.TADDClick(Sender: TObject);
begin
  frmInvoiceOutSQInPut := TfrmInvoiceOutSQInPut.Create(self);
  with frmInvoiceOutSQInPut do
  begin
    FIVID := '';
    FZKType := Self.FZKType;
    canshu1 := Self.canshu1;
    if showmodal = 1 then
    begin
      initGrid();
    end;
    free;
  end;

end;

procedure TfrmInvoiceOutSQList.TUPDATEClick(Sender: TObject);
begin
  frmInvoiceOutSQInPut := TfrmInvoiceOutSQInPut.Create(self);
  with frmInvoiceOutSQInPut do
  begin
    IvNo.Enabled := False;
    FIVID := trim(Self.CDS_Invoice.fieldbyname('IVID').asstring);
    canshu1 := Self.canshu1;
    if showmodal = 1 then
    begin
      Self.initGrid();
    end;
    free;
  end;

end;

procedure TfrmInvoiceOutSQList.TBDELClick(Sender: TObject);
var
  FIVIDS: string;
begin
  if CDS_Invoice.IsEmpty then
    exit;

  if not CDS_Invoice.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  if application.MessageBox('确定要删除发票吗？', '提示', 1) = 2 then
    exit;
  with CDS_Invoice do
  begin
    DisableControls;
    while CDS_Invoice.Locate('SSel', True, []) do
    begin
      FIVIDS := FIVIDS + Trim(CDS_Invoice.fieldbyname('IVID').AsString) + ',';

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        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;

      CDS_Invoice.Delete;
    end;
    EnableControls;
  end;

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('delete from Finance_Invoice_Apply');
      sql.add('where  IVFlag=''销项''  ');
      sql.add('and exists(select * from [dbo].[F_Tool_SplitString](' + quotedstr(FIVIDS) + ','','') X where X.RTVALUE=Finance_Invoice_Apply.IVID  )');
      execsql;
    end;
    application.MessageBox('删除成功', '提示');
  except
    application.MessageBox('删除失败', '提示');
    InitGrid();
  end;
end;

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

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

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

  FIVIDS := SelCDSKey(CDS_Invoice, ['IVID'])[0];

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update Finance_Invoice_Apply set Chker=''' + trim(DName) + ''',Chktime=getdate(),status=''9'' ');
      sql.add('where  IVFlag=''销项''  ');
      sql.add('and exists(select * from [dbo].[F_Tool_SplitString](' + quotedstr(FIVIDS) + ','','') X where X.RTVALUE=Finance_Invoice_Apply.IVID  )');
      execsql;
    end;
    application.MessageBox('审核成功', '提示');
    InitGrid();
  except
    application.MessageBox('审核失败', '提示');
    InitGrid();
  end;

end;

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

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

  FIVIDS := SelCDSKey(CDS_Invoice, ['IVID'])[0];

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update Finance_Invoice_Apply set Chker=null,Chktime=null,status=''0'' ');
      sql.add('where  IVFlag=''销项''  ');
      sql.add('and exists(select * from [dbo].[F_Tool_SplitString](' + quotedstr(FIVIDS) + ','','') X where X.RTVALUE=Finance_Invoice_Apply.IVID  )');
      execsql;
    end;
    application.MessageBox('撤销成功', '提示');
    InitGrid();
  except
    application.MessageBox('撤销失败', '提示');
    InitGrid();
  end;
end;

procedure TfrmInvoiceOutSQList.TSelClick(Sender: TObject);
begin
  frmInvoiceOutSQInPut := TfrmInvoiceOutSQInPut.Create(self);
  with frmInvoiceOutSQInPut do
  begin
    TSave.Visible := False;
    FIvID := trim(Self.CDS_Invoice.fieldbyname('IvID').asstring);
    if showmodal = 1 then
    begin

    end;
  end;
end;

procedure TfrmInvoiceOutSQList.FormCreate(Sender: TObject);
begin
  inherited;
  canshu1 := trim(self.fParameters1);
end;

procedure TfrmInvoiceOutSQList.N1Click(Sender: TObject);
begin

  SelOKNo(CDS_Invoice, True);

end;

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

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

end;

procedure TfrmInvoiceOutSQList.ToolButton4Click(Sender: TObject);
var
  MIvID: string;
begin
  if CDS_Invoice.IsEmpty then
    Exit;
  if CDS_Invoice.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  MIvID := '';
  CDS_Invoice.DisableControls;
  with CDS_Invoice do
  begin
    First;
    while CDS_Invoice.Locate('SSel', True, []) do
    begin

      MIvID := MIvID + ',' + (Trim(CDS_Invoice.fieldbyname('IvID').AsString));

      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  CDS_Invoice.EnableControls;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'InvoiceOut';
      FFiltration1 := MIvID;
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmInvoiceOutSQList.ToolButton5Click(Sender: TObject);
begin
  if CDS_Invoice.IsEmpty then
    Exit;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
      fkeyNO := Trim(Self.CDS_Invoice.fieldbyname('IVID').AsString);
      fType := '销项发票';
      FEditAuthority := True;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;
end;

procedure TfrmInvoiceOutSQList.btnWCClick(Sender: TObject);
var
  FIVIDS: string;
begin
  if CDS_Invoice.IsEmpty then
    exit;

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

  FIVIDS := SelCDSKey(CDS_Invoice, ['IVID'])[0];

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update Finance_Invoice_Apply set Comper=''' + trim(DName) + ''',Comptime=getdate(),status=''10'' ');
      sql.add('where  IVFlag=''销项''  ');
      sql.add('and exists(select * from [dbo].[F_Tool_SplitString](' + quotedstr(FIVIDS) + ','','') X where X.RTVALUE=Finance_Invoice_Apply.IVID  )');
      execsql;
    end;
    application.MessageBox('审核成功', '提示');
    InitGrid();
  except
    application.MessageBox('审核失败', '提示');
    InitGrid();
  end;

end;

procedure TfrmInvoiceOutSQList.btnWCCXClick(Sender: TObject);
var
  FIVIDS: string;
begin
  if CDS_Invoice.IsEmpty then
    exit;

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

  FIVIDS := SelCDSKey(CDS_Invoice, ['IVID'])[0];

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update Finance_Invoice_Apply set Comper=null,Comptime=null,status=''9'' ');
      sql.add('where  IVFlag=''销项''  ');
      sql.add('and exists(select * from [dbo].[F_Tool_SplitString](' + quotedstr(FIVIDS) + ','','') X where X.RTVALUE=Finance_Invoice_Apply.IVID  )');
      execsql;
    end;
    application.MessageBox('撤销成功', '提示');
    InitGrid();
  except
    application.MessageBox('撤销失败', '提示');
    InitGrid();
  end;
end;

procedure TfrmInvoiceOutSQList.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.

