unit U_DyePlanList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxCurrencyEdit, cxImage, cxDBEdit, Menus, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxPC, cxSplitter, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, ShellAPI, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  IdExplicitTLSClientServerBase, U_BaseList, cxCheckBox, cxDropDownEdit,
  dxScrollbarAnnotations, Clipbrd;

type
  TfrmDyePlanList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    v1OrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    PM_1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    Label8: TLabel;
    C_Name: TEdit;
    Tv1F_description: TcxGridDBColumn;
    Label11: TLabel;
    F_Name: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    TBcopy: TToolButton;
    cxSplitter1: TcxSplitter;
    DS_2: TDataSource;
    cxTabControl1: TcxTabControl;
    TbXiada: TToolButton;
    TbNoXiada: TToolButton;
    TbQuxiao: TToolButton;
    TbNoQuxiao: TToolButton;
    v1Column6: TcxGridDBColumn;
    Panel2: TPanel;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Tv1Column14: TcxGridDBColumn;
    Tv1QtyOrder: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    ToolButton2: TToolButton;
    N1: TMenuItem;
    CDS_2: TClientDataSet;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    Tv1Column1: TcxGridDBColumn;
    v1SOrddefstr1: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1PRTORDQTY: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolButton3: TToolButton;
    TV2Column1: TcxGridDBColumn;
    Tv1C_PlanQty: TcxGridDBColumn;
    TbJiesuan: TToolButton;
    TbNoJiesuan: TToolButton;
    TbTongBu: TToolButton;
    Tv1finishDescription: TcxGridDBColumn;
    N3: TMenuItem;
    N4: TMenuItem;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBcopyClick(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TbXiadaClick(Sender: TObject);
    procedure TbNoXiadaClick(Sender: TObject);
    procedure TbQuxiaoClick(Sender: TObject);
    procedure TbNoQuxiaoClick(Sender: TObject);
    procedure Picture4DblClick(Sender: TObject);
    procedure Label3DblClick(Sender: TObject);
    procedure Label9DblClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TbJiesuanClick(Sender: TObject);
    procedure TbNoJiesuanClick(Sender: TObject);
    procedure TbTongBuClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    function DelData(): Boolean;
    procedure InitSub();
    procedure SetStatus();
    procedure InitImage(fwbid: string);
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2, FWorkshop: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmDyePlanList: TfrmDyePlanList;

implementation

uses
  U_DataLink, U_DyePlanInPut, U_RTFun, U_LabelPrint, U_DyeInspSet;
{$R *.dfm}

procedure TfrmDyePlanList.InitImage(fwbid: string);
begin
  ADOQueryImage.close;
  if fwbid = '' then
    exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID=' + quotedstr(trim(fwbid)));
    open;
  end;
end;

procedure TfrmDyePlanList.SetStatus();
begin

  TBadd.Enabled := false;        //新增
  tbedit.Enabled := false;       //修改
  tbdel.Enabled := false;        //删除
  TBcopy.Enabled := false;       //复制

  TbXiada.Enabled := false;
  TbNoXiada.Enabled := false;
  TbJiesuan.Enabled := false;
  TbNoJiesuan.Enabled := false;
  TbQuxiao.Enabled := false;
  TbNoQuxiao.Enabled := false;

  if Trim(canshu1) = '管理' then
  begin
    TBadd.Enabled := true;
    TBcopy.Enabled := true;
    case cxTabControl1.TabIndex of
      0:
        begin
          tbedit.Enabled := true;
          tbdel.Enabled := true;

          TbXiada.Enabled := true;
        end;
      1:
        begin
          TbNoXiada.Enabled := true;
          TbJiesuan.Enabled := True;

        end;
      2:
        begin
          TbNoJiesuan.Enabled := true;
          TbQuxiao.Enabled := True;
        end;
      3:
        begin
          TbNoQuxiao.Enabled := True;
        end;
    end;
  end;

  if Trim(canshu1) = '录入' then
  begin
    TBadd.Enabled := true;
    TBcopy.Enabled := true;
    case cxTabControl1.TabIndex of
      0:
        begin
      // tchk.Visible:=true;
          tbedit.Enabled := true;
          tbdel.Enabled := true;
      // TBQX.Visible:=true;
        end;
      1:
        begin
      //  tnochk.Visible:=true;
        end;
      2:
        begin
        end;
      3:
        begin
    //    TBNOQX.Visible:=true;
        end;
    end;
  end;
end;

procedure TfrmDyePlanList.InitSub();
begin
  if Order_Main.IsEmpty then
    exit;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from Dye_Plan_Sub ');
    sql.Add('where mainID =' + quotedstr((Order_Main.fieldbyname('mainID').AsString)));
    open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);
