unit U_EquipmentRklist;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxButtonEdit, cxDropDownEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxContainer,
  cxTextEdit, cxCurrencyEdit, cxCheckBox, RM_System, RM_Dataset, RM_Common,
  RM_Class, RM_GridReport, cxPC, Menus, RM_e_Xls;

type
  TfrmEquipmentRklist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Qty: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRType: TcxGridDBColumn;
    v1CRTime: TcxGridDBColumn;
    v1Filler: TcxGridDBColumn;
    v1FillTime: TcxGridDBColumn;
    Label2: TLabel;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ckName: TComboBox;
    Label6: TLabel;
    v1INoutNo: TcxGridDBColumn;
    Label7: TLabel;
    inoutNo: TEdit;
    gangNo: TEdit;
    Label10: TLabel;
    RMDB_Main: TRMDBDataSet;
    v1Column4: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    v1Column10: TcxGridDBColumn;
    Label17: TLabel;
    FactoryName: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column2: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    ADOQueryMain: TADOQuery;
    v1Column1: TcxGridDBColumn;
    Label14: TLabel;
    P_CodeName: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure P_CodeNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
    procedure InitForm();
    { Private declarations }
  public
    fCKName, canshu1: string;
    fCRType: string;
    { Public declarations }
  end;

var
  frmEquipmentRklist: TfrmEquipmentRklist;

implementation

uses
  U_DataLink, U_Fun10, U_Fun, U_ZDYHelp, U_EquipmentRk;   
{$R *.dfm}

procedure TfrmEquipmentRklist.InitForm();
var
  fsj: string;
begin
  readCxGrid(self.Caption, Tv1, '仓储管理');
  SetStatus();
  fsj := 'select distinct(CRType) name from CK_WarpRoller_CR where CKName=''空轴'' and CRFlag=''入库''  and Valid=''Y''  ';
  SInitComBoxBySql(ADOQueryCmd, CRType, False, fsj);
end;

procedure TfrmEquipmentRklist.SetStatus();
begin
  TBEdit.Visible := False;
  TBDel.Visible := False;

  if Trim(canshu1) = '管理' then
  begin
    TBEdit.Visible := true;
    TBDel.Visible := true;
  end
  else
  begin

  end;
end;

procedure TfrmEquipmentRklist.InitGrid();
begin
  try
    with ADOQueryMain do
    begin
      close;
      sql.Clear;
      sql.Add('select A.* from CK_Equipment_CR A');
      sql.Add(' where A.CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.Add(' and  A.CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      sql.Add(' and CKName=''设备'' and A.CRFlag=''入库''  and A.Valid=''Y''  ');
      open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    TBFind.Click;
  finally
  end;
end;

procedure TfrmEquipmentRklist.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cafree;
end;

procedure TfrmEquipmentRklist.FormDestroy(Sender: TObject);
begin
  frmEquipmentRklist := nil;   
end;

procedure TfrmEquipmentRklist.TBAddClick(Sender: TObject);
begin
  frmEquipmentRk := TfrmEquipmentRk.create(self);
  with frmEquipmentRk do
  begin
    fkeyNo := '';
    fType := 0;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('crNo', trim(fkeyNo), []);
    end;
    free;
  end;
end;

procedure TfrmEquipmentRklist.P_CodeNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, CDS_Main);
  SInitCDSData20(ADOQueryMain, CDS_Main);
end;

procedure TfrmEquipmentRklist.FormShow(Sender: TObject);
begin
  InitForm();
  InitGrid();
end;

procedure TfrmEquipmentRklist.FormCreate(Sender: TObject);
begin
  EndDate.Date := SGetServerDate10(ADOQueryTmp);
  BegDate.Date := EndDate.Date - 30;
end;

procedure TfrmEquipmentRklist.TBCloseClick(Sender: TObject);
begin
  writeCxGrid(self.Caption, Tv1, '仓储管理');
  close;
end;

procedure TfrmEquipmentRklist.TBEditClick(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;

  frmEquipmentRk := TfrmEquipmentRk.create(self);
  with frmEquipmentRk do
  begin
    fkeyNo := self.cds_main.fieldbyname('CRNO').AsString;
    fType := 1;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('crNo', trim(fkeyNo), []);
    end;
    free;
  end;
end;

procedure TfrmEquipmentRklist.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.IsEmpty then
    Exit;
  if Trim(CDS_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
  begin
    Application.MessageBox('不能操作他人数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    if Trim(CDS_Main.FieldByName('CRNO').AsString) <> '' then
    begin
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('update CK_Equipment_CR set Valid=''N'' where CRNO=' + quotedstr(Trim(CDS_Main.FieldByName('CRNO').AsString)));
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('exec P_Up_Equipment ' + quotedstr(Trim(CDS_Main.FieldByName('CRNO').AsString)));
        Open;
      end;
      if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('出库数量超出入库数量!', '提示', 0);
        Exit;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Main.Delete;
    end;
  except
    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

procedure TfrmEquipmentRklist.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

procedure TfrmEquipmentRklist.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

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

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

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

end.

