unit U_XHGGZ;

interface

uses
  Windows, Messages, SysUtils, Variants, DateUtils, Classes, Graphics, Controls,
  Forms, Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit, cxNavigator,
  DB, cxDBData, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGridCustomPopupMenu, cxGridPopupMenu, DBClient, ADODB, cxGridLevel,
  cxClasses, cxGridCustomView, cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin,
  cxCheckBox;

type
  Tfrmxhggz = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label8: TLabel;
    Label4: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    YGName: TEdit;
    YGCode: TEdit;
    Edit1: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    ADOCmd: TADOQuery;
    Panel2: TPanel;
    Edit2: TEdit;
    Label5: TLabel;
    Button1: TButton;
    Button2: TButton;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column22: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure YGNameChange(Sender: TObject);
    procedure YGCodeChange(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    procedure InitGrid();
    function XHGGZData(): Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmxhggz: Tfrmxhggz;

implementation

uses
  U_DataLink, U_RTFun, U_Fun, U_ZDYHelpYuanGong, U_JLPTInPut, U_FjList_RZ;

{$R *.dfm}
function Tfrmxhggz.XHGGZData(): Boolean;
var
  maxno: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
//    ADOQueryCmd.Connection.BeginTrans;
//    ADOQueryCmd.DisableControls;
    if GetLSNo(ADOCmd, maxno, 'WP', 'Worker_Pay', 4, 1) = False then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('取规则流水号失败!', '提示', 0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Worker_Pay where WPID=''' + Trim(maxno) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
//       Edit;
      Append;

      FieldByName('WPID').Value := Trim(maxno);
      FieldByName('DataType').Value := 'B';
      FieldByName('Worker').Value := Trim(CDS_Main.fieldbyname('YGName').AsString);    //姓名
      FieldByName('MonMoney').Value := StrToFloatDef(Trim(CDS_Main.fieldbyname('JB').AsString), 0);    //基本工资
      FieldByName('DayQty').Value := Trim(CDS_Main.fieldbyname('SBTS').AsString);    //上班天数
      FieldByName('XiuJia').Value := Trim(CDS_Main.fieldbyname('cxts').AsString);    //厂休天数
      FieldByName('FJDay').Value := Trim(CDS_Main.fieldbyname('gsfj').AsString);    //公司放假
      FieldByName('JJRDay').Value := Trim(CDS_Main.fieldbyname('jjr').AsString);    //节假日
      FieldByName('QingJia').Value := Trim(CDS_Main.fieldbyname('qjts').AsString);    //请假天数
      FieldByName('JBGZ').Value := StrToFloatDef(Trim(CDS_Main.fieldbyname('JBCL').AsString), 0);     //产量工资     
      FieldByName('QTBT').Value := StrToFloatDef(Trim(CDS_Main.fieldbyname('QT').AsString), 0);    //其他补贴
      FieldByName('CXBT').Value := StrToFloatDef(Trim(CDS_Main.fieldbyname('cxgz').AsString), 0);    //厂休补贴
      FieldByName('QQJ').Value := StrToFloatDef(Trim(CDS_Main.fieldbyname('qjgz').AsString), 0);    //全勤奖
      FieldByName('BDBT').Value := StrToFloatDef(Trim(CDS_Main.fieldbyname('bdgz').AsString), 0);    //保底补贴
//      FieldByName('QTBT').Value := StrToFloatDef(Trim(CDS_Main.fieldbyname('qt').AsString), 0);    //其他补贴
      FieldByName('KouKuan').Value := StrToFloatDef(Trim(CDS_Main.fieldbyname('KKMoney').AsString), 0);    //扣款
      FieldByName('BXBT').Value := StrToFloatDef(Trim(Edit2.Text), 1300);     //保险补贴
      FieldByName('BasePay').Value := StrToFloatDef(Trim(CDS_Main.fieldbyname('YF').AsString), 0);
      FieldByName('YFHZMoney').Value :=StrToFloatDef(Trim(CDS_Main.fieldbyname('Money').AsString), 0);
//      FieldByName('BasePay').Value := StrToFloatDef(Trim(CDS_Main.fieldbyname('JBGZ').AsString), 0) + StrToFloatDef(Trim(CDS_Main.fieldbyname('outputWage').AsString), 0) + StrToFloatDef(Trim(Edit2.Text), 0);    //应发工资
//      FieldByName('YFHZMoney').Value := StrToFloatDef(Trim(CDS_Main.fieldbyname('JBGZ').AsString), 0) + StrToFloatDef(Trim(CDS_Main.fieldbyname('outputWage').AsString), 0) + StrToFloatDef(Trim(Edit2.Text), 0) + StrToFloatDef(Trim(CDS_Main.fieldbyname('cxgz').AsString), 0) + StrToFloatDef(Trim(CDS_Main.fieldbyname('bdgz').AsString), 0) + StrToFloatDef(Trim(CDS_Main.fieldbyname('dygz').AsString), 0) + StrToFloatDef(Trim(CDS_Main.fieldbyname('qjgz').AsString), 0);    //实发工资
//      RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'Worker_Pay', 0);
      FieldByName('WPDate').Value := Trim(CDS_Main.fieldbyname('crtime').AsString) + '-01';
//      FieldByName('WPDate').Value := Trim(FDate) + '-01';
      FieldByName('Filler').Value := Trim(DName);       
      Post;
    end;
//    ADOQueryCmd.EnableControls;
//    ADOQueryCmd.Connection.CommitTrans;
    ADOCmd.Connection.CommitTrans;
//    FCCID := Trim(MaxSubId);
    Result := True;
  except
    Result := false;
//    ADOQueryCmd.Connection.RollbackTrans;
//    ADOCmd.Connection.CommitTrans;
    ADOCmd.Connection.RollbackTrans;
//    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure Tfrmxhggz.InitGrid();
begin

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' exec P_View_WageMonthAll_copy1 @BXBT='''+Trim(Edit2.text)+''',@Begdate=' + QUOTEDSTR(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime))));
//      sql.add(' ,' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  TBFind.Click;
end;

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

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

procedure Tfrmxhggz.FormDestroy(Sender: TObject);
begin
  frmxhggz := nil;
end;

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

  BegDate.Date := StartOfTheMonth(Now);
  enddate.Date := EndOfTheMonth(BegDate.Date);

  InitGrid();
end;

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

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

procedure Tfrmxhggz.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 Tfrmxhggz.YGNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure Tfrmxhggz.YGCodeChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure Tfrmxhggz.ToolButton1Click(Sender: TObject);
begin
  Panel2.Left := (self.Width - Panel2.Width) div 2;
  Panel2.Visible := true;   

end;

procedure Tfrmxhggz.Button1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
//  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_Main do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        if fieldbyname('ssel').AsBoolean then
        begin
          if not XHGGZData() then
          begin
//             ADOQueryCmd.Connection.RollbackTrans;
            application.MessageBox('绣花工工资生成失败！', '提示信息', 0);
          end;

//          with ADOQueryTemp do
//          begin
//            Close;
//            sql.Clear;
//            sql.Add('Update BS_CYGL_CY Set YSZT=''1'' ');
//            sql.Add('where BIOSID=''' + Trim(CDS_Main.fieldbyname('BIOSID').AsString) + '''');
//            ExecSQL;
//          end;
        end;
        next;
      end;
      first;
      EnableControls;
    end;
//    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('绣花工工资生成成功！', '提示信息');
    initGrid();
    Panel2.Visible := False;
  except
//    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('绣花工工资生成失败！', '提示信息', 0);
    Panel2.Visible := False;
  end;
end;

procedure Tfrmxhggz.Button2Click(Sender: TObject);
begin
  Panel2.Visible := False;
end;

end.

