unit U_WXFPList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxPC, cxCheckBox;


type
  TfrmWXFPList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    ToolButton3: TToolButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Panel1: TPanel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ComboBox2: TComboBox;
    Label3: TLabel;
    Label1: TLabel;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    Label5: TLabel;
    FPNO: TEdit;
    v1Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    v1Column18: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Label4: TLabel;
    GHDanWei: TEdit;
    Label6: TLabel;
    XHDanWei: TEdit;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    ToolButton2: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure WorkerChange(Sender: TObject);
    procedure v1Column15CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure v1Column9CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure FPNOKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton2Click(Sender: TObject);

  private
    FInt,PFInt:Integer;
    canshu1,canshu2:string;
    FNowDate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridWsql(fsj:string);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmWXFPList: TfrmWXFPList;

implementation
uses
  U_DataLink,U_RTFun,U_WXFPInPut,U_ModuleNote, U_ZDYHelp,U_WXFPInPutWGL;

{$R *.dfm}

procedure TfrmWXFPList.FormDestroy(Sender: TObject);
begin
  frmWXFPList:=nil;
end;

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

procedure TfrmWXFPList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('外销发票列表',Tv1,'发票管理');
end;

procedure TfrmWXFPList.InitGrid();
var
  fsj:String;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select AA.* from (');
      sql.Add(' select A.*');
      sql.Add(',HZMoney=(select Sum(Money) from FP_XiaoShou_Sub B where B.FXID=A.FXID )');
      SQL.Add(' from FP_XiaoShou A');
      sql.Add(' ) AA');
      sql.Add(' where 1=1 ');
      //if cxTabControl1.TabIndex<>0 then
      begin
        fsj:=TA(ComboBox2.Items.Objects[ComboBox2.Items.IndexOf(Trim(ComboBox2.Text))]).S;
        sql.Add(' and AA.'+Trim(fsj)+'>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and AA.'+Trim(fsj)+'<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmWXFPList.InitForm();
begin
  FNowDate:=SGetServerDate(ADOQueryTemp);
  EndDate.Date:=FNowDate;
  BegDate.Date:=EndDate.Date-7;
  ReadCxGrid('外销发票列表',Tv1,'发票管理');
  //InitGrid();
end;

procedure TfrmWXFPList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;

  {if Trim(DName)<>Trim(Order_Main.fieldbyname('Filler').AsString) then
  begin
    Application.MessageBox('不能操作他人的信息!','提示',0);
    Exit;
  end;}

  try
    frmWXFPInPutWGL:=TfrmWXFPInPutWGL.Create(Application);
    with frmWXFPInPutWGL do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('FXID').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmWXFPInPutWGL.Free;
  end;
end;

procedure TfrmWXFPList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
 
  if Trim(DName)<>Trim(Order_Main.fieldbyname('Filler').AsString) then
  begin
    Application.MessageBox('不能操作他人的信息!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(HXMoney),0) HXMoney from FP_Money_HX where FXID='''+Trim(Order_Main.fieldbyname('FXID').AsString)+'''');
    sql.Add(' and isnull(Type,'''')=''核销内部发票'' ');
    Open;
  end;
  if ADOQueryTemp.FieldByName('HXMoney').Value<>0 then
  begin
    Application.MessageBox('已有核销数据不能删除!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmWXFPList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete FP_XiaoShou  where FXId='''+Trim(Order_Main.fieldbyname('FXId').AsString)+'''');
      sql.Add('delete FP_XiaoShou_Sub  where FXId='''+Trim(Order_Main.fieldbyname('FXId').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmWXFPList.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('外销发票',cxGrid1);
end;

procedure TfrmWXFPList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmWXFPList.TBAddClick(Sender: TObject);
begin
  try
    frmWXFPInPutWGL:=TfrmWXFPInPutWGL.Create(Application);
    with frmWXFPInPutWGL do
    begin
      PState:=0;
      FMainId:='';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmWXFPInPutWGL.Free;
  end;
end;

procedure TfrmWXFPList.FormShow(Sender: TObject);
var
  i:Integer;
begin
  InitForm();
  SInitComBoxByTvColumns(ComboBox2,Tv1,999,True,True);
end;

procedure TfrmWXFPList.cxPageControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmWXFPList.ToolButton3Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='外销发票';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmWXFPList.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
end;

procedure TfrmWXFPList.InitGridWsql(fsj:string);
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select AA.* from (');
      sql.Add(' select A.*');
      sql.Add(',HZMoney=(select Sum(Money) from FP_XiaoShou_Sub B where B.FXID=A.FXID )');
      SQL.Add(' from FP_XiaoShou A');
      sql.Add(' ) AA');
      sql.Add(' where 1=1 ');
      sql.Add(fsj);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmWXFPList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmWXFPList.WorkerChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmWXFPList.v1Column15CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id,Id10:Integer;
begin
  Id:=TV1.GetColumnByFieldName('YuJinagDate1').Index;//
  Id10:=TV1.GetColumnByFieldName('MoneyKC').Index;
  if Id<0 then Exit;
  //$008000FF 橙红色    橙色 $000D55FF   $00B9EE9F  浅绿
  if AViewInfo.GridRecord.Values[Id10]>0 then
  begin
    if AViewInfo.GridRecord.Values[Id]-FNowDate<=0 then
    begin
      ACanvas.Brush.Color:=clYellow;
    end  else
    if AViewInfo.GridRecord.Values[Id]=null then
    begin
     // ACanvas.Brush.Color:=clWhite;
    end;
  end else
  begin
    //ACanvas.Brush.Color:=clWhite;
  end;

end;

procedure TfrmWXFPList.v1Column9CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id,Id10:Integer;
begin
  Id:=TV1.GetColumnByFieldName('YuJinagDate2').Index;//
  Id10:=TV1.GetColumnByFieldName('MoneyKC').Index;
  if Id<0 then Exit;
  //$008000FF 橙红色    橙色 $000D55FF   $00B9EE9F  浅绿
  if AViewInfo.GridRecord.Values[Id10]>0 then
  begin
    if AViewInfo.GridRecord.Values[Id]-FNowDate<=0 then
    begin
      ACanvas.Brush.Color:=clRed;
    end  else
    if AViewInfo.GridRecord.Values[Id]=null then
    begin
      //ACanvas.Brush.Color:=clWhite;
    end;
  end else
  begin
    //ACanvas.Brush.Color:=clWhite;
  end;

end;

procedure TfrmWXFPList.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmWXFPList.FPNOKeyPress(Sender: TObject; var Key: Char);

  var
  fsj:String;
begin
  if Key<>#13 then Exit;
  if Length(Trim(FPNO.Text))<3 then Exit;
  fsj:=' and AA.FPNO like '''+'%'+Trim(FPNO.Text)+'%'+'''';
  InitGridWsql(fsj);
end;


procedure TfrmWXFPList.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;

  {if Trim(DName)<>Trim(Order_Main.fieldbyname('Filler').AsString) then
  begin
    Application.MessageBox('不能操作他人的信息!','提示',0);
    Exit;
  end;}

  try
    frmWXFPInPutWGL:=TfrmWXFPInPutWGL.Create(Application);
    with frmWXFPInPutWGL do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('FXID').AsString);
      TBSave.Visible:=False;
      ToolButton3.Visible:=False;
      ToolButton4.Visible:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmWXFPInPutWGL.Free;
  end;
end;

end.
