unit U_SBGZList;

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,
  DateUtils, cxPC;

type
  TfrmSBGZList = 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;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label3: TLabel;
    Label8: TLabel;
    YGName: TEdit;
    YGCode: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column23: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    v1Column5: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    TBFind: TToolButton;
    cxTabControl1: TcxTabControl;
    ToolButton2: TToolButton;
    v1Column2: TcxGridDBColumn;
    Label4: TLabel;
    Edit1: TEdit;
    RM2: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    v1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: 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 v1DuiZhangPropertiesEditValueChanged(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure YGNameChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure v1Column9PropertiesEditValueChanged(Sender: TObject);
    procedure EndDateExit(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmSBGZList: TfrmSBGZList;

implementation

uses
  U_DataLink, U_RTFun, U_Fun, U_XHGZListInPut, U_XHGZListNew, U_ZDYHelpYuanGong;

{$R *.dfm}

procedure TfrmSBGZList.FormDestroy(Sender: TObject);
begin
  frmSBGZList := nil;
end;

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

procedure TfrmSBGZList.InitGrid();
begin
  if cxTabControl1.TabIndex < 9 then
    BegDate.Date := StrToDate(Trim(FormatDateTime('yyyy-', enddate.DateTime)) + '0' + inttostr(cxTabControl1.TabIndex + 1) + '-01')
  else
    BegDate.Date := StrToDate(Trim(FormatDateTime('yyyy-', enddate.DateTime)) + inttostr(cxTabControl1.TabIndex + 1) + '-01');
  enddate.Date := EndOfTheMonth(BegDate.Date);
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' exec P_View_WageMonthsb ' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime))));
      sql.add(' ,' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  TBFind.Click;
end;

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

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

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

procedure TfrmSBGZList.FormShow(Sender: TObject);
begin
  ReadCxGrid('绣花工日工资列表', Tv1, '工资管理');
  cxTabControl1.TabIndex := StrToIntDef(Trim(FormatDateTime('MM', SGetServerDate10(ADOQueryTemp))), 1) - 1;
  InitGrid();
  //InitGrid();
end;

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

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

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

procedure TfrmSBGZList.v1DuiZhangPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName, FPDTiaoXian, FISM, FHXName: string;
  FMiShu, AZhenShu, FZhenShu, FPrice, FMoney, FOutputWage, FElseWage, FKKMoney: Double;
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 GZList 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 GZID=''' + Trim(CDS_Main.fieldbyname('GZID').AsString) + '''');
    ExecSQL;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmSBGZList.Tv1DblClick(Sender: TObject);
begin
  try
    frmXHGZList := TfrmXHGZList.Create(Application);
    with frmXHGZList do
    begin
      frmXHGZList.YGCode.Text := Trim(Self.CDS_Main.fieldbyname('YGCode').AsString);
      frmXHGZList.YGName.Text := Trim(Self.CDS_Main.fieldbyname('YGName').AsString);
      frmXHGZList.BegDate.DateTime := StrToDate(trim(Self.CDS_Main.fieldbyname('CRTime').AsString) + '-01');

      frmXHGZList.EndDate.DateTime := EndOfTheMonth(frmXHGZList.BegDate.DateTime);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmXHGZList.Free;
  end;
end;

procedure TfrmSBGZList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmSBGZList.YGNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmSBGZList.ToolButton2Click(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 TfrmSBGZList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmSBGZList.v1Column9PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldByName('SBBZMoney').Value := FieldByName('SBBZPrice').asfloat * FieldByName('zhenshu').asfloat;
      Post;

    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate GZListSub ');
      sql.Add(' Set SBBZPrice=' + Trim(mvalue));
      sql.Add(' ,SBBZMoney=zhenshu * ' + Trim(mvalue));
      sql.Add('exists(select * from GZListMain X where X.GZMainID=GZListSub.GZMainID and convert(varchar(7),X.CRTime,120)=' + quotedstr(CDS_Main.fieldbyname('CRTime').AsString) + ' ) ');
      sql.Add('and JTNo=' + quotedstr(CDS_Main.fieldbyname('JTNo').AsString));
      sql.Add('and HXName=' + quotedstr(CDS_Main.fieldbyname('HXName').AsString));
      sql.Add('and BSType=''双边'' ');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv1.Controller.EditingController.ShowEdit();
  except
    tv1.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmSBGZList.EndDateExit(Sender: TObject);
begin
  cxTabControl1.TabIndex := StrToIntDef(Trim(FormatDateTime('MM', EndDate.Date)), 1) - 1;
end;

end.

