unit U_TradeClothOutListNew;

interface

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

type
  TfrmTradeClothOutListNew = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    GPM_1: TcxGridPopupMenu;
    CDS_1: TClientDataSet;
    TBRKCX: TToolButton;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    N3: TMenuItem;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    Pnl_F1: TPanel;
    Label3: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    Label5: TLabel;
    Label20: TLabel;
    Label23: TLabel;
    Label1: TLabel;
    Label6: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    OrderNo: TEdit;
    conNO: TEdit;
    STKID: TEdit;
    C_Name: TEdit;
    C_Color: TEdit;
    C_Pattern: TEdit;
    C_Code: TEdit;
    PCId: TEdit;
    BatchNo: TEdit;
    CustName: TEdit;
    IONO: TEdit;
    Pnl_F2: TPanel;
    Label11: TLabel;
    Label12: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label21: TLabel;
    DateTimePicker3: TDateTimePicker;
    DateTimePicker4: TDateTimePicker;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit9: TEdit;
    Edit10: TEdit;
    Edit11: TEdit;
    GPM_2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    v1Column46: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    chk_F1: TCheckBox;
    Label9: TLabel;
    BegDate2: TDateTimePicker;
    EndDate2: TDateTimePicker;
    chk_F2: TCheckBox;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    PM_2: TPopupMenu;
    MenuItem3: TMenuItem;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column22: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column42: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column40: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    VC_BaoID: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    TV2Column4: TcxGridDBColumn;
    Label13: TLabel;
    Edit3: TEdit;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    TV2Column8: TcxGridDBColumn;
    TV2Column9: TcxGridDBColumn;
    TV2Column10: TcxGridDBColumn;
    DS_3: TDataSource;
    CDS_3: TClientDataSet;
    Panel1: TPanel;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridDBColumn32: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    cxGridDBColumn34: TcxGridDBColumn;
    cxGridDBColumn35: TcxGridDBColumn;
    cxGridDBColumn36: TcxGridDBColumn;
    cxGridDBColumn37: TcxGridDBColumn;
    cxGridDBColumn38: TcxGridDBColumn;
    cxGridDBColumn39: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel2: TPanel;
    Button1: TButton;
    TV2Column11: TcxGridDBColumn;
    TV3Column1: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    TV2Column12: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    TV2Column13: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure TBRKCXClick(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure BTNPClick(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure JYTypeChange(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure CDS_1BeforeOpen(DataSet: TDataSet);
    procedure Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
    procedure Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems6GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
    procedure ToolButton4Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure TV2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure MenuItem3Click(Sender: TObject);
    procedure TV2DblClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    lstCust: TStringList;
    procedure InitGrid(WSql: string);
    procedure InitPage();
    procedure SetStatus();
    { Private declarations }
  public
    canshu1, canshu2, FWorkshop: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint;

{$R *.dfm}
procedure TfrmTradeClothOutListNew.InitPage();
begin
  CurrentPage := 1;
  case cxPageControl1.ActivePageIndex of
    0:
      begin
        InitGrid(SGetHintFilters(Pnl_F2, 1, 2));
      end;
    1:
      begin
        InitGrid(SGetHintFilters(Pnl_F1, 1, 2));
      end;
  end;
end;

procedure TfrmTradeClothOutListNew.SetStatus();
begin
  TBRKCX.Enabled := False;
//  ToolButton1.Enabled := False;
  case cxPageControl1.ActivePageIndex of
    0:
      begin

      end;
    1:
      begin
//        ToolButton1.Enabled := true;
        if canshu1 = '管理' then
          TBRKCX.Enabled := true;
      end;
  end;
end;

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

procedure TfrmTradeClothOutListNew.FormCreate(Sender: TObject);
begin
  inherited;
  lstCust := TStringList.Create;
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 20;
  BegDate2.DateTime := BegDate.DateTime;
  EndDate2.DateTime := EndDate.DateTime;
  FWorkshop := trim(Self.fParameters3);
end;

procedure TfrmTradeClothOutListNew.InitGrid(WSql: string);
begin

  if Trim(WSql) <> '' then
    WSql := ' and ' + WSql;

  case cxPageControl1.ActivePageIndex of
    0:
      begin
        if chk_F2.Checked then
        begin
          WSql := WSql + ' and IOTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate2.DateTime));
          WSql := WSql + ' and IOTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate2.DateTime + 1));
        end;
        with ADOQueryMain do
        begin
          Close;
          sql.Clear;

//          sql.Add(' with a as(  ');
//          sql.Add(' select DISTINCT  A.IOID,A.StkId,A.IOCycles,A.IONo,A.IOTime,A.IOFlag,A.IOType,A.IOPosition,A.Tare,A.GrossWeight,A.NetWeight,A.Meter,A.Yardage ');
//          sql.Add(',A.FillId,A.Filler,A.Filltime,A.EditId,A.Editer,A.Edittime,A.DelId,A.Deler,A.Deltime,REPLACE(REPLACE(IONote, CHAR(10), ''''), CHAR(13), '''') AS IONote');
//          sql.Add(',B.CIID,B.StkCycles,B.StkExists,B.ConMId,B.ConSId,B.OrderNo,B.MainId,B.SubId,B.PCID,B.BCIOID,B.PacketId,B.PacketNo,B.PacketTime,B.PacketName,B.PacketCode,B.PieceNo,B.CoefficientType,B.Coefficient  ');
//          sql.Add(',B.StkPosition,B.StkGrossWeight,B.StkTare,B.StkNetWeight,B.StkMeter,B.StkYardage,B.Grade,B.BatchNo,B.CIMachNo,B.Loom,B.C_Code,B.C_Name,B.C_EName,B.C_Spec,B.C_Composition,B.C_EComposition,B.C_Color ');
//          sql.Add(',B.C_Width,B.C_GramWeight,B.C_Pattern,B.C_ColorNo,B.C_EColor  ');
//          sql.Add(',B.C_StyleNo,B.C_CustPattern,B.STKName,B.Workshop,B.FtyPCId  ');
//          sql.Add(',C.CustName,C.Saleser,D.BuyConNo,C.ConNo ');
//          sql.Add('from Trade_Cloth_IO A inner join Trade_Cloth_Stock B on A.STKID=B.StkId ');
//          sql.Add(' left join Trade_Plan_Main C on B.MainId=C.MainId ');
//          sql.Add('left join Trade_Plan_Sub D on D.MainId= C.MainID ');
//          sql.Add(' where 1=1');
//          sql.Add(WSql);
//          sql.Add(')');

          sql.Add(' select  IOTime=CONVERT(VARCHAR(10),IOTime,120),OrderNo,C_Name,PCId,IOType,Grade,CustName,C_Width,C_GramWeight,ToCoName');
          sql.Add(' ,CIMachNo,Workshop,BatchNo,IOPosition,IONote,C_Color,C_Pattern,Saleser,C_ColorNo,ConNo,BuyConNo,C_Code,IoNo,Filler ');
          sql.Add(' ,COUNT(PieceNo) SumPieceNo,sum(GrossWeight) SumGrossWeight,sum(Tare) SumTare,sum(NetWeight) SumNetWeight');
          sql.Add(' ,Sum(Meter) SumMeter,Sum(Yardage) SumYardage');
          sql.Add(' from V_Trade_Cloth_IO A ');

          sql.Add(' where IOFlag=''出库'' and 1=1' + WSql);
          if trim(FWorkshop) <> '' then
            sql.Add('  and Workshop=''' + trim(FWorkshop) + ''' ');

          sql.Add(' GROUP BY   CONVERT(VARCHAR(10),IOTime,120),OrderNo,C_Name,PCId,IOType,Grade,CustName,C_Width,C_GramWeight,ToCoName');
          sql.Add(' ,CIMachNo,Workshop,BatchNo,IOPosition,IONOte,C_Color,C_Pattern,Saleser,C_ColorNo,ConNo,BuyConNo,C_Code,IONo,Filler ');
//          showmessage(sql.text);
          Open;
        end;
        SCreateCDS(ADOQueryMain, CDS_2);
        SInitCDSData(ADOQueryMain, CDS_2);
      end;
    1:
      begin
        if chk_F1.Checked then
        begin
          WSql := WSql + ' and IOTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
          WSql := WSql + ' and IOTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
        end;
        with ADOQueryMain do
        begin
          Filtered := False;
          Close;
          sql.Clear;
//          sql.Add(' with a as(  ');
//          sql.Add(' select DISTINCT  A.IOID,A.StkId,A.IOCycles,A.IONo,A.IOTime,A.IOFlag,A.IOType,A.IOPosition,A.Tare,A.GrossWeight,A.NetWeight,A.Meter,A.Yardage ');
//          sql.Add(',A.FillId,A.Filler,A.Filltime,A.EditId,A.Editer,A.Edittime,A.DelId,A.Deler,A.Deltime,REPLACE(REPLACE(IONote, CHAR(10), ''''), CHAR(13), '''') AS IONote');
//          sql.Add(',B.CIID,B.StkCycles,B.StkExists,B.ConMId,B.ConSId,B.OrderNo,B.MainId,B.SubId,B.PCID,B.BCIOID,B.PacketId,B.PacketNo,B.PacketTime,B.PacketName,B.PacketCode,B.PieceNo,B.CoefficientType,B.Coefficient  ');
//          sql.Add(',B.StkPosition,B.StkGrossWeight,B.StkTare,B.StkNetWeight,B.StkMeter,B.StkYardage,B.Grade,B.BatchNo,B.CIMachNo,B.Loom,B.C_Code,B.C_Name,B.C_EName,B.C_Spec,B.C_Composition,B.C_EComposition,B.C_Color ');
//          sql.Add(',B.C_Width,B.C_GramWeight,B.C_Pattern,B.C_ColorNo,B.C_EColor  ');
//          sql.Add(',B.C_StyleNo,B.C_CustPattern,B.STKName,B.Workshop,B.FtyPCId  ');
//          sql.Add(',C.CustName,C.Saleser,D.BuyConNo,C.ConNo ');
//          sql.Add('from Trade_Cloth_IO A inner join Trade_Cloth_Stock B on A.STKID=B.StkId ');
//          sql.Add(' left join Trade_Plan_Main C on B.MainId=C.MainId ');
//          sql.Add('left join Trade_Plan_Sub D on D.MainId= C.MainID ');
//          sql.Add(' where 1=1');
//          sql.Add(WSql);
//          sql.Add(')');

          sql.Add(' select top ' + inttostr(RecordsNumber) + ' * ');
          sql.Add(' from  ');
          sql.Add('(select  rownumber=cast((row_number() over(order by A.IOTime desc)) as int)');
          sql.Add(',COUNT(1) OVER() AS TotalCount');
          sql.Add(',* from V_Trade_Cloth_IO  A where A.IOFlag = ''出库'' ');
          if trim(FWorkshop) <> '' then
            sql.Add(' and Workshop=''' + trim(FWorkshop) + ''' ');
          sql.Add('and 1=1 ' + WSql);
          sql.Add(') temp_row');
          sql.Add(' where  rownumber> ' + inttostr(((CurrentPage - 1) * RecordsNumber)));
          sql.Add(' order by rownumber');
//      showmessage(sql.text);
          Open;
        end;
        SCreateCDS(ADOQueryMain, CDS_1);
        SInitCDSData(ADOQueryMain, CDS_1);
        LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_1.FieldByName('TotalCount').AsInteger / RecordsNumber));
      end;
  end;

end;

procedure TfrmTradeClothOutListNew.TBRafreshClick(Sender: TObject);
begin
  InitPage();
end;

procedure TfrmTradeClothOutListNew.CDS_1BeforeOpen(DataSet: TDataSet);
begin

  lstCust.Clear;
end;

procedure TfrmTradeClothOutListNew.cxPageControl1Change(Sender: TObject);
begin
  SetStatus();
end;

procedure TfrmTradeClothOutListNew.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradeClothOutListNew.FormShow(Sender: TObject);
begin
  inherited;

  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '贸易成品仓库');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '贸易成品仓库');
  CurrentPage := 1;
  RecordsNumber := 500;
  canshu1 := self.fParameters1;
  canshu2 := self.fParameters3;
  FWorkshop := self.fParameters3;
  SetStatus();
  InitPage();
end;

procedure TfrmTradeClothOutListNew.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  if cxPageControl1.ActivePageIndex = 0 then
  begin
    TcxGridToExcel(Self.Caption, cxGrid2);
  end
  else
  begin
    TcxGridToExcel(Self.Caption, cxGrid1);
  end;
end;

procedure TfrmTradeClothOutListNew.orderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitPage();
  end;
end;

procedure TfrmTradeClothOutListNew.TBRKCXClick(Sender: TObject);
var
  MIOIDS, MStkIds, MOutNos, MPCIDS: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  MIOIDS := '';
  MStkIds := '';
  MOutNos := '';
  MPCIDS := '';
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.fieldbyname('SSel').AsBoolean then
      begin
        MIOIDS := MIOIDS + (Trim(CDS_1.fieldbyname('IOID').AsString)) + ',';
        MStkIds := MStkIds + (Trim(CDS_1.fieldbyname('STKID').AsString)) + ',';
        MOutNos := MOutNos + (Trim(CDS_1.fieldbyname('IONO').AsString)) + ',';
        MPCIDS := MPCIDS + (Trim(CDS_1.fieldbyname('PCID').AsString)) + ',';
      end;
      Next;
    end;
  end;
  MIOIDS := leftBstr(trim(MIOIDS), length(MIOIDS) - 1);
  MStkIds := leftBstr(trim(MStkIds), length(MStkIds) - 1);
  MOutNos := leftBstr(trim(MOutNos), length(MOutNos) - 1);
  CDS_1.Locate('SSel', True, []);
  CDS_1.EnableControls;

  if Application.MessageBox('确定要执行操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Trade_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''贸易成品出库''    ');
      sql.Add(',' + quotedstr(MPCIDS));
      sql.Add(',''撤销出库''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MStkIds));
      sql.Add(')  ');
      execsql;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''贸易成品出库''    ');
      sql.Add(',' + quotedstr(MOutNos));
      sql.Add(',''撤销出库''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MStkIds));
      sql.Add(')  ');
      execsql;
    end;

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Trade_Cloth_ReOut ');
      Sql.Add('@StkIds=' + quotedstr(trim(MStkIds)));
      Sql.Add(',@IOIDS=' + quotedstr(trim(MIOIDS)));
      Sql.Add(',@FillId=' + Quotedstr(DCode));
      Sql.Add(',@Filler=' + Quotedstr(DName));
//      ShowMessage(SQL.Text);
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;

    ADOQueryCmd.Connection.CommitTrans;
    InitPage();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmTradeClothOutListNew.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
//  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmTradeClothOutListNew.Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
begin
  lstCust.Clear;
end;

procedure TfrmTradeClothOutListNew.Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems6GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
begin
  if lstCust <> nil then
    AText := IntToStr(lstCust.Count);
end;

procedure TfrmTradeClothOutListNew.TV2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  BegDate.DateTime := BegDate2.DateTime;
  EndDate.DateTime := EndDate2.DateTime;
  chk_F2.Checked := chk_F1.Checked;
  cxPageControl1.ActivePageIndex := 1;
  SetStatus();
  InitGrid(SGetCDSFilters(Tv2, CDS_2));
end;

procedure TfrmTradeClothOutListNew.TV2DblClick(Sender: TObject);
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    sql.Add(' select  IOTime=CONVERT(VARCHAR(10),IOTime,120),OrderNo,C_Name,PCId,IOType,Grade,CustName,C_Width,C_GramWeight');
    sql.Add(' ,CIMachNo,Workshop,BatchNo,IOPosition,IONote,C_Color,C_Pattern,Saleser,C_ColorNo,ConNo,BuyConNo,C_Code,IoNo ');
    sql.Add(' ,COUNT(PieceNo) SumPieceNo,sum(GrossWeight) SumGrossWeight,sum(Tare) SumTare,sum(NetWeight) SumNetWeight');
    sql.Add(' ,Sum(Meter) SumMeter,Sum(Yardage) SumYardage');
    sql.Add(' from V_Trade_Cloth_IO A ');

    sql.Add(' where IOFlag=''出库'' and OrderNo=''' + trim(CDS_2.FieldByName('OrderNo').ASString) + ''' ');

    sql.Add(' GROUP BY   CONVERT(VARCHAR(10),IOTime,120),OrderNo,C_Name,PCId,IOType,Grade,CustName,C_Width,C_GramWeight');
    sql.Add(' ,CIMachNo,Workshop,BatchNo,IOPosition,IONote,C_Color,C_Pattern,Saleser,C_ColorNo,ConNo,BuyConNo,C_Code,IONo ');
//          showmessage(sql.text);
    Open;
  end;
  SCreateCDS(ADOQueryMain, CDS_3);
  SInitCDSData(ADOQueryMain, CDS_3);
  Panel1.Visible := True;
end;

procedure TfrmTradeClothOutListNew.BTNPClick(Sender: TObject);
begin
  if CurrentPage < CDS_1.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid(SGetHintFilters(Pnl_F1, 1, 2));
end;

procedure TfrmTradeClothOutListNew.Button1Click(Sender: TObject);
begin
  Panel1.Visible := False;

end;

procedure TfrmTradeClothOutListNew.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid(SGetHintFilters(Pnl_F1, 1, 2));
end;

procedure TfrmTradeClothOutListNew.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid(SGetHintFilters(Pnl_F1, 1, 2));
end;

procedure TfrmTradeClothOutListNew.JYTypeChange(Sender: TObject);
begin
  InitPage();
end;

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

procedure TfrmTradeClothOutListNew.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_1, true);

end;

procedure TfrmTradeClothOutListNew.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_1, false);

end;

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

procedure TfrmTradeClothOutListNew.ToolButton1Click(Sender: TObject);
begin

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeClothOut1';
      case cxPageControl1.ActivePageIndex of
        0:
          begin
            if not CDS_2.IsEmpty then
              FFiltration1 := Trim(self.CDS_2.fieldbyname('IONO').AsString);
          end;
        1:
          begin
            if not CDS_1.IsEmpty then
              FFiltration1 := Trim(self.CDS_1.fieldbyname('IONO').AsString);
          end;
      end;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmTradeClothOutListNew.ToolButton2Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  WSql := '';
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.fieldbyname('SSel').AsBoolean then
      begin
        if WSql <> '' then
        begin
          WSql := WSql + ',' + QuotedStr(Trim(CDS_1.fieldbyname('CIID').AsString));
        end
        else
        begin
          WSql := QuotedStr(Trim(CDS_1.fieldbyname('CIID').AsString));
        end;
      end;
      Next;
    end;
  end;
  CDS_1.Locate('SSel', True, []);
  CDS_1.EnableControls;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeClothOut2';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin

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

procedure TfrmTradeClothOutListNew.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '贸易成品仓库');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '贸易成品仓库');
end;

end.

