unit U_DyePlanCardList;

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, cxCheckBox, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu, dxSkinsCore, dxSkinsDefaultPainters,
  dxDateRanges, IdExplicitTLSClientServerBase, U_BaseList,
  dxScrollbarAnnotations, Clipbrd;

type
  TfrmDyePlanCardList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    v1OrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    CDS_1: TClientDataSet;
    RM1: TRMGridReport;
    RMDB_1: TRMDBDataSet;
    v1CustomerNoName: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    Label8: TLabel;
    C_Name: TEdit;
    Label9: TLabel;
    C_description: TEdit;
    v1MPRTKZ: TcxGridDBColumn;
    Label12: TLabel;
    MPRTKZ: TEdit;
    ADOQueryPrint: TADOQuery;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    v1Column6: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    cxStyle1: TcxStyle;
    v1Column12: TcxGridDBColumn;
    PCID: TEdit;
    Label2: TLabel;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    KaZu: TEdit;
    KaZulabrl: TLabel;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    ToolButton6: TToolButton;
    cxTabControl1: TcxTabControl;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    tbedit: TToolButton;
    btnWC: TToolButton;
    btnnowc: TToolButton;
    Tv1CurGlideName: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Filltime: TcxGridDBColumn;
    Tv1Chker: TcxGridDBColumn;
    Tv1Chktime: TcxGridDBColumn;
    Tv1TotalPCID: TcxGridDBColumn;
    Tv1TotalPCIDNum: TcxGridDBColumn;
    TbDetailGY: TToolButton;
    Tv1KaZu: TcxGridDBColumn;
    Tv1C_description: TcxGridDBColumn;
    TbUpdateGY: TToolButton;
    TbSimpleGY: TToolButton;
    Tv1CINote: TcxGridDBColumn;
    Tv1C_Color: TcxGridDBColumn;
    PCType: TComboBox;
    Label13: TLabel;
    Tv1KaZuQty: TcxGridDBColumn;
    Tv1KaZuPiece: TcxGridDBColumn;
    TbAbnormal: TToolButton;
    TnoAbnormal: TToolButton;
    N3: TMenuItem;
    Tv1isStop: TcxGridDBColumn;
    Tv1Status: TcxGridDBColumn;
    N4: TMenuItem;
    Tv1GYDTMNo: TcxGridDBColumn;
    Tv1GYDTMName: TcxGridDBColumn;
    Tv1GYEditer: TcxGridDBColumn;
    Tv1GYEdittime: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(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 CheckBox2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure Label2DblClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure tbeditClick(Sender: TObject);
    procedure btnWCClick(Sender: TObject);
    procedure btnnowcClick(Sender: TObject);
    procedure TbDetailGYClick(Sender: TObject);
    procedure TbUpdateGYClick(Sender: TObject);
    procedure TbSimpleGYClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure Tv1CurGlideNameStylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; var AStyle: TcxStyle);
    procedure TbAbnormalClick(Sender: TObject);
    procedure TnoAbnormalClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure N4Click(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure SetStatus();
    procedure InitForm();
//    procedure SetStatus();
    procedure PrintCar(MPCID: string);
    function UpdateQRCodeWithPrtCount(ADOQuery: TADOQuery; const PCID: string): Boolean;

    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2, FWorkshop: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmDyePlanCardList: TfrmDyePlanCardList;

implementation

uses
  U_DataLink, U_RTFun, U_DyePlanCardInput, U_LabelPrint, U_DyeTechnicsinputKaika,
  U_DyeSimpleTechnicsKaiKa;
{$R *.dfm}

procedure TfrmDyePlanCardList.PrintCar(MPCID: string);
var
  fPrintFile: string;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_Dye_Card_Prt ');
    sql.Add(' @Filtration=' + quotedstr(trim(MPCID)));
    Open;
  end;

  ExportFtErpFile('流程卡.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\流程卡.rmf';

  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.PrintReport;
    sleep(200);
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Dye_Plan_Card SET status=''9'' ');
      sql.Add('where PCID=' + quotedstr(trim(CDS_1.fieldbyname('PCID').AsString)));
      execsql;
    end;
  end
  else
    Application.MessageBox(PChar('没有找到' + trim(fPrintFile)), '提示信息', 0);
end;

procedure TfrmDyePlanCardList.SetStatus();
begin

  tchk.Enabled := false;
  tnochk.Enabled := false;
  tbadd.Enabled := false;
  tbedit.Enabled := false;
  tbdel.Enabled := false;
  btnWC.Enabled := false;
  btnnowc.Enabled := false;
  TbAbnormal.Enabled := False;
  TnoAbnormal.Enabled := false;

  tbadd.Enabled := true;
  if Trim(canshu1) = '录入' then
  begin
    tchk.Visible := False;
    tnochk.Visible := False;
    case cxTabControl1.TabIndex of
      0:
        begin
          TbAbnormal.Enabled := true;
          tbedit.Enabled := true;
          tbdel.Enabled := true;
        end;
      1:
        begin
          btnWC.Enabled := true;
        end;
      2:
        begin
          btnnowc.Enabled := true;
        end;
      3:
        begin
          TnoAbnormal.Enabled := false;
        end;
    end;
  end;
  if Trim(canshu1) = '管理' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          TbAbnormal.Enabled := true;
          tbedit.Enabled := true;
          tbdel.Enabled := true;
          tchk.Enabled := true;

        end;
      1:
        begin
          btnWC.Enabled := true;
          tnochk.Enabled := true;
        end;
      2:
        begin

          btnnowc.Enabled := true;
        end;
      3:
        begin
          TnoAbnormal.Enabled := true;
        end;
    end;
  end;
end;

procedure TfrmDyePlanCardList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyePlanCardList := nil;
end;

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

procedure TfrmDyePlanCardList.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 TfrmDyePlanCardList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmDyePlanCardList.InitGrid();
var
  strwhere: string;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add('select A.*,ConNo,OrdType,Salestype ,OrdDate,DlyDate');
      sql.Add(',GlideList=[dbo].[F_Dye_Card_GlideList](A.PCID) ');
      sql.Add(',KaZuQty=(SELECT SUM(PCQty) from Dye_Plan_Card X where X.KaZu = A.KaZu)');
      sql.Add(',KaZuPiece=(SELECT SUM(PCPiece) from Dye_Plan_Card X where X.KaZu = A.KaZu)');
      SQL.Add(',D.DTMNo as GYDTMNo,D.DTMName as GYDTMName,D.Editer as GYEditer,D.Edittime as GYEdittime ');
      sql.Add('from Dye_Plan_Card A ');
      sql.Add('left join Dye_Plan_sub B on B.subID=A.subID ');
      sql.Add('left join Dye_Plan_Main C on C.mainID=A.mainID ');
      SQL.Add(' left join Dye_Technics_Main D on D.DTMID=A.DTMID ');
      sql.Add('where A.PCTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.Add('and A.PCTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add('  and  isnull(A.status,''0'')= ''0'' ');     //未审核
          end;
        1:
          begin
            sql.Add('  and  isnull(A.status,''0'')=''9'' ');    //已审核
          end;
        2:
          begin
            sql.Add('  and  isnull(A.status,''0'')=''10'' ');   //已完成
          end;
        3:
          begin
            sql.Add('  and  isnull(A.status,''0'')=''-1'' ');   //异常卡
          end;

      end;
//      if trim(FWorkshop) <> '' then
//        sql.Add(' and C.Workshop=''' + trim(FWorkshop) + ''' ');
      if Trim(canshu1) = '' then
        sql.Add(' and A.Filler=''' + trim(DName) + ''' ');
      SQL.Add(' Order By PCID ');

//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmDyePlanCardList.Label2DblClick(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 TfrmDyePlanCardList.InitForm();
begin
  ReadCxGrid(self.Caption + 'TV1', Tv1, '生产指示单管理');
  BegDate.DateTime := SGetServerDate(ADOQueryTemp) - 3;
  EndDate.DateTime := SGetServerDate(ADOQueryTemp);

  SetStatus();

  InitGrid()
end;

procedure TfrmDyePlanCardList.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 TfrmDyePlanCardList.TBDelClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
//  if (Trim(CDS_1.fieldbyname('Filler').AsString) <> Trim(DName)) and (dname <> 'ADMIN') then
//  begin
//    Application.MessageBox('不能操作他人的数据!', '提示', 0);
//    Exit;
//  end;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          try
            ADOQueryCmd.Connection.BeginTrans;
//            with ADOQueryCmd do
//            begin
//              Close;
//              Sql.Clear;
//              Sql.Add('exec P_BS_Cloth_Out_Del ');
//              Sql.Add(' @BCIOIDS=' + quotedstr(Trim(CDS_1.fieldbyname('BCIOID').AsString)));
//              Sql.Add(',@DCode=' + quotedstr(Trim(CDS_1.fieldbyname('C_Code').AsString)));
//              Sql.Add(',@DName=' + quotedstr(Trim(CDS_1.fieldbyname('C_Name').AsString)));
//              Open;
//            end;
//            if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
//              raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));

            with ADOQueryCmd do
            begin
              Close;
              Sql.Clear;
              //删除流程卡
              Sql.Add('exec P_Dye_Card_Del ');
              Sql.Add('@PCIDS=' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
              Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
              Sql.Add(',@DName=' + quotedstr(Trim(DName)));
              Open;
            end;
            if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
              raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));

            with ADOQueryTemp do
            begin
              Close;
              SQL.Clear;
              SQL.Add('select * from Dye_Plan_card where KaZu = ' + QuotedStr(Trim(CDS_1.FieldByName('KaZu').AsString)));
              Open;
            end;
            //最后卡组最后一张卡删除，清除卡组数据
            if ADOQueryTemp.IsEmpty then
            begin
              with ADOQueryCmd do
              begin
                Close;
                Sql.Clear;
                Sql.Add('delete from Dye_Plan_glide where PCID = ' + quotedstr(Trim(CDS_1.fieldbyname('KaZu').AsString)));
                ExecSQL;
              end;

              with ADOQueryCmd do
              begin
                Close;
                Sql.Clear;
                Sql.Add('delete from Bs_Cloth_GY_Main_Kaika where PCID = ' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
                ExecSQL;
              end;

              with ADOQueryCmd do
              begin
                Close;
                Sql.Clear;
                Sql.Add('delete from Bs_Cloth_GY_Sub_Kaika where PCID = ' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
                ExecSQL;
              end;
            end;
           //恢复库存取消限制正常卡
//            if (Trim(CDS_1.fieldbyname('PCType').AsString) = '正常卡') then
//            begin
              with ADOQueryCmd do
              begin
                Close;
                Sql.Clear;
                //删除出库记录并且更新库存
                Sql.Add('exec P_Dye_GreyCloth_Out_Del ');
                Sql.Add('@PCID=' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
                Open;
              end;
              if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
                raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
//            end;
//            else
//            begin
//              with ADOQueryCmd do
//              begin
//                Close;
//                Sql.Clear;
//                Sql.Add('exec P_Dye_Card_Move_Del ');
//                Sql.Add('@PCID=' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
//                Open;
//              end;
//              if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
//                raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
//            end;

            ADOQueryCmd.Connection.CommitTrans;
          except
            ADOQueryCmd.Connection.RollbackTrans;
            raise Exception.Create(PChar(Exception(ExceptObject).Message));
          end;
        end;

        with ADOQueryCmd do
        begin
          Close;
          Sql.Clear;
          //更新总卡号分卡号
          Sql.Add('exec P_Dye_Plan_UpdateOrderNo ');
          Sql.Add('@KaZu=' + quotedstr(Trim(CDS_1.fieldbyname('KaZu').AsString)));
          Open;
        end;
        Next;
      end;
      EnableControls;
    end;

    TBRafresh.Click;
  except
    CDS_1.EnableControls;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

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

function TfrmDyePlanCardList.UpdateQRCodeWithPrtCount(ADOQuery: TADOQuery; const PCID: string): Boolean;
var
  mQRCode: string;
  mPrtCount: Integer;
  NewQRCode: string;
begin
  Result := False;

  try
    with ADOQuery do
    begin
      // 查询数据
      Close;
      SQL.Clear;
      SQL.Add('SELECT QRCode, PrtCount FROM Dye_Plan_Card WHERE PCID = ' + QuotedStr(Trim(PCID)));
      Open;

      if not IsEmpty then
      begin
        // 获取字段值
        mQRCode := FieldByName('QRCode').AsString;
        mPrtCount := FieldByName('PrtCount').AsInteger;

        // 检查QRCode是否为空
        if mQRCode <> '' then
        begin
          // 将最后一位替换为PrtCount
          NewQRCode := Copy(mQRCode, 1, Length(mQRCode) - 1) + IntToStr(mPrtCount);

          // 更新记录
          Edit;
          FieldByName('QRCode').AsString := NewQRCode;
          Post;

          Result := True;
        end;
      end;
    end;
  except
    on E: Exception do
    begin
      // 处理异常
      Application.MessageBox(PChar('更新QRCode时发生错误: ' + E.Message), '错误', MB_ICONERROR);
      Result := False;
    end;
  end;
end;

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

  MPCID := '';
  CDS_1.DisableControls;
  with CDS_1 do
  begin

    First;
    while CDS_1.Locate('SSel', True, []) do
    begin
      if MPCID = '' then
      begin
        MPCID := Trim(CDS_1.fieldbyname('PCID').AsString);
      end
      else
      begin
        MPCID := MPCID + ',' + Trim(CDS_1.fieldbyname('PCID').AsString);
      end;

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

      with Self.ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add(' update Dye_Plan_Card set PrtCount = PrtCount + 1 ');
        sql.add(' where PCID = ' + QuotedStr(Trim(CDS_1.fieldbyname('PCID').AsString)));
        ExecSQL;
      end;

      UpdateQRCodeWithPrtCount(ADOQueryCmd, CDS_1.fieldbyname('PCID').AsString);

    end;
  end;
  CDS_1.EnableControls;

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

  finally
    frmLabelPrint.Free;
  end;
end;
//var
//  i: integer;
//  MPCID: string;
//begin
//  if CDS_1.IsEmpty then
//    Exit;
//  if CDS_1.Locate('SSel', True, []) = False then
//  begin
//    Application.MessageBox('没有选择数据!', '提示', 0);
//    Exit;
//  end;
//  i := 0;
//  with CDS_1 do
//  begin
//    DisableControls;
//    First;
//    while not Eof do
//    begin
//      if Fieldbyname('Ssel').AsBoolean then
//      begin
//        if i = 0 then
//          MPCID := trim(CDS_1.fieldbyname('PCID').AsString);
//        PrintCar(trim(CDS_1.fieldbyname('PCID').AsString));
//        i := i + 1;
//      end;
//      Next;
//    end;
//    EnableControls;
//  end;
//  SelOKNoFiler(Tv1, False);
//  CDS_1.Locate('PCID', MPCID, []);
//end;

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

procedure TfrmDyePlanCardList.TbSimpleGYClick(Sender: TObject);
begin

  try
    frmDyeSimpleTechnicsKaiKa := TfrmDyeSimpleTechnicsKaiKa.Create(Application);
    with frmDyeSimpleTechnicsKaiKa do
    begin
      fKaZu := CDS_1.FieldByName('KaZu').AsString;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmDyeSimpleTechnicsKaiKa.Free;
  end;
end;

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

  CDS_1.DisableControls;
  with CDS_1 do
  begin

    First;
    while CDS_1.Locate('SSel', True, []) do
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('Delete From Bs_Cloth_GY_Main_Kaika where PCID = ' + quoTedStr(Trim(CDS_1.FieldByName('KaZu').AsString)));
        SQL.Add('Delete From Bs_Cloth_GY_Sub_Kaika where PCID = ' + quoTedStr(Trim(CDS_1.FieldByName('KaZu').AsString)));
        ExecSQL;
      end;

      with ADOQueryCmd do
      begin
        SQL.Clear;
        SQL.Add('EXEC P_InsertGxToKaika @PCID = ' + quotedstr(trim(CDS_1.FieldByName('KaZu').AsString)));
        ExecSQL;
      end;

      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  Application.MessageBox('更新成功！', '提示信息', MB_OK);
  CDS_1.EnableControls;

end;

procedure TfrmDyePlanCardList.TBAddClick(Sender: TObject);
begin

  try
    frmDyePlanCardInput := TfrmDyePlanCardInput.Create(Application);
    with frmDyePlanCardInput do
    begin
      FPCIDS := '';
      FWorkshop := self.FWorkshop;

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

procedure TfrmDyePlanCardList.OrderNoChange(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 TfrmDyePlanCardList.FormShow(Sender: TObject);
begin
  inherited;
  fFlileFlag := UserDataFlag + 'HX';
  canshu1 := self.fParameters1;
  canshu2 := self.fParameters3;
//  canshu1 := '录入';
  FWorkshop := self.fParameters3;
  InitForm();
  KeyPreview := True;
end;

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

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

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

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

end;

procedure TfrmDyePlanCardList.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;
    // 循环设置指定范围内的行
    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
        CDS_1.Edit;
        CDS_1.FieldByName('ssel').Value := True;
        CDS_1.Post;
      end;

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

  end;

  tv1.Controller.TopRowIndex := topIndex;
end;

procedure TfrmDyePlanCardList.tbeditClick(Sender: TObject);
var
  MPCIDS: string;
  I, j: Integer;
  Column: TcxGridDBColumn;
  firstCurrentValue, CurrentValue: string;
begin
  I := 0;
  j := 0; //第一个被选中的
  with CDS_1 do
  begin
    DisableControls;
    First;

    Column := Tv1.GetColumnByFieldName('OrderNo');

    while not Eof do
    begin
      if Fieldbyname('Ssel').AsBoolean then
      begin
        MPCIDS := MPCIDS + trim(CDS_1.FieldByName('PCID').AsString) + ';';
        if j = 0 then
        begin
          firstCurrentValue := VarToStr(Tv1.DataController.Values[I, Column.Index]);
          j := 1; //第一个选中值执行其余不执行
        end;

        if (Column = nil) or (Tv1.DataController.RecordCount = 0) then
        begin

          Exit;
        end;

        CurrentValue := VarToStr(Tv1.DataController.Values[I, Column.Index]);

        if CurrentValue <> firstCurrentValue then
        begin
          application.MessageBox('列内容不一致', '提示信息', MB_OK);
          EnableControls;
          Exit;
        end;

      end;
      I := I + 1;
      Next;
    end;
    EnableControls;
  end;

  try
    frmDyePlanCardInput := TfrmDyePlanCardInput.Create(Application);
    with frmDyePlanCardInput do
    begin
      FPCIDS := MPCIDS;
      FWorkshop := self.FWorkshop;
      fmainID := SELF.CDS_1.fieldbyname('mainID').asstring;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmDyePlanCardInput.Free;
  end;
end;

procedure TfrmDyePlanCardList.btnWCClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  Dye_Plan_Card SET status=''10'' ');
            sql.Add('where KaZu=' + quotedstr(trim(CDS_1.fieldbyname('KaZu').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(CDS_1.FieldByName('KaZu').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('操作成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyePlanCardList.btnnowcClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean and (Fieldbyname('isStop').AsString = '0') then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  Dye_Plan_Card SET status=''9'' ');
            sql.Add('where KaZu=' + quotedstr(trim(CDS_1.fieldbyname('KaZu').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(CDS_1.FieldByName('KaZu').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('操作成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyePlanCardList.TbDetailGYClick(Sender: TObject);
begin
  try
    frmDyeTechnicsinputKaika := TfrmDyeTechnicsinputKaika.Create(Application);
    with frmDyeTechnicsinputKaika do
    begin
      PState := '查看';
      FPCID := Trim(self.CDS_1.fieldbyname('KaZu').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmDyeTechnicsinputKaika.Free;
  end;
end;

procedure TfrmDyePlanCardList.TbAbnormalClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  Dye_Plan_Card SET status=''-1'' ');
            sql.Add(' ,Chker = ' + quotedstr(trim(DName)) + ',Chktime = getdate()');
            sql.Add('where KaZu=' + quotedstr(trim(CDS_1.fieldbyname('KaZu').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(CDS_1.FieldByName('KaZu').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('异常成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('异常失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyePlanCardList.TnoAbnormalClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  Dye_Plan_Card SET status=''0'' ');
            sql.Add('where KaZu=' + quotedstr(trim(CDS_1.fieldbyname('KaZu').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(CDS_1.FieldByName('KaZu').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('撤销异常成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销异常失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyePlanCardList.ToolButton6Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption + 'TV1', Tv1, '生产指示单管理');

end;

procedure TfrmDyePlanCardList.Tv1CurGlideNameStylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; var AStyle: TcxStyle);
var
  GroupValue: string;
  ColorValue: Integer;
  I: Integer;
begin
  if (ARecord <> nil) and (ARecord is TcxGridDataRow) then
  begin
    GroupValue := VarToStr(ARecord.Values[Tv1CurGlideName.Index]);

    if (GroupValue <> '') and (GroupValue <> null) then
    begin
      // 基于每个字符的位置和值计算哈希

      AStyle := TcxStyle.Create(nil);

      if GroupValue = '已生产' then
      begin
        AStyle.Color := $00CCFFCC;  // 淡绿色
      end;

      AStyle.TextColor := clBlack;
    end;
  end;
end;

procedure TfrmDyePlanCardList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  misStop, mstatus: Variant;
begin
  // 获取字段值
  misStop := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('isStop').Index];
  mstatus := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('status').Index];

  // 设置不同状态的颜色和字体
  if (misStop = '1') and (mstatus = '10') then               // 未排入计划单
  begin
    ACanvas.Brush.Color := clGray;  // 灰色背景
    ACanvas.Font.Color := clWindowText;
    ACanvas.Font.Style := [];
  end
  else  // 默认情况
  begin
    ACanvas.Brush.Color := clWindow;
    ACanvas.Font.Color := clWindowText;
    ACanvas.Font.Style := [];
  end;
//  if AViewInfo.GridRecord.Selected then
//  begin
//
//    ACanvas.Font.Color := $000080FF;
//    ACanvas.Font.Style := [fsBold];
//
//  end;
  // 如果需要自定义绘制完成，取消下面的注释
  // ADone := True;
end;

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

procedure TfrmDyePlanCardList.TchkClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  Dye_Plan_Card SET status=''9'' ');
            sql.Add(' ,Chker = ' + quotedstr(trim(DName)) + ',Chktime = getdate()');
            sql.Add('where KaZu=' + quotedstr(trim(CDS_1.fieldbyname('KaZu').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(CDS_1.FieldByName('KaZu').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('审核成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyePlanCardList.TnochkClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  Dye_Plan_Card SET status=''0'' ');
            sql.Add('where KaZu=' + quotedstr(trim(CDS_1.fieldbyname('KaZu').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(CDS_1.FieldByName('KaZu').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('撤销审核成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销审核失败！', '提示信息', 0);
  end;
end;

end.

