unit U_YGYPZZChkList_CLDJ;

interface

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

type
  TfrmYGYPZZChkList_CLDJ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    TBView: TToolButton;
    Panel1: TPanel;
    TBLiZhi: TToolButton;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    TCXBF: TToolButton;
    BegDate: TDateTimePicker;
    Label3: TLabel;
    EndDate: TDateTimePicker;
    DataSource2: TDataSource;
    Label2: TLabel;
    TBF: TToolButton;
    TCXSQ: TToolButton;
    TDel: TToolButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    ADOQuery1: TADOQuery;
    Label1: TLabel;
    CLName: TEdit;
    Label4: TLabel;
    CLNO: TEdit;
    TSsel: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1CLName: TcxGridDBColumn;
    v1CLBXDate: TcxGridDBColumn;
    v1CLNO: TcxGridDBColumn;
    v1CLNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CLStatus: TcxGridDBColumn;
    v1Ssel: TcxGridDBColumn;
    v1BXDate: TcxGridDBColumn;
    TNJWC: TToolButton;
    TBXWC: TToolButton;
    Panel4: TPanel;
    Label6: TLabel;
    Label8: TLabel;
    Panel5: TPanel;
    Image1: TImage;
    TSure: TButton;
    CLBXDate: TDateTimePicker;
    Label5: TLabel;
    CLStatus: TEdit;
    cxTabControl1: TcxTabControl;
    v1MQQty: TcxGridDBColumn;
    v1BYLCQty: TcxGridDBColumn;
    TBY: TToolButton;
    BYLCQty: TEdit;
    TGH: TToolButton;
    Label7: TLabel;
    v1MDPlace: TcxGridDBColumn;
    v1YGName: TcxGridDBColumn;
    Label9: TLabel;
    SYMoney: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure TBViewClick(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure TBLiZhiClick(Sender: TObject);
    procedure TCXBFClick(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TCXSQClick(Sender: TObject);
    procedure TBFClick(Sender: TObject);
    procedure TDelClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure CLNameChange(Sender: TObject);
    procedure TSselClick(Sender: TObject);
    procedure TNJWCClick(Sender: TObject);
    procedure TSureClick(Sender: TObject);
    procedure TBXWCClick(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBYClick(Sender: TObject);
    procedure TGHClick(Sender: TObject);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure setstatus();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    canshu1:string;
    { Public declarations }
  end;

var
  frmYGYPZZChkList_CLDJ: TfrmYGYPZZChkList_CLDJ;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp, U_YGYPInPut,
  U_ZDYHelpSel,U_YGYPInput_CLDJ;

{$R *.dfm}

procedure TfrmYGYPZZChkList_CLDJ.setstatus();
begin
  TSsel.Visible:=False;
  v1Ssel.Visible:=False;
  TBLizhi.Visible:=False;
  TCXSQ.Visible:=false;
  TDel.Visible:=False;
  TNJWC.Visible:=False;
  TBXWC.Visible:=False;
  TBF.Visible:=False;
  TCXBF.Visible:=False;
  TGH.Visible:=False;
  TBY.Visible:=False;
  cxtabControl1.Visible:=False;
  if trim(CanShu1)='查询' then
  begin
    TSsel.Visible:=true;
    v1Ssel.Visible:=true;
  end
  else
  begin
    cxtabControl1.Visible:=true;
    case cxTabControl1.TabIndex of
    0:begin
        TBLiZhi.Visible:=True;
        TCXSQ.Visible:=True;
        TNJWC.Visible:=true;
        TBXWC.Visible:=True;
        TBF.Visible:=True;
        Tdel.Visible:=true;
        TGH.Visible:=true;
        TBY.Visible:=true;
      end;
    1:begin
        TCXBF.Visible:=True;
      end;
    2:begin

      end;
    end;
  end;
end;

procedure TfrmYGYPZZChkList_CLDJ.FormDestroy(Sender: TObject);
begin
  frmYGYPZZChkList_CLDJ:=nil;
end;

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

procedure TfrmYGYPZZChkList_CLDJ.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('公司车辆列表1',Tv1,'OA管理');
end;

procedure TfrmYGYPZZChkList_CLDJ.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select * ');
      sql.add(',YGName=(select top 1 A.SQName from OA_YG_CLGLSQ A where A.CLID=B.CLID and B.CLStatus=''已使用'' and isnull(A.SQStatus,'''')=''申请通过'' order by A.SQChkTime desc)');
      sql.add(',MDPlace=(select top 1 A.TOPlace from OA_YG_CLGLSQ A where A.CLID=B.CLID and B.CLStatus=''已使用'' and isnull(A.SQStatus,'''')=''申请通过'' order by A.SQChkTime desc)');
      sql.Add(' from OA_YG_CLGL B');
      sql.Add(' where 1=1 ');
      if trim(canShu1)='查询' then
      begin
        sql.Add('and B.CLStatus=''未使用'' ');
      end
      else
      begin
        if CXTabControl1.TabIndex=0 then
        sql.add('and B.CLStatus<>''已报废'' ');
        if CXTabControl1.TabIndex=1 then
        sql.add('and B.CLStatus=''已报废'' ');
      end;
      sql.add('order by B.CLStatus');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYGYPZZChkList_CLDJ.InitForm();
begin
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-7;
  ReadCxGrid('公司车辆列表1',Tv1,'OA管理');
  setstatus();
  InitGrid();
end;

procedure TfrmYGYPZZChkList_CLDJ.TBFindClick(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

function TfrmYGYPZZChkList_CLDJ.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Delete OA_YG_DangAn where YGId='''+Trim(Order_Main.fieldbyname('YGId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Delete OA_YG_DangAn_Other  where YGId='''+Trim(Order_Main.fieldbyname('YGId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Delete TP_File  where WBId='''+Trim(Order_Main.fieldbyname('YGId').AsString)+'''');
      sql.Add(' and TFType=''YG'' ');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmYGYPZZChkList_CLDJ.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('员工加薪列表',cxGrid1);
end;

procedure TfrmYGYPZZChkList_CLDJ.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmYGYPZZChkList_CLDJ.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmYGYPZZChkList_CLDJ.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmYGYPZZChkList_CLDJ.TBViewClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  frmYGYPInPut_CLDJ:=TfrmYGYPInPut_CLDJ.create(self);
  with frmYGYPInPut_CLDJ do
  begin
    ToolBar1.Visible:=False;
    FCLID:=trim(Order_Main.fieldbyname('CLID').AsString);
    if showModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmYGYPZZChkList_CLDJ.CustomerNoNameChange(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmYGYPZZChkList_CLDJ.TBLiZhiClick(Sender: TObject);
begin
  frmYGYPInPut_CLDJ:=TfrmYGYPInPut_CLDJ.create(self);
  with frmYGYPInPut_CLDJ do
  begin
    FCLID:='';
    v1CLBXDate.Options.Editing:=true;
    v1BXDate.Options.Editing:=true;
    v1BYLCQty.Options.Editing:=true;
    if showModal=1 then
    begin

    end;
    free;
  end;
  initGrid();
end;

procedure TfrmYGYPZZChkList_CLDJ.TCXBFClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update OA_YG_CLGL set CLStatus=''未使用'' ');
    sql.Add('where CLID='''+trim(Order_Main.fieldbyname('CLID').asstring)+''' ');
    ExecSQL;
  end;
  initGrid();
end;

procedure TfrmYGYPZZChkList_CLDJ.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  TBView.Click;
end;

procedure TfrmYGYPZZChkList_CLDJ.TCXSQClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  frmYGYPInPut_CLDJ:=TfrmYGYPInPut_CLDJ.create(self);
  with frmYGYPInPut_CLDJ do
  begin
    FCLID:=trim(Order_Main.fieldbyname('CLID').AsString);
    v1CLBXDate.Options.Editing:=False;
    v1BXDate.Options.Editing:=False;
    v1BYLCQty.Options.Editing:=False;
    if showModal=1 then
    begin

    end;
    free;
  end;
  initGrid();
end;

procedure TfrmYGYPZZChkList_CLDJ.TBFClick(Sender: TObject);
var maxno: string;
begin
  if Order_Main.IsEmpty then exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update OA_YG_CLGL set CLStatus=''已报废'' ');
    sql.Add('where CLID='''+trim(Order_Main.fieldbyname('CLID').asstring)+''' ');
    ExecSQL;
  end;
  InitGrid();
end;

procedure TfrmYGYPZZChkList_CLDJ.TDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  ToolBar1.SetFocus;
  if application.MessageBox('确定要删除吗','提示',1)=2 then exit;
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('delete from OA_YG_CLGL where CLID='''+trim(Order_Main.fieldbyname('CLID').AsString)+'''');
    execsql;
  end;
  initGrid();
end;


procedure TfrmYGYPZZChkList_CLDJ.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,True);
end;

procedure TfrmYGYPZZChkList_CLDJ.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main,False);
end;

procedure TfrmYGYPZZChkList_CLDJ.CLNameChange(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmYGYPZZChkList_CLDJ.TSselClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if order_Main.Locate('Ssel',true,[])=False then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  Modalresult:=1;
end;

procedure TfrmYGYPZZChkList_CLDJ.TNJWCClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.FieldByName('CLStatus').AsString<>'年检到期' then
  begin
    application.MessageBox('车辆年检未到期,不能操作','提示');
    exit;
  end;
  Panel5.Caption:='年检信息登记';
  label8.Caption:='年检日期';
  Label7.Caption:='里 程 数';
  CLBXDate.DateTime:=Now;
  Panel4.Visible:=True;
end;

procedure TfrmYGYPZZChkList_CLDJ.TSureClick(Sender: TObject);
var FSubid,FYGName,FTOPlace,FSQNote: string;
begin
  if BYLCQty.Text='' then
  begin
    application.MessageBox('里程数不能为空','提示');
    exit;
  end;
  if SYMoney.Text='' then
  SYMoney.Text:='0';
  if GetLSNo(ADOQueryCmd,FSubid,'CS','OA_YG_CLGL_Sub',4,1)=False then
  begin
    Application.MessageBox('取最大号失败!','提示',0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select top 1 * ');
    sql.Add(' from OA_YG_CLGLSQ ');
    sql.add(' where CLID='''+trim(Order_Main.fieldbyname('CLID').AsString)+''' ');
    sql.add(' and isnull(SQStatus,'''')=''申请通过''');
    sql.add(' order by SQChkTime desc');
    open;
  end;
  if ADOQueryCmd.IsEmpty=False then
  begin
    FYGName:=ADOQueryCmd.fieldbyname('SQName').asstring;
    FTOPlace:=ADOQueryCmd.fieldbyname('TOPlace').asstring;
    FSQNote:=ADOQueryCmd.fieldbyname('SQNote').asstring;
  end
  else
  begin
    FYGName:=trim(DName);
    FTOPlace:='';
  end;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from OA_YG_CLGL_Sub ');
    sql.add('where 1=2');
    open;
    append;
    fieldbyname('CLSubid').AsString:=trim(FSubid);
    fieldbyname('YGName').AsString:=trim(FYGName);
    fieldbyname('TOPlace').AsString:=trim(FToplace);
    fieldbyname('SQNote').AsString:=trim(FSQNote);
    fieldbyname('CLName').AsString:=trim(Order_Main.fieldbyname('CLName').AsString);
    fieldbyname('CLID').AsString:=trim(Order_Main.fieldbyname('CLID').AsString);
    if trim(Order_Main.fieldbyname('CLStatus').AsString)='年检到期' then
    fieldbyname('CLStatus').AsString:='年检完成';
    if trim(Order_Main.fieldbyname('CLStatus').AsString)='保险到期' then
    fieldbyname('CLStatus').AsString:='保险完成';
    if trim(Order_Main.fieldbyname('CLStatus').AsString)='需保养' then
    fieldbyname('CLStatus').AsString:='保养完成';
    if trim(Order_Main.fieldbyname('CLStatus').AsString)='已使用' then
    fieldbyname('CLStatus').AsString:='归还完成';
    fieldbyname('SYMoney').Value:=trim(SYMoney.text);
    fieldbyname('filler').AsString:=trim(DName);
    fieldbyname('filltime').AsDateTime:=now;
    fieldbyname('CLNO').asstring:=trim(order_Main.fieldbyname('CLNO').AsString);
    fieldbyname('LCQty').Value:=trim(BYLCQty.Text);
    fieldbyname('GHDate').AsDateTime:=now;
    post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update OA_YG_CLGL set CLStatus=''未使用'' ');
    if Label8.Caption='年检日期' then
    begin
      sql.Add(',CLBXDate='''+trim(formatdatetime('yyyy-MM-dd',CLBXDate.DateTime))+''' ');
      sql.add(',MQQty=BYQCQty+(select isnull(Sum(LCQty),0) from OA_YG_CLGL_Sub where CLID=OA_YG_CLGL.CLID and CLStatus<>''保养完成'')');
    end;
    if Label8.Caption='保险日期' then
    begin
      sql.Add(',BXDate='''+trim(formatdatetime('yyyy-MM-dd',CLBXDate.DateTime))+''' ');
      sql.add(',MQQty=BYQCQty+(select isnull(Sum(LCQty),0) from OA_YG_CLGL_Sub where CLID=OA_YG_CLGL.CLID and CLStatus<>''保养完成'')');
    end;
    if Label8.Caption='保养日期' then
    begin
      sql.Add(',BYDate='''+trim(formatdatetime('yyyy-MM-dd',CLBXDate.DateTime))+''' ');
      sql.Add(',BYLCQty='''+trim(BYLCQty.Text)+''' ');
    end;
    if Label8.Caption='归还日期' then
    begin
      sql.add(',MQQty=BYQCQty+(select isnull(Sum(LCQty),0) from OA_YG_CLGL_Sub where CLID=OA_YG_CLGL.CLID and CLStatus<>''保养完成'')');
    end;
    sql.Add('where CLID='''+trim(Order_Main.fieldbyname('CLID').asstring)+''' ');
    ExecSQL;
  end;
  initGrid();
  Panel4.Visible:=False;
end;

procedure TfrmYGYPZZChkList_CLDJ.TBXWCClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.FieldByName('CLStatus').AsString<>'保险到期' then
  begin
    application.MessageBox('车辆保险未到期,不能操作','提示');
    exit;
  end;
  Panel5.Caption:='保险信息登记';
  label8.Caption:='保险日期';
  Label7.Caption:='里 程 数';
  CLBXDate.DateTime:=Now;
  Panel4.Visible:=True;
end;

procedure TfrmYGYPZZChkList_CLDJ.Image1Click(Sender: TObject);
begin
  Panel4.Visible:=False;
end;

procedure TfrmYGYPZZChkList_CLDJ.cxTabControl1Change(Sender: TObject);
begin
  setstatus();
  initGrid();
end;

procedure TfrmYGYPZZChkList_CLDJ.TBYClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.FieldByName('CLStatus').AsString<>'需保养' then
  begin
    application.MessageBox('车辆未到保养里程数,不能操作','提示');
    exit;
  end;
  Panel5.Caption:='保养信息登记';
  label8.Caption:='保养日期';
  Label7.Caption:='保养里程';
  CLBXDate.DateTime:=Now;
  Panel4.Visible:=True;
end;

procedure TfrmYGYPZZChkList_CLDJ.TGHClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.FieldByName('CLStatus').AsString<>'已使用' then
  begin
    application.MessageBox('车辆未使用,不能归还','提示');
    exit;
  end;
  Panel5.Caption:='归还信息登记';
  label8.Caption:='归还日期';
  Label7.Caption:='里 程 数';
  CLBXDate.DateTime:=Now;
  Panel4.Visible:=True;
end;

end.
