unit U_TatPlanMachList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxButtonEdit, cxDropDownEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxContainer,
  cxTextEdit, cxCurrencyEdit, cxCheckBox, RM_System, RM_Dataset, RM_Common,
  RM_Class, RM_GridReport, cxPC, Menus, RM_e_Xls, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxDateRanges, dxBarBuiltInMenu, U_BaseList,
  dxScrollbarAnnotations, dxCore, cxDateUtils, cxMaskEdit, cxCalendar,
  dxSkinsCore, dxSkinsDefaultPainters, cxProgressBar, cxImage, cxDBEdit, IdFTP,
  ShellAPI, dxSkinOffice2013White, dxSkinSharpPlus, dxSkinWXI;

type
  TfrmTatPlanMachList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ADOQueryMain: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    VC_MCNo: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label15: TLabel;
    MCNo2: TcxTextEdit;
    Label1: TLabel;
    OrderNo: TcxTextEdit;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Label5: TLabel;
    C_Code: TcxTextEdit;
    ToolButton2: TToolButton;
    cxTabControl1: TcxTabControl;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    C_Name: TcxTextEdit;
    Tv1Column2: TcxGridDBColumn;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Tv1Column3: TcxGridDBColumn;
    ToolButton3: TToolButton;
    BegDate: TcxDateEdit;
    EndDate: TcxDateEdit;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    ToolButton4: TToolButton;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    ADOQueryTemp: TADOQuery;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Picture4: TcxDBImage;
    DSImage: TDataSource;
    ADOQuery1: TADOQuery;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    ToolButton7: TToolButton;
    Tv1WbNo: TcxGridDBColumn;
    Tv1KKGroup: TcxGridDBColumn;
    Tv1ClothMeter: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure P_CodeNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure MCNoPropertiesChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure Picture4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton7Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
    procedure SetSequence(MOperate: string);
    { Private declarations }
  public
    fCKName, canshu1, FAPID: string;
    fCRType: string;
    { Public declarations }
  end;

var
  frmTatPlanMachList: TfrmTatPlanMachList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_TatPlanMachInput, U_LabelPrint;
{$R *.dfm}

procedure TfrmTatPlanMachList.SetStatus();
begin
  ToolButton3.Enabled := false;
  ToolButton4.Enabled := false;

  if canshu1 <> '业务员' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          ToolButton3.Enabled := true;
        end;
      1:
        begin
          ToolButton3.Enabled := true;
        end;
      2:
        begin
          ToolButton4.Enabled := true;
        end;
      3:
        begin

        end;
    end;
  end
  else
  begin
    ToolButton1.visible := false;
    ToolButton5.visible := false;
    ToolButton3.visible := false;
    ToolButton4.visible := false;
  end;

end;

