unit U_YSFP_FHDJ;

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, Menus, RM_e_Xls;

type
  TfrmYSFP_FHDJ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    XYZNo: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    Label3: TLabel;
    KHName: TEdit;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    TADD: TToolButton;
    Label4: TLabel;
    KPNO: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TUpdate: TToolButton;
    TPrint: TToolButton;
    TSel: TToolButton;
    TFZ: TToolButton;
    RMXLSExport1: TRMXLSExport;
    RMDB_Main: TRMDBDataSet;
    RMCD: TRMGridReport;
    Order_Print: TClientDataSet;
    ADOQuery1: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery2: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2FPNo: TcxGridDBColumn;
    v2XYZNo: TcxGridDBColumn;
    v2KHName: TcxGridDBColumn;
    v2CQDate: TcxGridDBColumn;
    v2DGDate: TcxGridDBColumn;
    v2JiaoDDate: TcxGridDBColumn;
    v2JIDDate: TcxGridDBColumn;
    TV1FPMoney: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure KHNameChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure KPNOChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure v2SselPropertiesChange(Sender: TObject);
    procedure TADDClick(Sender: TObject);
    procedure TUpdateClick(Sender: TObject);
    procedure TSelClick(Sender: TObject);
    procedure TFZClick(Sender: TObject);
    procedure TPrintClick(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    { Private declarations }

    procedure InitGridHZ();
    procedure InitGrid();
    function  SaveData():Boolean;

  public
    { Public declarations }
    RKFlag,FCYID:String;

  end;

var
  frmYSFP_FHDJ: TfrmYSFP_FHDJ;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_ProductOrderList, U_ProductOrderNewList,U_YSFP_WXDJCI,
  U_ZdyAttachment,U_YSFP_FHLR;

{$R *.dfm}


procedure TfrmYSFP_FHDJ.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* from JYOrderFHXX_Main A');
      sql.Add(' where A.filltime>='''+Trim(FormatDateTime('yyyy-MM-dd',begdate.DateTime))+''' ');
      sql.add(' and A.filltime<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+''' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYSFP_FHDJ.InitGridHZ();
begin
end;

procedure TfrmYSFP_FHDJ.FormDestroy(Sender: TObject);
begin
  frmYSFP_FHDJ:=nil;
end;

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

procedure TfrmYSFP_FHDJ.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应收款发票2',Tv1,'财务管理');
  
  Close;
end;
procedure TfrmYSFP_FHDJ.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if Trim(CDS_HZ.fieldbyname('FHID').AsString)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete JYOrderFHXX_Main where FHID='''+Trim(CDS_HZ.fieldbyname('FHID').AsString)+'''');
      ExecSQL;
    end;
  end;
  initGrid();
end;

procedure TfrmYSFP_FHDJ.FormShow(Sender: TObject);
var
  fsj:string;
begin
  ReadCxGrid('应收款发票2',Tv1,'财务管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=Enddate.DateTime-7;
  InitGrid();
  RMCD.CanExport:=true;
end;

procedure TfrmYSFP_FHDJ.TBRafreshClick(Sender: TObject);
begin
  Toolbar1.SetFocus;
  InitGrid();
end;

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

procedure TfrmYSFP_FHDJ.KHNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYSFP_FHDJ.Button2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if Trim(CDS_HZ.fieldbyname('SubID').AsString)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete YCL_SG_HZ where SubID='''+Trim(CDS_HZ.fieldbyname('SubID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

function TfrmYSFP_FHDJ.SaveData():Boolean;
var
  maxId,CRID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'YF','YF_Money_CR',4,1)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(CDS_HZ.fieldbyname('FactoryName').AsString)+'''');
      Open;
    end;
    if ADOQueryCmd.IsEmpty=False then
    begin
      CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_CRID set CRID=CRID+1');
        sql.Add('select * from YF_Money_CRID ');
        Open;
      end;
      CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YF_Money_KC where 1<>1');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('CRID').Value:=StrToInt(CRID);
        FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
        FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
        FieldByName('ZdyStr1').Value:='应收收';
        Post;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value:=Trim(maxId);
      FieldByName('CRID').Value:=StrToInt(CRID);
      //SSetSaveDataCDSNew(ADOQueryCmd,Tv2,CDS_HZ,'YF_Money_CR',3);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('CRType').Value:='应收款发票登记';
      FieldByName('CRFlag').Value:='应收收';
      FieldByName('QtyFlag').Value:=1;
      FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
      FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
      FieldByName('CRTime').Value:=SGetServerDate(ADOQueryTemp);
      fieldbyname('HuiLV').Value:=1;
      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('YFId').Value:=Trim(maxId);
      FieldByName('CRID').Value:=StrToInt(CRID);
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
end;

procedure TfrmYSFP_FHDJ.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,'发货信息列表');
end;

procedure TfrmYSFP_FHDJ.KPNOChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYSFP_FHDJ.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ,True);
end;

procedure TfrmYSFP_FHDJ.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ,false);
end;

