unit U_TradeClothStkListNew;

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, BtnEdit, Vcl.Buttons, dxSkinWXI, dxScrollbarAnnotations;

type
  TfrmTradeClothStkListNew = 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;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    PM_1: TPopupMenu;
    PM11: TMenuItem;
    N1: 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;
    lbl1: TLabel;
    Label20: TLabel;
    Label23: TLabel;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    OrderNo: TEdit;
    conNO: TEdit;
    STKID: TEdit;
    C_Name: TEdit;
    C_Color: TEdit;
    BCIOID: TEdit;
    C_Pattern: TEdit;
    C_Code: TEdit;
    Pnl_F2: TPanel;
    Label4: TLabel;
    Label6: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    DateTimePicker3: TDateTimePicker;
    DateTimePicker4: TDateTimePicker;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit7: TEdit;
    Edit8: 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;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    chk_F1: TCheckBox;
    Label1: TLabel;
    BegDate2: TDateTimePicker;
    EndDate2: TDateTimePicker;
    chk_F2: TCheckBox;
    TV2Column1: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    Label18: TLabel;
    Label19: TLabel;
    Edit9: TEdit;
    Edit10: TEdit;
    Label8: TLabel;
    Label14: TLabel;
    Edit3: TEdit;
    Edit11: TEdit;
    PM_2: TPopupMenu;
    MenuItem1: TMenuItem;
    N2: TMenuItem;
    TV2Column2: TcxGridDBColumn;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column22: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column42: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column34: TcxGridDBColumn;
    v1Column40: TcxGridDBColumn;
    v1Column46: TcxGridDBColumn;
    VC_BaoNo: TcxGridDBColumn;
    VC_PacketId: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolButton1: TToolButton;
    TV2Column4: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tv1Column5: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    TV2Column8: TcxGridDBColumn;
    TV2Column9: TcxGridDBColumn;
    Label11: TLabel;
    Edit6: TEdit;
    ToolButton5: TToolButton;
    Tv1Column9: TcxGridDBColumn;
    ToolButton6: TToolButton;
    TV2Column10: TcxGridDBColumn;
    ToolButton7: TToolButton;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    TV2Column11: TcxGridDBColumn;
    TV2Column12: TcxGridDBColumn;
    ToolButton8: TToolButton;
    Panel1: TPanel;
    Panel2: TPanel;
    IOPosition: TBtnEditC;
    Price: TEdit;
    库存调整: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn1: TBitBtn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    TV2Column13: TcxGridDBColumn;
    ToolButton9: TToolButton;
    StkType: TComboBox;
    TV2Column14: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Label15: TLabel;
    Panel3: TPanel;
    Label16: TLabel;
    Panel4: TPanel;
    Label17: TLabel;
    Panel5: TPanel;
    Label21: TLabel;
    JSUnit: TComboBox;
    ToolButton10: TToolButton;
    TV2Column15: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    ToolButton11: TToolButton;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    RMDB_1: TRMDBDataSet;
    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 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 ToolButton4Click(Sender: TObject);
    procedure PM11Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure TV2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure MenuItem1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1DataControllerSummaryFooterSummaryItemsSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
    procedure Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems6GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure IOPositionBtnUpClick(Sender: TObject);
    procedure IOPositionBtnDnClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure PriceKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton11Click(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    lstPacketId: TStringList;
    procedure InitGrid(WSql: string);
    procedure InitPage();
    procedure SetStatus();
    { Private declarations }
  public
    canshu1, FWorkshop, canshu2, canshu4, canshu5: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint, U_TradeClothSplit, U_ZDYHelp,
  U_TradeClothSplitWeight;

{$R *.dfm}
procedure TfrmTradeClothStkListNew.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 TfrmTradeClothStkListNew.IOPositionBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';

end;

procedure TfrmTradeClothStkListNew.IOPositionBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradeClothStkListNew.SetStatus();
begin
  if cxPageControl1.ActivePageIndex = 1 then
  begin
    if self.fParameters2 = '打印' then
      ToolButton11.Visible := true;
    ToolButton1.Visible := True;
    ToolButton9.Visible := True;
  end
  else
  begin
    ToolButton11.Visible := false;
    ToolButton1.Visible := False;
    ToolButton9.Visible := False;
  end;


//     if canshu1 then
  if canshu4 = '调整' then
  begin
    if cxPageControl1.ActivePageIndex = 1 then
    begin

      ToolButton8.Visible := True;

    end
    else
    begin

      ToolButton8.Visible := False;

    end;
  end
  else
  begin
    TV2Column12.visible := False;
    TV2Column12.VisibleForCustomization := False;
    TV2Column11.visible := False;
    TV2Column11.VisibleForCustomization := False;
    TV1Column13.visible := False;
    TV1Column14.visible := False;
    TV1Column13.VisibleForCustomization := False;
    TV1Column14.VisibleForCustomization := False;
  end;
end;

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

procedure TfrmTradeClothStkListNew.FormCreate(Sender: TObject);
begin
  inherited;
  lstPacketId := 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 TfrmTradeClothStkListNew.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
          Filtered := False;
          Close;
          sql.Clear;
          sql.Add(' select kl= DATEDIFF(dd,IOTime, GETDATE()),  IOTime=CONVERT(VARCHAR(10),IOTime,120),OrderNo,C_Name,PCId,IOType');
//          sql.Add(',Y_kl= DATEDIFF(YY,IOTime, GETDATE()),M_kl= DATEDIFF(MM,IOTime, GETDATE())');
          Sql.Add(',Grade,CustName,C_Width,C_GramWeight,CIMachNo,Workshop,BatchNo,IOPosition,C_Color,C_Pattern,StkType,Price');
          sql.Add(',C_Code,Saleser,dbo.getNum(C_ColorNo) SHNO,C_ColorNo,ConNo,BuyConNo,FOrdNo');
          sql.Add(' ,COUNT(PieceNo) SumPieceNo,sum(GrossWeight) SumGrossWeight,sum(Tare) SumTare,sum(NetWeight) SumNetWeight');
          sql.Add(' ,Sum(Meter) SumMeter,Sum(Yardage) SumYardage,Sum(Amount) Amount');
          sql.Add(' from V_Trade_Cloth_Stk A where 1=1 ');
          sql.Add(' and StkType=''' + trim(canshu5) + ''' ');
          if trim(FWorkshop) <> '' then
            sql.Add(' and Workshop=''' + trim(FWorkshop) + ''' ');
          if canshu2 = '死货' then
            sql.Add(' and ISDie=1 ');

          if canshu2 = '次品' then
            sql.Add(' and Grade=''次品'' ');

          sql.Add(WSql);
          sql.Add(' GROUP BY  DATEDIFF(dd,IOTime, GETDATE()),  CONVERT(VARCHAR(10),IOTime,120),OrderNo,C_Name,PCId,IOType');
          sql.Add(',Grade,CustName,C_Width,C_GramWeight,CIMachNo,Workshop,BatchNo,IOPosition,C_Color,C_Pattern,StkType,Price');
          Sql.Add(',C_Code,Saleser,dbo.getNum(C_ColorNo),C_ColorNo,ConNo,BuyConNo,FOrdNo');
//          showmessage(sql.text);
          Open;
        end;
        SCreateCDS(ADOQueryMain, CDS_2);
        SInitCDSData(ADOQueryMain, CDS_2);
        TV2.DataController.Filter.Clear;

      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(' select top ' + inttostr(RecordsNumber) + ' * ');
          sql.Add(' from  ');
          sql.Add('(select  rownumber=cast((row_number() over(order by AA.IOTime desc)) as int)');
          sql.Add(',COUNT(1) OVER() AS TotalCount,dbo.getNum(AA.C_ColorNo) SHNO');
          sql.Add(', kl= DATEDIFF(dd,IOTime, GETDATE()),* from V_Trade_Cloth_Stk   AA where 1=1 ');
          sql.Add(' and StkType=''' + trim(canshu5) + ''' ');
          if trim(FWorkshop) <> '' then
            sql.Add(' and Workshop=''' + trim(FWorkshop) + ''' ');
          if canshu2 = '死货' then
            sql.Add(' and ISDie=1 ');
          if canshu2 = '次品' then
            sql.Add(' and Grade=''次品'' ');

          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));
        TV1.DataController.Filter.Clear;
      end;
  end;

end;

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

procedure TfrmTradeClothStkListNew.CDS_1BeforeOpen(DataSet: TDataSet);
begin
  lstPacketId.Clear;
end;

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

procedure TfrmTradeClothStkListNew.PriceKeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9', #8, '.']) then
    Key := #0

end;

procedure TfrmTradeClothStkListNew.TBCloseClick(Sender: TObject);
begin

  Close;
end;

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

  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '贸易成品仓库');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '贸易成品仓库');
  CurrentPage := 1;
  RecordsNumber := 500;
  canshu1 := Trim(Self.fParameters1);

  canshu2 := Trim(Self.fParameters2);

  canshu4 := Trim(Self.fParameters4);

  canshu5 := Trim(Self.fParameters5);
  if Canshu5 = '订单' then
  begin
    Label15.Caption := '库龄超90天';
    Label16.Caption := '库龄超12月';
    Label17.Caption := '库龄超18月';
  end
  else if Canshu5 = '库存' then
  begin
    Label15.Caption := '库龄超2年';
    Label16.Caption := '库龄超3年';
    Label17.Caption := '库龄超5年';
  end;
  SetStatus();
  InitPage();
end;

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

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

procedure TfrmTradeClothStkListNew.PM11Click(Sender: TObject);
begin
  SelOKNo(CDS_1, false);
end;

procedure TfrmTradeClothStkListNew.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('kl').Index] > 90 then
    ACanvas.Brush.Color := $B3DEF5;
end;

procedure TfrmTradeClothStkListNew.Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
begin
  lstPacketId.Clear;      //重新打开数据集前清空lstPacketId
end;

procedure TfrmTradeClothStkListNew.Tv1DataControllerSummaryFooterSummaryItemsSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
var
  BaoID: string;
begin
  //对 VC_PacketId 列的合计进行处理
  if TcxGridDBTableSummaryItem(Arguments.SummaryItem).Column = VC_PacketId then
  begin
    BaoID := vartostr(ASender.DataController.Values[Arguments.RecordIndex, VC_PacketId.Index]);
    if BaoID = '' then
      Exit;
    if BaoID = null then
      Exit;
    if lstPacketId.IndexOf(BaoID) = -1 then
    begin
      lstPacketId.Add(BaoID);   //记下之前未出现过的包编号
    end;
  end;
end;

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

procedure TfrmTradeClothStkListNew.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 TfrmTradeClothStkListNew.TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if canshu5 = '订单' then
  begin
    if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('kl').Index] > 90 then
      ACanvas.Brush.Color := $B3DEF5;
    if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('kl').Index] > 365 then
      ACanvas.Brush.Color := $00A5FF;
    if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('kl').Index] > 548 then
      ACanvas.Brush.Color := $0000FF;
  end
  else if canshu5 = '库存' then
  begin
    if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('kl').Index] > 730 then
      ACanvas.Brush.Color := $B3DEF5;
    if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('kl').Index] > 1095 then
      ACanvas.Brush.Color := $00A5FF;
    if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('kl').Index] > 1825 then
      ACanvas.Brush.Color := $0000FF;
  end
end;

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

procedure TfrmTradeClothStkListNew.BitBtn1Click(Sender: TObject);
begin
  Panel1.Visible := False;

end;

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

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

procedure TfrmTradeClothStkListNew.JYTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitPage();
end;

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

end;

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

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

procedure TfrmTradeClothStkListNew.ToolButton10Click(Sender: TObject);
begin
   try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeClothStk';
      FFiltration1 := Trim(self.CDS_1.fieldbyname('OrderNo').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmTradeClothStkListNew.ToolButton11Click(Sender: TObject);
var
  fPrintFile, FLabVolume: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.FieldByName('SSel').AsBoolean = True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update Trade_Cloth_Inspect Set PrtCount=PrtCount+1,PrtDate=getdate(),Prter=''' + Trim(DName) + '''');
          sql.Add(' where CIID=''' + Trim(CDS_1.fieldbyname('CIID').AsString) + '''');
          ExecSQL;
        end;

        with ADOQueryPrint do
        begin
          Close;
          SQL.Clear;
          sql.Add(' EXEC P_Trade_Insp_Prt_Lab ');
          SQL.Add(' @CIID=''' + Trim(CDS_1.fieldbyname('CIID').AsString) + '''');
          Open;
        end;
        if ADOQueryPrint.IsEmpty then
        begin
          application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
          exit;
        end;

        if Trim(ADOQueryPrint.fieldbyname('Grade').AsString) = '正品' then
          FLabVolume := Trim(ADOQueryPrint.fieldbyname('LabVolume').AsString)
        else
          FLabVolume := '次品标签';

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

        if FileExists(fPrintFile) then
        begin
          RM1.LoadFromFile(fPrintFile);
//          if Trim(ADOQueryPrint.fieldbyname('LabVolume').AsString) <> '次品标签' then
//            Rm1.DefaultCopies := 2
//          else
          Rm1.DefaultCopies := 1;
          RM1.PrintReport;
        end
        else
        begin
          Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
        end;
      end;
      Next;
    end;
  end;
  CDS_1.EnableControls;

end;

procedure TfrmTradeClothStkListNew.ToolButton1Click(Sender: TObject);
begin
  try
    frmTradeClothSplit := TfrmTradeClothSplit.Create(Application);
    with frmTradeClothSplit do
    begin
      FStkID := trim(self.CDS_1.FieldByName('StkID').AsString);
      if ShowModal = 1 then
      begin
        InitGrid('');
      end;
    end;
  finally
    frmTradeClothSplit.Free;
  end;
end;

procedure TfrmTradeClothStkListNew.ToolButton2Click(Sender: TObject);
var
  WSql: string;
begin
  if cxPageControl1.ActivePageIndex <> 1 then
    Exit;
  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 + ',' + Trim(CDS_1.fieldbyname('CIID').AsString);
        end
        else
        begin
          WSql := Trim(CDS_1.fieldbyname('CIID').AsString);
        end;
      end;
      Next;
    end;
  end;
//  ShowMessage(WSql);
  CDS_1.Locate('SSel', True, []);
  CDS_1.EnableControls;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeClothStk';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin

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

procedure TfrmTradeClothStkListNew.ToolButton3Click(Sender: TObject);
var
  rtvalues: TArray<string>;
begin
  if cxPageControl1.ActivePageIndex <> 1 then
    Exit;

  if CDS_1.IsEmpty then
    Exit;
  rtvalues := SelCDSKey(CDS_1, ['CIID']);

  if rtvalues[0] = '' then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  if Application.MessageBox('确定要更改数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_Cloth_Stock SET ISDie=case when ISDie=1 then 0 else 1 end  ');
      sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(rtvalues[0]) + ','','') X where Trade_Cloth_Stock.CIID=X.RTValue )  ');

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('死货设置')));
      sql.Add(',' + quotedstr(trim('条码号：' + trim(rtvalues[0]))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('死货设置成功！', '提示信息');

  except
    application.MessageBox('死货设置失败！', '提示信息', 0);
  end;
end;

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

procedure TfrmTradeClothStkListNew.ToolButton5Click(Sender: TObject);
var
  FIOPosition: string;
  FReal: Double;
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;
  if Trim(IOPosition.Text) = '' then
  begin
    Application.MessageBox('库位不能为空!', '提示', 0);
    Exit;
  end;

  with CDS_1 do
  begin
    first;
    while not Eof do
    begin
      if CDS_1.FieldByName('Ssel').AsBoolean = true then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update Trade_Cloth_IO Set IOPosition=''' + Trim(IOPosition.Text) + '''');
          sql.Add(' where StKID=''' + Trim(CDS_1.fieldbyname('StkID').AsString) + '''');
//          showmessage(SQL.text);
          ExecSQL;
        end;
        with CDS_1 do
        begin
          Edit;
          FieldByName('IOPosition').Value := IOPosition.Text;
          Post;
        end;
      end;
      next;
    end;
  end;
//  CheckBox1.Checked:=False;
  with CDS_1 do
  begin
    DisableControls;
    first;
    while not Eof do
    begin
      edit;
      fieldbyname('Ssel').AsBoolean := False;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
end;

procedure TfrmTradeClothStkListNew.ToolButton6Click(Sender: TObject);
//var
//  FStkType: string;
//  FReal: Double;
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;
  if Trim(StkType.Text) = '' then
  begin
    Application.MessageBox('库存类型不能为空!', '提示', 0);
    Exit;
  end;

  with CDS_1 do
  begin
    first;
    while not Eof do
    begin
      if CDS_1.FieldByName('Ssel').AsBoolean = true then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update Trade_Cloth_IO Set StkType=''' + Trim(StkType.Text) + '''');
          sql.Add(' where StKID=''' + Trim(CDS_1.fieldbyname('StkID').AsString) + '''');
//          showmessage(SQL.text);
          ExecSQL;
        end;
        with CDS_1 do
        begin
          Edit;
          FieldByName('StkType').Value := StkType.Text;
          Post;
        end;
      end;
      next;
    end;
  end;
//  CheckBox1.Checked:=False;
  with CDS_1 do
  begin
    DisableControls;
    first;
    while not Eof do
    begin
      edit;
      fieldbyname('Ssel').AsBoolean := False;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
end;

procedure TfrmTradeClothStkListNew.ToolButton7Click(Sender: TObject);
var
  MPrice, MAmount: string;
  FPrice, FAmount, FM, FY, FZ: Double;
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据！', '提示', 0);
    Exit;
  end;
  if Trim(Price.Text) = '' then
  begin
    Application.MessageBox('单价不能为空!', '提示', 0);
    Exit;
  end;

  FPrice := StrToFloat(Trim(Price.Text));

  with CDS_1 do
  begin
    first;
    while not Eof do
    begin
      if CDS_1.FieldByName('Ssel').AsBoolean = true then
      begin
        FM := CDS_1.FieldByName('Meter').AsFloat;
        FY := CDS_1.FieldByName('Yardage').AsFloat;
        FZ := CDS_1.FieldByName('NetWeight').AsFloat;

        if (FM <> 0) and (FZ <> 0) then
        begin
          if JSunit.Text = '' then
          begin
            Application.MessageBox('请先选择计价单位!', '提示', 0);
            Exit;
          end
          else
          begin
            if JSUnit.Text = 'Kg' then
              FAmount := RoundFloat(FZ * FPrice, 2)
            else if JSUnit.Text = 'M' then
              FAmount := RoundFloat(FM * FPrice, 2);
          end;
        end
        else if FM = 0 then
        begin
          FAmount := RoundFloat(FZ * FPrice, 2);
        end
        else if FZ = 0 then
        begin
          FAmount := RoundFloat(FM * FPrice, 2);
        end;

//        if CDS_1.FieldByName('OrdUnit').AsString = 'M' then
//        begin
//          FAmount := RoundFloat(FM * FPrice, 2);
//        end
//        else if CDS_1.FieldByName('OrdUnit').AsString = 'Y' then
//        begin
//          FAmount := RoundFloat(FY * FPrice, 2);
//        end
//        else if CDS_1.FieldByName('OrdUnit').AsString = 'Kg' then
//        begin
//          FAmount := RoundFloat(FZ * FPrice, 2);
//        end;

        MAmount := FloatToStr(FAmount);

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update Trade_Cloth_IO Set Price=''' + Trim(Price.Text) + '''');
          sql.Add(',Amount=''' + Trim(MAmount) + '''');
          sql.Add(' where StKID=''' + Trim(CDS_1.fieldbyname('StkID').AsString) + '''');
//          showmessage(SQL.text);
          ExecSQL;
        end;
        with CDS_1 do
        begin
          Edit;
          FieldByName('Price').Value := FPrice;
          FieldByName('Amount').Value := FAmount;
          Post;
        end;
      end;
      next;
    end;
  end;
//  CheckBox1.Checked:=False;
  with CDS_1 do
  begin
    DisableControls;
    first;
    while not Eof do
    begin
      edit;
      fieldbyname('Ssel').AsBoolean := False;
      post;
      next;
    end;
    first;
    EnableControls;
  end;
end;

procedure TfrmTradeClothStkListNew.ToolButton8Click(Sender: TObject);
begin
  Panel1.Visible := True;

end;

procedure TfrmTradeClothStkListNew.ToolButton9Click(Sender: TObject);
begin
  try
    frmTradeClothSplitWeight := TfrmTradeClothSplitWeight.Create(Application);
    with frmTradeClothSplitWeight do
    begin
      FStkID := trim(self.CDS_1.FieldByName('StkID').AsString);
      if ShowModal = 1 then
      begin
        InitGrid('');
      end;
    end;
  finally
    frmTradeClothSplitWeight.Free;
  end;

end;

end.

