unit U_KHSKInPut;

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
  TfrmKHSKInPut = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    Label1: TLabel;
    ADOQueryMain: TADOQuery;
    Label3: TLabel;
    Label2: TLabel;
    CRTime: TDateTimePicker;
    ToolButton4: TToolButton;
    Label5: TLabel;
    FactoryName: TBtnEditC;
    YFName: TBtnEditC;
    PayMent: TBtnEditC;
    Label4: TLabel;
    Note: TEdit;
    Label6: TLabel;
    BZType: TComboBox;
    Label7: TLabel;
    ShuiDanMoney: TEdit;
    Label8: TLabel;
    SKDate: TDateTimePicker;
    Label9: TLabel;
    Label10: TLabel;
    ConNo: TEdit;
    Label11: TLabel;
    BKNO: TBtnEditC;
    Label13: TLabel;
    HuMingJC: TEdit;
    Label12: TLabel;
    ShiShouMoney: TEdit;
    Label14: TLabel;
    ShouXuFlag: TEdit;
    Panel2: TPanel;
    FactoryNoSub: TEdit;
    HuMing: TEdit;
    Label15: TLabel;
    Label16: TLabel;
    SKTaiTou: TBtnEditC;
    SKBankNo: TEdit;
    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 YFNameBtnUpClick(Sender: TObject);
    procedure PayMentBtnUpClick(Sender: TObject);
    procedure YFNameBtnDnClick(Sender: TObject);
    procedure PayMentBtnDnClick(Sender: TObject);
    procedure SKDateChange(Sender: TObject);
    procedure BKNOBtnUpClick(Sender: TObject);
    procedure BKNOBtnDnClick(Sender: TObject);
    procedure ShouXuFlagClick(Sender: TObject);
    procedure BZTypeChange(Sender: TObject);
    procedure SKTaiTouBtnUpClick(Sender: TObject);
    procedure SKTaiTouBtnDnClick(Sender: TObject);
  private
    { Private declarations }

    procedure InitData();
    function  SaveData():Boolean;
    procedure initJM();

  public
    { Public declarations }
    FMainId:String;
    FZZ:string;
  end;

var
  frmKHSKInPut: TfrmKHSKInPut;

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

{$R *.dfm}



procedure TfrmKHSKInPut.InitData();
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.BankNo,B.BankName,B.HuMingJC ');
    sql.Add(',HuMing=(select Top 1 HuMing from KH_Sub_Bank KBS where KBS.KHNOSub=A.FactoryNoSub)');
    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);
  if Trim(ADOQueryMain.fieldbyname('FactoryNoSub').AsString)<>'' then
  begin
    FactoryNoSub.Visible:=True;
    HuMing.Visible:=True;
    FactoryNoSub.Text:=Trim(ADOQueryMain.fieldbyname('FactoryNoSub').AsString);
    HuMing.Text:=Trim(ADOQueryMain.fieldbyname('HuMing').AsString);
  end;
end;


procedure TfrmKHSKInPut.FormDestroy(Sender: TObject);
begin
  frmKHSKInPut:=nil;
end;

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

procedure TfrmKHSKInPut.TBCloseClick(Sender: TObject);
begin
  Close;
end;
procedure TfrmKHSKInPut.FormShow(Sender: TObject);
begin
  CRTime.DateTime:=SGetServerDate(ADOQueryTemp);
  SKDate.DateTime:=CRTime.DateTime;
  if Trim(FMainId)<>'' then
  begin
    InitData();
  end;
  initJM();
end;
procedure TfrmKHSKInPut.initJM();
begin
  if Trim(BZType.Text)='￥' then
  begin
    Label7.Visible:=False;
    ShuiDanMoney.Visible:=False;
    Label14.Visible:=False;
    ShouXuFlag.Text:='';
    ShouXuFlag.Visible:=False;
  end else
  if Trim(BZType.Text)='$' then
  begin
    Label7.Visible:=True;
    ShuiDanMoney.Visible:=True;
    Label14.Visible:=True;
    ShouXuFlag.Text:='√';
    ShouXuFlag.Visible:=True;
  end;
end;

