unit U_XHGZListNew;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit;

type
  TfrmXHGZList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    TBDel: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    Label3: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    YGName: TEdit;
    HXName: TEdit;
    YGCode: TEdit;
    Label6: TLabel;
    TouJu: TEdit;
    Label7: TLabel;
    JTNo: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label10: TLabel;
    BanCi: TEdit;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Label5: TLabel;
    Label9: TLabel;
    JXModel: TEdit;
    PDTiaoXian: TComboBox;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    Label11: TLabel;
    ElseWageName: TEdit;
    v1Column8: TcxGridDBColumn;
    ToolButton1: TToolButton;
    prtdata: TDateTimePicker;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1DuiZhang: TcxGridDBColumn;
    v1CLBZQty: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure YGNameKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure v1DuiZhangPropertiesEditValueChanged(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    canshu1, canshu2, canshu3: string;
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmXHGZList: TfrmXHGZList;

implementation

uses
  U_DataLink, U_RTFun, U_XHGZListInPut;

{$R *.dfm}

procedure TfrmXHGZList.FormDestroy(Sender: TObject);
begin
  frmXHGZList := nil;
end;

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

procedure TfrmXHGZList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select * from GZListMain A left join GZListSub B on A.GZMainID=B.GZMainID ');
      sql.add(' where CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
      sql.Add(' and CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
      if SGetFilters(Panel1, 1, 2) <> '' then
        sql.Add(' and  ' + SGetFilters(Panel1, 1, 2));
      SQL.Add(' order by A.CRTime,A.GZMainID');
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmXHGZList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmXHGZList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmXHGZList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('绣花工日工资列表', Tv1, '工资管理');
  Close;
end;

procedure TfrmXHGZList.FormShow(Sender: TObject);
begin
  ReadCxGrid('绣花工日工资列表', Tv1, '工资管理');
  prtdata.DateTime := EndDate.DateTime;
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
  canshu3 := Trim(DParameters3);
  InitGrid();
  if Trim(canshu2) = '查看' then
  begin
    TBAdd.Visible := False;
    TBDel.Visible := False;
    TBEdit.Visible := False;
  end
  else
  begin
    TBAdd.Visible := True;
    TBDel.Visible := True;
    TBEdit.Visible := True;
  end;
  //InitGrid();
end;

procedure TfrmXHGZList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('绣花工日工资列表', cxGrid2);
end;

procedure TfrmXHGZList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmXHGZList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmXHGZList.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

//  if (Trim(canshu1) <> '最终权限') then
//  begin
//    if (Trim(CDS_Main.FieldByName('filler').AsString) <> Trim(DName)) then
//    begin
//      Application.MessageBox('权限不足，无法修改！', '提示', 0);
//      Exit;
//    end;
//  end;

  if Trim(CDS_Main.fieldbyname('GZSubID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;

    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Delete GZListSub where GZSubID=''' + Trim(CDS_Main.fieldbyname('GZSubID').AsString) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      InitGrid();
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;
  end
  else
  begin
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Delete GZListMain where GZMainID=''' + Trim(CDS_Main.fieldbyname('GZMainID').AsString) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Main.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;
  end;
end;

procedure TfrmXHGZList.TBAddClick(Sender: TObject);
begin
  try
    frmXHGZListInPut := TfrmXHGZListInPut.Create(Application);
    with frmXHGZListInPut do
    begin
      FCRTime := Trim(FormatDateTime('yyyy-MM-dd', SGetServerDate10(ADOQueryTemp)));
      FBanCi := '白班';
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmXHGZListInPut.Free;
  end;
end;

procedure TfrmXHGZList.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  try
    frmXHGZListInPut := TfrmXHGZListInPut.Create(Application);
    with frmXHGZListInPut do
    begin
      FCRTime := Trim(Self.CDS_Main.fieldbyname('CRTime').AsString);
      FBanCi := Trim(Self.CDS_Main.fieldbyname('BanCi').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmXHGZListInPut.Free;
  end;
end;

procedure TfrmXHGZList.YGNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    {if Length(Trim(SPID.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' select A.* ');
        sql.Add(',ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId)');
        //sql.Add(' ,ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId )');
        sql.Add(' from CK_SXPB_CR A');
        sql.add(' where SPID like :SXID');
        Parameters.ParamByName('SXID').Value:='%'+Trim(SXID.Text)+'%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;  }
  end;
end;

procedure TfrmXHGZList.ToolButton1Click(Sender: TObject);
var
  fPrintFile: string;
begin
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\新绣花工月工资.rmf';
  RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('exec P_Print_XHGZNew ' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', prtdata.Date))));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PRT);
  SInitCDSData20(ADOQueryTemp, CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\新绣花工月工资.rmf'), '提示', 0);
  end;
end;

procedure TfrmXHGZList.v1DuiZhangPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    edit;
    if CDS_Main.FieldByName('DuiZhang').AsBoolean = True then
    begin
      CDS_Main.FieldByName('DuiZhang').AsBoolean := false;
    end
    else
    begin
      CDS_Main.FieldByName('DuiZhang').AsBoolean := true;
    end;
    Post;
  end;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('update GZListSub set ');
    if CDS_Main.FieldByName('DuiZhang').AsBoolean = True then
    begin
      sql.Add('DuiZhang=1 ');
    end
    else
    begin
      sql.Add('DuiZhang=0 ');
    end;
    sql.Add('where GZSubID=''' + Trim(CDS_Main.fieldbyname('GZSubID').AsString) + '''');
    ExecSQL;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmXHGZList.FormCreate(Sender: TObject);
begin
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 30;
end;

end.

