unit U_WageList;

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

type
  TfrmWageList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    C_CodeName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2Column6: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    Label1: TLabel;
    C_Code: TEdit;
    v2Column10: TcxGridDBColumn;
    Label2: TLabel;
    DName: TEdit;
    v2Column17: TcxGridDBColumn;
    v2Column18: TcxGridDBColumn;
    v2Column19: TcxGridDBColumn;
    BegDate: TDateTimePicker;
    Label4: TLabel;
    v2Column1: TcxGridDBColumn;
    EndDate: TDateTimePicker;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    ToolButton6: TToolButton;
    MainType: TComboBox;
    Label5: TLabel;
    ToolButton3: TToolButton;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    ToolButton4: TToolButton;
    v2Column8: TcxGridDBColumn;
    N1: TMenuItem;
    N2: TMenuItem;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure v2Column17PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column4PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column7PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure v2Column19PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton3Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private

    { Private declarations }
    canshu1: string;
    procedure InitGrid();
    function IsWages(): Boolean;
  public
    fkhType: string;
    Fmanage: string;
  end;

var
  frmWageList: TfrmWageList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_WageListInput, U_InspectionInfo, U_WorkDetail,
  U_JTList;

{$R *.dfm}

function TfrmWageList.IsWages(): Boolean;
begin
  if CDS_HZ.FieldByName('GZID').AsInteger > 0 then
  begin
    Result := True;
  end
  else
  begin
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('insert into DCGMoney(MainID,SubID,CarNo,DName)');
        sql.Add('values(' + quotedstr(trim(Trim(CDS_HZ.fieldbyname('MainID').AsString))) + ', ' + quotedstr(trim(Trim(CDS_HZ.fieldbyname('SubID').AsString))) + ', ' + quotedstr(trim(Trim(CDS_HZ.fieldbyname('CarNo').AsString))) + ', ' + quotedstr(trim(Trim(CDS_HZ.fieldbyname('Filler').AsString))) + ')');
        SHOWMESSAGE(SQL.Text);
        execsql;
      end;
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from DCGMoney where ');
        sql.Add('MainID=' + quotedstr(Trim(CDS_HZ.fieldbyname('MainID').AsString)));
        sql.Add('and SubID=' + quotedstr(Trim(CDS_HZ.fieldbyname('SubID').AsString)));
        sql.Add('and CarNo=' + quotedstr(Trim(CDS_HZ.fieldbyname('CarNo').AsString)));
        sql.Add('and DName=' + quotedstr(Trim(CDS_HZ.fieldbyname('Filler').AsString)));
        open;
      end;
      if ADOQueryCmd.RecordCount > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('保存失败！', '提示', 0);
      end
      else
      begin
        ADOQueryCmd.Connection.CommitTrans;
        Result := false;
      end;
      CDS_HZ.Edit;
      CDS_HZ.FieldByName('GZID').Value := ADOQueryCmd.fieldbyname('GZID').AsInteger;
      Result := True;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Result := false;
      Application.MessageBox('保存失败！', '提示', 0);
    end;
  end;

end;

procedure TfrmWageList.InitGrid();
begin
  try

    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' exec P_DailyWageReport ' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
      sql.Add(' ,' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
      Open;
    end;

    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
    CDS_HZ.Last;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmWageList.FormDestroy(Sender: TObject);
begin
  frmWageList := nil;
end;

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

procedure TfrmWageList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('客户资料FM', Tv2, '客户自定义');
  Close;
end;

procedure TfrmWageList.FormShow(Sender: TObject);
begin
  ReadCxGrid('客户资料FM', Tv2, '客户自定义');
  InitGrid();
end;

procedure TfrmWageList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmWageList.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmWageList.C_CodeNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmWageList.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid2);
 // SelExportData(Tv2,ADOQueryMain,'客户资料列表');
end;

procedure TfrmWageList.FormCreate(Sender: TObject);
begin
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;
  canshu1 := Trim(DParameters1);
end;

