unit U_PBOuGLtListsel;

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, RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System,
  RM_GridReport, cxCheckBox, Menus, MovePanel, BtnEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmPBKCsel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    CDS_Print: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    TBCKCX: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    CDS_CX: TClientDataSet;
    CDS_DH: TClientDataSet;
    ADOQueryDH: TADOQuery;
    ADOQueryPrint: TADOQuery;
    CDS_Juan: TClientDataSet;
    CDS_JuanPRT: TClientDataSet;
    RMDBMX: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    cxgrdbclmnv1Column14: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label1: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    C_CodeName: TEdit;
    C_Code: TEdit;
    ConNo: TEdit;
    MJID: TEdit;
    khname: TEdit;
    CarNo: TEdit;
    APBatchNo: TEdit;
    Label2: TLabel;
    Edit1: TEdit;
    ToolButton1: TToolButton;
    lbl1: TLabel;
    CPType: TComboBox;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBCKCXClick(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure CheJianChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);

  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
    canshu1: string;
  end;

var
  frmPBKCsel: TfrmPBKCsel;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmPBKCsel.FormDestroy(Sender: TObject);
begin
//  frmPBOuGLtListSEL := nil;
end;

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

procedure TfrmPBKCsel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('exec P_View_PBCPKCMX :WSQl');
      Parameters.ParamByName('WSQl').Value := 'AND CONNO=' + QuotedStr(ConNo.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmPBKCsel.TBRafreshClick(Sender: TObject);
begin

  InitGrid();
end;

procedure TfrmPBKCsel.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmPBKCsel.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Self.Caption + '1', Tv1, '坯布仓库');
  Close;
end;

procedure TfrmPBKCsel.FormShow(Sender: TObject);
var
  fsj: string;
begin

  ReadCxGrid(Self.Caption, Tv1, '坯布仓库');
  ReadCxGrid(Self.Caption + '1', Tv1, '坯布仓库');
  if trim(Self.canshu1) = '查询' then
  begin
    TBCKCX.Visible := false;
    ToolButton1.Visible := False;
  end;
  InitGrid();
end;

procedure TfrmPBKCsel.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  SelExportData(Tv1, ADOQueryMain, '');
end;

procedure TfrmPBKCsel.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmPBKCsel.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBKCsel.orderNoChange(Sender: TObject);
begin
  if Length(ConNo.Text) < 4 then
    Exit;
  TBFind.Click;
end;

procedure TfrmPBKCsel.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmPBKCsel.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmPBKCsel.TBCKCXClick(Sender: TObject);
var
  FFMainId, FPrice, FHZ, FCKOrderNo: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;

  if Application.MessageBox('确定要报废出库吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while CDS_Main.Locate('SSel', True, []) = True do
      begin
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPDATE CK_PBCP_CR Set  ');
            sql.add(' CRTime=getdate()  ');
            sql.add(',CRType=' + quotedstr(Trim('报废出库')));

            sql.add(',CRFlag=''出库''  ');
            sql.add(',CKOrdNo=' + Quotedstr(Edit1.Text));
            sql.Add(' where CK_PBCP_CR.MJID=' + Quotedstr(Trim(CDS_Main.FIELDBYNAME('MJID').AsString)));
            sql.Add('and CK_PBCP_CR.CRFlag=''待出库''  ');

            sql.Add('UPDATE CK_PBCP_KC Set  ');
            sql.add(' KCValid=''N'',KCKgQty=0,KCQty=0  ');
            sql.Add(' where CK_PBCP_KC.MJID=' + Quotedstr(Trim(CDS_Main.FIELDBYNAME('MJID').AsString)));

            sql.Add(' update WFB_MJJY_PB set MJStr4=''已出库'' where MJID=' + Quotedstr(Trim(CDS_Main.FIELDBYNAME('MJID').AsString)));

            ExecSQL;
//        ShowMessage(sql.Text);
          end;
          CDS_Main.Delete;
        end;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
    Exit;
  except
    MovePanel2.Visible := False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！', '提示', 0);
  end;

end;

procedure TfrmPBKCsel.C_CodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBKCsel.ConNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(Trim(ConNo.Text)) < 4 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        SQL.Add('select A.*,B.ConNo,C.C_CodeName,C.C_Code,C.C_Color,D.APXH ');
        sql.add('from CK_PBCP_CR A ');
        Sql.add(' inner join Cloth_Main B on A.MainId=B.MainId');
        Sql.add(' inner join Cloth_Sub C on A.MainId=C.MainId');
        Sql.add(' inner join WFB_MJJY_PB D on A.MJId=D.MJId');
        sql.add('where B.ConNo like :ConNo');
        SQL.Add(' and CRFlag=''出库'' ');
        Parameters.ParamByName('ConNo').Value := '%' + Trim(ConNo.Text) + '%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain, CDS_Main);
      SInitCDSData20(ADOQueryMain, CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmPBKCsel.CheJianChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBKCsel.ToolButton1Click(Sender: TObject);
var
  FCKOrdNo: string;
begin
  if GetLSNo(ADOQueryTemp, FCKOrdNo, 'BF', 'CK_PBCP_CR', 4, 1) = False then
  begin
    Application.MessageBox('生成流水号异常！', '提示', 0);
    exit;
  end;
  edit1.TEXT := FCKOrdNo;
end;

end.

