unit U_ZDPrice;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, cxPC, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  U_BaseList, Vcl.Menus, Vcl.Clipbrd, DateUtils, dxSkinWXI,
  dxScrollbarAnnotations, cxContainer, cxProgressBar, cxLabel;

type
  TfrmZDPrice = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    Label1: TLabel;
    ComTaiTou: TEdit;
    ADOQueryPrint: TADOQuery;
    cxTabControl1: TcxTabControl;
    ToolButton1: TToolButton;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    Tv2Column4: TcxGridDBColumn;
    Tv2Column9: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column10: TcxGridDBColumn;
    Tv2Column5: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tv2Column6: TcxGridDBColumn;
    Tv2Column7: TcxGridDBColumn;
    ToolButton4: TToolButton;
    cxGridLevel2: TcxGridLevel;
    Tv3: TcxGridDBTableView;
    Tv3Column1: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    ToolButton2: TToolButton;
    Tv3Column4: TcxGridDBColumn;
    Tv3Column5: TcxGridDBColumn;
    Tv3Column6: TcxGridDBColumn;
    Tv3Column7: TcxGridDBColumn;
    Tv3Column8: TcxGridDBColumn;
    Tv3Column9: TcxGridDBColumn;
    Tv3Column10: TcxGridDBColumn;
    Tv3Column11: TcxGridDBColumn;
    Tv3Column12: TcxGridDBColumn;
    Tv3Column13: TcxGridDBColumn;
    Tv2Column8: TcxGridDBColumn;
    FromOrdNo: TcxTextEdit;
    cxLabel1: TcxLabel;
    PM_2: TPopupMenu;
    N2: TMenuItem;
    Tv3Column14: TcxGridDBColumn;
    Tv2Column11: TcxGridDBColumn;
    Tv2Column12: TcxGridDBColumn;
    Tv3Column15: TcxGridDBColumn;
    Tv3Column16: TcxGridDBColumn;
    Tv2Column13: TcxGridDBColumn;
    ToOrdNo: TcxTextEdit;
    cxLabel2: TcxLabel;
    Tv3Column17: TcxGridDBColumn;
    Tv3Column18: TcxGridDBColumn;
    Tv2Column14: TcxGridDBColumn;
    Tv2Column15: TcxGridDBColumn;
    N5: TMenuItem;
    N6: TMenuItem;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    Tv4: TcxGridDBTableView;
    cxGridLevel3: TcxGridLevel;
    Tv4Column1: TcxGridDBColumn;
    Tv4Column2: TcxGridDBColumn;
    Tv4Column3: TcxGridDBColumn;
    Tv4Column4: TcxGridDBColumn;
    Tv4Column5: TcxGridDBColumn;
    Tv4Column6: TcxGridDBColumn;
    Tv4Column7: TcxGridDBColumn;
    Tv4Column8: TcxGridDBColumn;
    Tv4Column9: TcxGridDBColumn;
    Tv4Column10: TcxGridDBColumn;
    Tv4Column11: TcxGridDBColumn;
    Tv4Column12: TcxGridDBColumn;
    Tv4Column13: TcxGridDBColumn;
    Tv4Column14: TcxGridDBColumn;
    Tv4Column15: TcxGridDBColumn;
    Tv4Column16: TcxGridDBColumn;
    Tv4Column17: TcxGridDBColumn;
    Tv4Column18: TcxGridDBColumn;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    Tv2Column16: TcxGridDBColumn;
    Tv2Column17: TcxGridDBColumn;
    Tv3Column19: TcxGridDBColumn;
    Tv3Column20: TcxGridDBColumn;
    Tv4Column19: TcxGridDBColumn;
    Tv4Column20: TcxGridDBColumn;
    ToolButton7: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FromOrdNoKeyPress(Sender: TObject; var Key: Char);
    procedure N2Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure Tv3DataControllerFilterChanged(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton6Click(Sender: TObject);
    procedure Tv3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure Tv4CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton7Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    fmanage, FZKType: string;
    { Public declarations }
    RKFlag, FCYID: string;
  end;

implementation

uses
  U_DataLink, U_RtFun, U_BatchEdit, U_ZDPriceDetails, U_PlanProfitDetails,U_BatchEditUnit;


{$R *.dfm}

procedure TfrmZDPrice.InitGrid();
begin
  try
//    Tv2.BeginUpdate;
//    Tv3.BeginUpdate;
    with ADOQueryMain do
    begin
      ADOQueryMain.Filtered := false;
      Close;
      SQL.Clear;
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' select BCIOID,IOTime,FromOrdNo, C_Code, C_Name, C_Color, Price, QtyUnit, qty, Piece, ToOrdNo = (select OrderNo from Trade_Plan_Main where MainID in(select MainID from Pur_ClothPlan_Main X where X.PurNo = A.ToOrdNo)),ZDAmount = (isnull(price, 0) * qty) ');
            sql.Add(' ,stkName, QtyUnit, FromMainId, C_ColorNo, C_Pattern, Chker, ChkTime');
            sql.Add(' from Bs_Cloth_IO A');
            sql.Add(' where IOTime >= ' + quotedStr(FormatDateTime('yyyy-MM-dd', begdate.DateTime)));
            sql.Add(' and IOTime <= ' + quotedStr(FormatDateTime('yyyy-MM-dd', enddate.DateTime)));
            sql.Add(' and STKName = ''贸易坯布'' and FromOrdNo <> (select OrderNo from Trade_Plan_Main where MainID in(select MainID from Pur_ClothPlan_Main X where X.PurNo = A.ToOrdNo))');
            sql.Add(' order by iotime desc');
            Open;
            SCreateCDS(ADOQueryMain, CDS_1);
            SInitCDSData(ADOQueryMain, CDS_1);
            cxGrid2.ActiveLevel := cxGridLevel1;
          end;
        1:
          begin
            sql.Add(' select BCIOID,IOTime,FromOrdNo, C_Code, C_Name, C_Color, Price, QtyUnit, qty, Piece, ToOrdNo = (select OrderNo from Trade_Plan_Main where MainID in(select MainID from Pur_ClothPlan_Main X where X.PurNo = A.ToOrdNo)),ZDAmount = (isnull(price, 0) * qty) ');
            sql.Add(' ,stkName, QtyUnit, FromMainId, C_ColorNo, C_Pattern, Chker, ChkTime ');
            sql.Add(' from Bs_Cloth_IO A');
            sql.Add(' where IOTime >= ' + quotedStr(FormatDateTime('yyyy-MM-dd', begdate.DateTime)));
            sql.Add(' and IOTime <= ' + quotedStr(FormatDateTime('yyyy-MM-dd', enddate.DateTime)));
            sql.Add(' and STKName = ''贸易待检布'' and FromOrdNo <> (select OrderNo from Trade_Plan_Main where MainID in(select MainID from Pur_ClothPlan_Main X where X.PurNo = A.ToOrdNo)) ');
            sql.Add(' order by iotime desc');
            Open;
            SCreateCDS(ADOQueryMain, CDS_1);
            SInitCDSData(ADOQueryMain, CDS_1);
            cxGrid2.ActiveLevel := cxGridLevel1;
          end;
        2:
          begin
            sql.Add(' select *,A.OrderNo as FromOrdNo, B.OrderNo as ToOrdNo, DlyDate = (select DlyDate from Trade_Cloth_DR_Main X where X.DRMID = B.DRMID) ');
            sql.Add(' ,ZDAmount = (case when QtyUnit = ''M'' then isnull(ZDPrice, 0) * StkMeter when QtyUnit = ''Y'' then isnull(ZDPrice, 0) * StkYardage when QtyUnit = ''Kg'' then isnull(ZDPrice, 0) * StkNetWeight else 0 end) ');
            sql.Add(' from Trade_Cloth_Stock_ZD A join Trade_Cloth_DR_Sub B on A.DRSID=B.DRSID ');
            sql.Add(' where (select DlyDate from Trade_Cloth_DR_Main X where X.DRMID = B.DRMID) >= ' + quotedStr(FormatDateTime('yyyy-MM-dd', begdate.DateTime)));
            sql.Add(' and (select DlyDate from Trade_Cloth_DR_Main X where X.DRMID = B.DRMID) <= ' + quotedStr(FormatDateTime('yyyy-MM-dd', enddate.DateTime)));
            sql.add(' order by ZDTime desc ');
            Open;
            SCreateCDS(ADOQueryMain, CDS_2);
            SInitCDSData(ADOQueryMain, CDS_2);
            cxGrid2.ActiveLevel := cxGridLevel2;
          end;
        3:
          begin
            sql.Add(' select B.stkid,A.Editer,A.Edittime,B.OrderNo fromordno,A.IOID,A.IOTime,B.C_Code,B.C_Name,B.PieceNo,B.StkMeter,B.StkYardage,B.StkGrossWeight,');
            SQL.Add(' B.StkNetWeight,B.C_Color,B.C_Pattern,B.C_ColorNo,B.MainID,');
            SQL.Add(' iif(A.price is null, (select top 1 price from Trade_Cloth_DR_Sub X WHERE X.DRMID = A.IONO), A.price) price, ');
            sql.Add('    (SELECT TOP 1 QtyUnit FROM Trade_Cloth_DR_Sub X WHERE X.DRMID = A.IONO) AS QtyUnit, ');
            sql.Add(' cast (case when (SELECT TOP 1 QtyUnit FROM Trade_Cloth_DR_Sub X WHERE X.DRMID = A.IONO)  = ''M'' ');
            sql.Add(' then iif(A.price is null, (select top 1 price from Trade_Cloth_DR_Sub X WHERE X.DRMID = A.IONO)* StkMeter, A.Amount)');
            SQL.Add(' when (SELECT TOP 1 QtyUnit FROM Trade_Cloth_DR_Sub X WHERE X.DRMID = A.IONO) = ''Kg'' ');
            SQL.Add(' 		then iif(A.price is null, (select top 1 price from Trade_Cloth_DR_Sub X WHERE X.DRMID = A.IONO) * StkNetWeight, A.Amount) ');
            SQL.Add(' 	when (SELECT TOP 1 QtyUnit FROM Trade_Cloth_DR_Sub X WHERE X.DRMID = A.IONO) = ''Y'' ');
            sql.add(' 		then iif(A.price is null, (select top 1 price from Trade_Cloth_DR_Sub X WHERE X.DRMID = A.IONO) * StkYardage, A.Amount) ');
            SQL.Add(' 	end as DECIMAL(18,2)) as zdamount,');
            sql.Add('    STUFF(');
            sql.Add('        (SELECT DISTINCT '', '' + OrderNo ');
            sql.Add('         FROM Trade_Cloth_DR_Sub X ');
            sql.Add('         WHERE X.DRMID = A.IONO ');
            sql.Add('         FOR XML PATH('''')), ');
            sql.Add('        1, 2, '''') AS ToOrdNo ');
            sql.Add('FROM ');
            sql.Add('    Trade_Cloth_IO A ');
            sql.Add('JOIN ');
            sql.Add('    Trade_Cloth_Stock B ');
            sql.Add('    ON A.StkID = B.StkID ');
            sql.Add('WHERE ');
            sql.Add('    B.OrderNo <> STUFF(');
            sql.Add('        (SELECT DISTINCT '', '' + OrderNo ');
            sql.Add('         FROM Trade_Cloth_DR_Sub X ');
            sql.Add('         WHERE X.DRMID = A.IONO ');
            sql.Add('         FOR XML PATH('''')), ');
            sql.Add('        1, 2, '''') ');
            sql.Add('    AND A.IOType IN (''加工出库'', ''回修出库'')');
            sql.Add('    order by iotime desc');
