unit U_DyeOutputList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, MovePanel, cxButtonEdit, cxCalendar, RM_System, RM_Common,
  RM_Class, RM_GridReport, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu,
  U_BaseList, dxScrollbarAnnotations, Vcl.Menus,Clipbrd;

type
  TfrmDyeOutputList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    KaZu: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Label5: TLabel;
    orderNo: TEdit;
    MovePanel2: TMovePanel;
    Label10: TLabel;
    RMGridReport1: TRMGridReport;
    Label16: TLabel;
    customerNoName: TEdit;
    Label13: TLabel;
    ywy: TEdit;
    Label17: TLabel;
    Tv1Column2: TcxGridDBColumn;
    Tv1KaZu: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    C_Color: TEdit;
    Label4: TLabel;
    C_ColorNo: TEdit;
    Label6: TLabel;
    lbHx: TEdit;
    Label7: TLabel;
    TBdel: TToolButton;
    GlideName: TComboBox;
    StaffName: TEdit;
    Label9: TLabel;
    Tv1Column15: TcxGridDBColumn;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    ToolButton1: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Panel2: TPanel;
    Label8: TLabel;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Tv1Column16: TcxGridDBColumn;
    Tv1finishDescription: TcxGridDBColumn;
    Tv1GlideNo: TcxGridDBColumn;
    Tv1F_Name: TcxGridDBColumn;
    Tv1QtyOrder: TcxGridDBColumn;
    Tv1C_PlanQty: TcxGridDBColumn;
    Tv1F_description: TcxGridDBColumn;
    Tv1C_description: TcxGridDBColumn;
    Tv1PCType: TcxGridDBColumn;
    Tv1PCTime: TcxGridDBColumn;
    Tv1DTMPSlist: TcxGridDBColumn;
    Tv1CINote: TcxGridDBColumn;
    Tv1Filler: TcxGridDBColumn;
    Tv1Ender: TcxGridDBColumn;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    Tv1UseTime: TcxGridDBColumn;
    Tv1actualSpeed: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure KaZuChange(Sender: TObject);
    procedure PRTColorChange(Sender: TObject);
    procedure TBdelClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure N4Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
  private
    FLeft, FTop: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    fsyrName, canshu1: string;
    { Public declarations }
  end;

var
  frmDyeOutputList: TfrmDyeOutputList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmDyeOutputList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyeOutputList := nil;
end;

procedure TfrmDyeOutputList.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 TfrmDyeOutputList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmDyeOutputList.FormCreate(Sender: TObject);
begin
  inherited;

  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;
  fsyrName := trim(DdataBase);
  canshu1 := trim(DParameters1);
  SInitComBoxBySql(ADOQueryTemp, GlideName, false, 'select PSName as Name from Dye_Process');
end;

procedure TfrmDyeOutputList.InitGrid();
var
  Msql, mbegdate, menddate: string;