end;

procedure TfrmDyePlanList.Label3DblClick(Sender: TObject);
var
  I: Integer;
  Column: TcxGridDBColumn;
  FieldNames: TStringList;
begin
  FieldNames := TStringList.Create;
  try
    for I := 0 to Tv1.ColumnCount - 1 do
    begin
      Column := Tv1.Columns[I];
      FieldNames.Add(Column.Caption + ':' + Column.DataBinding.FieldName);
    end;
    ShowMessage(FieldNames.Text);
  finally
    FieldNames.Free;
  end;
end;

procedure TfrmDyePlanList.Label9DblClick(Sender: TObject);
var
  I: Integer;
  Column: TcxGridDBColumn;
  FieldNames: TStringList;
begin
  FieldNames := TStringList.Create;
  try
    for I := 0 to Tv2.ColumnCount - 1 do
    begin
      Column := Tv2.Columns[I];
      FieldNames.Add(Column.Caption + ':' + Column.DataBinding.FieldName);
    end;
    ShowMessage(FieldNames.Text);
  finally
    FieldNames.Free;
  end;
end;

procedure TfrmDyePlanList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyePlanList := nil;
end;

procedure TfrmDyePlanList.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_ESCAPE then  // VK_ESCAPE 是 ESC 键的常量
  begin
    Key := 0;  // 清除按键，防止继续传递
    Close;     // 或者 ModalResult := mrCancel;
  end;
end;

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

procedure TfrmDyePlanList.FormCreate(Sender: TObject);
begin
  inherited;
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);
  canshu1 := trim(Self.fParameters1);
  canshu2 := trim(Self.fParameters2);
  FWorkshop := trim(Self.fParameters3);
end;

procedure TfrmDyePlanList.TBCloseClick(Sender: TObject);
begin
  Close;

end;

procedure TfrmDyePlanList.InitGrid();
begin

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add('select  A.* ');
      sql.Add('from Dye_Plan_MAIN A');
//      sql.Add('inner join  Dye_Plan_Sub B on B.MainId=A.MainId');
      sql.Add(' where 1=1');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add('  and  isnull(A.status,''0'') in(''已计划'') ');
          end;
        1:
          begin
            sql.Add('  and  isnull(A.status,''0'')in(''已下达'' )');
          end;
        2:
          begin
            sql.Add('  and  isnull(A.status,''0'')in(''已结算'' )');
          end;
        3:
          begin
            sql.Add('  and  isnull(A.status,''0'')in(''已取消'' )');
          end;
