unit U_WorkerDeptFeeInPutDJ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxDropDownEdit, cxCalendar, cxGridCustomPopupMenu,
  cxGridPopupMenu;

type
  TfrmWorkerDeptFeeInPutDJ = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Column5: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    Label9: TLabel;
    Label4: TLabel;
    WFDate: TDateTimePicker;
    Dept: TBtnEditA;
    Worker: TEdit;
    Label6: TLabel;
    FSDate: TDateTimePicker;
    WFType: TComboBox;
    Label7: TLabel;
    WFNO: TEdit;
    Label8: TLabel;
    Person1: TBtnEditA;
    Label10: TLabel;
    Person3: TBtnEditA;
    Label11: TLabel;
    Person2: TBtnEditA;
    Label12: TLabel;
    Person4: TComboBox;
    v1priceUnit: TcxGridDBColumn;
    v1ZhaiYao: TcxGridDBColumn;
    Label14: TLabel;
    BZDate: TDateTimePicker;
    ItemName: TBtnEditA;
    Note: TMemo;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure WorkerBtnClick(Sender: TObject);
    procedure v1Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column9PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Person1BtnClick(Sender: TObject);
    procedure Person2BtnClick(Sender: TObject);
    procedure Person3BtnClick(Sender: TObject);
    procedure WFTypeChange(Sender: TObject);
    procedure v1priceUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure DeptBtnClick(Sender: TObject);
    procedure v1ZhaiYaoPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ItemNameBtnClick(Sender: TObject);
    procedure v1Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    FXS:Integer;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    procedure GetMaxConNO();
    { Private declarations }
  public
    PState,PCopyInt:Integer;
    FMainId,FConNo,FConType:String;

    { Public declarations }
  end;

var
  frmWorkerDeptFeeInPutDJ: TfrmWorkerDeptFeeInPutDJ;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_GYSList,U_ZDYHelpSel,U_YGCXList,
  U_SYDeptUserView, U_SYDeptView;

{$R *.dfm}

procedure TfrmWorkerDeptFeeInPutDJ.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('费用报销录入2',Tv1,'费用管理');
end;

