unit U_WorkDetail;

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

type
  TfrmWorkDetail = 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;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Label1: TLabel;
    C_Code: TEdit;
    Label2: TLabel;
    Filler: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column19: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    cxgrdDownQty: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    cxgrdWzPrice: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column17: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label5: TLabel;
    Label6: TLabel;
    BegDate: TcxDateEdit;
    EndDate: TcxDateEdit;
    v2Column3: TcxGridDBColumn;
    Label4: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    APID: TEdit;
    CarNo: TEdit;
    ConNo: TEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    v2Column4: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    tbOK: TToolButton;
    v2Column13: TcxGridDBColumn;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    N1: TMenuItem;
    v2Column16: TcxGridDBColumn;
    ToolButton4: TToolButton;
    tbDel: TToolButton;
    tbDelCX: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column18: 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 MenuItem1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure tbOKClick(Sender: TObject);
    procedure cxgrdDownQtyPropertiesEditValueChanged(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv2DblClick(Sender: TObject);
    procedure tbDelClick(Sender: TObject);
    procedure tbDelCXClick(Sender: TObject);
  private

    { Private declarations }
    canshu1: string;
    procedure InitGrid();
    procedure SetStatus();
  public
    FGZID, FEnddate, FMainId, FSubId, FDName, FCarNo: string;
  end;

var
  frmWorkDetail: TfrmWorkDetail;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_WageListInput, U_SysLogHelp, U_WageEdit,
  U_GZHJList;

{$R *.dfm}
procedure TfrmWorkDetail.SetStatus();
begin
  tbOK.Visible := False;
  tbDel.Visible := False;
  tbDelCX.Visible := False;
  case cxTabControl1.TabIndex of
    0:
      begin
        tbOK.Visible := True;
        tbDel.Visible := True;
      end;
    2:
      begin
        tbDelCX.Visible := True;
      end;
  end;
end;

procedure TfrmWorkDetail.InitGrid();
begin
  try

    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('select A.CONNO ,B.MainId,B.SubId,B.C_Code,B.C_CodeName ');
      sql.Add(',MPMoney=round(((isnull(JGPrice,0)+ISNULL(CarBalance,0))*Qty/10000),1) ');
      sql.Add(',C.* from Worker_WorkDetail C left join Cloth_Main A  on  A.MainId=C.MainId');
      sql.Add('left join Cloth_sub B on C.SubId=B.SubId ');
      sql.Add('where C.DownTime<' + quotedstr(trim(enddate.text)));
      sql.Add('and C.DownTime>=' + quotedstr(trim(BegDate.text)));
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' and not exists(select * from DCGMoney X where X.GZID=C.GZID) ');
            sql.Add(' and C.Valid=''Y'' ');
          end;
        1:
          begin
            sql.Add(' and exists(select * from DCGMoney X where X.GZID=C.GZID) ');
            sql.Add(' and C.Valid=''Y'' ');
          end;
        2:
          begin
            sql.Add(' and C.Valid=''N'' ');
          end;
        3:
          begin
            sql.Add(' and C.Valid=''Y'' ');
          end;
      end;
      if Trim(FGZID) <> '' then
      begin
        sql.Add('and C.GZID=' + quotedstr(trim(FGZID)));
      end;
      sql.Add(' order by  CarNo,DownTime');
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
    CDS_HZ.Last;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmWorkDetail.FormDestroy(Sender: TObject);
begin
  frmWorkDetail := nil;
end;

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

procedure TfrmWorkDetail.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv2, '织造记录');
  Close;
end;

procedure TfrmWorkDetail.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption, Tv2, '织造记录');
  SetStatus();
  InitGrid();
end;

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

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

procedure TfrmWorkDetail.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid2);

end;

procedure TfrmWorkDetail.FormCreate(Sender: TObject);
begin
  EndDate.Text := FormatDateTime('yyyy-MM-dd', SGetServerDate10(ADOQueryTemp)) + ' 07:30:00';
  BegDate.Text := FormatDateTime('yyyy-MM-dd', SGetServerDate10(ADOQueryTemp) - 1) + ' 07:30:00';
  canshu1 := Trim(DParameters1);
end;

procedure TfrmWorkDetail.MenuItem1Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, True);
end;

procedure TfrmWorkDetail.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, false);
end;

