unit U_MachineManage;

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, cxPC, cxCheckComboBox, cxDropDownEdit, Menus, RM_e_Xls,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, RM_BarCode, dxScrollbarAnnotations, cxContainer,
  cxProgressBar, dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmMachineManage = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    GPM_1: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel1: TPanel;
    v2Column8: TcxGridDBColumn;
    Label1: TLabel;
    v2Column12: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    v2Column1: TcxGridDBColumn;
    ToolButton6: TToolButton;
    MCNO: TcxTextEdit;
    cxgrdbclmnTv2Column3: 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 ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure CustomerChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure Tv2Column1PropertiesChange(Sender: TObject);
    procedure MCNOPropertiesChange(Sender: TObject);
    procedure Tv2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv2Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    procedure InitGrid();
  public
    fFlag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmMachineManage: TfrmMachineManage;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_LabelPrint, U_MachineInPut, U_EmployeeSel;

{$R *.dfm}

procedure TfrmMachineManage.InitGrid();
begin
  MCNO.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* from  BS_Machine A order by MCNO ');
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmMachineManage.MCNOPropertiesChange(Sender: TObject);
var
  sql: string;
begin
  if ADOQueryMain.Active then
  begin
    sql := SGetFilters(Panel1, 1, 2);
    SDofilter(ADOQueryMain, sql);
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmMachineManage.FormDestroy(Sender: TObject);
begin
  inherited;
  frmMachineManage := nil;
end;

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

procedure TfrmMachineManage.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption), Tv2, '机台管理');
  Close;
end;

procedure TfrmMachineManage.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(Trim(Self.Caption), Tv2, '机台管理');
//  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
//  begdate.DateTime:=Enddate.DateTime-30;
  InitGrid();
end;

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

procedure TfrmMachineManage.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
//  SelExportData(Tv2, ADOQueryMain, trim(Self.Caption));
  TcxGridToExcel(self.Caption, cxgrid2);
end;

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

procedure TfrmMachineManage.N1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  SelOKNo(CDS_HZ, True);
end;

procedure TfrmMachineManage.N2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  SelOKNo(CDS_HZ, False);
end;

procedure TfrmMachineManage.CustomerChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmMachineManage.FormCreate(Sender: TObject);
begin
  inherited;
  fmanage := Trim(DParameters1);
end;

procedure TfrmMachineManage.ToolButton3Click(Sender: TObject);
var
  RTValues: TArray<string>;
begin

  if CDS_HZ.IsEmpty then
    Exit;
  if CDS_HZ.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据！', '提示', 0);
    Exit;
  end;
  RTValues := SelCDSKey(CDS_HZ, ['MCID']);

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'Machine';
      FFiltration1 := RTValues[0];
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmMachineManage.ToolButton4Click(Sender: TObject);
var
  maxId: string;
begin
  try
    frmMachineInPut := TfrmMachineInPut.Create(Application);
    with frmMachineInPut do
    begin
      FBCId := '';
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmMachineInPut.Free;
  end;
//  MCNO.SetFocus;
//  if GetLSNo(ADOQueryCmd, maxId, 'M', 'BS_Machine', 4, 1) = False then
//  begin
//    Application.MessageBox('取最大号失败！', '提示', 0);
//    Exit;
//  end;
//  with ADOQueryCmd do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('insert into BS_Machine(Filler) values(' + quotedstr(Trim(dname)) + ')');
//    ExecSQL;
//  end;
//  InitGrid();
end;

procedure TfrmMachineManage.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if CDS_HZ.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据！', '提示', 0);
    Exit;
  end;

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

  with CDS_HZ do
  begin
    while not Eof do
    begin
      if CDS_HZ.Locate('SSel', True, []) = True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete BS_Machine where MCID=' + QuotedStr(CDS_HZ.FieldByName('MCID').AsString));
          ExecSQL;
        end;
      end;
      CDS_HZ.Edit;
      CDS_HZ.FieldByName('SSel').Value := False;
      CDS_HZ.Post;
      Next;
    end;
  end;
  InitGrid();
end;

procedure TfrmMachineManage.ToolButton6Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  try
    frmMachineInPut := TfrmMachineInPut.Create(Application);
    with frmMachineInPut do
    begin
      FBCId := Trim(CDS_HZ.fieldbyname('MCID').AsString);
      TBDel.Visible := False;
      TBAdd.Visible := False;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmMachineInPut.Free;
  end;
end;

procedure TfrmMachineManage.Tv2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmEmployeeSel := TfrmEmployeeSel.Create(self);
    with frmEmployeeSel do
    begin
      FPost := '挡车工';
      if ShowModal = 1 then
      begin
        with CDS_HZ do
        begin
          Edit;
          FieldByName('DCGName1').Value := Trim(frmEmployeeSel.FRName);
          FieldByName('DCGNo1').Value := Trim(frmEmployeeSel.FRCode);
          Post;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate BS_Machine ');
          sql.Add(' Set DCGName1=''' + Trim(frmEmployeeSel.FRName) + '''');
          sql.Add(' , DCGNo1=''' + Trim(frmEmployeeSel.FRCode) + '''');
          sql.Add(' , Editer=''' + Trim(DName) + '''');
          sql.Add(' , Edittime=getdate()');
          sql.Add(' where MCID=' + quotedstr(CDS_HZ.fieldbyname('MCID').AsString));
//      ShowMessage(sql.text);
          ExecSQL;
        end;

      end;
    end;
  finally
    frmEmployeeSel.Free;
  end;
end;

procedure TfrmMachineManage.Tv2Column1PropertiesChange(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate BS_Machine ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' , Editer=''' + Trim(DName) + '''');
      sql.Add(' , Edittime=getdate()');
      sql.Add(' where MCID=' + quotedstr(CDS_HZ.fieldbyname('MCID').AsString));
//      ShowMessage(sql.text);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmMachineManage.Tv2Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmEmployeeSel := TfrmEmployeeSel.Create(self);
    with frmEmployeeSel do
    begin
      FPost := '挡车工';
      if ShowModal = 1 then
      begin
        with CDS_HZ do
        begin
          Edit;
          FieldByName('DCGName2').Value := Trim(frmEmployeeSel.FRName);
          FieldByName('DCGNo2').Value := Trim(frmEmployeeSel.FRCode);
          Post;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate BS_Machine ');
          sql.Add(' Set DCGName2=''' + Trim(frmEmployeeSel.FRName) + '''');
          sql.Add(' , DCGNo2=''' + Trim(frmEmployeeSel.FRCode) + '''');
          sql.Add(' , Editer=''' + Trim(DName) + '''');
          sql.Add(' , Edittime=getdate()');
          sql.Add(' where MCID=' + quotedstr(CDS_HZ.fieldbyname('MCID').AsString));
//      ShowMessage(sql.text);
          ExecSQL;
        end;

      end;
    end;
  finally
    frmEmployeeSel.Free;
  end;
end;

end.