//        4:
//          begin
//            sql.Add('  and  isnull(A.status,''0'')in(''已结算'' )');
//          end;
      end;
      sql.Add(' and OrdDate>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
      sql.Add(' and OrdDate<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
//      sql.Add(' and A.Workshop=''' + trim(FWorkshop) + ''' ');
      if Trim(canshu1) = '录入' then
        sql.Add('and Filler=' + QuotedStr(Trim(DName)));
//      ShowMessage(sql.Text);
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, Order_Main);
    SInitCDSData(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

procedure TfrmDyePlanList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
{  if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;   }
  try
    frmDyePlanInPut := TfrmDyePlanInPut.Create(Application);
    with frmDyePlanInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FOrderNo := Trim(Self.Order_Main.fieldbyname('OrderNo').AsString);
      canshu1 := self.canshu1;
      canshu2 := self.canshu2;
      FWorkshop := self.FWorkshop;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmDyePlanInPut.Free;
  end;
end;

procedure TfrmDyePlanList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if not Order_Main.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据！', '提示信息', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  while Order_Main.Locate('SSel', True, []) do
  begin
    if (Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName)) and (dname <> 'ADMIN') then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Dye_Plan_Card where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      Open;
    end;
    if not ADOQueryTemp.IsEmpty then
    begin
      Application.MessageBox('已开卡不能删除数据!', '提示', 0);
      Exit;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Dye_Cloth_Inspect where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      Open;
    end;

    if ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已检验不能删除数据!', '提示', 0);
      Exit;
    end;

    if DelData() then
    begin

    end;
    Order_Main.Delete;
  end;

end;

function TfrmDyePlanList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Dye_Plan_Main where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      sql.Add('delete Dye_Plan_Sub where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      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('指示单删除')));
      sql.Add(',' + quotedstr(trim('指示单号：' + trim(Order_Main.FieldByName('OrderNO').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmDyePlanList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
//  SelExportData(Tv1,ADOQueryMain,self.Caption);
end;

procedure TfrmDyePlanList.TBPrintClick(Sender: TObject);
var
  WSql: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  WSql := QuotedStr(Trim(Order_Main.fieldbyname('MainId').AsString));

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'DyePlanPrint';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

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

procedure TfrmDyePlanList.TBAddClick(Sender: TObject);
var
  maxno: string;
begin
  try
    frmDyePlanInPut := TfrmDyePlanInPut.Create(Application);
    with frmDyePlanInPut do
    begin
      PState := 0;
      FMainId := '';
      canshu1 := self.canshu1;
      canshu2 := self.canshu2;
      FWorkshop := self.FWorkshop;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmDyePlanInPut.Free;
  end;
end;

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

procedure TfrmDyePlanList.FormShow(Sender: TObject);
begin
  inherited;
  fFlileFlag := UserDataFlag + 'HX';
  canshu1 := self.fParameters1;
  canshu2 := self.fParameters2;
  FWorkshop := self.fParameters3;
  BegDate.DateTime := SGetServerDate(ADOQueryTemp) - 15;
  EndDate.DateTime := SGetServerDate(ADOQueryTemp);
  ReadCxGrid(self.Caption + 'TV1', Tv1, '生产指示单管理');
  ReadCxGrid(self.Caption + 'TV2', Tv2, '生产指示单管理');
  SetStatus();
  InitGrid();
  KeyPreview := True;
end;

procedure TfrmDyePlanList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if ToolButton1.Visible = False then
    Exit;
  TBPrint.Click;
end;

procedure TfrmDyePlanList.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmDyePlanList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmDyePlanList.Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id, id10: Integer;
begin

end;

procedure TfrmDyePlanList.v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 { Id:=TV1.GetColumnByFieldName('DeliveryDate').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('SubStatus').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id10]='完成' then Exit;
  if AViewInfo.GridRecord.Values[Id]-SGetServerDate(ADOQueryTemp)>=4 then Exit;
  if ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))>=0) and ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))<4) then
    ACanvas.Brush.Color:=clYellow
  else
  if (AViewInfo.GridRecord.Values[id])-(SGetServerDate(ADOQueryTemp)<0) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  if AViewInfo.GridRecord.Values[Id]='Purple' then
  begin
    ACanvas.Brush.Color:=clPurple;
  end else
  if AViewInfo.GridRecord.Values[Id]='Olive' then
  begin
    ACanvas.Brush.Color:=clOlive;
  end else
  if AViewInfo.GridRecord.Values[Id]='Teal' then
  begin
    ACanvas.Brush.Color:=clTeal;
  end else
  if AViewInfo.GridRecord.Values[Id]='Background' then
  begin
    ACanvas.Brush.Color:=clBackground;
  end;   }
end;

procedure TfrmDyePlanList.N1Click(Sender: TObject);
var
  topIndex: Integer;