procedure TfrmTatPlanMachList.SetSequence(MOperate: string);
begin
  if cxTabControl1.TabIndex <> 0 then
  begin
    application.MessageBox('请在未生产状态下', '提示信息', 0);
    exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      sql.Add('EXEC P_Tat_Plan_Mach_SetSequence  @PMID=''' + Trim(Self.CDS_1.fieldbyname('PMID').AsString) + '''');
      sql.Add(',@Operate=' + QuotedStr(MOperate));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmTatPlanMachList.InitGrid();
var
  MPmid: string;
begin
  if not CDS_1.IsEmpty then
    MPmid := Trim(Self.CDS_1.fieldbyname('PMID').AsString);

  with adoqueryMain do
  begin
    close;
    sql.Clear;
    sql.Add(' exec P_Tat_Plan_Mach ');
    sql.add(' @BegDate=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
    sql.Add(' ,@EndDate=''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.Date + 1)) + '''');

    case cxTabControl1.TabIndex of
      0:
        begin
          sql.Add(',@Status=''未完成'' ');
        end;
      1:
        begin
          sql.Add(',@Status=''生产中'' ');
        end;
      2:
        begin
          sql.Add(',@Status=''已完成'' ');
        end;
      3:
        begin
          sql.Add(',@Status=''全部'' ');
        end;

    end;
//    ShowMessage(SQL.Text);
    open;
  end;
  SCreateCDS(adoqueryMain, CDS_1);
  SInitCDSData(adoqueryMain, CDS_1);
  CDS_1.Locate('Pmid', MPmid, []);
end;

procedure TfrmTatPlanMachList.MCNoPropertiesChange(Sender: TObject);
begin
  if ADOQuerymain.Active = False then
    Exit;
  SDofilter(ADOQuerymain, SGetFilters(Panel1, 1, 3));
  SCreateCDS(ADOQuerymain, CDS_1);
  SInitCDSData(ADOQuerymain, CDS_1);
end;

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

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

procedure TfrmTatPlanMachList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTatPlanMachList := nil;
end;

procedure TfrmTatPlanMachList.Picture4Click(Sender: TObject);
var
  IdFTP1: TIdFTP;
  FPath, FFName: string;
  FInt: integer;
  mfileSize: Integer;
  mCreationTime: TdateTime;
  mWriteTime: TdateTime;
begin
  try
    IdFTP1 := TIdFTP.Create(self);
    IdFTP1.Host := PicSvr;
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    IdFTP1.Free;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;
  FPath := 'D:\Right1209\';
  if not DirectoryExists(ExtractFileDir(FPath)) then
    CreateDir(ExtractFileDir(FPath));
  FFName := Trim(CDS_1.fieldbyname('FileName').AsString);
  FFName := FPath + FFName;
  if FileExists(FFName) then
  begin
    FInt := 1;
    GetFileInfo(FFName, mfileSize, mCreationTime, mWriteTime);

    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select TFID,WBID,TFDate,Files,Filler,FillTime,FileName from TP_File A     ');
      SQL.Add(' where FileName=''' + Trim(CDS_1.fieldbyname('FileName').AsString) + '''');
      SQL.Add(' and FileType=''HX'' ');
      SQL.Add(' and TFDate>''' + FormatDateTime('yyyy-MM-dd hh:nn:ss', mWriteTime) + '''');
//     ShowMessage(SQL.Text);
      Open;
    end;
    if not ADOQueryTemp.IsEmpty then
    begin
      if FileExists(FFName) then
        DeleteFile(FFName);
      FInt := 0;
    end;

  end;
  if FInt <> 1 then
    IdFTP1.Get(UserDataFlag + 'HX\' + CDS_1.fieldbyname('FileName').AsString, pchar(FFName));

  if IdFTP1.Connected then
  begin
    IdFTP1.Quit;
    IdFTP1.Free;
  end;
  ShellExecute(Handle, 'open', PChar(FFName), '', '', SW_SHOWNORMAL);

end;

procedure TfrmTatPlanMachList.P_CodeNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmTatPlanMachList.FormShow(Sender: TObject);
begin
  inherited;
  canshu1 := DParameters1;
  BegDate.Date := SGetServerDate(ADOQueryTmp) - 30;
  EndDate.Date := SGetServerDate(ADOQueryTmp);
  readCxGrid(self.Caption, Tv1, '仓储管理');
  SetStatus();
  InitGrid();
end;

procedure TfrmTatPlanMachList.TBCloseClick(Sender: TObject);
begin

  close;
end;

procedure TfrmTatPlanMachList.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

procedure TfrmTatPlanMachList.TBExportClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmTatPlanMachList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmTatPlanMachList.N1Click(Sender: TObject);
begin
  SetSequence('上移');
end;

procedure TfrmTatPlanMachList.N2Click(Sender: TObject);
begin
  SetSequence('下移');
end;

procedure TfrmTatPlanMachList.ToolButton1Click(Sender: TObject);
begin
  try
    frmTatPlanMachInput := TfrmTatPlanMachInput.Create(Application);
    with frmTatPlanMachInput do
    begin
      FMCNo := Trim(Self.CDS_1.fieldbyname('MCNo').AsString);
      FMCType := Trim(Self.CDS_1.fieldbyname('MCType').AsString);
//      FPMID := Trim(Self.CDS_1.fieldbyname('PMID').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTatPlanMachInput.Free;
  end;
end;

procedure TfrmTatPlanMachList.ToolButton2Click(Sender: TObject);
begin
  writeCxGrid(self.Caption, Tv1, '仓储管理');
end;

procedure TfrmTatPlanMachList.ToolButton3Click(Sender: TObject);
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;

      sql.Add('update Tat_Plan_Mach set Status =''10'',Comptime=getdate() ');
      sql.Add(',CompId=' + QuotedStr(Trim(DCode)));
      sql.Add(',Comper=' + QuotedStr(Trim(DName)));
      sql.Add('where Status<>''10'' and PMID=' + QuotedStr(Trim(Self.CDS_1.fieldbyname('PMID').AsString)));

      sql.Add('EXEC P_Tat_Plan_Mach_SetSequence  @PMID=''' + Trim('') + '''');
      sql.Add(',@Operate=''结束'' ');
      Open;
    end;

    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmTatPlanMachList.ToolButton4Click(Sender: TObject);
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;

      sql.Add('update Tat_Plan_Mach set Status =''0'' ');
      sql.Add(',CompId=' + QuotedStr(Trim(DCode)));
      sql.Add(',Comper=' + QuotedStr(Trim(DName)));
      sql.Add('where Status=''10'' and PMID=' + QuotedStr(Trim(Self.CDS_1.fieldbyname('PMID').AsString)));

      sql.Add('EXEC P_Tat_Plan_Mach_SetSequence  @PMID=''' + Trim('') + '''');
      sql.Add(',@Operate=''返回未完成'' ');
      Open;
    end;

    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmTatPlanMachList.ToolButton5Click(Sender: TObject);
begin
  try
    frmTatPlanMachInput := TfrmTatPlanMachInput.Create(Application);
    with frmTatPlanMachInput do
    begin
      FMCNo := Trim(Self.CDS_1.fieldbyname('MCNo').AsString);
      FPMID := Trim(Self.CDS_1.fieldbyname('PMID').AsString);

      if cxTabControl1.TabIndex = 1 then
      begin
        PMType.enabled := false;
        OrderNo.enabled := false;
        C_Name.enabled := false;

        MCNO.enabled := false;
        C_Code.enabled := false;
        C_FinishLatitudeDensity.enabled := false;
      end;

      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTatPlanMachInput.Free;
  end;
end;

procedure TfrmTatPlanMachList.ToolButton6Click(Sender: TObject);
begin
  try
    frmTatPlanMachInput := TfrmTatPlanMachInput.Create(Application);
    with frmTatPlanMachInput do
    begin
      FMCNo := Trim(Self.CDS_1.fieldbyname('MCNo').AsString);
      FPMID := Trim(Self.CDS_1.fieldbyname('PMID').AsString);
      ToolButton1.Enabled := false;
      TV2.OptionsData.Editing := false;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTatPlanMachInput.Free;
  end;
end;

procedure TfrmTatPlanMachList.ToolButton7Click(Sender: TObject);
var
  MBCIOID: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  MBCIOID := '';
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while CDS_1.Locate('SSel', True, []) do
    begin

      MBCIOID := MBCIOID + ',' + (Trim(CDS_1.fieldbyname('PMID').AsString));

      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  CDS_1.EnableControls;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'PMID';
      FFiltration1 := MBCIOID;
      if ShowModal = 1 then
      begin

        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmTatPlanMachList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
//  if (AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('IsRed').Index]) = 0 then
//    ACanvas.Brush.Color := $4763FF;
end;

procedure TfrmTatPlanMachList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select XBer=(select top 1 StaffName from  Tat_Plan_OutPut X where X.ClothId = A.PCID)');
    sql.Add(' ,XBtime=(select top 1 FillTime from  Tat_Plan_OutPut X where X.ClothId = A.PCID )');
    sql.Add(' ,Qty=(select top 1  Qty from  Tat_Plan_OutPut X where X.ClothId = A.PCID)');
    sql.Add(' ,A.*,B.*,C.*');
    sql.Add(' from Tat_Plan_Card A  ');
    sql.Add(' inner join Tat_plan_Sub B on B.Subid=A.Subid ');
    sql.Add(' inner join Tat_Plan_Main C on A.MainId=C.MainId');
    sql.Add(' where PMID=  ' + QuotedStr(Trim(CDS_1.fieldbyname('PMID').AsString)));
//    ShowMessage(sql.text);
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);

//  with ADOQuery1 do
//  begin
//    close;
//    sql.Clear;
//    sql.Add(' select * from TP_File A    ');
//    sql.Add(' where TFType =''HX'' and A.FileName =' + quotedstr(trim(CDS_1.fieldbyname('FileName').AsString)));
////    ShowMessage(sql.text);
//    open;
//  end;

end;

end.

