unit U_YWHSInPut;

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, cxPC, cxDropDownEdit, BtnEdit;

type
  TfrmYWHSInPut = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton4: TToolButton;
    Label4: TLabel;
    Note: TEdit;
    Label12: TLabel;
    TCMoney: TEdit;
    Label3: TLabel;
    CRDate: TDateTimePicker;
    ADOQueryMain: TADOQuery;
    Label10: TLabel;
    HSPerson: TEdit;
    Label1: TLabel;
    KHQKMoney: TEdit;
    Label2: TLabel;
    KHMoneyYuKou: TEdit;
    Label5: TLabel;
    HSMoney: TEdit;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column4: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Column3: TcxGridDBColumn;
    DS_Sub: TDataSource;
    CDS_Sub: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    { Private declarations }
    FKType:string;
    procedure InitData();
    function  SaveData():Boolean;

  public
    { Public declarations }
    FMainId:String;

  end;

var
  frmYWHSInPut: TfrmYWHSInPut;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun;

{$R *.dfm}



procedure TfrmYWHSInPut.InitData();
var
  FFYFName:String;
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.BankNo,B.BankName,B.HuMingJC ');
    sql.Add('  from YW_HS_CR A');
    sql.Add(' left join YW_HS_KH B on A.HSID=B.HSID');
    sql.Add(' where A.HSID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQueryMain,Panel1,2);
end;


procedure TfrmYWHSInPut.FormDestroy(Sender: TObject);
begin
  frmYWHSInPut:=nil;
end;

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

procedure TfrmYWHSInPut.TBCloseClick(Sender: TObject);
begin
  Close;
end;
procedure TfrmYWHSInPut.FormShow(Sender: TObject);
begin
//  CRTime.Date:=SGetServerDate(ADOQueryTemp);
  if Trim(FMainId)<>'' then
  begin
    InitData();
  end;
end;

function TfrmYWHSInPut.SaveData():Boolean;
var
  maxId:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YS_Money_CR where YFID='''+Trim(FMainId)+'''');
      Open;
    end;
    FMainId:=Trim(ADOQueryCmd.fieldbyname('YFID').AsString);
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxId,'YU','YS_Money_CR',4,1)=False then
      begin
        Result:=False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！','提示',0);
        Exit;
      end;
    end else
    begin
      maxId:=FMainId;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_CR where YFID='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
        FieldByName('ChkStatusP').Value:='未审核';
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('YFID').Value:=Trim(maxId);

      FieldByName('CRType').Value:='供应商费用管理';
      FieldByName('CRFlag').Value:='费用管理';
      FieldByName('BZType').Value:='￥';
      if Trim(CRType1.Text)<>'' then
      begin
        FieldByName('FKType').Value:=Trim(CRType1.Hint);
        FieldByName('QtyFlag').Value:=0;
      end else
      if Trim(CRType2.Text)<>'' then
      begin
        FieldByName('FKType').Value:=Trim(CRType2.Hint);
        FieldByName('QtyFlag').Value:=1;
      end else
      if Trim(CRType3.Text)<>'' then
      begin
        FieldByName('FKType').Value:=Trim(CRType3.Hint);
        FieldByName('QtyFlag').Value:=1;
      end else
      if Trim(CRType4.Text)<>'' then
      begin
        FieldByName('FKType').Value:=Trim(CRType4.Hint);
        FieldByName('QtyFlag').Value:=-1;
      end else
      if Trim(CRType5.Text)<>'' then
      begin
        FieldByName('FKType').Value:=Trim(CRType5.Hint);
        FieldByName('QtyFlag').Value:=-1;
      end else
      if Trim(CRType6.Text)<>'' then
      begin
        FieldByName('FKType').Value:=Trim(CRType6.Hint);
        FieldByName('QtyFlag').Value:=-1;
      end;
      FieldByName('FactoryNo').Value:=Trim(FactoryName.TxtCode);
      RTSetsavedata(ADOQueryCmd,'YS_Money_CR',Panel1,2);
      Post;
    end;
    FMainId:=Trim(maxId);
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmYWHSInPut.ToolButton4Click(Sender: TObject);
var
  FReal:Double;
begin
  ToolBar1.SetFocus;
  if Trim(FactoryName.Text)='' then
  begin
      Application.MessageBox('供应商不能为空!','提示',0);
      Exit;
  end;
  if Trim(JSMoney.Text)<>'' then
  begin
    if TryStrToFloat(JSMoney.Text,FReal)=False then
    begin
      Application.MessageBox('金额非法数字!','提示',0);
      Exit;
    end;
    {if StrToFloat(JSMoney.Text)<0 then
    begin
      Application.MessageBox('金额不能小于零!','提示',0);
      Exit;
    end; }
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    ModalResult:=1;
  end;
end;


end.