begin
  Msql := '';
  if SGetFilters(Panel1, 1, 2) <> '' then
    Msql := Msql + ' and ' + SGetFilters(Panel1, 1, 2);
  mbegdate := FormatDateTime('yyyy-MM-dd', begdate.Date) + ' ' + FormatDateTime('HH:nn', begTime.Time);
  menddate := FormatDateTime('yyyy-MM-dd', enddate.Date) + ' ' + FormatDateTime('HH:nn', endTime.Time);
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      Filtered := False;
//      sql.Add('select GOID,A.GlideName,Qty,rollnum,groupName,B.*,a.filler as filler2,a.filltime as filltime2,gotime,QtyUnit,StaffName  from Dye_Plan_Glide_OutPut A  ');
      sql.Add('select * from V_Dye_Plan_Glide_OutPut A');
      sql.add('where A.GOTime>=''' + trim(mbegdate) + ''' ');
      sql.Add('and A.GOTime<''' + trim(menddate) + ''' ');
      sql.Add(Msql);
    //  showmessage(sql.Text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_Main);
    SInitCDSData(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  TBFind.Click;
end;

procedure TfrmDyeOutputList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, true);
end;

procedure TfrmDyeOutputList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmDyeOutputList.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));

end;

procedure TfrmDyeOutputList.N4Click(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;

    // 循环设置指定范围内的行
    CDS_Main.DisableControls;
    try
      CDS_Main.First;
      for i := 1 to CDS_Main.RecordCount do
      begin
        if (i >= StartRow) and (i <= EndRow) then
        begin
          CDS_Main.Edit;
          CDS_Main.FieldByName('ssel').Value := True;
          CDS_Main.post;
        end;

        CDS_Main.Next;
      end;
    finally
      CDS_Main.EnableControls;
    end;

  end;

  tv1.Controller.TopRowIndex := topIndex;
end;

procedure TfrmDyeOutputList.TBRafreshClick(Sender: TObject);
begin
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

procedure TfrmDyeOutputList.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption + tv1.Name, Tv1, '成品仓库');
end;

procedure TfrmDyeOutputList.ToolButton2Click(Sender: TObject);
begin
  inherited;
  if canshu1 <> '修改' then
    exit;

  Panel2.Visible := TRUE;
end;

procedure TfrmDyeOutputList.Button1Click(Sender: TObject);
var
  FReal: Double;
begin
  if CDS_Main.IsEmpty then
    exit;
  if TryStrToFloat(EDIT1.Text, FReal) = False then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Dye_Plan_Glide_OutPut SET QTY=' + (edit1.text));
      sql.Add('where GOID=' + quotedstr(trim(CDS_Main.fieldbyname('GOID').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(CDS_Main.FieldByName('GOID').AsString) + '原数量' + trim(CDS_Main.FieldByName('qty').AsString) + ';现 ' + trim(edit1.Text)));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');

      execsql;

    end;
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      SQL.Add('SELECT maxglideno =MAX(GlideNo)   FROM Dye_Plan_Glide A  where PCID= ' + quotedstr(trim(CDS_Main.fieldbyname('pcid').AsString)));
      open;
    end;
    if (CDS_Main.FieldByName('GlideNo').AsString = ADOQueryTemp.FieldByName('maxGlideNo').AsString) then
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('exec P_Dye_Card_CRCP @PCID=' + QuotedStr(CDS_Main.fieldbyname('pcid').AsString));
        execsql;
      end;
    end;

    application.MessageBox('修改成功！', '提示信息');
    ADOQueryCmd.Connection.CommitTrans;
    PANEL2.Visible := FALSE;
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('修改失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyeOutputList.Button2Click(Sender: TObject);
begin
  inherited;
  panel2.Visible := false;
end;

procedure TfrmDyeOutputList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmDyeOutputList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmDyeOutputList.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.Caption + tv1.Name, Tv1, '成品仓库');
  canshu1 := self.fParameters1;

  initgrid();
  KeyPreview := True;  // 确保表单可以预览按键
end;

procedure TfrmDyeOutputList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
//  SelExportData(tv1,ADOQueryMain,self.Caption);
  TcxGridToExcel(self.Caption, cxGrid2);
end;

procedure TfrmDyeOutputList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_Main);
    SInitCDSData(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmDyeOutputList.KaZuChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmDyeOutputList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmDyeOutputList.TBdelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Dye_Need_Up(UType,UDataId) values(''机台刷卡'',' + quotedstr(Trim(CDS_Main.fieldbyname('GOID').AsString)) + ')  ');
      sql.Add('delete Dye_Plan_Glide_OutPut where GOID=''' + Trim(CDS_Main.fieldbyname('GOID').AsString) + '''');

      ExecSQL;
    end;
//////////////////       更新进度       //////////////////
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Dye_Card_upCL @PCID=' + QuotedStr(CDS_Main.fieldbyname('pcid').AsString));

      execsql;
    end;
//////////////////       更新进度       //////////////////
    CDS_Main.Delete;
  except
    application.MessageBox('删除失败！', '提示信息', 0);
  end;

end;

end.

