unit U_FDINVOICE_S;

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;

type
  TfrmFDINVOICE_S = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    P_CodeName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    ywy: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_FP: TDataSource;
    CDS_FP: TClientDataSet;
    TADD: TToolButton;
    TUPDATE: TToolButton;
    TBDEL: TToolButton;
    v1InvoiceDate: TcxGridDBColumn;
    v1ToFactoryName: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    v1P_CodeName: TcxGridDBColumn;
    v1P_Spec: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1PriceUnit: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label4: TLabel;
    P_Spec: TEdit;
    Label5: TLabel;
    FactoryName: TEdit;
    TCHK: TToolButton;
    TNOCHK: TToolButton;
    cxTabControl1: TcxTabControl;
    TSel: TToolButton;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    orderNo: TEdit;
    KHConNo: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    v1Column5: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column6: 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 ywyChange(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);
  private
    { Private declarations }
    canshu1:string;
    procedure InitGrid();
    procedure SetStatus();
  public
    fInvoiceFlag:string;
    { Public declarations }
  end;

var
  frmFDINVOICE_S: TfrmFDINVOICE_S;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_FDINVOICE_Sub_S;

{$R *.dfm}

procedure TfrmFDINVOICE_S.SetStatus();
begin
  tchk.Visible:=false;
  tnochk.Visible:=false;
  TUPDATE.Visible:=false;
  tbdel.Visible:=false;
  if trim(canshu1)<>'高权限'  then
  begin
    case cxTabControl1.TabIndex of
    0:begin
       TUPDATE.Visible:=true;
       tbdel.Visible:=true;
      end;
    1:begin
      end;
    2:begin
      end;
    end;
  end
  else
  begin
    case cxTabControl1.TabIndex of
    0:begin
       tchk.Visible:=true;
       TUPDATE.Visible:=true;
       tbdel.Visible:=true;
      end;
    1:begin
       tnochk.Visible:=true;
      end;
    2:begin
      end;
    end;
  end;

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

procedure TfrmFDINVOICE_S.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add('select A.*,B.*,C.OrderNo,');
      sql.Add('KHConNO=(select Top 1 KHConNO  from JYOrdercon_Main X where X.conNO=C.conNO)');
      sql.Add('from FD_Invoice A  ');
      sql.add('inner join FD_Invoicesub B on B.InvoiceNo=A.InvoiceNo ');
      sql.Add('left join JYorder_main C on C.MainID=B.mainID') ;
      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''');
      if trim(canshu1)<>'高权限'then
      sql.add(' and A.ywy='''+trim(Dname)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_FP);
    SInitCDSData20(ADOQueryMain,CDS_FP);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmFDINVOICE_S.FormDestroy(Sender: TObject);
begin
  frmFDINVOICE_S:=nil;
end;

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

procedure TfrmFDINVOICE_S.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv1,'财务管理');
  Close;
end;
procedure TfrmFDINVOICE_S.FormShow(Sender: TObject);
begin
  canshu1:=trim(DParameters1);
  ReadCxGrid(self.Caption,Tv1,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=Enddate.DateTime-30;
  SetStatus();
  InitGrid();
end;

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

procedure TfrmFDINVOICE_S.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_S.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(self.Caption,cxgrid1);
end;

procedure TfrmFDINVOICE_S.ywyChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmFDINVOICE_S.TADDClick(Sender: TObject);
begin
  frmInvoice_Sub_S:=TfrmInvoice_Sub_S.Create(self);
  with frmInvoice_Sub_S do
  begin
    FInvoiceNo:='';
    fInvoiceFlag:=self.fInvoiceFlag;
    canshu1:=self.canshu1;
    if showmodal=1 then
    begin
      initGrid();
    end;
    free;
  end;

end;

procedure TfrmFDINVOICE_S.TUPDATEClick(Sender: TObject);
begin
  IF cds_Fp.IsEmpty then exit;
  frmInvoice_Sub_S:=TfrmInvoice_Sub_S.Create(self);
  with frmInvoice_Sub_S do
  begin
    InvoiceNo.Enabled:=False;
    FInvoiceNo:=trim(CDS_FP.fieldbyname('InvoiceNo').asstring);
    fInvoiceFlag:=self.fInvoiceFlag;
    canshu1:=self.canshu1;
    if showmodal=1 then
    begin
        initGrid();
    end;
    free;
  end;

end;

procedure TfrmFDINVOICE_S.TBDELClick(Sender: TObject);
begin
  if CDS_FP.IsEmpty then exit;
  if application.MessageBox('确定要删除发票吗？','提示',1)=2 then exit;
  with ADOQueryMain 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)+''' ');
    execsql;
  end;
  initGrid();
end;

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

procedure TfrmFDINVOICE_S.TCHKClick(Sender: TObject);
begin
  if CDS_FP.IsEmpty then exit;
  try
    with ADOQueryMain 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('审核成功','提示');
    initGrid();
  except;
    application.MessageBox('审核失败','提示');
  end;
end;

procedure TfrmFDINVOICE_S.TNOCHKClick(Sender: TObject);
begin
  if CDS_FP.IsEmpty then exit;
  try
    with ADOQueryMain 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('撤销成功','提示');
    initGrid();
  except;
    application.MessageBox('撤销失败','提示');
  end;
end;

procedure TfrmFDINVOICE_S.TSelClick(Sender: TObject);
begin
  frmInvoice_Sub_S:=TfrmInvoice_Sub_S.Create(self);
  with frmInvoice_Sub_S 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;

end.
