unit U_FDINVOICE;

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;

type
  TfrmFDINVOICE = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    P_CodeName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    InvoiceNo: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_FP: TDataSource;
    CDS_FP: TClientDataSet;
    TADD: TToolButton;
    TUPDATE: TToolButton;
    TBDEL: TToolButton;
    v1InvoiceNo: TcxGridDBColumn;
    v1InvoiceType: TcxGridDBColumn;
    v1InvoiceDate: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    P_Spec: TEdit;
    Label5: TLabel;
    FactoryName: TEdit;
    TCHK: TToolButton;
    TNOCHK: TToolButton;
    cxTabControl1: TcxTabControl;
    TSel: TToolButton;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    ToolButton3: TToolButton;
    v1Column6: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ADOQuerySub: TADOQuery;
    CDS_Sub: TClientDataSet;
    DataSource1: TDataSource;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton4: TToolButton;
    v1Column3: TcxGridDBColumn;
    CDS_Print: TClientDataSet;
    ADOQueryPrint: TADOQuery;
    Label7: TLabel;
    ToFactoryName: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    Label1: TLabel;
    SYRName: TEdit;
    Label4: TLabel;
    YFGSType: TComboBox;
    v1Column4: TcxGridDBColumn;
    TBHX: TToolButton;
    v1Column5: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    V2Column1: TcxGridDBColumn;
    v1Column8: 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 InvoiceNoChange(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 ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TBHXClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    { Private declarations }
    canshu1,canshu2:string;
    procedure InitGrid();
    procedure SetStatus();
  public
    fInvoiceFlag:string;
    { Public declarations }
  end;

var
  frmFDINVOICE: TfrmFDINVOICE;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_FDINVOICE_Sub, U_FjList10,U_JXFPHx,U_JXFPHXList;

{$R *.dfm}

procedure TfrmFDINVOICE.SetStatus();
begin
  tchk.Visible:=false;
  tnochk.Visible:=false;
  TUPDATE.Visible:=false;
  tbdel.Visible:=false;
  TBHX.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;
        TBHX.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;
       TBHX.Visible:=true;
      end;
    2:begin
      end;
    end;
  end;


  IF fInvoiceFlag='' then
  fInvoiceFlag:='1';
end;

procedure TfrmFDINVOICE.InitGrid();
begin
  try
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*,isfj=cast(case when isnull((select count(WBID) from TP_File X where X.WBID=A.invoiceNo and X.TFType=''发票''),0)>0 then 1 else 0 end as bit) ');
      sql.Add(' ,P_CodeName=cast((select distinct '',''+P_CodeName  from FD_Invoicesub X where X.invoiceNo=A.invoiceNo for xml path('''')) as varchar(80))');
      sql.Add(' ,P_Spec=cast((select distinct '',''+P_Spec  from FD_Invoicesub X where X.invoiceNo=A.invoiceNo for xml path('''')) as varchar(80))');
      sql.Add(' ,HSJE=(select SUM(taxMoney)  from FD_Invoicesub X where X.invoiceNo=A.invoiceNo) ');
      sql.Add(' from FD_Invoice A ');
      sql.add(' where A.InvoiceDate>='''+formatdatetime('yyyy-MM-dd',begdate.datetime)+''' ');
      sql.Add(' and A.InvoiceDate<'''+formatdateTime('yyyy-MM-dd',enddate.DateTime+1)+''' ');
      sql.Add(' and InvoiceFlag='''+fInvoiceFlag+''' ');
      if cxTabControl1.TabIndex=0 then
      sql.add(' and A.Chkstatus=''0''');
      if cxTabControl1.TabIndex=1 then
      sql.add(' and A.Chkstatus=''1''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_FP);
    SInitCDSData20(ADOQueryMain,CDS_FP);
  finally
    
  end;
end;


procedure TfrmFDINVOICE.FormDestroy(Sender: TObject);
begin
  frmFDINVOICE:=nil;
end;

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

procedure TfrmFDINVOICE.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('发票管理',Tv1,'财务管理');
  Close;
end;
procedure TfrmFDINVOICE.FormShow(Sender: TObject);
begin
  ReadCxGrid('发票管理',Tv1,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=Enddate.DateTime-30;
  
  SetStatus();
  InitGrid();
end;

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

procedure TfrmFDINVOICE.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_FP);
    SInitCDSData20(ADOQueryMain,CDS_FP);
  end;
end;

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

procedure TfrmFDINVOICE.InvoiceNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmFDINVOICE.TADDClick(Sender: TObject);
begin
  FrmInvoice_Sub:=TFrmInvoice_Sub.Create(self);
  with FrmInvoice_Sub do
  begin
    FInvoiceNo:='';
    fInvoiceFlag:=self.fInvoiceFlag;
    if showmodal=1 then
    begin
      initGrid();
    end;
    free;
  end;

end;

procedure TfrmFDINVOICE.TUPDATEClick(Sender: TObject);
begin
  FrmInvoice_Sub:=TFrmInvoice_Sub.Create(self);
  with FrmInvoice_Sub do
  begin
    InvoiceNo.Enabled:=False;
    FInvoiceNo:=trim(CDS_FP.fieldbyname('InvoiceNo').asstring);
    fInvoiceFlag:=self.fInvoiceFlag;
    if showmodal=1 then
    begin
        initGrid();
    end;
    free;
  end;

end;

procedure TfrmFDINVOICE.TBDELClick(Sender: TObject);
begin
  if CDS_FP.IsEmpty then exit;
  if application.MessageBox('确定要删除发票吗？','提示',1)=2 then exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('delete from FD_Invoicesub where InvoiceNo='''+trim(CDS_FP.fieldbyname('invoiceNo').asstring)+''' ');
      sql.add('delete from FD_Invoice where InvoiceNo='''+trim(CDS_FP.fieldbyname('invoiceNo').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_FP.FieldByName('invoiceNo').AsString))));
      sql.Add(','+quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_FP.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;

end;

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

procedure TfrmFDINVOICE.TCHKClick(Sender: TObject);
begin
  if CDS_FP.IsEmpty then exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update FD_Invoice set Chker='''+trim(DName)+''',Chktime=getdate(),chkstatus=''1'' ');
      sql.add('where InvoiceNo='''+trim(CDS_FP.fieldbyname('invoiceNo').asstring)+''' ');
      execsql;
    end;
    application.MessageBox('审核成功','提示');
    CDS_FP.delete;
  except;
    application.MessageBox('审核失败','提示');
  end;
end;

procedure TfrmFDINVOICE.TNOCHKClick(Sender: TObject);
begin
  if CDS_FP.IsEmpty then exit;
  if CDS_FP.Fieldbyname('HZFPHX').AsFloat>0  then
  begin
    application.MessageBox('不能撤销已核销数据！','提示信息',0);
    exit;
  end;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update FD_Invoice set Chker=null,Chktime=null,chkstatus=''0'' ');
      sql.add('where InvoiceNo='''+trim(CDS_FP.fieldbyname('invoiceNo').asstring)+''' ');
      execsql;
    end;
    application.MessageBox('撤销成功','提示');
    CDS_FP.delete;
  except;
    application.MessageBox('撤销失败','提示');
  end;
end;

procedure TfrmFDINVOICE.TSelClick(Sender: TObject);
begin
  FrmInvoice_Sub:=TFrmInvoice_Sub.Create(self);
  with FrmInvoice_Sub do
  begin
    TSave.Visible:=False;
    ToolBar2.Enabled:=False;
    FInvoiceNo:=trim(CDS_FP.fieldbyname('InvoiceNo').asstring);
    fInvoiceFlag:=self.fInvoiceFlag;
    if showmodal=1 then
    begin
      
    end;
  end;
end;

procedure TfrmFDINVOICE.ToolButton3Click(Sender: TObject);
begin
  if CDS_FP.IsEmpty then Exit;
  try
    frmFjList10:=TfrmFjList10.Create(Application);
    with frmFjList10 do
    begin
    //  PState:=1;
   //   if Trim(canshu1)<>'高权限'  then cxButton1.Visible:=false;
      fkeyNO:=Trim(Self.CDS_FP.fieldbyname('InvoiceNo').AsString);
      fType:='发票';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmFjList10.Free;
  end;
end;

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

procedure TfrmFDINVOICE.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
    with ADOQuerySub do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select B.*,shuie=taxMoney-Money from FD_Invoicesub B ');
      sql.add(' where B.InvoiceNo='+quotedstr(Trim(CDS_FP.fieldbyname('InvoiceNo').AsString)));
      Open;
    end;
    SCreateCDS20(ADOQuerySub,CDS_Sub);
    SInitCDSData20(ADOQuerySub,CDS_Sub);
end;

procedure TfrmFDINVOICE.N1Click(Sender: TObject);
begin
 CDS_FP.DisableControls;
  while CDS_FP.Locate('SSel',False,[]) do
  begin
      CDS_FP.Edit;
      CDS_FP.FieldByName('SSel').Value:=True;
      CDS_FP.Post;
  end;
  CDS_FP.EnableControls
end;

procedure TfrmFDINVOICE.N2Click(Sender: TObject);
begin
 CDS_FP.DisableControls;
  while CDS_FP.Locate('SSel',False,[]) do
  begin
      CDS_FP.Edit;
      CDS_FP.FieldByName('SSel').Value:=True;
      CDS_FP.Post;
  end;
  CDS_FP.EnableControls
end;

procedure TfrmFDINVOICE.ToolButton4Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno,LBName,SYRName:string;
   i,j:Integer;
   OrderKg:Double;
begin
  if CDS_FP.IsEmpty then Exit;
  if CDS_FP.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_FP.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_FP do
    begin
      First;
      while not Eof do
      begin
        If Fieldbyname('Ssel').AsBoolean then
        begin
            ADOQueryCmd.append;
            ADOQueryCmd.fieldbyname('SubId').Value:=Trim(CDS_FP.fieldbyname('InvoiceNo').AsString);
            ADOQueryCmd.fieldbyname('Dname').Value:=Trim(DCode);
            ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_FP.EnableControls;

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_View_FPTJ '+quotedstr(Trim(DCode)));
    Open;
  end;

  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  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 TfrmFDINVOICE.TBHXClick(Sender: TObject);
begin
  if CDS_FP.IsEmpty then exit;
  frmJXFPHx:=TfrmJXFPHx.create(self);
  with frmJXFPHx do
  begin
    fkeyNo:=CDS_FP.fieldbyname('InvoiceNo').AsString;
    ffactoryName:=CDS_FP.fieldbyname('factoryName').AsString;
    if showmodal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmFDINVOICE.Tv1DblClick(Sender: TObject);
begin
  frmJXFPHXList:=TfrmJXFPHXList.create(self);
  with frmJXFPHXList do
  begin
    FFromNo:=TRIM(CDS_Sub.Fieldbyname('InvoiceNo').AsString);
    FTONO:='';
    if showmodal=1 then
    begin

    end;
    free;
  end;
end;

end.
