unit U_GYSRFInPut;

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
  TfrmGYSRFInPut = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton4: TToolButton;
    Label4: TLabel;
    Note: TEdit;
    Label12: TLabel;
    JSMoney: TEdit;
    Label3: TLabel;
    CRTime: TDateTimePicker;
    FactoryName: TBtnEditC;
    Label5: TLabel;
    ADOQueryMain: TADOQuery;
    Label1: TLabel;
    P_CodeName: TEdit;
    Label2: TLabel;
    P_Color: TEdit;
    Label6: TLabel;
    P_GY: TEdit;
    Label7: TLabel;
    HZFeeName: TBtnEditC;
    Label8: TLabel;
    HZFee: TEdit;
    Label9: TLabel;
    PS: TEdit;
    Label11: TLabel;
    Qty: TEdit;
    Label10: TLabel;
    Price: TEdit;
    Label13: TLabel;
    QtyUnit: TComboBox;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure FactoryNameBtnUpClick(Sender: TObject);
    procedure FactoryNameBtnDnClick(Sender: TObject);
    procedure HZFeeNameBtnUpClick(Sender: TObject);
    procedure HZFeeNameBtnDnClick(Sender: TObject);
    procedure HZFeeChange(Sender: TObject);
  private
    { Private declarations }
    FKType:string;
    procedure InitData();
    function  SaveData():Boolean;

  public
    { Public declarations }
    FMainId,canshu1:String;

  end;

var
  frmGYSRFInPut: TfrmGYSRFInPut;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_GSListBankSel,U_GYSListSelJJ;

{$R *.dfm}



procedure TfrmGYSRFInPut.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 YS_Money_CR A');
    sql.Add(' left join KH_Sub_Bank B on A.BKNO=B.BKNO');
    sql.Add(' left join KH_Main C on B.KHMainId=C.KHMainId');
    sql.Add(' where YFID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQueryMain,Panel1,2);
  SCSHDataNew(ADOQueryMain,Panel1,0);
end;


procedure TfrmGYSRFInPut.FormDestroy(Sender: TObject);
begin
  frmGYSRFInPut:=nil;
end;

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

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

function TfrmGYSRFInPut.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,'RF','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:='￥';
      FieldByName('QtyFlag').Value:=1;
      FieldByName('FactoryNo').Value:=Trim(FactoryName.TxtCode);
      RTSetsavedata(ADOQueryCmd,'YS_Money_CR',Panel1,2);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate YS_Money_CR Set Money=JSMoney,HSMoney=JSMoney,HSPrice=Price where YFID='''+Trim(maxId)+'''');
      ExecSQL;
    end;
    FMainId:=Trim(maxId);
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmGYSRFInPut.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;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    ModalResult:=1;
  end;
end;


procedure TfrmGYSRFInPut.FactoryNameBtnUpClick(Sender: TObject);
begin
  try
    frmGYSListSelJJ:=TfrmGYSListSelJJ.Create(Application);
    with frmGYSListSelJJ do
    begin
      frmGYSListSelJJ.canshu2:='高权限';
      KHType.Text:='染厂';
      if Trim(Self.canshu1)<>'管理' then
      begin
        FXZ:='1';
      end;
      KHType.Enabled:=False;
      if ShowModal=1 then
      begin
        FactoryName.Text:=Trim(frmGYSListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
        FactoryName.TxtCode:=Trim(frmGYSListSelJJ.CDS_HZ.fieldbyname('KHNO').AsString);
      end;
    end;
  finally
    frmGYSListSelJJ.Free;
  end;
end;

procedure TfrmGYSRFInPut.FactoryNameBtnDnClick(Sender: TObject);
begin
  FactoryName.Text:='';
end;

procedure TfrmGYSRFInPut.HZFeeNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='HZFeeName';
      flagname:='费用名称';
      if ShowModal=1 then
      begin
        HZFeeName.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmGYSRFInPut.HZFeeNameBtnDnClick(Sender: TObject);
begin
  HZFeeName.Text:='';
end;

procedure TfrmGYSRFInPut.HZFeeChange(Sender: TObject);
var
  FHZFee,FQty,FPrice,FMoney:string;
  FReal:Double;
begin
  FHZFee:=Trim(HZFee.Text);
  if Trim(HZFee.Text)='' then
  begin
    FHZFee:='0';
  end else
  begin
    if TryStrToFloat(FHZFee,FReal)=False then
    begin
      FHZFee:='0';
    end;
  end;
  FQty:=Trim(Qty.Text);
  if Trim(Qty.Text)='' then
  begin
    FQty:='0';
  end else
  begin
    if TryStrToFloat(FQty,FReal)=False then
    begin
      FQty:='0';
    end;
  end;
  FPrice:=Trim(Price.Text);
  if Trim(Price.Text)='' then
  begin
    FPrice:='0';
  end else
  begin
    if TryStrToFloat(FQty,FReal)=False then
    begin
      FPrice:='0';
    end;
  end;
  JSMoney.Text:=FloatToStr(SSWR(StrToFloat(FHZFee)+StrToFloat(FQty)*strToFloat(FPrice))) ;

end;

end.
