unit U_WorkerDeptFeeInPut;

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
  TfrmWorkerDeptFeeInPut = 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;
    ADOZDY: TADOQuery;
    ADOQuery1: TADOQuery;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1MSMoney: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Column5: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    Label9: TLabel;
    Label4: TLabel;
    WFDate: TDateTimePicker;
    Worker: TBtnEditA;
    WFType: TBtnEditA;
    Note: TEdit;
    Dept: TBtnEditA;
    v1Column1: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Label3: TLabel;
    WFTypeSub: TBtnEditA;
    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 WFTypeBtnClick(Sender: TObject);
    procedure v1Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure DeptBtnClick(Sender: TObject);
    procedure WFTypeSubBtnClick(Sender: TObject);
  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
  frmWorkerDeptFeeInPut: TfrmWorkerDeptFeeInPut;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_GYSList,
  U_SYDeptUserView;

{$R *.dfm}

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

procedure TfrmWorkerDeptFeeInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*,B.* from Worker_Fee_Main A left join Worker_Fee_Sub B on A.WFID=B.WFID');
    sql.Add(' where A.WFID='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,Panel1,0);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from Worker_Fee_Sub ');
    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);
    WFDate.Checked:=False;
    if Trim(DParameters1)<>'高权限' then
    begin
      Worker.Text:=Trim(DName);
    end;
  end;
end;

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

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

end;
function TfrmWorkerDeptFeeInPut.SaveData():Boolean;
var
  maxno,maxSubNo:String;
begin
  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'WF','Worker_Fee_Main',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_Main 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_Main',Panel1,0);
      FieldByName('BXNO').Value:='BX'+Trim(maxno);
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('FillerCode').Value:=Trim(DCode);
      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_Sub',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_Sub 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);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'Worker_Fee_Sub',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_Main Set HZMoney=');
      sql.Add(' (select isnull(Sum(isnull(Money,0)),0) from Worker_Fee_Sub A where A.WFID=Worker_Fee_Main.WFID)');
      sql.Add(' where WFID='''+Trim(maxno)+'''');
      ExecSQL;
    end;
    ADOCmd.Connection.CommitTrans;
    FMainId:=Trim(maxno);
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmWorkerDeptFeeInPut.TBSaveClick(Sender: TObject);
var
  FReal:Double;
  FPPrice,PRTPrice,BCMoney,FPQty,ChaMoney: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(Dept.Text)='' then
  begin
    Application.MessageBox('部门不能为空!','提示',0);
    Exit;
  end; }
  if WFDate.Checked=False then
  begin
    Application.MessageBox('日期没有选择!','提示',0);
    Exit;
  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;
  Order_Sub.DisableControls;
  with Order_Sub do
  begin
    First;
    while not Eof do
    begin
      if Trim(Order_Sub.fieldbyname('MoneyKP').AsString)<>'' then
      begin
        if Order_Sub.fieldbyname('MoneyKP').Value<>0 then
        begin
          if Trim(Order_Sub.fieldbyname('FactoryName').AsString)='' then
          begin
            Application.MessageBox('货源地不能为空!','提示',0);
            Exit;
          end;
        end;
      end;
      next;
    end;
  end;
  Order_Sub.EnableControls;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    //ModalResult:=1;
  end;

end;

procedure TfrmWorkerDeptFeeInPut.ToolButton1Click(Sender: TObject);
begin
  with Order_Sub do
  begin
    Append;
    Post;
  end;
end;

procedure TfrmWorkerDeptFeeInPut.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;
  end;
  Order_Sub.Delete;
end;

procedure TfrmWorkerDeptFeeInPut.WorkerBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='BXWorker';
      flagname:='报销人';
      if ShowModal=1 then
      begin
        Worker.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWorkerDeptFeeInPut.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 TfrmWorkerDeptFeeInPut.WFTypeBtnClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TBtnEditA(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      MainType:=Trim(Self.Dept.Text);
      if Trim(flag)='WFType' then
      begin
        if Trim(DParameters1)<>'高权限' then
        begin
          TBAdd.Visible:=False;
          TBDel.Visible:=False;
          TBEdit.Visible:=False;
        end;
      end;
      if ShowModal=1 then
      begin
        TBtnEditA(Sender).Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWorkerDeptFeeInPut.v1Column2PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
 { try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('FactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end; }
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='BXGYS';
      flagname:='供应商';
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('FactoryName').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWorkerDeptFeeInPut.v1Column7PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='BXQtyUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('QtyUnit').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
         // Post;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWorkerDeptFeeInPut.v1Column5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FName,FQty,FPrice:String;
begin
  FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  mvalue:=TcxTextEdit(Sender).EditingText;
  with Order_Sub do
  begin
    Edit;
    FieldByName(FName).Value:=mvalue;
    Post;
  end;
  FQty:=Trim(Order_Sub.fieldbyname('Qty').AsString);
  FPrice:=Trim(Order_Sub.fieldbyname('Price').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);
  end;
end;

procedure TfrmWorkerDeptFeeInPut.DeptBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='BXDept';
      flagname:='报销部门';
      if Trim(DParameters1)<>'高权限' then
      begin
        TBAdd.Visible:=False;
        TBDel.Visible:=False;
        TBEdit.Visible:=False;
      end;
      if ShowModal=1 then
      begin
        Dept.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWorkerDeptFeeInPut.WFTypeSubBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WFTypeSub';
      flagname:='报销子类';
      MainType:=Trim(Dept.Text)+Trim(WFType.Text);
      if Trim(DParameters1)<>'高权限' then
      begin
        TBAdd.Visible:=False;
        TBDel.Visible:=False;
        TBEdit.Visible:=False;
      end;
      if ShowModal=1 then
      begin
        WFTypeSub.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