function TfrmKHSKInPut.SaveData():Boolean;
var
  maxId,CRID:String;
  FDate:TDateTime;
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,:FDate)+1, 0)) as dt');
    Parameters.ParamByName('FDate').Value:=CRTime.Date;
    Open;
  end;
  FDate:=ADOQueryTemp.fieldbyname('dt').AsDateTime;
  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,'SK','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_KC where FactoryName='''+Trim(FactoryName.TxtCode)+'''');
      sql.Add(' and KCType=''KH'' ');
      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 YS_Money_CRID set CRID=CRID+1');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
      CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YS_Money_KC where 1<>1');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('CRID').Value:=StrToInt(CRID);
        FieldByName('FactoryNo').Value:=Trim(FactoryName.TxtCode);
        FieldByName('FactoryName').Value:=Trim(FactoryName.Text);
        FieldByName('ZdyStr1').Value:='应收收';
        Post;
      end;
    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('CRID').Value:=StrToInt(CRID);

      FieldByName('CRType').Value:='收款登记';
      FieldByName('CRFlag').Value:='应收收';
      FieldByName('QtyFlag').Value:=-1;
      RTSetsavedata(ADOQueryCmd,'YS_Money_CR',Panel1,2);
      if Trim(FormatDateTime('yyyy-MM',CRTime.Date))=Trim(FormatDateTime('yyyy-MM',SKDate.Date)) then
      begin
        FieldByName('CRTime').Value:=SKDate.Date;
      end else
      begin
        FieldByName('CRTime').Value:=formatdateTIme('yyyy-MM-dd',FDate);
      end;

      FieldByName('ChkstatusP').Value:='未审核';
      if FactoryNoSub.Visible=True then
      begin
        FieldByName('FactoryNoSub').Value:=Trim(FactoryNoSub.Text);
      end else
      begin
        FieldByName('FactoryNoSub').Value:='';
      end;
      if Trim(ShuiDanMoney.Text)<>'' then
      begin
        if Trim(ShouXuFlag.Text)<>'' then
        begin
          FieldByName('JSMoney').Value:=Trim(ShuiDanMoney.Text);
          FieldByName('Money').Value:=Trim(ShuiDanMoney.Text);
        end else
        begin
          if Trim(ShiShouMoney.Text)<>'' then
          begin
            FieldByName('JSMoney').Value:=Trim(ShiShouMoney.Text);
            FieldByName('Money').Value:=Trim(ShiShouMoney.Text);
          end else                                                
          begin
            FieldByName('JSMoney').Value:=Null;
            FieldByName('Money').Value:=Null;
          end;
        end;
        if Trim(ShiShouMoney.Text)<>'' then
        begin
          FieldByName('ShouXuMoney').Value:=StrToFloat(ShuiDanMoney.Text)-StrToFloat(ShiShouMoney.Text);
        end else
        begin
          FieldByName('ShouXuMoney').Value:=Null;
        end;
      end else
      begin
        if Trim(ShiShouMoney.Text)<>'' then
          begin
            FieldByName('JSMoney').Value:=Trim(ShiShouMoney.Text);
            FieldByName('Money').Value:=Trim(ShiShouMoney.Text);
          end else
        begin
          FieldByName('JSMoney').Value:=Null;
          FieldByName('Money').Value:=Null;
        end;
      end;
      Post;
    end;
    FMainId:=Trim(maxId);
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmKHSKInPut.ToolButton4Click(Sender: TObject);
var
  FReal:Double;
begin
  ToolBar1.SetFocus;
  if Trim(FactoryName.Text)='' then
  begin
    Application.MessageBox('客户不能为空!','提示',0);
    Exit;
  end;
  if ShuiDanMoney.Visible=False then
  begin
    if Trim(ShiShouMoney.Text)='' then
    begin
      Application.MessageBox('实收金额不能为空!','提示',0);
      Exit;
    end;
    if TryStrToFloat(ShiShouMoney.Text,FReal)=False then
    begin
      Application.MessageBox('实收金额非法数字!','提示',0);
      Exit;
    end;
  end;
  if Trim(ShuiDanMoney.Text)<>'' then
  begin
    if TryStrToFloat(ShuiDanMoney.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 TfrmKHSKInPut.FactoryNameBtnUpClick(Sender: TObject);
begin
  try
    frmKHListSelJJ:=TfrmKHListSelJJ.Create(Application);
    with frmKHListSelJJ do
    begin
      frmKHListSelJJ.canshu2:='高权限';
      if ShowModal=1 then
      begin
        if FF='主' then
        begin
          FactoryName.Text:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
          FactoryName.TxtCode:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNO').AsString);
          if Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHType').AsString)='内销' then
          begin
            BZType.ItemIndex:=BZType.Items.IndexOf('￥');
          end else
          begin
            BZType.ItemIndex:=BZType.Items.IndexOf('$');
          end;
          FactoryNoSub.Visible:=False;
          HuMing.Visible:=False;
        end else
        if FF='子' then
        begin
          FactoryName.Text:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
          FactoryName.TxtCode:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNO').AsString);
          if Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHType').AsString)='内销' then
          begin
            BZType.ItemIndex:=BZType.Items.IndexOf('￥');
          end else
          begin
            BZType.ItemIndex:=BZType.Items.IndexOf('$');
          end;
          FactoryNoSub.Visible:=True;
          HuMing.Visible:=True;
          FactoryNoSub.Text:=Trim(frmKHListSelJJ.CDS_HZ3.fieldbyname('KHNOSub').AsString);
          HuMing.Text:=Trim(frmKHListSelJJ.CDS_HZ3.fieldbyname('HuMing').AsString);
        end;
      end;

    end;
  finally
    frmKHListSelJJ.Free;
  end;
end;

procedure TfrmKHSKInPut.FactoryNameBtnDnClick(Sender: TObject);
begin
  FactoryName.Text:='';
  FactoryName.TxtCode:='';
  FactoryName.Hint:='';
end;

procedure TfrmKHSKInPut.YFNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YFNameYS';
      flagname:='项目摘要';
      if Trim(DName)<>'ADMIN' then
      begin
        ViewFlag:=True;
      end;
      if ShowModal=1 then
      begin
        YFName.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKHSKInPut.PayMentBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PayMentYS';
      flagname:='收款方式';
      if Trim(DName)<>'ADMIN' then
      begin
        ViewFlag:=True;
      end;
      if ShowModal=1 then
      begin
        PayMent.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKHSKInPut.YFNameBtnDnClick(Sender: TObject);
begin
  YFName.Text:='';
end;

procedure TfrmKHSKInPut.PayMentBtnDnClick(Sender: TObject);
begin
  PayMent.Text:='';
end;

procedure TfrmKHSKInPut.SKDateChange(Sender: TObject);
begin
  CRTime.Date:=SKDate.Date;
end;

procedure TfrmKHSKInPut.BKNOBtnUpClick(Sender: TObject);
begin
  try
    frmGSListBankSel:=TfrmGSListBankSel.Create(Application);
    with frmGSListBankSel do
    begin
      if ShowModal=1 then
      begin
        BKNO.Text:=Trim(frmGSListBankSel.CDS_HZ3.fieldbyname('BKNO').AsString);
        BKNO.TxtCode:=Trim(frmGSListBankSel.CDS_HZ.fieldbyname('KHNO').AsString);
        HuMingJC.Text:=Trim(frmGSListBankSel.CDS_HZ3.fieldbyname('HuMingJC').AsString);
      end;
    end;
  finally
    frmGSListBankSel.Free;
  end;
end;

procedure TfrmKHSKInPut.BKNOBtnDnClick(Sender: TObject);
begin
  BKNO.Text:='';
  BKNO.TxtCode:='';
  HuMingJC.Text:='';
end;

procedure TfrmKHSKInPut.ShouXuFlagClick(Sender: TObject);
begin
  if Trim(ShouXuFlag.Text)='' then
  begin
    ShouXuFlag.Text:='√';
  end else
  begin
    ShouXuFlag.Text:='';
  end;
end;

procedure TfrmKHSKInPut.BZTypeChange(Sender: TObject);
begin
  initJM();
end;

procedure TfrmKHSKInPut.SKTaiTouBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SKTaiTouKH';
      flagname:='对方抬头';
      MainType:=Self.FactoryName.TxtCode;
      fnote:=True;
      V1Name.Caption:='对方抬头';
      V1Note.Caption:='账号';
      if ShowModal=1 then
      begin
        SKTaiTou.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        SKBankNo.Text:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKHSKInPut.SKTaiTouBtnDnClick(Sender: TObject);
begin
  SKTaiTou.Text:='';
  SKBankNo.Text:='';
end;

end.