procedure TfrmYSFP_FHDJ.v2SselPropertiesChange(Sender: TObject);
begin
  Toolbar1.SetFocus;
  //HJ(CDS_HZ,Label12,CDS_HZ.fieldbyname('KPMoney').AsCurrency);
end;

procedure TfrmYSFP_FHDJ.TADDClick(Sender: TObject);
begin
  frmYSFP_FHLR:=TfrmYSFP_FHLR.Create(self);
  with frmYSFP_FHLR do
  begin
    FFPID:='';
    if showmodal=1 then
    begin
      Initgrid();
    end;
    free;
  end;
end;

procedure TfrmYSFP_FHDJ.TUpdateClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  frmYSFP_FHLR:=TfrmYSFP_FHLR.Create(self);
  with frmYSFP_FHLR do
  begin
    FFPID:=trim(CDS_HZ.fieldbyname('FHID').AsString);
    if showmodal=1 then
    begin
      Initgrid();
    end;
    free;
  end;
end;

procedure TfrmYSFP_FHDJ.TSelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  frmYSFP_FHLR:=TfrmYSFP_FHLR.Create(self);
  with frmYSFP_FHLR do
  begin
    TBSave.Visible:=false;
    FFPID:=trim(CDS_HZ.fieldbyname('FHID').AsString);
    if showmodal=1 then
    begin
      Initgrid();
    end;
    free;
  end;
end;

procedure TfrmYSFP_FHDJ.TFZClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  frmYSFP_FHLR:=TfrmYSFP_FHLR.Create(self);
  with frmYSFP_FHLR do
  begin
    FromID:='1';
    FFPID:=trim(CDS_HZ.fieldbyname('FHID').AsString);
    if showmodal=1 then
    begin
      Initgrid();
    end;
    free;
  end;
end;

procedure TfrmYSFP_FHDJ.TPrintClick(Sender: TObject);
var
  fPrintFile,fZMFile:string;
begin
  if CDS_HZ.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('select A.* from JYOrderFPCI_Main A ');
    sql.add('where A.FHID='''+trim(CDS_HZ.fieldbyname('FHID').AsString)+''' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,Order_Print);
  SInitCDSData20(ADOQueryTemp,Order_Print);
  //fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(ComboBox1.Text)+'.rmf' ;
  if FileExists(fPrintFile) then
  begin
    if RMCD.CanExport=true then
    begin
      fZMFile:='C:\Users\Administrator\Desktop';
      if not DirectoryExists(fZMFile) then
      begin
        fZMFile:='C:\Documents and Settings\Administrator\桌面\'+trim(CDS_HZ.fieldbyname('FPNo').AsString)+'.xls';
      end
      else
      begin
        fZMFile:='C:\Users\Administrator\Desktop\'+trim(CDS_HZ.fieldbyname('FPNo').AsString)+'.XLS';
      end;
      RMCD.ExportTo(RMXLSExport1,fZMFile);
    end;
    RMCD.CanExport:=true;
    RMVariables['SumRLMoney']:=ADOQuery1.fieldbyname('SumRLMoney').AsCurrency;
    RMCD.LoadFromFile(fPrintFile);
    RMCD.ShowReport;
    RMCD.CanExport:=False;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\外销发票CI.rmf'),'提示',0);
  end;
end;

procedure TfrmYSFP_FHDJ.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  TSel.Click;
end;

end.