procedure TfrmWorkDetail.tbOKClick(Sender: TObject);
begin
  try
    CDS_HZ.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TbForBatch where DName=' + quotedstr(Trim(DName)));
      SQL.Add('and MainType=' + quotedstr('工资生成'));
      ExecSQL;
    end;
    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(' insert into TbForBatch(DName,MainType,Value1 ) ');
            sql.Add(' values( ' + quotedstr(Trim(DName)) + ',' + quotedstr('工资生成') + ',' + quotedstr(Trim(CDS_HZ.fieldbyname('DetailId').AsString)) + ')');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    with Self.ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('exec P_Do_UpGzMoney  @DName=' + quotedstr(trim(DName)));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_HZ.EnableControls;

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

procedure TfrmWorkDetail.cxgrdDownQtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName, FApid, FDetailId, Fopevent: string;
  FDownQty, FQty, FCarBalance, FCarPrice, FMPMoney: Double;
  YFDownQty, YFCarBalance: Double;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    FDownQty := FieldByName('DownQty').AsFloat;
    FCarBalance := FieldByName('CarBalance').AsFloat;
    FCarPrice := FieldByName('CarPrice').AsFloat;
    FApid := FieldByName('Apid').AsString;
    FDetailId := FieldByName('DetailId').AsString;
  end;
  FQty := FDownQty;
  FMPMoney := FQty * (FCarPrice + FCarBalance) / 10000;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Worker_WorkDetail where DetailId=' + quotedstr(FDetailId));
    Open;
  end;
  YFDownQty := ADOQueryTemp.FieldByName('DownQty').AsFloat;
  YFCarBalance := ADOQueryTemp.FieldByName('CarBalance').AsFloat;

  Fopevent := 'ID:' + Trim(FDetailId);
  Fopevent := Fopevent + ';下机转速：' + Trim(FloatToStr(YFDownQty)) + '>>>' + Trim(FloatToStr(FDownQty));
  Fopevent := Fopevent + ';补助单价：' + Trim(FloatToStr(YFCarBalance)) + '>>>' + Trim(FloatToStr(FCarBalance));

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Worker_WorkDetail where Apid=' + quotedstr(FApid));
    Open;
  end;
  if ADOQueryTemp.RecordCount > 1 then
    Exit;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('DownQty').Value := FDownQty;
    FieldByName('Qty').Value := FQty;
    FieldByName('CarBalance').Value := FCarBalance;
    FieldByName('MPMoney').Value := FMPMoney;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate Worker_WorkDetail ');
    sql.Add(' Set DownQty=' + Trim(FloatToStr(FDownQty)));
    sql.Add(' , Qty=' + Trim(FloatToStr(FQty)));
    sql.Add(' , CarBalance=' + Trim(FloatToStr(FCarBalance)));
    sql.Add(' where DetailId=' + quotedstr(Trim(FDetailId)));
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
    sql.Add(' ' + quotedstr(trim(DName)));
    sql.Add(',getdate() ');
    sql.Add(',' + quotedstr(trim(self.Caption)));
    sql.Add(',' + quotedstr(trim('修改' + FFieldName)));
    sql.Add(',' + quotedstr(Fopevent));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    ExecSQL;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmWorkDetail.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmWorkDetail.ToolButton4Click(Sender: TObject);
begin
  frmSysLogHelp := TfrmSysLogHelp.create(self);
  with frmSysLogHelp do
  begin
    fModel := self.caption;
  //  facction:='生产指示单删除';
    showmodal;
    free;
  end;
end;

procedure TfrmWorkDetail.Tv2DblClick(Sender: TObject);
begin
  try
    frmWageEdit := TfrmWageEdit.Create(Application);
    with frmWageEdit do
    begin
      FApid := Self.CDS_HZ.Fieldbyname('APID').asstring;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmWageEdit.Free;
  end;

end;

procedure TfrmWorkDetail.tbDelClick(Sender: TObject);
begin
  try
    CDS_HZ.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;

    with CDS_HZ do
    begin
      while CDS_HZ.Locate('SSel', True, []) do
      begin
        with Self.ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('update Worker_WorkDetail set Valid=''N'' where  DetailId=' + quotedstr(Trim(CDS_HZ.fieldbyname('DetailId').AsString)));
          ExecSQL;
        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 TfrmWorkDetail.tbDelCXClick(Sender: TObject);
begin
  try
    CDS_HZ.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;

    with CDS_HZ do
    begin
      while CDS_HZ.Locate('SSel', True, []) do
      begin
        with Self.ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('update Worker_WorkDetail set Valid=''Y'' where  DetailId=' + quotedstr(Trim(CDS_HZ.fieldbyname('DetailId').AsString)));
          ExecSQL;
        end;
        CDS_HZ.Delete;
      end;
    end;

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

end.

