unit U_WageList_HZ;

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, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, DateUtils;

type
  TfrmWageList_HZ = 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;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    Label1: TLabel;
    CP_Code: TEdit;
    Label2: TLabel;
    v2Column17: TcxGridDBColumn;
    v2Column19: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    ToolButton6: TToolButton;
    Label5: TLabel;
    PopupMenu2: TPopupMenu;
    ToolButton4: TToolButton;
    v2Column8: TcxGridDBColumn;
    N1: TMenuItem;
    N2: TMenuItem;
    v2Column15: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    v2Column20: TcxGridDBColumn;
    v2Column21: TcxGridDBColumn;
    ToolButton5: TToolButton;
    YGName: TEdit;
    YGType: TEdit;
    v2Column1: TcxGridDBColumn;
    Label3: TLabel;
    ComboBox1: TComboBox;
    DateTimePicker1: TDateTimePicker;
    Label6: TLabel;
    Button1: TButton;
    Panel2: TPanel;
    dtpYear: TDateTimePicker;
    Label7: TLabel;
    cxTabControl1: TcxTabControl;
    v2Column4: TcxGridDBColumn;
    Label4: TLabel;
    Edit1: TEdit;
    Button3: TButton;
    Label8: TLabel;
    isSJ: TComboBox;
    Label9: TLabel;
    MachXJ: TComboBox;
    Tv2Column5: TcxGridDBColumn;
    Tv2Column4: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    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 v2Column4PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column7PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure v2Column19PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure v2Column5PropertiesEditValueChanged(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure DateTimePicker1Change(Sender: TObject);
  private
    canshu1: string;
    procedure InitGrid();
    function SaveData(): Boolean;
      { Private declarations }
  public
    fkhType: string;
    Fmanage: string;
    YuanGong: string;
  end;

var
  frmWageList_HZ: TfrmWageList_HZ;

implementation

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

{$R *.dfm}

function TfrmWageList_HZ.SaveData(): Boolean;
var
  maxId, CRID, FXHNOPZ, FXHNO: string;
begin
  Result := False;
  try
    if Trim(CDS_HZ.fieldbyname('GZID').AsString) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'GZ', 'DCGMoney', 4, 1) = False then
      begin
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxId := Trim(CDS_HZ.fieldbyname('GZID').AsString);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from DCGMoney where GZID=' + Quotedstr(Trim(maxId)));
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(CDS_HZ.fieldbyname('GZID').AsString) = '' then
      begin
        Append;
        FieldByName('GZID').Value := maxId;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_HZ, 'DCGMoney', 0);

      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('GZID').Value := Trim(maxId);
      Post;
    end;
    Result := True;
  except
    Result := Result;
  end;
end;

