unit U_TatDRList;

interface

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

type
  TfrmTatDRList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    TbLook: TToolButton;
    Panel1: TPanel;
    Label4: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ToCoName: TEdit;
    MPRTCodeName: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    cxTabControl1: TcxTabControl;
    tchk: TToolButton;
    Tnochk: TToolButton;
    cxSplitter1: TcxSplitter;
    DS_2: TDataSource;
    Label2: TLabel;
    CDS_2: TClientDataSet;
    GPM_2: TcxGridPopupMenu;
    btnWC: TToolButton;
    btnWCCX: TToolButton;
    CheckBox1: TCheckBox;
    ToolButton6: TToolButton;
    Label1: TLabel;
    Label3: TLabel;
    shdanwei: TEdit;
    ToolButton2: TToolButton;
    IdHTTP1: TIdHTTP;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton7: TToolButton;
    TbGoodsEdit: TToolButton;
    Label5: TLabel;
    SSelJS: TLabel;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    PM11: TMenuItem;
    GPM_3: TcxGridPopupMenu;
    N2: TMenuItem;
    TbAddressCopy: TToolButton;
    ToolButton8: TToolButton;
    TbUndoVisible: TToolButton;
    TbNowDay: TcxButton;
    PM_2: TPopupMenu;
    N3: TMenuItem;
    TbEditPFPrice: TToolButton;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    cxgrdbclmnTV11OverTime: TcxGridDBColumn;
    Tv1CheckEdit: TcxGridDBColumn;
    cxgrdbclmnTV11CheckEditTime: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    Tv1DlyDate: TcxGridDBColumn;
    TV1ToCoName: TcxGridDBColumn;
    TV1Saleser: TcxGridDBColumn;
    TV1Contacts: TcxGridDBColumn;
    TV1Telephone: TcxGridDBColumn;
    Tv1Filler: TcxGridDBColumn;
    Tv1Chker: TcxGridDBColumn;
    Tv1ChkTime: TcxGridDBColumn;
    Tv1Note: TcxGridDBColumn;
    Tv1CoAddress: TcxGridDBColumn;
    Tv1wcnote: TcxGridDBColumn;
    Tv1Comper: TcxGridDBColumn;
    Tv1tube: TcxGridDBColumn;
    Tv1Packaging: TcxGridDBColumn;
    Tv1Seal: TcxGridDBColumn;
    Tv1sselJS: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    TV2issp: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cv1Column3: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    TV2PFPrice: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    TV2Volume: TcxGridDBColumn;
    TV2Applicant: TcxGridDBColumn;
    TV2ApplicationReaon: TcxGridDBColumn;
    TV2editStatus: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    pm_3: TPopupMenu;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    GPM_1: TcxGridPopupMenu;
    TbMXExport: TToolButton;
    TV3ssel: TcxGridDBColumn;
    TV1CheckEditPiece: TcxGridDBColumn;
    TV2isIncludeTax: TcxGridDBColumn;
    TV2note: TcxGridDBColumn;
    TbEditNote: TToolButton;
    TV1CompanyName: TcxGridDBColumn;
    ToolButton1: TToolButton;
    TV2Grade: TcxGridDBColumn;
    Panel2: TPanel;
    CompanyName: TBtnEditC;
    Label6: TLabel;
    Button1: TButton;
    Button2: TButton;
    TbUpdateM: TToolButton;
    //RMllPDFExport1: TRMllPDFExport;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    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 FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure TbLookClick(Sender: TObject);
    procedure ToCoNameChange(Sender: TObject);
    procedure tchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure btnWCClick(Sender: TObject);
    procedure TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure btnWCCXClick(Sender: TObject);
    procedure v1Column7PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column9PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure TbGoodsEditClick(Sender: TObject);
    procedure v1Column4PropertiesChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure PM11Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TbAddressCopyClick(Sender: TObject);
    procedure TV1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure Tv1CheckEditStylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; var AStyle: TcxStyle);
    procedure Tv1FlagStylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; var AStyle: TcxStyle);
    procedure ToolButton8Click(Sender: TObject);
    procedure cxgrdbclmnTV11OverTimeCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure TbUndoVisibleClick(Sender: TObject);
    procedure TbNowDayClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure TbEditPFPriceClick(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure TbMXExportClick(Sender: TObject);
    procedure TbEditNoteClick(Sender: TObject);
    procedure ToCoNameKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure TV3DblClick(Sender: TObject);
    procedure CompanyNameBtnUpClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TbUpdateMClick(Sender: TObject);
  private
    fcxGridStyleFlag: Boolean;
    procedure InitGrid();
    procedure InitSubGrid();
    procedure InitForm();
    procedure SetStatus();
    function DelData(): Boolean;
    procedure InitSubGrid2();

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

//var
  //frmContractListNX: TfrmContractListNX;

implementation

uses
  U_DataLink, U_TatDRtInPut, U_RTFun, U_ZDYHelp, U_LabelPrint,
  U_AttachmentUpload, U_TatDRList2;

{$R *.dfm}

procedure TfrmTatDRList.SetStatus();
begin
  tchk.Enabled := False;
  Tnochk.Enabled := False;
  btnWC.Enabled := False;
  btnWCCX.Enabled := False;
  ToolButton3.Enabled := False;
  ToolButton5.Enabled := False;
  TbGoodsEdit.Enabled := False;

  TBEdit.Enabled := False;

  Tv1ToCoName.Visible := False;
//  Tv1CoName.Visible := False;

  TBDel.Enabled := False;
  if CANSHU1 = '查询' then
  begin
    TbLook.Enabled := True;
    TBPrint.Enabled := True;
    ToolButton2.Enabled := True;
    ToolButton4.Enabled := True;
    TBExport.Enabled := True;
    ToolButton7.Enabled := True;
    TbGoodsEdit.Enabled := True;

    TBAdd.Enabled := True;

    Tv1ToCoName.Visible := True;
    TV2PFPrice.Visible := False;
//    TBDel.Enabled := True;
    case cxTabControl1.TabIndex of
      0:
        begin
          TBEdit.Enabled := True;
        end;
      1:
        begin
          btnWC.Enabled := true;
          TBEdit.Enabled := True;
        end;
    end;
  end;

  if canshu1 = '管理' then
  begin
//    Tv1CoName.Visible := True;
    Tv1ToCoName.Visible := True;
    case cxTabControl1.TabIndex of
      0:
        begin
          ToolButton3.Enabled := true;
//          TbGoodsEdit.Enabled := True;
          tchk.Enabled := true;

          TBDel.Enabled := True;

          TBEdit.Enabled := True;
        end;
      1:
        begin
          Tnochk.Enabled := true;
          btnWC.Enabled := true;
          TBDel.Enabled := True;

          TBEdit.Enabled := True;
        end;
      2:
        begin
          btnWCCX.Enabled := true;
        end;
      3:
        begin
          ToolButton5.Enabled := true;
        end;
    end;
  end
  else if canshu1 = '录入' then
  begin

    Tv1ToCoName.Visible := True;
    case cxTabControl1.TabIndex of
      0:
        begin
          TBEdit.Enabled := True;
        end;
      1:
        begin
          TBEdit.Enabled := True;
          btnWC.Enabled := true;

//          TBDel.Enabled := True;
        end;
    end;
  end;

  if canshu1 = '财务' then
  begin
    TBAdd.Enabled := False;
    TBPrint.Enabled := False;
    ToolButton2.Enabled := False;
    TbAddressCopy.Enabled := False;
    ToolButton8.Enabled := False;
    TbUndoVisible.Enabled := False;

//    Tv1CoName.Visible := True;
//    case cxTabControl1.TabIndex of
//      0:
//        begin
//          ToolButton3.Enabled := true;
////          TbGoodsEdit.Enabled := True;
//          tchk.Enabled := true;
//
//          TBDel.Enabled := True;
//
//          TBEdit.Enabled := True;
//        end;
//      1:
//        begin
//          Tnochk.Enabled := true;
//          btnWC.Enabled := true;
//          TBDel.Enabled := True;
//
//          TBEdit.Enabled := True;
//        end;
//      2:
//        begin
//          btnWCCX.Enabled := true;
//        end;
//      3:
//        begin
//          ToolButton5.Enabled := true;
//        end;
//    end;
  end
end;

procedure TfrmTatDRList.FormDestroy(Sender: TObject);
begin
  inherited;
  //frmFHSQList:=nil;

end;

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

procedure TfrmTatDRList.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmTatDRList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;

    sselJS.Caption := '0';
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      Sql.Add(' select A.*  ');
      sql.Add(' from V_TatDRList A  where 1=1');
      if CheckBox1.Checked then
      begin
        sql.Add(' and A.Filltime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
        sql.Add(' and A.Filltime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      end;
      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'')=''-2'' ');
          end;
        4:
          begin
            SQL.Add(' and isnull(A.status,''0'')<>''-2'' ');
          end;
      end;

      if Trim(canshu1) = '录入' then
      begin
        sql.Add(' and isnull(A.Filler,'''')=''' + Trim(DName) + '''');
      end;
      sql.Add(' order by  A.Filltime desc ');
//      ShowMessage(sql.Text);
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, Order_Main);
    SInitCDSData(ADOQueryMain, Order_Main);

  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;
    TV2.DataController.Filter.Clear;
  end;
end;

procedure TfrmTatDRList.InitForm();
begin

  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '发货管理');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '发货管理');
  ReadCxGrid(trim(self.Caption) + 'Tv3', Tv3, '发货管理');

  EndDate.DateTime := SGetServerDate(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 60;
  InitGrid();
  InitSubGrid2();

end;

procedure TfrmTatDRList.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 TfrmTatDRList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
//  if cxTabControl1.TabIndex <> 0 then
//    Exit;
//  if Trim(canshu1) <> '管理' then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      SQL.Add('select FILLER from Tat_Cloth_DR_Main A  ');
//      SQL.Add('where DRMID=''' + Trim(Order_Main.fieldbyname('DRMID').AsString) + ''' ');
//      SQL.Add('and   filler in ( select username from SY_User where UType=(select UType from SY_User where UserName=' + quotedstr(Trim(dname)));
//      SQL.Add('))');
//      Open;
//    end;
//
//    if ADOQueryTemp.ISEMPTY = True then
//    begin
//      Application.MessageBox('不能操作他人的数据!', '提示', 0);
//      Exit;
//    end;
  if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!', '提示', 0);
    Exit;
  end;
//  end;
//  with ADOQueryTemp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('select * from Tat_Cloth_DR_Main');
//    sql.Add('where DRMID=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
//    Open;
//  end;
//  if Trim(ADOQueryTemp.fieldbyname('YSID').AsString) <> '' then
//  begin
//    Application.MessageBox('已生成应收账款不能修改数据!', '提示', 0);
//    Exit;
//  end;
  try
    frmTatDRtInPut := TfrmTatDRtInPut.Create(Application);
    with frmTatDRtInPut do
    begin
      PState := 1;
      canshu1 := self.canshu1;
      isCheck := cxTabControl1.TabIndex;

      TbSavePFPrice.Visible := false;
      TbSave.Visible := true;
      TbSaveNote.Visible := false;

      Tv1Volume.Options.Editing := False;
      Tv1ApplicationReaon.Options.Editing := False;
      FDRMID := Trim(Self.Order_Main.fieldbyname('DRMID').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTatDRtInPut.Free;
  end;
end;

procedure TfrmTatDRList.TbEditNoteClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;

  try
    frmTatDRtInPut := TfrmTatDRtInPut.Create(Application);
    with frmTatDRtInPut do
    begin
      PState := 888; //修改备注
      TbSavePFPrice.Visible := false;
      TbSave.Visible := false;
      TbSaveNote.Visible := True;
      canshu1 := self.canshu1;
      isCheck := cxTabControl1.TabIndex;
      Tv1Volume.Options.Editing := False;
      Tv1ApplicationReaon.Options.Editing := False;
      FDRMID := Trim(Self.Order_Main.fieldbyname('DRMID').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTatDRtInPut.Free;
  end;

end;

procedure TfrmTatDRList.TbEditPFPriceClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;

  try
    frmTatDRtInPut := TfrmTatDRtInPut.Create(Application);
    with frmTatDRtInPut do
    begin
      PState := 999;   //修改单价
      TbSavePFPrice.Visible := True;
      TbSave.Visible := false;
      TbSaveNote.Visible := false;
      canshu1 := self.canshu1;
      isCheck := cxTabControl1.TabIndex;
      Tv1Volume.Options.Editing := False;
      Tv1ApplicationReaon.Options.Editing := False;
      FDRMID := Trim(Self.Order_Main.fieldbyname('DRMID').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTatDRtInPut.Free;
  end;

end;

procedure TfrmTatDRList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if (cxTabControl1.TabIndex <> 0) and (cxTabControl1.TabIndex <> 1) then
    Exit;
//  if canshu1 <> '管理' then
//  begin
//  if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
//  begin
//    Application.MessageBox('不能操作他人的数据!', '提示', 0);
//    Exit;
//  end;
//  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from Tat_Cloth_IO where ioflag=''出库'' ');
    sql.add(' and IONo=''' + Trim(Order_Main.fieldbyname('DRMID').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('出库数据不能删除!', '提示', 0);
    exit;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from Tat_Cloth_IO where ioflag=''待出库'' ');
    sql.add(' and IONo=''' + Trim(Order_Main.fieldbyname('DRMID').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('待出库数据不能删除!', '提示', 0);
    exit;
  end;

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

procedure TfrmTatDRList.TbGoodsEditClick(Sender: TObject);
var
  maxno: string;
begin
  try
    frmTatDRtInPut := TfrmTatDRtInPut.Create(Application);
    with frmTatDRtInPut do
    begin
      PState := 2; ///配货修改
      FDRMID := Trim(Self.Order_Main.fieldbyname('DRMID').AsString);
      canshu1 := self.canshu1;

      ScrollBox1.Visible := False;
      ToolBar2.Visible := false;
      TbPHSave.visible := True;
      TBSave.Visible := False;

//      Tv1Applicant.Visible := True;

      Tv1C_Name.Options.Editing := False;
      Tv1C_code.Options.Editing := False;
      Tv1C_width.Options.Editing := False;
      Tv1C_GramWeight.Options.Editing := False;
      Tv1c_Color.Options.Editing := False;
      Tv1Piece.Options.Editing := False;
      Tv1Qty.Options.Editing := False;
      Tv1PINGFANG.Options.Editing := False;

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

procedure TfrmTatDRList.TbMXExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  with CDS_3 do
  begin
    Filtered := false;
    Filter := ' ssel = 1';
    Filtered := true;
  end;
  TcxGridToExcel(Self.Caption, cxGrid3);
  InitsubGrid2();
end;

procedure TfrmTatDRList.TbNowDayClick(Sender: TObject);
begin
  BegDate.Date := strToDate(FormatDateTime('yyyy-MM-dd', SGetServerDate(ADOQueryCmd)));
  EndDate.Date := strToDate(FormatDateTime('yyyy-MM-dd', SGetServerDate(ADOQueryCmd)));
  InitGrid();
end;

function TfrmTatDRList.DelData(): Boolean;
begin

  try
    with Order_Main do
    begin
      DisableControls;
      ADOQueryCmd.Connection.BeginTrans;
      First;
      while not eof do
      begin
        if fieldbyname('SSel').AsBoolean then
        begin
          Result := false;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete Tat_Cloth_DR_Sub where DRMID=''' + Trim(Order_Main.fieldbyname('DRMID').AsString) + '''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete Tat_Cloth_DR_Main where DRMID=''' + Trim(Order_Main.fieldbyname('DRMID').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('DRMID：' + trim(Order_Main.fieldbyname('DRMID').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
            ExecSQL;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmTatDRList.TBExportClick(Sender: TObject);
begin
  if PageControl1.ActivePage.Caption = ' 汇总' then
  begin
    if Order_Main.IsEmpty then
      Exit;
    with Order_Main do
    begin
      Filtered := false;
      Filter := ' ssel = 1';
      Filtered := true;
    end;
    TcxGridToExcel(Self.Caption, cxGrid1);
    InitGrid();
  end
  else if PageControl1.ActivePage.Caption = ' 明细' then
  begin
    if CDS_3.IsEmpty then
      Exit;
    with CDS_3 do
    begin
      Filtered := false;
      Filter := ' ssel = 1';
      Filtered := true;
    end;
    TcxGridToExcel(Self.Caption, cxGrid3);
    InitSubGrid2();
  end;

end;

procedure TfrmTatDRList.TBPrintClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeClothDR1';
      FFiltration1 := Trim(self.Order_Main.fieldbyname('DRMID').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

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

procedure TfrmTatDRList.TbUndoVisibleClick(Sender: TObject);
begin
  with Order_Main do
  begin
    Edit;
    FieldByName('OverTime').Value := '';
    post;
  end;
end;

procedure TfrmTatDRList.TBAddClick(Sender: TObject);
var
  maxno: string;
begin
  try
    frmTatDRtInPut := TfrmTatDRtInPut.Create(Application);
    with frmTatDRtInPut do
    begin
      PState := 0;
      isCheck := 0;
      FDRMID := '';

      TbSavePFPrice.Visible := false;
      TbSave.Visible := true;
      TbSaveNote.Visible := false;

      canshu1 := self.canshu1;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTatDRtInPut.Free;
  end;
end;

//procedure TfrmTatDRList.TbAddressCopyClick(Sender: TObject);
//var
//  CopyValue : string;
//begin
//  if Order_Main.IsEmpty then
//  begin
//    Exit;
//  end;
//
//  if not Order_Main.Active then
//  begin
//    Application.MessageBox('未选中记录！','提示信息',MB_OK);
//  end;
//
//  CopyValue := Order_Main.FieldByName('Contacts').AsString + ' ' ;
//  CopyValue := CopyValue + Order_Main.FieldByName('Telephone').AsString + #13;
//  CopyValue := CopyValue + Order_Main.FieldByName('CoAddress').AsString;
//  Clipboard.SetTextBuf(PChar(Trim(CopyValue)));
//end;
procedure TfrmTatDRList.TbAddressCopyClick(Sender: TObject);
var
  CopyValue: string;
  MsgLabel: TLabel;
begin
  if Order_Main.IsEmpty or not Order_Main.Active then
  begin
    Application.MessageBox('未选中记录！', '提示信息', MB_OK);
    Exit;
  end;

  CopyValue := Order_Main.FieldByName('Contacts').AsString + ' ';
  CopyValue := CopyValue + Order_Main.FieldByName('Telephone').AsString + #13;
  CopyValue := CopyValue + Order_Main.FieldByName('CoAddress').AsString;

  Clipboard.AsText := Trim(CopyValue);
//  MessageDlg('地址信息已成功复制到剪贴板！', mtInformation, [mbOK], 0);


end;

procedure TfrmTatDRList.FormShow(Sender: TObject);
begin
  inherited;
  fcxGridStyleFlag := False;
  canshu1 := self.fParameters1;
  InitForm();
  SetStatus();

end;

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

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

procedure TfrmTatDRList.CompanyNameBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxButtonEdit(Sender).Hint);
  FWZ := Pos('CompanyName/公司抬头', fsj); //先英文再中文
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TcxButtonEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTatDRList.TbLookClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmTatDRtInPut := TfrmTatDRtInPut.Create(Application);
    with frmTatDRtInPut do
    begin
      PState := 1;
      FDRMID := Trim(Self.Order_Main.fieldbyname('DRMID').AsString);
      ToolBar2.Visible := False;
      TBSave.Visible := False;
      TbSavePFPrice.Visible := false;
      TbSaveNote.Visible := false;
      ScrollBox1.Enabled := False;
      canshu1 := self.canshu1;
      Tv1.OptionsSelection.CellSelect := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTatDRtInPut.Free;
  end;
end;

procedure TfrmTatDRList.ToolButton1Click(Sender: TObject);
//var
//  InputValue: string;
//  topIndex: Integer;
//  mDRMIDs: string;
begin

  if Order_Main.Locate('ssel', True, []) = false then
  begin
    ShowMessage('请先选择要修改的记录');
    Exit;
  end;
  CompanyName.text := '';
  Panel2.Visible := True;

//  topIndex := tv1.Controller.TopRowIndex;
//
//  // 初始化为空字符串
//  InputValue := '';
//
//  if InputQuery('公司抬头修改', '请输入公司抬头', InputValue) then
//  begin
//  // 循环设置指定范围内的行
//    Order_Main.DisableControls;
//
//    Order_Main.First;
//    while not Order_Main.eof do
//    begin
//      if Order_Main.fieldByName('ssel').Value then
//      begin
//        if mDRMIDs <> '' then
//        begin
//          mDRMIDs := mDRMIDs + ',' + QuotedStr(Order_Main.FieldByName('DRMID').AsString);
//        end
//        else
//        begin
//          mDRMIDs := QuotedStr(Order_Main.FieldByName('DRMID').AsString);
//        end;
//
//      end;
//      Order_Main.Next;
//    end;
//    Order_Main.EnableControls;
//
//    try
//
//      with ADOQueryCmd do
//      begin
//        Close;
//        sql.Clear;
//        sql.add(' update Tat_Cloth_DR_Main set CompanyName=' + QuotedStr(Trim(InputValue)));
//        SQL.Add(' WHERE DRMID IN (' + mDRMIDs + ')');  // 添加括号
////        showMessage(sql.Text);
//        ExecSQL;
//      end;
//
//    finally
////      Order_Main.Refresh;
//    end;
//
//  end;
//
//  tv1.Controller.TopRowIndex := topIndex;
end;

procedure TfrmTatDRList.ToolButton2Click(Sender: TObject);
begin
  inherited;
  if order_main.IsEmpty then
    Exit;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TatClothOut1';
      FFiltration1 := Trim(self.order_main.fieldbyname('DRMID').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmTatDRList.ToolButton3Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 0 then
    exit;

  if Order_Main.Locate('ssel', True, []) = false then
  begin
    Application.MessageBox('请先选择数据！', '提示信息', MB_OK);
    exit;
  end;
  try
    with Order_Main 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  Tat_Cloth_DR_Main SET  Status=''-2'' ');
            sql.Add('where DRMID=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    InitSubGrid();
    application.MessageBox('取消成功！', '提示信息');
  except
    application.MessageBox('取消失败！', '提示信息', 0);
  end;
end;

procedure TfrmTatDRList.ToolButton4Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
      if Trim(canshu1) <> '查询' then
        FEditAuthority := True;
      fkeyNO := Trim(Self.Order_Main.fieldbyname('DRMID').AsString);
      fType := '发货申请';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;

end;

procedure TfrmTatDRList.ToolButton5Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 3 then
    exit;

  if Order_Main.Locate('ssel', True, []) = false then
  begin
    Application.MessageBox('请先选择数据！', '提示信息', MB_OK);
    exit;
  end;
  try
    with Order_Main 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  Tat_Cloth_DR_Main SET  Status=''0'' ');
            sql.Add('where DRMID=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    InitSubGrid();
    application.MessageBox('撤销取消成功！', '提示信息');
  except
    application.MessageBox('撤销取消失败！', '提示信息', 0);
  end;
end;

procedure TfrmTatDRList.ToCoNameChange(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 TfrmTatDRList.ToCoNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitSubGrid2();
  end;

end;

procedure TfrmTatDRList.tchkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 0 then
    exit;

  if Order_Main.Locate('ssel', True, []) = false then
  begin
    Application.MessageBox('请先选择数据！', '提示信息', MB_OK);
    exit;
  end;

  try
    with Order_Main 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  Tat_Cloth_DR_Main SET Chker=''' + Trim(DName) + ''',ChkTime=getdate(),Status=''9'' ');
            sql.Add('where DRMID=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
            execsql;
          end;
        end;

        next;
      end;

      First;
      EnableControls;
    end;

    InitGrid();
    InitSubGrid();
    application.MessageBox('审核成功！', '提示信息');
  except
    application.MessageBox('审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmTatDRList.InitSubGrid();
begin

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add('from Tat_Cloth_DR_Sub A   where A.DRMID=''' + Trim(Order_Main.fieldbyname('DRMID').AsString) + '''');

//    sql.Add('from Tat_Cloth_DR_Sub A   where A.DRMID in (select rtvalue from F_Tool_SplitString(''' + SelCDSKey(Order_Main, ['DRMID'])[0]  + ''' , '',''))');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);
end;

procedure TfrmTatDRList.InitSubGrid2();
begin

  with ADOQueryCMD do
  begin

    Close;
    sql.Clear;
//    sql.Add('select A.* ');
//    sql.Add(' from Tat_Cloth_DR_Sub A   where A.DRMID in (select rtvalue from F_Tool_SplitString(''' + SelCDSKey(Order_Main, ['DRMID'])[0]  + ''' , '',''))');
    sql.Add(' select A.C_Code,A.C_Name,A.C_Width,A.C_GramWeight ');
    SQL.Add(' ,sum(Piece) as Piece,sum(pingfang) as pingfang,sum(DlyPiece) as DlyPiece,sum(DlyQty) as DlyQty,sum(DLYKGQTY) as DLYKGQTY');
//    SQL.Add(' ,Telephone=(select top 1 Telephone from Bs_Company_contact X where X.CoID=A.ToCoID )');
//    SQL.Add(' ,Contacts=(select top 1 Contacts from Bs_Company_contact X where X.CoID=A.ToCoID ) ');
    SQL.Add(' from   ');

    SQL.Add(' (select Y.*,X.status,X.ToCoName,X.shdanwei,X.Filltime,X.Filler from Tat_Cloth_DR_Sub Y left join Tat_Cloth_DR_Main X on X.DRMID=Y.DRMID ) A');
//    sql.Add(' left join Tat_Cloth_DR_Main A on A.DRMID = B.DRMID ');
//    sql.Add(' left join BS_Company C on A.ToCoID = C.COID ');

    SQL.Add(' where 1 = 1');

//     if DRMID.Text <> '' then
//     begin
//        SQL.Add(' and A.DRMID like ' + QuotedStr('%' + Trim(DRMID.text) + '%'));
//     end;
    if ToCoName.Text <> '' then
    begin
      SQL.Add(' and A.ToCoName like ' + QuotedStr('%' + Trim(ToCoName.text) + '%'));
    end;
    if shdanwei.Text <> '' then
    begin
      SQL.Add(' and A.shdanwei like ' + QuotedStr('%' + Trim(shdanwei.text) + '%'));
    end;

    if CheckBox1.Checked then
    begin
      sql.Add(' and A.Filltime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
      sql.Add(' and A.Filltime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
    end;
    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'')=''-2'' ');
        end;
      4:
        begin
          SQL.Add(' and isnull(A.status,''0'')<>''-2'' ');
        end;
    end;

    if Trim(canshu1) = '录入' then
    begin
      sql.Add(' and isnull(A.Filler,'''')=''' + Trim(DName) + '''');
    end;

    SQL.Add(' group by A.C_Code,A.C_Name,A.C_Width,A.C_GramWeight ');
    sql.Add(' order by  A.C_Code,A.C_Width,A.C_GramWeight  desc ');

//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS(ADOQueryCMD, CDS_3);
  SInitCDSData(ADOQueryCMD, CDS_3);
end;

procedure TfrmTatDRList.N1Click(Sender: TObject);
var
  msumJS: Currency;
begin
  SelOKNo(Order_Main, true);
  msumJS := 0;
  Order_Main.First;
  while not Order_Main.Eof do
  begin
    msumJS := msumJS + Order_Main.FieldByName('sselJS').AsCurrency;
    Order_Main.Next;
  end;

  SSelJS.Caption := CurrToStr(msumJS);
end;

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

procedure TfrmTatDRList.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_2.fieldbyname(TV2.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmTatDRList.N4Click(Sender: TObject);
begin
  SelOKNo(CDS_3, true);

end;

procedure TfrmTatDRList.N5Click(Sender: TObject);
begin
  SelOKNo(CDS_3, False);

end;

procedure TfrmTatDRList.N6Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_3.fieldbyname(TV3.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmTatDRList.PageControl1Change(Sender: TObject);
begin
  if PageControl1.TabIndex = 1 then
  begin
    InitSubGrid2();
    TbMXExport.Visible := True;

//     TbLook.Visible := False;
//     TBDel.visible := False;
//     TBEdit.Visible := False;
//     TbEditPFPrice.visible := false;
//     TbEditNote.visible := false;

  end
  else
  begin
    TbMXExport.Visible := False;
  end;
end;

procedure TfrmTatDRList.PM11Click(Sender: TObject);
begin
  SelOKNo(Order_Main, false);
  SSelJS.Caption := '0';
end;

procedure TfrmTatDRList.TnochkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 1 then
    exit;
  if Order_Main.Locate('ssel', True, []) = false then
  begin
    Application.MessageBox('请先选择数据！', '提示信息', MB_OK);
    exit;
  end;
  try
    with Order_Main 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  Tat_Cloth_DR_Main SET Chker=Null,ChkTime=Null,status=''0'' ');
            sql.Add('where DRMID=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    application.MessageBox('撤销成功！', '提示信息');
  except
    application.MessageBox('撤销失败！', '提示信息', 0);
  end;
end;

procedure TfrmTatDRList.cxTabControl1Change(Sender: TObject);
begin
  TBRafresh.Click;
  SetStatus();
end;

procedure TfrmTatDRList.btnWCCXClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;

  if Order_Main.Locate('ssel', True, []) = false then
  begin
    Application.MessageBox('请先选择数据！', '提示信息', MB_OK);
    exit;
  end;
  try
    with Order_Main do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if fieldbyname('SSel').AsBoolean then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('exec P_Fin_Judge_All @DataID=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
            Open;
          end;
          if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
          begin
            Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
            exit;
          end;

          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  Tat_Cloth_DR_Main SET status=''9'' ,Comper=null,Comptime=null ');
            sql.Add('where DRMID=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    InitSubGrid();
    application.MessageBox('操作成功！', '提示信息');
  except
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmTatDRList.Button1Click(Sender: TObject);
var
  topIndex: Integer;
  mDRMIDs: string;
begin

  if Order_Main.Locate('ssel', True, []) = false then
  begin
    ShowMessage('请先选择要修改的记录');
    Exit;
  end;

  if CompanyName.text = '' then
  begin
    ShowMessage('公司抬头不能为空');
    Exit;
  end;

  topIndex := tv1.Controller.TopRowIndex;


  // 循环设置指定范围内的行
//    Order_Main.DisableControls;
  TV1.beginupdate();
  Order_Main.First;
  while not Order_Main.eof do
  begin
    if Order_Main.fieldByName('ssel').Value then
    begin
      if mDRMIDs <> '' then
      begin
        mDRMIDs := mDRMIDs + ',' + QuotedStr(Order_Main.FieldByName('DRMID').AsString);
      end
      else
      begin
        mDRMIDs := QuotedStr(Order_Main.FieldByName('DRMID').AsString);
      end;

    end;
    Order_Main.Next;
  end;
  TV1.endupdate();

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.add(' update Tat_Cloth_DR_Main set CompanyName=' + QuotedStr(Trim(CompanyName.text)));
    SQL.Add(' WHERE DRMID IN (' + mDRMIDs + ')');  // 添加括号
//        showMessage(sql.Text);
    ExecSQL;
  end;
  Panel2.Visible := False;

  tv1.Controller.TopRowIndex := topIndex;
end;

procedure TfrmTatDRList.Button2Click(Sender: TObject);
begin
  Panel2.Visible := False;
end;

procedure TfrmTatDRList.ToolButton6Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '发货管理');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '发货管理');
  WriteCxGrid(trim(self.Caption) + 'Tv3', Tv3, '发货管理');
end;

procedure TfrmTatDRList.ToolButton7Click(Sender: TObject);
var
  mFHDH: string;
  sQZ: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin

      begin

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
    //SQL同时查询FHDH和QZ前缀
          SQL.Add(' exec Update_Tat_Cloth_IO_MaxFHDH   @CompanyName = ' + quotedstr(trim(Order_Main.fieldbyname('CompanyName').AsString)));
          Open;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
    //SQL同时查询FHDH和QZ前缀
          SQL.Add(' select FHDH=FHDH+1,QZ from tat_cloth_io_FHDH where CompanyName = ' + quotedstr(trim(Order_Main.fieldbyname('CompanyName').AsString)));
          Open;
        end;

        mFHDH := ADOQueryCmd.FieldByName('FHDH').AsString;
        sQZ := ADOQueryCmd.FieldByName('QZ').AsString; //从数据库QZ字段拿前缀

  //前缀 + 年份后两位 + 6位流水号
        mFHDH := sQZ + FormatDateTime('yy', Date) + Format('%.6d', [(StrToIntDef(mFHDH, 0) + 1000000) mod 1000000]);

        with ADOQueryCmd do
        begin
          Close;
          Sql.Clear;
          sql.Add(' update tat_cloth_io_FHDH SET FHDH = FHDH+1,ioFHDH=''' + mFHDH + ''' ');
          SQL.Add(' where CompanyName = ' + quotedstr(trim(Order_Main.fieldbyname('CompanyName').AsString)));
          EXECSQL;
        end;
      end;

      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        sql.Add('update  Tat_Cloth_IO SET FHDH = ''' + mFHDH + ''' ');
        sql.Add('where iono=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
        sql.add('and ioflag=''待出库''');
        EXECSQL;
      end;

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;

        sql.Add('update  Tat_Cloth_Stock SET StkExists=0     ');
//        sql.Add('where iono=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
        sql.add('where IsStocking=''1''');
        sql.Add('and STKID IN (SELECT STKID FROM Tat_Cloth_IO F ');
        SQL.Add('where iono=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
        sql.add('and ioflag=''待出库'')');

        sql.Add('update  Tat_Cloth_Inspect SET CIIOFlag=''已出库''     ');
//        sql.Add('where iono=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
        sql.add('where  ');
        sql.Add('  ciid IN (SELECT STKID FROM Tat_Cloth_IO F ');
        SQL.Add('where iono=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
        sql.add('and ioflag=''待出库'')');

        sql.Add('update  Tat_Cloth_IO SET ioflag=''出库'',iotime=getdate()    ');
        sql.Add('where iono=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
        sql.add('and ioflag=''待出库''');
        execsql;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Tat_Cloth_DR @OutNoS=' + Quotedstr(Trim(Order_Main.fieldbyname('DRMID').AsString)));
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      begin
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      end;
      EnableControls;
    end;
    InitGrid();
    InitSubGrid();
    application.MessageBox('操作成功！', '提示信息');
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmTatDRList.ToolButton8Click(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    SQL.Add('  SELECT 1 FROM Tat_Cloth_DR_Sub X WHERE X.DRMID=' + quotedStr(Trim(Order_Main.FieldByName('DRMID').asString)));
    sql.Add(' AND isnull(X.Piece,0) > isnull(X.DlyPiece,0)');
    SQL.Add(' AND CONVERT(date, ' + quotedStr(Trim(Order_Main.FieldByName('DlyDate').asString)) + ') < CONVERT(date, GETDATE())');
    Open;
  end;

  if not ADOQueryCmd.IsEmpty then
  begin
    with Order_Main do
    begin
      Edit;
      FieldByName('OverTime').asString := '未发完';
      post;
    end;
  end
  else
  begin
    Application.MessageBox('已发完！', '提示信息', MB_OK);
    Exit;
  end;

end;

procedure TfrmTatDRList.TbUpdateMClick(Sender: TObject);
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Tat_Cloth_DR @OutNoS=' + Quotedstr(Trim(Order_Main.fieldbyname('DRMID').AsString)));
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      begin
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      end;
    end;
    InitGrid();
    InitSubGrid();
    application.MessageBox('操作成功！', '提示信息');
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmTatDRList.btnWCClick(Sender: TObject);
var
  str: string;
begin
  if Order_Main.IsEmpty then
    exit;

  if Order_Main.Locate('ssel', True, []) = false then
  begin
    Application.MessageBox('请先选择数据！', '提示信息', MB_OK);
    exit;
  end;
  try
    with Order_Main do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if fieldbyname('SSel').AsBoolean then
        begin
          with ADOQueryTemp do
          begin
            close;
            sql.Clear;
            SQL.Add('SELECT  TT=ISNULL(SUM(DlyPiece),0),T1=ISNULL(SUM(PIECE),0) FROM Tat_Cloth_DR_SUB A WHERE A.DRMID=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
            OPEN;
          end;
          if ADOQueryTemp.FieldByName('TT').AsInteger = 0 then
          begin
            application.MessageBox('发货数量为0，不能完成！', '提示信息');
            EnableControls;
            exit;
          end;
          if ADOQueryTemp.FieldByName('TT').AsInteger < ADOQueryTemp.FieldByName('T1').AsInteger then
          begin
            if InputQuery('输入备注', '备注', str) then
            begin

              if Trim(str) = '' then
              begin
                ShowMessage('备注不能为空！');
                Exit;  // 或者使用 Exit; 来退出当前过程
              end;

              with ADOQueryCmd do
              begin
                Close;
                sql.Clear;
                sql.Add('update Tat_Cloth_DR_main         ');
                sql.Add(' set wcnote=' + quotedstr(str));
                sql.Add(' where   drmid=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
                ExecSQL;
              end;
            end
            else
            begin
              EnableControls;
              exit;
            end;
          end;

          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  Tat_Cloth_DR_Main SET status=''10'' ,Comper=''' + Trim(DName) + ''',Comptime=getdate() ');
            sql.Add('where DRMID=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));

//            sql.Add('exec P_Do_WXInsert_MD ');
//            sql.Add('@CKORDNO=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));

            execsql;
          end;

        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    InitSubGrid();
    application.MessageBox('操作成功！', '提示信息');
  except
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmTatDRList.Tv1CheckEditStylesGetContentStyle(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[Tv1CheckEdit.Index]);
    if (GroupValue <> '') and (GroupValue <> null) then
    begin
//      ColorValue := StrToInt(GroupValue);
      AStyle := TcxStyle.Create(nil);
      if GroupValue = '已改单' then
        AStyle.Color := $0090FFFF;
      AStyle.TextColor := clBlack;
    end;

  end;
end;

procedure TfrmTatDRList.Tv1Column9PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

  if mvalue = '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update Tat_Cloth_DR_Main set  ' + FFieldName + '=' + 'NULL');
      sql.Add('where  DRMID=' + quotedstr(Trim(Order_Main.fieldbyname('DRMID').AsString)));
      ExecSQL;
    end;
  end
  else
  begin
    with Order_Main do
    begin
      Edit;
      FieldByName(FFieldName).Value := mvalue;
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update Tat_Cloth_DR_Main set  ' + FFieldName + '=' + quotedstr(Trim(mvalue)));
      sql.Add('where  DRMID=' + quotedstr(Trim(Order_Main.fieldbyname('DRMID').AsString)));
      ExecSQL;
    end;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmTatDRList.TV1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  FLAG: Variant;
begin
//  FLAG := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('FLAG').Index];
//  if FLAG = 1 then               // 未排入计划单
//  begin
//    ACanvas.Brush.Color := $00CCCCFF;  // 浅红色背景
//    ACanvas.Font.Color := clRed;
//    ACanvas.Font.Style := [fsBold];
//  end;
end;

procedure TfrmTatDRList.Tv1FlagStylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; var AStyle: TcxStyle);
var
  GroupValue: string;
//  ColorValue: Integer;
  I: Integer;
begin

//  if not fcxGridStyleFlag then
//  begin
//    Exit;
//  end;
//
//  if (ARecord <> nil) and (ARecord is TcxGridDataRow) then
//  begin
//    GroupValue := VarToStr(ARecord.Values[Tv1OverTime.Index]);
//    if (GroupValue <> '') and (GroupValue <> null) then
//    begin
////      ColorValue := StrToInt(GroupValue);
//      AStyle := TcxStyle.Create(nil);
//      if GroupValue = '已超期' then
//        AStyle.Color := $00CCCCFF;
//      AStyle.TextColor := clBlack;
//    end;
//
//  end;
end;

procedure TfrmTatDRList.TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitSubGrid();
end;

procedure TfrmTatDRList.TV3DblClick(Sender: TObject);
begin
  if CDS_3.IsEmpty then
    Exit;
  try
    frmTatDRList2 := TfrmTatDRList2.Create(Application);
    with frmTatDRList2 do
    begin
      fEndDateTime := self.EndDate.DateTime;
      fBegDateTime := Self.BegDate.DateTime;

      fC_Name := Self.CDS_3.FieldByName('C_Name').AsString;
      fC_GramWeight := Self.CDS_3.FieldByName('C_GramWeight').AsString;
      fC_Width := Self.CDS_3.FieldByName('C_Width').AsString;
//      ffiller := Self.CDS_3.FieldByName('filler').AsString;
      fcanshu1 := canshu1;

      case cxTabControl1.TabIndex of
        0:
          begin
            fstatus := '0';
          end;
        1:
          begin
            fstatus := '9';
          end;
        2:
          begin
            fstatus := '10';
          end;
        3:
          begin
            fstatus := '-2';
          end;
        4:
          begin
            fstatus := '999';
          end;
      end;

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTatDRList2.Free;
  end;
end;

procedure TfrmTatDRList.cxgrdbclmnTV11OverTimeCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
//单格子
var
  FLAG: string;
begin
//  id := TV1.GetColumnByFieldName('OverTime').Index; //;-TV1.GroupedItemCount;
//  if AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('OverTime').Index] = 1 then
//    ACanvas.BRUSH.Color := clAqua;
//  if AViewInfo.GridRecord.Values[id] = 1 then
//  begin
//    ACanvas.Brush.Color := clRed;
//  end;

  FLAG := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('OverTime').Index];
  if FLAG = '未发完' then               // 未排入计划单
  begin
    ACanvas.Brush.Color := $00CCCCFF;  // 浅红色背景
    ACanvas.Font.Color := clRed;
    ACanvas.Font.Style := [fsBold];
  end;
end;

procedure TfrmTatDRList.v1Column4PropertiesChange(Sender: TObject);
var
  mDRMIDList: string;
  TotalPiece: Variant;
  mDRMID: string; //定位作用
begin
//  mDRMIDList := '';
  // 保存当前记录的 BCIID
//  if not Order_Main.IsEmpty then
//    mDRMID := Trim(Order_Main.FieldByName('DRMID').AsString)
//  else
//    mDRMID := '';
//
//  try
//    // 确保数据集状态正常
//    Order_Main.DisableControls;
//    try
//      Order_Main.First;
//      while not Order_Main.Eof do
//      begin
//        if Order_Main.FieldByName('ssel').AsBoolean then
//        begin
//          if mDRMIDList = '' then
//            mDRMIDList := QuotedStr(Trim(Order_Main.FieldByName('DRMID').AsString))
//          else
//            mDRMIDList := mDRMIDList + ',' + QuotedStr(Trim(Order_Main.FieldByName('DRMID').AsString));
//        end;
//        Order_Main.Next;
//      end;
//    finally
//      Order_Main.EnableControls;
//    end;
//
//    // 如果没有选择任何记录
//    if mDRMIDList = '' then
//    begin
//
//      if mDRMID <> '' then
//        Order_Main.Locate('DRMID', mDRMID, []);
//      sselJS.Caption := '0';
//      Exit;
//    end;
//
//    // 执行查询
//    with ADOQueryCmd do
//    begin
//      Close;
//      SQL.Clear;
//      SQL.Add('select isnull(sum(Piece), 0) as sselJS from Tat_Cloth_DR_Sub');
//      SQL.Add('where DRMID in (' + mDRMIDList + ')');
//
//      // 调试时可以显示SQL语句
////      ShowMessage('SQL: ' + SQL.Text);
//
//      Open;
//
//      // 处理结果（更安全的方式）
//      if not IsEmpty then
//      begin
//        TotalPiece := FieldByName('sselJS').Value;
//        // 处理NULL值和空值
//        if VarIsNull(TotalPiece) or VarIsEmpty(TotalPiece) then
//          sselJS.Caption := '0'
//        else
//          sselJS.Caption := VarToStr(TotalPiece);
//      end
//      else
//      begin
//        sselJS.Caption := '0';
//      end;
//    end;
//
//
//    // 最后尝试定位回原记录
//    if mDRMID <> '' then
//      Order_Main.Locate('DRMID', mDRMID, []);
////    sselJS.Text := '0';
////    sselJS.Text := ADOQueryCmd.FieldByName('sselJS').Value
//  except
//    on E: Exception do
//    begin
//      // 记录错误信息
//      sselJS.Caption := '0';
//      ShowMessage('查询失败: ' + E.Message + #13#10 + 'SQL语句: ' + ADOQueryCmd.SQL.Text);
//    end;
//  end;


  try

    mDRMID := Trim(Order_Main.FieldByName('DRMID').AsString);
    // 确保数据集状态正常
    Order_Main.DisableControls;
//    with ADOQueryCmd do
//    begin
//      Close;
//      SQL.Clear;
//      SQL.Add('select isnull(sum(Piece), 0) as sselJS from Tat_Cloth_DR_Sub ');
//      SQL.Add(' where DRMID = ' + QuotedStr(Trim(mDRMID)));
//      Open;
//    end;
    // 如果没有选择任何记录

    if Order_Main.IsEmpty then
      exit;
    with Order_Main do
    begin
      if fieldbyname('Ssel').AsBoolean = true then
        sselJS.Caption := currtostr(strtoCurr(sselJS.Caption) + Order_Main.FieldByName('sselJS').AsCurrency)
      else
        sselJS.Caption := currtostr(strtoCurr(sselJS.Caption) - Order_Main.FieldByName('sselJS').AsCurrency);
    end;

    Order_Main.EnableControls;

  except
    on E: Exception do
    begin
      // 记录错误信息
      sselJS.Caption := '0';
      ShowMessage('查询失败: ' + E.Message + #13#10 + 'SQL语句: ' + ADOQueryCmd.SQL.Text);
    end;
  end;
end;

procedure TfrmTatDRList.v1Column7PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with Order_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;

  end;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update Tat_Cloth_DR_Main set  ' + FFieldName + '=' + quotedstr(Trim(mvalue)));
    sql.Add('where  DRMID=' + quotedstr(Trim(Order_Main.fieldbyname('DRMID').AsString)));
    ExecSQL;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

end.

