unit U_InvoiceInList;

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,
  dxScrollbarAnnotations;

type
  TfrmInvoiceInList = 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;
    v1InvoiceNo: TcxGridDBColumn;
    v1InvoiceType: TcxGridDBColumn;
    v1InvoiceDate: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    TCHK: TToolButton;
    TNOCHK: TToolButton;
    cxTabControl1: TcxTabControl;
    TSel: TToolButton;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton4: TToolButton;
    v1Column3: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    ToolButton3: TToolButton;
    N3: TMenuItem;
    Tv1Column1: TcxGridDBColumn;
    Label3: TLabel;
    Label5: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    F_Name: TEdit;
    IVNo: TEdit;
    F_Spec: TEdit;
    OppCoName: TEdit;
    OurCoName: TEdit;
    ToolButton5: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    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);
  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_LabelPrint,
  U_AttachmentUpload;

{$R *.dfm}

procedure TfrmInvoiceInList.SetStatus();
begin
  tchk.Visible := false;
  tnochk.Visible := false;
  TUPDATE.Visible := false;
  tbdel.Visible := false;

  if trim(DParameters1) <> '高权限' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          TUPDATE.Visible := true;
          tbdel.Visible := true;
          TCHK.Visible := true;
        end;
      1:
        begin
          tnochk.Visible := true;

        end;
      2:
        begin
        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 TfrmInvoiceInList.InitGrid();
begin
  try
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*   ');
      sql.Add(',FJFlag=CAST((CASE WHEN (SELECT COUNT(X.FileName) FROM FJ_File X where X.TFType=''进项发票'' and X.WBID=A.IVNo)>0 THEN 1 ELSE 0 END) AS BIT)');

      sql.Add('  from Finance_Invoice A ');

      sql.add(' where A.IVDate>=''' + formatdatetime('yyyy-MM-dd', begdate.datetime) + ''' ');
      sql.Add(' and A.IVDate<''' + formatdateTime('yyyy-MM-dd', enddate.DateTime + 1) + ''' ');
      sql.Add(' and IVFlag=''进项'' ');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.add(' and isnull(A.Status,''0'')=''0''');
          end;
        1:
          begin
            sql.add(' and A.Status=''9''');
          end;
      end;

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

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

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

procedure TfrmInvoiceInList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

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

  SetStatus();
  InitGrid();
end;

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

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

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

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

procedure TfrmInvoiceInList.TADDClick(Sender: TObject);
begin
  frmInvoiceInInPut := TfrmInvoiceInInPut.Create(self);
  with frmInvoiceInInPut do
  begin
    FIVID := '';
    FZKType := Self.FZKType;

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

end;

procedure TfrmInvoiceInList.TUPDATEClick(Sender: TObject);
begin
  frmInvoiceInInPut := TfrmInvoiceInInPut.Create(self);
  with frmInvoiceInInPut do
  begin
    IvNo.Enabled := False;
    FIVID := trim(Self.CDS_Invoice.fieldbyname('IVID').asstring);

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

end;

procedure TfrmInvoiceInList.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');
      sql.add('where  IVFlag=''进项''  ');
      sql.add('and exists(select * from [dbo].[F_Tool_SplitString](' + quotedstr(FIVIDS) + ','','') X where X.RTVALUE=Finance_Invoice.IVID  )');
      execsql;
    end;
    application.MessageBox('删除成功', '提示');
  except
    application.MessageBox('删除失败', '提示');
    InitGrid();
  end;
end;

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

procedure TfrmInvoiceInList.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;

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

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update Finance_Invoice 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.IVID  )');
      execsql;
    end;
    application.MessageBox('审核成功', '提示');
  except
    application.MessageBox('审核失败', '提示');
    InitGrid();
  end;
end;

procedure TfrmInvoiceInList.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;

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

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update Finance_Invoice 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.IVID  )');
      execsql;
    end;
    application.MessageBox('撤销成功', '提示');

  except
    application.MessageBox('撤销失败', '提示');
    InitGrid();
  end;
end;

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

    end;
  end;
end;

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

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

end;

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

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

end;

procedure TfrmInvoiceInList.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 := 'InvoiceIn';
      FFiltration1 := MIvID;
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmInvoiceInList.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('IVNo').AsString);
      fType := '进项发票';
      FEditAuthority := True;
      if ShowModal = 1 then
      begin

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

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

