unit U_EquipmentCklist;

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, RM_Common,
  RM_Class, RM_e_Xls, RM_GridReport, RM_System, RM_Dataset, cxTextEdit, cxPC,
  cxCheckBox, Menus;

type
  TfrmEquipmentCklist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: 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;
    v1CRNO: TcxGridDBColumn;
    v1Filler: TcxGridDBColumn;
    v1FillTime: TcxGridDBColumn;
    Label2: TLabel;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOPrint: TADOQuery;
    RMXLSExport1: TRMXLSExport;
    v1inoutNO: TcxGridDBColumn;
    inoutNo: TEdit;
    Label7: TLabel;
    v1custName: TcxGridDBColumn;
    RMDB_Main: TRMDBDataSet;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    cxStyle_fontclTeal: TcxStyle;
    cxStyle_fontclOlive: TcxStyle;
    Label8: TLabel;
    ToFactoryName: TEdit;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    v1Column5: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column1: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure OrderNoChange(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 v1Column6CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitForm();
    procedure SetStatus();
    { Private declarations }
  public
    canshu1, canshu2: string;
    { Public declarations }
  end;

var
  frmEquipmentCklist: TfrmEquipmentCklist;

implementation

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

procedure TfrmEquipmentCklist.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 TfrmEquipmentCklist.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 TfrmEquipmentCklist.InitGrid();
begin
  try
    with ADOQueryMain do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.* ');
      SQL.Add(' from CK_Equipment_CR A ');
      sql.Add(' where CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.Add(' and CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      sql.Add(' and CKName=''设备'' and CRFlag=''出库'' and A.valid=''Y'' ');
      open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
  end;
end;

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

procedure TfrmEquipmentCklist.FormDestroy(Sender: TObject);
begin
  frmEquipmentCklist := nil;
end;

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

procedure TfrmEquipmentCklist.OrderNoChange(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 TfrmEquipmentCklist.FormShow(Sender: TObject);
begin
  InitForm();
  InitGrid();
end;

procedure TfrmEquipmentCklist.FormCreate(Sender: TObject);
begin
  EndDate.Date := SGetServerDate10(ADOQueryTmp);
  BegDate.Date := EndDate.Date - 30;
  cxGrid1.Align := alclient;
  canshu1 := trim(DParameters1);
end;

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

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

  if (cds_main.fieldbyname('Filler').AsString <> trim(DName)) and (trim(canshu2) <> '高权限') then
  begin
    application.MessageBox('操作员不正确，不能修改！', '提示信息', MB_ICONERROR);
    exit;
  end;

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

procedure TfrmEquipmentCklist.TBDelClick(Sender: TObject);
var
  YFID, CRID, note: string;
begin
  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
        CDS_Main.EnableControls;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('出库数量超出库存数量!', '提示', 0);
        Exit;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Main.Delete;
    end;
  except
    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

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

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

procedure TfrmEquipmentCklist.v1Column6CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
var
  colIdx0, colIdx1: integer;
begin
  colIdx0 := tv1.GetColumnByFieldName('CRNO').Index;
  if (ARow1.Values[colIdx0] = ARow2.Values[colIdx0]) then
    AAreEqual := True
  else
    AAreEqual := False;

end;

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

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

end.

