unit U_WeiXiuList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP,ShellAPI,IniFiles, cxCheckBox, cxCalendar,
  cxButtonEdit, cxTextEdit, cxPC, cxCheckComboBox, cxDropDownEdit, Menus,
  RM_e_Xls, TeEngine, Series, TeeProcs, Chart, DbChart;

type
  TfrmWeiXiuList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    TBADD: TToolButton;
    v2C_CodeName: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2ssel: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    v2Column11: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v2Column17: TcxGridDBColumn;
    Panel1: TPanel;
    Label2: TLabel;
    Label6: TLabel;
    Label5: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    WLName: TEdit;
    CDS_PRT: TClientDataSet;
    RMXLSExport2: TRMXLSExport;
    RMDBMain: TRMDBDataSet;
    v2Column3: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    v2Column18: TcxGridDBColumn;
    v2Column19: TcxGridDBColumn;
    v2Column20: TcxGridDBColumn;
    v2Column21: TcxGridDBColumn;
    v2Column22: TcxGridDBColumn;
    v2Column23: TcxGridDBColumn;
    v2Column24: TcxGridDBColumn;
    v2Column25: TcxGridDBColumn;
    v2Column26: TcxGridDBColumn;
    v2Column27: TcxGridDBColumn;
    v2Column28: TcxGridDBColumn;
    v2Column29: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    Label1: TLabel;
    v2Column12: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBADDClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure CustomerChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure v2Column8PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    function  SaveData():Boolean;
    procedure SetStatus();
    procedure setGrid();
  public
    fFlag:integer;
    { Public declarations }
    RKFlag,FCYID,fmanage:String;

  end;

var
  frmWeiXiuList: TfrmWeiXiuList;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}

procedure TfrmWeiXiuList.setGrid();
var
  i:Integer;
begin
  try

    case cxTabControl1.TabIndex of
      0:begin
          if Trim(CDS_HZ.FieldByName('Filler').AsString)=Trim(DName) then
          begin
            for i:=0 to tv2.ColumnCount-1 do
            begin
              tv2.Columns[i].Options.Editing:=true;
            end;
          end
          else
          begin
            for i:=0 to tv2.ColumnCount-1 do
            begin
              IF uppercase(tv2.Columns[i].DataBinding.FieldName)<>'SSEL' then
              tv2.Columns[i].Options.Editing:=true;
            end;
            v2Column7.Options.Editing:=True;
          end;
        end;
      1:begin
          for i:=0 to tv2.ColumnCount-1 do
          begin
            IF uppercase(tv2.Columns[i].DataBinding.FieldName)<>'SSEL' then
            tv2.Columns[i].Options.Editing:=false;
          end;
          v2Column7.Options.Editing:=True;
        end;
      2:begin
          for i:=0 to tv2.ColumnCount-1 do
          begin
            IF uppercase(tv2.Columns[i].DataBinding.FieldName)<>'SSEL' then
            tv2.Columns[i].Options.Editing:=false;
          end;
          v2Column7.Options.Editing:=True;
        end;
    end;
  except

  end
end;
procedure TfrmWeiXiuList.SetStatus();
var
  i:integer;
begin
  TBADD.Visible:=false;
  TBDel.Visible:=false;
  Tchk.Visible:=false;
  TNochk.Visible:=false;
  IF fmanage='高权限' then
  begin
    case cxTabControl1.TabIndex of
    0:begin
        Tchk.Visible:=true;
        TBADD.Visible:=true;
        TBDel.Visible:=true;
      end;
    1:begin
        TNochk.Visible:=true;
      end;
    end;
  end
  else
  IF fmanage='查询' then
  begin
    case cxTabControl1.TabIndex of
    0:begin

      end;
    1:begin
        
      end;
    end;
  end
  else
  begin
    case cxTabControl1.TabIndex of
    0:begin
        TBADD.Visible:=true;
        TBDel.Visible:=true;
      end;
    1:begin
        
      end;
    end;
  end;    
end;