//            ShowMessage(sql.Text);
            Open;
            SCreateCDS(ADOQueryMain, CDS_3);
            SInitCDSData(ADOQueryMain, CDS_3);
            cxGrid2.ActiveLevel := cxGridLevel3;
          end;
      end;
    end;
  finally

//    Tv2.EndUpdate;
//    Tv3.EndUpdate;
  end;
end;

procedure TfrmZDPrice.N2Click(Sender: TObject);
begin
  inherited;
  if (cxTabControl1.TabIndex = 0) or (cxTabControl1.TabIndex = 1) then
    Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV2.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)))
  else if cxTabControl1.TabIndex = 2 then
    Clipboard.SetTextBuf(PChar(Trim(CDS_2.fieldbyname(TV3.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)))
  else if cxTabControl1.TabIndex = 3 then
    Clipboard.SetTextBuf(PChar(Trim(CDS_3.fieldbyname(TV4.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)))
end;

procedure TfrmZDPrice.N5Click(Sender: TObject);
begin
  inherited;
  if (cxTabControl1.TabIndex = 0) or (cxTabControl1.TabIndex = 1) then
    SelOKNo(CDS_1, true)
  else if cxTabControl1.TabIndex = 2 then
    SelOKNo(CDS_2, true)
  else if cxTabControl1.TabIndex = 3 then
    SelOKNo(CDS_3, true);
end;

procedure TfrmZDPrice.N6Click(Sender: TObject);
begin
  inherited;
  if (cxTabControl1.TabIndex = 0) or (cxTabControl1.TabIndex = 1) then
    SelOKNo(CDS_1, false)
  else if cxTabControl1.TabIndex = 2 then
    SelOKNo(CDS_2, false)
  else if cxTabControl1.TabIndex = 3 then
    SelOKNo(CDS_3, false);
end;

procedure TfrmZDPrice.FromOrdNoKeyPress(Sender: TObject; var Key: Char);
begin
  inherited;
  if Key = #13 then
  begin
    ToolButton2.Click;
  end;
end;

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

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

procedure TfrmZDPrice.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption), Tv2, '转单价格管理');
  ReadCxGrid(trim(self.Caption), Tv3, '转单价格管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
//  begdate.DateTime := StartOfTheMonth(Enddate.DateTime);
  begdate.DateTime := EndDate.DateTime - 365;
  cxTabControl1.TabIndex := 0;
  InitGrid();
end;

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

procedure TfrmZDPrice.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv2, '转单价格管理');
  WriteCxGrid(trim(self.Caption), Tv3, '转单价格管理');
