unit U_KCList;

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, 
   dxDateRanges, dxBarBuiltInMenu, U_BaseList, cxPC,
  System.StrUtils, cxContainer, dxCore, cxDateUtils, cxMaskEdit, 
      
    
     
     
     
    
    
    
    
    
     
     
     
    
    
     dxScrollbarAnnotations,
  cxButtonEdit, cxButtons, cxProgressBar, cxImage, cxDBEdit, IdFTP, UrlMon,
  ShellAPI;

type
  TfrmKCList = 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;
    PM11: TMenuItem;
    ToolButton1: TToolButton;
    N1: TMenuItem;
    N2: TMenuItem;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    Pnl_F1: TPanel;
    Label2: TLabel;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    Pnl_F2: TPanel;
    Label1: TLabel;
    DateTimePicker3: TDateTimePicker;
    DateTimePicker4: TDateTimePicker;
    GPM_2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    PM_2: TPopupMenu;
    MenuItem3: TMenuItem;
    BegDate2: TcxDateEdit;
    EndDate2: TcxDateEdit;
    chk_F2: TcxCheckBox;
    chk_F1: TcxCheckBox;
    BegDate: TcxDateEdit;
    EndDate: TcxDateEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label4: TLabel;
    Label6: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Y_name: TcxTextEdit;
    FromCoName: TcxTextEdit;
    StkCoName: TcxTextEdit;
    Y_Spec: TcxComboBox;
    cxTextEdit1: TcxTextEdit;
    Yarn_Name: TcxTextEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn1: 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;
    cxGridDBColumn40: TcxGridDBColumn;
    cxGridDBColumn41: TcxGridDBColumn;
    cxGridDBColumn42: TcxGridDBColumn;
    cxGridDBColumn43: TcxGridDBColumn;
    cxGridDBColumn44: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Label3: TLabel;
    Label5: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    cxTextEdit2: TcxTextEdit;
    cxTextEdit3: TcxTextEdit;
    cxTextEdit5: TcxTextEdit;
    cxComboBox1: TcxComboBox;
    cxTextEdit8: TcxTextEdit;
    cxTextEdit9: TcxTextEdit;
    cxTabSheet3: TcxTabSheet;
    Panel1: TPanel;
    Label21: TLabel;
    Label22: TLabel;
    Button1: TButton;
    Button2: TButton;
    ComboBox1: TComboBox;
    Pnl_F3: TPanel;
    Label23: TLabel;
    Label24: TLabel;
    Label25: TLabel;
    Label26: TLabel;
    Label28: TLabel;
    Label32: TLabel;
    Label33: TLabel;
    DateTimePicker1: TDateTimePicker;
    DateTimePicker2: TDateTimePicker;
    cxCheckBox1: TcxCheckBox;
    cxDateEdit1: TcxDateEdit;
    cxDateEdit2: TcxDateEdit;
    cxTextEdit10: TcxTextEdit;
    cxTextEdit11: TcxTextEdit;
    cxTextEdit13: TcxTextEdit;
    cxComboBox2: TcxComboBox;
    cxTextEdit16: TcxTextEdit;
    cxTextEdit17: TcxTextEdit;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn45: TcxGridDBColumn;
    cxGridDBColumn46: TcxGridDBColumn;
    cxGridDBColumn47: TcxGridDBColumn;
    cxGridDBColumn48: TcxGridDBColumn;
    cxGridDBColumn49: TcxGridDBColumn;
    cxGridDBColumn50: TcxGridDBColumn;
    cxGridDBColumn51: TcxGridDBColumn;
    cxGridDBColumn52: TcxGridDBColumn;
    cxGridDBColumn53: TcxGridDBColumn;
    cxGridDBColumn54: TcxGridDBColumn;
    cxGridDBColumn55: TcxGridDBColumn;
    cxGridDBColumn56: TcxGridDBColumn;
    cxGridDBColumn57: TcxGridDBColumn;
    cxGridDBColumn58: TcxGridDBColumn;
    cxGridDBColumn59: TcxGridDBColumn;
    cxGridDBColumn60: TcxGridDBColumn;
    cxGridDBColumn61: TcxGridDBColumn;
    cxGridDBColumn62: TcxGridDBColumn;
    cxGridDBColumn63: TcxGridDBColumn;
    cxGridDBColumn64: TcxGridDBColumn;
    cxGridDBColumn65: TcxGridDBColumn;
    cxGridDBColumn66: TcxGridDBColumn;
    cxGridDBColumn67: TcxGridDBColumn;
    cxGridDBColumn68: TcxGridDBColumn;
    cxGridDBColumn69: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    DS_3: TDataSource;
    CDS_3: TClientDataSet;
    Tv3Column1: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Label34: TLabel;
    Label35: TLabel;
    BYIOID: TcxTextEdit;
    FromPurNo: TcxTextEdit;
    Label36: TLabel;
    StkPosition: TcxTextEdit;
    Label37: TLabel;
    Label38: TLabel;
    cxTextEdit18: TcxTextEdit;
    cxTextEdit19: TcxTextEdit;
    Tv2Column1: TcxGridDBColumn;
    Label8: TLabel;
    BanID: TcxTextEdit;
    Label11: TLabel;
    cxTextEdit4: TcxTextEdit;
    Label13: TLabel;
    cxTextEdit6: TcxTextEdit;
    Label14: TLabel;
    MXID: TcxTextEdit;
    GPM_3: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    Panel2: TPanel;
    Label15: TLabel;
    YMXID: TcxTextEdit;
    Label18: TLabel;
    BaoQty1: TcxTextEdit;
    Label27: TLabel;
    Label29: TLabel;
    BaoQty2: TcxTextEdit;
    BaoQty3: TcxTextEdit;
    cxButton1: TcxButton;
    cxButton2: TcxButton;
    Tv2Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv3Column4: TcxGridDBColumn;
    Tv3Column5: TcxGridDBColumn;
    Tv3Column6: TcxGridDBColumn;
    ToolButton5: TToolButton;
    Panel3: TPanel;
    Label30: TLabel;
    Label39: TLabel;
    Label40: TLabel;
    YMXID2: TcxTextEdit;
    BaoQty11: TcxTextEdit;
    BaoQty12: TcxTextEdit;
    cxButton3: TcxButton;
    cxButton4: TcxButton;
    Label41: TLabel;
    StkID2: TcxTextEdit;
    Label42: TLabel;
    StkID: TcxTextEdit;
    Tv3Column7: TcxGridDBColumn;
    DSImage: TDataSource;
    ADOQueryImage: TADOQuery;
    Tv3Column8: TcxGridDBColumn;
    Picture4: TcxDBImage;
    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 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 ToolButton1Click(Sender: TObject);
    procedure PM11Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TV2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure MenuItem3Click(Sender: TObject);
    procedure TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure FromCoNamePropertiesChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure BaoQty2PropertiesChange(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure cxButton3Click(Sender: TObject);
    procedure cxButton4Click(Sender: TObject);
    procedure Tv3FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure Picture4DblClick(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 TfrmKCList.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;
    2:
      begin
        InitGrid(SGetHintFilters(Pnl_F3, 1, 2));
      end;
  end;
end;

procedure TfrmKCList.SetStatus();
begin
  ToolButton3.Enabled := false;
  ToolButton5.Enabled := false;

  case cxPageControl1.ActivePageIndex of
    0:
      begin

      end;
    1:
      begin

      end;
    2:
      begin
        ToolButton3.Enabled := true;
        ToolButton5.Enabled := true;
      end;
  end;

end;

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

procedure TfrmKCList.FormCreate(Sender: TObject);
begin
  inherited;
  lstCust := TStringList.Create;
  EndDate.Date := SGetServerDateTime(ADOQueryTemp);
  BegDate.Date := EndDate.Date - 20;

  BegDate2.Date := BegDate.Date;
  EndDate2.Date := EndDate.Date;

  FWorkshop := trim(Self.fParameters3);

  Picture4.Left := ClientWidth - 10;  // 10 是距离右边的间距
  Picture4.Top := ClientHeight - 100; // 10 是距离底部的间距


end;

procedure TfrmKCList.InitGrid(WSql: string);
begin

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

  case cxPageControl1.ActivePageIndex of
    0:
      begin
        with ADOQueryMain do
        begin
          Close;
          sql.Clear;
          sql.Add(' select BS= ceiling(1.00*StkPiece/BBBS) ');
          sql.Add(' ,*,GS=(select Count(*) from BS_Yarn_IO_MX X where A.BYIOID =X.BYIOID and X.IOFlag =''已入库'')');
          sql.Add(' from BS_Yarn_IO A    ');
          SQL.Add(' where A.IOFlag=''入库'' and  isnull(StkQty,0)>0   ');
          sql.Add(' and isnull(stkName,'''')=''' + Trim('纱线') + '''');
          sql.Add(WSql);
//          ShowMessage(SQL.Text);
          Open;
        end;
        SCreateCDS(ADOQueryMain, CDS_2);
        SInitCDSData(ADOQueryMain, CDS_2);
      end;
    1:
      begin
        with ADOQueryMain do
        begin
          Close;
          sql.Clear;
          sql.Add(' select AA.* from (select B.BanID,B.BanNo,B.StkPosition,B.GS,A.*');
          sql.Add(' from BS_Yarn_IO A    ');
          sql.Add(' inner join BS_Yarn_IO_Ban B on A.BYIOID =B.BYIOID  ');
          SQL.Add(' where A.IOFlag=''入库'' and  isnull(A.StkQty,0)>0  and B.IOFlag =''已入库'' ');
          sql.Add(' and isnull(A.stkName,'''')=''' + Trim('纱线') + '''');
          sql.Add(' )AA  where 1=1');
          sql.Add(WSql);
          sql.Add('Order by BYIOID,BanNo  ');
//          ShowMessage(SQL.Text);
          Open;
        end;
        SCreateCDS(ADOQueryMain, CDS_1);
        SInitCDSData(ADOQueryMain, CDS_1);
      end;

    2:
      begin

        with ADOQueryMain do
        begin
          Close;
          sql.Clear;
          sql.Add(' select KeShu=isnull(AA.KeShu,0),KeKG=isnull(AA.KeKG,0),AA.* from (select KeShu2=isnull(B.KeShu,0),KeKG2=isnull(B.KeKG,0),B.MXID,B.BaoNo,B.StkPosition,B.BanNo,B.BanID,B.BaoQty,B.WeighQty,A.*');
          sql.Add(',IsTP=cast((case when (select COUNT(T.WBID) from TP_File T where  T.WBID=B.MXID)=0 then 0 else 1 end) as bit )  ');
          sql.Add(' from BS_Yarn_IO A    ');
          sql.Add(' inner join BS_Yarn_IO_MX B on A.BYIOID =B.BYIOID  ');
          SQL.Add(' where A.IOFlag=''入库'' and  isnull(A.StkQty,0)>0  and B.IOFlag =''已入库'' ');
          sql.Add(' and isnull(A.stkName,'''')=''' + Trim('纱线') + '''');
          sql.Add(' )AA  where 1=1');
          sql.Add(WSql);
          sql.Add(' Order by BYIOID,BanNo,BaoNo  ');
//          ShowMessage(SQL.Text);
          Open;
        end;
        SCreateCDS(ADOQueryMain, CDS_3);
        SInitCDSData(ADOQueryMain, CDS_3);
      end;
  end;

end;

procedure TfrmKCList.TBRafreshClick(Sender: TObject);
begin
  CurrentPage := 1;
  InitPage();
end;

procedure TfrmKCList.CDS_1BeforeOpen(DataSet: TDataSet);
begin

  lstCust.Clear;
end;

procedure TfrmKCList.cxButton1Click(Sender: TObject);
var
  MaxID, MMXID: string;
  MaxNo: Integer;
begin
  if strtofloatdef(BaoQty3.text, 0) <= 0 then
  begin
    Application.MessageBox('拆分重量填写不正确', '提示', 0);
    Exit;
  end;

  if strtofloatdef(BaoQty2.text, 0) <= 0 then
  begin
    Application.MessageBox('拆分重量填写不正确', '提示', 0);
    Exit;
  end;

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

  if GetLSNo(ADOQueryCmd, MaxID, 'bao', 'BS_Yarn_IO_MX', 4, 1) = False then
    raise Exception.Create('取最大号失败！');

  MaxID := MaxID + '-1';

  with ADOQueryCmd do
  begin
    Close;
    Sql.Clear;
    Sql.Add('select Max(BaoNO)+1 AS MaxNo from BS_Yarn_IO_MX');
    Sql.Add('where BYIOID =''' + Trim(CDS_3.fieldbyname('BYIOID').AsString) + '''');
    Open;
  end;

  MaxNo := ADOQueryCmd.fieldbyname('MaxNo').AsInteger;

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('update BS_Yarn_IO_MX set BaoQty =' + trim(BaoQty3.text) + '');
      Sql.Add('where MXID =''' + trim(YMXID.text) + '''');

      Sql.Add('update BS_Yarn_IO set StkPiece =StkPiece + 1');
      Sql.Add('where StkID =''' + Trim(CDS_3.fieldbyname('StkID').AsString) + '''');

      sql.Add('insert into BS_Yarn_IO_MX(MXID,BaoQty,BaoNo,StkPosition,BanNo,BanID,IOFlag,FromMXID,BYIOID,KeShu,KeKG) values( ');
      sql.Add(quotedstr(trim(MaxID)));
      sql.Add(',' + trim(BaoQty2.text) + '');
      sql.Add(',' + inttostr(MaxNo) + '');

      sql.Add(',''' + Trim(CDS_3.fieldbyname('StkPosition').AsString) + '''');
      sql.Add(',' + Trim(CDS_3.fieldbyname('BanNo').AsString) + '');
      sql.Add(',''' + Trim(CDS_3.fieldbyname('BanID').AsString) + '''');
      sql.Add(',''' + '已入库' + '''');
      sql.Add(',''' + trim(YMXID.text) + '''');
      sql.Add(',''' + Trim(CDS_3.fieldbyname('BYIOID').AsString) + '''');
      sql.Add(',' + Trim(CDS_3.fieldbyname('KeShu').AsString) + '');
      sql.Add(',' + Trim(CDS_3.fieldbyname('KeKG').AsString) + '');
      sql.Add(')');

//      ShowMessage(sql.Text);
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;

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

  MMXID := trim(YMXID.text) + ',' + MaxID;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'BaoMX';
      FFiltration1 := MMXID;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

  cxGrid3.Enabled := true;
  Panel2.Visible := False;

  StkID.text := '';
  YMXID.text := '';
  BaoQty1.text := '';
  BaoQty2.text := '';
  BaoQty3.text := '';
end;

procedure TfrmKCList.cxButton2Click(Sender: TObject);
begin
  cxGrid3.Enabled := true;
  Panel2.Visible := False;

  StkID.text := '';
  YMXID.text := '';
  BaoQty1.text := '';
  BaoQty2.text := '';
  BaoQty3.text := '';
end;

procedure TfrmKCList.cxButton3Click(Sender: TObject);
var
  MaxID, MMXID: string;
  MaxNo: Integer;
begin
  if strtofloatdef(BaoQty12.text, 0) <= 0 then
  begin
    Application.MessageBox('修改后重量填写不正确', '提示', 0);
    Exit;
  end;

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

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('update BS_Yarn_IO_MX set BaoQty =' + trim(BaoQty12.text) + '');
      Sql.Add('where MXID =''' + trim(YMXID2.text) + '''');

      sql.Add('Update BS_Yarn_IO Set StkPiece=(select count(*) from BS_Yarn_IO_MX X where X.BYIOID=A.BYIOID and X.IOFlag =''已入库'' and isnull(X.BaoQty,0) > 0)');
      sql.Add(',STKQty=(select Sum(BaoQty) from BS_Yarn_IO_MX X where X.BYIOID=A.BYIOID and X.IOFlag =''已入库'' and isnull(X.BaoQty,0) > 0 )');
      sql.Add('from BS_Yarn_IO A');
      sql.Add('where BYIOID=''' + Trim(StkID2.text) + '''');



//      sql.Add('Update BS_Yarn_IO Set StkPiece=(select Sum(isnull(Piece,0)*IOQtyFlag) from BS_Yarn_IO A where A.STKID=BS_Yarn_IO.BYIOID)');
//      sql.Add(',STKQty=(select Sum(isnull(Qty,0)*IOQtyFlag) from BS_Yarn_IO A where A.STKID=BS_Yarn_IO.BYIOID )');
//      sql.Add('where BYIOID=''' + Trim(StkID2.text) + '''');

//      ShowMessage(sql.Text);
      ExecSQL;
    end;

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

  cxGrid3.Enabled := true;
  Panel3.Visible := False;

  StkID2.text := '';
  YMXID2.text := '';
  BaoQty11.text := '';
  BaoQty12.text := '';

end;

procedure TfrmKCList.cxButton4Click(Sender: TObject);
begin
  cxGrid3.Enabled := true;
  Panel3.Visible := False;

  StkID2.text := '';
  YMXID2.text := '';
  BaoQty11.text := '';
  BaoQty12.text := '';
end;

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

procedure TfrmKCList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmKCList.FormShow(Sender: TObject);
begin
  inherited;
  canshu1 := self.fParameters1;
  canshu2 := self.fParameters2;
  FWorkshop := self.fParameters3;
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '梭织坯布仓库');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '梭织坯布仓库');
  CurrentPage := 1;
  RecordsNumber := 500;

  SetStatus();
  InitPage();
end;

procedure TfrmKCList.FromCoNamePropertiesChange(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmKCList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel(Self.Caption, cxGrid1);
end;

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

procedure TfrmKCList.Picture4DblClick(Sender: TObject);
var
  IdFTP1: TIdFTP;
  FPath, FFName, fPdfFilePath, fPdfFilePath1, FDHID: string;
  FInt: integer;
begin

  if not DirectoryExists(pchar('D:\Right1209')) then
    CreateDirectory(pchar('D:\Right1209'), nil);

  fPdfFilePath := 'D:\Right1209\' + FDHID;
  if not DirectoryExists(pchar(fPdfFilePath)) then
    CreateDirectory(pchar(fPdfFilePath), nil);
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where isnull(WBID,'''')<>'''' and WBID=' + quotedstr(Trim(CDS_3.FieldByName('MXID').AsString)));
    open;
  end;
  ADOQueryTemp.First;
  while not ADOQueryTemp.Eof do
  begin
    if Trim(ADOQueryTemp.FieldByName('URL').AsString) <> '' then
    begin

      fPdfFilePath1 := fPdfFilePath + '\' + trim(Trim(ADOQueryTemp.FieldByName('FileName').AsString));
      UrlDownloadToFile(nil, PChar(Trim(ADOQueryTemp.FieldByName('URL').AsString)), PChar(fPdfFilePath1), 0, nil);
      ShellExecute(Handle, 'open', PChar(fPdfFilePath + '\' + trim(ADOQueryTemp.FieldByName('FileName').AsString)), '', '', SW_SHOWNORMAL);
    end;
    ADOQueryTemp.Next;
  end;
//  ShellExecute(Handle, 'open', PChar(fPdfFilePath + '\' + trim(FileName)), '', '', SW_SHOWNORMAL);
end;

procedure TfrmKCList.PM11Click(Sender: TObject);
begin
  SelOKNo(CDS_1, true);

end;

procedure TfrmKCList.TBRKCXClick(Sender: TObject);
var
  MCIIDS, MIOIDS, MSTKIDS, MPCIDS: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  MCIIDS := '';
  MIOIDS := '';
  MSTKIDS := '';
  MPCIDS := '';
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.fieldbyname('SSel').AsBoolean then
      begin
        MCIIDS := MCIIDS + (Trim(CDS_1.fieldbyname('CIID').AsString)) + ',';
        MIOIDS := MIOIDS + (Trim(CDS_1.fieldbyname('IOID').AsString)) + ',';
        MSTKIDS := MSTKIDS + (Trim(CDS_1.fieldbyname('STKID').AsString)) + ',';
        MPCIDS := MPCIDS + (Trim(CDS_1.fieldbyname('PCID').AsString)) + ',';
      end;
      Next;
    end;
  end;
  MCIIDS := leftBstr(trim(MCIIDS), length(MCIIDS) - 1);
  MIOIDS := leftBstr(trim(MIOIDS), length(MIOIDS) - 1);
  MSTKIDS := leftBstr(trim(MSTKIDS), length(MSTKIDS) - 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 Tat_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('exec P_Tat_Cloth_ReIn ');
      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;

//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('exec P_Tat_Card_PSC ');
//      execsql;
//    end;
    InitPage();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmKCList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin

  cxPageControl1.ActivePageIndex := 2;
  SetStatus();
  InitGrid(SGetCDSFilters(Tv1, CDS_1));
end;

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

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

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

procedure TfrmKCList.TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
//  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('sh').Index] <> null then
//    if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('sh').Index] > 8 then
//      ACanvas.Brush.Color := $B3DEF5;
end;

procedure TfrmKCList.Tv3FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID =' + quotedstr(trim(CDS_3.fieldbyname('MXID').AsString)));
    open;
  end;
end;

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

procedure TfrmKCList.BaoQty2PropertiesChange(Sender: TObject);
begin
  BaoQty3.text := FloatToStr(strtofloatdef(BaoQty1.text, 0) - strtofloatdef(BaoQty2.text, 0));
end;

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

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

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

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

procedure TfrmKCList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_1, false);
end;

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

procedure TfrmKCList.ToolButton1Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;

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

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

procedure TfrmKCList.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 := 'TatClothIn2';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin

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

procedure TfrmKCList.ToolButton3Click(Sender: TObject);
begin
  cxGrid3.Enabled := False;
  Panel2.Visible := true;

  StkID.text := Trim(CDS_3.fieldbyname('StkID').AsString);
  YMXID.text := Trim(CDS_3.fieldbyname('MXID').AsString);
  BaoQty1.text := Trim(CDS_3.fieldbyname('BaoQty').AsString);
end;

procedure TfrmKCList.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv11', Tv1, '梭织坯布仓库');
  WriteCxGrid(trim(self.Caption) + 'Tv22', Tv2, '梭织坯布仓库');
  WriteCxGrid(trim(self.Caption) + 'Tv33', Tv3, '梭织坯布仓库');
end;

procedure TfrmKCList.ToolButton5Click(Sender: TObject);
begin
  cxGrid3.Enabled := False;
  Panel3.Visible := true;

  StkID2.text := Trim(CDS_3.fieldbyname('StkID').AsString);
  YMXID2.text := Trim(CDS_3.fieldbyname('MXID').AsString);
  BaoQty11.text := Trim(CDS_3.fieldbyname('BaoQty').AsString);
end;

end.