procedure TfrmWeiXiuList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered:=False;
      sql.Add(' select A.* from  WeiXiuList A ');
      sql.Add('where A.Filltime>='''+FormatDateTime('yyyy-MM-dd',begdate.DateTime)+''' ');
      sql.Add(' and A.Filltime<'''+FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1)+''' ');
      IF cxTabControl1.TabIndex<2 then
      sql.Add(' and ISNULL(A.status,0)='''+inttostr(cxTabControl1.TabIndex)+''' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmWeiXiuList.FormDestroy(Sender: TObject);
begin
  frmWeiXiuList:=nil;
end;

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

procedure TfrmWeiXiuList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption),Tv2,'机物料管理');
  Close;
end;
procedure TfrmWeiXiuList.TBDelClick(Sender: TObject);
var
  FBBMoney,CRID:string;
begin
  if CDS_HZ.IsEmpty then Exit;
  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        IF Fieldbyname('ssel').AsBoolean then
        begin
          IF fieldbyname('Filler').AsString<>Trim(DName) then
          begin
            EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('别人的数据不能删除!','提示',0);
            exit;
          end;
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('delete WuLiaoList where WXID='''+Trim(CDS_HZ.fieldbyname('WXID').AsString)+'''');
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

procedure TfrmWeiXiuList.FormShow(Sender: TObject);
begin
  ReadCxGrid(Trim(Self.Caption),Tv2,'机物料管理');
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=Enddate.DateTime-30;
  SetStatus();
  InitGrid();
end;

procedure TfrmWeiXiuList.TBRafreshClick(Sender: TObject);
begin
  WLName.SetFocus;
  InitGrid();
end;

procedure TfrmWeiXiuList.ToolButton2Click(Sender: TObject);
var
  sql:string;
begin
  if ADOQueryMain.Active then
  begin
    sql:=SGetFilters(Panel1,1,2);
    SDofilter(ADOQueryMain,sql);
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
    if fmanage='查询' then
    begin
      with CDS_HZ do
      begin
        DisableControls;
         while not Eof do
         begin
           Edit;
           FieldByName('FactoryName').Value:='';
           Post;
           Next;
         end;
         EnableControls;
      end;  
    end;
  end;
end;

function TfrmWeiXiuList.SaveData():Boolean;
var
  maxId,CRID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'Wx','WeiXiuList',4,1)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from WeiXiuList where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('WxID').Value:=Trim(maxId);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('Filltime').Value:=formatdateTIme('yyyy-MM-dd',SGetServerDate(ADOQueryTemp));
      FieldByName('status').Value:='0';
      FieldByName('WxPerSon').Value:=Trim(DName);
      FieldByName('Wxtime').Value:=formatdateTIme('yyyy-MM-dd',SGetServerDate(ADOQueryTemp));
      Post;
    end;
    with Self.CDS_HZ do
    begin
      Append;
      FieldByName('WxID').Value:=Trim(maxId);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('Filltime').Value:=SGetServerDate(ADOQueryTemp);
      FieldByName('WxPerSon').Value:=Trim(DName);
      FieldByName('Wxtime').Value:=SGetServerDate(ADOQueryTemp);
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
end;

procedure TfrmWeiXiuList.TBADDClick(Sender: TObject);
begin
  Self.SaveData();
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmWeiXiuList.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,trim(Self.Caption));
end;

procedure TfrmWeiXiuList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid;
end;

procedure TfrmWeiXiuList.TchkClick(Sender: TObject);
begin
  IF CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        IF Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update WeiXiuList SET status=''1'' ');
            sql.Add(' ,Chker='''+Trim(DName)+'''');
            sql.Add(' ,Chktime='''+formatdateTIme('yyyy-MM-dd',SGetServerDate(ADOQueryTemp))+'''');
            sql.Add(' where WXID ='+quotedstr(trim(CDS_HZ.fieldbyname('WXID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

procedure TfrmWeiXiuList.TnochkClick(Sender: TObject);
begin
  IF CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        IF Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update WeiXiuList SET status=''0'' ,Chker=null,Chktime=null ');
            sql.Add('where WXID ='+quotedstr(trim(CDS_HZ.fieldbyname('WXID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！','提示信息',0);
  end;
end;

procedure TfrmWeiXiuList.N1Click(Sender: TObject);
begin
  IF  CDS_HZ.IsEmpty then exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=true;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmWeiXiuList.N2Click(Sender: TObject);
begin
 IF  CDS_HZ.IsEmpty then exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=false;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmWeiXiuList.Tv2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  setGrid();
end;

procedure TfrmWeiXiuList.CustomerChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmWeiXiuList.FormCreate(Sender: TObject);
begin
  fmanage:=Trim(DParameters1);
end;

procedure TfrmWeiXiuList.v2Column8PropertiesEditValueChanged(
  Sender: TObject);
 var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value:=Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate WeiXiuList ');
      sql.Add(' Set '+FFieldName+'='''+Trim(mvalue)+'''');
      SQL.Add(',Editer='''+Trim(DName)+'''');
      SQL.Add(',Edittime=getdate() ');
      SQL.Add(' where WXID='''+Trim(CDS_HZ.fieldbyname('WXID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end
end;

end.