begin
  topIndex := tv1.Controller.TopRowIndex;
  SelOKNoFiler(Tv1, false);
  tv1.Controller.TopRowIndex := topIndex;
end;

procedure TfrmDyePlanList.N2Click(Sender: TObject);
var
  topIndex: Integer;
begin
  topIndex := tv1.Controller.TopRowIndex;
  SelOKNoFiler(Tv1, True);
  tv1.Controller.TopRowIndex := topIndex;
end;

procedure TfrmDyePlanList.N3Click(Sender: TObject);
var
  InputValues: array[0..1] of string;
  StartRow, EndRow, i: Integer;
  topIndex: Integer;
begin
  topIndex := tv1.Controller.TopRowIndex;

  // 初始化为空字符串
  InputValues[0] := '';
  InputValues[1] := '';

  if InputQuery('选择行范围', ['起始行:', '结束行:'], InputValues) then
  begin
    // 检查是否为空
    if InputValues[0] = '' then
    begin
      ShowMessage('请输入起始行');
      exit;
    end;

    if InputValues[1] = '' then
    begin
      ShowMessage('请输入结束行');
      exit;
    end;

    // 转换输入值
    StartRow := StrToIntDef(InputValues[0], 0);
    EndRow := StrToIntDef(InputValues[1], 0);

    // 验证范围
    if StartRow <= 0 then
    begin
      ShowMessage('起始行必须大于0');
      exit;
    end;

    if EndRow < StartRow then
    begin
      ShowMessage('结束行不能小于起始行');
      exit;
    end;

//    if EndRow > Order_Main.RecordCount then
//    begin
//      ShowMessage(Format('结束行不能超过总行数(%d)', [Order_Main.RecordCount]));
//      Exit;
//    end;
    // 循环设置指定范围内的行
    try
//      Order_Main.First;
//      for i := 1 to Order_Main.RecordCount do
//      begin
//        if (i >= StartRow) and (i <= EndRow) then
//        begin
//          Order_Main.Edit;
//          Order_Main.FieldByName('ssel').Value := True;
//          Order_Main.post;
//        end;
//
//        Order_Main.Next;
//      end;
      Tv1.BeginUpdate();
      for i := 0 to Tv1.DataController.RecordCount - 1 do
      begin
        Tv1.DataController.FocusedRowIndex := i;

        if (i + 1 >= StartRow) and (i + 1 <= EndRow) then
        begin
          Order_Main.Edit;
          Order_Main.FieldByName('ssel').Value := True;
          Order_Main.Post;
        end;

        if i + 1 >= EndRow then
          Break;
      end;
      Tv1.endUpdate();
    finally

    end;

  end;

  tv1.Controller.TopRowIndex := topIndex;
end;

procedure TfrmDyePlanList.N4Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmDyePlanList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmDyePlanInPut := TfrmDyePlanInPut.Create(Application);
    with frmDyePlanInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      ToolBar2.Visible := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := False;
      Tv1.OptionsSelection.CellSelect := False;
      FWorkshop := self.FWorkshop;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmDyePlanInPut.Free;
  end;
end;

procedure TfrmDyePlanList.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption + 'TV1', Tv1, '生产指示单管理');
  WriteCxGrid(self.Caption + 'TV2', Tv2, '生产指示单管理');
end;

procedure TfrmDyePlanList.ToolButton3Click(Sender: TObject);
begin
  try
    frmDyeInspSet := TfrmDyeInspSet.Create(Application);
    with frmDyeInspSet do
    begin

      FDataID := Trim(Self.Order_Main.fieldbyname('MainId').AsString);

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmDyeInspSet.Free;
  end;
end;

