unit U_YGYPZZChkList_CLWX;

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_CLWX = 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;
    TBADD: TToolButton;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    TCXTJ: TToolButton;
    BegDate: TDateTimePicker;
    Label3: TLabel;
    EndDate: TDateTimePicker;
    DataSource2: TDataSource;
    order_Sub: TClientDataSet;
    Label2: TLabel;
    TTiJiao: TToolButton;
    TUpdate: TToolButton;
    TDel: TToolButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    ADOQuery1: TADOQuery;
    Label1: TLabel;
    CLName: TEdit;
    Label4: TLabel;
    CLNO: TEdit;
    cxTabControl1: TcxTabControl;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    V2Chker: TcxGridDBColumn;
    V2Chktime: TcxGridDBColumn;
    V2Chkstatus: TcxGridDBColumn;
    V2chkNote: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1WXDate: TcxGridDBColumn;
    v1SXName: TcxGridDBColumn;
    v1CLName: TcxGridDBColumn;
    v1CLNO: TcxGridDBColumn;
    v1WXMoney: TcxGridDBColumn;
    v1WXFactory: TcxGridDBColumn;
    v1WXXianMu: TcxGridDBColumn;
    v1WXNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxStyle2: TcxStyle;
    TGHCL: TToolButton;
    Panel4: TPanel;
    Label6: TLabel;
    Panel5: TPanel;
    Image1: TImage;
    TSure: TButton;
    ETaiTou: TEdit;
    Label5: TLabel;
    Label7: TLabel;
    WXMoney: TEdit;
    v1WXStatus: TcxGridDBColumn;
    v1TCDate: TcxGridDBColumn;
    TCDate: TDateTimePicker;
    ADOQuery2: TADOQuery;
    Label8: TLabel;
    BYLCQty: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    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 TBADDClick(Sender: TObject);
    procedure TCXTJClick(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TUpdateClick(Sender: TObject);
    procedure TTiJiaoClick(Sender: TObject);
    procedure TDelClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure CLNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TGHCLClick(Sender: TObject);
    procedure TSureClick(Sender: TObject);
    procedure Image1Click(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure setstatus();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    { Public declarations }
  end;

var
  frmYGYPZZChkList_CLWX: TfrmYGYPZZChkList_CLWX;

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

{$R *.dfm}

procedure TfrmYGYPZZChkList_CLWX.setstatus();
begin
  TUpdate.Visible:=False;
  TTiJiao.Visible:=False;
  TCXTJ.Visible:=False;
  Tdel.Visible:=False;
  TGHCL.Visible:=False;
  case cxTabControl1.TabIndex of
    0:begin
        TUpdate.Visible:=True;
        TTiJiao.Visible:=True;
        Tdel.Visible:=true;
      end;
    1:begin
        TCXTJ.Visible:=true;
        TGHCL.Visible:=true;
      end;
    2:begin

      end;
    3:begin
      end;
  end;
end;

procedure TfrmYGYPZZChkList_CLWX.FormDestroy(Sender: TObject);
begin
  frmYGYPZZChkList_CLWX:=nil;
end;

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

procedure TfrmYGYPZZChkList_CLWX.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
end;

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

procedure TfrmYGYPZZChkList_CLWX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select * ');
      sql.Add(' from OA_YG_CLGLWX ');
      sql.Add(' where WXDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
      sql.Add(' and WXDate<='''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and (WXStatus=''未提交'' or WXStatus=''不通过'') ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.add(' and (isnull(WXStatus,'''')=''已送审'' or isnull(WXStatus,'''')=''申请通过'')');
      end;
      if cxTabControl1.TabIndex=2 then
      begin
        sql.add(' and isnull(WXStatus,'''')=''已提车'' ');
      end;
      if trim(canshu1)<>'高权限' then
      begin
        sql.Add(' and SXName='''+trim(DName)+''' ');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    with ADOQueryCmd do
    begin
      close;
      sql.clear;
      sql.add('select A.* from OA_Chk A ');
      sql.add('where A.Mainid='''+trim(Order_Main.fieldbyname('CLWXID').AsString)+''' ');
      sql.Add('and isnull(OAType,'''')=''车辆管理''');
      open;
    end;
    SCreateCDS20(ADOQueryCmd,Order_Sub);
    SInitCDSData20(ADOQueryCmd,Order_Sub);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYGYPZZChkList_CLWX.InitForm();
begin
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-7;
  ReadCxGrid('公司车辆列表',Tv1,'OA管理');
  //setstatus();
  if trim(CanShu1)='查询' then
  begin
    TBADD.Visible:=False;
    TUpdate.Visible:=false;
    TDel.Visible:=False;
  end
  else
  begin
    TBADD.Visible:=true;
    TUpdate.Visible:=true;
    TDel.Visible:=true;
  end;
  InitGrid();
end;

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

function TfrmYGYPZZChkList_CLWX.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_CLWX.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('员工加薪列表',cxGrid1);
end;

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

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

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

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

procedure TfrmYGYPZZChkList_CLWX.TBViewClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  frmYGYPInPut_CLWXDJ:=TfrmYGYPInPut_CLWXDJ.create(self);
  with frmYGYPInPut_CLWXDJ do
  begin
    ToolBar1.Visible:=False;
    FCLID:=trim(Order_Main.fieldbyname('CLWXID').AsString);
    if showModal=1 then
    begin

    end;
    free;
  end;
end;

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

procedure TfrmYGYPZZChkList_CLWX.TBADDClick(Sender: TObject);
begin
  frmYGYPInPut_CLWXDJ:=TfrmYGYPInPut_CLWXDJ.create(self);
  with frmYGYPInPut_CLWXDJ do
  begin
    FCLID:='';
    if showModal=1 then
    begin

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

procedure TfrmYGYPZZChkList_CLWX.TCXTJClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要执行撤销操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete from OA_Chk ');
    sql.Add(' where Mainid='''+Order_Main.fieldbyname('CLWXID').AsString+'''');
    sql.Add(' and isnull(OAType,'''')=''车辆管理'' ');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update OA_YG_CLGLWX Set WXStatus=''未提交'' ');
    sql.Add(' where CLWXID='''+trim(Order_Main.fieldbyname('CLWXID').AsString)+'''');
    ExecSQL;
  end;
  initGrid();
end;

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

procedure TfrmYGYPZZChkList_CLWX.TUpdateClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if trim(Order_Main.fieldbyname('SXName').AsString)<>trim(DName) then
  begin
    application.MessageBox('不能修改他人数据','提示');
    exit;
  end;
  frmYGYPInPut_CLWXDJ:=TfrmYGYPInPut_CLWXDJ.create(self);
  with frmYGYPInPut_CLWXDJ do
  begin
    FCLID:=trim(Order_Main.fieldbyname('CLWXID').AsString);
    if showModal=1 then
    begin

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

procedure TfrmYGYPZZChkList_CLWX.TTiJiaoClick(Sender: TObject);
var maxno: string;
begin
  if Order_Main.IsEmpty then exit;
  frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
  with frmZDYHelpSel do
  begin
    flag:='SPR';
    flagname:='审批人';
    MainType:=Trim(DName);
    if ShowModal=1 then
    begin
      with ClientDataSet1 do
      begin
        first;
        while not Eof do
        begin
          if fieldbyname('Ssel').AsBoolean=True then
          begin
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from SY_User where UserName='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
              Open;
            end;
            if ADOQueryTemp.IsEmpty then
            begin
              Application.MessageBox('审批人定义错误!','提示',0);
              Exit;
            end;
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from OA_Chk where MainId='''+Trim(Order_Main.fieldbyname('CLWXID').AsString)+'''');
              sql.Add(' and isnull(OAType,'''')=''车辆管理'' ');
              sql.Add(' and isnull(Chker,'''')='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
              Open;
            end;
            if ADOQueryTemp.IsEmpty then
            begin
              if GetLSNo(ADOQueryCmd,maxno,'WX','OA_Chk',4,1)=False then
              begin
                Application.MessageBox('取最大号失败!','提示',0);
                Exit;
              end;
              with ADOQueryCmd do
              begin
                Close;
                sql.Clear;
                sql.Add('select * from OA_Chk where 1=2 ');
                Open;
                append;
                FieldByName('CKID').Value:=Trim(maxno);
                FieldByName('Mainid').Value:=Trim(Order_Main.fieldbyname('CLWXID').AsString);
                FieldByName('OAType').value:='车辆管理';
                FieldByName('Chker').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
                Post;
              end;
            end;
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('update OA_YG_CLGLWX set WXStatus=''已送审'' ');
              sql.Add('where CLWXID='''+trim(Order_Main.fieldbyname('CLWXID').asstring)+''' ');
              ExecSQL;
            end;
          end;
          Next;
        end;
      end;
    end;
    Free;
  end;
  application.MessageBox('提交成功','提示');
  InitGrid();
end;

procedure TfrmYGYPZZChkList_CLWX.TDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if trim(Order_Main.fieldbyname('SXName').AsString)<>trim(DName) then
  begin
    application.MessageBox('不能删除他人数据','提示');
    exit;
  end;
  if application.MessageBox('确定要删除吗','提示',1)=2 then exit;
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('delete from OA_YG_CLGLWX where CLWXID='''+trim(Order_Main.fieldbyname('CLWXID').AsString)+'''');
    execsql;
  end;
  with ADOQuery2 do
  begin
    close;
    sql.Clear;
    sql.add('update OA_YG_CLGL set ');
    sql.add('CLStatus=''未使用'' ');
    sql.Add(' where CLID='''+trim(Order_Main.fieldbyname('CLID').AsString)+'''');
    execsql;
  end;
  initGrid();
end;


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

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

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

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

procedure TfrmYGYPZZChkList_CLWX.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQueryCmd do
  begin
    close;
    sql.clear;
    sql.add('select A.* from OA_Chk A ');
    sql.add('where A.Mainid='''+trim(Order_Main.fieldbyname('CLWXID').AsString)+''' ');
    sql.Add('and isnull(OAType,'''')=''车辆管理''');
    open;
  end;
  SCreateCDS20(ADOQueryCmd,Order_Sub);
  SInitCDSData20(ADOQueryCmd,Order_Sub);
end;

procedure TfrmYGYPZZChkList_CLWX.TGHCLClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if order_Main.FieldByName('WXStatus').AsString<>'申请通过' then
  begin
    application.MessageBox('单据未通过不能提车','提示');
    exit;
  end;
  TCDate.DateTime:=SGetServerDate(ADOQueryTemp);
  WXMoney.Text:=Order_Main.fieldbyname('WXMoney').AsString;
  Panel4.Visible:=true;
end;

procedure TfrmYGYPZZChkList_CLWX.TSureClick(Sender: TObject);
var fSubid: string;
begin
  if trim(WXMoney.Text)='' then
  begin
    application.MessageBox('维修金额不能为空','提示');
    exit;
  end;
  if BYLCQty.Text='' then
  begin
    application.MessageBox('里程数不能为空','提示');
    exit;
  end;
  if application.MessageBox('确定执行此操作吗','提示',1)=2 then exit;
  if GetLSNo(ADOQueryCmd,FSubid,'WS','OA_YG_CLGL_Sub',4,1)=False then
  begin
    Application.MessageBox('取最大号失败!','提示',0);
    Exit;
  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(order_Main.fieldbyname('SXName').asstring);
    fieldbyname('CLName').AsString:=trim(Order_Main.fieldbyname('CLName').AsString);
    fieldbyname('CLID').AsString:=trim(Order_Main.fieldbyname('CLID').AsString);
    if trim(Order_Main.fieldbyname('WXStatus').AsString)='年检到期' then
    fieldbyname('CLStatus').AsString:='年检完成';
    if trim(Order_Main.fieldbyname('WXStatus').AsString)='保险到期' then
    fieldbyname('CLStatus').AsString:='保险完成';
    if trim(Order_Main.fieldbyname('WXStatus').AsString)='需保养' then
    fieldbyname('CLStatus').AsString:='保养完成';
    if trim(Order_Main.fieldbyname('WXStatus').AsString)='已使用' then
    fieldbyname('CLStatus').AsString:='归还完成';
    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_CLGLWX set WXMoney='''+trim(WXMoney.Text)+''',TCDate='''+trim(formatdatetime('yyyy-MM-dd',TCDate.DateTime))+''',WXStatus=''已提车'' ');
    sql.Add('where CLWXID='''+trim(Order_Main.fieldbyname('CLWXID').asstring)+''' ');
    ExecSQL;
  end;
  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();
  Panel4.Visible:=False;
end;

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

end.