procedure TfrmWorkerDeptFeeInPutDJ.InitData();
var FDEPT:string;
    Fint:integer;
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.* ');
    sql.add('from OA_YG_DangAn A ');
    sql.Add('inner join SY_Dept B on A.DPID=B.DPID');
    SQL.Add('where isnull(A.YGEName,YGName)='''+trim(DName)+'''');
    Open;
  end;
  FDEPT:=Trim(ADOQuery1.fieldbyname('DPParent').AsString);
  for Fint:=2 to ADOQuery1.fieldbyname('DPlevel').AsInteger-1 do
  begin
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from SY_Dept where DPID='''+trim(FDEPT)+'''');
      open;
    end;
    FDEPT:=Trim(ADOCmd.fieldbyname('DPParent').AsString);
  end;
  if ADOCmd.IsEmpty=False then
  Dept.Text:=trim(ADOCmd.fieldbyname('DPName').asstring);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from Worker_Fee_LKMain');
    sql.Add(' where WFID='''+Trim(FMainId)+'''');
    Open;
  end;
  if ADOQuery1.IsEmpty=False then
  SCSHDataNew(ADOQuery1,Panel1,0);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from Worker_Fee_LKSub ');
    sql.Add(' where WFID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  if PState=0 then
  begin
    WFDate.DateTime:=SGetServerDate(ADOTemp);
    Worker.Text:=Trim(DName);
    //ItemName.Text:='领款申请单';
    //WFType.Text:='对私业务';
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.ZDYHelp(FButn:TcxButtonEdit;LType:string);
begin
end;

procedure TfrmWorkerDeptFeeInPutDJ.FormShow(Sender: TObject);
begin
  ReadCxGrid('费用报销录入2',Tv1,'费用管理');
 
  InitData();
end;

procedure TfrmWorkerDeptFeeInPutDJ.GetMaxConNO();
begin

end;

function TfrmWorkerDeptFeeInPutDJ.SaveData():Boolean;
var
  maxno,maxSubNo:String;
  DateStr:string;
begin
  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    if Trim(WFNO.Text)='' then
    begin
      Datestr:=trim(uppercase(formatdateTime('yy',Now)));
      if GetLSNo(ADOCmd,maxno,trim(DateStr),'Worker_Fee_LKMain',4,0)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取费用报销主流水号失败!','提示',0);
        Exit;
      end;
      WFNO.Text:=trim(maxno);
    end;
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'LK','Worker_Fee_LKMain',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取费用报销主流水号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Worker_Fee_LKMain where WFId='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('WFId').Value:=Trim(maxno);
      RTSetsavedata(ADOCmd,'Worker_Fee_LKMain',Panel1,0);
      FieldByName('HZStatus').Value:='待送审';
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('FillCode').Value:=Trim(DCode);
        FieldByName('FillerCode').Value:=Trim(DCode);
        fieldbyname('filltime').Value:=SGetServerDateTime(ADOTemp);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditerCode').Value:=Trim(DCode);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      Post;
    end;
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('WSId').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxSubNo,'WS','Worker_Fee_LKSub',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('生成费用报销子表流水号异常！','提示',0);
            exit;
          end;
        end else
        begin
          maxSubNo:=Trim(Order_Sub.fieldbyname('WSId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Worker_Fee_LKSub where ');
          sql.Add('  WSId='''+Trim(maxSubNo)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('WSId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('WFID').Value:=Trim(maxno);
          FieldByName('WSId').Value:=Trim(maxSubNo);
          fieldbyname('YCLPrice').Value:=Order_Sub.fieldbyname('YCLPrice').AsFloat;
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'Worker_Fee_LKSub',0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('WSId').Value:=Trim(maxSubNo);
        Next;
      end;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update Worker_Fee_LKMain Set HZMoney=');
      sql.Add(' (select isnull(Sum(isnull(Money,0)),0) from Worker_Fee_LKSub A where A.WFID=Worker_Fee_LKMain.WFID)');
      sql.Add(' where WFID='''+Trim(maxno)+'''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete from OA_Chk ');
      sql.Add(' where Mainid='''+trim(maxno)+'''');
      sql.Add(' and isnull(OAType,'''')=''费用报销'' ');
      ExecSQL;
    end;
    ADOCmd.Connection.CommitTrans;
    FMainId:=Trim(maxno);
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.TBSaveClick(Sender: TObject);
var
  FReal:Double;
  FPPrice,PRTPrice,BCMoney,FPQty,ChaMoney,FFK:string;
begin
  ToolBar1.SetFocus;
  if Trim(Worker.Text)='' then
  begin
    Application.MessageBox('报销人不能为空!','提示',0);
    Exit;
  end;
  if Trim(WFType.Text)='' then
  begin
    Application.MessageBox('公私类型不能为空!','提示',0);
    Exit;
  end;
  if Trim(ItemName.Text)='' then
  begin
    Application.MessageBox('单据类型不能为空!','提示',0);
    Exit;
  end;
  if trim(WFType.Text)='对公业务' then
  begin
    if Trim(PerSon2.Text)='' then
    begin
      Application.MessageBox('账号不能为空!','提示',0);
      Exit;
    end;
    if Trim(PerSon3.Text)='' then
    begin
      Application.MessageBox('开户银行不能为空!','提示',0);
      Exit;
    end;
    if Trim(PerSon1.Text)='' then
    begin
      Application.MessageBox('收款单位不能为空!','提示',0);
      Exit;
    end;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    exit;
  end;
  if Order_Sub.Locate('Money',Null,[])=True then
  begin
    Application.MessageBox('明细金额不能为空!','提示',0);
    Exit;
  end;
  {with Order_Sub do
  begin
    first;
    while not eof do
    begin
      if FFK='' then
      FFK:=fieldbyname('ZhaiYao').AsString
      else
      if FFK<>fieldbyname('ZhaiYao').AsString then
      begin
        application.MessageBox('公司抬头必须相同','提示');
        exit;
      end;
      next;
    end;
  end;}
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.ToolButton1Click(Sender: TObject);
var FFKDW: string;
begin
  If Order_Sub.IsEmpty then
  begin
    with Order_Sub do
    begin
      Append;
      fieldbyname('priceUnit').Value:='￥';
      Post;
    end;
  end
  else
  begin
    FFKDW:=order_Sub.fieldbyname('ZhaiYao').AsString;
    with Order_Sub do
    begin
      Append;
      fieldbyname('ZhaiYao').AsString:=trim(FFKDW);
      Post;
    end;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('WSId').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删行吗？','提示',32+4)<>IDYES then Exit;
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Worker_Fee_LKSub where WFId='''+Trim(Order_Sub.fieldbyname('WFId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmWorkerDeptFeeInPutDJ.WorkerBtnClick(Sender: TObject);
begin
  frmYGCXList:=TfrmYGCXList.Create(self);
  with frmYGCXList do
  begin
    FFInt:=100;
    TSel.Visible:=True;
    if showModal=1 then
    begin
      Dept.Text:=trim(CDS_Tree.fieldbyname('DPName').AsString);
    end;
    free;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.v1Column3PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KeMu';
      flagname:='科目';
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('KeMu').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
         // Post;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.v1Column3PropertiesEditValueChanged(
  Sender: TObject);
var
  FName,FQty,FPrice,mvalue:string;
begin
  FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  mvalue:=TcxTextEdit(Sender).EditingText;

  with Order_Sub do
  begin
    Edit;
    if Trim(mvalue)='' then
      FieldByName(FName).Value:=null
    else
      FieldByName(FName).Value:=mvalue;
    Post;
  end;
  FQty:=Trim(Order_Sub.fieldbyname('YCLQty').AsString);
  FPrice:=Trim(Order_Sub.fieldbyname('YCLPrice').AsString);
  if Trim(FQty)='' then FQty:='0';
  if Trim(FPrice)='' then FPrice:='0';
  with Order_Sub do
  begin
    Edit;
    FieldByName('Money').Value:=StrToFloat(FQty)*StrToFloat(FPrice);
    Post;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.v1Column9PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='YCLUnit';
    flagname:='数量单位';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('YCLUnit').asstring:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.v1Column1PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='ZhaiYao';
    flagname:='付款单位';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('ZhaiYao').asstring:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.Person1BtnClick(Sender: TObject);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='Person1';
    flagname:='收款单位';
    if showModal=1 then
    begin
      Person1.Text:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
    end;
    free;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.Person2BtnClick(Sender: TObject);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='Person2';
    flagname:='银行账号';
    FNote:=True;
    v1Note.Caption:='开户银行';
    if showModal=1 then
    begin
      Person2.Text:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      Person3.Text:=trim(ClientDataSet1.fieldbyname('Note').AsString);
      with ADOQuery1 do
      begin
        close;
        sql.Clear;
        sql.Add('select top 1 * from Worker_Fee_LKMain ');
        sql.add('where Person2='''+Trim(Person2.Text)+'''');
        sql.add(' Order By WFDate desc');
        open;
      end;
      Person1.Text:=trim(ADOQuery1.fieldbyname('Person1').AsString);
      //Person3.Text:=trim(ADOQuery1.fieldbyname('Person3').AsString);
    end;
    free;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.Person3BtnClick(Sender: TObject);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='Person3';
    flagname:='开户银行';
    if showModal=1 then
    begin
      Person3.Text:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
    end;
    free;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.WFTypeChange(Sender: TObject);
begin
  if WFType.Text='对私业务' then
  begin
    Person4.Text:='现金';
    Person4.Enabled:=False;
    ItemName.Text:='领款申请单';
  end
  else
  begin
    Person4.Text:='';
    Person4.Enabled:=True;
    ItemName.Text:='公司费用单';
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.v1priceUnitPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='priceUnit';
    flagname:='币种';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('priceUnit').asstring:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.DeptBtnClick(Sender: TObject);
begin
  try
    frmSYDeptView:=TfrmSYDeptView.Create(Application);
    with frmSYDeptView do
    begin
      if ShowModal=1 then
      begin
        Self.Dept.Text:=Trim(frmSYDeptView.CDS_Tree.fieldbyname('DPName').AsString);
      end;
    end;
  finally
    frmSYDeptView.Free;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.v1ZhaiYaoPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='ZhaiYao';
    flagname:='公司抬头';
    if showModal=1 then
    begin
      Order_Sub.Edit;
      Order_Sub.FieldByName('ZhaiYao').AsString:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
    end;
    free;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.ItemNameBtnClick(Sender: TObject);
begin
  if WFType.Text='' then
  begin
    application.MessageBox('请先输入用款类型','提示');
    exit;
  end;
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    TBADD.Visible:=False;
    TBDel.Visible:=False;
    TBEdit.Visible:=False;
    if WFType.Text='对私业务' then
    flag:='ItemNameDS'
    else
    flag:='ItemNameDG';
    flagname:='单据类型';
    if showModal=1 then
    begin
      ItemName.Text:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
    end;
    free;
  end;
end;

procedure TfrmWorkerDeptFeeInPutDJ.v1Column2PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='YCLName';
    flagname:='用途';
    if showModal=1 then
    begin
      Order_Sub.Edit;
      Order_Sub.FieldByName('YCLName').AsString:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
    end;
    free;
  end;
end;

end.