procedure TfrmWageList_HZ.InitGrid();
begin
  try

    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from  DCGMoney      ');
      sql.Add(' where  YGType=''' + YuanGong + '''      ');
      if cxTabControl1.TabIndex = 12 then
      begin
        sql.Add(' and year(GZDate)= ' + quotedstr(trim(FormatDateTime('yyyy', dtpYear.DateTime))));
        DateTimePicker1.DateTime := dtpYear.DateTime;
      end
      else
      begin
        if cxTabControl1.TabIndex < 9 then
        begin
          sql.Add(' and GZDate= ' + quotedstr(trim(FormatDateTime('yyyy-', dtpYear.DateTime)) + '0' + inttostr(cxTabControl1.TabIndex + 1)));
          DateTimePicker1.DateTime := StrToDate(trim(FormatDateTime('yyyy-', dtpYear.DateTime)) + '0' + inttostr(cxTabControl1.TabIndex + 1) + '-01');
        end
        else
        begin
          sql.Add(' and GZDate= ' + quotedstr(trim(FormatDateTime('yyyy-', dtpYear.DateTime)) + inttostr(cxTabControl1.TabIndex + 1)));
          DateTimePicker1.DateTime := StrToDate(trim(FormatDateTime('yyyy-', dtpYear.DateTime)) + inttostr(cxTabControl1.TabIndex + 1) + '-01');
        end;
      end;
//      ShowMessage(SQL.Text);
//      sql.Add(' where GZDate>= ' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
//      sql.Add(' and GZDate<' + 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_HZ.FormDestroy(Sender: TObject);
begin
  frmWageList_HZ := nil;
end;

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

procedure TfrmWageList_HZ.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(self.Caption), Tv2, '客户自定义');
  Close;
end;

procedure TfrmWageList_HZ.FormShow(Sender: TObject);
begin
  ReadCxGrid(Trim(self.Caption), Tv2, '客户自定义');
  InitGrid();
  ComboBox1.Visible := false;
  Label3.Visible := false;
  Button1.Click;
end;

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

procedure TfrmWageList_HZ.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_HZ.C_CodeNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

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

procedure TfrmWageList_HZ.FormCreate(Sender: TObject);
begin
  dtpYear.DateTime := SGetServerDate10(ADOQueryTemp);

  DateTimePicker1.DateTime := SGetServerDate10(ADOQueryTemp);
  canshu1 := Trim(DParameters1);
end;

procedure TfrmWageList_HZ.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_HZ.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_HZ.ToolButton6Click(Sender: TObject);
begin

  with Self.CDS_HZ do
  begin
    Append;
    FieldByName('GZDate').Value := SGetServerDate(ADOQueryTemp);
    Post;
  end;

end;

procedure TfrmWageList_HZ.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_HZ.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, True);
end;

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

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

    with CDS_HZ do
    begin
      while CDS_HZ.Locate('SSel', True, []) do
      begin
        if Trim(CDS_HZ.fieldbyname('GZID').AsString) <> '' then
        begin
          with Self.ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete DCGMoney where GZID=' + quotedstr(Trim(CDS_HZ.fieldbyname('GZID').AsString)));
            ExecSQL;
          end;
        end;
        CDS_HZ.Delete;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    CDS_HZ.EnableControls;

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

procedure TfrmWageList_HZ.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  ToolBar1.SetFocus;
  if not CDS_HZ.Locate('SSel', True, []) then
  begin
    Application.MessageBox('请选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin

      while CDS_HZ.Locate('SSel', True, []) do
      begin
        if not SaveData() then
        begin
          CDS_HZ.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('保存失败!', '提示', 0);
          Exit;
        end;
        with CDS_HZ do
        begin
          Edit;
          FieldByName('SSel').Value := False;
          Post;
        end;
        Next;
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!', '提示', 0);
  except
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmWageList_HZ.v2Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FGZQty, FMSPrice, FGZPrice, FCLMoney, FJLMoney, FCFMoney, FGZMoney, FCP_WeiMi: Double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    FGZQty := CDS_HZ.fieldbyname('PS').AsFloat;
    FJLMoney := CDS_HZ.fieldbyname('JLMoney').AsFloat;
    FCFMoney := CDS_HZ.fieldbyname('CFMoney').AsFloat;
    FGZPrice := CDS_HZ.fieldbyname('GZPrice').AsFloat;
    FCLMoney := RoundFloat(FGZQty * FGZPrice, 2);
    FGZMoney := FCLMoney + FJLMoney - FCFMoney;
  end;

  with CDS_HZ do
  begin
    Edit;
    FieldByName('GZMoney').Value := FGZMoney;
    FieldByName('CLMoney').Value := FCLMoney;
    Post;
  end;

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

procedure TfrmWageList_HZ.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
  Button1.Click;
end;

procedure TfrmWageList_HZ.Button1Click(Sender: TObject);
begin

  with ADOQueryMain do
  begin
    Filtered := False;
    Close;
    SQL.Clear;
    sql.Add(' exec P_GZ_All1     ');
    sql.Add(' @FGZDate= ' + quotedstr(trim(FormatDateTime('yyyy-MM', DateTimePicker1.DateTime))));
    sql.Add(' ,@GZType=' + quotedstr(trim(ComboBox1.Text)));
//    ShowMessage(sql.text);
    Open;
  end;

  SCreateCDS20(ADOQueryMain, CDS_HZ);
  SInitCDSData20(ADOQueryMain, CDS_HZ);
end;

procedure TfrmWageList_HZ.Button3Click(Sender: TObject);
var
  mvalue, FFieldName: string;
  FGZQty, FMSPrice, FGZPrice, FCLMoney, FJLMoney, FCFMoney, FGZMoney, FCP_WeiMi: Double;
begin

  FGZPrice := StrToFloatDef(Edit1.Text, 0);
  with CDS_HZ do
  begin
    First;
    while not Eof do
    begin
      if CDS_HZ.fieldbyname('SSel').AsBoolean then
      begin
        FGZQty := CDS_HZ.fieldbyname('PS').AsFloat;
        FJLMoney := CDS_HZ.fieldbyname('JLMoney').AsFloat;
        FCFMoney := CDS_HZ.fieldbyname('CFMoney').AsFloat;
        FCLMoney := RoundFloat(FGZQty * FGZPrice, 2);
        FGZMoney := FCLMoney + FJLMoney - FCFMoney;

        with CDS_HZ do
        begin
          Edit;
          FieldByName('GZPrice').Value := FGZPrice;
          FieldByName('GZMoney').Value := FGZMoney;
          FieldByName('CLMoney').Value := FCLMoney;
          Post;
        end;

      end;
      Next;
    end;

  end;

  tv2.Controller.EditingController.ShowEdit();
  CDS_HZ.Locate('SSel', true, []);
end;

procedure TfrmWageList_HZ.DateTimePicker1Change(Sender: TObject);
begin
  dtpYear.DateTime := DateTimePicker1.DateTime;
  cxTabControl1.TabIndex := MonthOf(dtpYear.DateTime) - 1;

end;

end.