end;

procedure TfrmZDPrice.ToolButton2Click(Sender: TObject);
begin
  inherited;
  if (cxTabControl1.TabIndex = 0) or (cxTabControl1.TabIndex = 1) then
  begin
    if ADOQueryMain.Active then
    begin
      SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
      SCreateCDS(ADOQueryMain, CDS_1);
      SInitCDSData(ADOQueryMain, CDS_1);
    end;
  end
  else if cxTabControl1.TabIndex = 2 then
  begin
    if ADOQueryMain.Active then
    begin
      SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
      SCreateCDS(ADOQueryMain, CDS_2);
      SInitCDSData(ADOQueryMain, CDS_2);
    end;
  end
  else if cxTabControl1.TabIndex = 3 then
  begin
    if ADOQueryMain.Active then
    begin
      SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
      SCreateCDS(ADOQueryMain, CDS_3);
      SInitCDSData(ADOQueryMain, CDS_3);
    end;
  end;

end;

procedure TfrmZDPrice.ToolButton3Click(Sender: TObject);
var
  PBCIOID, PStkId, PIOID, POrderNo: string;
  Bookmark: TBookmark;
begin
  inherited;

  if (cxTabControl1.TabIndex = 0) or (cxTabControl1.TabIndex = 1) then
    Bookmark := CDS_1.GetBookmark
  else if cxTabControl1.TabIndex = 2 then
    Bookmark := CDS_2.GetBookmark
  else if cxTabControl1.TabIndex = 3 then
    Bookmark := CDS_3.GetBookmark;
  PBCIOID := '';
  PStkId := '';
  if cxGrid2.ActiveLevel = cxGridLevel1 then
  begin
    if CDS_1.locate('ssel', true, []) = false then
    begin
      Application.MessageBox('请选择数据', '提示信息', 0);
      exit;
    end;

    CDS_1.DisableConstraints;
    Tv2.BeginUpdate;
    with CDS_1 do
    begin
      first;
      while not eof do
      begin
        if FieldByName('ssel').AsBoolean = true then
        begin
          if PBCIOID = '' then
          begin
            PBCIOID := PBCIOID + '''' + FieldByName('BCIOID').AsString + '''';
            POrderNo := POrderNo + '''' + FieldByName('fromOrdNo').AsString + '''' + ',''' + FieldByName('ToOrdNo').AsString + '''';
          end
          else
          begin
            PBCIOID := PBCIOID + ',''' + FieldByName('BCIOID').AsString + '''';
            POrderNo := POrderNo + ',''' + FieldByName('fromOrdNo').AsString + '''' + ',''' + FieldByName('ToOrdNo').AsString + '''';
          end;
        end;
        Next;
      end;
    end;

    CDS_1.EnableConstraints;
    Tv2.EndUpdate;

  end
  else if cxGrid2.ActiveLevel = cxGridLevel2 then
  begin
    if CDS_2.locate('ssel', true, []) = false then
    begin
      Application.MessageBox('请选择数据', '提示信息', 0);
      exit;
    end;

    CDS_2.DisableConstraints;
    Tv3.BeginUpdate;
    with CDS_2 do
    begin
      first;
      while not eof do
      begin
        if FieldByName('ssel').AsBoolean = true then
        begin
          if PStkId = '' then
          begin
            PStkId := PStkId + '''' + FieldByName('StkId').AsString + '''';
            POrderNo := POrderNo + '''' + FieldByName('fromOrdNo').AsString + '''' + ',''' + FieldByName('ToOrdNo').AsString + '''';
          end
          else
          begin
            PStkId := PStkId + ',''' + FieldByName('StkId').AsString + '''';
            POrderNo := POrderNo + '''' + FieldByName('fromOrdNo').AsString + '''' + ',''' + FieldByName('ToOrdNo').AsString + '''';
          end;
        end;
        Next;
      end;
    end;
    CDS_2.EnableControls;
    Tv3.endUpdate;
  end
  else if cxGrid2.ActiveLevel = cxGridLevel3 then
  begin
    if CDS_3.locate('ssel', true, []) = false then
    begin
      Application.MessageBox('请选择数据', '提示信息', 0);
      exit;
    end;

    CDS_3.DisableConstraints;
    Tv4.BeginUpdate;
    with CDS_3 do
    begin
      first;
      while not eof do
      begin
        if FieldByName('ssel').AsBoolean = true then
        begin
          if PIOID = '' then
          begin
            PIOID := PIOID + '''' + FieldByName('IOID').AsString + '''';
            POrderNo := POrderNo + '''' + FieldByName('fromOrdNo').AsString + '''' + ',''' + FieldByName('ToOrdNo').AsString + '''';
          end
          else
          begin
            PIOID := PIOID + ',''' + FieldByName('IOID').AsString + '''';
            POrderNo := POrderNo + '''' + FieldByName('fromOrdNo').AsString + '''' + ',''' + FieldByName('ToOrdNo').AsString + '''';
          end;
        end;
        Next;
      end;
    end;
    CDS_3.EnableControls;
    Tv4.endUpdate;
  end;

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    if (cxTabControl1.TabIndex = 0) or (cxTabControl1.TabIndex = 1) then
      sql.Add(' select * from Trade_Plan_Main where OrderNo in (' + POrderNo + ') and status in(''10'',''12'') ')
    else if cxTabControl1.TabIndex = 2 then
      sql.Add(' select * from Trade_Plan_Main where OrderNo in (' + POrderNo + ') and status in(''10'',''12'') ')
    else if cxTabControl1.TabIndex = 3 then
      sql.Add(' select * from Trade_Plan_Main where OrderNo in (' + POrderNo + ') and status in(''10'',''12'') ');
//    ShowMessage(SQL.Text);
    open;
  end;

  if not ADOQueryTemp.IsEmpty then
  begin
    application.MessageBox('计划单已完成或已清算，无法删除', '提示');
    Exit;
  end;

  try
    frmBatchEdit := TfrmBatchEdit.Create(Application);
    with frmBatchEdit do
    begin
      FBCIOID := PBCIOID;
      FStkId := PStkId;
      FIOID := PIOID;
      Fflag := cxTabControl1.TabIndex;
      if ShowModal = mrOk then
      begin
        self.InitGrid();
      end;
      if (cxTabControl1.TabIndex = 0) or (cxTabControl1.TabIndex = 1) then
      begin
        if CDS_1.BookmarkValid(Bookmark) then
        begin
          CDS_1.GotoBookmark(Bookmark);
        end;
      end
      else if cxTabControl1.TabIndex = 2 then
      begin
        if CDS_2.BookmarkValid(Bookmark) then
        begin
          CDS_2.GotoBookmark(Bookmark);
        end;
      end
      else if cxTabControl1.TabIndex = 3 then
      begin
        if CDS_3.BookmarkValid(Bookmark) then
        begin
          CDS_3.GotoBookmark(Bookmark);
        end;
      end;
    end;
  finally
    frmBatchEdit.Free;
  end;
end;

procedure TfrmZDPrice.ToolButton4Click(Sender: TObject);
begin
//  with ADOQueryCmd do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select Top 1 OrdUnit from Trade_Plan_Sub A left join Trade_Plan_Main B on A.MainID=B.MainID ');
//    SQL.Add('where OrderNO=''' + Trim(CDS_1.fieldbyname('FromOrdNo').AsString) + '''');
//    open;
//  end;

  try
    frmPlanProfitDetails := TfrmPlanProfitDetails.Create(Application);
    with frmPlanProfitDetails do
    begin
//      begdate.DateTime := self.begdate.DateTime;
//      enddate.DateTime := self.Enddate.DateTime;
      if (cxTabControl1.TabIndex = 0) or (cxTabControl1.TabIndex = 1) then
      begin
        FMainID := self.CDS_1.fieldbyname('FromMainID').AsString;
        OrderNo.Text := self.CDS_1.fieldbyname('FromOrdNo').AsString;
        FOrderNo := self.CDS_1.fieldbyname('FromOrdNo').AsString;
        FUnit := Self.CDS_1.FieldByName('QtyUnit').ASString;
        FOrderNo2 := self.CDS_1.fieldbyname('ToOrdNo').AsString;
      end
      else if cxTabControl1.TabIndex = 2 then
      begin
        FMainID := self.CDS_2.fieldbyname('MainID').AsString;
        OrderNo.Text := self.CDS_2.fieldbyname('fromOrdNo').AsString;
        FOrderNo := self.CDS_2.fieldbyname('fromOrdNo').AsString;
        FUnit := Self.CDS_2.FieldByName('QtyUnit').ASString;
      end
      else if cxTabControl1.TabIndex = 3 then
      begin
        FMainID := self.CDS_3.fieldbyname('MainID').AsString;
        OrderNo.Text := self.CDS_3.fieldbyname('fromOrdNo').AsString;
        FOrderNo := self.CDS_3.fieldbyname('fromOrdNo').AsString;
        FUnit := Self.CDS_3.FieldByName('QtyUnit').ASString;
      end;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmPlanProfitDetails.Free;
  end;
end;

procedure TfrmZDPrice.ToolButton5Click(Sender: TObject);
var
  PBCIOID, PStkId: string;
  Bookmark: TBookmark;
begin
  inherited;
  if (cxTabControl1.TabIndex = 0) or (cxTabControl1.TabIndex = 1) then
    Bookmark := CDS_1.GetBookmark
  else if cxTabControl1.TabIndex = 2 then
    Bookmark := CDS_2.GetBookmark
  else if cxTabControl1.TabIndex = 3 then
    Bookmark := CDS_3.GetBookmark;
  PBCIOID := '';
  PStkId := '';
  if cxGrid2.ActiveLevel = cxGridLevel1 then
  begin
    if CDS_1.locate('ssel', true, []) = false then
    begin
      Application.MessageBox('请选择数据', '提示信息', 0);
      exit;
    end;

    Tv2.BeginUpdate;
    with CDS_1 do
    begin
      first;
      while not eof do
      begin
        if FieldByName('ssel').AsBoolean = true then
        begin
          if PBCIOID = '' then
          begin
            PBCIOID := PBCIOID + '''' + FieldByName('BCIOID').AsString + '''';
          end
          else
          begin
            PBCIOID := PBCIOID + ',''' + FieldByName('BCIOID').AsString + '''';
          end;
        end;
        Next;
      end;
    end;

    with ADOQueryCmd do
    begin
      close;
      SQL.Clear;
      sql.add('update bs_cloth_io set chker = ' + QuotedStr(DName) + 'where bcioid in (' + PBCIOID + ')');
      sql.add('update bs_cloth_io set Chktime = getdate() where bcioid in (' + PBCIOID + ')');
      ExecSQL;
//      ShowMessage(sql.text);
    end;

    Tv2.EndUpdate;
  end
  else if cxGrid2.ActiveLevel = cxGridLevel2 then
  begin
    if CDS_2.locate('ssel', true, []) = false then
    begin
      Application.MessageBox('请选择数据', '提示信息', 0);
      exit;
    end;

    Tv3.BeginUpdate;
    with CDS_2 do
    begin
      first;
      while not eof do
      begin
        if FieldByName('ssel').AsBoolean = true then
        begin
          if PStkId = '' then
          begin
            PStkId := PStkId + '''' + FieldByName('StkId').AsString + '''';
          end
          else
          begin
            PStkId := PStkId + ',''' + FieldByName('StkId').AsString + '''';
          end;
        end;
        Next;
      end;
    end;

    with ADOQueryCmd do
    begin
      close;
      SQL.Clear;
      sql.add('update Trade_Cloth_Stock_ZD set ConMID = ' + QuotedStr(DName) + 'where stkid in (' + PStkId + ')');
      sql.add('update Trade_Cloth_Stock_ZD set ConSID = CONVERT(VARCHAR(10), GETDATE(), 23) where stkid in (' + PStkId + ')');
//      ShowMessage(sql.text);
      ExecSQL;
    end;

    Tv3.endUpdate;
  end
  else if cxGrid2.ActiveLevel = cxGridLevel3 then
  begin
    if CDS_3.locate('ssel', true, []) = false then
    begin
      Application.MessageBox('请选择数据', '提示信息', 0);
      exit;
    end;

    Tv4.BeginUpdate;
    with CDS_3 do
    begin
      first;
      while not eof do
      begin
        if FieldByName('ssel').AsBoolean = true then
        begin
          if PBCIOID = '' then
          begin
            PBCIOID := PBCIOID + '''' + FieldByName('ioid').AsString + '''';
          end
          else
          begin
            PBCIOID := PBCIOID + ',''' + FieldByName('ioid').AsString + '''';
          end;
        end;
        Next;
      end;
    end;

    with ADOQueryCmd do
    begin
      close;
      SQL.Clear;
      sql.add('update Trade_Cloth_io set Editer = ' + QuotedStr(DName) + 'where ioid in (' + PBCIOID + ')');
      sql.add('update Trade_Cloth_io set Edittime = CONVERT(VARCHAR(10), GETDATE(), 23) where ioid in (' + PBCIOID + ')');
//      ShowMessage(sql.text);
      ExecSQL;
    end;

    Tv4.endUpdate;
  end;
  InitGrid();
  if (cxTabControl1.TabIndex = 0) or (cxTabControl1.TabIndex = 1) then
  begin
    if CDS_1.BookmarkValid(Bookmark) then
    begin
      CDS_1.GotoBookmark(Bookmark);
    end
  end
  else if cxTabControl1.TabIndex = 2 then
  begin
    if CDS_2.BookmarkValid(Bookmark) then
    begin
      CDS_2.GotoBookmark(Bookmark);
    end
  end
  else if cxTabControl1.TabIndex = 3 then
  begin
    if CDS_3.BookmarkValid(Bookmark) then
    begin
      CDS_3.GotoBookmark(Bookmark);
    end
  end;
end;

procedure TfrmZDPrice.ToolButton6Click(Sender: TObject);
var
  PBCIOID, PStkId: string;
  Bookmark: TBookmark;
begin
  inherited;
  if (cxTabControl1.TabIndex = 0) or (cxTabControl1.TabIndex = 1) then
    Bookmark := CDS_1.GetBookmark
  else if cxTabControl1.TabIndex = 2 then
    Bookmark := CDS_2.GetBookmark
  else if cxTabControl1.TabIndex = 3 then
    Bookmark := CDS_2.GetBookmark;
  PBCIOID := '';
  PStkId := '';
  if cxGrid2.ActiveLevel = cxGridLevel1 then
  begin
    if CDS_1.locate('ssel', true, []) = false then
    begin
      Application.MessageBox('请选择数据', '提示信息', 0);
      exit;
    end;

    Tv2.BeginUpdate;
    with CDS_1 do
    begin
      first;
      while not eof do
      begin
        if FieldByName('ssel').AsBoolean = true then
        begin
          if PBCIOID = '' then
          begin
            PBCIOID := PBCIOID + '''' + FieldByName('BCIOID').AsString + '''';
          end
          else
          begin
            PBCIOID := PBCIOID + ',''' + FieldByName('BCIOID').AsString + '''';
          end;
        end;
        Next;
      end;
    end;

    with ADOQueryCmd do
    begin
      close;
      SQL.Clear;
      sql.add('update bs_cloth_io set chker = null where bcioid in (' + PBCIOID + ')');
      sql.add('update bs_cloth_io set Chktime = null where bcioid in (' + PBCIOID + ')');
      ExecSQL;
//      ShowMessage(sql.text);
    end;

    Tv2.EndUpdate;
  end
  else if cxGrid2.ActiveLevel = cxGridLevel2 then
  begin
    if CDS_2.locate('ssel', true, []) = false then
    begin
      Application.MessageBox('请选择数据', '提示信息', 0);
      exit;
    end;

    Tv3.BeginUpdate;
    with CDS_2 do
    begin
      first;
      while not eof do
      begin
        if FieldByName('ssel').AsBoolean = true then
        begin
          if PStkId = '' then
          begin
            PStkId := PStkId + '''' + FieldByName('StkId').AsString + '''';
          end
          else
          begin
            PStkId := PStkId + ',''' + FieldByName('StkId').AsString + '''';
          end;
        end;
        Next;
      end;
    end;

    with ADOQueryCmd do
    begin
      close;
      SQL.Clear;
      sql.add('update Trade_Cloth_Stock_ZD set ConMID = null where stkid in (' + PStkId + ')');
      sql.add('update Trade_Cloth_Stock_ZD set ConSID = null where stkid in (' + PStkId + ')');
      ExecSQL;
    end;

    Tv3.endUpdate;
  end
  else if cxGrid2.ActiveLevel = cxGridLevel3 then
  begin
    if CDS_3.locate('ssel', true, []) = false then
    begin
      Application.MessageBox('请选择数据', '提示信息', 0);
      exit;
    end;

    Tv4.BeginUpdate;
    with CDS_3 do
    begin
      first;
      while not eof do
      begin
        if FieldByName('ssel').AsBoolean = true then
        begin
          if PBCIOID = '' then
          begin
            PBCIOID := PBCIOID + '''' + FieldByName('ioid').AsString + '''';
          end
          else
          begin
            PBCIOID := PBCIOID + ',''' + FieldByName('ioid').AsString + '''';
          end;
        end;
        Next;
      end;
    end;

    with ADOQueryCmd do
    begin
      close;
      SQL.Clear;
      sql.add('update Trade_Cloth_IO set Editer = null where ioid in (' + PBCIOID + ')');
      sql.add('update Trade_Cloth_IO set Edittime = null where ioid in (' + PBCIOID + ')');
      ExecSQL;
    end;

    Tv4.endUpdate;
  end;
  InitGrid();
  if (cxTabControl1.TabIndex = 0) or (cxTabControl1.TabIndex = 1) then
  begin
    if CDS_1.BookmarkValid(Bookmark) then
    begin
      CDS_1.GotoBookmark(Bookmark);
    end
  end
  else if cxTabControl1.TabIndex = 2 then
  begin
    if CDS_2.BookmarkValid(Bookmark) then
    begin
      CDS_2.GotoBookmark(Bookmark);
    end
  end
  else if cxTabControl1.TabIndex = 3 then
  begin
    if CDS_3.BookmarkValid(Bookmark) then
    begin
      CDS_3.GotoBookmark(Bookmark);
    end
  end;
end;

procedure TfrmZDPrice.ToolButton7Click(Sender: TObject);
var
  PDRSID, POrderNo: string;
  Bookmark: TBookmark;
begin
  inherited;

  if cxTabControl1.TabIndex <> 2 then
    Exit;

  Bookmark := CDS_2.GetBookmark;
  PDRSID := '';
  POrderNo := '';

  if not CDS_2.Locate('ssel', True, []) then
  begin
    Application.MessageBox('请选择数据', '提示信息', 0);
    Exit;
  end;

  CDS_2.DisableControls;
  Tv3.BeginUpdate;
  try
    with CDS_2 do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('ssel').AsBoolean then
        begin
          if PDRSID = '' then
            PDRSID := '''' + FieldByName('DRSID').AsString + ''''
          else
            PDRSID := PDRSID + ',''' + FieldByName('DRSID').AsString + '''';

          if POrderNo = '' then
            POrderNo := '''' + FieldByName('fromOrdNo').AsString + '''' + ',''' + FieldByName('ToOrdNo').AsString + ''''
          else
            POrderNo := POrderNo + ',''' + FieldByName('fromOrdNo').AsString + '''' + ',''' + FieldByName('ToOrdNo').AsString + '''';
        end;
        Next;
      end;
    end;
  finally
    CDS_2.EnableControls;
    Tv3.EndUpdate;
  end;

  if PDRSID = '' then
  begin
    Application.MessageBox('请选择数据', '提示信息', 0);
    Exit;
  end;

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from Trade_Plan_Main where OrderNo in (' + POrderNo + ') and status in(''10'',''12'')');
    Open;
  end;

  if not ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('计划单已完成或已清算，无法修改', '提示', 0);
    Exit;
  end;

  try
    frmBatchEditUnit := TfrmBatchEditUnit.Create(Application);
    with frmBatchEditUnit do
    begin
      FDRSIDList := PDRSID;
      Fflag := 2;
      if ShowModal = mrOk then
      begin
        Self.InitGrid();
      end;
      if CDS_2.BookmarkValid(Bookmark) then
        CDS_2.GotoBookmark(Bookmark);
    end;
  finally
    frmBatchEditUnit.Free;
  end;
end;

procedure TfrmZDPrice.Tv2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  mColumn: TcxGridDBColumn;
  mvalue: Variant;
begin
  inherited;

  mColumn := Tv2.GetColumnByFieldName('chker');
  if mColumn = nil then
    exit;
  mvalue := AViewInfo.GridRecord.Values[mColumn.index];
  if (mvalue <> null) and (mvalue <> '') then
  begin
    ACanvas.Font.Color := clBlue;
    Exit;
  end;

end;

procedure TfrmZDPrice.Tv3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  mColumn: TcxGridDBColumn;
  mvalue: Variant;
begin
  inherited;
  mColumn := Tv3.GetColumnByFieldName('ConMID');
  if mColumn = nil then
    exit;
  mvalue := AViewInfo.GridRecord.Values[mColumn.index];
  if (mvalue <> null) and (mvalue <> '') then
  begin
    ACanvas.Font.Color := clBlue;
    Exit;
  end;
end;

procedure TfrmZDPrice.Tv3DataControllerFilterChanged(Sender: TObject);
begin
  inherited;
  CDS_2.Filtered := false;
  CDS_2.Filter := TV3.DataController.Filter.FilterText;
  CDS_2.Filtered := true;

end;

procedure TfrmZDPrice.Tv4CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  mColumn: TcxGridDBColumn;
  mvalue: Variant;
begin
  inherited;
  mColumn := Tv4.GetColumnByFieldName('Editer');
  if mColumn = nil then
    exit;
  mvalue := AViewInfo.GridRecord.Values[mColumn.index];
  if (mvalue <> null) and (mvalue <> '') then
  begin
    ACanvas.Font.Color := clBlue;
    Exit;
  end;
end;

procedure TfrmZDPrice.cxTabControl1Change(Sender: TObject);
begin
  ToolButton7.Visible := (cxTabControl1.TabIndex = 2);
  InitGrid();
end;

end.

