unit U_CKProductBCPKCList;

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, MovePanel, Menus, cxCheckBox, Clipbrd, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmCKProductBCPKCListPB = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    C_CodeName: TEdit;
    C_Code: TEdit;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Label5: TLabel;
    ConNo: TEdit;
    Label6: TLabel;
    MJID: TEdit;
    v1Column6: TcxGridDBColumn;
    Label7: TLabel;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Panel4: TPanel;
    Label11: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    MovePanel2: TMovePanel;
    v1Column2: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label8: TLabel;
    MJStr3: TComboBox;
    v1Column7: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label1: TLabel;
    KuWei: TEdit;
    v1Column13: TcxGridDBColumn;
    Label12: TLabel;
    CheJian: TComboBox;
    v1Column15: TcxGridDBColumn;
    Label9: TLabel;
    CarNo: TEdit;
    Label10: TLabel;
    APBatchNo: TEdit;
    CPType: TComboBox;
    v1Column16: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    B1: TToolButton;
    cxgrdbclmnv1Column14: TcxGridDBColumn;
    ToolButton1: TToolButton;
    tbSHCK: TToolButton;
    ToolButton2: TToolButton;
    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 C_CodeNameChange(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure Image2Click(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure CheJianChange(Sender: TObject);
    procedure B1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure tbSHCKClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    FLeft, FTop: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
    FMainid, FSubId, FConNo, FColor, FCodeName: string;
    canshu1: string;
  end;

var
  frmCKProductBCPKCListPB: TfrmCKProductBCPKCListPB;

implementation

uses
  U_DataLink, U_Fun, U_SysLogHelp;

{$R *.dfm}

procedure TfrmCKProductBCPKCListPB.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPKCListPB := nil;
end;

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

procedure TfrmCKProductBCPKCListPB.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 := '';
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKProductBCPKCListPB.TBRafreshClick(Sender: TObject);
begin
 // BegDate.SetFocus;
  ToolBar1.SetFocus;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

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

procedure TfrmCKProductBCPKCListPB.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('仓库库存列表PB', Tv1, '坯布仓库');
  Close;
end;

procedure TfrmCKProductBCPKCListPB.FormShow(Sender: TObject);
var
  fsj: string;
begin
  if canshu1 <> '管理' then
  begin
    B1.Visible := False;
    tbSHCK.Visible := False;
  end;

  fsj := 'select distinct Code=Null,Name=MCCJ from Machine order by MCCJ ';
  SInitComBoxBySql(ADOQueryTemp, CheJian, False, fsj);
  ReadCxGrid('仓库库存列表PB', Tv1, '坯布仓库');
  InitGrid();
end;

procedure TfrmCKProductBCPKCListPB.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('库存列表', cxGrid2);
end;

procedure TfrmCKProductBCPKCListPB.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 TfrmCKProductBCPKCListPB.C_CodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPKCListPB.Panel10MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND, $F012, 0);
end;

procedure TfrmCKProductBCPKCListPB.Image2Click(Sender: TObject);
begin
  Panel4.Visible := False;
end;

procedure TfrmCKProductBCPKCListPB.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

procedure TfrmCKProductBCPKCListPB.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  {if Trim(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName)<>'CDQK' then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select CDQK=dbo.F_Get_Order_SubStr(:MJID,''MJCDHZSL'')');
    Parameters.ParamByName('MJID').Value:=Trim(CDS_Main.fieldbyname('MJID').AsString);
    Open;
  end;
  with CDS_Main do
  begin
    Edit;
    FieldByName('CDQK').Value:=Trim(ADOQueryTemp.fieldbyname('CDQK').AsString);
    Post;
  end; }
  Panel4.Left := FLeft;
  Panel4.Top := FTop + 110;
  Panel4.Visible := True;
  Panel4.Refresh;
  Panel10.Caption := Trim(TV1.Controller.FocusedColumn.Caption);

  RichEdit1.Text := CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString;
end;

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

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

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

procedure TfrmCKProductBCPKCListPB.B1Click(Sender: TObject);
var
  str: string;
begin

  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  str := '';
  if InputQuery('请输入批号', '批号', str) then
  begin
    try
      ADOQueryCmd.Connection.BeginTrans;
      with CDS_Main do
      begin
        while Locate('SSel', True, []) do
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_PBCP_CR where  CRFlag=''出库'' and MJID=' + quotedstr(Trim(CDS_Main.fieldbyname('MJID').AsString)));
            Open;
          end;
          if ADOQueryTemp.RecordCount > 0 then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('已出库不能撤销入库!', '提示', 0);
            Exit;
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_PBCP_CR where  CRFlag=''预出库'' and MJID=' + quotedstr(Trim(CDS_Main.fieldbyname('MJID').AsString)));
            Open;
          end;
          if ADOQueryTemp.RecordCount > 0 then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('已预出库不能撤销入库!', '提示', 0);
            Exit;
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
//            sql.Add('update CK_PBCP_CR set APBatchNo='+quotedstr(Trim(str))+' where MJID=' + quotedstr(Trim(CDS_Main.fieldbyname('MJID').AsString)));
            sql.Add('update WFB_MJJY_PB set APBatchNo=' + quotedstr(Trim(str)) + ' where MJID=' + quotedstr(Trim(CDS_Main.fieldbyname('MJID').AsString)));
            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(CDS_Main.FieldByName('APBatchNo').AsString) + ' 条码：' + trim(CDS_Main.FieldByName('MJID').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;

          CDS_Main.Edit;
          CDS_Main.FieldByName('SSel').Value := False;
          CDS_Main.FieldByName('APBatchNo').Value := str;
        end;
      end;
      ADOQueryCmd.Connection.CommitTrans;

    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('修改失败!', '提示', 0);
    end;
  end;

end;

procedure TfrmCKProductBCPKCListPB.ToolButton1Click(Sender: TObject);
begin
  frmSysLogHelp := TfrmSysLogHelp.create(self);
  with frmSysLogHelp do
  begin
    fModel := self.caption;
  //  facction:='生产指示单删除';
    showmodal;
    free;
  end;
end;

procedure TfrmCKProductBCPKCListPB.tbSHCKClick(Sender: TObject);
var
  CKOrderNo: string;
begin
  CKOrderNo := 'SHCK' + Trim(FormatDateTime('yyyyMMdd', Now));
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      while Locate('SSel', True, []) do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_PBCP_CR where  CRFlag=''出库'' and MJID=' + quotedstr(Trim(CDS_Main.fieldbyname('MJID').AsString)));
          Open;
        end;
        if ADOQueryTemp.RecordCount > 0 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('已出库不能撤销入库!', '提示', 0);
          Exit;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_PBCP_CR where  CRFlag=''预出库'' and MJID=' + quotedstr(Trim(CDS_Main.fieldbyname('MJID').AsString)));
          Open;
        end;
        if ADOQueryTemp.RecordCount > 0 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('已预出库不能撤销入库!', '提示', 0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('exec P_Do_PBSHCK  @CKOrdNo=' + quotedstr(trim(CKOrderNo)));
          sql.Add(', @MJID=' + quotedstr(trim(CDS_Main.fieldbyname('MJID').asstring)));
          sql.Add(', @DName=' + quotedstr(trim(DName)));
          ExecSQL;
        end;

        CDS_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;

  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('修改失败!', '提示', 0);
  end;
end;

procedure TfrmCKProductBCPKCListPB.ToolButton2Click(Sender: TObject);
var
  CKOrderNo: string;
begin
  CKOrderNo := 'SHCK' + Trim(FormatDateTime('yyyyMMdd', Now));
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      while Locate('SSel', True, []) do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_PBCP_CR where  CRFlag=''出库'' and MJID=' + quotedstr(Trim(CDS_Main.fieldbyname('MJID').AsString)));
          Open;
        end;
        if ADOQueryTemp.RecordCount > 0 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('已出库不能撤销入库!', '提示', 0);
          Exit;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_PBCP_CR where  CRFlag=''预出库'' and MJID=' + quotedstr(Trim(CDS_Main.fieldbyname('MJID').AsString)));
          Open;
        end;
        if ADOQueryTemp.RecordCount > 0 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('已预出库不能撤销入库!', '提示', 0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('exec P_Do_PBCK @CRFlag=''直接出库'', @CKOrdNo=' + quotedstr(trim(CKOrderNo)));
          sql.Add(', @MJID=' + quotedstr(trim(CDS_Main.fieldbyname('MJID').asstring)));
          sql.Add(', @DName=' + quotedstr(trim(DName)));
          ExecSQL;
        end;

        CDS_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;

  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('修改失败!', '提示', 0);
  end;
end;

end.