procedure TfrmDyePlanList.TbJiesuanClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if not Order_Main.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据！', '提示信息', 0);
    Exit;
  end;
  try
    while Order_Main.Locate('SSel', True, []) do
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  Dye_Plan_Main SET status=''已结算'' ');
        sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));

        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('订单结算')));
        sql.Add(',' + quotedstr(trim('订单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;
      Order_Main.Delete;
    end;

    application.MessageBox('订单结算成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('订单结算失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyePlanList.TbNoJiesuanClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if not Order_Main.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据！', '提示信息', 0);
    Exit;
  end;
  try
    while Order_Main.Locate('SSel', True, []) do
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  Dye_Plan_Main SET status=''已下达'' ');
        sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
        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('订单撤销结算')));
        sql.Add(',' + quotedstr(trim('订单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;
      Order_Main.Delete;
    end;
    application.MessageBox('订单撤销结算成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('订单撤销结算失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyePlanList.TBcopyClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmDyePlanInPut := TfrmDyePlanInPut.Create(Application);
    with frmDyePlanInPut do
    begin
      PState := 1;
      CopyInt := 99;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      canshu1 := self.canshu1;
      canshu2 := self.canshu2;
      FWorkshop := self.FWorkshop;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmDyePlanInPut.Free;
  end;
end;

procedure TfrmDyePlanList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitSub();
end;

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

procedure TfrmDyePlanList.TbTongBuClick(Sender: TObject);
begin
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_OrderCR');

//      ShowMessage(sql.Text);
      execsql;
    end;

    // 同步成功提示
    ShowMessage('同步成功！');

  except
    on E: Exception do
    begin
      // 同步失败提示
      ShowMessage('同步失败：' + E.Message);
    end;
  end;
end;

procedure TfrmDyePlanList.TbXiadaClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if not Order_Main.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据！', '提示信息', 0);
    Exit;
  end;
  try
    while Order_Main.Locate('SSel', True, []) do
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  Dye_Plan_Main SET status=''已下达'' ');
        sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));

        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('订单下达')));
        sql.Add(',' + quotedstr(trim('订单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;
      Order_Main.Delete;
    end;

    application.MessageBox('订单下达成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('订单下达失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyePlanList.TbNoXiadaClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if not Order_Main.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据！', '提示信息', 0);
    Exit;
  end;
  try
    while Order_Main.Locate('SSel', True, []) do
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  Dye_Plan_Main SET status=''已计划'' ');
        sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
        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('订单撤销下达')));
        sql.Add(',' + quotedstr(trim('订单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;
      Order_Main.Delete;
    end;
    application.MessageBox('订单撤销下达成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('订单撤销下达失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyePlanList.TbQuxiaoClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if not Order_Main.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据！', '提示信息', 0);
    Exit;
  end;
  try
    while Order_Main.Locate('SSel', True, []) do
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  Dye_Plan_Main SET status=''已取消'' ');
        sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
        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('订单取消')));
        sql.Add(',' + quotedstr(trim('订单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;
      Order_Main.Delete;
    end;
    application.MessageBox('订单取消成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('订单取消失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyePlanList.TbNoQuxiaoClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if not Order_Main.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据！', '提示信息', 0);
    Exit;
  end;
  try
    while Order_Main.Locate('SSel', True, []) do
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  Dye_Plan_Main SET status=''已结算'' ');
        sql.Add('where mainID=' + quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
        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('订单撤销取消')));
        sql.Add(',' + quotedstr(trim('订单号：' + trim(Order_Main.FieldByName('orderNO').AsString))));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;
      Order_Main.Delete;
    end;
    application.MessageBox('订单撤销完成成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('订单撤销完成失败！', '提示信息', 0);
  end;
end;

//procedure TfrmDyePlanList.TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
//begin
//  initImage(ADOQuerySub.fieldbyname('HXFile').AsString);
//end;

procedure TfrmDyePlanList.Picture4DblClick(Sender: TObject);
var
  sFieldName: string;
  fileName: string;
 //	ff: TADOBlobStream;
 // FJStream : TMemoryStream;
begin
//  if Picture4.Picture.Height = 0 then
//    exit;
  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);
//  fileName := ADOQuerySub.fieldbyname('hxFile').AsString;
  sFieldName := sFieldName + '\' + trim(fileName);

  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin

    application.ProcessMessages;
    try
      IdFTP1.Get(fFlileFlag + '\' + Trim(fileName), sFieldName, true, false);
    except
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);

end;

end.

