unit U_YGYPInPut_CLDJ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxDropDownEdit,jpeg,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, cxPC,
  cxGridCustomPopupMenu, cxGridPopupMenu, Menus, ExtDlgs;

type
  TfrmYGYPInPut_CLDJ = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    TBSave: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1CLName: TcxGridDBColumn;
    v1CLNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    ADOQuery2: TADOQuery;
    TADD: TToolButton;
    TDel: TToolButton;
    v1CLBXDate: TcxGridDBColumn;
    v1CLNO: TcxGridDBColumn;
    v1filltime: TcxGridDBColumn;
    v1BXDate: TcxGridDBColumn;
    v1BYQCQty: TcxGridDBColumn;
    v1BYLCQty: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure v1BGUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TaddClick(Sender: TObject);
    procedure TDelClick(Sender: TObject);
    procedure v1CLNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    TPInt:Integer;
    procedure InitData();
    function  SaveData():Boolean;
    { Private declarations }
  public
    PState,CopyInt:Integer;
    FCLID:String;
    { Public declarations }
  end;

var
  frmYGYPInPut_CLDJ: TfrmYGYPInPut_CLDJ;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel,U_YGYPZZChkList_BGYPLYSQ;

{$R *.dfm}

procedure TfrmYGYPInPut_CLDJ.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmYGYPInPut_CLDJ.InitData();
begin
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from OA_YG_CLGL  ');
    sql.add('where CLID='''+trim(FCLID)+'''');
    Open;
  end;
  SCreateCDS20(ADOCmd,Order_Sub);
  SInitCDSData20(ADOCmd,Order_Sub);
end;

procedure TfrmYGYPInPut_CLDJ.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmYGYPInPut_CLDJ.SaveData():Boolean;
var
  maxno:String;
begin
  Result:=False;
  try
    ADOCmd.Connection.BeginTrans;
    with Order_Sub do
    begin
      first;
      while not eof do
      begin
        if trim(FCLID)='' then
        begin
          if GetLSNo(ADOCmd,FCLID,'CL','OA_YG_CLGL',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败!','提示',0);
            Exit;
          end;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from OA_YG_CLGL where CLID='''+Trim(FCLID)+'''');
          Open;
          if isempty then
          begin
            append;
          end
          else
          edit;
          fieldbyname('CLID').Value:=trim(FCLID);
          fieldbyname('CLStatus').Value:='未使用';
          fieldbyname('BYQCQty').AsFloat:=order_Sub.fieldbyname('BYQCQty').AsFloat;
          fieldbyname('BYLCQty').AsFloat:=order_Sub.fieldbyname('BYLCQty').AsFloat;
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'OA_YG_CLGL',0);
          post;
        end;
        next;
      end;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update OA_YG_CLGL set ');
      sql.add('MQQty=BYQCQty+(select isnull(Sum(LCQty),0) from OA_YG_CLGL_Sub where CLID=OA_YG_CLGL.CLID and CLStatus<>''保养完成'' ) ');
      sql.Add('where CLID='''+trim(order_Sub.fieldbyname('CLID').asstring)+''' ');
      execsql;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmYGYPInPut_CLDJ.TBSaveClick(Sender: TObject);
var
  FSFNO:String;
begin
  ToolBar1.SetFocus;
  if order_Sub.isempty then
  begin
    Application.MessageBox('明细不能为空','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    Modalresult:=1;
  end;
end;

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

procedure TfrmYGYPInPut_CLDJ.FormDestroy(Sender: TObject);
begin
  frmYGYPInPut_CLDJ:=nil;
end;

procedure TfrmYGYPInPut_CLDJ.v1BGUnitPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='BGUnit';
    flagname:='数量单位';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('BGUnit').Value:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_CLDJ.TaddClick(Sender: TObject);
begin
  if Order_Sub.Locate('CLName','',[])=true then
  begin
    application.MessageBox('车辆名称不能为空','提示');
    exit;
  end;
  with Order_Sub do
  begin
    append;
    fieldbyname('filltime').AsDateTime:=SGetServerDate(ADOCmd);
    fieldbyname('CLBXDate').AsDateTime:=SGetServerDate(ADOCmd);
    fieldbyname('BXDate').AsDateTime:=SGetServerDate(ADOCmd);
    post;
  end;
end;

procedure TfrmYGYPInPut_CLDJ.TDelClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty then exit;
  if trim(Order_Sub.fieldbyname('CLID').AsString)<>'' then
  begin
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from OA_YG_CLGL ');
      sql.add('where CLID='''+trim(Order_Sub.fieldbyname('CLID').AsString)+'''');
      execsql;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmYGYPInPut_CLDJ.v1CLNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='CLName';
    flagname:='车辆名称';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('CLName').Value:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

end.