procedure TfrmWageList.v2Column17PropertiesEditValueChanged(Sender: TObject);
var
  FFieldName, mvalue: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update DCGMoney set ' + FFieldName + ' =' + quotedstr(mvalue));
    sql.Add(' where GZID=' + trim(Self.CDS_HZ.FieldByName('GZID').AsString));
    EXECSQL;
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmWageList.v2Column4PropertiesEditValueChanged(Sender: TObject);
var
  FFieldName, mvalue: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update DCGMoney set GZMoney=' + mvalue);
    sql.Add(' where GZID=' + trim(Self.CDS_HZ.FieldByName('GZID').AsString));
    EXECSQL;
  end;

  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmWageList.v2Column7PropertiesEditValueChanged(Sender: TObject);
var
  FFieldName, mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update DCGMoney set ' + FFieldName + '=' + QuotedStr(Trim(mvalue)));
    sql.Add(' where GZID=' + trim(Self.CDS_HZ.FieldByName('GZID').AsString));
    EXECSQL;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmWageList.ToolButton6Click(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('insert into DCGMoney(GZDate)');
    sql.Add('values(getdate())');
    execsql;
  end;
  InitGrid();
end;

procedure TfrmWageList.v2Column19PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SCPerson';
      flagname := '挡车工';
      fnote := True;
      V1Name.Caption := '工号';
      V1Note.Caption := '姓名';
//      V1Note1.Caption := '班组';
//      fnote1 := True;
      TBAdd.Visible := False;
      TBDel.Visible := False;
      TBEdit.Visible := False;
      TBSave.Visible := False;
      if ShowModal = 1 then
      begin
        with CDS_HZ do
        begin
          Edit;
          FieldByName('DName').Value := Trim(ClientDataSet1.FieldByName('ZDYName').AsString);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update DCGMoney set DName=' + QuotedStr(Trim(ClientDataSet1.FieldByName('ZDYName').AsString)));
          sql.Add(' where GZID=' + trim(Self.CDS_HZ.FieldByName('GZID').AsString));
          EXECSQL;
        end;
        tv2.Controller.EditingController.ShowEdit();
      end;
    end;
  finally
    frmZDYHelp.free;
  end;
end;

procedure TfrmWageList.ToolButton3Click(Sender: TObject);
begin
  try
    frmWorkDetail := TfrmWorkDetail.Create(Application);
    with frmWorkDetail do
    begin
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmWorkDetail.Free;
  end;

end;

procedure TfrmWageList.MenuItem1Click(Sender: TObject);
begin

  if CDS_HZ.IsEmpty then
    exit;
  try
    frmWorkDetail := TfrmWorkDetail.Create(Application);
    with frmWorkDetail do
    begin
      begDate.Text := self.CDS_HZ.FieldByName('GZDate').AsString + ' 07:30:00';
      EndDate.Text := trim(FormatDateTime('yyyy-MM-dd', self.CDS_HZ.FieldByName('GZDate').AsDateTime + 1)) + ' 07:30:00';
      FGZID := Trim(self.CDS_HZ.fieldbyname('GZID').AsString);
      cxTabControl1.TabIndex := 1;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmWorkDetail.Free;
  end;
end;

procedure TfrmWageList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, True);
end;

procedure TfrmWageList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, false);
end;

procedure TfrmWageList.ToolButton4Click(Sender: TObject);
begin
  try
    CDS_HZ.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;

    with CDS_HZ do
    begin
      First;
      while not Eof do
      begin
        if CDS_HZ.FieldByName('SSel').AsBoolean then
        begin
          with Self.ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete DCGMoney where GZID=' + quotedstr(Trim(CDS_HZ.fieldbyname('GZID').AsString)));
            sql.Add('update Worker_WorkDetail set GZID=null where GZID=' + quotedstr(Trim(CDS_HZ.fieldbyname('GZID').AsString)));
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    CDS_HZ.EnableControls;

    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_HZ.EnableControls;
    application.MessageBox('数据生成失败！', '提示信息', 0);
  end;
end;

end.

